Skip to content

Commit

Permalink
fix: snippets and comments to clarify
Browse files Browse the repository at this point in the history
  • Loading branch information
nhussein11 committed Jan 8, 2025
1 parent b81e3d5 commit fba283a
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ const { ethers, JsonRpcProvider } = require('ethers');

const codegenDir = join(__dirname);

// Creates an Ethereum provider with specified RPC URL and chain details
const createProvider = (rpcUrl, chainId, chainName) => {
const provider = new JsonRpcProvider(rpcUrl, {
chainId: chainId,
name: chainName,
});

return provider;
};

// Reads and parses the ABI file for a given contract
const getAbi = (contractName) => {
try {
return JSON.parse(
Expand All @@ -27,6 +28,7 @@ const getAbi = (contractName) => {
}
};

// Reads the compiled bytecode for a given contract
const getByteCode = (contractName) => {
try {
return `0x${readFileSync(join(codegenDir, `${contractName}.polkavm`)).toString('hex')}`;
Expand All @@ -43,38 +45,33 @@ const deployContract = async (contractName, mnemonic, providerConfig) => {
console.log(`Deploying ${contractName}...`);

try {
// Create a provider
// Step 1: Set up provider and wallet
const provider = createProvider(
providerConfig.rpc,
providerConfig.chainId,
providerConfig.name,
);

// Derive the wallet from the mnemonic
const walletMnemonic = ethers.Wallet.fromPhrase(mnemonic);
const wallet = walletMnemonic.connect(provider);

// Create the contract factory
// Step 2: Create and deploy the contract
const factory = new ethers.ContractFactory(
getAbi(contractName),
getByteCode(contractName),
wallet,
);

// Deploy the contract
const contract = await factory.deploy();
await contract.waitForDeployment();

// Step 3: Save deployment information
const address = await contract.getAddress();
console.log(`Contract ${contractName} deployed at: ${address}`);

// Save the deployed address
const addressesFile = join(codegenDir, 'contract-address.json');
const addresses = existsSync(addressesFile)
? JSON.parse(readFileSync(addressesFile, 'utf8'))
: {};
addresses[contractName] = address;

writeFileSync(addressesFile, JSON.stringify(addresses, null, 2), 'utf8');
} catch (error) {
console.error(`Failed to deploy contract ${contractName}:`, error);
Expand Down
12 changes: 6 additions & 6 deletions develop/toolkit/api-libraries/smart-contracts/ethers-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ npm install ethers
To interact with the Asset Hub, you'll need to set up an Ethers.js provider. This provider connects to a blockchain node, allowing you to query blockchain data and interact with smart contracts. Here's how to configure it:

```js
--8<-- 'code/develop/toolkit/api-libraries/smart-contracts/ether-js/connectToProvider.js'
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/connectToProvider.js'
```

!!! note
Expand All @@ -51,7 +51,7 @@ With the [`Provider`](https://docs.ethers.org/v6/api/providers/#Provider){target
??? code "Fetch Last Block code"

```js
--8<-- 'code/develop/toolkit/api-libraries/smart-contracts/ether-js/fetchLastBlock.js'
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/fetchLastBlock.js'
```

## Compile Contracts
Expand All @@ -70,13 +70,13 @@ Here's a sample Solidity contract (`Storage.sol`) to be compiled and deployed to
??? code "Storage.sol"

```solidity
--8<-- 'code/develop/toolkit/api-libraries/smart-contracts/ether-js/Storage.sol'
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/Storage.sol'
```

To compile this contract, use the following script:

```js
--8<-- 'code/develop/toolkit/api-libraries/smart-contracts/ether-js/compile.js'
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/compile.js'
```

Note that the script above is tailored to the `Storage.sol` contract. It can be adjusted for other contracts by changing the file name or modifying the ABI and bytecode paths accordingly.
Expand All @@ -90,7 +90,7 @@ To deploy the compiled contract to the Asset Hub, you will need a wallet with a
Here's the script to deploy the contract:

```js
--8<-- 'code/develop/toolkit/api-libraries/smart-contracts/ether-js/deploy.js'
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/deploy.js'
```

!!! note
Expand All @@ -103,7 +103,7 @@ After running the script above, the contract will be deployed to the Asset Hub n
Once the contract is deployed, you can interact with it by calling its functions. For example, to set a number, read it and then modify that number by its double, you can use the following script:

```js
--8<-- 'code/develop/toolkit/api-libraries/smart-contracts/ether-js/checkStorage.js'
--8<-- 'code/develop/smart-contracts/evm-toolkit/ethers-js/checkStorage.js'
```

Ensure you replace the `INSERT_MNEMONIC`, `INSERT_CONTRACT_ADDRESS` and `INSERT_ADDRESS_TO_CHECK` placeholders with actual values. Also, the contract ABI file (`Storage.json`) should be correctly referenced.
Expand Down

0 comments on commit fba283a

Please sign in to comment.