-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "Deploy an ERC20" with remix tutorial (#344)
* fix: adding tutorial to deploy erc20 and images * fix: moving smart contract code to snippet * Apply suggestions from code review Co-authored-by: 0xLucca <[email protected]> * fix: adding standard erc20 functions * fix: feedback * Update tutorials/smart-contracts/deploy-erc20.md Co-authored-by: Erin Shaben <[email protected]> * Update tutorials/smart-contracts/deploy-erc20.md Co-authored-by: Erin Shaben <[email protected]> * fix: erc-20 * fix: erc-20 instance * Update tutorials/smart-contracts/deploy-erc20.md Co-authored-by: Erin Shaben <[email protected]> * fix: sentence case applied * Update tutorials/smart-contracts/deploy-erc20.md Co-authored-by: Erin Shaben <[email protected]> * fix: bold list items * Update tutorials/smart-contracts/deploy-erc20.md Co-authored-by: Erin Shaben <[email protected]> * Update tutorials/smart-contracts/deploy-erc20.md Co-authored-by: Erin Shaben <[email protected]> * fix: action titles * fix: adding variables --------- Co-authored-by: 0xLucca <[email protected]> Co-authored-by: Erin Shaben <[email protected]>
- Loading branch information
1 parent
f841f45
commit afbc7e1
Showing
16 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
.snippets/code/tutorials/smart-contracts/deploy-erc20/MyToken.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Compatible with OpenZeppelin Contracts ^5.0.0 | ||
pragma solidity ^0.8.22; | ||
|
||
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; | ||
|
||
contract MyToken is ERC20, Ownable { | ||
constructor(address initialOwner) | ||
ERC20("MyToken", "MTK") | ||
Ownable(initialOwner) | ||
{} | ||
|
||
function mint(address to, uint256 amount) public onlyOwner { | ||
_mint(to, amount); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
title: Smart Contracts | ||
nav: | ||
- index.md | ||
- 'Deploy an NFT': deploy-nft.md | ||
- 'Deploy an ERC-20': deploy-erc20.md | ||
- 'Deploy an NFT': deploy-nft.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
--- | ||
title: Deploy an ERC-20 to Asset Hub | ||
description: Deploy an ERC-20 token on Asset Hub using PolkaVM. This guide covers contract creation, compilation, deployment, and interaction via Polkadot Remix IDE. | ||
--- | ||
|
||
# Deploy ERC-20 to Asset Hub | ||
|
||
## Introduction | ||
|
||
[ERC-20](https://eips.ethereum.org/EIPS/eip-20){target=\_blank} tokens are fungible tokens commonly used for creating cryptocurrencies, governance tokens, and staking mechanisms. Asset Hub enables easy token deployment with EVM-compatible smart contracts via PolkaVM. | ||
|
||
This tutorial covers deploying an ERC-20 contract on the Westend TestNet using [Polkadot Remix IDE](https://remix.polkadot.io){target=\_blank}, a web-based development tool. [OpenZeppelin's ERC-20 contracts]({{ dependencies.open_zeppelin_contracts.repository_url}}/tree/{{ dependencies.open_zeppelin_contracts.version}}/contracts/token/ERC20){target=\_blank} are used for security and compliance. | ||
|
||
## Prerequisites | ||
|
||
Before starting, make sure you have: | ||
|
||
- [MetaMask](https://metamask.io/){target=\_blank} installed and connected to [Westend Asset Hub](https://chainlist.org/chain/420420421){target=\_blank} | ||
- A funded account with some WND tokens (you can get them from the [Westend Faucet](https://faucet.polkadot.io/westend?parachain=1000){target=\_blank}) | ||
- Basic understanding of Solidity and fungible tokens | ||
|
||
## Create the ERC-20 Contract | ||
|
||
To create the ERC-20 contract, you can follow the steps below: | ||
|
||
1. Navigate to the [Polkadot Remix IDE](https://remix.polkadot.io){target=\_blank} | ||
2. Click in the **Create new file** button under the **contracts** folder, and name your contract as `MyToken.sol` | ||
|
||
 | ||
|
||
3. Now, paste the following ERC-20 contract code into the editor | ||
|
||
```solidity title="MyToken.sol" | ||
--8<-- 'code/tutorials/smart-contracts/deploy-erc20/MyToken.sol' | ||
``` | ||
The key components of the code above are: | ||
- Contract imports | ||
- [**`ERC20.sol`**]({{ dependencies.open_zeppelin_contracts.repository_url}}/tree/{{ dependencies.open_zeppelin_contracts.version}}/contracts/token/ERC20/ERC20.sol){target=\_blank} - the base contract for fungible tokens, implementing core functionality like transfers, approvals, and balance tracking | ||
- [**`Ownable.sol`**]({{ dependencies.open_zeppelin_contracts.repository_url}}/tree/{{ dependencies.open_zeppelin_contracts.version}}/contracts/access/Ownable.sol){target=\_blank} - provides basic authorization control, ensuring only the contract owner can mint new tokens | ||
- Constructor parameters | ||
- **`initialOwner`** - sets the address that will have administrative rights over the contract | ||
- **`"MyToken"`** - the full name of your token | ||
- **`"MTK"`** - the symbol representing your token in wallets and exchanges | ||
- Key functions | ||
- **`mint(address to, uint256 amount)`** - allows the contract owner to create new tokens for any address. The amount should include 18 decimals (e.g., 1 token = 1000000000000000000) | ||
- Inherited [Standard ERC-20](https://ethereum.org/en/developers/docs/standards/tokens/erc-20/){target=\_blank} functions: | ||
- **`transfer(address recipient, uint256 amount)`** - sends a specified amount of tokens to another address | ||
- **`approve(address spender, uint256 amount)`** - grants permission for another address to spend a specific number of tokens on behalf of the token owner | ||
- **`transferFrom(address sender, address recipient, uint256 amount)`** - transfers tokens from one address to another, if previously approved | ||
- **`balanceOf(address account)`** - returns the token balance of a specific address | ||
- **`allowance(address owner, address spender)`** - checks how many tokens an address is allowed to spend on behalf of another address | ||
!!! tip | ||
Use the [OpenZeppelin Contracts Wizard](https://wizard.openzeppelin.com/){target=\_blank} to quickly generate customized smart contracts. Simply configure your contract, copy the generated code, and paste it into Polkadot Remix IDE for deployment. | ||
## Compile the Contract | ||
The compilation transforms your Solidity source code into bytecode that can be deployed on the blockchain. During this process, the compiler checks your contract for syntax errors, ensures type safety, and generates the machine-readable instructions needed for blockchain execution. To compile your contract, follow the instructions below: | ||
1. Select the **Solidity Compiler** plugin from the left panel | ||
 | ||
2. Click the **Compile MyToken.sol** button | ||
 | ||
3. If the compilation succeeded, you'll see a green checkmark indicating success in the **Solidity Compiler** icon | ||
 | ||
## Deploy the Contract | ||
Deployment is the process of publishing your compiled smart contract to the blockchain, making it permanently available for interaction. During deployment, you'll create a new instance of your contract on the blockchain, which involves: | ||
1. Select the **Deploy & Run Transactions** plugin from the left panel | ||
 | ||
2. Configure the deployment settings | ||
1. From the **ENVIRONMENT** dropdown, select **Westend Testnet - MetaMask** | ||
2. From the **ACCOUNT** dropdown, select the account you want to use for the deploy | ||
 | ||
3. Configure the contract parameters | ||
1. Enter the address that will own the deployed token contract | ||
2. Click the **Deploy** button to initiate the deployment | ||
 | ||
4. MetaMask will pop up - review the transaction details. Click **Confirm** to deploy your contract | ||
{: .browser-extension} | ||
If the deployment process succeeded, you will see the transaction details in the terminal, including the contract address and deployment transaction hash: | ||
 | ||
## Interact with Your ERC-20 Contract | ||
Once deployed, you can interact with your contract through Remix: | ||
1. Find your contract under **Deployed/Unpinned Contracts**, and click it to expand the available methods | ||
 | ||
2. To mint new tokens: | ||
1. Click in the contract to expand its associated methods | ||
2. Expand the **mint** function | ||
3. Enter: | ||
- The recipient address | ||
- The amount (remember to add 18 zeros for 1 whole token) | ||
4. Click **Transact** | ||
 | ||
3. Confirm the transaction in MetaMask | ||
{: .browser-extension} | ||
If the transaction succeeds, you will see the following output in the terminal: | ||
 | ||
Other common functions you can use: | ||
- **`balanceOf(address)`** - check token balance of any address | ||
- **`transfer(address to, uint256 amount)`** - send tokens to another address | ||
- **`approve(address spender, uint256 amount)`** - allow another address to spend your tokens | ||
Feel free to explore and interact with the contract's other functions using the same approach - selecting the method, providing any required parameters, and confirming the transaction through MetaMask when needed. |