starknet 3.5.1 → 3.8.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 (84) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/__tests__/account.test.ts +38 -20
  3. package/__tests__/accountContract.test.ts +0 -31
  4. package/__tests__/constancts.ts +2 -0
  5. package/__tests__/contract.test.ts +14 -21
  6. package/__tests__/provider.test.ts +8 -0
  7. package/account/default.d.ts +14 -1
  8. package/account/default.js +78 -14
  9. package/account/interface.d.ts +14 -0
  10. package/contract/default.d.ts +13 -3
  11. package/contract/default.js +36 -25
  12. package/contract/interface.d.ts +21 -3
  13. package/dist/account/default.d.ts +8 -2
  14. package/dist/account/default.js +57 -11
  15. package/dist/account/interface.d.ts +13 -1
  16. package/dist/contract/default.d.ts +7 -4
  17. package/dist/contract/default.js +27 -24
  18. package/dist/contract/interface.d.ts +9 -4
  19. package/dist/provider/default.d.ts +11 -4
  20. package/dist/provider/default.js +34 -30
  21. package/dist/provider/utils.d.ts +1 -2
  22. package/dist/provider/utils.js +7 -8
  23. package/dist/signer/index.d.ts +1 -0
  24. package/dist/signer/index.js +1 -0
  25. package/dist/signer/ledger.d.ts +12 -0
  26. package/dist/signer/ledger.js +138 -0
  27. package/dist/types/api.d.ts +60 -3
  28. package/package.json +5 -2
  29. package/provider/default.d.ts +13 -3
  30. package/provider/default.js +49 -42
  31. package/provider/utils.d.ts +1 -2
  32. package/provider/utils.js +7 -8
  33. package/signer/index.d.ts +1 -0
  34. package/signer/index.js +1 -0
  35. package/signer/ledger.d.ts +15 -0
  36. package/signer/ledger.js +243 -0
  37. package/src/account/default.ts +40 -6
  38. package/src/account/interface.ts +15 -0
  39. package/src/contract/default.ts +37 -34
  40. package/src/contract/interface.ts +21 -3
  41. package/src/provider/default.ts +31 -23
  42. package/src/provider/utils.ts +7 -8
  43. package/src/signer/index.ts +1 -0
  44. package/src/signer/ledger.ts +81 -0
  45. package/src/types/api.ts +65 -4
  46. package/tsconfig.json +1 -10
  47. package/types/api.d.ts +60 -3
  48. package/www/README.md +41 -0
  49. package/www/babel.config.js +3 -0
  50. package/www/code-examples/account.js +62 -0
  51. package/www/code-examples/amm.js +49 -0
  52. package/www/code-examples/erc20.js +10 -0
  53. package/www/code-examples/package-lock.json +336 -0
  54. package/www/code-examples/package.json +15 -0
  55. package/www/docs/API/_category_.json +5 -0
  56. package/www/docs/API/account.md +11 -0
  57. package/www/docs/API/contract.md +14 -0
  58. package/www/docs/API/index.md +4 -0
  59. package/www/docs/API/provider.md +10 -0
  60. package/www/docs/API/signer.md +8 -0
  61. package/www/docusaurus.config.js +131 -0
  62. package/www/guides/account.md +60 -0
  63. package/www/guides/cra.md +3 -0
  64. package/www/guides/erc20.md +88 -0
  65. package/www/guides/intro.md +20 -0
  66. package/www/package-lock.json +22285 -0
  67. package/www/package.json +43 -0
  68. package/www/sidebars.js +31 -0
  69. package/www/src/components/HomepageFeatures/index.tsx +67 -0
  70. package/www/src/components/HomepageFeatures/styles.module.css +10 -0
  71. package/www/src/css/custom.css +39 -0
  72. package/www/src/pages/index.module.css +23 -0
  73. package/www/src/pages/index.tsx +40 -0
  74. package/www/src/pages/markdown-page.md +7 -0
  75. package/www/static/.nojekyll +0 -0
  76. package/www/static/img/docusaurus.png +0 -0
  77. package/www/static/img/favicon.ico +0 -0
  78. package/www/static/img/logo.svg +17 -0
  79. package/www/static/img/starknet-1.png +0 -0
  80. package/www/static/img/starknet-2.png +0 -0
  81. package/www/static/img/starknet-3.png +0 -0
  82. package/www/static/img/tutorial/docsVersionDropdown.png +0 -0
  83. package/www/static/img/tutorial/localeDropdown.png +0 -0
  84. package/www/tsconfig.json +8 -0
@@ -43,6 +43,12 @@ type BlockIdentifierObject =
43
43
  * @returns block identifier object
44
44
  */
45
45
  export function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdentifierObject {
46
+ if (blockIdentifier === null) {
47
+ return { type: 'BLOCK_NUMBER', data: null };
48
+ }
49
+ if (blockIdentifier === 'pending') {
50
+ return { type: 'BLOCK_NUMBER', data: 'pending' };
51
+ }
46
52
  if (typeof blockIdentifier === 'number') {
47
53
  return { type: 'BLOCK_NUMBER', data: blockIdentifier };
48
54
  }
@@ -52,12 +58,6 @@ export function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdent
52
58
  if (typeof blockIdentifier === 'string' && !Number.isNaN(parseInt(blockIdentifier, 10))) {
53
59
  return { type: 'BLOCK_NUMBER', data: parseInt(blockIdentifier, 10) };
54
60
  }
55
- if (blockIdentifier === null) {
56
- return { type: 'BLOCK_NUMBER', data: null };
57
- }
58
- if (blockIdentifier === 'pending') {
59
- return { type: 'BLOCK_NUMBER', data: 'pending' };
60
- }
61
61
  if (typeof blockIdentifier === 'string') {
62
62
  throw new Error(`Invalid block identifier: ${blockIdentifier}`);
63
63
  }
@@ -69,8 +69,7 @@ export function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdent
69
69
  *
70
70
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L164-L173)
71
71
  *
72
- * @param blockNumber
73
- * @param blockHash
72
+ * @param blockIdentifier
74
73
  * @returns block identifier for API request
75
74
  */
76
75
  export function getFormattedBlockIdentifier(blockIdentifier: BlockIdentifier = null): string {
@@ -1,2 +1,3 @@
1
1
  export * from './interface';
2
2
  export * from './default';
3
+ export * from './ledger';
@@ -0,0 +1,81 @@
1
+ import Eth from '@ledgerhq/hw-app-eth';
2
+ import Transport from '@ledgerhq/hw-transport';
3
+ import TransportWebHID from '@ledgerhq/hw-transport-webhid';
4
+
5
+ import { Invocation, InvocationsSignerDetails, Signature } from '../types';
6
+ import { addHexPrefix } from '../utils/encode';
7
+ import { hashMulticall } from '../utils/hash';
8
+ import { TypedData, getMessageHash } from '../utils/typedData';
9
+ import { SignerInterface } from './interface';
10
+
11
+ function hexZeroPad(hash: string, length: number): string {
12
+ let value = hash;
13
+ if (value.length > 2 * length + 2) {
14
+ throw new Error('value out of range');
15
+ }
16
+ while (value.length < 2 * length + 2) {
17
+ value = `0x0${value.substring(2)}`;
18
+ }
19
+ return value;
20
+ }
21
+
22
+ export class LedgerBlindSigner implements SignerInterface {
23
+ public derivationPath = "/2645'/579218131'/1148870696'/0'/0'/0";
24
+
25
+ private transport: Transport | undefined;
26
+
27
+ private async getEthApp(): Promise<Eth> {
28
+ if (!this.transport) {
29
+ try {
30
+ this.transport = await TransportWebHID.create();
31
+ } catch {
32
+ throw new Error('Device connection error');
33
+ }
34
+ }
35
+ return new Eth(this.transport);
36
+ }
37
+
38
+ public async getPubKey(): Promise<string> {
39
+ const eth = await this.getEthApp();
40
+ const response = await eth.starkGetPublicKey(this.derivationPath);
41
+ const starkPub = `0x${response.slice(1, 1 + 32).toString('hex')}`;
42
+ return starkPub;
43
+ }
44
+
45
+ public async signTransaction(
46
+ transactions: Invocation[],
47
+ transactionsDetail: InvocationsSignerDetails
48
+ ): Promise<Signature> {
49
+ const msgHash = hashMulticall(
50
+ transactionsDetail.walletAddress,
51
+ transactions,
52
+ transactionsDetail.nonce.toString(),
53
+ transactionsDetail.maxFee.toString()
54
+ );
55
+
56
+ return this.sign(msgHash);
57
+ }
58
+
59
+ public async signMessage(typedData: TypedData, accountAddress: string): Promise<Signature> {
60
+ const msgHash = getMessageHash(typedData, accountAddress);
61
+
62
+ return this.sign(msgHash);
63
+ }
64
+
65
+ protected async sign(msgHash: string): Promise<Signature> {
66
+ const eth = await this.getEthApp();
67
+
68
+ const {
69
+ r,
70
+ s,
71
+ }: {
72
+ r: string;
73
+ s: string;
74
+ } = (await eth.starkUnsafeSign(this.derivationPath, hexZeroPad(msgHash, 32))) as {
75
+ r: string;
76
+ s: string;
77
+ };
78
+
79
+ return [addHexPrefix(r), addHexPrefix(s)];
80
+ }
81
+ }
package/src/types/api.ts CHANGED
@@ -36,6 +36,13 @@ export type Endpoints = {
36
36
  REQUEST: never;
37
37
  RESPONSE: GetTransactionStatusResponse;
38
38
  };
39
+ get_transaction_trace: {
40
+ QUERY: {
41
+ transactionHash: string;
42
+ };
43
+ REQUEST: never;
44
+ RESPONSE: GetTransactionTraceResponse;
45
+ };
39
46
  get_storage_at: {
40
47
  QUERY: {
41
48
  contractAddress: string;
@@ -68,8 +75,10 @@ export type Endpoints = {
68
75
  RESPONSE: CallContractResponse;
69
76
  };
70
77
  estimate_fee: {
71
- QUERY: never;
72
- REQUEST: Transaction;
78
+ QUERY: {
79
+ blockIdentifier: BlockIdentifier;
80
+ };
81
+ REQUEST: CallContractTransaction;
73
82
  RESPONSE: EstimateFeeResponse;
74
83
  };
75
84
  };
@@ -95,6 +104,33 @@ export type InvokeFunctionTransaction = {
95
104
  entry_point_selector: string;
96
105
  calldata?: RawCalldata;
97
106
  nonce?: BigNumberish;
107
+ max_fee?: BigNumberish;
108
+ };
109
+
110
+ export type InvokeFunctionTrace = {
111
+ caller_address: string;
112
+ contract_address: string;
113
+ code_address: string;
114
+ selector: string;
115
+ calldata: RawCalldata;
116
+ result: Array<any>;
117
+ execution_resources: ExecutionResources;
118
+ internal_call: Array<InvokeFunctionTrace>;
119
+ events: Array<any>;
120
+ messages: Array<any>;
121
+ };
122
+
123
+ export type ExecutionResources = {
124
+ n_steps: number;
125
+ builtin_instance_counter: {
126
+ pedersen_builtin: number;
127
+ range_check_builtin: number;
128
+ bitwise_builtin: number;
129
+ output_builtin: number;
130
+ ecdsa_builtin: number;
131
+ ec_op_builtin: number;
132
+ };
133
+ n_memory_holes: number;
98
134
  };
99
135
 
100
136
  export type CallContractTransaction = Omit<
@@ -149,6 +185,22 @@ export type GetTransactionStatusResponse = {
149
185
  };
150
186
  };
151
187
 
188
+ export type GetTransactionTraceResponse = {
189
+ function_invocation: {
190
+ caller_address: string;
191
+ contract_address: string;
192
+ code_address: string;
193
+ selector: string;
194
+ calldata: RawArgs;
195
+ result: Array<any>;
196
+ execution_resources: any;
197
+ internal_call: Array<any>;
198
+ events: Array<any>;
199
+ messages: Array<any>;
200
+ };
201
+ signature: Signature;
202
+ };
203
+
152
204
  export type GetTransactionResponse = {
153
205
  status: Status;
154
206
  transaction: Transaction;
@@ -173,11 +225,20 @@ export type TransactionReceipt = {
173
225
  l2_to_l1_messages: string[];
174
226
  events: string[];
175
227
  };
176
- // TODO: Add response data
177
- export type EstimateFeeResponse = {};
228
+
229
+ export type EstimateFeeResponse = {
230
+ amount: number;
231
+ unit: string;
232
+ };
178
233
 
179
234
  export type RawArgs = {
180
235
  [inputName: string]: string | string[] | { type: 'struct'; [k: string]: BigNumberish };
181
236
  };
182
237
 
183
238
  export type Calldata = string[];
239
+
240
+ export type Overrides = {
241
+ maxFee?: BigNumberish;
242
+ nonce?: BigNumberish;
243
+ signature?: Signature;
244
+ };
package/tsconfig.json CHANGED
@@ -103,14 +103,5 @@
103
103
  "skipLibCheck": true /* Skip type checking all .d.ts files. */
104
104
  },
105
105
  "include": ["src/**/*"],
106
- "exclude": ["node_modules"],
107
- "typedocOptions": {
108
- "entryPoints": "src/index.ts",
109
- "entryPointStrategy": "expand",
110
- "out": "docs",
111
- "githubPages": false,
112
- "readme": "./README.md",
113
- "name": "StarkNet.js Docs",
114
- "sort": "required-first"
115
- }
106
+ "exclude": ["node_modules"]
116
107
  }
package/types/api.d.ts CHANGED
@@ -35,6 +35,13 @@ export declare type Endpoints = {
35
35
  REQUEST: never;
36
36
  RESPONSE: GetTransactionStatusResponse;
37
37
  };
38
+ get_transaction_trace: {
39
+ QUERY: {
40
+ transactionHash: string;
41
+ };
42
+ REQUEST: never;
43
+ RESPONSE: GetTransactionTraceResponse;
44
+ };
38
45
  get_storage_at: {
39
46
  QUERY: {
40
47
  contractAddress: string;
@@ -67,8 +74,10 @@ export declare type Endpoints = {
67
74
  RESPONSE: CallContractResponse;
68
75
  };
69
76
  estimate_fee: {
70
- QUERY: never;
71
- REQUEST: Transaction;
77
+ QUERY: {
78
+ blockIdentifier: BlockIdentifier;
79
+ };
80
+ REQUEST: CallContractTransaction;
72
81
  RESPONSE: EstimateFeeResponse;
73
82
  };
74
83
  };
@@ -91,6 +100,31 @@ export declare type InvokeFunctionTransaction = {
91
100
  entry_point_selector: string;
92
101
  calldata?: RawCalldata;
93
102
  nonce?: BigNumberish;
103
+ max_fee?: BigNumberish;
104
+ };
105
+ export declare type InvokeFunctionTrace = {
106
+ caller_address: string;
107
+ contract_address: string;
108
+ code_address: string;
109
+ selector: string;
110
+ calldata: RawCalldata;
111
+ result: Array<any>;
112
+ execution_resources: ExecutionResources;
113
+ internal_call: Array<InvokeFunctionTrace>;
114
+ events: Array<any>;
115
+ messages: Array<any>;
116
+ };
117
+ export declare type ExecutionResources = {
118
+ n_steps: number;
119
+ builtin_instance_counter: {
120
+ pedersen_builtin: number;
121
+ range_check_builtin: number;
122
+ bitwise_builtin: number;
123
+ output_builtin: number;
124
+ ecdsa_builtin: number;
125
+ ec_op_builtin: number;
126
+ };
127
+ n_memory_holes: number;
94
128
  };
95
129
  export declare type CallContractTransaction = Omit<
96
130
  InvokeFunctionTransaction,
@@ -138,6 +172,21 @@ export declare type GetTransactionStatusResponse = {
138
172
  error_message: string;
139
173
  };
140
174
  };
175
+ export declare type GetTransactionTraceResponse = {
176
+ function_invocation: {
177
+ caller_address: string;
178
+ contract_address: string;
179
+ code_address: string;
180
+ selector: string;
181
+ calldata: RawArgs;
182
+ result: Array<any>;
183
+ execution_resources: any;
184
+ internal_call: Array<any>;
185
+ events: Array<any>;
186
+ messages: Array<any>;
187
+ };
188
+ signature: Signature;
189
+ };
141
190
  export declare type GetTransactionResponse = {
142
191
  status: Status;
143
192
  transaction: Transaction;
@@ -160,7 +209,10 @@ export declare type TransactionReceipt = {
160
209
  l2_to_l1_messages: string[];
161
210
  events: string[];
162
211
  };
163
- export declare type EstimateFeeResponse = {};
212
+ export declare type EstimateFeeResponse = {
213
+ amount: number;
214
+ unit: string;
215
+ };
164
216
  export declare type RawArgs = {
165
217
  [inputName: string]:
166
218
  | string
@@ -171,3 +223,8 @@ export declare type RawArgs = {
171
223
  };
172
224
  };
173
225
  export declare type Calldata = string[];
226
+ export declare type Overrides = {
227
+ maxFee?: BigNumberish;
228
+ nonce?: BigNumberish;
229
+ signature?: Signature;
230
+ };
package/www/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Website
2
+
3
+ This website is built using [Docusaurus 2](https://docusaurus.io/), a modern static website generator.
4
+
5
+ ### Installation
6
+
7
+ ```
8
+ $ yarn
9
+ ```
10
+
11
+ ### Local Development
12
+
13
+ ```
14
+ $ yarn start
15
+ ```
16
+
17
+ This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server.
18
+
19
+ ### Build
20
+
21
+ ```
22
+ $ yarn build
23
+ ```
24
+
25
+ This command generates static content into the `build` directory and can be served using any static contents hosting service.
26
+
27
+ ### Deployment
28
+
29
+ Using SSH:
30
+
31
+ ```
32
+ $ USE_SSH=true yarn deploy
33
+ ```
34
+
35
+ Not using SSH:
36
+
37
+ ```
38
+ $ GIT_USER=<Your GitHub username> yarn deploy
39
+ ```
40
+
41
+ If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch.
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
3
+ };
@@ -0,0 +1,62 @@
1
+
2
+ // Install the latest version of starknet with npm install starknet@next and import starknet
3
+ import * as starknet from "starknet";
4
+
5
+ // Generate public and private key pair.
6
+
7
+ const keyPair = starknet.ec.genKeyPair();
8
+ const starkKey = starknet.ec.getStarkKey(keyPair);
9
+ const starkKeyInt = starknet.number.toBN(starknet.encode.removeHexPrefix(starkKey), 16);
10
+
11
+ const { address: walletAddressLocal } = await provider.deployContract({contract: COMPILED_WALLET_CONTRACT_JSON, constructorCallData: [starkKeyInt], addressSalt: 0});
12
+
13
+ walletAddress = walletAddressLocal;
14
+
15
+ const { code: codeErc20, address: erc20AddressLocal } = await defaultProvider.deployContract({
16
+ contract: compiledErc20,
17
+ });
18
+
19
+ const erc20Address = erc20AddressLocal;
20
+ const erc20 = new Contract(compiledErc20.abi, erc20Address);
21
+
22
+ const { code: codeErc20Mint, transaction_hash: txErc20Mint } = await erc20.invoke('mint', {
23
+ recipient: walletAddress,
24
+ amount: '1000',
25
+ });
26
+
27
+ const balanceBeforeTransfer = await erc20.call('balance_of', {
28
+ user: walletAddress,
29
+ }).res;
30
+
31
+ console.log(number.toBN(res).toString())
32
+
33
+ const { nonce } = await wallet.call('get_nonce');
34
+ const msgHash = encode.addHexPrefix(
35
+ hash.hashMessage(
36
+ wallet.connectedTo,
37
+ erc20Address,
38
+ stark.getSelectorFromName('transfer'),
39
+ [erc20Address, '10'],
40
+ nonce.toString()
41
+ )
42
+ );
43
+
44
+ const signature = ec.sign(starkKeyPair, msgHash);
45
+ const { code, transaction_hash } = await wallet.invoke(
46
+ 'execute',
47
+ {
48
+ to: erc20Address,
49
+ selector: stark.getSelectorFromName('transfer'),
50
+ calldata: [erc20Address, '10'],
51
+ nonce: nonce.toString(),
52
+ },
53
+ signature
54
+ );
55
+
56
+ await defaultProvider.waitForTx(transaction_hash);
57
+
58
+ const balanceAfterTransfer = await erc20.call('balance_of', {
59
+ user: walletAddress,
60
+ }).res;
61
+
62
+ console.log('Balance after transfer', balanceAfterTransfer)
@@ -0,0 +1,49 @@
1
+ import { defaultProvider, stark } from 'starknet';
2
+ const { getSelectorFromName } = stark;
3
+
4
+
5
+ /**
6
+ * !! IMPORTANT NOTE !! When fees are introduced all function invocations will go through the account account contract and this example will be deprecated.
7
+ **/
8
+
9
+ const CONTRACT_ADDRESS =
10
+ "0x03e19baa6cb2078631bcdb34844f3f7879449a544c9ce722681a54af08cff4b9";
11
+
12
+ /**
13
+ * invokeFunction() example
14
+ **/
15
+
16
+ /** Reset the liquidity pool **/
17
+ const addLiquidityResponse = await defaultProvider.LEGACYinvokeFunction(
18
+ {
19
+ contractAddress: CONTRACT_ADDRESS,
20
+ entrypoint: "init_pool",
21
+ calldata: ["1000000", "1000000"],
22
+ }
23
+ );
24
+ console.log(addLiquidityResponse);
25
+
26
+ /**
27
+ * callContract() example
28
+ **/
29
+
30
+ /** Get the balance of the liquidity pool of token A **/
31
+ const poolBalanceTokenA = await defaultProvider.callContract({
32
+ contractAddress: CONTRACT_ADDRESS,
33
+ entrypoint: "get_pool_token_balance",
34
+ calldata: ["1"], // Account 1 (no account implemented)
35
+ });
36
+ const balanceA = poolBalanceTokenA.result[0];
37
+ console.log('token a liquidity pool balance: ', parseInt(balanceA, 16));
38
+
39
+ /** Get the balance of the liquidity pool of token B **/
40
+ const poolBalanceTokenB = await defaultProvider.callContract({
41
+ contractAddress: CONTRACT_ADDRESS,
42
+ entrypoint: "get_pool_token_balance",
43
+ calldata: ["2"],
44
+ });
45
+ const balanceB = poolBalanceTokenB.result[0];
46
+ console.log('token b liquidity pool balance: ', parseInt(balanceB, 16));
47
+
48
+
49
+ /** Make a swap */
@@ -0,0 +1,10 @@
1
+ import * as starknet from "starknet";
2
+
3
+ const keyPair = starknet.ec.genKeyPair();
4
+ const starkKey = starknet.ec.getStarkKey(keyPair);
5
+ const starkKeyInt = starknet.number.toBN(starknet.encode.removeHexPrefix(starkKey), 16);
6
+
7
+ const deployWalletTx = await provider.deployContract({contract: COMPILED_WALLET_CONTRACT_JSON, constructorCallData: [starkKeyInt], addressSalt: 0});
8
+
9
+ await defaultProvider.waitForTx(deployWalletTx.transaction_hash);
10
+