Created
February 23, 2021 17:29
-
-
Save preston4896/90363389c57d9c99d5e329f42b23512b to your computer and use it in GitHub Desktop.
Hello World Smart Contract Tutorial for the Celo Blockchain
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
const Web3 = require('web3') | |
const ContractKit = require('@celo/contractkit') | |
const web3 = new Web3('https://alfajores-forno.celo-testnet.org') | |
const kit = ContractKit.newKitFromWeb3(web3) | |
const getAccount = require('./getAccount').getAccount | |
const HelloWorld = require('./build/contracts/HelloWorld.json') | |
async function awaitWrapper(){ | |
let account = await getAccount() | |
// This account must have a CELO balance to pay tx fees | |
// get some testnet funds at https://celo.org/build/faucet | |
console.log(account.address) | |
kit.connection.addAccount(account.privateKey) // this account must have a CELO balance to pay transaction fees | |
let tx = await kit.connection.sendTransaction({ | |
from: account.address, | |
data: HelloWorld.bytecode | |
}) | |
const receipt = await tx.waitReceipt() | |
console.log(receipt) | |
} | |
awaitWrapper() |
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
// This script requires that you have already deployed HelloWorld.sol with Truffle | |
// Go back and do that if you haven't already | |
// Account address: 0x8d02CC83da06eF1BE14c2df6289ED9F8Af64fBAf | |
// 1. Import web3 and contractkit | |
const Web3 = require("web3") | |
const ContractKit = require('@celo/contractkit') | |
// 2. Import the getAccount function | |
const getAccount = require('./getAccount').getAccount | |
// 3. Init a new kit, connected to the alfajores testnet | |
const web3 = new Web3('https://alfajores-forno.celo-testnet.org') | |
const kit = ContractKit.newKitFromWeb3(web3) | |
// import HelloWorld info | |
const HelloWorld = require('./build/contracts/HelloWorld.json') | |
// Initialize a new Contract interface | |
async function initContract(){ | |
// Check the Celo network ID | |
const networkId = await web3.eth.net.getId(); | |
const deployedNetwork = HelloWorld.networks[networkId]; | |
// Create a new contract instance with the HelloWorld contract info | |
let instance = new web3.eth.Contract( | |
HelloWorld.abi, | |
deployedNetwork && deployedNetwork.address | |
); | |
getName(instance) | |
setName(instance, "hello world!") | |
} | |
// Read the 'name' stored in the HelloWorld.sol contract | |
async function getName(instance){ | |
let name = await instance.methods.getName().call() | |
console.log(name) | |
} | |
// Set the 'name' stored in the HelloWorld.sol contract | |
async function setName(instance, newName){ | |
let account = await getAccount() | |
// Add your account to ContractKit to sign transactions | |
// This account must have a CELO balance to pay tx fees, get some https://celo.org/build/faucet | |
kit.connection.addAccount(account.privateKey) | |
// Encode the transaction to HelloWorld.sol according to the ABI | |
let txObject = await instance.methods.setName(newName) | |
// Send the transaction | |
let tx = await kit.sendTransactionObject(txObject, { from: account.address }) | |
let receipt = await tx.waitReceipt() | |
console.log(receipt) | |
} | |
initContract() |
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
// SPDX-License-Identifier: MIT | |
pragma solidity >=0.4.22 <0.8.0; | |
contract HelloWorld { | |
string name = "Celo"; | |
function getName() public view returns (string memory) { | |
return name; | |
} | |
function setName(string memory input) public { | |
name = input; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment