-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
128 lines (102 loc) · 2.93 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// assume: node 8 or above
const ora = require("ora")
const parseArgs = require("minimist")
const {
Hydra,
} = require("hydrachainjs")
const repoData = require("./solar.json")
const hydra = new Hydra("http://user:password@localhost:3389", repoData)
const myToken = hydra.contract("lcstoken")
async function totalSupply() {
const result = await myToken.call("totalSupply")
// supply is a BigNumber instance (see: bn.js)
const supply = result.outputs[0]
console.log("supply", supply.toNumber())
}
async function balanceOf(owner) {
const res = await myToken.call("balanceOf", [owner])
// balance is a BigNumber instance (see: bn.js)
const balance = res.outputs[0]
console.log(`balance:`, balance.toNumber())
}
async function mint(toAddr, amount) {
const tx = await myToken.send("mint", [toAddr, amount])
console.log("mint tx:", tx.txid)
console.log(tx)
// or: await tx.confirm(1)
const confirmation = tx.confirm(1)
ora.promise(confirmation, "confirm mint")
const receipt = await confirmation
console.log("tx receipt:", JSON.stringify(receipt, null, 2))
}
async function transfer(fromAddr, toAddr, amount) {
const tx = await myToken.send("transfer", [toAddr, amount], {
senderAddress: fromAddr,
})
console.log("transfer tx:", tx.txid)
console.log(tx)
// or: await tx.confirm(1)
const confirmation = tx.confirm(1)
ora.promise(confirmation, "confirm transfer")
await confirmation
}
async function streamEvents() {
console.log("Subscribed to contract events")
console.log("Ctrl-C to terminate events subscription")
myToken.onLog((entry) => {
console.log(entry)
}, { minconf: 1 })
}
async function getLogs(fromBlock, toBlock) {
const logs = await myToken.logs({
fromBlock,
toBlock,
minconf: 1,
})
console.log(JSON.stringify(logs, null, 2))
}
async function main() {
const argv = parseArgs(process.argv.slice(2))
const cmd = argv._[0]
if (process.env.DEBUG) {
console.log("argv", argv)
console.log("cmd", cmd)
}
switch (cmd) {
case "supply":
case "totalSupply":
await totalSupply()
break
case "balance":
const owner = argv._[1]
if (!owner) {
throw new Error("please specify an address")
}
await balanceOf(owner)
break
case "mint":
const mintToAddr = argv._[1]
const mintAmount = parseInt(argv._[2])
await mint(mintToAddr, mintAmount)
break
case "transfer":
const fromAddr = argv._[1]
const toAddr = argv._[2]
const amount = argv._[3]
await transfer(fromAddr, toAddr, amount)
break
case "logs":
const fromBlock = parseInt(argv._[1]) || 0
const toBlock = parseInt(argv._[2]) || "latest"
await getLogs(fromBlock, toBlock)
break
case "events":
await streamEvents() // logEvents will never return
break
default:
console.log("unrecognized command", cmd)
}
}
main().catch(err => {
console.log("error", err)
})