starknet 4.12.0 → 4.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/.eslintrc +1 -0
  2. package/CHANGELOG.md +14 -0
  3. package/__tests__/account.test.ts +0 -2
  4. package/dist/index.d.ts +2683 -24
  5. package/dist/index.global.js +22994 -0
  6. package/dist/index.global.js.map +1 -0
  7. package/dist/index.js +4713 -53
  8. package/dist/index.js.map +1 -0
  9. package/dist/index.mjs +4655 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/index.d.ts +2683 -24
  12. package/index.global.js +22994 -0
  13. package/index.global.js.map +1 -0
  14. package/index.js +4713 -53
  15. package/index.js.map +1 -0
  16. package/index.mjs +4655 -0
  17. package/index.mjs.map +1 -0
  18. package/package.json +8 -2
  19. package/src/account/default.ts +43 -8
  20. package/src/account/interface.ts +25 -2
  21. package/src/constants.ts +0 -1
  22. package/src/provider/default.ts +3 -0
  23. package/src/provider/interface.ts +2 -0
  24. package/src/provider/rpc.ts +4 -4
  25. package/src/provider/sequencer.ts +4 -1
  26. package/src/types/lib.ts +1 -1
  27. package/src/types/provider.ts +5 -0
  28. package/src/utils/ellipticCurve.ts +3 -1
  29. package/src/utils/fetchPonyfill.ts +3 -2
  30. package/src/utils/hash.ts +3 -2
  31. package/src/utils/number.ts +2 -2
  32. package/tsup.config.ts +10 -0
  33. package/account/default.d.ts +0 -30
  34. package/account/default.js +0 -526
  35. package/account/index.d.ts +0 -2
  36. package/account/index.js +0 -18
  37. package/account/interface.d.ts +0 -161
  38. package/account/interface.js +0 -27
  39. package/constants.d.ts +0 -36
  40. package/constants.js +0 -2067
  41. package/contract/contractFactory.d.ts +0 -31
  42. package/contract/contractFactory.js +0 -102
  43. package/contract/default.d.ts +0 -108
  44. package/contract/default.js +0 -640
  45. package/contract/index.d.ts +0 -3
  46. package/contract/index.js +0 -19
  47. package/contract/interface.d.ts +0 -80
  48. package/contract/interface.js +0 -9
  49. package/dist/account/default.d.ts +0 -30
  50. package/dist/account/default.js +0 -526
  51. package/dist/account/index.d.ts +0 -2
  52. package/dist/account/index.js +0 -18
  53. package/dist/account/interface.d.ts +0 -161
  54. package/dist/account/interface.js +0 -27
  55. package/dist/constants.d.ts +0 -36
  56. package/dist/constants.js +0 -2067
  57. package/dist/contract/contractFactory.d.ts +0 -31
  58. package/dist/contract/contractFactory.js +0 -102
  59. package/dist/contract/default.d.ts +0 -108
  60. package/dist/contract/default.js +0 -640
  61. package/dist/contract/index.d.ts +0 -3
  62. package/dist/contract/index.js +0 -19
  63. package/dist/contract/interface.d.ts +0 -80
  64. package/dist/contract/interface.js +0 -9
  65. package/dist/provider/default.d.ts +0 -37
  66. package/dist/provider/default.js +0 -207
  67. package/dist/provider/errors.d.ts +0 -9
  68. package/dist/provider/errors.js +0 -40
  69. package/dist/provider/index.d.ts +0 -7
  70. package/dist/provider/index.js +0 -24
  71. package/dist/provider/interface.d.ts +0 -203
  72. package/dist/provider/interface.js +0 -9
  73. package/dist/provider/rpc.d.ts +0 -86
  74. package/dist/provider/rpc.js +0 -605
  75. package/dist/provider/sequencer.d.ts +0 -79
  76. package/dist/provider/sequencer.js +0 -575
  77. package/dist/provider/utils.d.ts +0 -30
  78. package/dist/provider/utils.js +0 -84
  79. package/dist/signer/default.d.ts +0 -13
  80. package/dist/signer/default.js +0 -102
  81. package/dist/signer/index.d.ts +0 -2
  82. package/dist/signer/index.js +0 -18
  83. package/dist/signer/interface.d.ts +0 -60
  84. package/dist/signer/interface.js +0 -9
  85. package/dist/types/account.d.ts +0 -11
  86. package/dist/types/account.js +0 -2
  87. package/dist/types/api/index.d.ts +0 -10
  88. package/dist/types/api/index.js +0 -18
  89. package/dist/types/api/openrpc.d.ts +0 -572
  90. package/dist/types/api/openrpc.js +0 -45
  91. package/dist/types/api/rpc.d.ts +0 -43
  92. package/dist/types/api/rpc.js +0 -2
  93. package/dist/types/api/sequencer.d.ts +0 -320
  94. package/dist/types/api/sequencer.js +0 -2
  95. package/dist/types/contract.d.ts +0 -5
  96. package/dist/types/contract.js +0 -2
  97. package/dist/types/index.d.ts +0 -7
  98. package/dist/types/index.js +0 -35
  99. package/dist/types/lib.d.ts +0 -120
  100. package/dist/types/lib.js +0 -8
  101. package/dist/types/provider.d.ts +0 -99
  102. package/dist/types/provider.js +0 -2
  103. package/dist/types/signer.d.ts +0 -19
  104. package/dist/types/signer.js +0 -2
  105. package/dist/utils/address.d.ts +0 -5
  106. package/dist/utils/address.js +0 -41
  107. package/dist/utils/ellipticCurve.d.ts +0 -29
  108. package/dist/utils/ellipticCurve.js +0 -132
  109. package/dist/utils/encode.d.ts +0 -15
  110. package/dist/utils/encode.js +0 -96
  111. package/dist/utils/fetchPonyfill.d.ts +0 -2
  112. package/dist/utils/fetchPonyfill.js +0 -6
  113. package/dist/utils/hash.d.ts +0 -37
  114. package/dist/utils/hash.js +0 -162
  115. package/dist/utils/json.d.ts +0 -16
  116. package/dist/utils/json.js +0 -19
  117. package/dist/utils/merkle.d.ts +0 -10
  118. package/dist/utils/merkle.js +0 -89
  119. package/dist/utils/number.d.ts +0 -16
  120. package/dist/utils/number.js +0 -112
  121. package/dist/utils/provider.d.ts +0 -4
  122. package/dist/utils/provider.js +0 -38
  123. package/dist/utils/responseParser/index.d.ts +0 -11
  124. package/dist/utils/responseParser/index.js +0 -9
  125. package/dist/utils/responseParser/rpc.d.ts +0 -20
  126. package/dist/utils/responseParser/rpc.js +0 -44
  127. package/dist/utils/responseParser/sequencer.d.ts +0 -17
  128. package/dist/utils/responseParser/sequencer.js +0 -99
  129. package/dist/utils/shortString.d.ts +0 -4
  130. package/dist/utils/shortString.js +0 -26
  131. package/dist/utils/stark.d.ts +0 -16
  132. package/dist/utils/stark.js +0 -91
  133. package/dist/utils/transaction.d.ts +0 -20
  134. package/dist/utils/transaction.js +0 -79
  135. package/dist/utils/typedData/index.d.ts +0 -72
  136. package/dist/utils/typedData/index.js +0 -239
  137. package/dist/utils/typedData/types.d.ts +0 -32
  138. package/dist/utils/typedData/types.js +0 -2
  139. package/dist/utils/typedData/utils.d.ts +0 -8
  140. package/dist/utils/typedData/utils.js +0 -16
  141. package/dist/utils/uint256.d.ts +0 -11
  142. package/dist/utils/uint256.js +0 -28
  143. package/dist/utils/url.d.ts +0 -7
  144. package/dist/utils/url.js +0 -49
  145. package/provider/default.d.ts +0 -37
  146. package/provider/default.js +0 -207
  147. package/provider/errors.d.ts +0 -9
  148. package/provider/errors.js +0 -40
  149. package/provider/index.d.ts +0 -7
  150. package/provider/index.js +0 -24
  151. package/provider/interface.d.ts +0 -203
  152. package/provider/interface.js +0 -9
  153. package/provider/rpc.d.ts +0 -86
  154. package/provider/rpc.js +0 -605
  155. package/provider/sequencer.d.ts +0 -79
  156. package/provider/sequencer.js +0 -575
  157. package/provider/utils.d.ts +0 -30
  158. package/provider/utils.js +0 -84
  159. package/signer/default.d.ts +0 -13
  160. package/signer/default.js +0 -102
  161. package/signer/index.d.ts +0 -2
  162. package/signer/index.js +0 -18
  163. package/signer/interface.d.ts +0 -60
  164. package/signer/interface.js +0 -9
  165. package/types/account.d.ts +0 -11
  166. package/types/account.js +0 -2
  167. package/types/api/index.d.ts +0 -10
  168. package/types/api/index.js +0 -18
  169. package/types/api/openrpc.d.ts +0 -572
  170. package/types/api/openrpc.js +0 -45
  171. package/types/api/rpc.d.ts +0 -43
  172. package/types/api/rpc.js +0 -2
  173. package/types/api/sequencer.d.ts +0 -320
  174. package/types/api/sequencer.js +0 -2
  175. package/types/contract.d.ts +0 -5
  176. package/types/contract.js +0 -2
  177. package/types/index.d.ts +0 -7
  178. package/types/index.js +0 -35
  179. package/types/lib.d.ts +0 -120
  180. package/types/lib.js +0 -8
  181. package/types/provider.d.ts +0 -99
  182. package/types/provider.js +0 -2
  183. package/types/signer.d.ts +0 -19
  184. package/types/signer.js +0 -2
  185. package/utils/address.d.ts +0 -5
  186. package/utils/address.js +0 -41
  187. package/utils/ellipticCurve.d.ts +0 -29
  188. package/utils/ellipticCurve.js +0 -132
  189. package/utils/encode.d.ts +0 -15
  190. package/utils/encode.js +0 -96
  191. package/utils/fetchPonyfill.d.ts +0 -2
  192. package/utils/fetchPonyfill.js +0 -6
  193. package/utils/hash.d.ts +0 -37
  194. package/utils/hash.js +0 -162
  195. package/utils/json.d.ts +0 -16
  196. package/utils/json.js +0 -19
  197. package/utils/merkle.d.ts +0 -10
  198. package/utils/merkle.js +0 -89
  199. package/utils/number.d.ts +0 -16
  200. package/utils/number.js +0 -112
  201. package/utils/provider.d.ts +0 -4
  202. package/utils/provider.js +0 -38
  203. package/utils/responseParser/index.d.ts +0 -11
  204. package/utils/responseParser/index.js +0 -9
  205. package/utils/responseParser/rpc.d.ts +0 -20
  206. package/utils/responseParser/rpc.js +0 -44
  207. package/utils/responseParser/sequencer.d.ts +0 -17
  208. package/utils/responseParser/sequencer.js +0 -99
  209. package/utils/shortString.d.ts +0 -4
  210. package/utils/shortString.js +0 -26
  211. package/utils/stark.d.ts +0 -16
  212. package/utils/stark.js +0 -91
  213. package/utils/transaction.d.ts +0 -20
  214. package/utils/transaction.js +0 -79
  215. package/utils/typedData/index.d.ts +0 -72
  216. package/utils/typedData/index.js +0 -239
  217. package/utils/typedData/types.d.ts +0 -32
  218. package/utils/typedData/types.js +0 -2
  219. package/utils/typedData/utils.d.ts +0 -8
  220. package/utils/typedData/utils.js +0 -16
  221. package/utils/uint256.d.ts +0 -11
  222. package/utils/uint256.js +0 -28
  223. package/utils/url.d.ts +0 -7
  224. package/utils/url.js +0 -49
@@ -1,640 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __read = (this && this.__read) || function (o, n) {
50
- var m = typeof Symbol === "function" && o[Symbol.iterator];
51
- if (!m) return o;
52
- var i = m.call(o), r, ar = [], e;
53
- try {
54
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
- }
56
- catch (error) { e = { error: error }; }
57
- finally {
58
- try {
59
- if (r && !r.done && (m = i["return"])) m.call(i);
60
- }
61
- finally { if (e) throw e.error; }
62
- }
63
- return ar;
64
- };
65
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
66
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
67
- if (ar || !(i in from)) {
68
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
69
- ar[i] = from[i];
70
- }
71
- }
72
- return to.concat(ar || Array.prototype.slice.call(from));
73
- };
74
- var __importDefault = (this && this.__importDefault) || function (mod) {
75
- return (mod && mod.__esModule) ? mod : { "default": mod };
76
- };
77
- Object.defineProperty(exports, "__esModule", { value: true });
78
- exports.Contract = void 0;
79
- var bn_js_1 = __importDefault(require("bn.js"));
80
- var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
81
- var provider_1 = require("../provider");
82
- var number_1 = require("../utils/number");
83
- function parseFelt(candidate) {
84
- try {
85
- return (0, number_1.toBN)(candidate);
86
- }
87
- catch (e) {
88
- throw Error('Couldnt parse felt');
89
- }
90
- }
91
- /**
92
- * Adds call methods to the contract
93
- *
94
- */
95
- function buildCall(contract, functionAbi) {
96
- return function () {
97
- var args = [];
98
- for (var _i = 0; _i < arguments.length; _i++) {
99
- args[_i] = arguments[_i];
100
- }
101
- return __awaiter(this, void 0, void 0, function () {
102
- return __generator(this, function (_a) {
103
- return [2 /*return*/, contract.call(functionAbi.name, args)];
104
- });
105
- });
106
- };
107
- }
108
- /**
109
- * Adds invoke methods to the contract
110
- *
111
- */
112
- function buildInvoke(contract, functionAbi) {
113
- return function () {
114
- var args = [];
115
- for (var _i = 0; _i < arguments.length; _i++) {
116
- args[_i] = arguments[_i];
117
- }
118
- return __awaiter(this, void 0, void 0, function () {
119
- var inputs, inputsLength, options;
120
- return __generator(this, function (_a) {
121
- inputs = functionAbi.inputs;
122
- inputsLength = inputs.reduce(function (acc, input) {
123
- if (!/_len$/.test(input.name)) {
124
- return acc + 1;
125
- }
126
- return acc;
127
- }, 0);
128
- options = {};
129
- if (inputsLength + 1 === args.length && typeof args[args.length - 1] === 'object') {
130
- Object.assign(options, args.pop());
131
- }
132
- return [2 /*return*/, contract.invoke(functionAbi.name, args, options)];
133
- });
134
- });
135
- };
136
- }
137
- /**
138
- * Adds call/invoke methods to the contract
139
- *
140
- */
141
- function buildDefault(contract, functionAbi) {
142
- if (functionAbi.stateMutability === 'view') {
143
- return buildCall(contract, functionAbi);
144
- }
145
- return buildInvoke(contract, functionAbi);
146
- }
147
- /**
148
- * Adds populate for methods to the contract
149
- *
150
- */
151
- function buildPopulate(contract, functionAbi) {
152
- return function () {
153
- var args = [];
154
- for (var _i = 0; _i < arguments.length; _i++) {
155
- args[_i] = arguments[_i];
156
- }
157
- return contract.populate(functionAbi.name, args);
158
- };
159
- }
160
- /**
161
- * Adds estimateFee for methods to the contract
162
- *
163
- */
164
- function buildEstimate(contract, functionAbi) {
165
- return function () {
166
- var args = [];
167
- for (var _i = 0; _i < arguments.length; _i++) {
168
- args[_i] = arguments[_i];
169
- }
170
- return contract.estimate(functionAbi.name, args);
171
- };
172
- }
173
- var Contract = /** @class */ (function () {
174
- /**
175
- * Contract class to handle contract methods
176
- *
177
- * @param abi - Abi of the contract object
178
- * @param address (optional) - address to connect to
179
- * @param providerOrAccount (optional) - Provider or Account to attach to
180
- */
181
- function Contract(abi, address, providerOrAccount) {
182
- if (providerOrAccount === void 0) { providerOrAccount = provider_1.defaultProvider; }
183
- var _this = this;
184
- this.address = address && address.toLowerCase();
185
- this.providerOrAccount = providerOrAccount;
186
- this.abi = abi;
187
- this.structs = abi
188
- .filter(function (abiEntry) { return abiEntry.type === 'struct'; })
189
- .reduce(function (acc, abiEntry) {
190
- var _a;
191
- return (__assign(__assign({}, acc), (_a = {}, _a[abiEntry.name] = abiEntry, _a)));
192
- }, {});
193
- Object.defineProperty(this, 'functions', {
194
- enumerable: true,
195
- value: {},
196
- writable: false,
197
- });
198
- Object.defineProperty(this, 'callStatic', {
199
- enumerable: true,
200
- value: {},
201
- writable: false,
202
- });
203
- Object.defineProperty(this, 'populateTransaction', {
204
- enumerable: true,
205
- value: {},
206
- writable: false,
207
- });
208
- Object.defineProperty(this, 'estimateFee', {
209
- enumerable: true,
210
- value: {},
211
- writable: false,
212
- });
213
- this.abi.forEach(function (abiElement) {
214
- if (abiElement.type !== 'function') {
215
- return;
216
- }
217
- var signature = abiElement.name;
218
- if (!_this[signature]) {
219
- Object.defineProperty(_this, signature, {
220
- enumerable: true,
221
- value: buildDefault(_this, abiElement),
222
- writable: false,
223
- });
224
- }
225
- if (!_this.functions[signature]) {
226
- Object.defineProperty(_this.functions, signature, {
227
- enumerable: true,
228
- value: buildDefault(_this, abiElement),
229
- writable: false,
230
- });
231
- }
232
- if (!_this.callStatic[signature]) {
233
- Object.defineProperty(_this.callStatic, signature, {
234
- enumerable: true,
235
- value: buildCall(_this, abiElement),
236
- writable: false,
237
- });
238
- }
239
- if (!_this.populateTransaction[signature]) {
240
- Object.defineProperty(_this.populateTransaction, signature, {
241
- enumerable: true,
242
- value: buildPopulate(_this, abiElement),
243
- writable: false,
244
- });
245
- }
246
- if (!_this.estimateFee[signature]) {
247
- Object.defineProperty(_this.estimateFee, signature, {
248
- enumerable: true,
249
- value: buildEstimate(_this, abiElement),
250
- writable: false,
251
- });
252
- }
253
- });
254
- }
255
- Contract.prototype.attach = function (address) {
256
- this.address = address;
257
- };
258
- Contract.prototype.connect = function (providerOrAccount) {
259
- this.providerOrAccount = providerOrAccount;
260
- };
261
- Contract.prototype.deployed = function () {
262
- return __awaiter(this, void 0, void 0, function () {
263
- return __generator(this, function (_a) {
264
- switch (_a.label) {
265
- case 0:
266
- if (!this.deployTransactionHash) return [3 /*break*/, 2];
267
- return [4 /*yield*/, this.providerOrAccount.waitForTransaction(this.deployTransactionHash)];
268
- case 1:
269
- _a.sent();
270
- this.deployTransactionHash = undefined;
271
- _a.label = 2;
272
- case 2: return [2 /*return*/, this];
273
- }
274
- });
275
- });
276
- };
277
- Contract.prototype.call = function (method, args, _a) {
278
- if (args === void 0) { args = []; }
279
- var _b = _a === void 0 ? {} : _a, _c = _b.blockIdentifier, blockIdentifier = _c === void 0 ? 'pending' : _c;
280
- return __awaiter(this, void 0, void 0, function () {
281
- var inputs, calldata;
282
- var _this = this;
283
- return __generator(this, function (_d) {
284
- // ensure contract is connected
285
- (0, minimalistic_assert_1.default)(this.address !== null, 'contract is not connected to an address');
286
- // validate method and args
287
- this.validateMethodAndArgs('CALL', method, args);
288
- inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
289
- calldata = this.compileCalldata(args, inputs);
290
- return [2 /*return*/, this.providerOrAccount
291
- .callContract({
292
- contractAddress: this.address,
293
- calldata: calldata,
294
- entrypoint: method,
295
- }, blockIdentifier)
296
- .then(function (x) { return _this.parseResponse(method, x.result); })];
297
- });
298
- });
299
- };
300
- Contract.prototype.invoke = function (method, args, options) {
301
- if (args === void 0) { args = []; }
302
- if (options === void 0) { options = {}; }
303
- // ensure contract is connected
304
- (0, minimalistic_assert_1.default)(this.address !== null, 'contract is not connected to an address');
305
- // validate method and args
306
- this.validateMethodAndArgs('INVOKE', method, args);
307
- var inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
308
- var inputsLength = inputs.reduce(function (acc, input) {
309
- if (!/_len$/.test(input.name)) {
310
- return acc + 1;
311
- }
312
- return acc;
313
- }, 0);
314
- if (args.length !== inputsLength) {
315
- throw Error("Invalid number of arguments, expected ".concat(inputsLength, " arguments, but got ").concat(args.length));
316
- }
317
- // compile calldata
318
- var calldata = this.compileCalldata(args, inputs);
319
- var invocation = {
320
- contractAddress: this.address,
321
- calldata: calldata,
322
- entrypoint: method,
323
- };
324
- if ('execute' in this.providerOrAccount) {
325
- return this.providerOrAccount.execute(invocation, undefined, {
326
- maxFee: options.maxFee,
327
- nonce: options.nonce,
328
- });
329
- }
330
- if (!options.nonce) {
331
- throw new Error("Nonce is required when invoking a function without an account");
332
- }
333
- // eslint-disable-next-line no-console
334
- console.warn("Invoking ".concat(method, " without an account. This will not work on a public node."));
335
- return this.providerOrAccount.invokeFunction(__assign(__assign({}, invocation), { signature: options.signature || [] }), {
336
- nonce: options.nonce,
337
- });
338
- };
339
- Contract.prototype.estimate = function (method, args) {
340
- if (args === void 0) { args = []; }
341
- return __awaiter(this, void 0, void 0, function () {
342
- var invocation;
343
- var _a;
344
- return __generator(this, function (_b) {
345
- // ensure contract is connected
346
- (0, minimalistic_assert_1.default)(this.address !== null, 'contract is not connected to an address');
347
- // validate method and args
348
- this.validateMethodAndArgs('INVOKE', method, args);
349
- invocation = (_a = this.populateTransaction)[method].apply(_a, __spreadArray([], __read(args), false));
350
- if ('estimateInvokeFee' in this.providerOrAccount) {
351
- return [2 /*return*/, this.providerOrAccount.estimateInvokeFee(invocation)];
352
- }
353
- throw Error('Contract must be connected to the account contract to estimate');
354
- });
355
- });
356
- };
357
- Contract.prototype.populate = function (method, args) {
358
- if (args === void 0) { args = []; }
359
- var inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
360
- return {
361
- contractAddress: this.address,
362
- entrypoint: method,
363
- calldata: this.compileCalldata(args, inputs),
364
- };
365
- };
366
- /**
367
- * Deep parse of the object that has been passed to the method
368
- *
369
- * @param struct - struct that needs to be calculated
370
- * @return {number} - number of members for the given struct
371
- */
372
- Contract.prototype.calculateStructMembers = function (struct) {
373
- var _this = this;
374
- return this.structs[struct].members.reduce(function (acc, member) {
375
- if (member.type === 'felt') {
376
- return acc + 1;
377
- }
378
- return acc + _this.calculateStructMembers(member.type);
379
- }, 0);
380
- };
381
- /**
382
- * Validates if all arguments that are passed to the method are corresponding to the ones in the abi
383
- *
384
- * @param type - type of the method
385
- * @param method - name of the method
386
- * @param args - arguments that are passed to the method
387
- */
388
- Contract.prototype.validateMethodAndArgs = function (type, method, args) {
389
- var _this = this;
390
- if (args === void 0) { args = []; }
391
- // ensure provided method exists
392
- var invokeableFunctionNames = this.abi
393
- .filter(function (abi) {
394
- if (abi.type !== 'function')
395
- return false;
396
- var isView = abi.stateMutability === 'view';
397
- return type === 'INVOKE' ? !isView : isView;
398
- })
399
- .map(function (abi) { return abi.name; });
400
- (0, minimalistic_assert_1.default)(invokeableFunctionNames.includes(method), "".concat(type === 'INVOKE' ? 'invokeable' : 'viewable', " method not found in abi"));
401
- // ensure args match abi type
402
- var methodAbi = this.abi.find(function (abi) { return abi.name === method && abi.type === 'function'; });
403
- var argPosition = 0;
404
- methodAbi.inputs.forEach(function (input) {
405
- if (/_len$/.test(input.name)) {
406
- return;
407
- }
408
- if (input.type === 'felt') {
409
- (0, minimalistic_assert_1.default)(typeof args[argPosition] === 'string' ||
410
- typeof args[argPosition] === 'number' ||
411
- args[argPosition] instanceof bn_js_1.default, "arg ".concat(input.name, " should be a felt (string, number, BigNumber)"));
412
- argPosition += 1;
413
- }
414
- else if (input.type in _this.structs && typeof args[argPosition] === 'object') {
415
- if (Array.isArray(args[argPosition])) {
416
- var structMembersLength = _this.calculateStructMembers(input.type);
417
- (0, minimalistic_assert_1.default)(args[argPosition].length === structMembersLength, "arg should be of length ".concat(structMembersLength));
418
- }
419
- else {
420
- _this.structs[input.type].members.forEach(function (_a) {
421
- var name = _a.name;
422
- (0, minimalistic_assert_1.default)(Object.keys(args[argPosition]).includes(name), "arg should have a property ".concat(name));
423
- });
424
- }
425
- argPosition += 1;
426
- }
427
- else {
428
- (0, minimalistic_assert_1.default)(Array.isArray(args[argPosition]), "arg ".concat(input.name, " should be an Array"));
429
- if (input.type === 'felt*') {
430
- args[argPosition].forEach(function (felt) {
431
- (0, minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, "arg ".concat(input.name, " should be an array of string, number or BigNumber"));
432
- });
433
- argPosition += 1;
434
- }
435
- else if (/\(felt/.test(input.type)) {
436
- var tupleLength = input.type.split(',').length;
437
- (0, minimalistic_assert_1.default)(args[argPosition].length === tupleLength, "arg ".concat(input.name, " should have ").concat(tupleLength, " elements in tuple"));
438
- args[argPosition].forEach(function (felt) {
439
- (0, minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, "arg ".concat(input.name, " should be an array of string, number or BigNumber"));
440
- });
441
- argPosition += 1;
442
- }
443
- else {
444
- var arrayType_1 = input.type.replace('*', '');
445
- args[argPosition].forEach(function (struct) {
446
- _this.structs[arrayType_1].members.forEach(function (_a) {
447
- var name = _a.name;
448
- if (Array.isArray(struct)) {
449
- var structMembersLength = _this.calculateStructMembers(arrayType_1);
450
- (0, minimalistic_assert_1.default)(struct.length === structMembersLength, "arg should be of length ".concat(structMembersLength));
451
- }
452
- else {
453
- (0, minimalistic_assert_1.default)(Object.keys(struct).includes(name), "arg ".concat(input.name, " should be an array of ").concat(arrayType_1));
454
- }
455
- });
456
- });
457
- argPosition += 1;
458
- }
459
- }
460
- });
461
- };
462
- /**
463
- * Deep parse of the object that has been passed to the method
464
- *
465
- * @param element - element that needs to be parsed
466
- * @param type - name of the method
467
- * @return {string | string[]} - parsed arguments in format that contract is expecting
468
- */
469
- Contract.prototype.parseCalldataValue = function (element, type) {
470
- var _this = this;
471
- if (element === undefined) {
472
- throw Error('Missing element in calldata');
473
- }
474
- if (Array.isArray(element)) {
475
- var structMemberNum = this.calculateStructMembers(type);
476
- if (element.length !== structMemberNum) {
477
- throw Error('Missing element in calldata');
478
- }
479
- return element.map(function (el) { return (0, number_1.toFelt)(el); });
480
- }
481
- // checking if the passed element is struct or element in struct
482
- if (this.structs[type] && this.structs[type].members.length) {
483
- // going through all the members of the struct and parsing the value
484
- return this.structs[type].members.reduce(function (acc, member) {
485
- // if the member of the struct is another struct this will return array of the felts if not it will be single felt
486
- // TODO: refactor types so member name can be used as keyof ParsedStruct
487
- /* @ts-ignore */
488
- var parsedData = _this.parseCalldataValue(element[member.name], member.type);
489
- if (typeof parsedData === 'string') {
490
- acc.push(parsedData);
491
- }
492
- else {
493
- acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
494
- }
495
- return acc;
496
- }, []);
497
- }
498
- return (0, number_1.toFelt)(element);
499
- };
500
- /**
501
- * Parse of the response elements that are converted to Object (Struct) by using the abi
502
- *
503
- * @param responseIterator - iterator of the response
504
- * @param type - type of the struct
505
- * @return {BigNumberish | ParsedStruct} - parsed arguments in format that contract is expecting
506
- */
507
- Contract.prototype.parseResponseStruct = function (responseIterator, type) {
508
- var _this = this;
509
- // check the type of current element
510
- if (type in this.structs && this.structs[type]) {
511
- return this.structs[type].members.reduce(function (acc, el) {
512
- // parse each member of the struct (member can felt or nested struct)
513
- acc[el.name] = _this.parseResponseStruct(responseIterator, el.type);
514
- return acc;
515
- }, {});
516
- }
517
- return parseFelt(responseIterator.next().value);
518
- };
519
- /**
520
- * Parse one field of the calldata by using input field from the abi for that method
521
- *
522
- * @param args - value of the field
523
- * @param input - input(field) information from the abi that will be used to parse the data
524
- * @return {string | string[]} - parsed arguments in format that contract is expecting
525
- */
526
- Contract.prototype.parseCalldataField = function (argsIterator, input) {
527
- var _this = this;
528
- var name = input.name, type = input.type;
529
- var value = argsIterator.next().value;
530
- var parsedCalldata = [];
531
- switch (true) {
532
- case /\*/.test(type):
533
- if (Array.isArray(value)) {
534
- parsedCalldata.push((0, number_1.toFelt)(value.length));
535
- return value.reduce(function (acc, el) {
536
- if (/felt/.test(type)) {
537
- acc.push((0, number_1.toFelt)(el));
538
- }
539
- else {
540
- acc.push.apply(acc, __spreadArray([], __read(_this.parseCalldataValue(el, type.replace('*', ''))), false));
541
- }
542
- return acc;
543
- }, parsedCalldata);
544
- }
545
- throw Error("Expected ".concat(name, " to be array"));
546
- case type in this.structs:
547
- return this.parseCalldataValue(value, type);
548
- case /\(felt/.test(type):
549
- if (Array.isArray(value)) {
550
- return value.map(function (el) { return (0, number_1.toFelt)(el); });
551
- }
552
- throw Error("Expected ".concat(name, " to be array"));
553
- default:
554
- return (0, number_1.toFelt)(value);
555
- }
556
- };
557
- /**
558
- * Parse the calldata by using input fields from the abi for that method
559
- *
560
- * @param args - arguments passed the the method
561
- * @param inputs - list of inputs(fields) that are in the abi
562
- * @return {Calldata} - parsed arguments in format that contract is expecting
563
- */
564
- Contract.prototype.compileCalldata = function (args, inputs) {
565
- var _this = this;
566
- var argsIterator = args[Symbol.iterator]();
567
- return inputs.reduce(function (acc, input) {
568
- if (/_len$/.test(input.name)) {
569
- return acc;
570
- }
571
- var parsedData = _this.parseCalldataField(argsIterator, input);
572
- if (Array.isArray(parsedData)) {
573
- acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
574
- }
575
- else {
576
- acc.push(parsedData);
577
- }
578
- return acc;
579
- }, []);
580
- };
581
- /**
582
- * Parse elements of the response and structuring them into one field by using output property from the abi for that method
583
- *
584
- * @param responseIterator - iterator of the response
585
- * @param output - output(field) information from the abi that will be used to parse the data
586
- * @return - parsed response corresponding to the abi structure of the field
587
- */
588
- Contract.prototype.parseResponseField = function (responseIterator, output, parsedResult) {
589
- var name = output.name, type = output.type;
590
- var parsedDataArr = [];
591
- switch (true) {
592
- case /_len$/.test(name):
593
- return parseFelt(responseIterator.next().value).toNumber();
594
- case /\(felt/.test(type):
595
- return type.split(',').reduce(function (acc) {
596
- acc.push(parseFelt(responseIterator.next().value));
597
- return acc;
598
- }, []);
599
- case /\*/.test(type):
600
- if (parsedResult && parsedResult["".concat(name, "_len")]) {
601
- var arrLen = parsedResult["".concat(name, "_len")];
602
- while (parsedDataArr.length < arrLen) {
603
- parsedDataArr.push(this.parseResponseStruct(responseIterator, output.type.replace('*', '')));
604
- }
605
- }
606
- return parsedDataArr;
607
- case type in this.structs:
608
- return this.parseResponseStruct(responseIterator, type);
609
- default:
610
- return parseFelt(responseIterator.next().value);
611
- }
612
- };
613
- /**
614
- * Parse elements of the response array and structuring them into response object
615
- *
616
- * @param method - method name
617
- * @param response - response from the method
618
- * @return - parsed response corresponding to the abi
619
- */
620
- Contract.prototype.parseResponse = function (method, response) {
621
- var _this = this;
622
- var outputs = this.abi.find(function (abi) { return abi.name === method; }).outputs;
623
- var responseIterator = response.flat()[Symbol.iterator]();
624
- var resultObject = outputs.flat().reduce(function (acc, output) {
625
- acc[output.name] = _this.parseResponseField(responseIterator, output, acc);
626
- if (acc[output.name] && acc["".concat(output.name, "_len")]) {
627
- delete acc["".concat(output.name, "_len")];
628
- }
629
- return acc;
630
- }, {});
631
- return Object.entries(resultObject).reduce(function (acc, _a) {
632
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
633
- acc.push(value);
634
- acc[key] = value;
635
- return acc;
636
- }, []);
637
- };
638
- return Contract;
639
- }());
640
- exports.Contract = Contract;
@@ -1,3 +0,0 @@
1
- export * from './default';
2
- export * from './interface';
3
- export * from './contractFactory';
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./default"), exports);
18
- __exportStar(require("./interface"), exports);
19
- __exportStar(require("./contractFactory"), exports);