Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3a7dfa5
Update navigation and documentation for basic contract deployment
nhussein11 Oct 20, 2025
8ca8842
fix: llms
nhussein11 Oct 20, 2025
0eef625
Fix navigation entry for deploying a basic contract (PVM) in .nav.yml
nhussein11 Oct 21, 2025
c20f681
Update deployment guides for basic EVM and PVM contracts, specifying …
nhussein11 Oct 21, 2025
16aef31
Update smart-contracts/cookbook/smart-contracts/deploy-basic-pvm.md
nhussein11 Oct 21, 2025
37d8abe
Update smart-contracts/cookbook/smart-contracts/deploy-basic-pvm.md
nhussein11 Oct 21, 2025
b3bc379
Fix: adding reference to polkadot hub
nhussein11 Oct 21, 2025
999e78c
fix: llms
nhussein11 Oct 21, 2025
ed1cdf2
fix evm
0xlukem Oct 23, 2025
0ee669e
fix pvm
0xlukem Oct 23, 2025
3824734
fix
0xlukem Oct 23, 2025
6d7b21e
fresh llms
0xlukem Oct 23, 2025
766c77f
Update bytecode writing method in EVM deployment guide and remove TOD…
nhussein11 Oct 23, 2025
3b6ff77
hiding pvm content
0xlukem Oct 23, 2025
3aeef4c
add tabbed content
0xlukem Oct 23, 2025
cf2a44a
llms
0xlukem Oct 23, 2025
1feeaf6
Merge branch 'staging/product-ia' into nhussein11/add-deploy-basic-pvm
0xlukem Oct 23, 2025
fc18dc3
merge issue
0xlukem Oct 23, 2025
b0015d8
llms
0xlukem Oct 23, 2025
7c155d6
break deploy-basic-evm in small sections
0xlukem Oct 24, 2025
3a50c0f
fix naming
0xlukem Oct 24, 2025
31646b4
feedback
0xlukem Oct 29, 2025
34abe1b
fix
0xlukem Oct 30, 2025
6494346
Merge branch 'staging/product-ia' into nhussein11/add-deploy-basic-pvm
0xlukem Oct 30, 2025
ee94462
feedback
0xlukem Oct 30, 2025
c97c843
Apply suggestions from code review
0xlukem Oct 31, 2025
a468128
feedback
0xlukem Oct 31, 2025
98537a6
Merge branch 'staging/product-ia' into nhussein11/add-deploy-basic-pvm
0xlukem Oct 31, 2025
6eb8030
Apply suggestions from code review
eshaben Nov 4, 2025
f4698b9
Merge branch 'staging/product-ia' into nhussein11/add-deploy-basic-pvm
eshaben Nov 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,892 changes: 3,516 additions & 376 deletions .ai/categories/smart-contracts.md

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
---
title: JavaScript with Ethers.js
description: Learn how to deploy a basic smart contract to Polkadot Hub using Ethers.js, best for lightweight, programmatic deployments and application integration.
categories: Smart Contracts
url: https://docs.polkadot.com/smart-contracts/cookbook/smart-contracts/deploy-basic-contract/deploy-basic-ethers-js/
---

[Ethers.js](https://docs.ethers.org/v6/){target=\_blank} provides a lightweight approach for deploying contracts using pure JavaScript. This method is ideal for developers who want programmatic control over the deployment process or need to integrate contract deployment into existing applications.

**Prerequisites:**

- Basic understanding of Solidity programming.
- [Node.js](https://nodejs.org/en/download){target=\_blank} v22.13.1 or later.
- Test tokens for gas fees (available from the [Polkadot faucet](https://faucet.polkadot.io/){target=\_blank}).
- A wallet with a private key for signing transactions.

### Setup

First, initialize your project and install dependencies:

```bash
mkdir ethers-deployment
cd ethers-deployment
npm init -y
npm install ethers@6.15.0 solc@0.8.30
```

### Create and Compile Your Contract

Create a simple storage contract in `contracts/Storage.sol`:

```solidity title="contracts/Storage.sol"
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract Storage {
uint256 private storedNumber;

function store(uint256 num) public {
storedNumber = num;
}

function retrieve() public view returns (uint256) {
return storedNumber;
}
}
```

Create a compilation script `compile.js`:

```javascript title="compile.js"
const solc = require('solc');
const { readFileSync, writeFileSync } = require('fs');
const { basename, join } = require('path');

const compileContract = async (solidityFilePath, outputDir) => {
try {
// Read the Solidity file
const source = readFileSync(solidityFilePath, 'utf8');

// Construct the input object for the compiler
const input = {
language: 'Solidity',
sources: {
[basename(solidityFilePath)]: { content: source },
},
settings: {
outputSelection: {
'*': {
'*': ['*'],
},
},
},
};

console.log(`Compiling contract: ${basename(solidityFilePath)}...`);

// Compile the contract
const output = JSON.parse(solc.compile(JSON.stringify(input)));

if (output.errors) {
output.errors.forEach(error => {
console.error('Compilation error:', error.message);
});
return;
}

for (const contracts of Object.values(output.contracts)) {
for (const [name, contract] of Object.entries(contracts)) {
console.log(`Compiled contract: ${name}`);

// Write the ABI
const abiPath = join(outputDir, `${name}.json`);
writeFileSync(abiPath, JSON.stringify(contract.abi, null, 2));
console.log(`ABI saved to ${abiPath}`);

// Write the bytecode
const bytecodePath = join(outputDir, `${name}.bin`);
writeFileSync(bytecodePath,
Buffer.from(
contract.evm.bytecode.object,
'hex'
));
console.log(`Bytecode saved to ${bytecodePath}`);
}
}
} catch (error) {
console.error('Error compiling contracts:', error);
}
};

const solidityFilePath = join(__dirname, 'contracts/Storage.sol');
const outputDir = join(__dirname, 'contracts');

compileContract(solidityFilePath, outputDir);
```

Run the compilation:

```bash
node compile.js
```

### Deploy the Contract

Create a deployment script `deploy.js`:

```javascript title="deploy.js"
const { writeFileSync, existsSync, readFileSync } = require('fs');
const { join } = require('path');
const { ethers, JsonRpcProvider } = require('ethers');

const codegenDir = join(__dirname);

// Creates a 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(
readFileSync(join(codegenDir, 'contracts', `${contractName}.json`), 'utf8'),
);
} catch (error) {
console.error(
`Could not find ABI for contract ${contractName}:`,
error.message,
);
throw error;
}
};

// Reads the compiled bytecode for a given contract
const getByteCode = (contractName) => {
try {
const bytecodePath = join(
codegenDir,
'contracts',
`${contractName}.bin`,
);
return `0x${readFileSync(bytecodePath).toString('hex')}`;
} catch (error) {
console.error(
`Could not find bytecode for contract ${contractName}:`,
error.message,
);
throw error;
}
};

const deployContract = async (contractName, mnemonic, providerConfig) => {
console.log(`Deploying ${contractName}...`);

try {
// Step 1: Set up provider and wallet
const provider = createProvider(
providerConfig.rpc,
providerConfig.chainId,
providerConfig.name,
);
const walletMnemonic = ethers.Wallet.fromPhrase(mnemonic);
const wallet = walletMnemonic.connect(provider);

// Step 2: Create and deploy the contract
const factory = new ethers.ContractFactory(
getAbi(contractName),
getByteCode(contractName),
wallet,
);
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}`);

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);
}
};

const providerConfig = {
rpc: 'https://testnet-passet-hub-eth-rpc.polkadot.io',
chainId: 420420422,
name: 'polkadot-hub-testnet',
};

const mnemonic = 'INSERT_MNEMONIC';

deployContract('Storage', mnemonic, providerConfig);
```

Replace the `INSERT_MNEMONIC` placeholder with your actual mnemonic.

Execute the deployment:

```bash
node deploy.js
```

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.

### Next Steps

- Deploy an ERC-20 token on Polkadot Hub, either using the [Deploy an ERC-20](/smart-contracts/cookbook/smart-contracts/deploy-erc20) guide or the [Deploy an ERC-20 to Polkadot Hub](/smart-contracts/cookbook/smart-contracts/deploy-erc20) guide.
- Deploy an NFT on Polkadot Hub, either using the [Deploy an NFT](/smart-contracts/cookbook/smart-contracts/deploy-nft) guide or the [Deploy an NFT to Polkadot Hub](/smart-contracts/cookbook/smart-contracts/deploy-nft) guide.
- Check out in details each [development environment](/smart-contracts/dev-environments/).
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: Foundry
description: Learn how to deploy a basic smart contract to Polkadot Hub using Foundry, excellent for developers who prefer fast, command-line driven development.
categories: Smart Contracts
url: https://docs.polkadot.com/smart-contracts/cookbook/smart-contracts/deploy-basic-contract/deploy-basic-foundry/
---

[Foundry](https://getfoundry.sh/){target=\_blank} offers a fast, modular toolkit written in Rust. It's perfect for developers who prefer command-line interfaces and need high-performance compilation and deployment.

**Prerequisites:**

- Basic understanding of Solidity programming.
- Test tokens for gas fees (available from the [Polkadot faucet](https://faucet.polkadot.io/){target=\_blank}).
- A wallet with a private key for signing transactions.

### Setup

Install Foundry:

```bash
curl -L https://foundry.paradigm.xyz | bash
foundryup
```

Initialize your project:

```bash
forge init foundry-deployment
cd foundry-deployment
```

### Configure Foundry

Edit `foundry.toml`:

```toml
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

[rpc_endpoints]
polkadot_hub_testnet = "https://testnet-passet-hub-eth-rpc.polkadot.io"
```

### Create Your Contract

Replace the default contract in `src/Storage.sol`:

```solidity title="src/Storage.sol"
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract Storage {
uint256 private storedNumber;

function store(uint256 num) public {
storedNumber = num;
}

function retrieve() public view returns (uint256) {
return storedNumber;
}
}
```

### Compile

```bash
forge build
```

Verify the compilation by inspecting the bytecode:

```bash
forge inspect Storage bytecode
```

### Deploy

Deploy to Polkadot Hub TestNet:

```bash
forge create Storage \
--rpc-url polkadot_hub_testnet \
--private-key YOUR_PRIVATE_KEY \
--broadcast
```

### Next Steps

- Deploy an ERC-20 token on Polkadot Hub, either using the [Deploy an ERC-20](/smart-contracts/cookbook/smart-contracts/deploy-erc20) guide or the [Deploy an ERC-20 to Polkadot Hub](/smart-contracts/cookbook/smart-contracts/deploy-erc20) guide.
- Deploy an NFT on Polkadot Hub, either using the [Deploy an NFT](/smart-contracts/cookbook/smart-contracts/deploy-nft) guide or the [Deploy an NFT to Polkadot Hub](/smart-contracts/cookbook/smart-contracts/deploy-nft) guide.
- Check out in details each [development environment](/smart-contracts/dev-environments/).
Loading
Loading