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.d.ts CHANGED
@@ -1,230 +1,153 @@
1
- import { Abi } from "./abi";
1
+ import { Signer, SignerInterface } from "./Signer";
2
+ import { Provider } from "./Provider";
3
+ import { Serializer } from "./Serializer";
4
+ import { CallContractOperationNested, UploadContractOperationNested, TransactionJson, Abi, TransactionOptions, DecodedOperationJson, SendTransactionResponse } from "./interface";
2
5
  /**
3
- * These entries definitions are used to serialize and deserialize
4
- * contract operations. Each entry contains a name (defined in the
5
- * key field), id (entry point id), inputs (abi describing the
6
- * serialization of the inputs), and outputs (abi describing the
7
- * serialization of the outputs). See [[Abi]].
8
- *
9
- * @example
10
- * ```ts
11
- * const entries = {
12
- * transfer: {
13
- * id: 0x62efa292,
14
- * inputs: {
15
- * type: [
16
- * {
17
- * name: "from",
18
- * type: "string",
19
- * },
20
- * {
21
- * name: "to",
22
- * type: "string",
23
- * },
24
- * {
25
- * name: "value",
26
- * type: "uint64",
27
- * },
28
- * ],
29
- * },
30
- * },
31
- * balance_of: {
32
- * id: 0x15619248,
33
- * inputs: { type: "string" },
34
- * outputs: { type: "uint64" },
35
- * },
36
- * };
37
- * ```
38
- */
39
- export interface Entries {
40
- /** Name of the entry */
41
- [x: string]: {
42
- /** Entry point ID */
43
- id: number;
44
- /** ABI definition for input serialization */
45
- inputs: Abi;
46
- /** ABI definition for output serialization */
47
- outputs?: Abi;
48
- };
49
- }
50
- /**
51
- * Operation using the format for the communication with the RPC node
52
- *
53
- * @example
54
- * ```ts
55
- * const opEncoded = {
56
- * type: "koinos::protocol::call_contract_operation",
57
- * value: {
58
- * contract_id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
59
- * entry_point: 0x62efa292,
60
- * args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
61
- * }
62
- * }
63
- * ```
64
- */
65
- export interface EncodedOperation {
66
- /** It should be "koinos::protocol::call_contract_operation" */
67
- type: string;
68
- /** Value of call contract operation */
69
- value: {
70
- /** Contract ID */
71
- contract_id: string;
72
- /** Entry point ID */
73
- entry_point: number;
74
- /** Arguments serialized and encoded in base64 */
75
- args?: string;
76
- };
77
- }
78
- /**
79
- * Human readable format operation
80
- *
81
- * @example
82
- * ```ts
83
- * const opDecoded = {
84
- * name: "transfer",
85
- * args: {
86
- * from: "1Krs7v1rtpgRyfwEZncuKMQQnY5JhqXVSx",
87
- * to: "1BqtgWBcqm9cSZ97avLGZGJdgso7wx6pCA",
88
- * value: 1000,
89
- * },
90
- * };
91
- * ```
92
- */
93
- export interface DecodedOperation {
94
- /** Operation name */
95
- name: string;
96
- /** Arguments decoded. See [[Abi]] */
97
- args: unknown;
98
- }
99
- /**
100
- * The contract class contains the contract ID and contrac entries
6
+ * The contract class contains the contract ID and contract entries
101
7
  * definition needed to encode/decode operations during the
102
8
  * interaction with the user and the communication with the RPC node.
103
9
  *
104
- * Operations are encoded to communicate with the RPC node. However,
105
- * this format is not human readable as the data is serialized and
106
- * encoded in Base64 format. When decoding operations, they can be
107
- * read by the user (see [[EncodedOperation]] and [[DecodedOperation]]).
108
- *
109
10
  * @example
110
11
  *
111
12
  * ```ts
112
- * const contract = new Contract({
113
- * id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
114
- * entries: {
115
- * transfer: {
116
- * id: 0x62efa292,
117
- * inputs: {
118
- * type: [
119
- * {
120
- * name: "from",
121
- * type: "string",
122
- * },
123
- * {
124
- * name: "to",
125
- * type: "string",
126
- * },
127
- * {
128
- * name: "value",
129
- * type: "uint64",
130
- * },
131
- * ],
132
- * },
133
- * },
134
- * balance_of: {
135
- * id: 0x15619248,
136
- * inputs: { type: "string" },
137
- * outputs: { type: "uint64" },
138
- * },
139
- * },
13
+ * const { Contract, Provider, Signer, utils } = require("koilib");
14
+ * const rpcNodes = ["http://api.koinos.io:8080"];
15
+ * const privateKey = "f186a5de49797bfd52dc42505c33d75a46822ed5b60046e09d7c336242e20200";
16
+ * const provider = new Provider(rpcNodes);
17
+ * const signer = new Signer({ privateKey, provider });
18
+ * const koinContract = new Contract({
19
+ * id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
20
+ * abi: utils.Krc20Abi,
21
+ * provider,
22
+ * signer,
140
23
  * });
24
+ * const koin = koinContract.functions;
141
25
  *
142
- * const opEncoded = contract.encodeOperation({
143
- * name: "transfer",
144
- * args: {
145
- * from: "alice",
146
- * to: "bob",
147
- * value: BigInt(1000),
148
- * },
26
+ * // optional: preformat input/output
27
+ * koinContract.abi.methods.balanceOf.preformatInput = (owner) =>
28
+ * ({ owner });
29
+ * koinContract.abi.methods.balanceOf.preformatOutput = (res) =>
30
+ * utils.formatUnits(res.value, 8);
31
+ * koinContract.abi.methods.transfer.preformatInput = (input) => ({
32
+ * from: signer.getAddress(),
33
+ * to: input.to,
34
+ * value: utils.parseUnits(input.value, 8),
149
35
  * });
150
36
  *
151
- * console.log(opEncoded);
152
- * // {
153
- * // type: "koinos::protocol::call_contract_operation",
154
- * // value: {
155
- * // contract_id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
156
- * // entry_point: 0x62efa292,
157
- * // args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
158
- * // }
159
- * // }
37
+ * async funtion main() {
38
+ * // Get balance
39
+ * const { result } = await koin.balanceOf("12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD");
40
+ * console.log(result)
160
41
  *
161
- * const opDecoded = contract.decodeOperation(opEncoded);
162
- * console.log(opDecoded);
163
- * // {
164
- * // name: "transfer",
165
- * // args: {
166
- * // from: "alice",
167
- * // to: "bob",
168
- * // value: 1000n,
169
- * // },
170
- * // }
42
+ * // Transfer
43
+ * const { transaction, transactionResponse } = await koin.transfer({
44
+ * to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
45
+ * value: "10.0001",
46
+ * });
47
+ * console.log(`Transaction id ${transaction.id} submitted`);
171
48
  *
172
- * const resultDecoded = contract.decodeResult("MAAsZnAzyD0E=", "balance_of");
173
- * console.log(resultDecoded)
174
- * // 3124382766600001n
49
+ * // wait to be mined
50
+ * const blockId = await transactionResponse.wait();
51
+ * console.log(`Transaction mined. Block id: ${blockId}`);
52
+ * }
53
+ *
54
+ * main();
175
55
  * ```
176
56
  */
177
57
  export declare class Contract {
178
58
  /**
179
59
  * Contract ID
180
60
  */
181
- id: string;
182
- /**
183
- * Contract entries. See [[Entries]]
184
- */
185
- entries: Entries;
61
+ id?: Uint8Array;
186
62
  /**
187
- * The constructor receives the contract ID and
188
- * contract entries definition
189
- * @param c - Object with contract id and contract entries
63
+ * Set of functions to interact with the smart
64
+ * contract. These functions are automatically generated
65
+ * in the constructor of the class
190
66
  *
191
67
  * @example
192
68
  * ```ts
193
- * const contract = new Contract({
194
- * id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
195
- * entries: {
196
- * transfer: {
197
- * id: 0x62efa292,
198
- * inputs: {
199
- * type: [
200
- * {
201
- * name: "from",
202
- * type: "string",
203
- * },
204
- * {
205
- * name: "to",
206
- * type: "string",
207
- * },
208
- * {
209
- * name: "value",
210
- * type: "uint64",
211
- * },
212
- * ],
213
- * },
214
- * },
215
- * balance_of: {
216
- * id: 0x15619248,
217
- * inputs: { type: "string" },
218
- * outputs: { type: "uint64" },
219
- * },
220
- * },
221
- * });
69
+ * const owner = "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb";
70
+ * await koinContract.functions.balanceOf({ owner });
222
71
  * ```
223
72
  */
73
+ functions: {
74
+ [x: string]: <T = Record<string, unknown>>(args?: unknown, opts?: TransactionOptions) => Promise<{
75
+ operation: CallContractOperationNested;
76
+ transaction?: TransactionJson;
77
+ transactionResponse?: SendTransactionResponse;
78
+ result?: T;
79
+ }>;
80
+ };
81
+ /**
82
+ * Application Binary Interface
83
+ */
84
+ abi?: Abi;
85
+ /**
86
+ * Signer interacting with the smart contract
87
+ */
88
+ signer?: SignerInterface;
89
+ /**
90
+ * Provider to connect with the blockchain
91
+ */
92
+ provider?: Provider;
93
+ /**
94
+ * Serializer to serialize/deserialize data types
95
+ */
96
+ serializer?: Serializer;
97
+ /**
98
+ * Bytecode. Needed to deploy the smart contract.
99
+ */
100
+ bytecode?: Uint8Array;
101
+ /**
102
+ * Options to apply when creating transactions.
103
+ * By default it set rc_limit to 1e8, sendTransaction true,
104
+ * sendAbis true, and nonce undefined (to get it from the blockchain)
105
+ */
106
+ options: TransactionOptions;
224
107
  constructor(c: {
225
- id: string;
226
- entries: Entries;
108
+ id?: string;
109
+ abi?: Abi;
110
+ bytecode?: Uint8Array;
111
+ options?: TransactionOptions;
112
+ signer?: Signer;
113
+ provider?: Provider;
114
+ /**
115
+ * Set this option if you can not use _eval_ functions
116
+ * in the current environment. In such cases, the
117
+ * serializer must come from an environment where it
118
+ * is able to use those functions.
119
+ */
120
+ serializer?: Serializer;
227
121
  });
122
+ /**
123
+ * Compute contract Id
124
+ */
125
+ static computeContractId(address: string): Uint8Array;
126
+ /**
127
+ * Get contract Id
128
+ */
129
+ getId(): string;
130
+ /**
131
+ * Function to deploy a new smart contract.
132
+ * The Bytecode must be defined in the constructor of the class
133
+ * @example
134
+ * ```ts
135
+ * const privateKey = "f186a5de49797bfd52dc42505c33d75a46822ed5b60046e09d7c336242e20200";
136
+ * const provider = new Provider(["http://api.koinos.io:8080"]);
137
+ * const signer = new Signer({ privateKey, provider });
138
+ * const bytecode = new Uint8Array([1, 2, 3, 4]);
139
+ * const contract = new Contract({ signer, provider, bytecode });
140
+ * const { transactionResponse } = await contract.deploy();
141
+ * // wait to be mined
142
+ * const blockId = await transactionResponse.wait();
143
+ * console.log(`Contract uploaded in block id ${blockId}`);
144
+ * ```
145
+ */
146
+ deploy(options?: TransactionOptions): Promise<{
147
+ operation: UploadContractOperationNested;
148
+ transaction?: TransactionJson;
149
+ transactionResponse?: SendTransactionResponse;
150
+ }>;
228
151
  /**
229
152
  * Encondes a contract operation using Koinos serialization
230
153
  * and taking the contract entries as reference to build it
@@ -235,32 +158,30 @@ export declare class Contract {
235
158
  * const opEncoded = contract.encodeOperation({
236
159
  * name: "transfer",
237
160
  * args: {
238
- * from: "alice",
239
- * to: "bob",
240
- * value: 1000,
161
+ * from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
162
+ * to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
163
+ * value: "1000",
241
164
  * }
242
165
  * });
243
166
  *
244
167
  * console.log(opEncoded);
245
168
  * // {
246
- * // type: "koinos::protocol::call_contract_operation",
247
- * // value: {
248
- * // contract_id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
169
+ * // call_contract: {
170
+ * // contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
249
171
  * // entry_point: 0x62efa292,
250
172
  * // args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
251
173
  * // }
252
174
  * // }
253
175
  * ```
254
176
  */
255
- encodeOperation(op: DecodedOperation): EncodedOperation;
177
+ encodeOperation(op: DecodedOperationJson): Promise<CallContractOperationNested>;
256
178
  /**
257
179
  * Decodes a contract operation to be human readable
258
180
  * @example
259
181
  * ```ts
260
182
  * const opDecoded = contract.decodeOperation({
261
- * type: "koinos::protocol::call_contract_operation",
262
- * value: {
263
- * contract_id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
183
+ * call_contract: {
184
+ * contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
264
185
  * entry_point: 0x62efa292,
265
186
  * args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
266
187
  * }
@@ -269,29 +190,13 @@ export declare class Contract {
269
190
  * // {
270
191
  * // name: "transfer",
271
192
  * // args: {
272
- * // from: "alice",
273
- * // to: "bob",
274
- * // value: 1000n,
193
+ * // from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
194
+ * // to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
195
+ * // value: "1000",
275
196
  * // },
276
197
  * // }
277
198
  * ```
278
199
  */
279
- decodeOperation(op: EncodedOperation): DecodedOperation;
280
- /**
281
- * Decodes a result. This function is used in conjunction with
282
- * [[Provider.readContract | readContract of Provider class]] to read a
283
- * contract and decode the result. "outputs" field must be defined in
284
- * the abi for the operation name.
285
- * @param result - Encoded result in base64
286
- * @param opName - Operation name
287
- * @returns Decoded result
288
- * @example
289
- * ```ts
290
- * const resultDecoded = contract.decodeResult("MAAsZnAzyD0E=", "balance_of");
291
- * console.log(resultDecoded)
292
- * // 3124382766600001n
293
- * ```
294
- */
295
- decodeResult(result: string, opName: string): unknown;
200
+ decodeOperation(op: CallContractOperationNested): Promise<DecodedOperationJson>;
296
201
  }
297
202
  export default Contract;