-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.js
More file actions
218 lines (166 loc) · 5.29 KB
/
examples.js
File metadata and controls
218 lines (166 loc) · 5.29 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"use srtict";
const grpc = require('grpc')
const protoLoader = require('@grpc/proto-loader')
const utils = require('./utils.js')
const PROTO_PATH = './NodeCore/veriblock.proto'
const packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const vbk_proto = grpc.loadPackageDefinition(packageDefinition).core;
const client = new vbk_proto.Admin('localhost:10502', grpc.credentials.createInsecure());
function getInfo() {
// get info
client.GetInfo({}, (err, res) => {
if(err) return console.error(err)
console.log("NC_CLI command: getinfo")
console.log(`LastBlock.Hash= ${res.last_block.hash.toString("hex")}`)
console.log(`EstimatedHasrate= ${res.estimated_hashrate} h/s`)
})
client.GetStateInfo({}, (err, res) => {
if(err) return console.error(err)
console.log("NC_CLI command: getstateinfo")
console.log(`NetworkHeight= ${res.network_height}`)
console.log(`NetworkVersion= ${res.network_version}`)
console.log(`LocalBlockchainHeight= ${res.local_blockchain_height}`)
console.log("---------------------")
console.log("")
})
}
function sendTranasction() {
// sending implies also signing
const sourseAddress = "VFXWGNLcGR4vTCSU6VAMXvgru9EKk3"
const targetAddress = "V5gb4UCrzn7rxzqaJE9EYNvZoF3KUC"
const amount = 10.5
const request = {
source_address: utils.convertAddressToByteString(sourseAddress),
amounts: [{
address: utils.convertAddressToByteString(targetAddress),
amount: 10
}]
}
client.SendCoins(request, (err, res) => {
if(err) return console.error(err)
console.log("SendTransaction");
console.log("NC_CLI command: send <amount> <destinationAddress> [sourceAddress]");
//Note - could create multiple Tx, pick just the first one for demo:
const txHash = res.tx_ids[0].toString("hex");
console.log(`Created transaction: ${txHash}`)
console.log("---------------------")
console.log("")
})
}
function getBlockByIndex() {
const blockIndex = 100
const request = {
search_length: 2000,
filters: [{
index: 100
}]
}
client.getBlocks(request, (err, res) => {
if(err) return console.error(err)
console.log("GetBlockByIndex")
console.log("NC_CLI command: getblockfromindex <blockIndex>")
console.log("")
if(res.blocks.length > 0) {
// display info
const blockHash = res.blocks[0].hash.toString("hex")
console.log(`BlocksHash= ${blockHash}`)
console.log("---------------------")
console.log("")
}
})
}
function getBlockByHash() {
const blockHash = "00000000b4316a043aa23f327a2b19f9e23fafd0356e48a8"
const request = {
search_length: 2000,
filters: [{
hash: Buffer.from(blockHash, "hex")
}]
}
client.getBlocks(request, (err, res) => {
console.log("GetBlockByHash")
console.log("NC_CLI command: getblockfromhash <blockHash>")
console.log("")
if(err) return console.error(err)
if(res.blocks.length > 0) {
// display info
const blockIndex = res.blocks[0].number
console.log(`BlockIndex= ${blockIndex}`)
console.log("---------------------")
console.log("")
}
})
}
// -------------------------------
function getTransactionById() {
const txId = "12498E1EF73BCA555C5EB1F0AC1D7C6D8F3256DEED9AE7A78C74DD7A762D1B8B"
// get transaction
const request = {
search_length: 2000,
ids: [Buffer.from(txId, "hex")]
}
client.GetTransactions(request, (err,res) => {
console.log("GetTransactionById");
console.log("NC_CLI command: gettransaction <txId> [searchLength]");
console.log("")
if(err) return console.error(err)
if(res.transactions.length > 0) {
// display info
const blockIndex = res.transactions[0].block_number
const amount = utils.convertAtomicToVbkUnits(res.transactions[0].transaction.source_amount)
console.log(`BlockIndex= ${blockIndex}, sourseAmount= ${amount}`)
console.log("---------------------")
console.log("")
}
})
}
function getBalance() {
const address = "VFXWGNLcGR4vTCSU6VAMXvgru9EKk3"
// get balance
const request = {
addresses: [utils.convertAddressToByteString(address)]
}
client.getBalance(request, (err, res) => {
console.log("GetBalance")
console.log("NC_CLI command: getbalance [address]")
console.log("")
if(err) return console.error(err)
console.log(`Confirmed= ${utils.convertAtomicToVbkUnits(res.confirmed[0].unlocked_amount)} VBK`)
console.log(`Pending= ${utils.convertAtomicToVbkUnits(res.unconfirmed[0].amount)} VBK`)
console.log("---------------------")
console.log("")
})
}
function getNewAddress() {
// generate address
client.GetNewAddress(1, (err,res) => {
console.log("GetNewAddress")
console.log("NC_CLI command: getnewaddress")
console.log("")
if(res.success) {
if(err) return console.error(err)
const bytes = Buffer.from(res.address, 'hex')
const address = utils.convertByteToAddressString(bytes)
console.log(`New address: ${address}`)
console.log("------------------")
console.log("")
}
})
}
function main() {
getInfo()
getBlockByIndex()
getBlockByHash()
getTransactionById()
getBalance()
getNewAddress()
sendTranasction()
}
main()