-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.js
77 lines (69 loc) · 1.85 KB
/
lib.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
const {api, noAuthApi, rpc} = require("./utils");
async function getBalance(code, scope) {
let result = await rpc.get_table_rows({
json: true, //json
code: code,//CONTRACT_NAME
scope: scope,// SCOPE_ACCOUNT (Normally contract)
table: "accounts", // TABLE_NAME
lower_bound: "0",// index low >=
upper_bound: "-1",// index high <
limit: "10",// county
key_type: "i64",//
index_position: "1" //
});
return result.rows[0].balance
}
async function sendToken(from, to, account, amount) {
let data = {from: from, to: to, quantity: amount, memo: ''};
return await this.sendTrans(from, account, "transfer", data)
}
async function sendTrans(from, account, action, data) {
let result = await api.transact({
actions: [{
account: account,
name: action,
authorization: [{
actor: from,
permission: 'active',
}],
data: data,
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
return result.transaction_id
}
async function notOwnerSendTrans(from, account, action, data) {
let result = await noAuthApi.transact({
actions: [{
account: account,
name: action,
authorization: [{
actor: from,
permission: 'active',
}],
data: data,
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
return result.transaction_id
}
async function awaitBeforeGetReceipt(tx) {
while (true) {
let ret = await rpc.history_get_transaction(tx);
if (ret.traces[0].producer_block_id) {
break
}
}
return true
}
module.exports = {
getBalance,
sendToken,
awaitBeforeGetReceipt,
sendTrans,
notOwnerSendTrans,
};