starknet 4.17.0 → 4.18.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 (150) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +2 -10
  3. package/dist/index.d.ts +27 -9
  4. package/dist/index.global.js +30 -13
  5. package/dist/index.global.js.map +1 -1
  6. package/dist/index.js +30 -13
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.mjs +30 -13
  9. package/dist/index.mjs.map +1 -1
  10. package/package.json +5 -1
  11. package/.babelrc +0 -6
  12. package/.commitlintrc +0 -22
  13. package/.eslintignore +0 -2
  14. package/.eslintrc +0 -28
  15. package/.prettierrc +0 -6
  16. package/.releaserc +0 -28
  17. package/CODE_OF_CONDUCT.md +0 -128
  18. package/CONTRIBUTING.md +0 -52
  19. package/__mocks__/Account.json +0 -25486
  20. package/__mocks__/ERC20.json +0 -40027
  21. package/__mocks__/TestDapp.json +0 -12962
  22. package/__mocks__/contract.json +0 -33191
  23. package/__mocks__/l1l2_compiled.json +0 -10107
  24. package/__mocks__/multicall.json +0 -8139
  25. package/__mocks__/naming_compiled.json +0 -53283
  26. package/__mocks__/starknetId_compiled.json +0 -44703
  27. package/__mocks__/typedDataExample.json +0 -35
  28. package/__mocks__/typedDataSessionExample.json +0 -42
  29. package/__mocks__/typedDataStructArrayExample.json +0 -44
  30. package/__tests__/account.test.ts +0 -345
  31. package/__tests__/contract.test.ts +0 -249
  32. package/__tests__/defaultProvider.test.ts +0 -177
  33. package/__tests__/fixtures.ts +0 -84
  34. package/__tests__/jest.setup.ts +0 -30
  35. package/__tests__/rpcProvider.test.ts +0 -132
  36. package/__tests__/sequencerProvider.test.ts +0 -123
  37. package/__tests__/utils/__snapshots__/ellipticalCurve.test.ts.snap +0 -5
  38. package/__tests__/utils/__snapshots__/utils.browser.test.ts.snap +0 -5
  39. package/__tests__/utils/__snapshots__/utils.test.ts.snap +0 -5
  40. package/__tests__/utils/address.test.ts +0 -45
  41. package/__tests__/utils/ellipticalCurve.test.ts +0 -91
  42. package/__tests__/utils/merkle.test.ts +0 -256
  43. package/__tests__/utils/shortString.test.ts +0 -22
  44. package/__tests__/utils/starknetId.test.ts +0 -53
  45. package/__tests__/utils/transactionHash.test.ts +0 -17
  46. package/__tests__/utils/typedData.test.ts +0 -223
  47. package/__tests__/utils/uint256.test.ts +0 -32
  48. package/__tests__/utils/utils.browser.test.ts +0 -29
  49. package/__tests__/utils/utils.test.ts +0 -141
  50. package/img/logo.svg +0 -17
  51. package/index.d.ts +0 -2683
  52. package/index.global.js +0 -23192
  53. package/index.global.js.map +0 -1
  54. package/index.js +0 -4910
  55. package/index.js.map +0 -1
  56. package/index.mjs +0 -4852
  57. package/index.mjs.map +0 -1
  58. package/src/account/default.ts +0 -501
  59. package/src/account/index.ts +0 -2
  60. package/src/account/interface.ts +0 -309
  61. package/src/constants.ts +0 -2065
  62. package/src/contract/contractFactory.ts +0 -88
  63. package/src/contract/default.ts +0 -425
  64. package/src/contract/index.ts +0 -3
  65. package/src/contract/interface.ts +0 -106
  66. package/src/index.ts +0 -26
  67. package/src/provider/default.ts +0 -187
  68. package/src/provider/errors.ts +0 -14
  69. package/src/provider/index.ts +0 -9
  70. package/src/provider/interface.ts +0 -281
  71. package/src/provider/rpc.ts +0 -482
  72. package/src/provider/sequencer.ts +0 -523
  73. package/src/provider/utils.ts +0 -100
  74. package/src/signer/default.ts +0 -99
  75. package/src/signer/index.ts +0 -2
  76. package/src/signer/interface.ts +0 -77
  77. package/src/types/account.ts +0 -43
  78. package/src/types/api/index.ts +0 -10
  79. package/src/types/api/openrpc.ts +0 -607
  80. package/src/types/api/rpc.ts +0 -45
  81. package/src/types/api/sequencer.ts +0 -369
  82. package/src/types/contract.ts +0 -5
  83. package/src/types/index.ts +0 -8
  84. package/src/types/lib.ts +0 -143
  85. package/src/types/provider.ts +0 -134
  86. package/src/types/signer.ts +0 -24
  87. package/src/utils/address.ts +0 -44
  88. package/src/utils/calldata.ts +0 -250
  89. package/src/utils/ellipticCurve.ts +0 -120
  90. package/src/utils/encode.ts +0 -66
  91. package/src/utils/events.ts +0 -32
  92. package/src/utils/fetchPonyfill.ts +0 -5
  93. package/src/utils/hash.ts +0 -235
  94. package/src/utils/json.ts +0 -15
  95. package/src/utils/merkle.ts +0 -69
  96. package/src/utils/number.ts +0 -98
  97. package/src/utils/provider.ts +0 -28
  98. package/src/utils/responseParser/index.ts +0 -28
  99. package/src/utils/responseParser/rpc.ts +0 -70
  100. package/src/utils/responseParser/sequencer.ts +0 -135
  101. package/src/utils/shortString.ts +0 -21
  102. package/src/utils/stark.ts +0 -57
  103. package/src/utils/starknetId.ts +0 -116
  104. package/src/utils/transaction.ts +0 -56
  105. package/src/utils/typedData/index.ts +0 -247
  106. package/src/utils/typedData/types.ts +0 -37
  107. package/src/utils/typedData/utils.ts +0 -16
  108. package/src/utils/uint256.ts +0 -32
  109. package/src/utils/url.ts +0 -53
  110. package/tsconfig.eslint.json +0 -4
  111. package/tsup.config.ts +0 -9
  112. package/www/README.md +0 -41
  113. package/www/babel.config.js +0 -3
  114. package/www/code-examples/account.js +0 -65
  115. package/www/code-examples/amm.js +0 -44
  116. package/www/code-examples/erc20.js +0 -13
  117. package/www/code-examples/package-lock.json +0 -336
  118. package/www/code-examples/package.json +0 -15
  119. package/www/docs/API/_category_.json +0 -5
  120. package/www/docs/API/account.md +0 -463
  121. package/www/docs/API/changelog.md +0 -15
  122. package/www/docs/API/contract.md +0 -135
  123. package/www/docs/API/contractFactory.md +0 -45
  124. package/www/docs/API/index.md +0 -3
  125. package/www/docs/API/provider.md +0 -725
  126. package/www/docs/API/signer.md +0 -105
  127. package/www/docs/API/utils.md +0 -374
  128. package/www/docusaurus.config.js +0 -130
  129. package/www/guides/account.md +0 -140
  130. package/www/guides/cra.md +0 -3
  131. package/www/guides/erc20.md +0 -163
  132. package/www/guides/intro.md +0 -63
  133. package/www/package-lock.json +0 -20405
  134. package/www/package.json +0 -43
  135. package/www/sidebars.js +0 -31
  136. package/www/src/components/HomepageFeatures/index.tsx +0 -67
  137. package/www/src/components/HomepageFeatures/styles.module.css +0 -10
  138. package/www/src/css/custom.css +0 -39
  139. package/www/src/pages/index.module.css +0 -23
  140. package/www/src/pages/index.tsx +0 -40
  141. package/www/src/pages/markdown-page.md +0 -7
  142. package/www/static/.nojekyll +0 -0
  143. package/www/static/img/docusaurus.png +0 -0
  144. package/www/static/img/favicon.ico +0 -0
  145. package/www/static/img/logo.svg +0 -17
  146. package/www/static/img/starknet-1.png +0 -0
  147. package/www/static/img/starknet-2.png +0 -0
  148. package/www/static/img/starknet-3.png +0 -0
  149. package/www/static/img/tutorial/docsVersionDropdown.png +0 -0
  150. package/www/static/img/tutorial/localeDropdown.png +0 -0
package/src/utils/hash.ts DELETED
@@ -1,235 +0,0 @@
1
- /* eslint-disable import/extensions */
2
- import BN from 'bn.js';
3
- import { keccak256 } from 'ethereum-cryptography/keccak.js';
4
- import { hexToBytes } from 'ethereum-cryptography/utils.js';
5
- import assert from 'minimalistic-assert';
6
-
7
- import {
8
- CONSTANT_POINTS,
9
- FIELD_PRIME,
10
- MASK_250,
11
- ONE,
12
- StarknetChainId,
13
- TransactionHashPrefix,
14
- ZERO,
15
- } from '../constants';
16
- import { RawCalldata } from '../types';
17
- import { ec } from './ellipticCurve';
18
- import { addHexPrefix, buf2hex, removeHexPrefix, utf8ToArray } from './encode';
19
- import {
20
- BigNumberish,
21
- isHex,
22
- isStringWholeNumber,
23
- toBN,
24
- toFelt,
25
- toHex,
26
- toHexString,
27
- } from './number';
28
-
29
- export const transactionVersion = 1;
30
- export const feeTransactionVersion = toBN(2).pow(toBN(128)).add(toBN(transactionVersion));
31
-
32
- export function keccakBn(value: BigNumberish): string {
33
- const hexWithoutPrefix = removeHexPrefix(toHex(toBN(value)));
34
- const evenHex = hexWithoutPrefix.length % 2 === 0 ? hexWithoutPrefix : `0${hexWithoutPrefix}`;
35
- return addHexPrefix(buf2hex(keccak256(hexToBytes(evenHex))));
36
- }
37
-
38
- function keccakHex(value: string): string {
39
- return addHexPrefix(buf2hex(keccak256(utf8ToArray(value))));
40
- }
41
-
42
- /**
43
- * Function to get the starknet keccak hash from a string
44
- *
45
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L17-L22)
46
- * @param value - string you want to get the starknetKeccak hash from
47
- * @returns starknet keccak hash as BigNumber
48
- */
49
- export function starknetKeccak(value: string): BN {
50
- return toBN(keccakHex(value)).and(MASK_250);
51
- }
52
-
53
- /**
54
- * Function to get the hex selector from a given function name
55
- *
56
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
57
- * @param funcName - selectors abi function name
58
- * @returns hex selector of given abi function name
59
- */
60
- export function getSelectorFromName(funcName: string) {
61
- // sometimes BigInteger pads the hex string with zeros, which is not allowed in the starknet api
62
- return toHex(starknetKeccak(funcName));
63
- }
64
-
65
- /**
66
- * Function to get hex selector from function name, decimal string or hex string
67
- * @param value hex string | decimal string | string
68
- * @returns Hex selector
69
- */
70
- export function getSelector(value: string) {
71
- if (isHex(value)) {
72
- return value;
73
- }
74
- if (isStringWholeNumber(value)) {
75
- return toHexString(value);
76
- }
77
- return getSelectorFromName(value);
78
- }
79
-
80
- const constantPoints = CONSTANT_POINTS.map((coords: string[]) =>
81
- ec.curve.point(coords[0], coords[1])
82
- );
83
-
84
- export function pedersen(input: [BigNumberish, BigNumberish]) {
85
- let point = constantPoints[0];
86
- for (let i = 0; i < input.length; i += 1) {
87
- let x = toBN(input[i]);
88
- assert(x.gte(ZERO) && x.lt(toBN(addHexPrefix(FIELD_PRIME))), `Invalid input: ${input[i]}`);
89
- if (!x.isZero()) {
90
- for (let j = 0; j < 252; j += 1) {
91
- const pt = constantPoints[2 + i * 252 + j];
92
- assert(!point.getX().eq(pt.getX()));
93
- if (x.and(ONE).toNumber() !== 0) {
94
- point = point.add(pt);
95
- }
96
- x = x.shrn(1);
97
- }
98
- }
99
- }
100
- return addHexPrefix(point.getX().toString(16));
101
- }
102
-
103
- export function computeHashOnElements(data: BigNumberish[]) {
104
- return [...data, data.length].reduce((x, y) => pedersen([x, y]), 0).toString();
105
- }
106
-
107
- // following implementation is based on this python implementation:
108
- // https://github.com/starkware-libs/cairo-lang/blob/b614d1867c64f3fb2cf4a4879348cfcf87c3a5a7/src/starkware/starknet/core/os/transaction_hash/transaction_hash.py
109
-
110
- export function calculateTransactionHashCommon(
111
- txHashPrefix: TransactionHashPrefix,
112
- version: BigNumberish,
113
- contractAddress: BigNumberish,
114
- entryPointSelector: BigNumberish,
115
- calldata: BigNumberish[],
116
- maxFee: BigNumberish,
117
- chainId: StarknetChainId,
118
- additionalData: BigNumberish[] = []
119
- ): string {
120
- const calldataHash = computeHashOnElements(calldata);
121
- const dataToHash = [
122
- txHashPrefix,
123
- version,
124
- contractAddress,
125
- entryPointSelector,
126
- calldataHash,
127
- maxFee,
128
- chainId,
129
- ...additionalData,
130
- ];
131
- return computeHashOnElements(dataToHash);
132
- }
133
-
134
- export function calculateDeployTransactionHash(
135
- contractAddress: BigNumberish,
136
- constructorCalldata: BigNumberish[],
137
- version: BigNumberish,
138
- chainId: StarknetChainId
139
- ): string {
140
- return calculateTransactionHashCommon(
141
- TransactionHashPrefix.DEPLOY,
142
- version,
143
- contractAddress,
144
- getSelectorFromName('constructor'),
145
- constructorCalldata,
146
- ZERO,
147
- chainId
148
- );
149
- }
150
-
151
- export function calculateDeclareTransactionHash(
152
- // contractClass: ContractClass, // Should be used once class hash is present in ContractClass
153
- classHash: BigNumberish,
154
- senderAddress: BigNumberish,
155
- version: BigNumberish,
156
- maxFee: BigNumberish,
157
- chainId: StarknetChainId,
158
- nonce: BigNumberish
159
- ): string {
160
- return calculateTransactionHashCommon(
161
- TransactionHashPrefix.DECLARE,
162
- version,
163
- senderAddress,
164
- 0,
165
- [classHash],
166
- maxFee,
167
- chainId,
168
- [nonce]
169
- );
170
- }
171
-
172
- export function calculateDeployAccountTransactionHash(
173
- contractAddress: BigNumberish,
174
- classHash: BigNumberish,
175
- constructorCalldata: BigNumberish[],
176
- salt: BigNumberish,
177
- version: BigNumberish,
178
- maxFee: BigNumberish,
179
- chainId: StarknetChainId,
180
- nonce: BigNumberish
181
- ) {
182
- const calldata = [classHash, salt, ...constructorCalldata];
183
-
184
- return calculateTransactionHashCommon(
185
- TransactionHashPrefix.DEPLOY_ACCOUNT,
186
- version,
187
- contractAddress,
188
- 0,
189
- calldata,
190
- maxFee,
191
- chainId,
192
- [nonce]
193
- );
194
- }
195
-
196
- export function calculateTransactionHash(
197
- contractAddress: BigNumberish,
198
- version: BigNumberish,
199
- calldata: BigNumberish[],
200
- maxFee: BigNumberish,
201
- chainId: StarknetChainId,
202
- nonce: BigNumberish
203
- ): string {
204
- return calculateTransactionHashCommon(
205
- TransactionHashPrefix.INVOKE,
206
- version,
207
- contractAddress,
208
- 0,
209
- calldata,
210
- maxFee,
211
- chainId,
212
- [nonce]
213
- );
214
- }
215
-
216
- export function calculateContractAddressFromHash(
217
- salt: BigNumberish,
218
- classHash: BigNumberish,
219
- constructorCalldata: RawCalldata,
220
- deployerAddress: BigNumberish
221
- ) {
222
- const constructorCalldataHash = computeHashOnElements(constructorCalldata);
223
-
224
- const CONTRACT_ADDRESS_PREFIX = toFelt('0x535441524b4e45545f434f4e54524143545f41444452455353'); // Equivalent to 'STARKNET_CONTRACT_ADDRESS'
225
-
226
- const dataToHash = [
227
- CONTRACT_ADDRESS_PREFIX,
228
- deployerAddress,
229
- salt,
230
- classHash,
231
- constructorCalldataHash,
232
- ];
233
-
234
- return computeHashOnElements(dataToHash);
235
- }
package/src/utils/json.ts DELETED
@@ -1,15 +0,0 @@
1
- import Json from 'json-bigint';
2
-
3
- const json = (alwaysParseAsBig: boolean) => {
4
- return Json({
5
- alwaysParseAsBig,
6
- useNativeBigInt: true,
7
- protoAction: 'preserve',
8
- constructorAction: 'preserve',
9
- });
10
- };
11
-
12
- export const { parse, stringify } = json(false);
13
- export const { parse: parseAlwaysAsBig, stringify: stringifyAlwaysAsBig } = json(true);
14
-
15
- export default { parse, stringify };
@@ -1,69 +0,0 @@
1
- import { pedersen } from './hash';
2
- import { toBN } from './number';
3
-
4
- export class MerkleTree {
5
- public leaves: string[];
6
-
7
- public branches: string[][] = [];
8
-
9
- public root: string;
10
-
11
- constructor(leafHashes: string[]) {
12
- this.leaves = leafHashes;
13
- this.root = this.build(leafHashes);
14
- }
15
-
16
- private build(leaves: string[]): string {
17
- if (leaves.length === 1) {
18
- return leaves[0];
19
- }
20
- if (leaves.length !== this.leaves.length) {
21
- this.branches.push(leaves);
22
- }
23
- const newLeaves = [];
24
- for (let i = 0; i < leaves.length; i += 2) {
25
- if (i + 1 === leaves.length) {
26
- newLeaves.push(MerkleTree.hash(leaves[i], '0x0'));
27
- } else {
28
- newLeaves.push(MerkleTree.hash(leaves[i], leaves[i + 1]));
29
- }
30
- }
31
- return this.build(newLeaves);
32
- }
33
-
34
- static hash(a: string, b: string) {
35
- const [aSorted, bSorted] = [toBN(a), toBN(b)].sort((x: any, y: any) => (x.gte(y) ? 1 : -1));
36
- return pedersen([aSorted, bSorted]);
37
- }
38
-
39
- public getProof(leaf: string, branch = this.leaves, hashPath: string[] = []): string[] {
40
- const index = branch.indexOf(leaf);
41
- if (index === -1) {
42
- throw new Error('leaf not found');
43
- }
44
- if (branch.length === 1) {
45
- return hashPath;
46
- }
47
- const isLeft = index % 2 === 0;
48
- const neededBranch = (isLeft ? branch[index + 1] : branch[index - 1]) ?? '0x0';
49
- const newHashPath = [...hashPath, neededBranch];
50
- const currentBranchLevelIndex =
51
- this.leaves.length === branch.length
52
- ? -1
53
- : this.branches.findIndex((b) => b.length === branch.length);
54
- const nextBranch = this.branches[currentBranchLevelIndex + 1] ?? [this.root];
55
- return this.getProof(
56
- MerkleTree.hash(isLeft ? leaf : neededBranch, isLeft ? neededBranch : leaf),
57
- nextBranch,
58
- newHashPath
59
- );
60
- }
61
- }
62
-
63
- export function proofMerklePath(root: string, leaf: string, path: string[]): boolean {
64
- if (path.length === 0) {
65
- return root === leaf;
66
- }
67
- const [next, ...rest] = path;
68
- return proofMerklePath(root, MerkleTree.hash(leaf, next), rest);
69
- }
@@ -1,98 +0,0 @@
1
- import BN from 'bn.js';
2
- import assert from 'minimalistic-assert';
3
-
4
- import { addHexPrefix, removeHexPrefix } from './encode';
5
-
6
- export type BigNumberish = string | number | BN;
7
-
8
- export function isHex(hex: string): boolean {
9
- return /^0x[0-9a-f]*$/i.test(hex);
10
- }
11
-
12
- export function toBN(number: BigNumberish, base?: number | 'hex') {
13
- if (typeof number === 'string') {
14
- // eslint-disable-next-line no-param-reassign
15
- number = number.toLowerCase();
16
- }
17
- if (typeof number === 'string' && isHex(number) && !base)
18
- return new BN(removeHexPrefix(number), 'hex');
19
- return new BN(number, base);
20
- }
21
-
22
- export function toHex(number: BN): string {
23
- return addHexPrefix(number.toString('hex'));
24
- }
25
-
26
- export function hexToDecimalString(hex: string): string {
27
- return toBN(`0x${hex.replace(/^0x/, '')}`).toString();
28
- }
29
-
30
- export function toFelt(num: BigNumberish): string {
31
- if (BN.isBN(num)) {
32
- return num.toString();
33
- }
34
- return toBN(num).toString();
35
- }
36
-
37
- /**
38
- * Remove hex string leading zero and lower case '0x01A'.. -> '0x1a..'
39
- * @param hex string
40
- */
41
- export const cleanHex = (hex: string) => hex.toLowerCase().replace(/^(0x)0+/, '$1');
42
-
43
- /*
44
- Asserts input is equal to or greater then lowerBound and lower then upperBound.
45
- Assert message specifies inputName.
46
- input, lowerBound, and upperBound should be of type BN.
47
- inputName should be a string.
48
- */
49
- export function assertInRange(
50
- input: BigNumberish,
51
- lowerBound: BigNumberish,
52
- upperBound: BigNumberish,
53
- inputName = ''
54
- ) {
55
- const messageSuffix = inputName === '' ? 'invalid length' : `invalid ${inputName} length`;
56
- const inputBn = toBN(input);
57
- assert(
58
- inputBn.gte(toBN(lowerBound)) && inputBn.lt(toBN(upperBound)),
59
- `Message not signable, ${messageSuffix}.`
60
- );
61
- }
62
-
63
- export function bigNumberishArrayToDecimalStringArray(rawCalldata: BigNumberish[]): string[] {
64
- return rawCalldata.map((x) => toBN(x).toString(10));
65
- }
66
-
67
- export function bigNumberishArrayToHexadecimalStringArray(rawCalldata: BigNumberish[]): string[] {
68
- return rawCalldata.map((x) => toHex(toBN(x)));
69
- }
70
-
71
- export const isStringWholeNumber = (value: string) => /^\d+$/.test(value);
72
- export const toHexString = (value: string) => toHex(toBN(value));
73
-
74
- export function getDecimalString(value: string) {
75
- if (isHex(value)) {
76
- return hexToDecimalString(value);
77
- }
78
- if (isStringWholeNumber(value)) {
79
- return value;
80
- }
81
- throw new Error(`${value} need to be hex-string or whole-number-string`);
82
- }
83
-
84
- export function getHexString(value: string) {
85
- if (isHex(value)) {
86
- return value;
87
- }
88
- if (isStringWholeNumber(value)) {
89
- return toHexString(value);
90
- }
91
- throw new Error(`${value} need to be hex-string or whole-number-string`);
92
- }
93
-
94
- export function getHexStringArray(value: Array<string>) {
95
- return value.map((el) => getHexString(el));
96
- }
97
-
98
- export const toCairoBool = (value: boolean): string => (+value).toString();
@@ -1,28 +0,0 @@
1
- import { CompiledContract, ContractClass, RawCalldata } from '../types';
2
- import { parse } from './json';
3
- import { isHex, toBN, toHex } from './number';
4
- import { compressProgram } from './stark';
5
-
6
- export function wait(delay: number) {
7
- return new Promise((res) => {
8
- setTimeout(res, delay);
9
- });
10
- }
11
-
12
- export function parseCalldata(calldata: RawCalldata = []) {
13
- return calldata.map((data) => {
14
- if (typeof data === 'string' && isHex(data as string)) {
15
- return data;
16
- }
17
- return toHex(toBN(data));
18
- });
19
- }
20
-
21
- export function parseContract(contract: CompiledContract | string) {
22
- const parsedContract =
23
- typeof contract === 'string' ? (parse(contract) as CompiledContract) : contract;
24
- return {
25
- ...parsedContract,
26
- program: compressProgram(parsedContract.program),
27
- } as ContractClass;
28
- }
@@ -1,28 +0,0 @@
1
- import {
2
- CallContractResponse,
3
- DeclareContractResponse,
4
- DeployContractResponse,
5
- EstimateFeeResponse,
6
- GetBlockResponse,
7
- GetTransactionReceiptResponse,
8
- GetTransactionResponse,
9
- InvokeFunctionResponse,
10
- } from '../../types';
11
-
12
- export abstract class ResponseParser {
13
- abstract parseGetBlockResponse(res: any): GetBlockResponse;
14
-
15
- abstract parseGetTransactionResponse(res: any): GetTransactionResponse;
16
-
17
- abstract parseGetTransactionReceiptResponse(res: any): GetTransactionReceiptResponse;
18
-
19
- abstract parseFeeEstimateResponse(res: any): EstimateFeeResponse;
20
-
21
- abstract parseCallContractResponse(res: any): CallContractResponse;
22
-
23
- abstract parseInvokeFunctionResponse(res: any): InvokeFunctionResponse;
24
-
25
- abstract parseDeployContractResponse(res: any): DeployContractResponse;
26
-
27
- abstract parseDeclareContractResponse(res: any): DeclareContractResponse;
28
- }
@@ -1,70 +0,0 @@
1
- /**
2
- * Map RPC Response to common interface response
3
- * Intersection (sequencer response ∩ (∪ rpc responses))
4
- */
5
- import {
6
- CallContractResponse,
7
- EstimateFeeResponse,
8
- GetBlockResponse,
9
- GetTransactionResponse,
10
- RPC,
11
- } from '../../types';
12
- import { toBN } from '../number';
13
- import { ResponseParser } from '.';
14
-
15
- type RpcGetBlockResponse = RPC.GetBlockWithTxHashesResponse & {
16
- [key: string]: any;
17
- };
18
-
19
- type GetTransactionByHashResponse = RPC.GetTransactionByHashResponse & {
20
- [key: string]: any;
21
- };
22
-
23
- export class RPCResponseParser
24
- implements
25
- Omit<
26
- ResponseParser,
27
- | 'parseDeclareContractResponse'
28
- | 'parseDeployContractResponse'
29
- | 'parseInvokeFunctionResponse'
30
- | 'parseGetTransactionReceiptResponse'
31
- >
32
- {
33
- public parseGetBlockResponse(res: RpcGetBlockResponse): GetBlockResponse {
34
- return {
35
- timestamp: res.timestamp,
36
- block_hash: res.block_hash,
37
- block_number: res.block_number,
38
- new_root: res.new_root,
39
- parent_hash: res.parent_hash,
40
- status: res.status,
41
- transactions: res.transactions,
42
- };
43
- }
44
-
45
- public parseGetTransactionResponse(res: GetTransactionByHashResponse): GetTransactionResponse {
46
- return {
47
- calldata: res.calldata || [],
48
- contract_address: res.contract_address,
49
- max_fee: res.max_fee,
50
- nonce: res.nonce,
51
- signature: res.signature || [],
52
- transaction_hash: res.transaction_hash,
53
- version: res.version,
54
- };
55
- }
56
-
57
- public parseFeeEstimateResponse(res: RPC.EstimateFeeResponse): EstimateFeeResponse {
58
- return {
59
- overall_fee: toBN(res.overall_fee),
60
- gas_consumed: toBN(res.gas_consumed),
61
- gas_price: toBN(res.gas_price),
62
- };
63
- }
64
-
65
- public parseCallContractResponse(res: Array<string>): CallContractResponse {
66
- return {
67
- result: res,
68
- };
69
- }
70
- }
@@ -1,135 +0,0 @@
1
- /**
2
- * Map Sequencer Response to common interface response
3
- * Intersection (sequencer response ∩ (∪ rpc responses))
4
- */
5
- import {
6
- CallContractResponse,
7
- DeclareContractResponse,
8
- DeployContractResponse,
9
- EstimateFeeResponse,
10
- GetBlockResponse,
11
- GetTransactionReceiptResponse,
12
- GetTransactionResponse,
13
- InvokeFunctionResponse,
14
- Sequencer,
15
- } from '../../types';
16
- import { toBN } from '../number';
17
- import { ResponseParser } from '.';
18
-
19
- export class SequencerAPIResponseParser extends ResponseParser {
20
- public parseGetBlockResponse(res: Sequencer.GetBlockResponse): GetBlockResponse {
21
- return {
22
- ...res,
23
- new_root: res.state_root,
24
- parent_hash: res.parent_block_hash,
25
- transactions: Object.values(res.transactions)
26
- .map((value) => 'transaction_hash' in value && value.transaction_hash)
27
- .filter(Boolean) as Array<string>,
28
- };
29
- }
30
-
31
- public parseGetTransactionResponse(
32
- res: Sequencer.GetTransactionResponse
33
- ): GetTransactionResponse {
34
- return {
35
- ...res,
36
- calldata: 'calldata' in res.transaction ? (res.transaction.calldata as Array<string>) : [],
37
- contract_address:
38
- 'contract_address' in res.transaction ? res.transaction.contract_address : undefined,
39
- contract_class:
40
- 'contract_class' in res.transaction ? (res.transaction.contract_class as any) : undefined,
41
- entry_point_selector:
42
- 'entry_point_selector' in res.transaction
43
- ? res.transaction.entry_point_selector
44
- : undefined,
45
- max_fee: 'max_fee' in res.transaction ? (res.transaction.max_fee as string) : undefined,
46
- nonce: res.transaction.nonce as string,
47
- sender_address:
48
- 'sender_address' in res.transaction
49
- ? (res.transaction.sender_address as string)
50
- : undefined,
51
- signature: 'signature' in res.transaction ? res.transaction.signature : undefined,
52
- transaction_hash:
53
- 'transaction_hash' in res.transaction ? res.transaction.transaction_hash : undefined,
54
- version: 'version' in res.transaction ? (res.transaction.version as string) : undefined,
55
- };
56
- }
57
-
58
- public parseGetTransactionReceiptResponse(
59
- res: Sequencer.TransactionReceiptResponse
60
- ): GetTransactionReceiptResponse {
61
- return {
62
- transaction_hash: res.transaction_hash,
63
- status: res.status,
64
- messages_sent: res.l2_to_l1_messages as any, // TODO: parse
65
- events: res.events as any,
66
- ...('block_hash' in res && { block_hash: res.block_hash }),
67
- ...('block_number' in res && { block_number: res.block_number }),
68
- ...('actual_fee' in res && { actual_fee: res.actual_fee }),
69
- ...('transaction_index' in res && { transaction_index: res.transaction_index }),
70
- ...('execution_resources' in res && { execution_resources: res.execution_resources }),
71
- ...('l1_to_l2_consumed_message' in res && {
72
- // eslint-disable-next-line @typescript-eslint/dot-notation
73
- l1_to_l2_consumed_message: res['l1_to_l2_consumed_message'],
74
- }),
75
- ...('transaction_failure_reason' in res && {
76
- transaction_failure_reason: res.transaction_failure_reason,
77
- }),
78
- };
79
- }
80
-
81
- public parseFeeEstimateResponse(res: Sequencer.EstimateFeeResponse): EstimateFeeResponse {
82
- if ('overall_fee' in res) {
83
- let gasInfo = {};
84
-
85
- try {
86
- gasInfo = {
87
- gas_consumed: toBN(res.gas_usage),
88
- gas_price: toBN(res.gas_price),
89
- };
90
- } catch {
91
- // do nothing
92
- }
93
-
94
- return {
95
- overall_fee: toBN(res.overall_fee),
96
- ...gasInfo,
97
- };
98
- }
99
- return {
100
- overall_fee: toBN(res.amount),
101
- };
102
- }
103
-
104
- public parseCallContractResponse(res: Sequencer.CallContractResponse): CallContractResponse {
105
- return {
106
- result: res.result,
107
- };
108
- }
109
-
110
- public parseInvokeFunctionResponse(
111
- res: Sequencer.AddTransactionResponse
112
- ): InvokeFunctionResponse {
113
- return {
114
- transaction_hash: res.transaction_hash,
115
- };
116
- }
117
-
118
- public parseDeployContractResponse(
119
- res: Sequencer.AddTransactionResponse
120
- ): DeployContractResponse {
121
- return {
122
- transaction_hash: res.transaction_hash,
123
- contract_address: res.address as string,
124
- };
125
- }
126
-
127
- public parseDeclareContractResponse(
128
- res: Sequencer.AddTransactionResponse
129
- ): DeclareContractResponse {
130
- return {
131
- transaction_hash: res.transaction_hash,
132
- class_hash: res.class_hash as string,
133
- };
134
- }
135
- }
@@ -1,21 +0,0 @@
1
- import { addHexPrefix, removeHexPrefix } from './encode';
2
-
3
- export function isASCII(str: string) {
4
- // eslint-disable-next-line no-control-regex
5
- return /^[\x00-\x7F]*$/.test(str);
6
- }
7
-
8
- // function to check if string has less or equal 31 characters
9
- export function isShortString(str: string) {
10
- return str.length <= 31;
11
- }
12
-
13
- export function encodeShortString(str: string) {
14
- if (!isASCII(str)) throw new Error(`${str} is not an ASCII string`);
15
- if (!isShortString(str)) throw new Error(`${str} is too long`);
16
- return addHexPrefix(str.replace(/./g, (char) => char.charCodeAt(0).toString(16)));
17
- }
18
-
19
- export function decodeShortString(str: string) {
20
- return removeHexPrefix(str).replace(/.{2}/g, (hex) => String.fromCharCode(parseInt(hex, 16)));
21
- }