starknet 4.13.2 → 4.14.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 +20 -0
- package/CODE_OF_CONDUCT.md +128 -0
- package/README.md +2 -2
- package/__tests__/account.test.ts +38 -14
- package/__tests__/contract.test.ts +70 -57
- package/__tests__/defaultProvider.test.ts +14 -13
- package/__tests__/fixtures.ts +26 -27
- package/__tests__/rpcProvider.test.ts +19 -16
- package/__tests__/sequencerProvider.test.ts +47 -55
- package/__tests__/utils/utils.test.ts +10 -0
- package/dist/index.d.ts +112 -28
- package/dist/index.global.js +511 -453
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +107 -49
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +107 -49
- package/dist/index.mjs.map +1 -1
- package/index.d.ts +112 -28
- package/index.global.js +511 -453
- package/index.global.js.map +1 -1
- package/index.js +107 -49
- package/index.js.map +1 -1
- package/index.mjs +107 -49
- package/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/account/default.ts +29 -7
- package/src/account/interface.ts +71 -5
- package/src/contract/contractFactory.ts +20 -13
- package/src/contract/default.ts +10 -1
- 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 +11 -6
- 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/www/docs/API/account.md +156 -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
package/www/guides/erc20.md
CHANGED
|
@@ -4,104 +4,160 @@ sidebar_position: 3
|
|
|
4
4
|
|
|
5
5
|
# Deploy an ERC20 Contract
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Deploying a contract relies on having an account already set up with enough ETH to pay fees for both:
|
|
8
|
+
|
|
9
|
+
1. The class declaration
|
|
10
|
+
2. The transaction interacting with the UDC (universal deployer contract)
|
|
11
|
+
|
|
12
|
+
> You must first declare your contract class and only then deploy a new instance of it!
|
|
13
|
+
|
|
14
|
+
High level explanations from StarkWare can be found in this Notion [page](https://starkware.notion.site/Deploy-a-contract-and-an-account-on-StarkNet-ed2fd13301d2414e8223bb72bb90e386).
|
|
15
|
+
|
|
16
|
+
ERC20 implementations:
|
|
17
|
+
|
|
18
|
+
1. Argent ERC20 contract can be found [here](https://github.com/argentlabs/argent-contracts-starknet/blob/develop/contracts/lib/ERC20.cairo).
|
|
19
|
+
2. OpenZeppelin ERC20 can be found [here](https://github.com/OpenZeppelin/cairo-contracts/tree/main/src/openzeppelin/token/erc20).
|
|
20
|
+
|
|
21
|
+
## Setup
|
|
8
22
|
|
|
9
23
|
```javascript
|
|
10
24
|
const compiledErc20 = json.parse(
|
|
11
25
|
fs.readFileSync("./ERC20.json").toString("ascii")
|
|
12
26
|
);
|
|
13
|
-
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
// devnet private key from Account #0 if generated with --seed 0
|
|
31
|
+
const starkKeyPair = ec.getKeyPair("0xe3e70682c2094cac629f6fbed82c07cd");
|
|
32
|
+
const accountAddress = "0x7e00d496e324876bbc8531f2d9a82bf154d1a04a50218ee74cdd372f75a551a";
|
|
33
|
+
|
|
34
|
+
const recieverAddress = '0x69b49c2cc8b16e80e86bfc5b0614a59aa8c9b601569c7b80dde04d3f3151b79';
|
|
35
|
+
|
|
36
|
+
// Starknet.js currently doesn't have the functionality to calculate the class hash
|
|
37
|
+
const erc20ClassHash = '0x03f794a28472089a1a99b7969fc51cd5fbe22dd09e3f38d2bd6fa109cb3f4ecf';
|
|
38
|
+
|
|
39
|
+
const account = new Account(
|
|
40
|
+
provider,
|
|
41
|
+
accountAddress,
|
|
42
|
+
starkKeyPair
|
|
43
|
+
);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Declare contract
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
const erc20DeclareResponse = await account.declare({
|
|
50
|
+
classHash: erc20ClassHash,
|
|
14
51
|
contract: compiledErc20,
|
|
15
|
-
constructorCalldata: [encodeShortString('TokenName'), encodeShortString('TokenSymbol'), recipient], // Here the `recipient` receives the initial 1000 tokens
|
|
16
52
|
});
|
|
17
53
|
|
|
18
|
-
|
|
19
|
-
await defaultProvider.waitForTransaction(erc20Response.transaction_hash);
|
|
54
|
+
await provider.waitForTransaction(erc20DeclareResponse.transaction_hash);
|
|
20
55
|
```
|
|
21
56
|
|
|
22
|
-
|
|
23
|
-
>
|
|
24
|
-
> The ERC20 contract can be found [here](https://github.com/argentlabs/argent-contracts-starknet/blob/develop/contracts/lib/ERC20.cairo)
|
|
25
|
-
|
|
26
|
-
## Get the erc20 contract address and create the contact object
|
|
57
|
+
## Deploy contract
|
|
27
58
|
|
|
28
59
|
```javascript
|
|
29
|
-
const
|
|
30
|
-
|
|
60
|
+
const salt = '900080545022'; // use some random salt
|
|
61
|
+
|
|
62
|
+
const erc20Response = await account.deploy({
|
|
63
|
+
classHash: erc20ClassHash,
|
|
64
|
+
constructorCalldata: stark.compileCalldata({
|
|
65
|
+
name: shortString.encodeShortString('TestToken'),
|
|
66
|
+
symbol: shortString.encodeShortString('ERC20'),
|
|
67
|
+
decimals: 18,
|
|
68
|
+
initial_supply: ['1000'],
|
|
69
|
+
recipient: account.address,
|
|
70
|
+
}),
|
|
71
|
+
salt,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
await provider.waitForTransaction(erc20Response.transaction_hash);
|
|
75
|
+
|
|
76
|
+
const txReceipt = await provider.getTransactionReceipt(erc20Response.transaction_hash);
|
|
31
77
|
```
|
|
32
78
|
|
|
33
|
-
##
|
|
79
|
+
## Interact with contracts
|
|
34
80
|
|
|
35
|
-
|
|
81
|
+
We have 2 options to interact with contracts.
|
|
36
82
|
|
|
37
|
-
|
|
83
|
+
### Option 1 - call the contract object
|
|
38
84
|
|
|
39
85
|
```javascript
|
|
86
|
+
const erc20 = new Contract(compiledErc20.abi, erc20Address, provider);
|
|
87
|
+
|
|
40
88
|
erc20.connect(account);
|
|
41
89
|
|
|
42
|
-
const { transaction_hash: mintTxHash } = await erc20.
|
|
43
|
-
|
|
44
|
-
|
|
90
|
+
const { transaction_hash: mintTxHash } = await erc20.transfer(
|
|
91
|
+
recieverAddress,
|
|
92
|
+
['0', '10'], // send 10 tokens as Uint256
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
await provider.waitForTransaction(mintTxHash);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Option 2 - call contract from Account
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
101
|
+
const executeHash = await account.execute(
|
|
45
102
|
{
|
|
46
|
-
|
|
103
|
+
contractAddress: erc20Address,
|
|
104
|
+
entrypoint: 'transfer',
|
|
105
|
+
calldata: stark.compileCalldata({
|
|
106
|
+
recipient: recieverAddress,
|
|
107
|
+
amount: ['10']
|
|
108
|
+
})
|
|
47
109
|
}
|
|
48
110
|
);
|
|
49
111
|
|
|
50
|
-
|
|
51
|
-
await defaultProvider.waitForTransaction(mintTxHash);
|
|
112
|
+
await provider.waitForTransaction(executeHash.transaction_hash);
|
|
52
113
|
```
|
|
53
114
|
|
|
54
|
-
|
|
55
|
-
>
|
|
56
|
-
> Transaction can be rejected if `maxFee` is lower than actual.
|
|
57
|
-
>
|
|
58
|
-
> _Error: REJECTED: FEE_TRANSFER_FAILURE_
|
|
59
|
-
>
|
|
60
|
-
> _Actual fee exceeded max fee._
|
|
61
|
-
>
|
|
62
|
-
> If this occurs, set `maxFee` to a higher value, for example: 999999995330000
|
|
63
|
-
|
|
64
|
-
## Check balance after mint
|
|
115
|
+
## Check the balance
|
|
65
116
|
|
|
66
117
|
```javascript
|
|
67
|
-
console.log(`Calling StarkNet for account balance...`);
|
|
68
118
|
const balanceBeforeTransfer = await erc20.balanceOf(account.address);
|
|
69
119
|
|
|
70
120
|
console.log(
|
|
71
121
|
`account Address ${account.address} has a balance of:`,
|
|
72
|
-
number.toBN(balanceBeforeTransfer.
|
|
122
|
+
number.toBN(balanceBeforeTransfer[0].high).toString()
|
|
73
123
|
);
|
|
74
124
|
```
|
|
75
125
|
|
|
76
|
-
##
|
|
126
|
+
## Convenience Methods
|
|
77
127
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
contractAddress: erc20Address,
|
|
84
|
-
entrypoint: "transfer",
|
|
85
|
-
calldata: [erc20Address, "10", "0"],
|
|
86
|
-
},
|
|
87
|
-
undefined,
|
|
88
|
-
{ maxFee: "1" }
|
|
89
|
-
);
|
|
128
|
+
### deployContract convenience method
|
|
129
|
+
|
|
130
|
+
High level wrapper for deploy. Doesn't require `waitForTransaction`. Response similar to deprecated `provider.deployContract`.
|
|
131
|
+
|
|
132
|
+
Convenient also to get the address of the deployed contract in the same response - easier than using the `deploy` already mentioned in the guide.
|
|
90
133
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
134
|
+
```typescript
|
|
135
|
+
const deployResponse = await account.deployContract({
|
|
136
|
+
classHash: erc20ClassHash,
|
|
137
|
+
constructorCalldata: [
|
|
138
|
+
encodeShortString('Token'),
|
|
139
|
+
encodeShortString('ERC20'),
|
|
140
|
+
account.address,
|
|
141
|
+
],
|
|
142
|
+
});
|
|
94
143
|
```
|
|
95
144
|
|
|
96
|
-
|
|
145
|
+
### declareDeploy convenience method
|
|
97
146
|
|
|
98
|
-
|
|
99
|
-
// Check balance after transfer - should be 1990 (1000 initial supply + 1000 mint - 10 transfer)
|
|
100
|
-
console.log(`Calling StarkNet for account balance...`);
|
|
101
|
-
const balanceAfterTransfer = await erc20.balanceOf(account.address);
|
|
147
|
+
High level wrapper for declare & deploy. Doesn't require `waitForTransaction`. Functionality similar to deprecated `provider.deployContract`.
|
|
102
148
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
149
|
+
Declare and Deploy contract using single function:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
const declareDeploy = await account.declareDeploy({
|
|
153
|
+
contract: compiledErc20,
|
|
154
|
+
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
|
|
155
|
+
constructorCalldata: [
|
|
156
|
+
encodeShortString('Token'),
|
|
157
|
+
encodeShortString('ERC20'),
|
|
158
|
+
account.address,
|
|
159
|
+
],
|
|
160
|
+
});
|
|
161
|
+
const declareTransactionHash = declareDeploy.declare.transaction_hash
|
|
162
|
+
const erc20Address = declareDeploy.deploy.contract_address;
|
|
107
163
|
```
|
package/www/guides/intro.md
CHANGED
|
@@ -5,9 +5,12 @@ sidebar_position: 1
|
|
|
5
5
|
# Getting Started
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
+
|
|
9
|
+
# use the main branch
|
|
10
|
+
|
|
8
11
|
npm install starknet
|
|
9
12
|
|
|
10
|
-
# to use latest features
|
|
13
|
+
# to use latest features (merges in develop branch)
|
|
11
14
|
|
|
12
15
|
npm install starknet@next
|
|
13
16
|
```
|
|
@@ -16,8 +19,12 @@ npm install starknet@next
|
|
|
16
19
|
|
|
17
20
|
Please check the StarkNet documentation <ins>[here](https://www.cairo-lang.org/docs/hello_starknet/intro.html)</ins> to compile starknet contracts.
|
|
18
21
|
|
|
19
|
-
Additional helpful resources can also be found at [OpenZeppelin](https://docs.openzeppelin.com/contracts-cairo/0.
|
|
22
|
+
Additional helpful resources can also be found at <ins>[OpenZeppelin](https://docs.openzeppelin.com/contracts-cairo/0.5.0/)</ins> documentation site.
|
|
23
|
+
|
|
24
|
+
Get the class hash of a contract: [starkli](https://github.com/xJonathanLEI/starkli).
|
|
25
|
+
|
|
26
|
+
## Full example with account & erc20 deployments
|
|
20
27
|
|
|
21
|
-
|
|
28
|
+
Please take a look at our workshop using OpenZeppelin contracts <ins>[here](https://github.com/0xs34n/starknet.js-workshop)</ins>.
|
|
22
29
|
|
|
23
|
-
|
|
30
|
+
Example with Argent contract <ins>[here](https://github.com/0xs34n/starknet.js-account)</ins>.
|