starknet 4.7.0 → 4.9.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 (95) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/CONTRIBUTING.md +2 -2
  3. package/__mocks__/ERC20.json +32561 -29055
  4. package/__mocks__/l1l2_compiled.json +10107 -0
  5. package/__tests__/account.test.ts +32 -24
  6. package/__tests__/contract.test.ts +25 -14
  7. package/__tests__/defaultProvider.test.ts +19 -49
  8. package/__tests__/fixtures.ts +11 -1
  9. package/__tests__/rpcProvider.test.ts +6 -15
  10. package/__tests__/sequencerProvider.test.ts +57 -11
  11. package/__tests__/utils/merkle.test.ts +113 -3
  12. package/__tests__/utils/typedData.test.ts +3 -3
  13. package/account/default.d.ts +10 -44
  14. package/account/default.js +255 -61
  15. package/account/interface.d.ts +78 -7
  16. package/constants.d.ts +1 -0
  17. package/constants.js +1 -0
  18. package/contract/default.js +6 -6
  19. package/dist/account/default.d.ts +10 -44
  20. package/dist/account/default.js +255 -61
  21. package/dist/account/interface.d.ts +78 -7
  22. package/dist/constants.d.ts +1 -0
  23. package/dist/constants.js +1 -0
  24. package/dist/contract/default.js +6 -6
  25. package/dist/provider/default.d.ts +8 -3
  26. package/dist/provider/default.js +31 -4
  27. package/dist/provider/interface.d.ts +67 -5
  28. package/dist/provider/rpc.d.ts +7 -2
  29. package/dist/provider/rpc.js +83 -8
  30. package/dist/provider/sequencer.d.ts +9 -3
  31. package/dist/provider/sequencer.js +93 -14
  32. package/dist/signer/default.d.ts +4 -1
  33. package/dist/signer/default.js +22 -0
  34. package/dist/signer/interface.d.ts +27 -2
  35. package/dist/types/api/openrpc.d.ts +24 -2
  36. package/dist/types/api/sequencer.d.ts +43 -23
  37. package/dist/types/lib.d.ts +23 -2
  38. package/dist/types/provider.d.ts +19 -10
  39. package/dist/types/signer.d.ts +14 -1
  40. package/dist/utils/hash.d.ts +8 -0
  41. package/dist/utils/hash.js +28 -2
  42. package/dist/utils/merkle.js +4 -5
  43. package/dist/utils/number.d.ts +5 -0
  44. package/dist/utils/number.js +29 -1
  45. package/dist/utils/responseParser/rpc.d.ts +2 -6
  46. package/dist/utils/responseParser/rpc.js +0 -11
  47. package/dist/utils/responseParser/sequencer.js +11 -33
  48. package/package.json +1 -1
  49. package/provider/default.d.ts +8 -3
  50. package/provider/default.js +31 -4
  51. package/provider/interface.d.ts +67 -5
  52. package/provider/rpc.d.ts +7 -2
  53. package/provider/rpc.js +83 -8
  54. package/provider/sequencer.d.ts +9 -3
  55. package/provider/sequencer.js +93 -14
  56. package/signer/default.d.ts +4 -1
  57. package/signer/default.js +22 -0
  58. package/signer/interface.d.ts +27 -2
  59. package/src/account/default.ts +201 -53
  60. package/src/account/interface.ts +104 -6
  61. package/src/constants.ts +1 -0
  62. package/src/contract/default.ts +6 -6
  63. package/src/provider/default.ts +43 -5
  64. package/src/provider/interface.ts +92 -7
  65. package/src/provider/rpc.ts +86 -12
  66. package/src/provider/sequencer.ts +105 -13
  67. package/src/signer/default.ts +54 -2
  68. package/src/signer/interface.ts +31 -2
  69. package/src/types/api/openrpc.ts +28 -2
  70. package/src/types/api/sequencer.ts +54 -25
  71. package/src/types/lib.ts +30 -2
  72. package/src/types/provider.ts +31 -11
  73. package/src/types/signer.ts +18 -1
  74. package/src/utils/hash.ts +70 -2
  75. package/src/utils/merkle.ts +4 -5
  76. package/src/utils/number.ts +27 -0
  77. package/src/utils/responseParser/rpc.ts +4 -20
  78. package/src/utils/responseParser/sequencer.ts +14 -7
  79. package/types/api/openrpc.d.ts +24 -2
  80. package/types/api/sequencer.d.ts +43 -23
  81. package/types/lib.d.ts +23 -2
  82. package/types/provider.d.ts +19 -10
  83. package/types/signer.d.ts +14 -1
  84. package/utils/hash.d.ts +8 -0
  85. package/utils/hash.js +28 -2
  86. package/utils/merkle.js +4 -5
  87. package/utils/number.d.ts +5 -0
  88. package/utils/number.js +29 -1
  89. package/utils/responseParser/rpc.d.ts +2 -6
  90. package/utils/responseParser/rpc.js +0 -11
  91. package/utils/responseParser/sequencer.js +11 -33
  92. package/www/docs/API/account.md +60 -1
  93. package/www/docs/API/provider.md +320 -23
  94. package/www/guides/account.md +1 -1
  95. package/www/guides/erc20.md +13 -7
@@ -1,8 +1,7 @@
1
1
  import BN from 'bn.js';
2
2
  import { BlockIdentifier } from '../../provider/utils';
3
3
  import { BigNumberish } from '../../utils/number';
4
- import { Abi, BlockNumber, EntryPointType, RawCalldata, Signature, Status, TransactionStatus } from '../lib';
5
- import { ContractClass } from '../provider';
4
+ import { Abi, BlockNumber, ContractClass, EntryPointType, RawCalldata, Signature, Status, TransactionStatus } from '../lib';
6
5
  export declare type GetTransactionStatusResponse = {
7
6
  tx_status: Status;
8
7
  block_hash?: string;
@@ -15,15 +14,17 @@ export declare type GetContractAddressesResponse = {
15
14
  Starknet: string;
16
15
  GpsStatementVerifier: string;
17
16
  };
18
- export declare type InvokeFunctionTrace = {
17
+ export declare type FunctionInvocation = {
19
18
  caller_address: string;
20
19
  contract_address: string;
21
- code_address: string;
22
- selector: string;
23
20
  calldata: RawCalldata;
21
+ call_type?: string;
22
+ class_hash?: string;
23
+ selector?: string;
24
+ entry_point_type?: EntryPointType;
24
25
  result: Array<any>;
25
26
  execution_resources: ExecutionResources;
26
- internal_call: Array<InvokeFunctionTrace>;
27
+ internal_calls: Array<FunctionInvocation>;
27
28
  events: Array<any>;
28
29
  messages: Array<any>;
29
30
  };
@@ -40,18 +41,9 @@ export declare type ExecutionResources = {
40
41
  n_memory_holes: number;
41
42
  };
42
43
  export declare type GetTransactionTraceResponse = {
43
- function_invocation: {
44
- caller_address: string;
45
- contract_address: string;
46
- code_address: string;
47
- selector: string;
48
- calldata: RawArgs;
49
- result: Array<any>;
50
- execution_resources: ExecutionResources;
51
- internal_call: Array<any>;
52
- events: Array<any>;
53
- messages: Array<any>;
54
- };
44
+ validate_invocation?: FunctionInvocation;
45
+ function_invocation?: FunctionInvocation;
46
+ fee_transfer_invocation?: FunctionInvocation;
55
47
  signature: Signature;
56
48
  };
57
49
  export declare type RawArgs = {
@@ -60,13 +52,21 @@ export declare type RawArgs = {
60
52
  [k: string]: BigNumberish;
61
53
  };
62
54
  };
55
+ export declare type CallL1Handler = {
56
+ from_address: string;
57
+ to_address: string;
58
+ entry_point_selector: string;
59
+ payload: Array<string>;
60
+ };
63
61
  export declare namespace Sequencer {
64
62
  type DeclareTransaction = {
65
63
  type: 'DECLARE';
64
+ sender_address: string;
66
65
  contract_class: ContractClass;
66
+ signature?: Signature;
67
67
  nonce: BigNumberish;
68
- sender_address: BigNumberish;
69
- signature: Signature;
68
+ max_fee?: BigNumberish;
69
+ version?: BigNumberish;
70
70
  };
71
71
  type DeployTransaction = {
72
72
  type: 'DEPLOY';
@@ -75,6 +75,16 @@ export declare namespace Sequencer {
75
75
  constructor_calldata: string[];
76
76
  nonce?: BigNumberish;
77
77
  };
78
+ type DeployAccountTransaction = {
79
+ type: 'DEPLOY_ACCOUNT';
80
+ class_hash: string;
81
+ contract_address_salt: BigNumberish;
82
+ constructor_calldata: string[];
83
+ signature?: Signature;
84
+ max_fee?: BigNumberish;
85
+ version?: BigNumberish;
86
+ nonce?: BigNumberish;
87
+ };
78
88
  type InvokeFunctionTransaction = {
79
89
  type: 'INVOKE_FUNCTION';
80
90
  contract_address: string;
@@ -85,7 +95,7 @@ export declare namespace Sequencer {
85
95
  max_fee?: BigNumberish;
86
96
  version?: BigNumberish;
87
97
  };
88
- type Transaction = DeclareTransaction | DeployTransaction | InvokeFunctionTransaction;
98
+ type Transaction = DeclareTransaction | DeployTransaction | InvokeFunctionTransaction | DeployAccountTransaction;
89
99
  type AddTransactionResponse = {
90
100
  transaction_hash: string;
91
101
  code?: TransactionStatus;
@@ -165,6 +175,7 @@ export declare namespace Sequencer {
165
175
  status: Status;
166
176
  gas_price: string;
167
177
  sequencer_address: string;
178
+ starknet_version: string;
168
179
  };
169
180
  type CallContractTransaction = Omit<InvokeFunctionTransaction, 'type' | 'entry_point_type' | 'nonce'> & {
170
181
  entry_point_selector: string;
@@ -172,7 +183,11 @@ export declare namespace Sequencer {
172
183
  type CallContractResponse = {
173
184
  result: string[];
174
185
  };
175
- type EstimateFeeTransaction = Omit<InvokeFunctionTransaction, 'max_fee' | 'entry_point_type'>;
186
+ type InvokeEstimateFee = Omit<InvokeFunctionTransaction, 'max_fee' | 'entry_point_type'>;
187
+ type DeclareEstimateFee = Omit<DeclareTransaction, 'max_fee'>;
188
+ type DeployAccountEstimateFee = Omit<DeployAccountTransaction, 'max_fee'>;
189
+ type DeployEstimateFee = DeployTransaction;
190
+ type EstimateFeeRequest = InvokeEstimateFee | DeclareEstimateFee | DeployEstimateFee | DeployAccountEstimateFee;
176
191
  type EstimateFeeResponse = {
177
192
  overall_fee: number;
178
193
  gas_price: number;
@@ -263,7 +278,7 @@ export declare namespace Sequencer {
263
278
  QUERY: {
264
279
  blockIdentifier: BlockIdentifier;
265
280
  };
266
- REQUEST: EstimateFeeTransaction;
281
+ REQUEST: EstimateFeeRequest;
267
282
  RESPONSE: EstimateFeeResponse;
268
283
  };
269
284
  get_class_by_hash: {
@@ -296,5 +311,10 @@ export declare namespace Sequencer {
296
311
  REQUEST: never;
297
312
  RESPONSE: any;
298
313
  };
314
+ estimate_message_fee: {
315
+ QUERY: any;
316
+ REQUEST: any;
317
+ RESPONSE: EstimateFeeResponse;
318
+ };
299
319
  };
300
320
  }
package/types/lib.d.ts CHANGED
@@ -1,16 +1,37 @@
1
1
  import type { ec as EC } from 'elliptic';
2
2
  import type { BigNumberish } from '../utils/number';
3
+ import { RPC } from './api/rpc';
3
4
  export declare type KeyPair = EC.KeyPair;
4
5
  export declare type Signature = string[];
5
6
  export declare type RawCalldata = BigNumberish[];
7
+ export declare type AllowArray<T> = T | T[];
8
+ export interface ContractClass {
9
+ program: CompressedProgram;
10
+ entry_points_by_type: RPC.ContractClass['entry_points_by_type'];
11
+ abi?: Abi;
12
+ }
6
13
  export declare type DeployContractPayload = {
7
14
  contract: CompiledContract | string;
8
15
  constructorCalldata?: RawCalldata;
9
16
  addressSalt?: string;
10
17
  };
18
+ export declare type DeployAccountContractPayload = {
19
+ classHash: BigNumberish;
20
+ constructorCalldata?: RawCalldata;
21
+ addressSalt?: BigNumberish;
22
+ contractAddress?: string;
23
+ };
24
+ export declare type DeployAccountContractTransaction = Omit<DeployAccountContractPayload, 'contractAddress'> & {
25
+ signature?: Signature;
26
+ };
11
27
  export declare type DeclareContractPayload = {
12
28
  contract: CompiledContract | string;
13
- version?: BigNumberish;
29
+ classHash: BigNumberish;
30
+ };
31
+ export declare type DeclareContractTransaction = {
32
+ contractDefinition: ContractClass;
33
+ senderAddress: string;
34
+ signature?: Signature;
14
35
  };
15
36
  export declare type CallDetails = {
16
37
  contractAddress: string;
@@ -32,7 +53,7 @@ export declare type InvocationsDetailsWithNonce = InvocationsDetails & {
32
53
  };
33
54
  export declare type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_L1' | 'REJECTED';
34
55
  export declare type TransactionStatus = 'TRANSACTION_RECEIVED';
35
- export declare type Type = 'DECLARE' | 'DEPLOY' | 'INVOKE_FUNCTION';
56
+ export declare type TransactionType = 'DECLARE' | 'DEPLOY' | 'INVOKE_FUNCTION' | 'DEPLOY_ACCOUNT';
36
57
  export declare type EntryPointType = 'EXTERNAL';
37
58
  export declare type CompressedProgram = string;
38
59
  export declare type AbiEntry = {
@@ -3,8 +3,7 @@
3
3
  * Intersection (sequencer response ∩ (∪ rpc responses))
4
4
  */
5
5
  import BN from 'bn.js';
6
- import { RPC } from './api/rpc';
7
- import { Abi, CompressedProgram, RawCalldata, Signature, Status } from './lib';
6
+ import { AllowArray, Call, DeclareContractPayload, DeployAccountContractPayload, RawCalldata, Signature, Status } from './lib';
8
7
  export interface GetBlockResponse {
9
8
  timestamp: number;
10
9
  block_hash: string;
@@ -13,6 +12,10 @@ export interface GetBlockResponse {
13
12
  parent_hash: string;
14
13
  status: Status;
15
14
  transactions: Array<string>;
15
+ gas_price?: string;
16
+ sequencer_address?: string;
17
+ starknet_version?: string;
18
+ transaction_receipts?: any;
16
19
  }
17
20
  export interface GetCodeResponse {
18
21
  bytecode: string[];
@@ -34,11 +37,6 @@ export interface ContractEntryPoint {
34
37
  offset: string;
35
38
  selector: string;
36
39
  }
37
- export interface ContractClass {
38
- program: CompressedProgram;
39
- entry_points_by_type: RPC.ContractClass['entry_points_by_type'];
40
- abi?: Abi;
41
- }
42
40
  export interface DeclareTransactionResponse extends CommonTransactionResponse {
43
41
  contract_class?: any;
44
42
  sender_address?: string;
@@ -46,7 +44,7 @@ export interface DeclareTransactionResponse extends CommonTransactionResponse {
46
44
  export declare type GetTransactionReceiptResponse = InvokeTransactionReceiptResponse | DeclareTransactionReceiptResponse;
47
45
  export interface CommonTransactionReceiptResponse {
48
46
  transaction_hash: string;
49
- status: Status;
47
+ status?: Status;
50
48
  actual_fee?: string;
51
49
  status_data?: string;
52
50
  }
@@ -64,8 +62,9 @@ export interface MessageToL2 {
64
62
  payload: Array<string>;
65
63
  }
66
64
  export interface InvokeTransactionReceiptResponse extends CommonTransactionReceiptResponse {
67
- messages_sent: Array<MessageToL1>;
68
- events: Array<Event>;
65
+ /** @deprecated Use l2_to_l1_messages */
66
+ messages_sent?: Array<MessageToL1>;
67
+ events?: Array<Event>;
69
68
  l1_origin_message?: MessageToL2;
70
69
  }
71
70
  export declare type DeclareTransactionReceiptResponse = CommonTransactionReceiptResponse;
@@ -88,3 +87,13 @@ export interface DeclareContractResponse {
88
87
  export declare type CallContractResponse = {
89
88
  result: Array<string>;
90
89
  };
90
+ export declare type EstimateFeeAction = {
91
+ type: 'INVOKE';
92
+ payload: AllowArray<Call>;
93
+ } | {
94
+ type: 'DECLARE';
95
+ payload: DeclareContractPayload;
96
+ } | {
97
+ type: 'DEPLOY_ACCOUNT';
98
+ payload: DeployAccountContractPayload;
99
+ };
package/types/signer.d.ts CHANGED
@@ -1,6 +1,19 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import { InvocationsDetails } from './lib';
2
+ import { BigNumberish } from '../utils/number';
3
+ import { DeployAccountContractPayload, InvocationsDetails } from './lib';
3
4
  export interface InvocationsSignerDetails extends Required<InvocationsDetails> {
4
5
  walletAddress: string;
5
6
  chainId: StarknetChainId;
6
7
  }
8
+ export interface DeclareSignerDetails {
9
+ classHash: BigNumberish;
10
+ senderAddress: BigNumberish;
11
+ chainId: StarknetChainId;
12
+ maxFee: BigNumberish;
13
+ version: BigNumberish;
14
+ nonce: BigNumberish;
15
+ }
16
+ export declare type DeployAccountSignerDetails = Required<DeployAccountContractPayload> & Required<InvocationsDetails> & {
17
+ contractAddress: BigNumberish;
18
+ chainId: StarknetChainId;
19
+ };
package/utils/hash.d.ts CHANGED
@@ -21,9 +21,17 @@ export declare function starknetKeccak(value: string): BN;
21
21
  * @returns hex selector of given abi function name
22
22
  */
23
23
  export declare function getSelectorFromName(funcName: string): string;
24
+ /**
25
+ * Function to get hex selector from function name, decimal string or hex string
26
+ * @param value hex string | decimal string | string
27
+ * @returns Hex selector
28
+ */
29
+ export declare function getSelector(value: string): string;
24
30
  export declare function pedersen(input: [BigNumberish, BigNumberish]): string;
25
31
  export declare function computeHashOnElements(data: BigNumberish[]): string;
26
32
  export declare function calculateTransactionHashCommon(txHashPrefix: TransactionHashPrefix, version: BigNumberish, contractAddress: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId, additionalData?: BigNumberish[]): string;
27
33
  export declare function calculateDeployTransactionHash(contractAddress: BigNumberish, constructorCalldata: BigNumberish[], version: BigNumberish, chainId: StarknetChainId): string;
34
+ export declare function calculateDeclareTransactionHash(classHash: BigNumberish, senderAddress: BigNumberish, version: BigNumberish, maxFee: BigNumberish, chainId: StarknetChainId, nonce: BigNumberish): string;
35
+ export declare function calculateDeployAccountTransactionHash(contractAddress: BigNumberish, classHash: BigNumberish, constructorCalldata: BigNumberish[], salt: BigNumberish, version: BigNumberish, maxFee: BigNumberish, chainId: StarknetChainId, nonce: BigNumberish): string;
28
36
  export declare function calculateTransactionHash(contractAddress: BigNumberish, version: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId, nonce: BigNumberish): string;
29
37
  export declare function calculateContractAddressFromHash(salt: BigNumberish, classHash: BigNumberish, constructorCalldata: RawCalldata, deployerAddress: BigNumberish): string;
package/utils/hash.js CHANGED
@@ -28,7 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  return (mod && mod.__esModule) ? mod : { "default": mod };
29
29
  };
30
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.calculateContractAddressFromHash = exports.calculateTransactionHash = exports.calculateDeployTransactionHash = exports.calculateTransactionHashCommon = exports.computeHashOnElements = exports.pedersen = exports.getSelectorFromName = exports.starknetKeccak = exports.keccakBn = exports.feeTransactionVersion = exports.transactionVersion = void 0;
31
+ exports.calculateContractAddressFromHash = exports.calculateTransactionHash = exports.calculateDeployAccountTransactionHash = exports.calculateDeclareTransactionHash = exports.calculateDeployTransactionHash = exports.calculateTransactionHashCommon = exports.computeHashOnElements = exports.pedersen = exports.getSelector = exports.getSelectorFromName = exports.starknetKeccak = exports.keccakBn = exports.feeTransactionVersion = exports.transactionVersion = void 0;
32
32
  var keccak_1 = require("ethereum-cryptography/keccak");
33
33
  var utils_1 = require("ethereum-cryptography/utils");
34
34
  var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
@@ -66,10 +66,25 @@ exports.starknetKeccak = starknetKeccak;
66
66
  * @returns hex selector of given abi function name
67
67
  */
68
68
  function getSelectorFromName(funcName) {
69
- // sometimes BigInteger pads the hex string with zeros, which isnt allowed in the starknet api
69
+ // sometimes BigInteger pads the hex string with zeros, which is not allowed in the starknet api
70
70
  return (0, number_1.toHex)(starknetKeccak(funcName));
71
71
  }
72
72
  exports.getSelectorFromName = getSelectorFromName;
73
+ /**
74
+ * Function to get hex selector from function name, decimal string or hex string
75
+ * @param value hex string | decimal string | string
76
+ * @returns Hex selector
77
+ */
78
+ function getSelector(value) {
79
+ if ((0, number_1.isHex)(value)) {
80
+ return value;
81
+ }
82
+ if ((0, number_1.isStringWholeNumber)(value)) {
83
+ return (0, number_1.toHexString)(value);
84
+ }
85
+ return getSelectorFromName(value);
86
+ }
87
+ exports.getSelector = getSelector;
73
88
  var constantPoints = constants_1.CONSTANT_POINTS.map(function (coords) {
74
89
  return ellipticCurve_1.ec.curve.point(coords[0], coords[1]);
75
90
  });
@@ -117,6 +132,17 @@ function calculateDeployTransactionHash(contractAddress, constructorCalldata, ve
117
132
  return calculateTransactionHashCommon(constants_1.TransactionHashPrefix.DEPLOY, version, contractAddress, getSelectorFromName('constructor'), constructorCalldata, constants_1.ZERO, chainId);
118
133
  }
119
134
  exports.calculateDeployTransactionHash = calculateDeployTransactionHash;
135
+ function calculateDeclareTransactionHash(
136
+ // contractClass: ContractClass, // Should be used once class hash is present in ContractClass
137
+ classHash, senderAddress, version, maxFee, chainId, nonce) {
138
+ return calculateTransactionHashCommon(constants_1.TransactionHashPrefix.DECLARE, version, senderAddress, 0, [classHash], maxFee, chainId, [nonce]);
139
+ }
140
+ exports.calculateDeclareTransactionHash = calculateDeclareTransactionHash;
141
+ function calculateDeployAccountTransactionHash(contractAddress, classHash, constructorCalldata, salt, version, maxFee, chainId, nonce) {
142
+ var calldata = __spreadArray([classHash, salt], __read(constructorCalldata), false);
143
+ return calculateTransactionHashCommon(constants_1.TransactionHashPrefix.DEPLOY_ACCOUNT, version, contractAddress, 0, calldata, maxFee, chainId, [nonce]);
144
+ }
145
+ exports.calculateDeployAccountTransactionHash = calculateDeployAccountTransactionHash;
120
146
  function calculateTransactionHash(contractAddress, version, calldata, maxFee, chainId, nonce) {
121
147
  return calculateTransactionHashCommon(constants_1.TransactionHashPrefix.INVOKE, version, contractAddress, 0, calldata, maxFee, chainId, [nonce]);
122
148
  }
package/utils/merkle.js CHANGED
@@ -27,6 +27,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
27
27
  Object.defineProperty(exports, "__esModule", { value: true });
28
28
  exports.proofMerklePath = exports.MerkleTree = void 0;
29
29
  var hash_1 = require("./hash");
30
+ var number_1 = require("./number");
30
31
  var MerkleTree = /** @class */ (function () {
31
32
  function MerkleTree(leafHashes) {
32
33
  this.branches = [];
@@ -43,7 +44,7 @@ var MerkleTree = /** @class */ (function () {
43
44
  var newLeaves = [];
44
45
  for (var i = 0; i < leaves.length; i += 2) {
45
46
  if (i + 1 === leaves.length) {
46
- newLeaves.push(leaves[i]);
47
+ newLeaves.push(MerkleTree.hash(leaves[i], '0x0'));
47
48
  }
48
49
  else {
49
50
  newLeaves.push(MerkleTree.hash(leaves[i], leaves[i + 1]));
@@ -52,7 +53,7 @@ var MerkleTree = /** @class */ (function () {
52
53
  return this.build(newLeaves);
53
54
  };
54
55
  MerkleTree.hash = function (a, b) {
55
- var _a = __read([a, b].sort(), 2), aSorted = _a[0], bSorted = _a[1];
56
+ var _a = __read([(0, number_1.toBN)(a), (0, number_1.toBN)(b)].sort(function (x, y) { return (x.gte(y) ? 1 : -1); }), 2), aSorted = _a[0], bSorted = _a[1];
56
57
  return (0, hash_1.pedersen)([aSorted, bSorted]);
57
58
  };
58
59
  MerkleTree.prototype.getProof = function (leaf, branch, hashPath) {
@@ -73,9 +74,7 @@ var MerkleTree = /** @class */ (function () {
73
74
  ? -1
74
75
  : this.branches.findIndex(function (b) { return b.length === branch.length; });
75
76
  var nextBranch = (_b = this.branches[currentBranchLevelIndex + 1]) !== null && _b !== void 0 ? _b : [this.root];
76
- return this.getProof(neededBranch === '0x0'
77
- ? leaf
78
- : MerkleTree.hash(isLeft ? leaf : neededBranch, isLeft ? neededBranch : leaf), nextBranch, newHashPath);
77
+ return this.getProof(MerkleTree.hash(isLeft ? leaf : neededBranch, isLeft ? neededBranch : leaf), nextBranch, newHashPath);
79
78
  };
80
79
  return MerkleTree;
81
80
  }());
package/utils/number.d.ts CHANGED
@@ -8,3 +8,8 @@ export declare function toFelt(num: BigNumberish): string;
8
8
  export declare function assertInRange(input: BigNumberish, lowerBound: BigNumberish, upperBound: BigNumberish, inputName?: string): void;
9
9
  export declare function bigNumberishArrayToDecimalStringArray(rawCalldata: BigNumberish[]): string[];
10
10
  export declare function bigNumberishArrayToHexadecimalStringArray(rawCalldata: BigNumberish[]): string[];
11
+ export declare const isStringWholeNumber: (value: string) => boolean;
12
+ export declare const toHexString: (value: string) => string;
13
+ export declare function getDecimalString(value: string): string;
14
+ export declare function getHexString(value: string): string;
15
+ export declare function getHexStringArray(value: Array<string>): string[];
package/utils/number.js CHANGED
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.bigNumberishArrayToHexadecimalStringArray = exports.bigNumberishArrayToDecimalStringArray = exports.assertInRange = exports.toFelt = exports.hexToDecimalString = exports.toHex = exports.toBN = exports.isHex = void 0;
29
+ exports.getHexStringArray = exports.getHexString = exports.getDecimalString = exports.toHexString = exports.isStringWholeNumber = exports.bigNumberishArrayToHexadecimalStringArray = exports.bigNumberishArrayToDecimalStringArray = exports.assertInRange = exports.toFelt = exports.hexToDecimalString = exports.toHex = exports.toBN = exports.isHex = void 0;
30
30
  var bn_js_1 = __importStar(require("bn.js"));
31
31
  var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
32
32
  var encode_1 = require("./encode");
@@ -80,3 +80,31 @@ function bigNumberishArrayToHexadecimalStringArray(rawCalldata) {
80
80
  return rawCalldata.map(function (x) { return toHex(toBN(x)); });
81
81
  }
82
82
  exports.bigNumberishArrayToHexadecimalStringArray = bigNumberishArrayToHexadecimalStringArray;
83
+ var isStringWholeNumber = function (value) { return /^\d+$/.test(value); };
84
+ exports.isStringWholeNumber = isStringWholeNumber;
85
+ var toHexString = function (value) { return toHex(toBN(value)); };
86
+ exports.toHexString = toHexString;
87
+ function getDecimalString(value) {
88
+ if (isHex(value)) {
89
+ return hexToDecimalString(value);
90
+ }
91
+ if ((0, exports.isStringWholeNumber)(value)) {
92
+ return value;
93
+ }
94
+ throw new Error("".concat(value, " need to be hex-string or whole-number-string"));
95
+ }
96
+ exports.getDecimalString = getDecimalString;
97
+ function getHexString(value) {
98
+ if (isHex(value)) {
99
+ return value;
100
+ }
101
+ if ((0, exports.isStringWholeNumber)(value)) {
102
+ return (0, exports.toHexString)(value);
103
+ }
104
+ throw new Error("".concat(value, " need to be hex-string or whole-number-string"));
105
+ }
106
+ exports.getHexString = getHexString;
107
+ function getHexStringArray(value) {
108
+ return value.map(function (el) { return getHexString(el); });
109
+ }
110
+ exports.getHexStringArray = getHexStringArray;
@@ -2,7 +2,7 @@
2
2
  * Map RPC Response to common interface response
3
3
  * Intersection (sequencer response ∩ (∪ rpc responses))
4
4
  */
5
- import { CallContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse } from '../../types';
5
+ import { CallContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionResponse } from '../../types';
6
6
  import { RPC } from '../../types/api';
7
7
  import { ResponseParser } from '.';
8
8
  declare type RpcGetBlockResponse = RPC.GetBlockWithTxHashesResponse & {
@@ -11,13 +11,9 @@ declare type RpcGetBlockResponse = RPC.GetBlockWithTxHashesResponse & {
11
11
  declare type GetTransactionByHashResponse = RPC.GetTransactionByHashResponse & {
12
12
  [key: string]: any;
13
13
  };
14
- declare type TransactionReceipt = RPC.TransactionReceipt & {
15
- [key: string]: any;
16
- };
17
- export declare class RPCResponseParser implements Omit<ResponseParser, 'parseDeclareContractResponse' | 'parseDeployContractResponse' | 'parseInvokeFunctionResponse'> {
14
+ export declare class RPCResponseParser implements Omit<ResponseParser, 'parseDeclareContractResponse' | 'parseDeployContractResponse' | 'parseInvokeFunctionResponse' | 'parseGetTransactionReceiptResponse'> {
18
15
  parseGetBlockResponse(res: RpcGetBlockResponse): GetBlockResponse;
19
16
  parseGetTransactionResponse(res: GetTransactionByHashResponse): GetTransactionResponse;
20
- parseGetTransactionReceiptResponse(res: TransactionReceipt): GetTransactionReceiptResponse;
21
17
  parseFeeEstimateResponse(res: RPC.EstimateFeeResponse): EstimateFeeResponse;
22
18
  parseCallContractResponse(res: Array<string>): CallContractResponse;
23
19
  }
@@ -27,17 +27,6 @@ var RPCResponseParser = /** @class */ (function () {
27
27
  version: res.version,
28
28
  };
29
29
  };
30
- RPCResponseParser.prototype.parseGetTransactionReceiptResponse = function (res) {
31
- return {
32
- transaction_hash: res.transaction_hash,
33
- actual_fee: res.actual_fee,
34
- status: res.status,
35
- status_data: res.status_data,
36
- messages_sent: res.messages_sent,
37
- l1_origin_message: res.l1_origin_message,
38
- events: res.events,
39
- };
40
- };
41
30
  RPCResponseParser.prototype.parseFeeEstimateResponse = function (res) {
42
31
  return {
43
32
  overall_fee: (0, number_1.toBN)(res.overall_fee),
@@ -35,46 +35,24 @@ var SequencerAPIResponseParser = /** @class */ (function (_super) {
35
35
  return _super !== null && _super.apply(this, arguments) || this;
36
36
  }
37
37
  SequencerAPIResponseParser.prototype.parseGetBlockResponse = function (res) {
38
- return {
39
- timestamp: res.timestamp,
40
- block_hash: res.block_hash,
41
- block_number: res.block_number,
42
- new_root: res.state_root,
43
- parent_hash: res.parent_block_hash,
44
- status: res.status,
45
- transactions: Object.values(res.transactions)
38
+ return __assign(__assign({}, res), { new_root: res.state_root, parent_hash: res.parent_block_hash, transactions: Object.values(res.transactions)
46
39
  .map(function (value) { return 'transaction_hash' in value && value.transaction_hash; })
47
- .filter(Boolean),
48
- };
40
+ .filter(Boolean) });
49
41
  };
50
42
  SequencerAPIResponseParser.prototype.parseGetTransactionResponse = function (res) {
51
- return {
52
- calldata: 'calldata' in res.transaction ? res.transaction.calldata : [],
53
- contract_address: 'contract_address' in res.transaction ? res.transaction.contract_address : undefined,
54
- contract_class: 'contract_class' in res.transaction ? res.transaction.contract_class : undefined,
55
- entry_point_selector: 'entry_point_selector' in res.transaction
43
+ return __assign(__assign({}, res), { calldata: 'calldata' in res.transaction ? res.transaction.calldata : [], contract_address: 'contract_address' in res.transaction ? res.transaction.contract_address : undefined, contract_class: 'contract_class' in res.transaction ? res.transaction.contract_class : undefined, entry_point_selector: 'entry_point_selector' in res.transaction
56
44
  ? res.transaction.entry_point_selector
57
- : undefined,
58
- max_fee: 'max_fee' in res.transaction ? res.transaction.max_fee : undefined,
59
- nonce: res.transaction.nonce,
60
- sender_address: 'sender_address' in res.transaction
45
+ : undefined, max_fee: 'max_fee' in res.transaction ? res.transaction.max_fee : undefined, nonce: res.transaction.nonce, sender_address: 'sender_address' in res.transaction
61
46
  ? res.transaction.sender_address
62
- : undefined,
63
- signature: 'signature' in res.transaction ? res.transaction.signature : undefined,
64
- transaction_hash: 'transaction_hash' in res.transaction ? res.transaction.transaction_hash : undefined,
65
- version: 'version' in res.transaction ? res.transaction.version : undefined,
66
- };
47
+ : undefined, signature: 'signature' in res.transaction ? res.transaction.signature : undefined, transaction_hash: 'transaction_hash' in res.transaction ? res.transaction.transaction_hash : undefined, version: 'version' in res.transaction ? res.transaction.version : undefined });
67
48
  };
68
49
  SequencerAPIResponseParser.prototype.parseGetTransactionReceiptResponse = function (res) {
69
- return {
70
- transaction_hash: res.transaction_hash,
71
- actual_fee: 'actual_fee' in res ? res.actual_fee : undefined,
72
- status: res.status,
73
- status_data: undefined,
74
- messages_sent: res.l2_to_l1_messages,
75
- events: res.events,
76
- l1_origin_message: undefined,
77
- };
50
+ return __assign(__assign(__assign(__assign(__assign(__assign(__assign({ transaction_hash: res.transaction_hash, status: res.status, messages_sent: res.l2_to_l1_messages, events: res.events }, ('block_hash' in res && { block_hash: res.block_hash })), ('block_number' in res && { block_number: res.block_number })), ('actual_fee' in res && { actual_fee: res.actual_fee })), ('transaction_index' in res && { transaction_index: res.transaction_index })), ('execution_resources' in res && { execution_resources: res.execution_resources })), ('l1_to_l2_consumed_message' in res && {
51
+ // eslint-disable-next-line @typescript-eslint/dot-notation
52
+ l1_to_l2_consumed_message: res['l1_to_l2_consumed_message'],
53
+ })), ('transaction_failure_reason' in res && {
54
+ transaction_failure_reason: res.transaction_failure_reason,
55
+ }));
78
56
  };
79
57
  SequencerAPIResponseParser.prototype.parseFeeEstimateResponse = function (res) {
80
58
  if ('overall_fee' in res) {
@@ -30,7 +30,7 @@ Gets the new Nonce of the connected account for the next transaction.
30
30
 
31
31
  <hr />
32
32
 
33
- account.**estimateFee**(calls [ , options ]) => _Promise < EstimateFeeResponse >_
33
+ account.**estimateInvokeFee**(calls [ , options ]) => _Promise < EstimateFeeResponse >_
34
34
 
35
35
  Gets the estimated fee for the call(s).
36
36
 
@@ -51,6 +51,27 @@ The _options_ object may include any of:
51
51
 
52
52
  <hr />
53
53
 
54
+ account.**estimateDeclareFee**(contractPayload [ , options ]) => _Promise < EstimateFeeResponse >_
55
+
56
+ Gets the estimated fee for the declare transaction.
57
+
58
+ The _options_ object may include any of:
59
+
60
+ - options.**blockIdentifier** - Block Identifier for the transaction
61
+ - options.**nonce** - Nonce for the transaction
62
+
63
+ ###### _EstimateFeeResponse_
64
+
65
+ ```typescript
66
+ {
67
+ overall_fee: BN;
68
+ gas_consumed?: BN;
69
+ gas_price?: BN;
70
+ }
71
+ ```
72
+
73
+ <hr />
74
+
54
75
  account.**execute**(calls [ , abi , transactionsDetail ]) => _Promise < AddTransactionResponse >_
55
76
 
56
77
  Executes one or multiple calls using the account contract.
@@ -71,6 +92,44 @@ The _transactionsDetail_ object may include any of:
71
92
 
72
93
  <hr />
73
94
 
95
+ account.**declare**(payload [ , transactionsDetail ]) => _Promise < DeclareContractResponse >_
96
+
97
+ The _payload_ object consists of:
98
+
99
+ - payload.**contract** - The compiled contract
100
+ - payload.**classHash** - Hash of the compiled contract
101
+
102
+ The _transactionsDetail_ object may include any of:
103
+
104
+ - transactionsDetail.**maxFee** - Max Fee that that will be used to execute the call(s)
105
+ - transactionsDetail.**nonce** - Nonce for the transaction
106
+ - transactionsDetail.**version** - Version for the transaction (default is 1)
107
+
108
+ Declares a contract on Starknet.
109
+
110
+ > _Note:_ Once the classHash is included in CompiledContract, this parameter can be removed. Currently it can be pre-computed from starknet-cli.
111
+
112
+ Example:
113
+
114
+ ```typescript
115
+ const declareTx = await account.declare({
116
+ contract: compiledErc20,
117
+ // classHash is pre-computed from starknet-cli
118
+ classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
119
+ });
120
+ ```
121
+
122
+ ###### _DeclareContractResponse_
123
+
124
+ ```typescript
125
+ {
126
+ transaction_hash: string;
127
+ class_hash: string;
128
+ };
129
+ ```
130
+
131
+ <hr/>
132
+
74
133
  account.**signMessage**(typedData) => _Promise < Signature >_
75
134
 
76
135
  Creates a signature from the passed data.