Skip to content

Instantly share code, notes, and snippets.

@critesjosh
Created September 21, 2020 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save critesjosh/b9c6290d568722b8680d83786aaf0b9d to your computer and use it in GitHub Desktop.
Save critesjosh/b9c6290d568722b8680d83786aaf0b9d to your computer and use it in GitHub Desktop.
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('https://alfajores-forno.celo-testnet.org')
// Specify an arbitrary recipient of the transaction
const recipient = '0xB9727f7f1e1f4a5229a49E260fBBBD410d10f2Ff'
const privateKey = '3a26ebc37944c305670a21ea6a5d16c1084db2f18bc6635ba95f487e5c59868f'
contractKit.addAccount(privateKey)
// 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(goldToken.address)
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}`)
}
transferCeloToken()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment