-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.js
More file actions
179 lines (152 loc) · 4.26 KB
/
Copy pathbasic.js
File metadata and controls
179 lines (152 loc) · 4.26 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
import { Serialize } from 'cyberwayjs';
export const BLOCKS_BEHIND = 3;
export const EXPIRE_SECONDS = 30;
export default class BasicApi {
constructor() {
this.api = undefined;
}
setApi(api) {
this.api = api;
if (!this._contractActions) {
return;
}
for (const actionName of this._contractActions) {
this[actionName] = function(contractAccount, auth, data, options) {
if (typeof contractAccount !== 'string') {
// action(actor, data, options)
[auth, data, options] = arguments;
contractAccount = this._contractAccount;
}
return this._transaction(
[
{
contractAccount,
actionName,
auth,
data,
},
],
options
);
};
}
}
_verifyAuth(authList) {
if (authList.length === 0) {
throw new Error('Empty "auth" parameter');
}
for (const { actor, accountName } of authList) {
if (!actor && !accountName) {
throw new Error('Item in "auth" should be an object with a field "actor"');
}
}
}
_verifyApi() {
if (!this.api) {
throw new Error('API is not initialized');
}
}
_verifyContractAccount(contractAccount) {
if (!contractAccount) {
throw new Error('Property "contractAccount" should be set');
}
}
_validateTransactionOptions(contractAccount, actionName, authList, data) {
this._verifyContractAccount(contractAccount);
this._verifyApi();
this._verifyAuth(authList);
}
prepareAction(contractAccount, actionName, authList, data) {
return {
account: contractAccount,
name: actionName,
authorization: authList,
data,
};
}
_transaction = async (
actions,
{
broadcast = true,
msig = false,
msigExpires = 600,
provideBandwidthFor = null,
bandwidthProvider = 'cyber',
delaySec = 0,
expireSeconds = null,
signByActors = null,
} = {}
) => {
const preparedActions = actions.map(({ contractAccount, actionName, auth, data }) => {
const authList = this._normalizeAuth(auth);
this._validateTransactionOptions(contractAccount, actionName, authList, data);
return this.prepareAction(contractAccount, actionName, authList, data);
});
if (msig) {
return {
...(await this._makeTransactionHeader({ expires: msigExpires, delaySec })),
actions: await this.api.serializeActions(preparedActions),
};
}
if (provideBandwidthFor) {
const list = Array.isArray(provideBandwidthFor) ? provideBandwidthFor : [provideBandwidthFor];
for (const account of list) {
preparedActions.push(
this.prepareAction(
'cyber',
'providebw',
[
{
actor: bandwidthProvider,
permission: 'providebw',
},
],
{
provider: bandwidthProvider,
account,
}
)
);
}
}
if (!broadcast && !provideBandwidthFor) {
return preparedActions;
}
return await this.transact(
{ actions: preparedActions, delay_sec: delaySec },
{ providebw: Boolean(provideBandwidthFor), broadcast, signByActors, expireSeconds }
);
};
async transact(trx, options) {
return await this.api.transact(trx, {
...options,
blocksBehind: BLOCKS_BEHIND,
expireSeconds: (options && options.expireSeconds) || EXPIRE_SECONDS,
});
}
async _makeTransactionHeader({ expires, delaySec }) {
const info = await this.api.rpc.get_info();
const refBlock = await this.api.rpc.get_block(info.head_block_num - BLOCKS_BEHIND);
return {
...this.api.getDefaultTransactionHeader(),
...Serialize.transactionHeader(refBlock, expires),
delay_sec: delaySec,
};
}
_normalizeAuth(auth) {
if (!auth) {
return [];
}
let authList = auth;
if (!Array.isArray(auth)) {
authList = [auth];
}
return authList.map(({ actor, accountName, permission }) => ({
actor: actor || accountName,
permission: permission || 'active',
}));
}
executeActions(_, actions, options) {
return this._transaction(actions, options);
}
}