TransferUtils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. var supportedTypes = ['native', 'erc20', 'erc721', 'erc1155'];
  2. var ERC1155TransferABI = [{
  3. inputs: [{
  4. internalType: 'address',
  5. name: 'from',
  6. type: 'address'
  7. }, {
  8. internalType: 'address',
  9. name: 'to',
  10. type: 'address'
  11. }, {
  12. internalType: 'uint256',
  13. name: 'id',
  14. type: 'uint256'
  15. }, {
  16. internalType: 'uint256',
  17. name: 'value',
  18. type: 'uint256'
  19. }, {
  20. internalType: 'bytes',
  21. name: 'data',
  22. type: 'bytes'
  23. }],
  24. outputs: [{
  25. name: '',
  26. type: 'bool'
  27. }],
  28. name: 'safeTransferFrom',
  29. type: 'function',
  30. constant: false,
  31. payable: false
  32. }, {
  33. inputs: [{
  34. internalType: 'address',
  35. name: 'from',
  36. type: 'address'
  37. }, {
  38. internalType: 'address',
  39. name: 'to',
  40. type: 'address'
  41. }, {
  42. internalType: 'uint256',
  43. name: 'id',
  44. type: 'uint256'
  45. }, {
  46. internalType: 'uint256',
  47. name: 'value',
  48. type: 'uint256'
  49. }],
  50. outputs: [{
  51. name: '',
  52. type: 'bool'
  53. }],
  54. name: 'transferFrom',
  55. type: 'function',
  56. constant: false,
  57. payable: false
  58. }];
  59. var ERC721TransferABI = [{
  60. inputs: [{
  61. internalType: 'address',
  62. name: 'from',
  63. type: 'address'
  64. }, {
  65. internalType: 'address',
  66. name: 'to',
  67. type: 'address'
  68. }, {
  69. internalType: 'uint256',
  70. name: 'tokenId',
  71. type: 'uint256'
  72. }],
  73. outputs: [{
  74. name: '',
  75. type: 'bool'
  76. }],
  77. name: 'safeTransferFrom',
  78. type: 'function',
  79. constant: false,
  80. payable: false
  81. }, {
  82. inputs: [{
  83. internalType: 'address',
  84. name: 'from',
  85. type: 'address'
  86. }, {
  87. internalType: 'address',
  88. name: 'to',
  89. type: 'address'
  90. }, {
  91. internalType: 'uint256',
  92. name: 'tokenId',
  93. type: 'uint256'
  94. }],
  95. outputs: [{
  96. name: '',
  97. type: 'bool'
  98. }],
  99. name: 'transferFrom',
  100. type: 'function',
  101. constant: false,
  102. payable: false
  103. }];
  104. var ERC20TransferABI = [{
  105. constant: false,
  106. inputs: [{
  107. name: '_to',
  108. type: 'address'
  109. }, {
  110. name: '_value',
  111. type: 'uint256'
  112. }],
  113. name: 'transfer',
  114. outputs: [{
  115. name: '',
  116. type: 'bool'
  117. }],
  118. payable: false,
  119. stateMutability: 'nonpayable',
  120. type: 'function'
  121. }, {
  122. constant: true,
  123. inputs: [{
  124. name: '_owner',
  125. type: 'address'
  126. }],
  127. name: 'balanceOf',
  128. outputs: [{
  129. name: 'balance',
  130. type: 'uint256'
  131. }],
  132. payable: false,
  133. stateMutability: 'view',
  134. type: 'function'
  135. }];
  136. var tokenParams = {
  137. native: {
  138. receiver: '',
  139. amount: ''
  140. },
  141. erc20: {
  142. contractAddress: '',
  143. receiver: '',
  144. amount: ''
  145. },
  146. erc721: {
  147. contractAddress: '',
  148. receiver: '',
  149. tokenId: ''
  150. },
  151. erc1155: {
  152. contractAddress: '',
  153. receiver: '',
  154. tokenId: '',
  155. amount: ''
  156. }
  157. };
  158. var isNotEmpty = function (value) {
  159. return typeof value !== 'undefined' && value ? true : false;
  160. };
  161. var validateInput = function (type, payload) {
  162. var errors = [];
  163. var parameters = tokenParams[type];
  164. for (var _i = 0, _Object$keys = Object.keys(parameters); _i < _Object$keys.length; _i++) {
  165. var key = _Object$keys[_i];
  166. if (!isNotEmpty(payload[key])) {
  167. errors.push(key + " is required");
  168. }
  169. }
  170. if (errors.length > 0) {
  171. throw errors;
  172. }
  173. };
  174. var isSupportedType = function (type) {
  175. if (supportedTypes.indexOf(type) === -1) throw 'Unsupported type';
  176. return true;
  177. };
  178. var isUint256 = function (tokenId) {
  179. if (!Number.isInteger(+tokenId) || +tokenId < 0) throw new Error('Invalid token Id');
  180. return true;
  181. };
  182. module.exports = {
  183. abi: {
  184. erc1155: ERC1155TransferABI,
  185. erc721: ERC721TransferABI,
  186. erc20: ERC20TransferABI
  187. },
  188. validateInput: validateInput,
  189. isSupportedType: isSupportedType,
  190. isNotEmpty: isNotEmpty,
  191. isUint256: isUint256
  192. };