Skip to main content

Create an ERC-20 Token in Artela

Intro​

Below is an example that uses @artela/aspect-tool to deploy erc20 contract to Artela TestNet.

Pre-requisites:

1. Setting up a new project​

Make sure you have a recent version of Node.js and npm installed, Start by installing the aspect-tool:

npm install -g @artela/aspect-tool

Project Initialization, to kick off your project with aspect-tool, follow these steps:

# Create a new directory and navigate into it
mkdir erc20-token && cd erc20-token

# Set up the npm project with aspect-tool
aspect-tool init

# Install the necessary dependencies
npm install

This will create a project directory with the following structure:

.
β”œβ”€β”€ README.md
β”œβ”€β”€ asconfig.json
β”œβ”€β”€ aspect <-- Your aspect code resides here
β”‚Β Β  └── index.ts <-- Entry functions for the aspect
β”œβ”€β”€ contracts <-- Place your smart contracts here
β”œβ”€β”€ package.json
β”œβ”€β”€ project.config.json
β”œβ”€β”€ scripts <-- Utility scripts, including deploying, binding and etc.
β”‚Β Β  β”œβ”€β”€ aspect-deploy.cjs
β”‚Β Β  β”œβ”€β”€ bind.cjs
β”‚Β Β  β”œβ”€β”€ contract-call.cjs
β”‚Β Β  β”œβ”€β”€ contract-deploy.cjs
β”‚Β Β  β”œβ”€β”€ contract-send.cjs
β”‚Β Β  └── create-account.cjs
β”œβ”€β”€ tests
└── tsconfig.json

2. Create a smart contract​

Install the required dependency @openzeppelin/contracts:

npm install @openzeppelin/contracts

Within the contracts directory, create your smart contract source files with a .sol extension.

2.1. create a ArtToken.sol file​

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract ArtToken is ERC20, Ownable {
constructor(uint256 initialSupply) ERC20("Artela", "ART") Ownable(msg.sender){
_mint(msg.sender, initialSupply);
}
}

2.2. create a Broker.sol file (Holding ArtToken).​

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract Broker is Ownable {
address private deployer;
address immutable _tokenIn;

constructor(address mytoken) Ownable(msg.sender) {
_tokenIn=mytoken;
deployer = msg.sender;
}

function isOwner(address user) external view returns (bool result) {
if (user == deployer) {
return true;
} else {
return false;
}
}

function startSchedule() public pure returns(bool) {
return true;
}

function transfer(address target,uint256 amount) public onlyOwner{
require(amount > 0, "You need to sell at least some tokens");
require(amount < IERC20(_tokenIn).balanceOf(address(this)) ,"Unable to afford sufficient amount");
IERC20(_tokenIn).transfer(target,amount);
}

function allowance(address aspectId) onlyOwner external view returns (uint256 valueWei) {
require(aspectId>address(0),"aspectId empty");
//todo check aspectId
return IERC20(_tokenIn).balanceOf(msg.sender);
}

}

3. Compile the Smart Contract​

This step relies on solcjs, first check if solc is installed correctly

 npm install -g solc

solcjs --version

Update contract:build command in Package.json. Use solcjs to build contract.

{
"contract:build": "solcjs --abi --bin --include-path ./node_modules/ --base-path . -o ./build/contract/ ./contracts/*.sol",
}

Compile your contract using:

npm run contract:build

βœ… Successful compilation will generate some *.abi files in the build/contract directory.

4. Deploy the Smart Contract​

4.1 Update project.config.json​

Update the project.config.json in the root directory with the appropriate network configuration:

{
"node": "https://betanet-rpc1.artela.network"
}
πŸ’‘

For more details regarding development environment setup, please refer to artela devnet

4.2 Create a blockchain account (optional).​

Execute the following command under the erc20-token folder to create an account if you haven't already done so:

// create ArtToken deployer
npm run account:create -- --skfile ./tokenPk.txt

// create Broker deployer
npm run account:create -- --skfile ./brokerPk.txt

  • --skfile : privateKey path for sender. (optional, default value: ./privateKey.txt).

If your account lacks test tokens, join Discord,and request some in testnet-faucet channel.

4.3 Deploy your contract​

Execute the following command within the erc20-token folder, using the provided script:


npm run contract:deploy -- --skfile ./tokenPk.txt \
--abi ./build/contract/contracts_ArtToken_sol_ArtToken.abi \
--bytecode ./build/contract/contracts_ArtToken_sol_ArtToken.bin \
--args 10000000


npm run contract:deploy -- --skfile ./brokerPk.txt \
--abi ./build/contract/contracts_Broker_sol_Broker.abi \
--bytecode ./build/contract/contracts_Broker_sol_Broker.bin \
--args {ArtToken_Address}

Upon successful deployment, the terminal will display the contract address.

5. Call the Contract​

5.1 Transfer artToken to broker address​

Execute the following command within the erc20-token folder:


npm run contract:send -- --skfile ./tokenPk.txt \
--contract {artToken-address} \
--abi ./build/contract/contracts_ArtToken_sol_ArtToken.abi \
--method transfer \
--args {broker-address} 100 \
--gas 200000


5.2 Check transaction​

Confirm the successful transfer on Artela TestNet blockchain explorer using transaction hash in output.

img.png