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 thebuild/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.