-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelloWorld.js
57 lines (44 loc) · 1.88 KB
/
helloWorld.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// This script requires that you have already deployed HelloWorld.sol with Truffle
// Go back and do that if you haven't already
// 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()