TransferUtils.js 4.0 KB

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