starknet 4.13.2 → 4.15.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.
- package/CHANGELOG.md +39 -0
- package/CODE_OF_CONDUCT.md +128 -0
- package/README.md +2 -2
- package/__mocks__/naming_compiled.json +53283 -0
- package/__mocks__/starknetId_compiled.json +44703 -0
- package/__tests__/account.test.ts +100 -15
- package/__tests__/contract.test.ts +70 -57
- package/__tests__/defaultProvider.test.ts +14 -13
- package/__tests__/fixtures.ts +27 -26
- package/__tests__/rpcProvider.test.ts +19 -16
- package/__tests__/sequencerProvider.test.ts +47 -55
- package/__tests__/utils/starknetId.test.ts +53 -0
- package/__tests__/utils/utils.test.ts +10 -0
- package/dist/index.d.ts +116 -30
- package/dist/index.global.js +660 -463
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +247 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +247 -51
- package/dist/index.mjs.map +1 -1
- package/index.d.ts +116 -30
- package/index.global.js +660 -463
- package/index.global.js.map +1 -1
- package/index.js +247 -51
- package/index.js.map +1 -1
- package/index.mjs +247 -51
- package/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/account/default.ts +81 -8
- package/src/account/interface.ts +71 -5
- package/src/contract/contractFactory.ts +20 -13
- package/src/contract/default.ts +11 -2
- package/src/provider/default.ts +26 -11
- package/src/provider/interface.ts +9 -3
- package/src/provider/rpc.ts +15 -11
- package/src/provider/sequencer.ts +32 -20
- package/src/provider/utils.ts +19 -11
- package/src/types/account.ts +21 -1
- package/src/types/lib.ts +5 -2
- package/src/types/provider.ts +0 -5
- package/src/utils/events.ts +32 -0
- package/src/utils/number.ts +6 -0
- package/src/utils/starknetId.ts +116 -0
- package/www/docs/API/account.md +176 -2
- package/www/docs/API/contractFactory.md +7 -11
- package/www/docs/API/provider.md +5 -9
- package/www/docs/API/utils.md +8 -0
- package/www/guides/account.md +89 -38
- package/www/guides/erc20.md +115 -59
- package/www/guides/intro.md +11 -4
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { isBN } from 'bn.js';
|
|
2
2
|
|
|
3
3
|
import typedDataExample from '../__mocks__/typedDataExample.json';
|
|
4
|
-
import { Account, Contract, Provider, number, stark } from '../src';
|
|
4
|
+
import { Account, Contract, DeployContractPayload, Provider, number, stark } from '../src';
|
|
5
5
|
import { feeTransactionVersion } from '../src/utils/hash';
|
|
6
|
-
import { toBN } from '../src/utils/number';
|
|
6
|
+
import { hexToDecimalString, toBN } from '../src/utils/number';
|
|
7
7
|
import { encodeShortString } from '../src/utils/shortString';
|
|
8
8
|
import { randomAddress } from '../src/utils/stark';
|
|
9
9
|
import {
|
|
10
10
|
compiledErc20,
|
|
11
|
+
compiledNamingContract,
|
|
12
|
+
compiledStarknetId,
|
|
11
13
|
compiledTestDapp,
|
|
12
14
|
erc20ClassHash,
|
|
13
|
-
getERC20DeployPayload,
|
|
14
15
|
getTestAccount,
|
|
15
16
|
getTestProvider,
|
|
16
17
|
} from './fixtures';
|
|
@@ -25,25 +26,29 @@ describe('deploy and test Wallet', () => {
|
|
|
25
26
|
beforeAll(async () => {
|
|
26
27
|
expect(account).toBeInstanceOf(Account);
|
|
27
28
|
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
const declareDeploy = await account.declareDeploy({
|
|
30
|
+
contract: compiledErc20,
|
|
31
|
+
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
|
|
32
|
+
constructorCalldata: [
|
|
33
|
+
encodeShortString('Token'),
|
|
34
|
+
encodeShortString('ERC20'),
|
|
35
|
+
account.address,
|
|
36
|
+
],
|
|
37
|
+
});
|
|
31
38
|
|
|
32
|
-
erc20Address =
|
|
39
|
+
erc20Address = declareDeploy.deploy.contract_address;
|
|
33
40
|
erc20 = new Contract(compiledErc20.abi, erc20Address, provider);
|
|
34
41
|
|
|
35
|
-
await provider.waitForTransaction(erc20Response.transaction_hash);
|
|
36
|
-
|
|
37
42
|
const x = await erc20.balanceOf(account.address);
|
|
38
43
|
|
|
39
44
|
expect(number.toBN(x[0].low).toString()).toStrictEqual(number.toBN(1000).toString());
|
|
40
45
|
|
|
41
|
-
const dappResponse = await
|
|
46
|
+
const dappResponse = await account.declareDeploy({
|
|
42
47
|
contract: compiledTestDapp,
|
|
48
|
+
classHash: '0x04367b26fbb92235e8d1137d19c080e6e650a6889ded726d00658411cc1046f5',
|
|
43
49
|
});
|
|
44
|
-
dapp = new Contract(compiledTestDapp.abi, dappResponse.contract_address!, provider);
|
|
45
50
|
|
|
46
|
-
|
|
51
|
+
dapp = new Contract(compiledTestDapp.abi, dappResponse.deploy.contract_address!, provider);
|
|
47
52
|
});
|
|
48
53
|
|
|
49
54
|
test('estimate fee', async () => {
|
|
@@ -71,7 +76,7 @@ describe('deploy and test Wallet', () => {
|
|
|
71
76
|
calldata: [erc20.address, '10', '0'],
|
|
72
77
|
});
|
|
73
78
|
|
|
74
|
-
await provider.waitForTransaction(transaction_hash);
|
|
79
|
+
await provider.waitForTransaction(transaction_hash, undefined, ['ACCEPTED_ON_L2']);
|
|
75
80
|
});
|
|
76
81
|
|
|
77
82
|
test('read balance of wallet after transfer', async () => {
|
|
@@ -110,7 +115,7 @@ describe('deploy and test Wallet', () => {
|
|
|
110
115
|
},
|
|
111
116
|
]);
|
|
112
117
|
|
|
113
|
-
await provider.waitForTransaction(transaction_hash);
|
|
118
|
+
await provider.waitForTransaction(transaction_hash, undefined, ['ACCEPTED_ON_L2']);
|
|
114
119
|
|
|
115
120
|
const response = await dapp.get_number(account.address);
|
|
116
121
|
expect(toBN(response.number as string).toString()).toStrictEqual('57');
|
|
@@ -161,6 +166,65 @@ describe('deploy and test Wallet', () => {
|
|
|
161
166
|
|
|
162
167
|
expect(declareTx.class_hash).toBeDefined();
|
|
163
168
|
});
|
|
169
|
+
|
|
170
|
+
test('Get the stark name of the account and account from stark name (using starknet.id)', async () => {
|
|
171
|
+
// Deploy naming contract
|
|
172
|
+
const namingPlayLoad: DeployContractPayload = { contract: compiledNamingContract };
|
|
173
|
+
const namingResponse = await provider.deployContract(namingPlayLoad);
|
|
174
|
+
const namingAddress = namingResponse.contract_address;
|
|
175
|
+
|
|
176
|
+
// Deploy Starknet id contract
|
|
177
|
+
const idPlayLoad: DeployContractPayload = { contract: compiledStarknetId };
|
|
178
|
+
const idResponse = await provider.deployContract(idPlayLoad);
|
|
179
|
+
const idAddress = idResponse.contract_address;
|
|
180
|
+
|
|
181
|
+
const { transaction_hash } = await account.execute([
|
|
182
|
+
{
|
|
183
|
+
contractAddress: namingAddress,
|
|
184
|
+
entrypoint: 'initializer',
|
|
185
|
+
calldata: [
|
|
186
|
+
idAddress, // starknetid_contract_addr
|
|
187
|
+
'0', // pricing_contract_addr
|
|
188
|
+
account.address, // admin
|
|
189
|
+
'1576987121283045618657875225183003300580199140020787494777499595331436496159', // whitelisting_key
|
|
190
|
+
'0', // l1_contract
|
|
191
|
+
],
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
contractAddress: idAddress,
|
|
195
|
+
entrypoint: 'mint',
|
|
196
|
+
calldata: ['1'], // TokenId
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
contractAddress: namingAddress,
|
|
200
|
+
entrypoint: 'whitelisted_mint',
|
|
201
|
+
calldata: [
|
|
202
|
+
'18925', // Domain encoded "ben"
|
|
203
|
+
'1697380617', // Expiry
|
|
204
|
+
'1', // Starknet id linked
|
|
205
|
+
account.address, // receiver_address
|
|
206
|
+
'1249449923402095645023546949816521361907869702415870903008894560968474148064', // sig 0 for whitelist
|
|
207
|
+
'543901326374961504443808953662149863005450004831659662383974986108355067943', // sig 1 for whitelist
|
|
208
|
+
],
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
contractAddress: namingAddress,
|
|
212
|
+
entrypoint: 'set_address_to_domain',
|
|
213
|
+
calldata: [
|
|
214
|
+
'1', // length
|
|
215
|
+
'18925', // Domain encoded "ben"
|
|
216
|
+
],
|
|
217
|
+
},
|
|
218
|
+
]);
|
|
219
|
+
|
|
220
|
+
await provider.waitForTransaction(transaction_hash);
|
|
221
|
+
|
|
222
|
+
const address = await account.getAddressFromStarkName('ben.stark', namingAddress);
|
|
223
|
+
expect(hexToDecimalString(address as string)).toEqual(hexToDecimalString(account.address));
|
|
224
|
+
|
|
225
|
+
const name = await account.getStarkName(namingAddress);
|
|
226
|
+
expect(name).toEqual('ben.stark');
|
|
227
|
+
});
|
|
164
228
|
});
|
|
165
229
|
|
|
166
230
|
describe('Declare and UDC Deploy Flow', () => {
|
|
@@ -173,7 +237,28 @@ describe('deploy and test Wallet', () => {
|
|
|
173
237
|
await provider.waitForTransaction(declareTx.transaction_hash);
|
|
174
238
|
|
|
175
239
|
expect(declareTx).toHaveProperty('class_hash');
|
|
176
|
-
expect(declareTx.class_hash).toEqual(erc20ClassHash);
|
|
240
|
+
expect(hexToDecimalString(declareTx.class_hash)).toEqual(hexToDecimalString(erc20ClassHash));
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test('UDC DeployContract', async () => {
|
|
244
|
+
const deployResponse = await account.deployContract({
|
|
245
|
+
classHash: erc20ClassHash,
|
|
246
|
+
constructorCalldata: [
|
|
247
|
+
encodeShortString('Token'),
|
|
248
|
+
encodeShortString('ERC20'),
|
|
249
|
+
account.address,
|
|
250
|
+
],
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
expect(deployResponse.contract_address).toBeDefined();
|
|
254
|
+
expect(deployResponse.transaction_hash).toBeDefined();
|
|
255
|
+
expect(deployResponse.address).toBeDefined();
|
|
256
|
+
expect(deployResponse.deployer).toBeDefined();
|
|
257
|
+
expect(deployResponse.unique).toBeDefined();
|
|
258
|
+
expect(deployResponse.classHash).toBeDefined();
|
|
259
|
+
expect(deployResponse.calldata_len).toBeDefined();
|
|
260
|
+
expect(deployResponse.calldata).toBeDefined();
|
|
261
|
+
expect(deployResponse.salt).toBeDefined();
|
|
177
262
|
});
|
|
178
263
|
|
|
179
264
|
test('UDC Deploy', async () => {
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { isBN } from 'bn.js';
|
|
2
2
|
|
|
3
3
|
import { Contract, ContractFactory, stark } from '../src';
|
|
4
|
-
import { DeployContractPayload } from '../src/types';
|
|
5
4
|
import { getSelectorFromName } from '../src/utils/hash';
|
|
6
5
|
import { BigNumberish, toBN } from '../src/utils/number';
|
|
6
|
+
import { encodeShortString } from '../src/utils/shortString';
|
|
7
7
|
import { compileCalldata } from '../src/utils/stark';
|
|
8
8
|
import {
|
|
9
9
|
compiledErc20,
|
|
10
10
|
compiledMulticall,
|
|
11
11
|
compiledTypeTransformation,
|
|
12
|
-
|
|
12
|
+
getTestAccount,
|
|
13
13
|
getTestProvider,
|
|
14
14
|
} from './fixtures';
|
|
15
15
|
|
|
@@ -17,45 +17,46 @@ const provider = getTestProvider();
|
|
|
17
17
|
|
|
18
18
|
describe('class Contract {}', () => {
|
|
19
19
|
const wallet = stark.randomAddress();
|
|
20
|
+
const account = getTestAccount(provider);
|
|
20
21
|
|
|
21
22
|
describe('Basic Interaction', () => {
|
|
22
|
-
let
|
|
23
|
-
let
|
|
23
|
+
let erc20Contract: Contract;
|
|
24
|
+
let multicallContract: Contract;
|
|
24
25
|
|
|
25
26
|
beforeAll(async () => {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
erc20 = new Contract(compiledErc20.abi, contract_address!, provider);
|
|
33
|
-
await provider.waitForTransaction(transaction_hash);
|
|
34
|
-
// Deploy Multicall
|
|
27
|
+
const { deploy } = await account.declareDeploy({
|
|
28
|
+
contract: compiledErc20,
|
|
29
|
+
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
|
|
30
|
+
constructorCalldata: [encodeShortString('Token'), encodeShortString('ERC20'), wallet],
|
|
31
|
+
});
|
|
35
32
|
|
|
36
|
-
|
|
37
|
-
await provider.deployContract({
|
|
38
|
-
contract: compiledMulticall,
|
|
39
|
-
});
|
|
33
|
+
erc20Contract = new Contract(compiledErc20.abi, deploy.contract_address!, provider);
|
|
40
34
|
|
|
41
|
-
|
|
35
|
+
const { deploy: multicallDeploy } = await account.declareDeploy({
|
|
36
|
+
contract: compiledMulticall,
|
|
37
|
+
classHash: '0x06f94f3229a8d9c1d51cb84f1f5ec306c8552a805e307540727dda53c4936b43',
|
|
38
|
+
});
|
|
42
39
|
|
|
43
|
-
|
|
40
|
+
multicallContract = new Contract(
|
|
41
|
+
compiledMulticall.abi,
|
|
42
|
+
multicallDeploy.contract_address!,
|
|
43
|
+
provider
|
|
44
|
+
);
|
|
44
45
|
});
|
|
45
46
|
|
|
46
47
|
test('populate transaction for initial balance of that account', async () => {
|
|
47
|
-
const res = await
|
|
48
|
+
const res = await erc20Contract.populateTransaction.balanceOf(wallet);
|
|
48
49
|
expect(res).toHaveProperty('contractAddress');
|
|
49
50
|
expect(res).toHaveProperty('entrypoint');
|
|
50
51
|
expect(res).toHaveProperty('calldata');
|
|
51
52
|
});
|
|
52
53
|
|
|
53
54
|
test('estimate gas fee for `mint` should fail when connected to the provider', async () => {
|
|
54
|
-
expect(
|
|
55
|
+
expect(erc20Contract.estimateFee.mint(wallet, ['10', '0'])).rejects.toThrow();
|
|
55
56
|
});
|
|
56
57
|
|
|
57
58
|
test('read initial balance of that account', async () => {
|
|
58
|
-
const result = await
|
|
59
|
+
const result = await erc20Contract.balanceOf(wallet);
|
|
59
60
|
const [res] = result;
|
|
60
61
|
expect(res.low).toStrictEqual(toBN(1000));
|
|
61
62
|
expect(res).toStrictEqual(result.balance);
|
|
@@ -65,17 +66,17 @@ describe('class Contract {}', () => {
|
|
|
65
66
|
const args1 = { user: wallet };
|
|
66
67
|
const args2 = {};
|
|
67
68
|
const calls = [
|
|
68
|
-
|
|
69
|
+
erc20Contract.address,
|
|
69
70
|
getSelectorFromName('balanceOf'),
|
|
70
71
|
Object.keys(args1).length,
|
|
71
72
|
...compileCalldata(args1),
|
|
72
73
|
|
|
73
|
-
|
|
74
|
+
erc20Contract.address,
|
|
74
75
|
getSelectorFromName('decimals'),
|
|
75
76
|
Object.keys(args2).length,
|
|
76
77
|
...compileCalldata(args2),
|
|
77
78
|
];
|
|
78
|
-
const result = await
|
|
79
|
+
const result = await multicallContract.aggregate(calls);
|
|
79
80
|
const [block_number, res] = result;
|
|
80
81
|
expect(isBN(block_number));
|
|
81
82
|
expect(Array.isArray(res));
|
|
@@ -86,36 +87,47 @@ describe('class Contract {}', () => {
|
|
|
86
87
|
});
|
|
87
88
|
|
|
88
89
|
describe('Type Transformation', () => {
|
|
89
|
-
let
|
|
90
|
+
let typeTransformedContract: Contract;
|
|
90
91
|
|
|
91
92
|
beforeAll(async () => {
|
|
92
|
-
const {
|
|
93
|
+
const { deploy } = await account.declareDeploy({
|
|
93
94
|
contract: compiledTypeTransformation,
|
|
95
|
+
classHash: '0x022a0e662b13d18a2aaa3ee54ae290de6569621b549022c18169c6e7893809ea',
|
|
94
96
|
});
|
|
95
|
-
|
|
96
|
-
|
|
97
|
+
|
|
98
|
+
typeTransformedContract = new Contract(
|
|
99
|
+
compiledTypeTransformation.abi,
|
|
100
|
+
deploy.contract_address!,
|
|
101
|
+
provider
|
|
102
|
+
);
|
|
97
103
|
});
|
|
98
104
|
|
|
99
105
|
describe('Request Type Transformation', () => {
|
|
100
106
|
test('Parsing the felt in request', async () => {
|
|
101
|
-
return expect(
|
|
107
|
+
return expect(typeTransformedContract.request_felt(3)).resolves.not.toThrow();
|
|
102
108
|
});
|
|
103
109
|
|
|
104
110
|
test('Parsing the array of felt in request', async () => {
|
|
105
|
-
return expect(
|
|
111
|
+
return expect(
|
|
112
|
+
typeTransformedContract.request_array_of_felts([1, 2])
|
|
113
|
+
).resolves.not.toThrow();
|
|
106
114
|
});
|
|
107
115
|
|
|
108
116
|
test('Parsing the struct in request', async () => {
|
|
109
|
-
return expect(
|
|
117
|
+
return expect(
|
|
118
|
+
typeTransformedContract.request_struct({ x: 1, y: 2 })
|
|
119
|
+
).resolves.not.toThrow();
|
|
110
120
|
});
|
|
111
121
|
|
|
112
122
|
test('Parsing the array of structs in request', async () => {
|
|
113
|
-
return expect(
|
|
123
|
+
return expect(
|
|
124
|
+
typeTransformedContract.request_array_of_structs([{ x: 1, y: 2 }])
|
|
125
|
+
).resolves.not.toThrow();
|
|
114
126
|
});
|
|
115
127
|
|
|
116
128
|
test('Parsing the nested structs in request', async () => {
|
|
117
129
|
return expect(
|
|
118
|
-
|
|
130
|
+
typeTransformedContract.request_nested_structs({
|
|
119
131
|
p1: { x: 1, y: 2 },
|
|
120
132
|
p2: { x: 3, y: 4 },
|
|
121
133
|
extra: 5,
|
|
@@ -124,43 +136,45 @@ describe('class Contract {}', () => {
|
|
|
124
136
|
});
|
|
125
137
|
|
|
126
138
|
test('Parsing the tuple in request', async () => {
|
|
127
|
-
return expect(
|
|
139
|
+
return expect(typeTransformedContract.request_tuple([1, 2])).resolves.not.toThrow();
|
|
128
140
|
});
|
|
129
141
|
|
|
130
142
|
test('Parsing the multiple types in request', async () => {
|
|
131
|
-
return expect(
|
|
143
|
+
return expect(
|
|
144
|
+
typeTransformedContract.request_mixed_types(2, { x: 1, y: 2 }, [1])
|
|
145
|
+
).resolves.not.toThrow();
|
|
132
146
|
});
|
|
133
147
|
});
|
|
134
148
|
|
|
135
149
|
describe('Response Type Transformation', () => {
|
|
136
150
|
test('Parsing the felt in response', async () => {
|
|
137
|
-
const { res } = await
|
|
151
|
+
const { res } = await typeTransformedContract.get_felt();
|
|
138
152
|
expect(res).toStrictEqual(toBN(4));
|
|
139
153
|
});
|
|
140
154
|
|
|
141
155
|
test('Parsing the array of felt in response', async () => {
|
|
142
|
-
const result = await
|
|
156
|
+
const result = await typeTransformedContract.get_array_of_felts();
|
|
143
157
|
const [res] = result;
|
|
144
158
|
expect(res).toStrictEqual([toBN(4), toBN(5)]);
|
|
145
159
|
expect(res).toStrictEqual(result.res);
|
|
146
160
|
});
|
|
147
161
|
|
|
148
162
|
test('Parsing the array of structs in response', async () => {
|
|
149
|
-
const result = await
|
|
163
|
+
const result = await typeTransformedContract.get_struct();
|
|
150
164
|
const [res] = result;
|
|
151
165
|
expect(res).toStrictEqual({ x: toBN(1), y: toBN(2) });
|
|
152
166
|
expect(res).toStrictEqual(result.res);
|
|
153
167
|
});
|
|
154
168
|
|
|
155
169
|
test('Parsing the array of structs in response', async () => {
|
|
156
|
-
const result = await
|
|
170
|
+
const result = await typeTransformedContract.get_array_of_structs();
|
|
157
171
|
const [res] = result;
|
|
158
172
|
expect(res).toStrictEqual([{ x: toBN(1), y: toBN(2) }]);
|
|
159
173
|
expect(res).toStrictEqual(result.res);
|
|
160
174
|
});
|
|
161
175
|
|
|
162
176
|
test('Parsing the nested structs in response', async () => {
|
|
163
|
-
const result = await
|
|
177
|
+
const result = await typeTransformedContract.get_nested_structs();
|
|
164
178
|
const [res] = result;
|
|
165
179
|
expect(res).toStrictEqual({
|
|
166
180
|
p1: { x: toBN(1), y: toBN(2) },
|
|
@@ -171,14 +185,14 @@ describe('class Contract {}', () => {
|
|
|
171
185
|
});
|
|
172
186
|
|
|
173
187
|
test('Parsing the tuple in response', async () => {
|
|
174
|
-
const result = await
|
|
188
|
+
const result = await typeTransformedContract.get_tuple();
|
|
175
189
|
const [res] = result;
|
|
176
190
|
expect(res).toStrictEqual([toBN(1), toBN(2), toBN(3)]);
|
|
177
191
|
expect(res).toStrictEqual(result.res);
|
|
178
192
|
});
|
|
179
193
|
|
|
180
194
|
test('Parsing the multiple types in response', async () => {
|
|
181
|
-
const result = await
|
|
195
|
+
const result = await typeTransformedContract.get_mixed_types();
|
|
182
196
|
const [tuple, number, array, point] = result;
|
|
183
197
|
expect(tuple).toStrictEqual([toBN(1), toBN(2)]);
|
|
184
198
|
expect(number).toStrictEqual(toBN(3));
|
|
@@ -196,30 +210,29 @@ describe('class Contract {}', () => {
|
|
|
196
210
|
describe('class ContractFactory {}', () => {
|
|
197
211
|
let erc20Address: string;
|
|
198
212
|
const wallet = stark.randomAddress();
|
|
199
|
-
|
|
213
|
+
const account = getTestAccount(provider);
|
|
214
|
+
const constructorCalldata = [encodeShortString('Token'), encodeShortString('ERC20'), wallet];
|
|
215
|
+
const classHash = '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a';
|
|
200
216
|
|
|
201
217
|
beforeAll(async () => {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
);
|
|
207
|
-
|
|
208
|
-
await provider.waitForTransaction(transaction_hash);
|
|
209
|
-
erc20Address = contract_address;
|
|
218
|
+
await account.declareDeploy({
|
|
219
|
+
contract: compiledErc20,
|
|
220
|
+
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
|
|
221
|
+
constructorCalldata,
|
|
222
|
+
});
|
|
210
223
|
});
|
|
211
224
|
test('deployment of new contract', async () => {
|
|
212
|
-
const factory = new ContractFactory(compiledErc20,
|
|
213
|
-
const erc20 = await factory.deploy(
|
|
225
|
+
const factory = new ContractFactory(compiledErc20, classHash, account);
|
|
226
|
+
const erc20 = await factory.deploy(constructorCalldata);
|
|
214
227
|
expect(erc20 instanceof Contract);
|
|
215
228
|
});
|
|
216
229
|
test('wait for deployment transaction', async () => {
|
|
217
|
-
const factory = new ContractFactory(compiledErc20,
|
|
218
|
-
const contract = await factory.deploy(
|
|
230
|
+
const factory = new ContractFactory(compiledErc20, classHash, account);
|
|
231
|
+
const contract = await factory.deploy(constructorCalldata);
|
|
219
232
|
expect(contract.deployed()).resolves.not.toThrow();
|
|
220
233
|
});
|
|
221
234
|
test('attach new contract', async () => {
|
|
222
|
-
const factory = new ContractFactory(compiledErc20,
|
|
235
|
+
const factory = new ContractFactory(compiledErc20, classHash, account);
|
|
223
236
|
const erc20 = factory.attach(erc20Address);
|
|
224
237
|
expect(erc20 instanceof Contract);
|
|
225
238
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BlockNumber, GetBlockResponse, stark } from '../src';
|
|
2
2
|
import { toBN } from '../src/utils/number';
|
|
3
|
-
import {
|
|
3
|
+
import { encodeShortString } from '../src/utils/shortString';
|
|
4
|
+
import { compiledErc20, erc20ClassHash, getTestAccount, getTestProvider } from './fixtures';
|
|
4
5
|
|
|
5
6
|
const { compileCalldata } = stark;
|
|
6
7
|
|
|
@@ -9,21 +10,21 @@ const testProvider = getTestProvider();
|
|
|
9
10
|
describe('defaultProvider', () => {
|
|
10
11
|
let exampleTransactionHash: string;
|
|
11
12
|
let erc20ContractAddress: string;
|
|
12
|
-
|
|
13
13
|
let exampleBlock: GetBlockResponse;
|
|
14
14
|
let exampleBlockNumber: BlockNumber;
|
|
15
15
|
let exampleBlockHash: string;
|
|
16
16
|
const wallet = stark.randomAddress();
|
|
17
|
+
const account = getTestAccount(testProvider);
|
|
17
18
|
|
|
18
19
|
beforeAll(async () => {
|
|
19
|
-
const
|
|
20
|
+
const { deploy } = await account.declareDeploy({
|
|
21
|
+
contract: compiledErc20,
|
|
22
|
+
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
|
|
23
|
+
constructorCalldata: [encodeShortString('Token'), encodeShortString('ERC20'), wallet],
|
|
24
|
+
});
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
);
|
|
24
|
-
await testProvider.waitForTransaction(transaction_hash);
|
|
25
|
-
exampleTransactionHash = transaction_hash;
|
|
26
|
-
erc20ContractAddress = contract_address;
|
|
26
|
+
exampleTransactionHash = deploy.transaction_hash;
|
|
27
|
+
erc20ContractAddress = deploy.contract_address;
|
|
27
28
|
|
|
28
29
|
exampleBlock = await testProvider.getBlock('latest');
|
|
29
30
|
exampleBlockHash = exampleBlock.block_hash;
|
|
@@ -31,7 +32,7 @@ describe('defaultProvider', () => {
|
|
|
31
32
|
});
|
|
32
33
|
|
|
33
34
|
describe('endpoints', () => {
|
|
34
|
-
test('
|
|
35
|
+
test('declareDeploy()', () => {
|
|
35
36
|
expect(erc20ContractAddress).toBeTruthy();
|
|
36
37
|
expect(exampleTransactionHash).toBeTruthy();
|
|
37
38
|
});
|
|
@@ -74,9 +75,9 @@ describe('defaultProvider', () => {
|
|
|
74
75
|
});
|
|
75
76
|
});
|
|
76
77
|
|
|
77
|
-
test('
|
|
78
|
-
const nonce = await testProvider.
|
|
79
|
-
return expect(nonce).toEqual('0x0');
|
|
78
|
+
test('getNonceForAddress()', async () => {
|
|
79
|
+
const nonce = await testProvider.getNonceForAddress(erc20ContractAddress);
|
|
80
|
+
return expect(toBN(nonce)).toEqual(toBN('0x0'));
|
|
80
81
|
});
|
|
81
82
|
|
|
82
83
|
test('getClassAt(contractAddress, blockNumber="latest")', async () => {
|
package/__tests__/fixtures.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
2
3
|
|
|
3
4
|
import { Account, ProviderInterface, RpcProvider, SequencerProvider, ec, json } from '../src';
|
|
4
|
-
import { CompiledContract
|
|
5
|
-
import { encodeShortString } from '../src/utils/shortString';
|
|
5
|
+
import { CompiledContract } from '../src/types';
|
|
6
6
|
|
|
7
7
|
const readContract = (name: string): CompiledContract =>
|
|
8
|
-
json.parse(
|
|
8
|
+
json.parse(
|
|
9
|
+
fs.readFileSync(path.resolve(__dirname, `../__mocks__/${name}.json`)).toString('ascii')
|
|
10
|
+
);
|
|
9
11
|
|
|
10
12
|
export const compiledOpenZeppelinAccount = readContract('Account');
|
|
11
13
|
export const compiledErc20 = readContract('ERC20');
|
|
@@ -13,30 +15,36 @@ export const compiledL1L2 = readContract('l1l2_compiled');
|
|
|
13
15
|
export const compiledTypeTransformation = readContract('contract');
|
|
14
16
|
export const compiledMulticall = readContract('multicall');
|
|
15
17
|
export const compiledTestDapp = readContract('TestDapp');
|
|
18
|
+
export const compiledStarknetId = readContract('starknetId_compiled');
|
|
19
|
+
export const compiledNamingContract = readContract('naming_compiled');
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
const
|
|
21
|
+
/* Default test config based on run `starknet-devnet --seed 0` */
|
|
22
|
+
const DEFAULT_TEST_PROVIDER_SEQUENCER_URL = 'http://127.0.0.1:5050/';
|
|
23
|
+
const DEFAULT_TEST_ACCOUNT_ADDRESS =
|
|
19
24
|
'0x7e00d496e324876bbc8531f2d9a82bf154d1a04a50218ee74cdd372f75a551a';
|
|
20
25
|
const DEFAULT_TEST_ACCOUNT_PRIVATE_KEY = '0xe3e70682c2094cac629f6fbed82c07cd';
|
|
21
26
|
|
|
22
|
-
|
|
27
|
+
/* User defined config or default one */
|
|
28
|
+
const BASE_URL = process.env.TEST_PROVIDER_BASE_URL || DEFAULT_TEST_PROVIDER_SEQUENCER_URL;
|
|
23
29
|
const RPC_URL = process.env.TEST_RPC_URL;
|
|
24
30
|
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
/* Detect user defined node or sequencer, if none default to sequencer if both default to node */
|
|
32
|
+
const PROVIDER_URL = RPC_URL || BASE_URL;
|
|
33
|
+
|
|
34
|
+
/* Detect is localhost devnet */
|
|
35
|
+
export const IS_LOCALHOST_DEVNET =
|
|
36
|
+
PROVIDER_URL.includes('localhost') || PROVIDER_URL.includes('127.0.0.1');
|
|
37
|
+
|
|
38
|
+
/* Definitions */
|
|
39
|
+
export const IS_RPC = !!RPC_URL;
|
|
40
|
+
export const IS_SEQUENCER = !RPC_URL;
|
|
33
41
|
|
|
34
42
|
export const getTestProvider = (): ProviderInterface => {
|
|
35
43
|
const provider = RPC_URL
|
|
36
44
|
? new RpcProvider({ nodeUrl: RPC_URL })
|
|
37
45
|
: new SequencerProvider({ baseUrl: BASE_URL });
|
|
38
46
|
|
|
39
|
-
if (
|
|
47
|
+
if (IS_LOCALHOST_DEVNET) {
|
|
40
48
|
// accelerate the tests when running locally
|
|
41
49
|
const originalWaitForTransaction = provider.waitForTransaction.bind(provider);
|
|
42
50
|
provider.waitForTransaction = (txHash, retryInterval) => {
|
|
@@ -52,11 +60,10 @@ export const getTestAccount = (provider: ProviderInterface) => {
|
|
|
52
60
|
let testAccountAddress = process.env.TEST_ACCOUNT_ADDRESS;
|
|
53
61
|
let testAccountPrivateKey = process.env.TEST_ACCOUNT_PRIVATE_KEY;
|
|
54
62
|
|
|
55
|
-
if (!
|
|
63
|
+
if (!IS_LOCALHOST_DEVNET) {
|
|
56
64
|
if (!testAccountPrivateKey) {
|
|
57
65
|
throw new Error('TEST_ACCOUNT_PRIVATE_KEY is not set');
|
|
58
66
|
}
|
|
59
|
-
|
|
60
67
|
if (!testAccountAddress) {
|
|
61
68
|
throw new Error('TEST_ACCOUNT_ADDRESS is not set');
|
|
62
69
|
}
|
|
@@ -69,15 +76,9 @@ export const getTestAccount = (provider: ProviderInterface) => {
|
|
|
69
76
|
};
|
|
70
77
|
|
|
71
78
|
const describeIf = (condition: boolean) => (condition ? describe : describe.skip);
|
|
72
|
-
export const describeIfSequencer = describeIf(
|
|
79
|
+
export const describeIfSequencer = describeIf(IS_SEQUENCER);
|
|
73
80
|
export const describeIfRpc = describeIf(IS_RPC);
|
|
74
|
-
export const describeIfNotDevnet = describeIf(!
|
|
81
|
+
export const describeIfNotDevnet = describeIf(!IS_LOCALHOST_DEVNET);
|
|
82
|
+
export const describeIfDevnet = describeIf(IS_LOCALHOST_DEVNET);
|
|
75
83
|
|
|
76
84
|
export const erc20ClassHash = '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a';
|
|
77
|
-
|
|
78
|
-
export const getERC20DeployPayload = (recipient: string): DeployContractPayload => {
|
|
79
|
-
return {
|
|
80
|
-
contract: compiledErc20,
|
|
81
|
-
constructorCalldata: [encodeShortString('Token'), encodeShortString('ERC20'), recipient],
|
|
82
|
-
};
|
|
83
|
-
};
|