Last active
December 4, 2023 18:52
-
-
Save critesjosh/7dd9cdb3076d9d5874c061612af8057e to your computer and use it in GitHub Desktop.
This script is a demo of how to use the ContractKit to send a transaction on the Celo network (https://celo.org)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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