koilib 2.6.0 → 2.8.0
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/README.md +5 -5
- package/dist/koinos.js +178 -688
- package/dist/koinos.min.js +1 -1
- package/lib/Contract.d.ts +9 -11
- package/lib/Contract.js +12 -12
- package/lib/Contract.js.map +1 -1
- package/lib/Provider.d.ts +24 -17
- package/lib/Provider.js +83 -81
- package/lib/Provider.js.map +1 -1
- package/lib/Serializer.d.ts +6 -2
- package/lib/Serializer.js +10 -4
- package/lib/Serializer.js.map +1 -1
- package/lib/Signer.d.ts +24 -10
- package/lib/Signer.js +71 -24
- package/lib/Signer.js.map +1 -1
- package/lib/browser/Contract.d.ts +200 -0
- package/lib/browser/Contract.js +298 -0
- package/lib/browser/Contract.js.map +1 -0
- package/lib/browser/Provider.d.ts +160 -0
- package/lib/browser/Provider.js +246 -0
- package/lib/browser/Provider.js.map +1 -0
- package/lib/browser/Serializer.d.ts +85 -0
- package/lib/browser/Serializer.js +175 -0
- package/lib/browser/Serializer.js.map +1 -0
- package/lib/browser/Signer.d.ts +310 -0
- package/lib/browser/Signer.js +468 -0
- package/lib/browser/Signer.js.map +1 -0
- package/lib/browser/index.d.ts +7 -0
- package/lib/browser/index.js +34 -0
- package/lib/browser/index.js.map +1 -0
- package/lib/browser/index2.d.ts +2 -0
- package/lib/browser/index2.js +33 -0
- package/lib/browser/index2.js.map +1 -0
- package/lib/browser/interface.d.ts +279 -0
- package/lib/browser/interface.js +3 -0
- package/lib/browser/interface.js.map +1 -0
- package/lib/browser/jsonDescriptors/krc20-proto.json +183 -0
- package/lib/browser/jsonDescriptors/protocol-proto.json +246 -0
- package/lib/browser/utils.d.ts +326 -0
- package/lib/browser/utils.js +252 -0
- package/lib/browser/utils.js.map +1 -0
- package/lib/interface.d.ts +22 -21
- package/package.json +9 -6
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Contract = void 0;
|
|
4
|
+
const Serializer_1 = require("./Serializer");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
/**
|
|
7
|
+
* The contract class contains the contract ID and contract entries
|
|
8
|
+
* definition needed to encode/decode operations during the
|
|
9
|
+
* interaction with the user and the communication with the RPC node.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const { Contract, Provider, Signer, utils } = require("koilib");
|
|
15
|
+
* const rpcNodes = ["http://api.koinos.io:8080"];
|
|
16
|
+
* const privateKey = "f186a5de49797bfd52dc42505c33d75a46822ed5b60046e09d7c336242e20200";
|
|
17
|
+
* const provider = new Provider(rpcNodes);
|
|
18
|
+
* const signer = new Signer({ privateKey, provider });
|
|
19
|
+
* const koinContract = new Contract({
|
|
20
|
+
* id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
|
|
21
|
+
* abi: utils.Krc20Abi,
|
|
22
|
+
* provider,
|
|
23
|
+
* signer,
|
|
24
|
+
* });
|
|
25
|
+
* const koin = koinContract.functions;
|
|
26
|
+
*
|
|
27
|
+
* // optional: preformat input/output
|
|
28
|
+
* koinContract.abi.methods.balanceOf.preformatInput = (owner) =>
|
|
29
|
+
* ({ owner });
|
|
30
|
+
* koinContract.abi.methods.balanceOf.preformatOutput = (res) =>
|
|
31
|
+
* utils.formatUnits(res.value, 8);
|
|
32
|
+
* koinContract.abi.methods.transfer.preformatInput = (input) => ({
|
|
33
|
+
* from: signer.getAddress(),
|
|
34
|
+
* to: input.to,
|
|
35
|
+
* value: utils.parseUnits(input.value, 8),
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* async funtion main() {
|
|
39
|
+
* // Get balance
|
|
40
|
+
* const { result } = await koin.balanceOf("12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD");
|
|
41
|
+
* console.log(result)
|
|
42
|
+
*
|
|
43
|
+
* // Transfer
|
|
44
|
+
* const { transaction } = await koin.transfer({
|
|
45
|
+
* to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
|
|
46
|
+
* value: "10.0001",
|
|
47
|
+
* });
|
|
48
|
+
* console.log(`Transaction id ${transaction.id} submitted`);
|
|
49
|
+
*
|
|
50
|
+
* // wait to be mined
|
|
51
|
+
* const blockNumber = await transaction.wait();
|
|
52
|
+
* console.log(`Transaction mined. Block number: ${blockNumber}`);
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* main();
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
class Contract {
|
|
59
|
+
constructor(c) {
|
|
60
|
+
var _a;
|
|
61
|
+
if (c.id)
|
|
62
|
+
this.id = (0, utils_1.decodeBase58)(c.id);
|
|
63
|
+
this.signer = c.signer;
|
|
64
|
+
this.provider = c.provider || ((_a = c.signer) === null || _a === void 0 ? void 0 : _a.provider);
|
|
65
|
+
this.abi = c.abi;
|
|
66
|
+
this.bytecode = c.bytecode;
|
|
67
|
+
if (c.serializer) {
|
|
68
|
+
this.serializer = c.serializer;
|
|
69
|
+
}
|
|
70
|
+
else if (c.abi && c.abi.types) {
|
|
71
|
+
this.serializer = new Serializer_1.Serializer(c.abi.types);
|
|
72
|
+
}
|
|
73
|
+
this.options = {
|
|
74
|
+
rc_limit: 1e8,
|
|
75
|
+
sendTransaction: true,
|
|
76
|
+
sendAbis: true,
|
|
77
|
+
...c.options,
|
|
78
|
+
};
|
|
79
|
+
this.functions = {};
|
|
80
|
+
if (this.signer &&
|
|
81
|
+
this.provider &&
|
|
82
|
+
this.abi &&
|
|
83
|
+
this.abi.methods &&
|
|
84
|
+
this.serializer) {
|
|
85
|
+
Object.keys(this.abi.methods).forEach((name) => {
|
|
86
|
+
this.functions[name] = async (argu = {}, options) => {
|
|
87
|
+
if (!this.provider)
|
|
88
|
+
throw new Error("provider not found");
|
|
89
|
+
if (!this.abi || !this.abi.methods)
|
|
90
|
+
throw new Error("Methods are not defined");
|
|
91
|
+
if (!this.abi.methods[name])
|
|
92
|
+
throw new Error(`Method ${name} not defined in the ABI`);
|
|
93
|
+
const opts = {
|
|
94
|
+
...this.options,
|
|
95
|
+
...options,
|
|
96
|
+
};
|
|
97
|
+
const { readOnly, output, defaultOutput, preformatInput, preformatOutput, } = this.abi.methods[name];
|
|
98
|
+
let args;
|
|
99
|
+
if (typeof preformatInput === "function") {
|
|
100
|
+
args = preformatInput(argu);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
args = argu;
|
|
104
|
+
}
|
|
105
|
+
const operation = await this.encodeOperation({ name, args });
|
|
106
|
+
if (readOnly) {
|
|
107
|
+
if (!output)
|
|
108
|
+
throw new Error(`No output defined for ${name}`);
|
|
109
|
+
// read contract
|
|
110
|
+
const { result: resultEncoded } = await this.provider.readContract({
|
|
111
|
+
contract_id: (0, utils_1.encodeBase58)(operation.call_contract.contract_id),
|
|
112
|
+
entry_point: operation.call_contract.entry_point,
|
|
113
|
+
args: (0, utils_1.encodeBase64)(operation.call_contract.args),
|
|
114
|
+
});
|
|
115
|
+
let result = defaultOutput;
|
|
116
|
+
if (resultEncoded) {
|
|
117
|
+
result = await this.serializer.deserialize(resultEncoded, output);
|
|
118
|
+
}
|
|
119
|
+
if (typeof preformatOutput === "function") {
|
|
120
|
+
result = preformatOutput(result);
|
|
121
|
+
}
|
|
122
|
+
return { operation, result };
|
|
123
|
+
}
|
|
124
|
+
// return operation if send is false
|
|
125
|
+
if (!(opts === null || opts === void 0 ? void 0 : opts.sendTransaction))
|
|
126
|
+
return { operation };
|
|
127
|
+
// write contract (sign and send)
|
|
128
|
+
if (!this.signer)
|
|
129
|
+
throw new Error("signer not found");
|
|
130
|
+
const tx = await this.signer.encodeTransaction({
|
|
131
|
+
...opts,
|
|
132
|
+
operations: [operation],
|
|
133
|
+
});
|
|
134
|
+
const abis = {};
|
|
135
|
+
if (opts === null || opts === void 0 ? void 0 : opts.sendAbis) {
|
|
136
|
+
const contractId = (0, utils_1.encodeBase58)(this.id);
|
|
137
|
+
abis[contractId] = this.abi;
|
|
138
|
+
}
|
|
139
|
+
const transaction = await this.signer.sendTransaction(tx, abis);
|
|
140
|
+
return { operation, transaction };
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Compute contract Id
|
|
147
|
+
*/
|
|
148
|
+
static computeContractId(address) {
|
|
149
|
+
return (0, utils_1.decodeBase58)(address);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get contract Id
|
|
153
|
+
*/
|
|
154
|
+
getId() {
|
|
155
|
+
if (!this.id)
|
|
156
|
+
throw new Error("id is not defined");
|
|
157
|
+
return (0, utils_1.encodeBase58)(this.id);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Function to deploy a new smart contract.
|
|
161
|
+
* The Bytecode must be defined in the constructor of the class
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* const privateKey = "f186a5de49797bfd52dc42505c33d75a46822ed5b60046e09d7c336242e20200";
|
|
165
|
+
* const provider = new Provider(["http://api.koinos.io:8080"]);
|
|
166
|
+
* const signer = new Signer({ privateKey, provider });
|
|
167
|
+
* const bytecode = new Uint8Array([1, 2, 3, 4]);
|
|
168
|
+
* const contract = new Contract({ signer, provider, bytecode });
|
|
169
|
+
* const { transaction } = await contract.deploy();
|
|
170
|
+
* // wait to be mined
|
|
171
|
+
* const blockNumber = await transaction.wait();
|
|
172
|
+
* console.log(`Contract uploaded in block number ${blockNumber}`);
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
async deploy(options) {
|
|
176
|
+
if (!this.signer)
|
|
177
|
+
throw new Error("signer not found");
|
|
178
|
+
if (!this.bytecode)
|
|
179
|
+
throw new Error("bytecode not found");
|
|
180
|
+
const opts = {
|
|
181
|
+
...this.options,
|
|
182
|
+
...options,
|
|
183
|
+
};
|
|
184
|
+
const operation = {
|
|
185
|
+
upload_contract: {
|
|
186
|
+
contract_id: Contract.computeContractId(this.signer.getAddress()),
|
|
187
|
+
bytecode: this.bytecode,
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
// return operation if send is false
|
|
191
|
+
if (!(opts === null || opts === void 0 ? void 0 : opts.sendTransaction))
|
|
192
|
+
return { operation };
|
|
193
|
+
const tx = await this.signer.encodeTransaction({
|
|
194
|
+
...opts,
|
|
195
|
+
operations: [operation],
|
|
196
|
+
});
|
|
197
|
+
const transaction = await this.signer.sendTransaction(tx);
|
|
198
|
+
return { operation, transaction };
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Encondes a contract operation using Koinos serialization
|
|
202
|
+
* and taking the contract entries as reference to build it
|
|
203
|
+
* @param op - Operation to encode
|
|
204
|
+
* @returns Operation encoded
|
|
205
|
+
* @example
|
|
206
|
+
* ```ts
|
|
207
|
+
* const opEncoded = contract.encodeOperation({
|
|
208
|
+
* name: "transfer",
|
|
209
|
+
* args: {
|
|
210
|
+
* from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
|
|
211
|
+
* to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
|
|
212
|
+
* value: "1000",
|
|
213
|
+
* }
|
|
214
|
+
* });
|
|
215
|
+
*
|
|
216
|
+
* console.log(opEncoded);
|
|
217
|
+
* // {
|
|
218
|
+
* // call_contract: {
|
|
219
|
+
* // contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
|
|
220
|
+
* // entry_point: 0x62efa292,
|
|
221
|
+
* // args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
|
|
222
|
+
* // }
|
|
223
|
+
* // }
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
async encodeOperation(op) {
|
|
227
|
+
if (!this.abi || !this.abi.methods || !this.abi.methods[op.name])
|
|
228
|
+
throw new Error(`Operation ${op.name} unknown`);
|
|
229
|
+
if (!this.serializer)
|
|
230
|
+
throw new Error("Serializer is not defined");
|
|
231
|
+
if (!this.id)
|
|
232
|
+
throw new Error("Contract id is not defined");
|
|
233
|
+
const method = this.abi.methods[op.name];
|
|
234
|
+
let bufferInputs = new Uint8Array(0);
|
|
235
|
+
if (method.input) {
|
|
236
|
+
if (!op.args)
|
|
237
|
+
throw new Error(`No arguments defined for type '${method.input}'`);
|
|
238
|
+
bufferInputs = await this.serializer.serialize(op.args, method.input);
|
|
239
|
+
}
|
|
240
|
+
return {
|
|
241
|
+
call_contract: {
|
|
242
|
+
contract_id: this.id,
|
|
243
|
+
entry_point: method.entryPoint,
|
|
244
|
+
args: bufferInputs,
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Decodes a contract operation to be human readable
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* const opDecoded = contract.decodeOperation({
|
|
253
|
+
* call_contract: {
|
|
254
|
+
* contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
|
|
255
|
+
* entry_point: 0x62efa292,
|
|
256
|
+
* args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
|
|
257
|
+
* }
|
|
258
|
+
* });
|
|
259
|
+
* console.log(opDecoded);
|
|
260
|
+
* // {
|
|
261
|
+
* // name: "transfer",
|
|
262
|
+
* // args: {
|
|
263
|
+
* // from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
|
|
264
|
+
* // to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
|
|
265
|
+
* // value: "1000",
|
|
266
|
+
* // },
|
|
267
|
+
* // }
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
async decodeOperation(op) {
|
|
271
|
+
if (!this.id)
|
|
272
|
+
throw new Error("Contract id is not defined");
|
|
273
|
+
if (!this.abi || !this.abi.methods)
|
|
274
|
+
throw new Error("Methods are not defined");
|
|
275
|
+
if (!this.serializer)
|
|
276
|
+
throw new Error("Serializer is not defined");
|
|
277
|
+
if (!op.call_contract)
|
|
278
|
+
throw new Error("Operation is not CallContractOperation");
|
|
279
|
+
if ((0, utils_1.encodeBase58)(op.call_contract.contract_id) !== (0, utils_1.encodeBase58)(this.id))
|
|
280
|
+
throw new Error(`Invalid contract id. Expected: ${(0, utils_1.encodeBase58)(this.id)}. Received: ${(0, utils_1.encodeBase58)(op.call_contract.contract_id)}`);
|
|
281
|
+
for (let i = 0; i < Object.keys(this.abi.methods).length; i += 1) {
|
|
282
|
+
const opName = Object.keys(this.abi.methods)[i];
|
|
283
|
+
const method = this.abi.methods[opName];
|
|
284
|
+
if (op.call_contract.entry_point === method.entryPoint) {
|
|
285
|
+
if (!method.input)
|
|
286
|
+
return { name: opName };
|
|
287
|
+
return {
|
|
288
|
+
name: opName,
|
|
289
|
+
args: await this.serializer.deserialize(op.call_contract.args, method.input),
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
throw new Error(`Unknown method id ${op.call_contract.entry_point}`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
exports.Contract = Contract;
|
|
297
|
+
exports.default = Contract;
|
|
298
|
+
//# sourceMappingURL=Contract.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Contract.js","sourceRoot":"","sources":["../../src/Contract.ts"],"names":[],"mappings":";;;AAGA,6CAA0C;AAS1C,mCAAmE;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,MAAa,QAAQ;IA4DnB,YAAY,CAcX;;QACC,IAAI,CAAC,CAAC,EAAE;YAAE,IAAI,CAAC,EAAE,GAAG,IAAA,oBAAY,EAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACvC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC;QACvB,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,KAAI,MAAA,CAAC,CAA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CAAC,aAAa,CAAC,WAAW,CAAC,KAAK,IAAA,oBAAY,EAAC,IAAI,CAAC,EAAE,CAAC;YACtE,MAAM,IAAI,KAAK,CACb,kCAAkC,IAAA,oBAAY,EAC5C,IAAI,CAAC,EAAE,CACR,eAAe,IAAA,oBAAY,EAAC,EAAE,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE,CAC7D,CAAC;QACJ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;YAChE,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;YAChD,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YACxC,IAAI,EAAE,CAAC,aAAa,CAAC,WAAW,KAAK,MAAM,CAAC,UAAU,EAAE;gBACtD,IAAI,CAAC,MAAM,CAAC,KAAK;oBAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;gBAC3C,OAAO;oBACL,IAAI,EAAE,MAAM;oBACZ,IAAI,EAAE,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CACrC,EAAE,CAAC,aAAa,CAAC,IAAI,EACrB,MAAM,CAAC,KAAK,CACb;iBACF,CAAC;aACH;SACF;QACD,MAAM,IAAI,KAAK,CAAC,qBAAqB,EAAE,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC,CAAC;IACvE,CAAC;CACF;AArVD,4BAqVC;AAED,kBAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { BlockJson, TransactionJson, CallContractOperationJson } from "./interface";
|
|
2
|
+
/**
|
|
3
|
+
* Class to connect with the RPC node
|
|
4
|
+
*/
|
|
5
|
+
export declare class Provider {
|
|
6
|
+
/**
|
|
7
|
+
* Array of URLs of RPC nodes
|
|
8
|
+
*/
|
|
9
|
+
rpcNodes: string[];
|
|
10
|
+
/**
|
|
11
|
+
* Function triggered when a node is down. Returns a
|
|
12
|
+
* boolean determining if the call should be aborted.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const provider = new Provider([
|
|
17
|
+
* "http://45.56.104.152:8080",
|
|
18
|
+
* "http://159.203.119.0:8080"
|
|
19
|
+
* ]);
|
|
20
|
+
*
|
|
21
|
+
* provider.onError = (error, node, newNode) => {
|
|
22
|
+
* console.log(`Error from node ${node}: ${error.message}`);
|
|
23
|
+
* console.log(`changing node to ${newNode}`);
|
|
24
|
+
* const abort = false;
|
|
25
|
+
* return abort;
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
onError: (error: Error,
|
|
30
|
+
/** node that threw the error */
|
|
31
|
+
currentNode: string,
|
|
32
|
+
/** node used for the next iteration */
|
|
33
|
+
newNode: string) => boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Index of current node in rpcNodes
|
|
36
|
+
*/
|
|
37
|
+
currentNodeId: number;
|
|
38
|
+
/**
|
|
39
|
+
*
|
|
40
|
+
* @param rpcNodes - URL of the rpc node, or array of urls
|
|
41
|
+
* to switch between them when someone is down
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const provider = new Provider([
|
|
45
|
+
* "http://45.56.104.152:8080",
|
|
46
|
+
* "http://159.203.119.0:8080"
|
|
47
|
+
* ]);
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
constructor(rpcNodes: string | string[]);
|
|
51
|
+
/**
|
|
52
|
+
* Function to make jsonrpc requests to the RPC node
|
|
53
|
+
* @param method - jsonrpc method
|
|
54
|
+
* @param params - jsonrpc params
|
|
55
|
+
* @returns Result of jsonrpc response
|
|
56
|
+
*/
|
|
57
|
+
call<T = unknown>(method: string, params: unknown): Promise<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Function to call "chain.get_account_nonce" to return the number of
|
|
60
|
+
* transactions for a particular account. This call is used
|
|
61
|
+
* when creating new transactions.
|
|
62
|
+
* @param account - account address
|
|
63
|
+
* @returns Nonce
|
|
64
|
+
*/
|
|
65
|
+
getNonce(account: string): Promise<number>;
|
|
66
|
+
getAccountRc(account: string): Promise<string>;
|
|
67
|
+
/**
|
|
68
|
+
* Get transactions by id and their corresponding block ids
|
|
69
|
+
*/
|
|
70
|
+
getTransactionsById(transactionIds: string[]): Promise<{
|
|
71
|
+
transactions: {
|
|
72
|
+
transaction: TransactionJson[];
|
|
73
|
+
containing_blocks: string[];
|
|
74
|
+
}[];
|
|
75
|
+
}>;
|
|
76
|
+
getBlocksById(blockIds: string[]): Promise<{
|
|
77
|
+
block_items: {
|
|
78
|
+
block_id: string;
|
|
79
|
+
block_height: string;
|
|
80
|
+
block: BlockJson;
|
|
81
|
+
}[];
|
|
82
|
+
}>;
|
|
83
|
+
/**
|
|
84
|
+
* Function to get info from the head block in the blockchain
|
|
85
|
+
*/
|
|
86
|
+
getHeadInfo(): Promise<{
|
|
87
|
+
head_topology: {
|
|
88
|
+
id: string;
|
|
89
|
+
height: string;
|
|
90
|
+
previous: string;
|
|
91
|
+
};
|
|
92
|
+
last_irreversible_block: string;
|
|
93
|
+
}>;
|
|
94
|
+
/**
|
|
95
|
+
* Function to get consecutive blocks in descending order
|
|
96
|
+
* @param height - Starting block height
|
|
97
|
+
* @param numBlocks - Number of blocks to fetch
|
|
98
|
+
* @param idRef - Block ID reference to speed up searching blocks.
|
|
99
|
+
* This ID must be from a greater block height. By default it
|
|
100
|
+
* gets the ID from the block head.
|
|
101
|
+
*/
|
|
102
|
+
getBlocks(height: number, numBlocks?: number, idRef?: string): Promise<{
|
|
103
|
+
block_id: string;
|
|
104
|
+
block_height: string;
|
|
105
|
+
block: BlockJson;
|
|
106
|
+
block_receipt: {
|
|
107
|
+
[x: string]: unknown;
|
|
108
|
+
};
|
|
109
|
+
}[]>;
|
|
110
|
+
/**
|
|
111
|
+
* Function to get a block by its height
|
|
112
|
+
*/
|
|
113
|
+
getBlock(height: number): Promise<{
|
|
114
|
+
block_id: string;
|
|
115
|
+
block_height: string;
|
|
116
|
+
block: BlockJson;
|
|
117
|
+
block_receipt: {
|
|
118
|
+
[x: string]: unknown;
|
|
119
|
+
};
|
|
120
|
+
}>;
|
|
121
|
+
/**
|
|
122
|
+
* Function to wait for a transaction to be mined.
|
|
123
|
+
* @param txId - transaction id
|
|
124
|
+
* @param type - Type must be "byBlock" (default) or "byTransactionId".
|
|
125
|
+
* _byBlock_ will query the blockchain to get blocks and search for the
|
|
126
|
+
* transaction there. _byTransactionId_ will query the "transaction store"
|
|
127
|
+
* microservice to search the transaction by its id. If non of them is
|
|
128
|
+
* specified the function will use "byBlock" (as "byTransactionId"
|
|
129
|
+
* requires the transaction store, which is an optional microservice).
|
|
130
|
+
*
|
|
131
|
+
* When _byBlock_ is used it returns the block number.
|
|
132
|
+
*
|
|
133
|
+
* When _byTransactionId_ is used it returns the block id.
|
|
134
|
+
*
|
|
135
|
+
* @param timeout - Timeout in milliseconds. By default it is 30000
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* const blockNumber = await provider.wait(txId);
|
|
139
|
+
* // const blockNumber = await provider.wait(txId, "byBlock", 30000);
|
|
140
|
+
* // const blockId = await provider.wait(txId, "byTransactionId", 30000);
|
|
141
|
+
* console.log("Transaction mined")
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
wait(txId: string, type?: "byTransactionId" | "byBlock", timeout?: number): Promise<string | number>;
|
|
145
|
+
/**
|
|
146
|
+
* Function to call "chain.submit_transaction" to send a signed
|
|
147
|
+
* transaction to the blockchain.
|
|
148
|
+
*/
|
|
149
|
+
sendTransaction(transaction: TransactionJson): Promise<{}>;
|
|
150
|
+
/**
|
|
151
|
+
* Function to call "chain.read_contract" to read a contract.
|
|
152
|
+
* This function is used by [[Contract]] class when read methods
|
|
153
|
+
* are invoked.
|
|
154
|
+
*/
|
|
155
|
+
readContract(operation: CallContractOperationJson): Promise<{
|
|
156
|
+
result: string;
|
|
157
|
+
logs: string;
|
|
158
|
+
}>;
|
|
159
|
+
}
|
|
160
|
+
export default Provider;
|