starknet 3.3.0 → 3.5.1

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 (67) hide show
  1. package/.eslintrc +2 -1
  2. package/CHANGELOG.md +33 -0
  3. package/__tests__/account.test.ts +7 -17
  4. package/__tests__/accountContract.test.ts +13 -25
  5. package/__tests__/contract.test.ts +152 -55
  6. package/__tests__/utils/utils.browser.test.ts +1 -3
  7. package/account/default.d.ts +6 -0
  8. package/account/default.js +129 -0
  9. package/contract/contractFactory.d.ts +36 -0
  10. package/contract/contractFactory.js +218 -0
  11. package/contract/default.d.ts +143 -0
  12. package/{contract.js → contract/default.js} +356 -86
  13. package/contract/index.d.ts +3 -0
  14. package/contract/index.js +28 -0
  15. package/contract/interface.d.ts +79 -0
  16. package/contract/interface.js +8 -0
  17. package/dist/account/default.d.ts +6 -1
  18. package/dist/account/default.js +99 -0
  19. package/dist/contract/contractFactory.d.ts +32 -0
  20. package/dist/contract/contractFactory.js +102 -0
  21. package/dist/contract/default.d.ts +121 -0
  22. package/dist/{contract.js → contract/default.js} +320 -73
  23. package/dist/contract/index.d.ts +3 -0
  24. package/dist/contract/index.js +15 -0
  25. package/dist/contract/interface.d.ts +72 -0
  26. package/dist/contract/interface.js +9 -0
  27. package/dist/index.d.ts +1 -1
  28. package/dist/index.js +1 -1
  29. package/dist/provider/default.d.ts +5 -13
  30. package/dist/provider/default.js +12 -22
  31. package/dist/provider/interface.d.ts +3 -1
  32. package/dist/types/api.d.ts +6 -0
  33. package/dist/types/contract.d.ts +5 -0
  34. package/dist/types/contract.js +2 -0
  35. package/dist/types/index.d.ts +1 -0
  36. package/dist/types/index.js +1 -0
  37. package/dist/types/lib.d.ts +11 -1
  38. package/dist/utils/transaction.d.ts +1 -2
  39. package/index.d.ts +1 -1
  40. package/index.js +1 -1
  41. package/package.json +1 -1
  42. package/provider/default.d.ts +4 -18
  43. package/provider/default.js +18 -37
  44. package/provider/interface.d.ts +3 -1
  45. package/src/account/default.ts +109 -1
  46. package/src/contract/contractFactory.ts +78 -0
  47. package/src/contract/default.ts +626 -0
  48. package/src/contract/index.ts +3 -0
  49. package/src/contract/interface.ts +87 -0
  50. package/src/index.ts +1 -1
  51. package/src/provider/default.ts +15 -36
  52. package/src/provider/interface.ts +3 -1
  53. package/src/types/api.ts +7 -0
  54. package/src/types/contract.ts +5 -0
  55. package/src/types/index.ts +1 -0
  56. package/src/types/lib.ts +12 -1
  57. package/src/utils/transaction.ts +1 -2
  58. package/types/api.d.ts +6 -0
  59. package/types/contract.d.ts +5 -0
  60. package/types/contract.js +2 -0
  61. package/types/index.d.ts +1 -0
  62. package/types/index.js +1 -0
  63. package/types/lib.d.ts +11 -1
  64. package/utils/transaction.d.ts +1 -2
  65. package/contract.d.ts +0 -98
  66. package/dist/contract.d.ts +0 -94
  67. package/src/contract.ts +0 -357
@@ -1,4 +1,4 @@
1
- import { Abi, AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, Endpoints, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Invocation, Signature, TransactionReceipt } from '../types';
1
+ import { Abi, AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, Endpoints, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Invocation, TransactionReceipt } from '../types';
2
2
  import { BigNumberish } from '../utils/number';
3
3
  import { ProviderInterface } from './interface';
4
4
  import { BlockIdentifier } from './utils';
@@ -36,7 +36,9 @@ export declare class Provider implements ProviderInterface {
36
36
  * @param blockNumber
37
37
  * @returns the result of the function on the smart contract.
38
38
  */
39
- callContract({ contractAddress, entrypoint, calldata }: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
39
+ callContract({ contractAddress, entrypoint, calldata }: Call, options?: {
40
+ blockIdentifier: BlockIdentifier;
41
+ }): Promise<CallContractResponse>;
40
42
  /**
41
43
  * Gets the block information
42
44
  *
@@ -118,17 +120,7 @@ export declare class Provider implements ProviderInterface {
118
120
  * @returns response from addTransaction
119
121
  */
120
122
  invokeFunction(invocation: Invocation, _abi?: Abi): Promise<AddTransactionResponse>;
121
- /**
122
- * Invokes a function on starknet
123
- * @deprecated This method wont be supported as soon as fees are mandatory
124
- *
125
- * @param contractAddress - target contract address for invoke
126
- * @param entrypointSelector - target entrypoint selector for
127
- * @param calldata - (optional, default []) calldata
128
- * @param signature - (optional) signature to send along
129
- * @returns response from addTransaction
130
- */
131
- LEGACY_invokeFunction(contractAddress: string, entrypointSelector: string, calldata?: string[], signature?: Signature): Promise<AddTransactionResponse>;
123
+ estimateFee(invocation: Invocation): Promise<any>;
132
124
  waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
133
125
  /**
134
126
  * @deprecated use `waitForTransaction` instead
@@ -117,7 +117,7 @@ var Provider = /** @class */ (function () {
117
117
  return gatewayUrlEndpoints.includes(endpoint) ? this.gatewayUrl : this.feederGatewayUrl;
118
118
  };
119
119
  Provider.prototype.getFetchMethod = function (endpoint) {
120
- var postMethodEndpoints = ['add_transaction', 'call_contract'];
120
+ var postMethodEndpoints = ['add_transaction', 'call_contract', 'estimate_fee'];
121
121
  return postMethodEndpoints.includes(endpoint) ? 'POST' : 'GET';
122
122
  };
123
123
  Provider.prototype.getQueryString = function (query) {
@@ -219,14 +219,12 @@ var Provider = /** @class */ (function () {
219
219
  * @param blockNumber
220
220
  * @returns the result of the function on the smart contract.
221
221
  */
222
- Provider.prototype.callContract = function (_a, blockIdentifier) {
222
+ Provider.prototype.callContract = function (_a, options) {
223
223
  var contractAddress = _a.contractAddress, entrypoint = _a.entrypoint, _b = _a.calldata, calldata = _b === void 0 ? [] : _b;
224
- if (blockIdentifier === void 0) { blockIdentifier = null; }
224
+ if (options === void 0) { options = { blockIdentifier: null }; }
225
225
  return __awaiter(this, void 0, void 0, function () {
226
226
  return __generator(this, function (_c) {
227
- return [2 /*return*/, this.fetchEndpoint('call_contract', {
228
- blockIdentifier: blockIdentifier,
229
- }, {
227
+ return [2 /*return*/, this.fetchEndpoint('call_contract', options, {
230
228
  signature: [],
231
229
  contract_address: contractAddress,
232
230
  entry_point_selector: (0, hash_1.getSelectorFromName)(entrypoint),
@@ -385,23 +383,15 @@ var Provider = /** @class */ (function () {
385
383
  signature: (0, number_1.bigNumberishArrayToDecimalStringArray)((_b = invocation.signature) !== null && _b !== void 0 ? _b : []),
386
384
  });
387
385
  };
388
- /**
389
- * Invokes a function on starknet
390
- * @deprecated This method wont be supported as soon as fees are mandatory
391
- *
392
- * @param contractAddress - target contract address for invoke
393
- * @param entrypointSelector - target entrypoint selector for
394
- * @param calldata - (optional, default []) calldata
395
- * @param signature - (optional) signature to send along
396
- * @returns response from addTransaction
397
- */
398
- Provider.prototype.LEGACY_invokeFunction = function (contractAddress, entrypointSelector, calldata, signature) {
399
- return this.fetchEndpoint('add_transaction', undefined, {
386
+ Provider.prototype.estimateFee = function (invocation) {
387
+ var _a, _b;
388
+ return this.fetchEndpoint('estimate_fee', undefined, {
389
+ // TODO: change the TYPE of the call
400
390
  type: 'INVOKE_FUNCTION',
401
- contract_address: contractAddress,
402
- entry_point_selector: entrypointSelector,
403
- calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(calldata !== null && calldata !== void 0 ? calldata : []),
404
- signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(signature !== null && signature !== void 0 ? signature : []),
391
+ contract_address: invocation.contractAddress,
392
+ entry_point_selector: (0, hash_1.getSelectorFromName)(invocation.entrypoint),
393
+ calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)((_a = invocation.calldata) !== null && _a !== void 0 ? _a : []),
394
+ signature: (0, number_1.bigNumberishArrayToDecimalStringArray)((_b = invocation.signature) !== null && _b !== void 0 ? _b : []),
405
395
  });
406
396
  };
407
397
  Provider.prototype.waitForTransaction = function (txHash, retryInterval) {
@@ -21,7 +21,9 @@ export declare abstract class ProviderInterface {
21
21
  * @param blockIdentifier block identifier
22
22
  * @returns the result of the function on the smart contract.
23
23
  */
24
- abstract callContract(invokeTransaction: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
24
+ abstract callContract(invokeTransaction: Call, options: {
25
+ blockIdentifier: BlockIdentifier;
26
+ }): Promise<CallContractResponse>;
25
27
  /**
26
28
  * Gets the block information
27
29
  *
@@ -57,6 +57,11 @@ export declare type Endpoints = {
57
57
  REQUEST: CallContractTransaction;
58
58
  RESPONSE: CallContractResponse;
59
59
  };
60
+ estimate_fee: {
61
+ QUERY: never;
62
+ REQUEST: Transaction;
63
+ RESPONSE: EstimateFeeResponse;
64
+ };
60
65
  };
61
66
  export declare type GetContractAddressesResponse = {
62
67
  Starknet: string;
@@ -143,6 +148,7 @@ export declare type TransactionReceipt = {
143
148
  l2_to_l1_messages: string[];
144
149
  events: string[];
145
150
  };
151
+ export declare type EstimateFeeResponse = {};
146
152
  export declare type RawArgs = {
147
153
  [inputName: string]: string | string[] | {
148
154
  type: 'struct';
@@ -0,0 +1,5 @@
1
+ export declare type AsyncContractFunction<T = any> = (...args: Array<any>) => Promise<T>;
2
+ export declare type ContractFunction = (...args: Array<any>) => any;
3
+ export interface Result extends Array<any> {
4
+ [key: string]: any;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,4 @@
1
1
  export * from './lib';
2
2
  export * from './api';
3
3
  export * from './signer';
4
+ export * from './contract';
@@ -13,3 +13,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./lib"), exports);
14
14
  __exportStar(require("./api"), exports);
15
15
  __exportStar(require("./signer"), exports);
16
+ __exportStar(require("./contract"), exports);
@@ -33,7 +33,7 @@ export declare type FunctionAbi = {
33
33
  name: string;
34
34
  outputs: AbiEntry[];
35
35
  stateMutability?: 'view';
36
- type: 'function';
36
+ type: 'function' | 'constructor';
37
37
  };
38
38
  export declare type StructAbi = {
39
39
  members: (AbiEntry & {
@@ -55,3 +55,13 @@ export declare type CompiledContract = {
55
55
  export declare type CompressedCompiledContract = Omit<CompiledContract, 'program'> & {
56
56
  program: CompressedProgram;
57
57
  };
58
+ export declare type Struct = {
59
+ type: 'struct';
60
+ [k: string]: BigNumberish;
61
+ };
62
+ export declare type Args = {
63
+ [inputName: string]: BigNumberish | BigNumberish[] | ParsedStruct | ParsedStruct[];
64
+ };
65
+ export declare type ParsedStruct = {
66
+ [key: string]: BigNumberish | ParsedStruct;
67
+ };
@@ -1,5 +1,4 @@
1
- import { ParsedStruct } from '../contract';
2
- import { Call } from '../types';
1
+ import { Call, ParsedStruct } from '../types';
3
2
  /**
4
3
  * Transforms a list of Calls, each with their own calldata, into
5
4
  * two arrays: one with the entrypoints, and one with the concatenated calldata.
package/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Main
3
3
  */
4
- export * from './types';
5
4
  export * from './contract';
5
+ export * from './types';
6
6
  export * from './provider';
7
7
  export * from './account';
8
8
  export * from './signer';
package/index.js CHANGED
@@ -58,8 +58,8 @@ exports.typedData =
58
58
  /**
59
59
  * Main
60
60
  */
61
- __exportStar(require('./types'), exports);
62
61
  __exportStar(require('./contract'), exports);
62
+ __exportStar(require('./types'), exports);
63
63
  __exportStar(require('./provider'), exports);
64
64
  __exportStar(require('./account'), exports);
65
65
  __exportStar(require('./signer'), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "3.3.0",
3
+ "version": "3.5.1",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,7 +11,6 @@ import {
11
11
  GetTransactionResponse,
12
12
  GetTransactionStatusResponse,
13
13
  Invocation,
14
- Signature,
15
14
  TransactionReceipt,
16
15
  } from '../types';
17
16
  import { BigNumberish } from '../utils/number';
@@ -66,7 +65,9 @@ export declare class Provider implements ProviderInterface {
66
65
  */
67
66
  callContract(
68
67
  { contractAddress, entrypoint, calldata }: Call,
69
- blockIdentifier?: BlockIdentifier
68
+ options?: {
69
+ blockIdentifier: BlockIdentifier;
70
+ }
70
71
  ): Promise<CallContractResponse>;
71
72
  /**
72
73
  * Gets the block information
@@ -156,22 +157,7 @@ export declare class Provider implements ProviderInterface {
156
157
  * @returns response from addTransaction
157
158
  */
158
159
  invokeFunction(invocation: Invocation, _abi?: Abi): Promise<AddTransactionResponse>;
159
- /**
160
- * Invokes a function on starknet
161
- * @deprecated This method wont be supported as soon as fees are mandatory
162
- *
163
- * @param contractAddress - target contract address for invoke
164
- * @param entrypointSelector - target entrypoint selector for
165
- * @param calldata - (optional, default []) calldata
166
- * @param signature - (optional) signature to send along
167
- * @returns response from addTransaction
168
- */
169
- LEGACY_invokeFunction(
170
- contractAddress: string,
171
- entrypointSelector: string,
172
- calldata?: string[],
173
- signature?: Signature
174
- ): Promise<AddTransactionResponse>;
160
+ estimateFee(invocation: Invocation): Promise<any>;
175
161
  waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
176
162
  /**
177
163
  * @deprecated use `waitForTransaction` instead
@@ -231,7 +231,7 @@ var Provider = /** @class */ (function () {
231
231
  return gatewayUrlEndpoints.includes(endpoint) ? this.gatewayUrl : this.feederGatewayUrl;
232
232
  };
233
233
  Provider.prototype.getFetchMethod = function (endpoint) {
234
- var postMethodEndpoints = ['add_transaction', 'call_contract'];
234
+ var postMethodEndpoints = ['add_transaction', 'call_contract', 'estimate_fee'];
235
235
  return postMethodEndpoints.includes(endpoint) ? 'POST' : 'GET';
236
236
  };
237
237
  Provider.prototype.getQueryString = function (query) {
@@ -343,30 +343,24 @@ var Provider = /** @class */ (function () {
343
343
  * @param blockNumber
344
344
  * @returns the result of the function on the smart contract.
345
345
  */
346
- Provider.prototype.callContract = function (_a, blockIdentifier) {
346
+ Provider.prototype.callContract = function (_a, options) {
347
347
  var contractAddress = _a.contractAddress,
348
348
  entrypoint = _a.entrypoint,
349
349
  _b = _a.calldata,
350
350
  calldata = _b === void 0 ? [] : _b;
351
- if (blockIdentifier === void 0) {
352
- blockIdentifier = null;
351
+ if (options === void 0) {
352
+ options = { blockIdentifier: null };
353
353
  }
354
354
  return __awaiter(this, void 0, void 0, function () {
355
355
  return __generator(this, function (_c) {
356
356
  return [
357
357
  2 /*return*/,
358
- this.fetchEndpoint(
359
- 'call_contract',
360
- {
361
- blockIdentifier: blockIdentifier,
362
- },
363
- {
364
- signature: [],
365
- contract_address: contractAddress,
366
- entry_point_selector: (0, hash_1.getSelectorFromName)(entrypoint),
367
- calldata: calldata,
368
- }
369
- ),
358
+ this.fetchEndpoint('call_contract', options, {
359
+ signature: [],
360
+ contract_address: contractAddress,
361
+ entry_point_selector: (0, hash_1.getSelectorFromName)(entrypoint),
362
+ calldata: calldata,
363
+ }),
370
364
  ];
371
365
  });
372
366
  });
@@ -568,31 +562,18 @@ var Provider = /** @class */ (function () {
568
562
  ),
569
563
  });
570
564
  };
571
- /**
572
- * Invokes a function on starknet
573
- * @deprecated This method wont be supported as soon as fees are mandatory
574
- *
575
- * @param contractAddress - target contract address for invoke
576
- * @param entrypointSelector - target entrypoint selector for
577
- * @param calldata - (optional, default []) calldata
578
- * @param signature - (optional) signature to send along
579
- * @returns response from addTransaction
580
- */
581
- Provider.prototype.LEGACY_invokeFunction = function (
582
- contractAddress,
583
- entrypointSelector,
584
- calldata,
585
- signature
586
- ) {
587
- return this.fetchEndpoint('add_transaction', undefined, {
565
+ Provider.prototype.estimateFee = function (invocation) {
566
+ var _a, _b;
567
+ return this.fetchEndpoint('estimate_fee', undefined, {
568
+ // TODO: change the TYPE of the call
588
569
  type: 'INVOKE_FUNCTION',
589
- contract_address: contractAddress,
590
- entry_point_selector: entrypointSelector,
570
+ contract_address: invocation.contractAddress,
571
+ entry_point_selector: (0, hash_1.getSelectorFromName)(invocation.entrypoint),
591
572
  calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(
592
- calldata !== null && calldata !== void 0 ? calldata : []
573
+ (_a = invocation.calldata) !== null && _a !== void 0 ? _a : []
593
574
  ),
594
575
  signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(
595
- signature !== null && signature !== void 0 ? signature : []
576
+ (_b = invocation.signature) !== null && _b !== void 0 ? _b : []
596
577
  ),
597
578
  });
598
579
  };
@@ -35,7 +35,9 @@ export declare abstract class ProviderInterface {
35
35
  */
36
36
  abstract callContract(
37
37
  invokeTransaction: Call,
38
- blockIdentifier?: BlockIdentifier
38
+ options: {
39
+ blockIdentifier: BlockIdentifier;
40
+ }
39
41
  ): Promise<CallContractResponse>;
40
42
  /**
41
43
  * Gets the block information
@@ -1,3 +1,5 @@
1
+ import assert from 'minimalistic-assert';
2
+
1
3
  import { Provider } from '../provider';
2
4
  import { Signer, SignerInterface } from '../signer';
3
5
  import {
@@ -5,10 +7,18 @@ import {
5
7
  AddTransactionResponse,
6
8
  Call,
7
9
  InvocationsDetails,
10
+ InvokeFunctionTransaction,
8
11
  KeyPair,
9
12
  Signature,
13
+ Transaction,
10
14
  } from '../types';
11
- import { getSelectorFromName } from '../utils/hash';
15
+ import { sign } from '../utils/ellipticCurve';
16
+ import {
17
+ computeHashOnElements,
18
+ getSelectorFromName,
19
+ transactionPrefix,
20
+ transactionVersion,
21
+ } from '../utils/hash';
12
22
  import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
13
23
  import { compileCalldata } from '../utils/stark';
14
24
  import { fromCallsToExecuteCalldata } from '../utils/transaction';
@@ -68,6 +78,104 @@ export class Account extends Provider implements AccountInterface {
68
78
  });
69
79
  }
70
80
 
81
+ /**
82
+ * Temporary method to allow dapps on starknet.js v2 to work with Argent X v3
83
+ * @deprecated to remove ASAP
84
+ */
85
+ public async LEGACY_addTransaction(transaction: Transaction): Promise<AddTransactionResponse> {
86
+ if (transaction.type === 'DEPLOY') throw new Error('No DEPLOYS');
87
+
88
+ assert(
89
+ !transaction.signature,
90
+ "Adding signatures to a signer transaction currently isn't supported"
91
+ );
92
+
93
+ let nonceBn;
94
+ if (transaction.nonce) {
95
+ nonceBn = toBN(transaction.nonce);
96
+ } else {
97
+ const { result } = await this.callContract({
98
+ contractAddress: this.address,
99
+ entrypoint: 'get_nonce',
100
+ });
101
+ nonceBn = toBN(result[0]);
102
+ }
103
+
104
+ function hashMulticall(
105
+ account: string,
106
+ transactions: InvokeFunctionTransaction[],
107
+ nonce: string,
108
+ maxFee: string
109
+ ) {
110
+ const hashArray = transactions
111
+ .map(({ contract_address, entry_point_selector, calldata }) => [
112
+ contract_address,
113
+ entry_point_selector,
114
+ computeHashOnElements(calldata || []),
115
+ ])
116
+ .map(bigNumberishArrayToDecimalStringArray)
117
+ .map(computeHashOnElements);
118
+
119
+ return computeHashOnElements([
120
+ transactionPrefix,
121
+ account,
122
+ computeHashOnElements(hashArray),
123
+ nonce,
124
+ maxFee,
125
+ transactionVersion,
126
+ ]);
127
+ }
128
+ const msgHash = hashMulticall(this.address, [transaction], nonceBn.toString(), '0');
129
+ if (!('keyPair' in this.signer)) {
130
+ throw new Error('No keyPair');
131
+ }
132
+ const signature = sign((this.signer as any).keyPair, msgHash);
133
+
134
+ const transformCallsToMulticallArrays = (calls: InvokeFunctionTransaction[]) => {
135
+ const callArray: any[] = [];
136
+ const calldata: BigNumberish[] = [];
137
+ calls.forEach((call) => {
138
+ const data = call.calldata || [];
139
+ callArray.push({
140
+ to: toBN(call.contract_address).toString(10),
141
+ selector: toBN(call.entry_point_selector).toString(10),
142
+ data_offset: calldata.length.toString(),
143
+ data_len: data.length.toString(),
144
+ });
145
+ calldata.push(...data);
146
+ });
147
+ return {
148
+ callArray,
149
+ calldata: bigNumberishArrayToDecimalStringArray(calldata),
150
+ };
151
+ };
152
+
153
+ const fromCallsToExecuteCalldata2 = (calls: InvokeFunctionTransaction[]): string[] => {
154
+ const { callArray, calldata } = transformCallsToMulticallArrays(calls);
155
+ return [
156
+ callArray.length.toString(),
157
+ ...callArray
158
+ .map(
159
+ ({ to, selector, data_offset, data_len }) =>
160
+ [to, selector, data_offset, data_len] as string[]
161
+ )
162
+ .flat(),
163
+ calldata.length.toString(),
164
+ ...calldata,
165
+ ];
166
+ };
167
+
168
+ const calldata = [...fromCallsToExecuteCalldata2([transaction]), nonceBn.toString()];
169
+
170
+ return this.fetchEndpoint('add_transaction', undefined, {
171
+ type: 'INVOKE_FUNCTION',
172
+ contract_address: this.address,
173
+ entry_point_selector: getSelectorFromName('__execute__'),
174
+ calldata,
175
+ signature: bigNumberishArrayToDecimalStringArray(signature),
176
+ });
177
+ }
178
+
71
179
  /**
72
180
  * Sign an JSON object with the starknet private key and return the signature
73
181
  *
@@ -0,0 +1,78 @@
1
+ import assert from 'minimalistic-assert';
2
+
3
+ import { Account } from '../account';
4
+ import { Provider, defaultProvider } from '../provider';
5
+ import { Abi, CompiledContract, RawCalldata } from '../types';
6
+ import { BigNumberish } from '../utils/number';
7
+ import { Contract } from './default';
8
+
9
+ export class ContractFactory {
10
+ abi: Abi;
11
+
12
+ compiledContract: CompiledContract;
13
+
14
+ providerOrAccount: Provider | Account;
15
+
16
+ constructor(
17
+ compiledContract: CompiledContract,
18
+ providerOrAccount: Provider | Account = defaultProvider,
19
+ abi: Abi = compiledContract.abi // abi can be different from the deployed contract ie for proxy contracts
20
+ ) {
21
+ this.abi = abi;
22
+ this.compiledContract = compiledContract;
23
+ this.providerOrAccount = providerOrAccount;
24
+ }
25
+
26
+ /**
27
+ * Deploys contract and returns new instance of the Contract
28
+ *
29
+ * @param constructorCalldata - Constructor Calldata
30
+ * @param addressSalt (optional) - Address Salt for deployment
31
+ * @returns deployed Contract
32
+ */
33
+ public async deploy(
34
+ constructorCalldata?: RawCalldata,
35
+ addressSalt?: BigNumberish
36
+ ): Promise<Contract> {
37
+ const { address, code, transaction_hash } = await this.providerOrAccount.deployContract({
38
+ contract: this.compiledContract,
39
+ constructorCalldata,
40
+ addressSalt,
41
+ });
42
+ assert(
43
+ code === 'TRANSACTION_RECEIVED' && Boolean(address),
44
+ 'Deployment of the contract failed'
45
+ );
46
+
47
+ const contractInstance = new Contract(
48
+ this.compiledContract.abi,
49
+ address!,
50
+ this.providerOrAccount
51
+ );
52
+ contractInstance.deployTransactionHash = transaction_hash;
53
+
54
+ return contractInstance;
55
+ }
56
+
57
+ /**
58
+ * Attaches to new Provider or Account
59
+ *
60
+ * @param providerOrAccount - new Provider or Account to attach to
61
+ */
62
+ connect(providerOrAccount: Provider | Account): ContractFactory {
63
+ this.providerOrAccount = providerOrAccount;
64
+ return this;
65
+ }
66
+
67
+ /**
68
+ * Attaches current abi and provider or account to the new address
69
+ *
70
+ * @param address - Contract address
71
+ * @returns Contract
72
+ */
73
+ attach(address: string): Contract {
74
+ return new Contract(this.abi, address, this.providerOrAccount);
75
+ }
76
+
77
+ // ethers.js' getDeployTransaction cant be supported as it requires the account or signer to return a signed transaction which is not possible with the current implementation
78
+ }