koilib 1.5.0 → 2.3.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/lib/Contract.js CHANGED
@@ -1,129 +1,201 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Contract = void 0;
4
- const abi_1 = require("./abi");
5
- const serializer_1 = require("./serializer");
6
- const VariableBlob_1 = require("./VariableBlob");
4
+ const Serializer_1 = require("./Serializer");
5
+ const utils_1 = require("./utils");
7
6
  /**
8
- * The contract class contains the contract ID and contrac entries
7
+ * The contract class contains the contract ID and contract entries
9
8
  * definition needed to encode/decode operations during the
10
9
  * interaction with the user and the communication with the RPC node.
11
10
  *
12
- * Operations are encoded to communicate with the RPC node. However,
13
- * this format is not human readable as the data is serialized and
14
- * encoded in Base64 format. When decoding operations, they can be
15
- * read by the user (see [[EncodedOperation]] and [[DecodedOperation]]).
16
- *
17
11
  * @example
18
12
  *
19
13
  * ```ts
20
- * const contract = new Contract({
21
- * id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
22
- * entries: {
23
- * transfer: {
24
- * id: 0x62efa292,
25
- * inputs: {
26
- * type: [
27
- * {
28
- * name: "from",
29
- * type: "string",
30
- * },
31
- * {
32
- * name: "to",
33
- * type: "string",
34
- * },
35
- * {
36
- * name: "value",
37
- * type: "uint64",
38
- * },
39
- * ],
40
- * },
41
- * },
42
- * balance_of: {
43
- * id: 0x15619248,
44
- * inputs: { type: "string" },
45
- * outputs: { type: "uint64" },
46
- * },
47
- * },
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,
48
24
  * });
25
+ * const koin = koinContract.functions;
49
26
  *
50
- * const opEncoded = contract.encodeOperation({
51
- * name: "transfer",
52
- * args: {
53
- * from: "alice",
54
- * to: "bob",
55
- * value: BigInt(1000),
56
- * },
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),
57
36
  * });
58
37
  *
59
- * console.log(opEncoded);
60
- * // {
61
- * // type: "koinos::protocol::call_contract_operation",
62
- * // value: {
63
- * // contract_id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
64
- * // entry_point: 0x62efa292,
65
- * // args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
66
- * // }
67
- * // }
38
+ * async funtion main() {
39
+ * // Get balance
40
+ * const { result } = await koin.balanceOf("12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD");
41
+ * console.log(result)
42
+ *
43
+ * // Transfer
44
+ * const { transaction, transactionResponse } = await koin.transfer({
45
+ * to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
46
+ * value: "10.0001",
47
+ * });
48
+ * console.log(`Transaction id ${transaction.id} submitted`);
68
49
  *
69
- * const opDecoded = contract.decodeOperation(opEncoded);
70
- * console.log(opDecoded);
71
- * // {
72
- * // name: "transfer",
73
- * // args: {
74
- * // from: "alice",
75
- * // to: "bob",
76
- * // value: 1000n,
77
- * // },
78
- * // }
50
+ * // wait to be mined
51
+ * const blockId = await transactionResponse.wait();
52
+ * console.log(`Transaction mined. Block id: ${blockId}`);
53
+ * }
79
54
  *
80
- * const resultDecoded = contract.decodeResult("MAAsZnAzyD0E=", "balance_of");
81
- * console.log(resultDecoded)
82
- * // 3124382766600001n
55
+ * main();
83
56
  * ```
84
57
  */
85
58
  class Contract {
59
+ constructor(c) {
60
+ var _a;
61
+ if (c.id)
62
+ this.id = 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: utils_1.encodeBase58(operation.call_contract.contract_id),
112
+ entry_point: operation.call_contract.entry_point,
113
+ args: 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 transaction = 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 = utils_1.encodeBase58(this.id);
137
+ abis[contractId] = this.abi;
138
+ }
139
+ const transactionResponse = await this.signer.sendTransaction(transaction, abis);
140
+ return { operation, transaction, transactionResponse };
141
+ };
142
+ });
143
+ }
144
+ }
86
145
  /**
87
- * The constructor receives the contract ID and
88
- * contract entries definition
89
- * @param c - Object with contract id and contract entries
90
- *
146
+ * Compute contract Id
147
+ */
148
+ static computeContractId(address) {
149
+ return 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 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
91
162
  * @example
92
163
  * ```ts
93
- * const contract = new Contract({
94
- * id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
95
- * entries: {
96
- * transfer: {
97
- * id: 0x62efa292,
98
- * inputs: {
99
- * type: [
100
- * {
101
- * name: "from",
102
- * type: "string",
103
- * },
104
- * {
105
- * name: "to",
106
- * type: "string",
107
- * },
108
- * {
109
- * name: "value",
110
- * type: "uint64",
111
- * },
112
- * ],
113
- * },
114
- * },
115
- * balance_of: {
116
- * id: 0x15619248,
117
- * inputs: { type: "string" },
118
- * outputs: { type: "uint64" },
119
- * },
120
- * },
121
- * });
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 { transactionResponse } = await contract.deploy();
170
+ * // wait to be mined
171
+ * const blockId = await transactionResponse.wait();
172
+ * console.log(`Contract uploaded in block id ${blockId}`);
122
173
  * ```
123
174
  */
124
- constructor(c) {
125
- this.id = c.id;
126
- this.entries = c.entries;
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 transaction = await this.signer.encodeTransaction({
194
+ ...opts,
195
+ operations: [operation],
196
+ });
197
+ const transactionResponse = await this.signer.sendTransaction(transaction);
198
+ return { operation, transaction, transactionResponse };
127
199
  }
128
200
  /**
129
201
  * Encondes a contract operation using Koinos serialization
@@ -135,35 +207,41 @@ class Contract {
135
207
  * const opEncoded = contract.encodeOperation({
136
208
  * name: "transfer",
137
209
  * args: {
138
- * from: "alice",
139
- * to: "bob",
140
- * value: 1000,
210
+ * from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
211
+ * to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
212
+ * value: "1000",
141
213
  * }
142
214
  * });
143
215
  *
144
216
  * console.log(opEncoded);
145
217
  * // {
146
- * // type: "koinos::protocol::call_contract_operation",
147
- * // value: {
148
- * // contract_id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
218
+ * // call_contract: {
219
+ * // contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
149
220
  * // entry_point: 0x62efa292,
150
221
  * // args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
151
222
  * // }
152
223
  * // }
153
224
  * ```
154
225
  */
155
- encodeOperation(op) {
156
- if (!this.entries || !this.entries[op.name])
226
+ async encodeOperation(op) {
227
+ if (!this.abi || !this.abi.methods || !this.abi.methods[op.name])
157
228
  throw new Error(`Operation ${op.name} unknown`);
158
- const entry = this.entries[op.name];
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
+ }
159
240
  return {
160
- type: abi_1.abiCallContractOperation.name,
161
- value: {
241
+ call_contract: {
162
242
  contract_id: this.id,
163
- entry_point: entry.id,
164
- ...(entry.inputs && {
165
- args: serializer_1.serialize(op.args, entry.inputs).toString(),
166
- }),
243
+ entry_point: method.entryPoint,
244
+ args: bufferInputs,
167
245
  },
168
246
  };
169
247
  }
@@ -172,9 +250,8 @@ class Contract {
172
250
  * @example
173
251
  * ```ts
174
252
  * const opDecoded = contract.decodeOperation({
175
- * type: "koinos::protocol::call_contract_operation",
176
- * value: {
177
- * contract_id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
253
+ * call_contract: {
254
+ * contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
178
255
  * entry_point: 0x62efa292,
179
256
  * args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
180
257
  * }
@@ -183,49 +260,37 @@ class Contract {
183
260
  * // {
184
261
  * // name: "transfer",
185
262
  * // args: {
186
- * // from: "alice",
187
- * // to: "bob",
188
- * // value: 1000n,
263
+ * // from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
264
+ * // to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
265
+ * // value: "1000",
189
266
  * // },
190
267
  * // }
191
268
  * ```
192
269
  */
193
- decodeOperation(op) {
194
- if (op.value.contract_id !== this.id)
195
- throw new Error(`Invalid contract id. Expected: ${this.id}. Received: ${op.value.contract_id}`);
196
- for (let i = 0; i < Object.keys(this.entries).length; i += 1) {
197
- const opName = Object.keys(this.entries)[i];
198
- const entry = this.entries[opName];
199
- if (op.value.entry_point === entry.id) {
200
- const vb = new VariableBlob_1.VariableBlob(op.value.args);
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 (op.call_contract.contract_id !== this.id)
280
+ throw new Error(`Invalid contract id. Expected: ${utils_1.encodeBase58(this.id)}. Received: ${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 };
201
287
  return {
202
288
  name: opName,
203
- args: serializer_1.deserialize(vb, entry.inputs),
289
+ args: await this.serializer.deserialize(op.call_contract.args, method.input),
204
290
  };
205
291
  }
206
292
  }
207
- throw new Error(`Unknown entry id ${op.value.entry_point}`);
208
- }
209
- /**
210
- * Decodes a result. This function is used in conjunction with
211
- * [[Provider.readContract | readContract of Provider class]] to read a
212
- * contract and decode the result. "outputs" field must be defined in
213
- * the abi for the operation name.
214
- * @param result - Encoded result in base64
215
- * @param opName - Operation name
216
- * @returns Decoded result
217
- * @example
218
- * ```ts
219
- * const resultDecoded = contract.decodeResult("MAAsZnAzyD0E=", "balance_of");
220
- * console.log(resultDecoded)
221
- * // 3124382766600001n
222
- * ```
223
- */
224
- decodeResult(result, opName) {
225
- const entry = this.entries[opName];
226
- if (!entry.outputs)
227
- throw new Error(`There are no outputs defined for ${opName}`);
228
- return serializer_1.deserialize(result, entry.outputs);
293
+ throw new Error(`Unknown method id ${op.call_contract.entry_point}`);
229
294
  }
230
295
  }
231
296
  exports.Contract = Contract;
@@ -1 +1 @@
1
- {"version":3,"file":"Contract.js","sourceRoot":"","sources":["../src/Contract.ts"],"names":[],"mappings":";;;AAAA,+BAAsD;AACtD,6CAAsD;AACtD,iDAA8C;AA4G9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6EG;AACH,MAAa,QAAQ;IAWnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqCG;IACH,YAAY,CAAmC;QAC7C,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC;QACf,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC;IAC3B,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACH,eAAe,CAAC,EAAoB;QAClC,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC;YACzC,MAAM,IAAI,KAAK,CAAC,aAAa,EAAE,CAAC,IAAI,UAAU,CAAC,CAAC;QAClD,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;QACpC,OAAO;YACL,IAAI,EAAE,8BAAwB,CAAC,IAAc;YAC7C,KAAK,EAAE;gBACL,WAAW,EAAE,IAAI,CAAC,EAAE;gBACpB,WAAW,EAAE,KAAK,CAAC,EAAE;gBACrB,GAAG,CAAC,KAAK,CAAC,MAAM,IAAI;oBAClB,IAAI,EAAE,sBAAS,CAAC,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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;AA3VD,4BA2VC;AAED,kBAAe,QAAQ,CAAC"}
package/lib/Provider.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { EncodedOperation } from "./Contract";
2
- import { Block, Transaction } from "./interface";
1
+ import { BlockJson, TransactionJson, CallContractOperationJson, SendTransactionResponse } from "./interface";
3
2
  /**
4
3
  * Class to connect with the RPC node
5
4
  */
@@ -60,20 +59,37 @@ export declare class Provider {
60
59
  * Function to call "chain.get_account_nonce" to return the number of
61
60
  * transactions for a particular account. This call is used
62
61
  * when creating new transactions.
63
- * @param address - account address
62
+ * @param account - account address
64
63
  * @returns Nonce
65
64
  */
66
- getNonce(address: string): Promise<number>;
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
+ }>;
67
83
  /**
68
84
  * Function to get info from the head block in the blockchain
69
85
  */
70
86
  getHeadInfo(): Promise<{
71
87
  head_topology: {
72
88
  id: string;
73
- height: number;
89
+ height: string;
74
90
  previous: string;
75
91
  };
76
- last_irreversible_height: number;
92
+ last_irreversible_height: string;
77
93
  }>;
78
94
  /**
79
95
  * Function to get consecutive blocks in descending order
@@ -86,7 +102,7 @@ export declare class Provider {
86
102
  getBlocks(height: number, numBlocks?: number, idRef?: string): Promise<{
87
103
  block_id: string;
88
104
  block_height: number;
89
- block: Block;
105
+ block: BlockJson;
90
106
  block_receipt: {
91
107
  [x: string]: unknown;
92
108
  };
@@ -97,28 +113,37 @@ export declare class Provider {
97
113
  getBlock(height: number): Promise<{
98
114
  block_id: string;
99
115
  block_height: number;
100
- block: Block;
116
+ block: BlockJson;
101
117
  block_receipt: {
102
118
  [x: string]: unknown;
103
119
  };
104
120
  }>;
105
121
  /**
106
122
  * Function to call "chain.submit_transaction" to send a signed
107
- * transaction to the blockchain
123
+ * transaction to the blockchain. It returns an object with the async
124
+ * function "wait", which can be called to wait for the
125
+ * transaction to be mined.
108
126
  * @param transaction - Signed transaction
109
- * @returns
127
+ * @example
128
+ * ```ts
129
+ * const { transactionResponse } = await provider.sendTransaction({
130
+ * id: "1220...",
131
+ * active: "...",
132
+ * signatureData: "...",
133
+ * });
134
+ * console.log("Transaction submitted to the mempool");
135
+ * // wait to be mined
136
+ * const blockId = await transactionResponse.wait();
137
+ * console.log("Transaction mined")
138
+ * ```
110
139
  */
111
- sendTransaction(transaction: Transaction): Promise<unknown>;
140
+ sendTransaction(transaction: TransactionJson): Promise<SendTransactionResponse>;
112
141
  /**
113
142
  * Function to call "chain.read_contract" to read a contract.
114
- * The operation must be encoded (see [[EncodedOperation]]).
115
- * See also [[Wallet.readContract]] which, apart from the Provider,
116
- * uses the contract definition and it is prepared to receive
117
- * the operation decoded and return the result decoded as well.
118
- * @param operation - Encoded operation
119
- * @returns Encoded result
143
+ * This function is used by [[Contract]] class when read methods
144
+ * are invoked.
120
145
  */
121
- readContract(operation: EncodedOperation): Promise<{
146
+ readContract(operation: CallContractOperationJson): Promise<{
122
147
  result: string;
123
148
  logs: string;
124
149
  }>;