Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Celo token native transfer and ERC20 transfer using contractkit
// Import ContractKit
const ContractKit = require('@celo/contractkit')
// Create a new instance of ContractKit, connecting to a local Celo node
//const contractKit = ContractKit.newKit('http://localhost:8545')
const contractKit = ContractKit.newKit('')
// Specify an arbitrary recipient of the transaction
const recipient = '0xB9727f7f1e1f4a5229a49E260fBBBD410d10f2Ff'
const privateKey = '3a26ebc37944c305670a21ea6a5d16c1084db2f18bc6635ba95f487e5c59868f'
// Specify an amount to trasnfer, and convert it to the appropriate units
const transferAmount = '0.10'
const weiTransferAmount = contractKit.web3.utils.toWei(transferAmount, 'ether')
async function transferCeloToken() {
let accounts = await contractKit.web3.eth.getAccounts()
contractKit.defaultAccount = accounts[0]
const goldToken = await contractKit.contracts.getGoldToken()
// Get + print the balance of the bank account
const balance = await goldToken.balanceOf(accounts[0])
console.log(`Celo Token balance of ${accounts[0]}: ${balance}`)
let gasPrice = '0'
// Transfer the specified amount to the specified recipient
const tx = await goldToken.transfer(recipient, weiTransferAmount).send({gasPrice: gasPrice ? gasPrice : '0'})
// Get + print the transaction hash
const hash = await tx.getHash()
console.log(`ERC20 transfer Transaction hash`,hash)
// Get + print the transaction receipt
const receipt = await tx.waitReceipt()
console.log(`ERC20 transfer Tranaction receipt`, receipt)
// Send CELO natively
let secondTx = await contractKit.sendTransaction({
to: recipient,
value: weiTransferAmount
let secondReceipt = await secondTx.waitReceipt()
console.log(`Native transfer receipt: `, secondReceipt)
// Get + print the new account balance
const newBalance = await goldToken.balanceOf(accounts[0]);
console.log(`Celo Token balance of ${accounts[0]}: ${newBalance}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment