Skip to content

Instantly share code, notes, and snippets.

@critesjosh

critesjosh/index.js

Last active May 6, 2020
Embed
What would you like to do?
This script is a demo of how to use the ContractKit to send a transaction on the Celo network (https://celo.org)
#!/usr/bin/env node
/*
This script is a demo of how to use the ContractKit to send a transaction on the Celo network (https://celo.org)
With npm installed, run this script with the command:
npx https://gist.github.com/critesjosh/7dd9cdb3076d9d5874c061612af8057e
*/
// Import ContractKit
const ContractKit = require('@celo/contractkit')
// Create a new instance of ContractKit, connecting to Celo via the public API
const contractKit = ContractKit.newKit('https://alfajores-forno.celo-testnet.org')
// Specify an arbitrary recipient of the transaction
const recipient = '0xB9727f7f1e1f4a5229a49E260fBBBD410d10f2Ff'
// Specify an amount to trasnfer, and convert it to the appropriate units
const transferAmount = '0.10'
const weiTransferAmount = contractKit.web3.utils.toWei(transferAmount, 'ether')
// If you want to create a new account: const account = kit.web3.eth.accounts.create();
// You can faucet an account here: https://celo.org/build/faucet
// Import an existing, funded account
const bankAccount = {
privateKey: '0x51788d6e1eae7c73e1663d63404f8633ceffb3543005e6ab3509129caeb483d7',
address: '0xFAfD7d820bf9294d344393A43cDB0F693e91Cfab'
}
// Add the account private key to contractKit so it can sign transactions
contractKit.addAccount(bankAccount.privateKey)
async function transferStableToken() {
// Get the stable token (cUSD) contract
const stableToken = await contractKit.contracts.getStableToken()
// Get + print the balance of the bank account
const balance = await stableToken.balanceOf(bankAccount.address)
console.log(`Stable Token balance of ${bankAccount.address}: ${balance}`)
// Transfer the specified amount to the specified recipient
const tx = await stableToken.transfer(recipient, weiTransferAmount).send({
from: bankAccount.address,
})
// Get + print the transaction hash
const hash = await tx.getHash()
console.log(`Transaction hash`,hash)
// Get + print the transaction receipt
const receipt = await tx.waitReceipt()
console.log(`Tranaction receipt`, receipt)
// Get + print the new account balance
const newBalance = await stableToken.balanceOf(bankAccount.address);
console.log(`Stable Token balance of ${bankAccount.address}: ${newBalance}`)
}
transferStableToken()
{
"name": "contractkit-example",
"version": "0.0.0",
"bin": "./index.js",
"dependencies": {
"@celo/contractkit": "latest"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment