You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Ethers.js](https://docs.ethers.org/v6/){target=\_blank} is a lightweight library that enables interaction with Ethereum Virtual Machine (EVM)-compatible blockchains through JavaScript. Ethers is widely used as a toolkit to establish connections and read and write blockchain data. This article demonstrates using Ethers.js to interact and deploy smart contracts to Polkadot Hub.
@@ -39,7 +37,7 @@ ethers-project
39
37
├── abis
40
38
│ ├── Storage.json
41
39
├── artifacts
42
-
│ ├── Storage.polkavm
40
+
│ ├── Storage.bin
43
41
├── contract-address.json
44
42
├── node_modules/
45
43
├── package.json
@@ -65,6 +63,17 @@ Next, run the following command to install the Ethers.js library:
65
63
npm install ethers
66
64
```
67
65
66
+
Add the Solidity compiler so you can generate standard EVM bytecode:
67
+
68
+
```bash
69
+
npm install --save-dev solc
70
+
```
71
+
72
+
This guide uses `solc` version `{{ dependencies.javascript_packages.solc.version }}`.
73
+
74
+
!!! tip
75
+
The sample scripts use ECMAScript modules. Add `"type": "module"` to your `package.json` (or rename the files to `.mjs`) so that `node` can run the `import` statements.
76
+
68
77
## Set Up the Ethers.js Provider
69
78
70
79
A [`Provider`](https://docs.ethers.org/v6/api/providers/#Provider){target=\_blank} is an abstraction of a connection to the Ethereum network, allowing you to query blockchain data and send transactions. It serves as a bridge between your application and the blockchain.
@@ -89,7 +98,7 @@ To interact with Polkadot Hub, you must set up an Ethers.js provider. This provi
89
98
To connect to the provider, execute:
90
99
91
100
```bash
92
-
node connectToProvider
101
+
node scripts/connectToProvider.js
93
102
```
94
103
95
104
With the provider set up, you can start querying the blockchain. For instance, to fetch the latest block number:
@@ -102,19 +111,7 @@ With the provider set up, you can start querying the blockchain. For instance, t
102
111
103
112
## Compile Contracts
104
113
105
-
--8<-- 'text/smart-contracts/code-size.md'
106
-
107
-
The `revive` compiler transforms Solidity smart contracts into [PolkaVM](/smart-contracts/overview/#native-smart-contracts){target=\_blank} bytecode for deployment on Polkadot Hub. Revive's Ethereum RPC interface allows you to use familiar tools like Ethers.js and MetaMask to interact with contracts.
108
-
109
-
### Install the Revive Library
110
-
111
-
The [`@parity/resolc`](https://www.npmjs.com/package/@parity/resolc){target=\_blank} library will compile your Solidity code for deployment on Polkadot Hub. Run the following command in your terminal to install the library:
112
-
113
-
```bash
114
-
npm install --save-dev @parity/resolc
115
-
```
116
-
117
-
This guide uses `@parity/resolc` version `{{ dependencies.javascript_packages.resolc.version }}`.
114
+
Polkadot Hub exposes an Ethereum JSON-RPC endpoint, so you can compile Solidity contracts to familiar EVM bytecode with the upstream [`solc`](https://www.npmjs.com/package/solc){target=\_blank} compiler. The resulting artifacts work with any EVM-compatible toolchain and can be deployed through Ethers.js.
118
115
119
116
### Sample Storage Smart Contract
120
117
@@ -140,10 +137,10 @@ The ABI (Application Binary Interface) is a JSON representation of your contract
140
137
Execute the script above by running:
141
138
142
139
```bash
143
-
node compile
140
+
node scripts/compile.js
144
141
```
145
142
146
-
After executing the script, the Solidity contract will be compiled into the required PolkaVM bytecode format. The ABI and bytecode will be saved into files with `.json` and `.polkavm` extensions, respectively. You can now proceed with deploying the contract to Polkadot Hub, as outlined in the next section.
143
+
After executing the script, the Solidity contract is compiled into standard EVM bytecode. The ABI and bytecode are saved into files with `.json` and `.bin` extensions, respectively. You can now proceed with deploying the contract to Polkadot Hub, as outlined in the next section.
147
144
148
145
## Deploy the Compiled Contract
149
146
@@ -154,7 +151,7 @@ You can create a `deploy.js` script in the root of your project to achieve this.
@@ -195,7 +192,7 @@ You can create a `deploy.js` script in the root of your project to achieve this.
195
192
To run the script, execute the following command:
196
193
197
194
```bash
198
-
node deploy
195
+
node scripts/deploy.js
199
196
```
200
197
201
198
After running this script, your contract will be deployed to Polkadot Hub, and its address will be saved in `contract-address.json` within your project directory. You can use this address for future contract interactions.
@@ -208,12 +205,12 @@ Once the contract is deployed, you can interact with it by calling its functions
Ensure you replace the `INSERT_MNEMONIC`, `INSERT_CONTRACT_ADDRESS`, and `INSERT_ADDRESS_TO_CHECK` placeholders with actual values. Also, ensure the contract ABI file (`Storage.json`) is correctly referenced.
208
+
Ensure you replace the `INSERT_MNEMONIC`, `INSERT_CONTRACT_ADDRESS`, and `INSERT_ADDRESS_TO_CHECK` placeholders with actual values. Also, ensure the contract ABI file (`Storage.json`) is correctly referenced. The script prints the balance for `ADDRESS_TO_CHECK` before it writes and doubles the stored value, so pick any account you want to monitor.
Polkadot Hub enforces Ethereum's 24 KiB contract-size limit (EIP-170) for EVM deployments. Keep contracts modular, share logic through libraries, and make use of compiler optimizations so your bytecode stays within the limit.
0 commit comments