TransferUtils.js 3.4 KB

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