starknet 3.4.0 → 3.5.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 (62) hide show
  1. package/.eslintrc +2 -1
  2. package/CHANGELOG.md +20 -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/contract/contractFactory.d.ts +36 -0
  8. package/contract/contractFactory.js +218 -0
  9. package/contract/default.d.ts +143 -0
  10. package/{contract.js → contract/default.js} +357 -86
  11. package/contract/index.d.ts +3 -0
  12. package/contract/index.js +28 -0
  13. package/contract/interface.d.ts +79 -0
  14. package/contract/interface.js +8 -0
  15. package/dist/contract/contractFactory.d.ts +32 -0
  16. package/dist/contract/contractFactory.js +102 -0
  17. package/dist/contract/default.d.ts +121 -0
  18. package/dist/{contract.js → contract/default.js} +321 -73
  19. package/dist/contract/index.d.ts +3 -0
  20. package/dist/contract/index.js +15 -0
  21. package/dist/contract/interface.d.ts +72 -0
  22. package/dist/contract/interface.js +9 -0
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.js +1 -1
  25. package/dist/provider/default.d.ts +4 -1
  26. package/dist/provider/default.js +15 -6
  27. package/dist/provider/interface.d.ts +3 -1
  28. package/dist/types/api.d.ts +6 -0
  29. package/dist/types/contract.d.ts +5 -0
  30. package/dist/types/contract.js +2 -0
  31. package/dist/types/index.d.ts +1 -0
  32. package/dist/types/index.js +1 -0
  33. package/dist/types/lib.d.ts +11 -1
  34. package/dist/utils/transaction.d.ts +1 -2
  35. package/index.d.ts +1 -1
  36. package/index.js +1 -1
  37. package/package.json +1 -1
  38. package/provider/default.d.ts +4 -1
  39. package/provider/default.js +25 -16
  40. package/provider/interface.d.ts +3 -1
  41. package/src/contract/contractFactory.ts +78 -0
  42. package/src/contract/default.ts +622 -0
  43. package/src/contract/index.ts +3 -0
  44. package/src/contract/interface.ts +87 -0
  45. package/src/index.ts +1 -1
  46. package/src/provider/default.ts +19 -14
  47. package/src/provider/interface.ts +3 -1
  48. package/src/types/api.ts +7 -0
  49. package/src/types/contract.ts +5 -0
  50. package/src/types/index.ts +1 -0
  51. package/src/types/lib.ts +12 -1
  52. package/src/utils/transaction.ts +1 -2
  53. package/types/api.d.ts +6 -0
  54. package/types/contract.d.ts +5 -0
  55. package/types/contract.js +2 -0
  56. package/types/index.d.ts +1 -0
  57. package/types/index.js +1 -0
  58. package/types/lib.d.ts +11 -1
  59. package/utils/transaction.d.ts +1 -2
  60. package/contract.d.ts +0 -98
  61. package/dist/contract.d.ts +0 -94
  62. package/src/contract.ts +0 -357
package/dist/index.js CHANGED
@@ -26,8 +26,8 @@ exports.typedData = exports.shortString = exports.uint256 = exports.ec = exports
26
26
  /**
27
27
  * Main
28
28
  */
29
- __exportStar(require("./types"), exports);
30
29
  __exportStar(require("./contract"), exports);
30
+ __exportStar(require("./types"), exports);
31
31
  __exportStar(require("./provider"), exports);
32
32
  __exportStar(require("./account"), exports);
33
33
  __exportStar(require("./signer"), exports);
@@ -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,6 +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>;
123
+ estimateFee(invocation: Invocation): Promise<any>;
121
124
  waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
122
125
  /**
123
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,6 +383,17 @@ var Provider = /** @class */ (function () {
385
383
  signature: (0, number_1.bigNumberishArrayToDecimalStringArray)((_b = invocation.signature) !== null && _b !== void 0 ? _b : []),
386
384
  });
387
385
  };
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
390
+ type: 'INVOKE_FUNCTION',
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 : []),
395
+ });
396
+ };
388
397
  Provider.prototype.waitForTransaction = function (txHash, retryInterval) {
389
398
  if (retryInterval === void 0) { retryInterval = 8000; }
390
399
  return __awaiter(this, void 0, void 0, function () {
@@ -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.4.0",
3
+ "version": "3.5.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -65,7 +65,9 @@ export declare class Provider implements ProviderInterface {
65
65
  */
66
66
  callContract(
67
67
  { contractAddress, entrypoint, calldata }: Call,
68
- blockIdentifier?: BlockIdentifier
68
+ options?: {
69
+ blockIdentifier: BlockIdentifier;
70
+ }
69
71
  ): Promise<CallContractResponse>;
70
72
  /**
71
73
  * Gets the block information
@@ -155,6 +157,7 @@ export declare class Provider implements ProviderInterface {
155
157
  * @returns response from addTransaction
156
158
  */
157
159
  invokeFunction(invocation: Invocation, _abi?: Abi): Promise<AddTransactionResponse>;
160
+ estimateFee(invocation: Invocation): Promise<any>;
158
161
  waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
159
162
  /**
160
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,6 +562,21 @@ var Provider = /** @class */ (function () {
568
562
  ),
569
563
  });
570
564
  };
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
569
+ type: 'INVOKE_FUNCTION',
570
+ contract_address: invocation.contractAddress,
571
+ entry_point_selector: (0, hash_1.getSelectorFromName)(invocation.entrypoint),
572
+ calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(
573
+ (_a = invocation.calldata) !== null && _a !== void 0 ? _a : []
574
+ ),
575
+ signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(
576
+ (_b = invocation.signature) !== null && _b !== void 0 ? _b : []
577
+ ),
578
+ });
579
+ };
571
580
  Provider.prototype.waitForTransaction = function (txHash, retryInterval) {
572
581
  if (retryInterval === void 0) {
573
582
  retryInterval = 8000;
@@ -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
@@ -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
+ }