forked from txiuqw4/farmersworld-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transact.js
83 lines (76 loc) · 2 KB
/
transact.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
import { Api, JsonRpc, RpcError } from "eosjs";
import { JsSignatureProvider } from "eosjs/dist/eosjs-jssig.js";
import fetch from "node-fetch";
import { TextEncoder, TextDecoder } from "util";
const rpcEndPoint = "https://wax.greymass.com";
const MAX_RETRIES = 3;
/*
* Make a transact
*
* @param privateKey string
* @param action array
* @param paybw object | null
* @return Promise
*/
function makeTransaction(privateKey, actions, paybw = null) {
const privateKeys = [privateKey];
const actionsClone = actions.map(r => r);
// insert noop action
if (paybw) {
privateKeys.push(paybw.privateKey);
actionsClone.unshift({
account: "boost.wax",
name: "noop",
authorization: [
{
actor: paybw.wallet,
permission: "active",
},
],
data: null,
});
}
const signatureProvider = new JsSignatureProvider(privateKeys);
const rpc = new JsonRpc(rpcEndPoint, { fetch });
const api = new Api({
rpc,
signatureProvider,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder(),
});
return api.transact(
{
actions: actionsClone,
},
{
blocksBehind: 3,
expireSeconds: 30,
}
);
}
/*
* Retry transact
*
* @param privateKey string
* @param actions array
* @param paybw object | null
* @param tries number default 0
*
* @return Promise
*/
export default async function transact(privateKey, actions, paybw = null, tries = 0) {
let result;
try {
result = await makeTransaction(privateKey, actions, paybw);
console.log(
"[Finished] - Successful - Transaction id",
result.transaction_id
);
} catch (e) {
console.log("[ERROR] -", e.message);
if (tries < MAX_RETRIES)
return transact(privateKey, actions, paybw, tries + 1);
throw e;
}
return result;
}