openchain-nodejs-ts-yxl 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +66 -0
- package/_config.yml +1 -0
- package/doc/SDK_CN.md +2003 -0
- package/example/atp10TokenDemo.js +319 -0
- package/example/exchange.js +184 -0
- package/example/offlineSignatureDemo.js +83 -0
- package/example/submitTransactionDemo.js +92 -0
- package/index.d.ts +186 -0
- package/index.js +9 -0
- package/lib/account/index.js +234 -0
- package/lib/blockchain/block.js +380 -0
- package/lib/blockchain/transaction.js +264 -0
- package/lib/common/ctp.js +5 -0
- package/lib/common/operation/accountSetMetadata.js +55 -0
- package/lib/common/operation/accountSetPrivilege.js +90 -0
- package/lib/common/operation/activateAccount.js +58 -0
- package/lib/common/operation/contractCreate.js +72 -0
- package/lib/common/operation/contractInvokeByAsset.js +75 -0
- package/lib/common/operation/contractInvokeByBU.js +53 -0
- package/lib/common/operation/createLog.js +44 -0
- package/lib/common/operation/ctp10TokenApprove.js +59 -0
- package/lib/common/operation/ctp10TokenAssign.js +59 -0
- package/lib/common/operation/ctp10TokenChangeOwner.js +58 -0
- package/lib/common/operation/ctp10TokenIssue.js +78 -0
- package/lib/common/operation/ctp10TokenTransfer.js +59 -0
- package/lib/common/operation/ctp10TokenTransferFrom.js +60 -0
- package/lib/common/operation/issueAsset.js +35 -0
- package/lib/common/operation/payAsset.js +62 -0
- package/lib/common/operation/payCoin.js +44 -0
- package/lib/common/util.js +880 -0
- package/lib/contract/index.js +212 -0
- package/lib/crypto/protobuf/bundle.json +1643 -0
- package/lib/exception/customErrors.js +56 -0
- package/lib/exception/errors.js +240 -0
- package/lib/exception/index.js +8 -0
- package/lib/operation/account.js +193 -0
- package/lib/operation/asset.js +106 -0
- package/lib/operation/bu.js +52 -0
- package/lib/operation/contract.js +184 -0
- package/lib/operation/ctp10Token.js +394 -0
- package/lib/operation/index.js +30 -0
- package/lib/operation/log.js +47 -0
- package/lib/sdk.js +70 -0
- package/lib/token/asset.js +79 -0
- package/lib/token/ctp10Token.js +301 -0
- package/lib/token/index.js +17 -0
- package/lib/util/index.js +86 -0
- package/openchain-sdk-nodejs.iml +9 -0
- package/package.json +39 -0
- package/test/Ctp10Token.test.js +96 -0
- package/test/account.test.js +132 -0
- package/test/accountActivateOperation.test.js +99 -0
- package/test/accountSetMetadata.test.js +102 -0
- package/test/accountSetPrivilege.test.js +66 -0
- package/test/asset.test.js +63 -0
- package/test/assetIssueOperation.test.js +77 -0
- package/test/assetSendOperation.test.js +103 -0
- package/test/blob.test.js +128 -0
- package/test/block.test.js +165 -0
- package/test/buSendOperation.test.js +64 -0
- package/test/contract.test.js +64 -0
- package/test/contractCreateOperation.test.js +41 -0
- package/test/contractCreateTransaction.test.js +116 -0
- package/test/contractInvokeByAssetOperation.test.js +109 -0
- package/test/contractInvokeByBUOperation.test.js +107 -0
- package/test/ctp10TokenApproveOperation.test.js +98 -0
- package/test/ctp10TokenAssignOperation.test.js +99 -0
- package/test/ctp10TokenChangeOwnerOperation.test.js +98 -0
- package/test/ctp10TokenIssueOperation.test.js +40 -0
- package/test/ctp10TokenIssueOperationTransaction.test.js +106 -0
- package/test/ctp10TokenTransferFromOperation.test.js +101 -0
- package/test/ctp10TokenTransferOperation.test.js +98 -0
- package/test/log.transaction.test.js +103 -0
- package/test/transaction.test.js +166 -0
- package/test/util.test.js +93 -0
@@ -0,0 +1,380 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const wrap = require('co-wrap-all');
|
4
|
+
const is = require('is-type-of');
|
5
|
+
const merge = require('merge-descriptors');
|
6
|
+
const humps = require('humps');
|
7
|
+
const errors = require('../exception');
|
8
|
+
|
9
|
+
module.exports = Block;
|
10
|
+
|
11
|
+
function Block(options) {
|
12
|
+
if (!(this instanceof Block)) {
|
13
|
+
return new Block(options);
|
14
|
+
}
|
15
|
+
|
16
|
+
this.options = options;
|
17
|
+
}
|
18
|
+
|
19
|
+
const proto = Block.prototype;
|
20
|
+
|
21
|
+
merge(proto, require('../common/util'));
|
22
|
+
|
23
|
+
proto.getNumber = function* () {
|
24
|
+
try {
|
25
|
+
return yield this._getBlockNumber();
|
26
|
+
} catch (err) {
|
27
|
+
throw err;
|
28
|
+
}
|
29
|
+
};
|
30
|
+
|
31
|
+
proto.checkStatus = function* () {
|
32
|
+
try {
|
33
|
+
const data = yield this._request('get', 'getModulesStatus');
|
34
|
+
const info = data.ledger_manager;
|
35
|
+
|
36
|
+
if (info.chain_max_ledger_seq === info.ledger_sequence) {
|
37
|
+
return this._responseData({
|
38
|
+
isSynchronous: true,
|
39
|
+
});
|
40
|
+
}
|
41
|
+
return this._responseData({
|
42
|
+
isSynchronous: false,
|
43
|
+
});
|
44
|
+
} catch (err) {
|
45
|
+
throw err;
|
46
|
+
}
|
47
|
+
};
|
48
|
+
|
49
|
+
proto.getTransactions = function* (blockNumber) {
|
50
|
+
try {
|
51
|
+
if (!this._verifyValue(blockNumber)) {
|
52
|
+
return this._responseError(errors.INVALID_BLOCKNUMBER_ERROR);
|
53
|
+
}
|
54
|
+
|
55
|
+
const data = yield this._request('get', 'getTransactionHistory', {
|
56
|
+
ledger_seq: blockNumber,
|
57
|
+
});
|
58
|
+
|
59
|
+
if (data.error_code === 0) {
|
60
|
+
return this._responseData(data.result);
|
61
|
+
}
|
62
|
+
|
63
|
+
if (data.error_code === 4) {
|
64
|
+
return this._responseError(errors.QUERY_RESULT_NOT_EXIST, data.result);
|
65
|
+
}
|
66
|
+
|
67
|
+
return this._responseError(errors.FAIL);
|
68
|
+
|
69
|
+
} catch (err) {
|
70
|
+
throw err;
|
71
|
+
}
|
72
|
+
};
|
73
|
+
|
74
|
+
proto.getInfo = function* (blockNumber) {
|
75
|
+
try {
|
76
|
+
if (!this._verifyValue(blockNumber)) {
|
77
|
+
return this._responseError(errors.INVALID_BLOCKNUMBER_ERROR);
|
78
|
+
}
|
79
|
+
|
80
|
+
const data = yield this._request('get', 'getLedger', {
|
81
|
+
seq: blockNumber,
|
82
|
+
});
|
83
|
+
if (data.error_code === 0) {
|
84
|
+
let info = {};
|
85
|
+
|
86
|
+
if (data.result && data.result.header) {
|
87
|
+
const header = data.result.header;
|
88
|
+
const closeTime = header.close_time || '';
|
89
|
+
const number = header.seq;
|
90
|
+
const txCount = header.tx_count || '';
|
91
|
+
const version = header.version || '';
|
92
|
+
info = {
|
93
|
+
closeTime,
|
94
|
+
number,
|
95
|
+
txCount,
|
96
|
+
version,
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
return this._responseData(info);
|
101
|
+
}
|
102
|
+
|
103
|
+
if (data.error_code === 4) {
|
104
|
+
return this._responseError(errors.QUERY_RESULT_NOT_EXIST, data.result);
|
105
|
+
}
|
106
|
+
|
107
|
+
return this._responseError(errors.FAIL);
|
108
|
+
|
109
|
+
} catch (err) {
|
110
|
+
throw err;
|
111
|
+
}
|
112
|
+
};
|
113
|
+
|
114
|
+
|
115
|
+
proto.getLatestInfo = function* () {
|
116
|
+
try {
|
117
|
+
const data = yield this._request('get', 'getLedger');
|
118
|
+
|
119
|
+
if (data.error_code === 0) {
|
120
|
+
let info = {};
|
121
|
+
|
122
|
+
if (data.result && data.result.header) {
|
123
|
+
const header = data.result.header;
|
124
|
+
const closeTime = header.close_time || '';
|
125
|
+
const number = header.seq;
|
126
|
+
const txCount = header.tx_count || '';
|
127
|
+
const version = header.version || '';
|
128
|
+
info = {
|
129
|
+
closeTime,
|
130
|
+
number,
|
131
|
+
txCount,
|
132
|
+
version,
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
return this._responseData(info);
|
137
|
+
}
|
138
|
+
|
139
|
+
if (data.error_code === 4) {
|
140
|
+
return this._responseError(errors.QUERY_RESULT_NOT_EXIST, data.result);
|
141
|
+
}
|
142
|
+
|
143
|
+
return this._responseError(errors.FAIL);
|
144
|
+
|
145
|
+
} catch (err) {
|
146
|
+
throw err;
|
147
|
+
}
|
148
|
+
};
|
149
|
+
|
150
|
+
proto.getValidators = function* (blockNumber) {
|
151
|
+
try {
|
152
|
+
if (!this._verifyValue(blockNumber)) {
|
153
|
+
return this._responseError(errors.INVALID_BLOCKNUMBER_ERROR);
|
154
|
+
}
|
155
|
+
|
156
|
+
const data = yield this._request('get', 'getLedger', {
|
157
|
+
seq: blockNumber,
|
158
|
+
with_validator: true,
|
159
|
+
});
|
160
|
+
|
161
|
+
if (data.error_code === 0) {
|
162
|
+
let validatorsInfo = data.result.validators;
|
163
|
+
let validators = [];
|
164
|
+
if (!is.array(validatorsInfo) && validatorsInfo.length === 0) {
|
165
|
+
validators = [];
|
166
|
+
}
|
167
|
+
// [{a_b: 'demo'}] => [{aB: 'demo'}]
|
168
|
+
// validatorsInfo = humps.camelizeKeys(validatorsInfo);
|
169
|
+
|
170
|
+
validatorsInfo.forEach(item => {
|
171
|
+
if (!item.pledge_coin_amount) {
|
172
|
+
item.pledge_coin_amount = '0';
|
173
|
+
}
|
174
|
+
validators.push(item);
|
175
|
+
});
|
176
|
+
return this._responseData({
|
177
|
+
validators,
|
178
|
+
});
|
179
|
+
}
|
180
|
+
|
181
|
+
if (data.error_code === 4) {
|
182
|
+
return this._responseError(errors.QUERY_RESULT_NOT_EXIST, data.result);
|
183
|
+
}
|
184
|
+
|
185
|
+
return this._responseError(errors.FAIL);
|
186
|
+
|
187
|
+
} catch (err) {
|
188
|
+
throw err;
|
189
|
+
}
|
190
|
+
};
|
191
|
+
|
192
|
+
|
193
|
+
proto.getLatestValidators = function* () {
|
194
|
+
try {
|
195
|
+
const data = yield this._request('get', 'getLedger', {
|
196
|
+
with_validator: true,
|
197
|
+
});
|
198
|
+
|
199
|
+
if (data.error_code === 0) {
|
200
|
+
let validatorsInfo = data.result.validators;
|
201
|
+
let validators = [];
|
202
|
+
if (!is.array(validatorsInfo) && validatorsInfo.length === 0) {
|
203
|
+
validators = [];
|
204
|
+
}
|
205
|
+
// [{a_b: 'demo'}] => [{aB: 'demo'}]
|
206
|
+
// validatorsInfo = humps.camelizeKeys(validatorsInfo);
|
207
|
+
|
208
|
+
validatorsInfo.forEach(item => {
|
209
|
+
if (!item.pledge_coin_amount) {
|
210
|
+
item.pledge_coin_amount = '0';
|
211
|
+
}
|
212
|
+
validators.push(item);
|
213
|
+
});
|
214
|
+
return this._responseData({
|
215
|
+
validators,
|
216
|
+
});
|
217
|
+
}
|
218
|
+
|
219
|
+
if (data.error_code === 4) {
|
220
|
+
return this._responseError(errors.QUERY_RESULT_NOT_EXIST, data.result);
|
221
|
+
}
|
222
|
+
|
223
|
+
return this._responseError(errors.FAIL);
|
224
|
+
|
225
|
+
} catch (err) {
|
226
|
+
throw err;
|
227
|
+
}
|
228
|
+
};
|
229
|
+
|
230
|
+
|
231
|
+
proto.getReward = function* (blockNumber) {
|
232
|
+
try {
|
233
|
+
if (!this._verifyValue(blockNumber)) {
|
234
|
+
return this._responseError(errors.INVALID_BLOCKNUMBER_ERROR);
|
235
|
+
}
|
236
|
+
|
237
|
+
const data = yield this._request('get', 'getLedger', {
|
238
|
+
seq: blockNumber,
|
239
|
+
with_block_reward: true,
|
240
|
+
});
|
241
|
+
|
242
|
+
if (data.error_code === 0) {
|
243
|
+
// console.log(data)
|
244
|
+
const result = data.result;
|
245
|
+
const blockReward = result.block_reward;
|
246
|
+
const validatorsReward = result.validators_reward;
|
247
|
+
const validatorItems = [];
|
248
|
+
const keys = Object.keys(validatorsReward);
|
249
|
+
|
250
|
+
if (keys.length > 0) {
|
251
|
+
keys.forEach(item => {
|
252
|
+
validatorItems.push({
|
253
|
+
validator: item,
|
254
|
+
reward: validatorsReward[item],
|
255
|
+
});
|
256
|
+
})
|
257
|
+
}
|
258
|
+
|
259
|
+
return this._responseData({
|
260
|
+
blockReward,
|
261
|
+
validatorsReward: validatorItems,
|
262
|
+
});
|
263
|
+
}
|
264
|
+
|
265
|
+
if (data.error_code === 4) {
|
266
|
+
return this._responseError(errors.QUERY_RESULT_NOT_EXIST, data.result);
|
267
|
+
}
|
268
|
+
|
269
|
+
return this._responseError(errors.FAIL);
|
270
|
+
|
271
|
+
} catch (err) {
|
272
|
+
throw err;
|
273
|
+
}
|
274
|
+
};
|
275
|
+
|
276
|
+
|
277
|
+
proto.getLatestReward = function* () {
|
278
|
+
try {
|
279
|
+
const data = yield this._request('get', 'getLedger', {
|
280
|
+
with_block_reward: true,
|
281
|
+
});
|
282
|
+
|
283
|
+
if (data.error_code === 0) {
|
284
|
+
// console.log(data)
|
285
|
+
const result = data.result;
|
286
|
+
const blockReward = result.block_reward;
|
287
|
+
const validatorsReward = result.validators_reward;
|
288
|
+
const validatorItems = [];
|
289
|
+
const keys = Object.keys(validatorsReward);
|
290
|
+
|
291
|
+
if (keys.length > 0) {
|
292
|
+
keys.forEach(item => {
|
293
|
+
validatorItems.push({
|
294
|
+
validator: item,
|
295
|
+
reward: validatorsReward[item],
|
296
|
+
});
|
297
|
+
})
|
298
|
+
}
|
299
|
+
|
300
|
+
return this._responseData({
|
301
|
+
blockReward,
|
302
|
+
validatorsReward: validatorItems,
|
303
|
+
});
|
304
|
+
}
|
305
|
+
|
306
|
+
if (data.error_code === 4) {
|
307
|
+
return this._responseError(errors.QUERY_RESULT_NOT_EXIST, data.result);
|
308
|
+
}
|
309
|
+
|
310
|
+
return this._responseError(errors.FAIL);
|
311
|
+
|
312
|
+
} catch (err) {
|
313
|
+
throw err;
|
314
|
+
}
|
315
|
+
};
|
316
|
+
|
317
|
+
|
318
|
+
proto.getFees = function* (blockNumber) {
|
319
|
+
try {
|
320
|
+
if (!this._verifyValue(blockNumber)) {
|
321
|
+
return this._responseError(errors.INVALID_BLOCKNUMBER_ERROR);
|
322
|
+
}
|
323
|
+
|
324
|
+
const data = yield this._request('get', 'getLedger', {
|
325
|
+
seq: blockNumber,
|
326
|
+
with_fee: true,
|
327
|
+
});
|
328
|
+
|
329
|
+
if (data.error_code === 0) {
|
330
|
+
// console.log(data)
|
331
|
+
const result = data.result;
|
332
|
+
const fees = result.fees;
|
333
|
+
|
334
|
+
return this._responseData({
|
335
|
+
// fees: humps.camelizeKeys(fees),
|
336
|
+
fees,
|
337
|
+
});
|
338
|
+
}
|
339
|
+
|
340
|
+
if (data.error_code === 4) {
|
341
|
+
return this._responseError(errors.QUERY_RESULT_NOT_EXIST, data.result);
|
342
|
+
}
|
343
|
+
|
344
|
+
return this._responseError(errors.FAIL);
|
345
|
+
|
346
|
+
} catch (err) {
|
347
|
+
throw err;
|
348
|
+
}
|
349
|
+
};
|
350
|
+
|
351
|
+
|
352
|
+
proto.getLatestFees = function* () {
|
353
|
+
try {
|
354
|
+
const data = yield this._request('get', 'getLedger', {
|
355
|
+
with_fee: true,
|
356
|
+
});
|
357
|
+
|
358
|
+
if (data.error_code === 0) {
|
359
|
+
// console.log(data)
|
360
|
+
const result = data.result;
|
361
|
+
const fees = result.fees;
|
362
|
+
|
363
|
+
return this._responseData({
|
364
|
+
// fees: humps.camelizeKeys(fees),
|
365
|
+
fees,
|
366
|
+
});
|
367
|
+
}
|
368
|
+
|
369
|
+
if (data.error_code === 4) {
|
370
|
+
return this._responseError(errors.QUERY_RESULT_NOT_EXIST, data.result);
|
371
|
+
}
|
372
|
+
|
373
|
+
return this._responseError(errors.FAIL);
|
374
|
+
|
375
|
+
} catch (err) {
|
376
|
+
throw err;
|
377
|
+
}
|
378
|
+
};
|
379
|
+
|
380
|
+
wrap(proto);
|
@@ -0,0 +1,264 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const wrap = require('co-wrap-all');
|
4
|
+
const is = require('is-type-of');
|
5
|
+
const merge = require('merge-descriptors');
|
6
|
+
const long = require('long');
|
7
|
+
const humps = require('humps');
|
8
|
+
const JSONbig = require('json-bigint');
|
9
|
+
const errors = require('../exception');
|
10
|
+
|
11
|
+
|
12
|
+
module.exports = Transaction;
|
13
|
+
|
14
|
+
function Transaction(options) {
|
15
|
+
if (!(this instanceof Transaction)) {
|
16
|
+
return new Transaction(options);
|
17
|
+
}
|
18
|
+
|
19
|
+
this.options = options;
|
20
|
+
}
|
21
|
+
|
22
|
+
const proto = Transaction.prototype;
|
23
|
+
|
24
|
+
merge(proto, require('../common/util'));
|
25
|
+
|
26
|
+
proto.buildBlob = function(args) {
|
27
|
+
try {
|
28
|
+
|
29
|
+
if (is.array(args) || !is.object(args)) {
|
30
|
+
return this._responseError(errors.INVALID_ARGUMENTS);
|
31
|
+
}
|
32
|
+
|
33
|
+
const schema = {
|
34
|
+
sourceAddress: {
|
35
|
+
required: true,
|
36
|
+
string: true,
|
37
|
+
address: true,
|
38
|
+
},
|
39
|
+
gasPrice: {
|
40
|
+
required: true,
|
41
|
+
numeric: true,
|
42
|
+
},
|
43
|
+
feeLimit: {
|
44
|
+
required: true,
|
45
|
+
numeric: true,
|
46
|
+
},
|
47
|
+
nonce: {
|
48
|
+
required: true,
|
49
|
+
numeric: true,
|
50
|
+
},
|
51
|
+
ceilLedgerSeq: {
|
52
|
+
required: false,
|
53
|
+
numeric: true,
|
54
|
+
},
|
55
|
+
operations: {
|
56
|
+
required: true,
|
57
|
+
operations: true,
|
58
|
+
},
|
59
|
+
metadata: {
|
60
|
+
required: false,
|
61
|
+
string: true,
|
62
|
+
}
|
63
|
+
};
|
64
|
+
|
65
|
+
if (!this._validate(args, schema).tag) {
|
66
|
+
const msg = this._validate(args, schema).msg;
|
67
|
+
return this._responseError(errors[msg]);
|
68
|
+
}
|
69
|
+
|
70
|
+
return this._responseData(this._buildBlob(args));
|
71
|
+
} catch (err) {
|
72
|
+
throw err;
|
73
|
+
}
|
74
|
+
};
|
75
|
+
|
76
|
+
proto.sign = function(args) {
|
77
|
+
// privateKeys, blob
|
78
|
+
try {
|
79
|
+
|
80
|
+
if (is.array(args) || !is.object(args)) {
|
81
|
+
return this._responseError(errors.INVALID_ARGUMENTS);
|
82
|
+
}
|
83
|
+
|
84
|
+
const schema = {
|
85
|
+
privateKeys: {
|
86
|
+
required: true,
|
87
|
+
privateKeys: true,
|
88
|
+
},
|
89
|
+
blob: {
|
90
|
+
required: true,
|
91
|
+
hex: true,
|
92
|
+
},
|
93
|
+
};
|
94
|
+
|
95
|
+
if (!this._validate(args, schema).tag) {
|
96
|
+
const msg = this._validate(args, schema).msg;
|
97
|
+
return this._responseError(errors[msg]);
|
98
|
+
}
|
99
|
+
|
100
|
+
return this._responseData(this._signBlob(args));
|
101
|
+
} catch (err) {
|
102
|
+
throw err;
|
103
|
+
}
|
104
|
+
|
105
|
+
};
|
106
|
+
|
107
|
+
// blob, signData, publicKey
|
108
|
+
proto.submit = function* (args) {
|
109
|
+
|
110
|
+
if (is.array(args) || !is.object(args)) {
|
111
|
+
return this._responseError(errors.INVALID_ARGUMENTS);
|
112
|
+
}
|
113
|
+
|
114
|
+
// blob, signature
|
115
|
+
const schema = {
|
116
|
+
signature: {
|
117
|
+
required: true,
|
118
|
+
signatures: true,
|
119
|
+
},
|
120
|
+
blob: {
|
121
|
+
required: true,
|
122
|
+
hex: true,
|
123
|
+
},
|
124
|
+
};
|
125
|
+
|
126
|
+
if (!this._validate(args, schema).tag) {
|
127
|
+
const msg = this._validate(args, schema).msg;
|
128
|
+
return this._responseError(errors[msg]);
|
129
|
+
}
|
130
|
+
|
131
|
+
args = humps.decamelizeKeys(args, { separator: '_' });
|
132
|
+
|
133
|
+
return yield this._submit(args);
|
134
|
+
};
|
135
|
+
|
136
|
+
proto.evaluateFee = function* (args) {
|
137
|
+
try {
|
138
|
+
|
139
|
+
if (is.array(args) || !is.object(args)) {
|
140
|
+
return this._responseError(errors.INVALID_ARGUMENTS);
|
141
|
+
}
|
142
|
+
|
143
|
+
let { sourceAddress, nonce, operations, signtureNumber, metadata, ceilLedgerSeq } = args;
|
144
|
+
signtureNumber = signtureNumber || '1';
|
145
|
+
const schema = {
|
146
|
+
sourceAddress: {
|
147
|
+
required: true,
|
148
|
+
string: true,
|
149
|
+
address: true,
|
150
|
+
},
|
151
|
+
nonce: {
|
152
|
+
required: true,
|
153
|
+
string: true,
|
154
|
+
numeric: true,
|
155
|
+
},
|
156
|
+
operations: {
|
157
|
+
required: true,
|
158
|
+
operations: true,
|
159
|
+
},
|
160
|
+
signtureNumber: {
|
161
|
+
required: false,
|
162
|
+
string: true,
|
163
|
+
numeric: true,
|
164
|
+
},
|
165
|
+
metadata: {
|
166
|
+
required: false,
|
167
|
+
string: true,
|
168
|
+
},
|
169
|
+
ceilLedgerSeq: {
|
170
|
+
required: false,
|
171
|
+
numeric: true,
|
172
|
+
},
|
173
|
+
};
|
174
|
+
|
175
|
+
if (!this._validate(args, schema).tag) {
|
176
|
+
const msg = this._validate(args, schema).msg;
|
177
|
+
return this._responseError(errors[msg]);
|
178
|
+
}
|
179
|
+
|
180
|
+
signtureNumber = signtureNumber || 1;
|
181
|
+
|
182
|
+
const operationList = [];
|
183
|
+
|
184
|
+
operations.forEach(item => {
|
185
|
+
const type = item.type;
|
186
|
+
const argsData = item.data;
|
187
|
+
|
188
|
+
const operationItem = this._buildOperation(type, argsData);
|
189
|
+
let operationMsg = humps.decamelizeKeys(operationItem, { separator: '_' });
|
190
|
+
// convert long to int
|
191
|
+
operationMsg = this._longToInt(operationMsg);
|
192
|
+
|
193
|
+
operationList.push(operationMsg);
|
194
|
+
});
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
let data = {
|
199
|
+
items: [
|
200
|
+
{
|
201
|
+
transaction_json: {
|
202
|
+
source_address: sourceAddress,
|
203
|
+
metadata: metadata,
|
204
|
+
nonce: nonce,
|
205
|
+
operations: operationList,
|
206
|
+
ceil_ledger_seq: ceilLedgerSeq,
|
207
|
+
chain_id: this.options.chainId,
|
208
|
+
},
|
209
|
+
}
|
210
|
+
]
|
211
|
+
};
|
212
|
+
|
213
|
+
data = JSONbig.stringify(data);
|
214
|
+
const response = yield this._request('post', 'testTransaction', data);
|
215
|
+
if (is.object(response)) {
|
216
|
+
const info = response;
|
217
|
+
if (info.error_code === 0) {
|
218
|
+
if (info.result.txs && info.result.txs.length > 0) {
|
219
|
+
const fee = info.result.txs[0].transaction_env.transaction;
|
220
|
+
return this._responseData({
|
221
|
+
feeLimit: fee.fee_limit,
|
222
|
+
gasPrice: fee.gas_price,
|
223
|
+
});
|
224
|
+
}
|
225
|
+
}
|
226
|
+
|
227
|
+
return {
|
228
|
+
errorCode: info.error_code,
|
229
|
+
errorDesc: info.error_desc,
|
230
|
+
};
|
231
|
+
}
|
232
|
+
} catch (err) {
|
233
|
+
throw err;
|
234
|
+
}
|
235
|
+
};
|
236
|
+
|
237
|
+
proto.getInfo = function* (hash) {
|
238
|
+
try {
|
239
|
+
|
240
|
+
if (!is.string(hash) || this._isEmptyString(hash)) {
|
241
|
+
return this._responseError(errors.INVALID_HASH_ERROR);
|
242
|
+
}
|
243
|
+
|
244
|
+
const data = yield this._request('get', 'getTransactionHistory', {
|
245
|
+
hash: hash,
|
246
|
+
});
|
247
|
+
|
248
|
+
if (data.error_code === 0) {
|
249
|
+
return this._responseData(data.result);
|
250
|
+
}
|
251
|
+
|
252
|
+
if (data.error_code === 4) {
|
253
|
+
return this._responseError(errors.QUERY_RESULT_NOT_EXIST, data.result);
|
254
|
+
}
|
255
|
+
|
256
|
+
return this._responseError(errors.FAIL);
|
257
|
+
|
258
|
+
} catch (err) {
|
259
|
+
throw err;
|
260
|
+
}
|
261
|
+
};
|
262
|
+
|
263
|
+
|
264
|
+
wrap(proto);
|
@@ -0,0 +1,5 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
module.exports = {
|
4
|
+
v10: `'use strict';let globalAttribute={};function globalAttributeKey(){return'global_attribute';}function loadGlobalAttribute(){if(Object.keys(globalAttribute).length===0){let value=storageLoad(globalAttributeKey());assert(value!==false,'Get global attribute from metadata failed.');globalAttribute=JSON.parse(value);}}function storeGlobalAttribute(){let value=JSON.stringify(globalAttribute);storageStore(globalAttributeKey(),value);}function powerOfBase10(exponent){let i=0;let power=1;while(i<exponent){power=power*10;i=i+1;}return power;}function makeBalanceKey(address){return'balance_'+address;}function makeAllowanceKey(owner,spender){return'allow_'+owner+'_to_'+spender;}function valueCheck(value){if(value.startsWith('-')||value==='0'){return false;}return true;}function approve(spender,value){assert(addressCheck(spender)===true,'Arg-spender is not a valid address.');assert(stoI64Check(value)===true,'Arg-value must be alphanumeric.');assert(valueCheck(value)===true,'Arg-value must be positive number.');let key=makeAllowanceKey(sender,spender);storageStore(key,value);tlog('approve',sender,spender,value);return true;}function allowance(owner,spender){assert(addressCheck(owner)===true,'Arg-owner is not a valid address.');assert(addressCheck(spender)===true,'Arg-spender is not a valid address.');let key=makeAllowanceKey(owner,spender);let value=storageLoad(key);assert(value!==false,'Get allowance '+owner+' to '+spender+' from metadata failed.');return value;}function transfer(to,value){assert(addressCheck(to)===true,'Arg-to is not a valid address.');assert(stoI64Check(value)===true,'Arg-value must be alphanumeric.');assert(valueCheck(value)===true,'Arg-value must be positive number.');if(sender===to){tlog('transfer',sender,to,value);return true;}let senderKey=makeBalanceKey(sender);let senderValue=storageLoad(senderKey);assert(senderValue!==false,'Get balance of '+sender+' from metadata failed.');assert(int64Compare(senderValue,value)>=0,'Balance:'+senderValue+' of sender:'+sender+' < transfer value:'+value+'.');let toKey=makeBalanceKey(to);let toValue=storageLoad(toKey);toValue=(toValue===false)?value:int64Add(toValue,value);storageStore(toKey,toValue);senderValue=int64Sub(senderValue,value);storageStore(senderKey,senderValue);tlog('transfer',sender,to,value);return true;}function assign(to,value){assert(addressCheck(to)===true,'Arg-to is not a valid address.');assert(stoI64Check(value)===true,'Arg-value must be alphanumeric.');assert(valueCheck(value)===true,'Arg-value must be positive number.');if(thisAddress===to){tlog('assign',to,value);return true;}loadGlobalAttribute();assert(sender===globalAttribute.contractOwner,sender+' has no permission to assign contract balance.');assert(int64Compare(globalAttribute.balance,value)>=0,'Balance of contract:'+globalAttribute.balance+' < assign value:'+value+'.');let toKey=makeBalanceKey(to);let toValue=storageLoad(toKey);toValue=(toValue===false)?value:int64Add(toValue,value);storageStore(toKey,toValue);globalAttribute.balance=int64Sub(globalAttribute.balance,value);storeGlobalAttribute();tlog('assign',to,value);return true;}function transferFrom(from,to,value){assert(addressCheck(from)===true,'Arg-from is not a valid address.');assert(addressCheck(to)===true,'Arg-to is not a valid address.');assert(stoI64Check(value)===true,'Arg-value must be alphanumeric.');assert(valueCheck(value)===true,'Arg-value must be positive number.');if(from===to){tlog('transferFrom',sender,from,to,value);return true;}let fromKey=makeBalanceKey(from);let fromValue=storageLoad(fromKey);assert(fromValue!==false,'Get value failed, maybe '+from+' has no value.');assert(int64Compare(fromValue,value)>=0,from+' balance:'+fromValue+' < transfer value:'+value+'.');let allowValue=allowance(from,sender);assert(int64Compare(allowValue,value)>=0,'Allowance value:'+allowValue+' < transfer value:'+value+' from '+from+' to '+to+'.');let toKey=makeBalanceKey(to);let toValue=storageLoad(toKey);toValue=(toValue===false)?value:int64Add(toValue,value);storageStore(toKey,toValue);fromValue=int64Sub(fromValue,value);storageStore(fromKey,fromValue);let allowKey=makeAllowanceKey(from,sender);allowValue=int64Sub(allowValue,value);storageStore(allowKey,allowValue);tlog('transferFrom',sender,from,to,value);return true;}function changeOwner(address){assert(addressCheck(address)===true,'Arg-address is not a valid address.');loadGlobalAttribute();assert(sender===globalAttribute.contractOwner,sender+' has no permission to modify contract ownership.');globalAttribute.contractOwner=address;storeGlobalAttribute();tlog('changeOwner',sender,address);}function name(){return globalAttribute.name;}function symbol(){return globalAttribute.symbol;}function decimals(){return globalAttribute.decimals;}function totalSupply(){return globalAttribute.totalSupply;}function ctp(){return globalAttribute.ctp;}function contractInfo(){return globalAttribute;}function balanceOf(address){assert(addressCheck(address)===true,'Arg-address is not a valid address.');if(address===globalAttribute.contractOwner||address===thisAddress){return globalAttribute.balance;}let key=makeBalanceKey(address);let value=storageLoad(key);assert(value!==false,'Get balance of '+address+' from metadata failed.');return value;}function init(input_str){let input=JSON.parse(input_str);assert(stoI64Check(input.params.supply)===true&&typeof input.params.name==='string'&&typeof input.params.symbol==='string'&&typeof input.params.decimals==='number','Args check failed.');globalAttribute.ctp='1.0';globalAttribute.name=input.params.name;globalAttribute.symbol=input.params.symbol;globalAttribute.decimals=input.params.decimals;globalAttribute.totalSupply=int64Mul(input.params.supply,powerOfBase10(globalAttribute.decimals));globalAttribute.contractOwner=sender;globalAttribute.balance=globalAttribute.totalSupply;storageStore(globalAttributeKey(),JSON.stringify(globalAttribute));}function main(input_str){let input=JSON.parse(input_str);if(input.method==='transfer'){transfer(input.params.to,input.params.value);}else if(input.method==='transferFrom'){transferFrom(input.params.from,input.params.to,input.params.value);}else if(input.method==='approve'){approve(input.params.spender,input.params.value);}else if(input.method==='assign'){assign(input.params.to,input.params.value);}else if(input.method==='changeOwner'){changeOwner(input.params.address);}else{throw'<unidentified operation type>';}}function query(input_str){loadGlobalAttribute();let result={};let input=JSON.parse(input_str);if(input.method==='name'){result.name=name();}else if(input.method==='symbol'){result.symbol=symbol();}else if(input.method==='decimals'){result.decimals=decimals();}else if(input.method==='totalSupply'){result.totalSupply=totalSupply();}else if(input.method==='ctp'){result.ctp=ctp();}else if(input.method==='contractInfo'){result.contractInfo=contractInfo();}else if(input.method==='balanceOf'){result.balance=balanceOf(input.params.address);}else if(input.method==='allowance'){result.allowance=allowance(input.params.owner,input.params.spender);}else{throw'<unidentified operation type>';}log(result);return JSON.stringify(result);}`,
|
5
|
+
};
|
@@ -0,0 +1,55 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const is = require('is-type-of');
|
4
|
+
const protobuf = require('protobufjs');
|
5
|
+
const long = require('long');
|
6
|
+
const tou8 = require('buffer-to-uint8array');
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Account set metadata
|
10
|
+
* @param args
|
11
|
+
* @return {payload}
|
12
|
+
*/
|
13
|
+
module.exports = function (args) {
|
14
|
+
try {
|
15
|
+
|
16
|
+
const { key, value, version, sourceAddress, deleteFlag, metadata } = args;
|
17
|
+
const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
|
18
|
+
const setMetadata = root.lookupType('protocol.OperationSetMetadata');
|
19
|
+
|
20
|
+
const opt = {
|
21
|
+
key,
|
22
|
+
value,
|
23
|
+
};
|
24
|
+
|
25
|
+
if (version) {
|
26
|
+
opt.version = parseInt(version);
|
27
|
+
}
|
28
|
+
|
29
|
+
if (deleteFlag) {
|
30
|
+
opt.deleteFlag = deleteFlag;
|
31
|
+
}
|
32
|
+
|
33
|
+
const setMetadataMsg = setMetadata.create(opt);
|
34
|
+
const operation = root.lookupType('protocol.Operation');
|
35
|
+
const payload = {
|
36
|
+
setMetadata: setMetadataMsg,
|
37
|
+
type: operation.Type.SET_METADATA,
|
38
|
+
sourceAddress,
|
39
|
+
};
|
40
|
+
|
41
|
+
if (metadata) {
|
42
|
+
payload.metadata = metadata;
|
43
|
+
}
|
44
|
+
|
45
|
+
const err = operation.verify(payload);
|
46
|
+
|
47
|
+
if (err) {
|
48
|
+
throw Error(err);
|
49
|
+
}
|
50
|
+
|
51
|
+
return operation.create(payload);
|
52
|
+
} catch (err) {
|
53
|
+
throw err;
|
54
|
+
}
|
55
|
+
};
|