This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helloCelo.js
105 lines (79 loc) · 3.24 KB
/
helloCelo.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Add package imports and setup here
//
// 1. Import ContractKit and Web3
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)
//
// Read Accounts
//
async function readAccount(){
// 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'
// 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()}`)
}
//
// 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()}`)
}
//
// Send CELO
//
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()}`)
}
readAccount()
createAccount()
send()