Skip to content

Commit 7cdac8f

Browse files
authored
[FIX] - ethersjs snippets (#749)
* fix: ethersjs snippets and rendering * fix: deployjs script
1 parent 5bed6da commit 7cdac8f

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

.snippets/code/develop/smart-contracts/evm-toolkit/ethers-js/compile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const compileContract = async (solidityFilePath, outputDir) => {
3030
const bytecodePath = join(outputDir, `${name}.polkavm`);
3131
writeFileSync(
3232
bytecodePath,
33-
Buffer.from(contract.evm.bytecode.object, 'hex')
33+
Buffer.from(contract.evm.bytecode.object, 'hex'),
3434
);
3535
console.log(`Bytecode saved to ${bytecodePath}`);
3636
}
@@ -40,7 +40,7 @@ const compileContract = async (solidityFilePath, outputDir) => {
4040
}
4141
};
4242

43-
const solidityFilePath = './Storage.sol';
44-
const outputDir = '.';
43+
const solidityFilePath = join(__dirname, '../contracts/Storage.sol');
44+
const outputDir = join(__dirname, '../contracts');
4545

4646
compileContract(solidityFilePath, outputDir);

.snippets/code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Deploy an EVM-compatible smart contract using ethers.js
12
const { writeFileSync, existsSync, readFileSync } = require('fs');
23
const { join } = require('path');
34
const { ethers, JsonRpcProvider } = require('ethers');
@@ -31,9 +32,12 @@ const getAbi = (contractName) => {
3132
// Reads the compiled bytecode for a given contract
3233
const getByteCode = (contractName) => {
3334
try {
34-
return `0x${readFileSync(
35-
join(codegenDir, `${contractName}.polkavm`),
36-
).toString('hex')}`;
35+
const bytecodePath = join(
36+
codegenDir,
37+
'../contracts',
38+
`${contractName}.polkavm`,
39+
);
40+
return `0x${readFileSync(bytecodePath).toString('hex')}`;
3741
} catch (error) {
3842
console.error(
3943
`Could not find bytecode for contract ${contractName}:`,

develop/smart-contracts/libraries/ethers-js.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,31 +153,31 @@ You can create a `deploy.js` script in the root of your project to achieve this.
153153
1. Set up the required imports and utilities:
154154

155155
```js title="scripts/deploy.js"
156-
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js:1:5'
156+
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js:1:6'
157157
```
158158

159159
2. Create a provider to connect to Polkadot Hub:
160160

161161
```js title="scripts/deploy.js"
162-
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js:6:14'
162+
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js:7:15'
163163
```
164-
164+
r2
165165
3. Set up functions to read contract artifacts:
166166

167167
```js title="scripts/deploy.js"
168-
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js:16:44'
168+
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js:17:45'
169169
```
170170

171171
4. Create the main deployment function:
172172

173173
```js title="scripts/deploy.js"
174-
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js:46:81'
174+
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js:47:82'
175175
```
176176

177177
5. Configure and execute the deployment:
178178

179179
```js title="scripts/deploy.js"
180-
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js:83:92'
180+
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js:84:92'
181181
```
182182

183183
!!! note
@@ -188,7 +188,7 @@ You can create a `deploy.js` script in the root of your project to achieve this.
188188
??? code "View complete script"
189189
190190
```js title="scripts/deploy.js"
191-
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js'
191+
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js:1'
192192
```
193193
194194
To run the script, execute the following command:

llms.txt

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9430,7 +9430,7 @@ const compileContract = async (solidityFilePath, outputDir) => {
94309430
const bytecodePath = join(outputDir, `${name}.polkavm`);
94319431
writeFileSync(
94329432
bytecodePath,
9433-
Buffer.from(contract.evm.bytecode.object, 'hex')
9433+
Buffer.from(contract.evm.bytecode.object, 'hex'),
94349434
);
94359435
console.log(`Bytecode saved to ${bytecodePath}`);
94369436
}
@@ -9440,8 +9440,8 @@ const compileContract = async (solidityFilePath, outputDir) => {
94409440
}
94419441
};
94429442

9443-
const solidityFilePath = './Storage.sol';
9444-
const outputDir = '.';
9443+
const solidityFilePath = join(__dirname, '../contracts/Storage.sol');
9444+
const outputDir = join(__dirname, '../contracts');
94459445

94469446
compileContract(solidityFilePath, outputDir);
94479447
```
@@ -9468,7 +9468,8 @@ You can create a `deploy.js` script in the root of your project to achieve this.
94689468
1. Set up the required imports and utilities:
94699469

94709470
```js title="scripts/deploy.js"
9471-
const { join } = require('path');
9471+
const { writeFileSync, existsSync, readFileSync } = require('fs');
9472+
const { join } = require('path');
94729473
const { ethers, JsonRpcProvider } = require('ethers');
94739474

94749475
const codegenDir = join(__dirname);
@@ -9486,7 +9487,7 @@ const createProvider = (rpcUrl, chainId, chainName) => {
94869487
return provider;
94879488
};
94889489
```
9489-
9490+
r2
94909491
3. Set up functions to read contract artifacts:
94919492

94929493
```js title="scripts/deploy.js"
@@ -9507,23 +9508,26 @@ const createProvider = (rpcUrl, chainId, chainName) => {
95079508
// Reads the compiled bytecode for a given contract
95089509
const getByteCode = (contractName) => {
95099510
try {
9510-
return `0x${readFileSync(
9511-
join(codegenDir, `${contractName}.polkavm`),
9512-
).toString('hex')}`;
9511+
const bytecodePath = join(
9512+
codegenDir,
9513+
'../contracts',
9514+
`${contractName}.polkavm`,
9515+
);
9516+
return `0x${readFileSync(bytecodePath).toString('hex')}`;
95139517
} catch (error) {
95149518
console.error(
95159519
`Could not find bytecode for contract ${contractName}:`,
95169520
error.message,
95179521
);
9518-
throw error;
9519-
}
9520-
};
95219522
```
95229523

95239524
4. Create the main deployment function:
95249525

95259526
```js title="scripts/deploy.js"
9526-
console.log(`Deploying ${contractName}...`);
9527+
};
9528+
9529+
const deployContract = async (contractName, mnemonic, providerConfig) => {
9530+
console.log(`Deploying ${contractName}...`);
95279531

95289532
try {
95299533
// Step 1: Set up provider and wallet
@@ -9555,22 +9559,18 @@ const getByteCode = (contractName) => {
95559559
addresses[contractName] = address;
95569560
writeFileSync(addressesFile, JSON.stringify(addresses, null, 2), 'utf8');
95579561
} catch (error) {
9558-
console.error(`Failed to deploy contract ${contractName}:`, error);
9559-
}
9560-
};
95619562
```
95629563

95639564
5. Configure and execute the deployment:
95649565

95659566
```js title="scripts/deploy.js"
9566-
rpc: 'https://testnet-passet-hub-eth-rpc.polkadot.io',
9567+
};
9568+
9569+
const providerConfig = {
9570+
rpc: 'https://testnet-passet-hub-eth-rpc.polkadot.io',
95679571
chainId: 420420422,
95689572
name: 'polkadot-hub-testnet',
95699573
};
9570-
9571-
const mnemonic = 'INSERT_MNEMONIC';
9572-
9573-
deployContract('Storage', mnemonic, providerConfig);
95749574
```
95759575

95769576
!!! note
@@ -9581,7 +9581,8 @@ deployContract('Storage', mnemonic, providerConfig);
95819581
??? code "View complete script"
95829582

95839583
```js title="scripts/deploy.js"
9584-
const { writeFileSync, existsSync, readFileSync } = require('fs');
9584+
// Deploy an EVM-compatible smart contract using ethers.js
9585+
const { writeFileSync, existsSync, readFileSync } = require('fs');
95859586
const { join } = require('path');
95869587
const { ethers, JsonRpcProvider } = require('ethers');
95879588

@@ -9614,9 +9615,12 @@ const getAbi = (contractName) => {
96149615
// Reads the compiled bytecode for a given contract
96159616
const getByteCode = (contractName) => {
96169617
try {
9617-
return `0x${readFileSync(
9618-
join(codegenDir, `${contractName}.polkavm`),
9619-
).toString('hex')}`;
9618+
const bytecodePath = join(
9619+
codegenDir,
9620+
'../contracts',
9621+
`${contractName}.polkavm`,
9622+
);
9623+
return `0x${readFileSync(bytecodePath).toString('hex')}`;
96209624
} catch (error) {
96219625
console.error(
96229626
`Could not find bytecode for contract ${contractName}:`,

0 commit comments

Comments
 (0)