Skip to content

Instantly share code, notes, and snippets.

@erazhu31
Created February 17, 2021 14:54
Show Gist options
  • Save erazhu31/cc522fd992d03ae65427e9294f9eddf6 to your computer and use it in GitHub Desktop.
Save erazhu31/cc522fd992d03ae65427e9294f9eddf6 to your computer and use it in GitHub Desktop.
Celo - Learn To Send CELO And CUSD
Your account address: 0x586497e77b4631CE9C6fE7B5E261A6A7364B0087
This simple Turorial will guide you on how to setup and test an Celo account.
This is from the Celo Document guide : https://docs.celo.org/developer-guide/start/hellocelo
1:
Get the repository from : git clone https://github.com/critesjosh/helloCelo.git
2:
install the dependencies with
npm i
3:
Here you learn how to first get an account Celo account, Get credit from the Celo Faucet to do a Transaction
1
use the script helloCello.js
you should fill each sequence with the code providing here
// 1. Import web3 and contractkit
const Web3 = require("web3")
const ContractKit = require('@celo/contractkit')
// 2. Init a new kit, connected to the alfajores testnet
const web3 = new Web3('https://alfajores-forno.celo-testnet.org')
const kit = ContractKit.newKitFromWeb3(web3)
At that point you can test console.log and kit you should have an Object
// 3. Get the token contract wrappers
let goldtoken = await kit.contracts.getGoldToken()
let stabletoken = await kit.contracts.getStableToken()
// 4. Address to look up
let anAddress = '0xD86518b29BB52a5DAC5991eACf09481CE4B0710d'
Here you will learn how to query an account , you will query the account above for test
// 5. Get token balances
let celoBalance = await goldtoken.balanceOf(anAddress)
let cUSDBalance = await stabletoken.balanceOf(anAddress)
// Print balances
console.log(`${anAddress} CELO balance: ${celoBalance.toString()}`)
console.log(`${anAddress} cUSD balance: ${cUSDBalance.toString()}`)
Now using getAccount.js you will create a new Celo account;
no need to change getAccount.js it is imported and called in helloCelo.js
//
// Create an Account
//
// 6. Import the getAccount function
const getAccount = require('./getAccount').getAccount
async function createAccount(){
// 7. Get your account
let account = await getAccount()
// 8. Get the token contract wrappers
let goldtoken = await kit.contracts.getGoldToken()
let stabletoken = await kit.contracts.getStableToken()
// 9. Get your token balances
let celoBalance = await goldtoken.balanceOf(account.address)
let cUSDBalance = await stabletoken.balanceOf(account.address)
// Print your account info
console.log(`Your account address: ${account.address}`)
console.log(`Your account CELO balance: ${celoBalance.toString()}`)
console.log(`Your account cUSD balance: ${cUSDBalance.toString()}`)
}
Now you have a new account in my case it is
Your account address: 0x586497e77b4631CE9C6fE7B5E261A6A7364B0087
Your account CELO balance: 0
Your account cUSD balance: 0
You should use the Celo Faucet Test to test the transaction: https://celo.org/developers/faucet
You only to paste you address in the faucet and you get some credti for test network
run helloCelo and you should get
Your account address: 0x586497e77b4631CE9C6fE7B5E261A6A7364B0087
Your account CELO balance: 5000000000000000000
Your account cUSD balance: 10000000000000000000
now to do a transaction
async function send(){
// 10. Get your account
let account = await getAccount()
// 11. Add your account to ContractKit to sign transactions
kit.connection.addAccount(account.privateKey)
// 12. Specify recipient Address
let anAddress = '0xD86518b29BB52a5DAC5991eACf09481CE4B0710d'
// 13. Specify an amount to send
let amount = 100000
// 14. Get the token contract wrappers
let goldtoken = await kit.contracts.getGoldToken()
let stabletoken = await kit.contracts.getStableToken()
// 15. Transfer CELO and cUSD from your account to anAddress
// Specify cUSD as the feeCurrency when sending cUSD
let celotx = await goldtoken.transfer(anAddress, amount).send({from: account.address})
let cUSDtx = await stabletoken.transfer(anAddress, amount).send({from: account.address, feeCurrency: stabletoken.address})
// 16. Wait for the transactions to be processed
let celoReceipt = await celotx.waitReceipt()
let cUSDReceipt = await cUSDtx.waitReceipt()
// 17. Print receipts
console.log('CELO Transaction receipt: %o', celoReceipt)
console.log('cUSD Transaction receipt: %o', cUSDReceipt)
// 18. Get your new balances
let celoBalance = await goldtoken.balanceOf(account.address)
let cUSDBalance = await stabletoken.balanceOf(account.address)
// 19. Print new balance
console.log(`Your new account CELO balance: ${celoBalance.toString()}`)
console.log(`Your new account cUSD balance: ${cUSDBalance.toString()}`)
}
you should have this output
CELO Transaction receipt: {
blockHash: '0x25b5a1af58d8b93dda5dc5952918bee755fc4b2295f0f7c7ca514afd730bdaee',
blockNumber: 3659351,
contractAddress: null,
cumulativeGasUsed: 46108,
from: '0x586497e77b4631ce9c6fe7b5e261a6a7364b0087',
gasUsed: 46108,
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000004000000000000000000000100001000000000000000000000000000000000000000000000000000000000010000000004000000000000000000000000000000000000000000000000000000000000000000100000002000000000000000010000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000020000000',
status: true,
to: '0xf194afdf50b03e69bd7d057c1aa9e10c9954e4c9',
transactionHash: '0xcf9584d2de40aa0f1e5193e774c653f650864ced3eda97c175731b0410706732',
transactionIndex: 0,
events: {
Transfer: {
address: '0xF194afDf50B03e69Bd7D057c1Aa9e10c9954E4C9',
blockNumber: 3659351,
transactionHash: '0xcf9584d2de40aa0f1e5193e774c653f650864ced3eda97c175731b0410706732',
transactionIndex: 0,
blockHash: '0x25b5a1af58d8b93dda5dc5952918bee755fc4b2295f0f7c7ca514afd730bdaee',
logIndex: 0,
removed: false,
id: 'log_151c1e98',
returnValues: Result {
'0': '0x586497e77b4631CE9C6fE7B5E261A6A7364B0087',
'1': '0xD86518b29BB52a5DAC5991eACf09481CE4B0710d',
'2': '100000',
from: '0x586497e77b4631CE9C6fE7B5E261A6A7364B0087',
to: '0xD86518b29BB52a5DAC5991eACf09481CE4B0710d',
value: '100000'
},
event: 'Transfer',
signature: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
raw: {
data: '0x00000000000000000000000000000000000000000000000000000000000186a0',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x000000000000000000000000586497e77b4631ce9c6fe7b5e261a6a7364b0087',
'0x000000000000000000000000d86518b29bb52a5dac5991eacf09481ce4b0710d',
[length]: 3
]
}
}
}
}
cUSD Transaction receipt: {
blockHash: '0x25b5a1af58d8b93dda5dc5952918bee755fc4b2295f0f7c7ca514afd730bdaee',
blockNumber: 3659351,
contractAddress: null,
cumulativeGasUsed: 140703,
from: '0x586497e77b4631ce9c6fe7b5e261a6a7364b0087',
gasUsed: 94595,
logsBloom: '0x00000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000008100001000000000000000000000000000000000000000000000000000800000010000000000040000800000000000000000000000000400000000000000000000400000000000100000002000000000000100010000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000',
status: true,
to: '0x874069fa1eb16d44d622f2e0ca25eea172369bc1',
transactionHash: '0x0870583e74b8e53d33a682122a2383c5f5177e887abb6ebdb770d72909aa29d7',
transactionIndex: 1,
events: {
Transfer: [
{
address: '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1',
blockNumber: 3659351,
transactionHash: '0x0870583e74b8e53d33a682122a2383c5f5177e887abb6ebdb770d72909aa29d7',
transactionIndex: 1,
blockHash: '0x25b5a1af58d8b93dda5dc5952918bee755fc4b2295f0f7c7ca514afd730bdaee',
logIndex: 1,
removed: false,
id: 'log_83659ed4',
returnValues: Result {
'0': '0x586497e77b4631CE9C6fE7B5E261A6A7364B0087',
'1': '0xD86518b29BB52a5DAC5991eACf09481CE4B0710d',
'2': '100000',
from: '0x586497e77b4631CE9C6fE7B5E261A6A7364B0087',
to: '0xD86518b29BB52a5DAC5991eACf09481CE4B0710d',
value: '100000'
},
event: 'Transfer',
signature: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
raw: {
data: '0x00000000000000000000000000000000000000000000000000000000000186a0',
topics: [Array]
}
},
{
address: '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1',
blockNumber: 3659351,
transactionHash: '0x0870583e74b8e53d33a682122a2383c5f5177e887abb6ebdb770d72909aa29d7',
transactionIndex: 1,
blockHash: '0x25b5a1af58d8b93dda5dc5952918bee755fc4b2295f0f7c7ca514afd730bdaee',
logIndex: 2,
removed: false,
id: 'log_2cc6c2cd',
returnValues: Result {
'0': '0x586497e77b4631CE9C6fE7B5E261A6A7364B0087',
'1': '0xAA963FC97281d9632d96700aB62A4D1340F9a28a',
'2': '44294101087805',
from: '0x586497e77b4631CE9C6fE7B5E261A6A7364B0087',
to: '0xAA963FC97281d9632d96700aB62A4D1340F9a28a',
value: '44294101087805'
},
event: 'Transfer',
signature: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
raw: {
data: '0x000000000000000000000000000000000000000000000000000028490629363d',
topics: [Array]
}
},
{
address: '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1',
blockNumber: 3659351,
transactionHash: '0x0870583e74b8e53d33a682122a2383c5f5177e887abb6ebdb770d72909aa29d7',
transactionIndex: 1,
blockHash: '0x25b5a1af58d8b93dda5dc5952918bee755fc4b2295f0f7c7ca514afd730bdaee',
logIndex: 3,
removed: false,
id: 'log_07fa3430',
returnValues: Result {
'0': '0x586497e77b4631CE9C6fE7B5E261A6A7364B0087',
'1': '0x621843731fe33418007C06ee48CfD71e0ea828d9',
'2': '177176404351220',
from: '0x586497e77b4631CE9C6fE7B5E261A6A7364B0087',
to: '0x621843731fe33418007C06ee48CfD71e0ea828d9',
value: '177176404351220'
},
event: 'Transfer',
signature: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
raw: {
data: '0x0000000000000000000000000000000000000000000000000000a12418a4d8f4',
topics: [Array]
}
},
[length]: 3
]
}
}
Your new account CELO balance: 4999976945999900000
Your new account cUSD balance: 9999778529494460975
@critesjosh
Copy link

thanks for submitting! where did you hear about this bounty?

@erazhu31
Copy link
Author

erazhu31 commented Feb 28, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment