@@ -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
94469446compileContract(solidityFilePath, outputDir);
94479447```
@@ -9468,7 +9468,8 @@ You can create a `deploy.js` script in the root of your project to achieve this.
946894681. 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');
94729473const { ethers, JsonRpcProvider } = require('ethers');
94739474
94749475const codegenDir = join(__dirname);
@@ -9486,7 +9487,7 @@ const createProvider = (rpcUrl, chainId, chainName) => {
94869487 return provider;
94879488};
94889489 ```
9489-
9490+ r2
949094913. 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
95089509const 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
952395244. 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
956395645. 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');
95859586const { join } = require('path');
95869587const { ethers, JsonRpcProvider } = require('ethers');
95879588
@@ -9614,9 +9615,12 @@ const getAbi = (contractName) => {
96149615// Reads the compiled bytecode for a given contract
96159616const 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