EVM Tokens (ERC-20) How to create Tokens on Ethereum, Polygon, Celo, BSC, Avalanche C Chain, Stratus, Hyperledger Besu
Straight to the code!
Instantiate Cryptum SDK first:
Copy const sdk = new CryptumSdk ({
environment : 'testnet' ,
apiKey : 'YOUR-API-KEY' ,
})
Create ERC20 token
sdk.token.create(opts)
The smart contract used in this deployment is already precompiled in Cryptum. For more details, you can see the source code here ./contracts/TokenERC20.sol
.
opts.wallet
(Wallet) (required ) - wallet creating the token.
opts.name
(string) (required ) - token name.
opts.symbol
(string) (required ) - token symbol.
opts.decimals
(number) (required ) - token decimal places.
opts.amount
(string) (required ) - token amount to be first minted. (this is in largest unit ether)
opts.protocol
(string) (required ) - EVMs only .
This function returns the transaction hash from the blockchain. This hash can be used later to retrieve the token address.
Copy const { hash } = await sdk . token .create ({
wallet ,
name : 'Token name' ,
symbol : 'TOK' ,
decimals : 18 ,
amount : '1000000' ,
protocol : 'ETHEREUM' ,
})
Transfer tokens
sdk.token.transfer(opts)
Transfer native and ERC20 tokens.
opts.wallet
(Wallet) (required ) - wallet transferring tokens.
opts.token
(string) (required ) - token name if it's native, like BNB, CELO, ETH etc, or the token address for ERC20 tokens.
opts.amount
(string) (required ) - token amount to be transferred. (this is in largest unit ether)
opts.destination
(string) (required ) - destination address.
opts.protocol
(string) (required ) - EVMs only .
This function returns the hash of this transferring transaction from the blockchain.
Copy // native token
const { hash } = await sdk . token .transfer ({
wallet ,
protocol : 'POLYGON' ,
token : 'MATIC' ,
destination : '0x31ec6686ee15...931b5e12cacb920e' ,
amount : '17.44' ,
})
// custom ERC20 token
const { hash } = await sdk . token .transfer ({
wallet ,
protocol : 'AVAXCCHAIN' ,
token : '0x1b5e12ca...1b5e12ca' ,
destination : '0x31ec6686ee15...07A931b5e12cacb920e' ,
amount : '9.129045' ,
})
Mint tokens
sdk.token.mint(opts)
Mint ERC20 tokens.
*Obs: This method will only work for the tokens created with the method sdk.token.create .
opts.wallet
(Wallet) (required ) - wallet minting tokens.
opts.token
(string) (required ) - token address.
opts.amount
(string) (required ) - token amount to be minted. (this is in largest unit ether considering the token decimal places)
opts.destination
(string) (required ) - destination address.
opts.protocol
(string) (required ) - EVMs only .
This function returns the hash of this minting transaction from the blockchain.
Copy const { hash } = await sdk . token .mint ({
wallet ,
protocol : 'CELO' ,
token : '0x1b5e12ca...1b5e12ca' ,
destination : '0x31ec6686ee1597...A931b5e12cacb920e' ,
amount : '40' ,
})
Burn tokens
sdk.token.burn(opts)
Burn ERC20 tokens.
*Obs: This method will only work for the tokens created with the method sdk.token.create .
opts.wallet
(Wallet) (required ) - wallet burning tokens.
opts.token
(string) (required ) - token address.
opts.amount
(string) (required ) - token amount to be burnt. (this is in largest unit ether considering the token decimal places)
opts.protocol
(string) (required ) - EVMs only .
This function returns the hash of this burning transaction from the blockchain.
Copy const { hash } = await sdk . token .burn ({
wallet ,
protocol : 'CELO' ,
token : '0x1b5e12ca...1b5e12ca' ,
amount : '8.23' ,
})
Approve tokens
sdk.token.approve(opts)
Invoke method "approve" from ERC20-compatible smart contracts.
*Obs: This method will only work for the tokens compatible with ERC-20 standard.
opts.wallet
(Wallet) (required ) - wallet signing transaction that owns the tokens.
opts.token
(string) (required ) - token address.
opts.amount
(string) (required ) - token amount to be approved. (this is in largest unit ether considering the token decimal places)
opts.spender
(string) (required ) - address allowed to withdraw tokens from this wallet.
opts.protocol
(string) (required ) - EVMs only .
This function returns the hash of this burning transaction from the blockchain.
Copy const { hash } = await sdk . token .approve ({
wallet ,
protocol : 'CELO' ,
token : '0x1b5e12ca...1b5e12ca' ,
amount : '8.82' ,
spender : '0x9377888...3342232'
})
Creating an ERC-20 token
Step 1
As with most actions performed using Cryptum, the first thing you need to do is create an instance of the SDK like this:
Copy const CryptumSdk = require ( 'cryptum-sdk' )
const sdk = new CryptumSdk ({
environment : 'testnet' , // 'testnet', 'mainnet'
apiKey : "YOUR-API-KEY-HERE" ,
})
Step 2
Now you must have or create a Wallet object using the SDK. This wallet will be used to send transactions to the blockchain in order to create your token, so make sure it has enough funds to perform all the desired interactions.
Copy const wallet = await sdk . wallet .generateWallet ({
protocol : 'ETHEREUM' ,
mnemonic : 'lorem ipsum dolor sit amet consectetur adipiscing elit ...' ,
derivation : { account : 0 , address : 0 }
})
Step 3
Now we must call the sdk.token.create
function on the SDK. The smart contract used in this deployment is in the contracts directory, so you must pass as arguments the token name, token symbol, decimals (integer number) and the amount of total supply.
Copy const { hash } = await sdk . token .create ({
wallet ,
name : 'Token name' ,
symbol : 'TOK' ,
decimals : 18 ,
amount : '1000000' ,
protocol : 'ETHEREUM'
})
The function returns the transaction hash of the smart contract deployment to the blockchain. To use this new token you need its address which you can retrive it from like this:
Copy const { contractAddress } = await sdk . transaction .getTransactionReceiptByHash ({
protocol : 'ETHEREUM' ,
hash : '0x000.....'
})
Minting tokens
If you’re the owner of the token’s contract, you can mint extra tokens like this:
Copy const { hash } = await sdk . token .mint ({
wallet ,
token : '0x19......86B' , // token address
destination : '0x0c3.....A342' , // destination address
amount : '40' ,
protocol : 'ETHEREUM'
})
Burning tokens
Unlike minting, anybody can burn the tokens they own.
Copy const { hash } = await sdk . token .burn ({
wallet ,
token : '0x19......86B' , // token address
amount : '40' ,
protocol : 'ETHEREUM'
})
Transferring tokens
To transfer tokens between accounts, follow this guideline:
Copy const { hash } = await sdk . token .transfer ({
wallet ,
token : '0x19......86B' , // token address
destination : '0x0c3.....A342' , // destination address
amount : '40' ,
protocol : 'ETHEREUM'
})
Besides ERC20-based tokens, you also can transfer native tokens by passing the native token symbol instead of the token address. Depending on the protocol you're using there are the supported native tokens that you can transfer:
"ETH
" for protocol ETHEREUM;
"CELO", "cUSD", "cEUR"
for protocol CELO;
"MATIC"
for protocol POLYGON;
"AVAX"
for protocol AVAXCCHAIN;
"CWN"
for protocol STRATUS;
Copy const { hash } = await sdk . token .transfer ({
wallet ,
token : 'ETH' , // token address
destination : '0x0c3.....A342' , // destination address
amount : '0.037' ,
protocol : 'ETHEREUM'
})
Last updated 7 months ago