starknet 3.15.0 → 3.15.3

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.
@@ -12,7 +12,7 @@ import {
12
12
  } from './fixtures';
13
13
 
14
14
  describe('deploy and test Wallet', () => {
15
- const account: Account = getTestAccount();
15
+ const account = getTestAccount();
16
16
  const provider = getTestProvider();
17
17
  let erc20: Contract;
18
18
  let erc20Address: string;
@@ -24,8 +24,9 @@ describe('deploy and test Wallet', () => {
24
24
  const erc20Response = await provider.deployContract({
25
25
  contract: compiledErc20,
26
26
  });
27
- erc20Address = erc20Response.address;
28
- erc20 = new Contract(compiledErc20.abi, erc20Address);
27
+
28
+ erc20Address = erc20Response.address!;
29
+ erc20 = new Contract(compiledErc20.abi, erc20Address, provider);
29
30
  expect(erc20Response.code).toBe('TRANSACTION_RECEIVED');
30
31
 
31
32
  await provider.waitForTransaction(erc20Response.transaction_hash);
@@ -40,10 +41,14 @@ describe('deploy and test Wallet', () => {
40
41
 
41
42
  await provider.waitForTransaction(mintResponse.transaction_hash);
42
43
 
44
+ const x = await erc20.balance_of(account.address);
45
+
46
+ expect(number.toBN(x.res as string).toString()).toStrictEqual(number.toBN(1000).toString());
47
+
43
48
  const dappResponse = await provider.deployContract({
44
49
  contract: compiledTestDapp,
45
50
  });
46
- dapp = new Contract(compiledTestDapp.abi, dappResponse.address);
51
+ dapp = new Contract(compiledTestDapp.abi, dappResponse.address!, provider);
47
52
  expect(dappResponse.code).toBe('TRANSACTION_RECEIVED');
48
53
 
49
54
  await provider.waitForTransaction(dappResponse.transaction_hash);
@@ -60,9 +65,9 @@ describe('deploy and test Wallet', () => {
60
65
  });
61
66
 
62
67
  test('read balance of wallet', async () => {
63
- const { res } = await erc20.balance_of(account.address);
68
+ const x = await erc20.balance_of(account.address);
64
69
 
65
- expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(1000).toString());
70
+ expect(number.toBN(x.res as string).toString()).toStrictEqual(number.toBN(1000).toString());
66
71
  });
67
72
 
68
73
  test('execute by wallet owner', async () => {
@@ -123,9 +128,15 @@ describe('deploy and test Wallet', () => {
123
128
  expect(toBN(response.number as string).toString()).toStrictEqual('57');
124
129
  });
125
130
 
126
- test('sign and verify offchain message', async () => {
131
+ test('sign and verify offchain message fail', async () => {
127
132
  const signature = await account.signMessage(typedDataExample);
133
+ // change the signature to make it invalid
134
+ signature[0] += '123';
135
+ expect(await account.verifyMessage(typedDataExample, signature)).toBe(false);
136
+ });
128
137
 
138
+ test('sign and verify offchain message', async () => {
139
+ const signature = await account.signMessage(typedDataExample);
129
140
  expect(await account.verifyMessage(typedDataExample, signature)).toBe(true);
130
141
  });
131
142
 
@@ -143,7 +154,7 @@ describe('deploy and test Wallet', () => {
143
154
 
144
155
  await provider.waitForTransaction(accountResponse.transaction_hash);
145
156
 
146
- newAccount = new Account(provider, accountResponse.address, starkKeyPair);
157
+ newAccount = new Account(provider, accountResponse.address!, starkKeyPair);
147
158
  });
148
159
 
149
160
  test('read nonce', async () => {
@@ -38,7 +38,7 @@ describe('class Contract {}', () => {
38
38
  contract: compiledMulticall,
39
39
  });
40
40
 
41
- contract = new Contract(compiledMulticall.abi, multicallAddress!);
41
+ contract = new Contract(compiledMulticall.abi, multicallAddress!, provider);
42
42
 
43
43
  expect(m_code).toBe('TRANSACTION_RECEIVED');
44
44
 
@@ -223,12 +223,6 @@ describe('class Contract {}', () => {
223
223
  erc20.connect(account);
224
224
  expect(erc20.providerOrAccount instanceof Account);
225
225
  });
226
-
227
- test('estimate gas fee for `mint`', async () => {
228
- const res = await erc20.estimateFee.mint(wallet, '10');
229
- expect(res).toHaveProperty('amount');
230
- expect(res).toHaveProperty('unit');
231
- });
232
226
  });
233
227
  });
234
228
 
@@ -243,17 +237,17 @@ describe('class ContractFactory {}', () => {
243
237
  erc20Address = address!;
244
238
  });
245
239
  test('deployment of new contract', async () => {
246
- const factory = new ContractFactory(compiledErc20);
240
+ const factory = new ContractFactory(compiledErc20, provider);
247
241
  const erc20 = await factory.deploy();
248
242
  expect(erc20 instanceof Contract);
249
243
  });
250
244
  test('wait for deployment transaction', async () => {
251
- const factory = new ContractFactory(compiledErc20);
245
+ const factory = new ContractFactory(compiledErc20, provider);
252
246
  const contract = await factory.deploy();
253
247
  expect(contract.deployed()).resolves.not.toThrow();
254
248
  });
255
249
  test('attach new contract', async () => {
256
- const factory = new ContractFactory(compiledErc20);
250
+ const factory = new ContractFactory(compiledErc20, provider);
257
251
  const erc20 = factory.attach(erc20Address);
258
252
  expect(erc20 instanceof Contract);
259
253
  });
@@ -1,33 +1,45 @@
1
1
  import fs from 'fs';
2
2
 
3
- import { Account, defaultProvider, ec, json } from '../src';
3
+ import { Account, Provider, ec, json } from '../src';
4
4
  import { CompiledContract } from '../src/types';
5
5
 
6
6
  const readContract = (name: string): CompiledContract =>
7
7
  json.parse(fs.readFileSync(`./__mocks__/${name}.json`).toString('ascii'));
8
8
 
9
9
  export const compiledOpenZeppelinAccount = readContract('Account');
10
- export const compiledArgentAccount = readContract('ArgentAccount');
11
10
  export const compiledErc20 = readContract('ERC20');
12
11
  export const compiledTypeTransformation = readContract('contract');
13
12
  export const compiledMulticall = readContract('multicall');
14
13
  export const compiledTestDapp = readContract('TestDapp');
15
14
 
16
- const DEFAULT_TEST_ACCOUNT_ADDRESS =
17
- '0x6c0a3ca4f79e978f3b7005898aaa49bef4a24aeaa5f10c6a97887516400197e';
15
+ const DEFAULT_TEST_PROVIDER_BASE_URL = 'http://127.0.0.1:5050/';
16
+ const DEFAULT_TEST_ACCOUNT_ADDRESS = // run `starknet-devnet --seed 0` and this will be the first account
17
+ '0x65d53c8ec4178096167b35a08e16e548d8075cb08ad7bc63d07966ca13569dc';
18
+ const DEFAULT_TEST_ACCOUNT_PRIVATE_KEY = '0xe3e70682c2094cac629f6fbed82c07cd';
18
19
 
19
- export const getTestAccount = () => {
20
- const testAccountAddress = process.env.TEST_ACCOUNT_ADDRESS || DEFAULT_TEST_ACCOUNT_ADDRESS;
21
- const testAccountPrivateKey = process.env.TEST_ACCOUNT_PRIVATE_KEY;
20
+ export const getTestProvider = () => {
21
+ const baseUrl = process.env.TEST_PROVIDER_BASE_URL || DEFAULT_TEST_PROVIDER_BASE_URL;
22
22
 
23
- if (!testAccountPrivateKey) {
24
- throw new Error('TEST_ACCOUNT_PRIVATE_KEY is not set');
23
+ const provider = new Provider({ baseUrl });
24
+
25
+ if (baseUrl.includes('localhost') || baseUrl.includes('127.0.0.1')) {
26
+ // accelerate the tests when running locally
27
+ const originalWaitForTransaction = provider.waitForTransaction.bind(provider);
28
+ provider.waitForTransaction = (txHash, retryInterval) => {
29
+ return originalWaitForTransaction(txHash, retryInterval || 1000);
30
+ };
25
31
  }
26
32
 
27
- return new Account(defaultProvider, testAccountAddress, ec.getKeyPair(testAccountPrivateKey));
33
+ return provider;
28
34
  };
29
35
 
30
- export const getTestProvider = () => {
31
- // Will support both local and remote providers in the future
32
- return defaultProvider;
36
+ // test account with fee token balance
37
+ export const getTestAccount = () => {
38
+ const provider = getTestProvider();
39
+
40
+ const testAccountAddress = process.env.TEST_ACCOUNT_ADDRESS || DEFAULT_TEST_ACCOUNT_ADDRESS;
41
+ const testAccountPrivateKey =
42
+ process.env.TEST_ACCOUNT_PRIVATE_KEY || DEFAULT_TEST_ACCOUNT_PRIVATE_KEY;
43
+
44
+ return new Account(provider, testAccountAddress, ec.getKeyPair(testAccountPrivateKey));
33
45
  };
@@ -1,6 +1,9 @@
1
1
  /* eslint-disable no-console */
2
+ import fetch from 'cross-fetch';
2
3
  import { register } from 'fetch-intercept';
3
4
 
5
+ global.fetch = fetch;
6
+
4
7
  jest.setTimeout(50 * 60 * 1000);
5
8
 
6
9
  if (process.env.DEBUG === 'true') {
@@ -1,95 +1,82 @@
1
- import { defaultProvider, stark } from '../src';
1
+ import { BlockNumber, stark } from '../src';
2
2
  import { toBN } from '../src/utils/number';
3
- import { compiledArgentAccount, compiledErc20 } from './fixtures';
3
+ import { compiledErc20, compiledOpenZeppelinAccount, getTestProvider } from './fixtures';
4
4
 
5
5
  const { compileCalldata } = stark;
6
6
 
7
+ const provider = getTestProvider();
8
+
9
+ const testIf = (condition: boolean) => (condition ? test : test.skip);
10
+
7
11
  describe('defaultProvider', () => {
12
+ let exampleTransactionHash: string;
13
+ let exampleContractAddress: string;
14
+ let exampleBlockHash: string;
15
+ let exampleBlockNumber: BlockNumber;
16
+ beforeAll(async () => {
17
+ const { code, transaction_hash, address } = await provider.deployContract({
18
+ contract: compiledErc20,
19
+ });
20
+ expect(code).toBe('TRANSACTION_RECEIVED');
21
+ await provider.waitForTransaction(transaction_hash);
22
+ exampleTransactionHash = transaction_hash;
23
+ exampleContractAddress = address!;
24
+ const transaction = await provider.getTransaction(transaction_hash);
25
+
26
+ if (transaction.status !== 'REJECTED') {
27
+ exampleBlockHash = transaction.block_hash;
28
+ exampleBlockNumber = transaction.block_number;
29
+ }
30
+ });
31
+
8
32
  describe('feeder gateway endpoints', () => {
9
- test('getContractAddresses()', async () => {
10
- const { GpsStatementVerifier, Starknet } = await defaultProvider.getContractAddresses();
33
+ testIf(provider.baseUrl.includes('starknet.io'))('getContractAddresses()', async () => {
34
+ // not supported in starknet-devnet
35
+ const { GpsStatementVerifier, Starknet } = await provider.getContractAddresses();
11
36
  expect(typeof GpsStatementVerifier).toBe('string');
12
37
  expect(typeof Starknet).toBe('string');
13
38
  });
14
- test('getBlock(blockHash=0x26e33ad2807590b93e98a04e703d7d64d4ead13591b50984ae558bdbe8fbcd2, blockNumber=undefined)', () => {
15
- return expect(
16
- defaultProvider.getBlock(
17
- '0x26e33ad2807590b93e98a04e703d7d64d4ead13591b50984ae558bdbe8fbcd2'
18
- )
19
- ).resolves.not.toThrow();
39
+ test(`getBlock(blockHash=${exampleBlockHash}, blockNumber=undefined)`, () => {
40
+ return expect(provider.getBlock(exampleBlockHash)).resolves.not.toThrow();
20
41
  });
21
- test('getBlock(blockHash=undefined, blockNumber=168890)', () => {
22
- return expect(defaultProvider.getBlock(168890)).resolves.not.toThrow();
42
+ test(`getBlock(blockHash=undefined, blockNumber=${exampleBlockNumber})`, () => {
43
+ return expect(provider.getBlock(exampleBlockNumber)).resolves.not.toThrow();
23
44
  });
24
45
  test('getBlock(blockHash=undefined, blockNumber=null)', () => {
25
- return expect(defaultProvider.getBlock()).resolves.not.toThrow();
46
+ return expect(provider.getBlock()).resolves.not.toThrow();
26
47
  });
27
48
  test('getBlock() -> { blockNumber }', async () => {
28
- const block = await defaultProvider.getBlock();
49
+ const block = await provider.getBlock();
29
50
  return expect(block).toHaveProperty('block_number');
30
51
  });
31
52
  test('getCode()', () => {
32
- return expect(
33
- defaultProvider.getCode(
34
- '0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
35
- 36663
36
- )
37
- ).resolves.not.toThrow();
53
+ return expect(provider.getCode(exampleContractAddress, 36663)).resolves.not.toThrow();
38
54
  });
39
55
  test('getCode(blockHash=undefined, blockNumber=null)', () => {
40
- return expect(
41
- defaultProvider.getCode(
42
- '0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166'
43
- )
44
- ).resolves.not.toThrow();
56
+ return expect(provider.getCode(exampleContractAddress)).resolves.not.toThrow();
45
57
  });
46
58
  test('getStorageAt() with "key" type of number', () => {
47
- return expect(
48
- defaultProvider.getStorageAt(
49
- '0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
50
- 0,
51
- 36663
52
- )
53
- ).resolves.not.toThrow();
59
+ return expect(provider.getStorageAt(exampleContractAddress, 0, 36663)).resolves.not.toThrow();
54
60
  });
55
61
  test('getStorageAt() with "key" type of string', () => {
56
62
  return expect(
57
- defaultProvider.getStorageAt(
58
- '0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
59
- '0',
60
- 36663
61
- )
63
+ provider.getStorageAt(exampleContractAddress, '0', 36663)
62
64
  ).resolves.not.toThrow();
63
65
  });
64
66
  test('getStorageAt() with "key" type of BN', () => {
65
67
  return expect(
66
- defaultProvider.getStorageAt(
67
- '0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
68
- toBN('0x0'),
69
- 36663
70
- )
68
+ provider.getStorageAt(exampleContractAddress, toBN('0x0'), 36663)
71
69
  ).resolves.not.toThrow();
72
70
  });
73
71
  test('getStorageAt(blockHash=undefined, blockNumber=null)', () => {
74
- return expect(
75
- defaultProvider.getStorageAt(
76
- '0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
77
- 0
78
- )
79
- ).resolves.not.toThrow();
72
+ return expect(provider.getStorageAt(exampleContractAddress, 0)).resolves.not.toThrow();
80
73
  });
81
74
  test('getTransactionStatus()', async () => {
82
- return expect(
83
- defaultProvider.getTransactionStatus(
84
- '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348'
85
- )
86
- ).resolves.not.toThrow();
75
+ return expect(provider.getTransactionStatus(exampleTransactionHash)).resolves.not.toThrow();
87
76
  });
88
77
 
89
78
  test('getTransaction() - successful transaction', async () => {
90
- const transaction = await defaultProvider.getTransaction(
91
- '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348'
92
- );
79
+ const transaction = await provider.getTransaction(exampleTransactionHash);
93
80
 
94
81
  expect(transaction).not.toHaveProperty('transaction_failure_reason');
95
82
 
@@ -98,36 +85,16 @@ describe('defaultProvider', () => {
98
85
  return expect(transaction.status).not.toEqual('REJECTED');
99
86
  });
100
87
 
101
- test('getTransaction() - failed transaction', async () => {
102
- const transaction = await defaultProvider.getTransaction(
103
- '0x698e60db2bae3ef8d5fafda10ff83b6ba634351aa1f4fcf8455ec0cffa738d9'
104
- );
105
-
106
- expect(transaction).toHaveProperty('transaction_failure_reason');
107
-
108
- return expect(transaction.status).toEqual('REJECTED');
109
- });
110
-
111
88
  test('getTransactionReceipt() - successful transaction', async () => {
112
- const transactionReceipt = await defaultProvider.getTransactionReceipt(
113
- '0x18c49389193b40e178dfc9f2f595a7c79a7a55639e9951d956329f2ce6cfd4f'
114
- );
89
+ const transactionReceipt = await provider.getTransactionReceipt(exampleTransactionHash);
115
90
 
116
91
  return expect(transactionReceipt).toHaveProperty('actual_fee');
117
92
  });
118
93
 
119
- test('getTransactionReceipt() - failed transaction', async () => {
120
- const transactionReceipt = await defaultProvider.getTransactionReceipt(
121
- '0x698e60db2bae3ef8d5fafda10ff83b6ba634351aa1f4fcf8455ec0cffa738d9'
122
- );
123
-
124
- return expect(transactionReceipt).not.toHaveProperty('actual_fee');
125
- });
126
-
127
94
  test('callContract()', () => {
128
95
  return expect(
129
- defaultProvider.callContract({
130
- contractAddress: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
96
+ provider.callContract({
97
+ contractAddress: exampleContractAddress,
131
98
  entrypoint: 'balance_of',
132
99
  calldata: compileCalldata({
133
100
  user: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
@@ -137,9 +104,7 @@ describe('defaultProvider', () => {
137
104
  });
138
105
 
139
106
  test('transaction trace', async () => {
140
- const transactionTrace = await defaultProvider.getTransactionTrace(
141
- '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348'
142
- );
107
+ const transactionTrace = await provider.getTransactionTrace(exampleTransactionHash);
143
108
  expect(transactionTrace).toHaveProperty('function_invocation');
144
109
  expect(transactionTrace).toHaveProperty('signature');
145
110
  });
@@ -147,7 +112,7 @@ describe('defaultProvider', () => {
147
112
 
148
113
  describe('addTransaction()', () => {
149
114
  test('declareContract()', async () => {
150
- const response = await defaultProvider.declareContract({
115
+ const response = await provider.declareContract({
151
116
  contract: compiledErc20,
152
117
  });
153
118
 
@@ -157,8 +122,8 @@ describe('defaultProvider', () => {
157
122
  });
158
123
 
159
124
  test('deployContract()', async () => {
160
- const response = await defaultProvider.deployContract({
161
- contract: compiledArgentAccount,
125
+ const response = await provider.deployContract({
126
+ contract: compiledOpenZeppelinAccount,
162
127
  });
163
128
 
164
129
  expect(response.code).toBe('TRANSACTION_RECEIVED');