koilib 5.3.1 → 5.5.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.
@@ -3,11 +3,15 @@ import { Serializer } from "./Serializer";
3
3
  /**
4
4
  * Application Binary Interface (ABI)
5
5
  *
6
- * ABIs are composed of 2 elements: methods and types.
6
+ * ABIs are composed of 3 elements: methods, events, and types.
7
7
  * - The methods define the names of the entries of the smart contract,
8
8
  * the corresponding endpoints and the name of the types used.
9
- * - The types all the description to serialize and deserialize
10
- * using proto buffers.
9
+ * - The events define possible events triggered by the smart contract
10
+ * and the name of the types used.
11
+ * - The types contain the description to serialize and deserialize
12
+ * data using proto buffers. It is used to encode/decode the methods
13
+ * and events. These types can be provided in binary format or json
14
+ * format (koilib_types)
11
15
  *
12
16
  * To generate the types is necessary to use the dependency
13
17
  * protobufjs. The following example shows how to generate the
@@ -49,7 +53,12 @@ import { Serializer } from "./Serializer";
49
53
  * return: "mint_result",
50
54
  * },
51
55
  * },
52
- * types: tokenJson,
56
+ * events: {
57
+ * 'koinos.contracts.token.mint_event': {
58
+ * argument: "mint"
59
+ * },
60
+ * },
61
+ * koilib_types: tokenJson,
53
62
  * };
54
63
  * ```
55
64
  *
@@ -58,6 +67,95 @@ import { Serializer } from "./Serializer";
58
67
  * empty response (for instance when there are no balance records
59
68
  * for a specific address) and you require a default output in
60
69
  * such cases.
70
+ *
71
+ * **Definition of events**
72
+ *
73
+ * There are 2 ways to define events in koinos:
74
+ * - Event names as protobuffer names
75
+ * - Custom event names
76
+ *
77
+ * 1. Event names as protobuffer names: The name of the event links
78
+ * with the protobuffer definition. In this case there is no need
79
+ * to define the event in the ABI.
80
+ *
81
+ * Example:
82
+ *
83
+ * Proto definition
84
+ * ```
85
+ * package koinos.contracts.token;
86
+ *
87
+ * message transfer_arguments {
88
+ * bytes from = 1 [(btype) = ADDRESS];
89
+ * bytes to = 2 [(btype) = ADDRESS];
90
+ * uint64 value = 3 [jstype = JS_STRING];
91
+ * }
92
+ *
93
+ * message transfer_event {
94
+ * bytes from = 1 [(btype) = ADDRESS];
95
+ * bytes to = 2 [(btype) = ADDRESS];
96
+ * uint64 value = 3 [jstype = JS_STRING];
97
+ * }
98
+ * ```
99
+ *
100
+ * Contract
101
+ * ```ts
102
+ * // token-contract.ts
103
+ * transfer(args: token.transfer_arguments): token.transfer_result {
104
+ * ...
105
+ * System.event("koinos.contracts.token.transfer_event", Protobuf.encode(event, token.transfer_event.encode), impacted);
106
+ * }
107
+ * ```
108
+ *
109
+ * 2. Custom event names: The previous definition has a limitation. It's
110
+ * necessary to define a proto message for each event and argument, and they can
111
+ * not be reused. In this second solution, the event names are defined
112
+ * in the ABI and they are linked with the corresponding protobuffer definitions.
113
+ * In this sense, they can be reused.
114
+ *
115
+ * Example
116
+ *
117
+ * Proto definition
118
+ * ```
119
+ * package koinos.contracts.token;
120
+ *
121
+ * // only 1 message
122
+ * message transfer {
123
+ * bytes from = 1 [(btype) = ADDRESS];
124
+ * bytes to = 2 [(btype) = ADDRESS];
125
+ * uint64 value = 3 [jstype = JS_STRING];
126
+ * }
127
+ * ```
128
+ *
129
+ * Contract
130
+ * ```ts
131
+ * // token-contract.ts
132
+ *
133
+ * // Transfer of tokens
134
+ * // @event transfer_event token.transfer
135
+ * transfer(args: token.transfer): void {
136
+ * ...
137
+ * System.event("transfer_event", Protobuf.encode(event, token.transfer.encode), impacted);
138
+ * }
139
+ * ```
140
+ *
141
+ * ABI
142
+ * ```ts
143
+ * const abiToken = {
144
+ * methods: {
145
+ * transfer: {
146
+ * entry_point: 0x62efa292,
147
+ * argument: "transfer",
148
+ * return: "",
149
+ * },
150
+ * },
151
+ * events: {
152
+ * 'transfer_event': {
153
+ * argument: "transfer"
154
+ * },
155
+ * },
156
+ * koilib_types: tokenJson,
157
+ * };
158
+ * ```
61
159
  */
62
160
  export interface Abi {
63
161
  methods: {
@@ -84,11 +182,26 @@ export interface Abi {
84
182
  description?: string;
85
183
  };
86
184
  };
185
+ /**
186
+ * Protobuffers descriptor in binary format encoded in base64url.
187
+ */
188
+ types?: string;
87
189
  /**
88
190
  * Protobuffers descriptor in JSON format.
89
191
  * See https://www.npmjs.com/package/protobufjs#using-json-descriptors
90
192
  */
91
- koilib_types: INamespace;
193
+ koilib_types?: INamespace;
194
+ /**
195
+ * Definition of events
196
+ */
197
+ events?: {
198
+ [x: string]: {
199
+ /** Protobuffer type for argument */
200
+ argument?: string;
201
+ /** Description of the event */
202
+ description?: string;
203
+ };
204
+ };
92
205
  }
93
206
  /**
94
207
  * Human readable format operation
@@ -111,7 +224,61 @@ export interface DecodedOperationJson {
111
224
  /** Arguments decoded. See [[Abi]] */
112
225
  args?: Record<string, unknown>;
113
226
  }
114
- export interface BaseTransactionOptions {
227
+ export interface SendTransactionOptions {
228
+ /**
229
+ * Broadcast
230
+ *
231
+ * Boolean to define if the transaction should be broadcasted
232
+ * to the different nodes in the network. By default it is true.
233
+ *
234
+ * Set it to false if you want to interact with a contract for
235
+ * testing purposes and check the possible events triggered.
236
+ */
237
+ broadcast?: boolean;
238
+ /**
239
+ * Collection of Abis so that the receiver can parse the
240
+ * operations in the transaction
241
+ */
242
+ abis?: Record<string, Abi>;
243
+ /**
244
+ * Send abis
245
+ *
246
+ * Boolean to define if the abis should be shared with
247
+ * the signer so it will be able to decode the operations.
248
+ * By default it is true.
249
+ */
250
+ sendAbis?: boolean;
251
+ /**
252
+ * Function to be called before sending a transaction to the
253
+ * blockchain. It is useful to apply multisignatures to
254
+ * the transaction.
255
+ *
256
+ * @example
257
+ * ```ts
258
+ * const signer2 = Signer.fromSeed("signer2");
259
+ * const signer3 = Signer.fromSeed("signer3");
260
+ *
261
+ * const addMoreSignatures = async (tx, opts) => {
262
+ * await signer2.signTransaction(tx);
263
+ * await signer3.signTransaction(tx);
264
+ * };
265
+ *
266
+ * const { transaction } = await koin.transfer(
267
+ * {
268
+ * from: "16MT1VQFgsVxEfJrSGinrA5buiqBsN5ViJ",
269
+ * to: "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb",
270
+ * value: "1000000",
271
+ * },
272
+ * {
273
+ * payer: signer2.getAddress(),
274
+ * beforeSend: addMoreSignatures,
275
+ * }
276
+ * );
277
+ * ```
278
+ */
279
+ beforeSend?: (tx: TransactionJson, options?: SendTransactionOptions) => Promise<void>;
280
+ }
281
+ export interface TransactionOptions extends SendTransactionOptions {
115
282
  /**
116
283
  * Chain ID
117
284
  *
@@ -160,6 +327,8 @@ export interface BaseTransactionOptions {
160
327
  * not set the blockchain will increase the payer's nonce.
161
328
  */
162
329
  payee?: string;
330
+ }
331
+ export interface ContractTransactionOptions extends TransactionOptions {
163
332
  /**
164
333
  * Only operation
165
334
  *
@@ -195,34 +364,9 @@ export interface BaseTransactionOptions {
195
364
  * true.
196
365
  */
197
366
  sendTransaction?: boolean;
198
- /**
199
- * Broadcast
200
- *
201
- * Boolean to define if the transaction should be broadcasted
202
- * to the different nodes in the network. By default it is true.
203
- *
204
- * Set it to false if you want to interact with a contract for
205
- * testing purposes and check the possible events triggered.
206
- */
207
- broadcast?: boolean;
208
- /**
209
- * Function to be called before sending a transaction to the
210
- * blockchain. It is useful to apply multisignatures to
211
- * the transaction
212
- */
213
- beforeSend?: (tx: TransactionJson, options?: SendTransactionOptions) => Promise<void>;
214
- }
215
- export interface TransactionOptions extends BaseTransactionOptions {
216
- /**
217
- * Send abis
218
- *
219
- * Boolean to define if the abis should be shared with
220
- * the signer so it will be able to decode the operations.
221
- * By default it is true.
222
- */
223
- sendAbis?: boolean;
224
367
  }
225
- export interface DeployOptions extends BaseTransactionOptions {
368
+ export declare type CallContractOptions = ContractTransactionOptions;
369
+ export interface DeployOptions extends ContractTransactionOptions {
226
370
  /**
227
371
  * ABI
228
372
  *
@@ -255,52 +399,6 @@ export interface DeployOptions extends BaseTransactionOptions {
255
399
  */
256
400
  authorizesUploadContract?: boolean;
257
401
  }
258
- export interface SendTransactionOptions {
259
- /**
260
- * Broadcast
261
- *
262
- * Boolean to define if the transaction should be broadcasted
263
- * to the different nodes in the network. By default it is true.
264
- *
265
- * Set it to false if you want to interact with a contract for
266
- * testing purposes and check the possible events triggered.
267
- */
268
- broadcast?: boolean;
269
- /**
270
- * Collection of Abis so that the receiver can parse the
271
- * operations in the transaction
272
- */
273
- abis?: Record<string, Abi>;
274
- /**
275
- * Function to be called before sending a transaction to the
276
- * blockchain. It is useful to apply multisignatures to
277
- * the transaction.
278
- *
279
- * @example
280
- * ```ts
281
- * const signer2 = Signer.fromSeed("signer2");
282
- * const signer3 = Signer.fromSeed("signer3");
283
- *
284
- * const addMoreSignatures = async (tx, opts) => {
285
- * await signer2.signTransaction(tx);
286
- * await signer3.signTransaction(tx);
287
- * };
288
- *
289
- * const { transaction } = await koin.transfer(
290
- * {
291
- * from: "16MT1VQFgsVxEfJrSGinrA5buiqBsN5ViJ",
292
- * to: "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb",
293
- * value: "1000000",
294
- * },
295
- * {
296
- * payer: signer2.getAddress(),
297
- * beforeSend: addMoreSignatures,
298
- * }
299
- * );
300
- * ```
301
- */
302
- beforeSend?: (tx: TransactionJson, options?: SendTransactionOptions) => Promise<void>;
303
- }
304
402
  export interface RecoverPublicKeyOptions {
305
403
  /**
306
404
  * Boolean to define if the public key should
@@ -539,6 +637,16 @@ export interface ValueType {
539
637
  uint64_value?: string;
540
638
  [x: string]: unknown;
541
639
  }
640
+ export interface EventData {
641
+ sequence: number;
642
+ source: string;
643
+ name: string;
644
+ data: string;
645
+ impacted: string[];
646
+ }
647
+ export interface DecodedEventData extends EventData {
648
+ args: Record<string, unknown>;
649
+ }
542
650
  export interface TransactionReceipt {
543
651
  id: string;
544
652
  payer: string;
@@ -549,12 +657,6 @@ export interface TransactionReceipt {
549
657
  network_bandwidth_used: string;
550
658
  compute_bandwidth_used: string;
551
659
  reverted: boolean;
552
- events: {
553
- sequence: number;
554
- source: string;
555
- name: string;
556
- data: string;
557
- impacted: string[];
558
- }[];
660
+ events: EventData[];
559
661
  logs: string[];
560
662
  }
package/lib/index.d.ts CHANGED
@@ -4,4 +4,5 @@ export * as interfaces from "./interface";
4
4
  export * from "./Contract";
5
5
  export * from "./Signer";
6
6
  export * from "./Provider";
7
+ export * from "./Transaction";
7
8
  export * from "./Serializer";
package/lib/index.js CHANGED
@@ -34,5 +34,6 @@ exports.interfaces = __importStar(require("./interface"));
34
34
  __exportStar(require("./Contract"), exports);
35
35
  __exportStar(require("./Signer"), exports);
36
36
  __exportStar(require("./Provider"), exports);
37
+ __exportStar(require("./Transaction"), exports);
37
38
  __exportStar(require("./Serializer"), exports);
38
39
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wEAAwE;AACxE,iCAAiC;AACjC,sDAAsC;AACtC,0DAA0C;AAC1C,6CAA2B;AAC3B,2CAAyB;AACzB,6CAA2B;AAC3B,+CAA6B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wEAAwE;AACxE,iCAAiC;AACjC,sDAAsC;AACtC,0DAA0C;AAC1C,6CAA2B;AAC3B,2CAAyB;AACzB,6CAA2B;AAC3B,gDAA8B;AAC9B,+CAA6B"}
package/lib/index2.js CHANGED
@@ -28,10 +28,12 @@ const utils = __importStar(require("./utils"));
28
28
  const Contract_1 = require("./Contract");
29
29
  const Signer_1 = require("./Signer");
30
30
  const Provider_1 = require("./Provider");
31
+ const Transaction_1 = require("./Transaction");
31
32
  const Serializer_1 = require("./Serializer");
32
33
  window.utils = utils;
33
34
  window.Contract = Contract_1.Contract;
34
35
  window.Signer = Signer_1.Signer;
35
36
  window.Provider = Provider_1.Provider;
37
+ window.Transaction = Transaction_1.Transaction;
36
38
  window.Serializer = Serializer_1.Serializer;
37
39
  //# sourceMappingURL=index2.js.map
package/lib/index2.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index2.js","sourceRoot":"","sources":["../src/index2.ts"],"names":[],"mappings":";AAAA,wEAAwE;;;;;;;;;;;;;;;;;;;;;;;;;AAExE,+CAAiC;AACjC,yCAAsC;AACtC,qCAAkC;AAClC,yCAAsC;AACtC,6CAA0C;AAI1C,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC;AACrB,MAAM,CAAC,QAAQ,GAAG,mBAAQ,CAAC;AAC3B,MAAM,CAAC,MAAM,GAAG,eAAM,CAAC;AACvB,MAAM,CAAC,QAAQ,GAAG,mBAAQ,CAAC;AAC3B,MAAM,CAAC,UAAU,GAAG,uBAAU,CAAC"}
1
+ {"version":3,"file":"index2.js","sourceRoot":"","sources":["../src/index2.ts"],"names":[],"mappings":";AAAA,wEAAwE;;;;;;;;;;;;;;;;;;;;;;;;;AAExE,+CAAiC;AACjC,yCAAsC;AACtC,qCAAkC;AAClC,yCAAsC;AACtC,+CAA4C;AAC5C,6CAA0C;AAI1C,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC;AACrB,MAAM,CAAC,QAAQ,GAAG,mBAAQ,CAAC;AAC3B,MAAM,CAAC,MAAM,GAAG,eAAM,CAAC;AACvB,MAAM,CAAC,QAAQ,GAAG,mBAAQ,CAAC;AAC3B,MAAM,CAAC,WAAW,GAAG,yBAAW,CAAC;AACjC,MAAM,CAAC,UAAU,GAAG,uBAAU,CAAC"}