Issue FT (ERC20)
What is FT?
Fungible Token or FT, which is a token that is generally guaranteed to have the same value if they are the same FT. I think it's easier to understand if you think of it as something similar to digital money, such as digital money in a bank account.
Here, let's issue the ERC20 standard FT "My Token" and learn the mechanism of exchange. Click here for the ERC20 standard
Prerequisites
ERC20 SDK usage
Install ERC20 SDK
Prepare empty folder
mkdir test-erc20
cd test-erc20
Quick initialize npm project
npm init -y
Setting npm server for G.U.SDK
echo > .npmrc
echo @gusdk:registry=https://gusdk.gu.net/ >> .npmrc
At this point, the contents of the directory are as follows.
DIRECTORY ROOT
├── .npmrc
└── package.json
Install ERC20 SDK and ethers
npm install @gusdk/erc20-basic ethers@v5
Deploy a new token using ERC20 SDK
Create src
folder and create deploy.js
file
mkdir src && cd src && echo > deploy.js
Modify content deploy.js
following:
const erc20BasicLib = require("@gusdk/erc20-basic")
const ethers = require('ethers')
async function deploy() {
const provider = new ethers.providers.JsonRpcProvider({ url: "https://sandbox1.japanopenchain.org:8545/" })
const signer = new ethers.Wallet(YOUR_PRIVATE_KEY, provider);
const factory = new ERC20Basic__factory(signer)
const erc20 = await factory.deploy(
YOUR_TOKEN_NAME, // Eg. "TEST"
YOUR_TOKEN_SYMBOL, // Eg. "TEST"
YOUR_TOKEN_SUPPLY, // Eg. "100"
YOUR_TOKEN_DECIMALS // Eg. "18"
);
return erc20.address
}
deploy().then(address => console.log(address));
Execute it following command
node deploy.js
NOTE:: We can using token above or exist token address with ERC20 SDK
Get token name
Create name.js
in src
folder
echo > name.js
Modify content name.js
following:
const erc20BasicLib = require("@gusdk/erc20-basic")
const ethers = require('ethers')
async function getTokenName(tokenAddress) {
endpointUrl = 'https://sandbox1.japanopenchain.org:8545/'
const provider = new ethers.providers.JsonRpcProvider({url:endpointUrl})
const coin = erc20BasicLib.ERC20Basic__factory.connect(tokenAddress, provider);
return await coin.name()
}
const tokenAddress = '0x9C19fB1e8c0773eE5f6B6FE95ea711279F524534'
getTokenName(tokenAddress).then(value => console.log(value));
Execute it following command
node name.js
Get token balance of my account
Create balance.js
in src
folder
echo > balance.js
Modify content balance.js
following:
const erc20BasicLib = require("@gusdk/erc20-basic")
const ethers = require('ethers')
async function getMyTokenBalance(tokenAddress, walletAddress) {
endpointUrl = 'https://sandbox1.japanopenchain.org:8545/'
const provider = new ethers.providers.JsonRpcProvider({url:endpointUrl})
const coin = erc20BasicLib.ERC20Basic__factory.connect(tokenAddress, provider);
return await coin.balanceOf(walletAddress)
}
const tokenAddress = '0x9C19fB1e8c0773eE5f6B6FE95ea711279F524534'
const yourWalletAddress = '0x48a1ebb823ebA4940395ddCA05b6705b3EC8755b'
getMyTokenBalance(tokenAddress, yourWalletAddress).then(value => console.log(value));
Execute it following command
node balance.js
Transfer token to another account
Create transfer.js
in src
folder
echo > transfer.js
Modify content transfer.js
following:
const { ERC20Basic__factory } = require("@gusdk/erc20-basic")
const ethers = require('ethers')
async function transfer(tokenAddress, to, amount) {
endpointUrl = 'https://sandbox1.japanopenchain.org:8545/'
const provider = new ethers.providers.JsonRpcProvider({url:endpointUrl})
const signer = new ethers.Wallet(YOUR_PRIVATE_KEY, provider);
const coin = ERC20Basic__factory.connect(tokenAddress, signer);
return await coin.transfer(to, amount)
}
const tokenAddress = '0x9C19fB1e8c0773eE5f6B6FE95ea711279F524534'
const toAddress = '0x48a1ebb823ebA4940395ddCA05b6705b3EC8755b'
const amount = ethers.BigNumber.from(10)
transfer(tokenAddress, toAddress, amount).then(value => console.log(value));
Execute it following command
node transfer.js
Please see the reference for other features.