-
Notifications
You must be signed in to change notification settings - Fork 1
/
bet.js
72 lines (48 loc) · 2.05 KB
/
bet.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
const BN = require("bn.js");
const Bet = artifacts.require("Bet");
module.exports = async function (callback) {
try {
let instance = await Bet.deployed();
console.log("Contract instance address: " + instance.address);
// Get account list from Ganache
let accounts = await web3.eth.getAccounts();
if (accounts.length < 2) {
callback(new Error("accounts not enough"));
return;
}
console.log("Initial balances: ");
await printBalance(accounts[0], instance);
await printBalance(accounts[1], instance);
// Add some balances to the addresses;
let amount = new BN(200);
let response = await instance.addBalance(amount, {from: accounts[0]});
console.log("Transaction hash: " + response.tx);
console.log("Balances after addBalance: ");
await printBalance(accounts[0], instance);
await printBalance(accounts[1], instance);
// Init bet
let betAmount = new BN(100);
response = await instance.initBet(betAmount, {from: accounts[0]});
console.log("Transaction hash: " + response.tx);
console.log("Balances after initBet: ");
await printBalance(accounts[0], instance);
await printBalance(accounts[1], instance);
// Submit result
let result = 2;
response = await instance.submitResult(result, {from: accounts[1]});
console.log("Transaction hash: " + response.tx);
console.log("Balances after submitResult: ");
await printBalance(accounts[0], instance);
await printBalance(accounts[1], instance);
callback();
} catch (e) {
callback(e);
}
};
async function printBalance(account, instance) {
console.log("---------------------------------------------------------------");
console.log("Account: " + account);
let balance = await instance.getBalance(account);
console.log("Balance: " + balance.toString());
console.log("---------------------------------------------------------------");
}