koilib 5.5.3 → 5.5.5

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.
@@ -0,0 +1,241 @@
1
+ import { Contract } from "./Contract";
2
+ import { Provider } from "./Provider";
3
+ import { Signer } from "./Signer";
4
+ import {
5
+ Abi,
6
+ OperationJson,
7
+ SendTransactionOptions,
8
+ TransactionJson,
9
+ TransactionOptions,
10
+ TransactionReceipt,
11
+ WaitFunction,
12
+ } from "./interface";
13
+
14
+ export class Transaction {
15
+ /**
16
+ * Signer interacting with the smart contracts
17
+ */
18
+ signer?: Signer;
19
+
20
+ /**
21
+ * Provider to connect with the blockchain
22
+ */
23
+ provider?: Provider;
24
+
25
+ /**
26
+ * Transaction
27
+ */
28
+ transaction: TransactionJson;
29
+
30
+ /**
31
+ * Function to wait for the transaction to be mined
32
+ */
33
+ waitFunction?: WaitFunction;
34
+
35
+ /**
36
+ * Transaction options
37
+ */
38
+ options: TransactionOptions;
39
+
40
+ constructor(c?: {
41
+ signer?: Signer;
42
+ provider?: Provider;
43
+ options?: TransactionOptions;
44
+ }) {
45
+ this.signer = c?.signer;
46
+ this.provider = c?.provider;
47
+ this.options = {
48
+ broadcast: true,
49
+ sendAbis: true,
50
+ ...c?.options,
51
+ };
52
+ this.transaction = {
53
+ header: {
54
+ ...(c?.options?.chainId && { chain_id: c.options.chainId }),
55
+ ...(c?.options?.rcLimit && { rc_limit: c.options.rcLimit }),
56
+ ...(c?.options?.nonce && { nonce: c.options.nonce }),
57
+ ...(c?.options?.payer && { payer: c.options.payer }),
58
+ ...(c?.options?.payee && { payee: c.options.payee }),
59
+ },
60
+ operations: [],
61
+ };
62
+ }
63
+
64
+ /**
65
+ * Function to push an operation to the transaction. It can be created
66
+ * in several ways. Example:
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * const koin = new Contract({
71
+ * id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
72
+ * abi: utils.tokenAbi,
73
+ * }).functions;
74
+ * const signer = Signer.fromSeed("my seed");
75
+ * const provider = new Provider(["https://api.koinos.io"]);
76
+ * signer.provider = provider;
77
+ * const tx = new Transaction({ signer });
78
+ *
79
+ * // method 1
80
+ * await tx.pushOperation(koin.transfer, {
81
+ * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
82
+ * to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
83
+ * value: "1000",
84
+ * });
85
+ *
86
+ * // method 2
87
+ * await tx.pushOperation(
88
+ * koin.transfer({
89
+ * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
90
+ * to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
91
+ * value: "1000",
92
+ * },{
93
+ * onlyOperation: true,
94
+ * })
95
+ * );
96
+ *
97
+ * // method 3
98
+ * await tx.pushOperation(
99
+ * await koin.transfer({
100
+ * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
101
+ * to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
102
+ * value: "1000",
103
+ * },{
104
+ * onlyOperation: true,
105
+ * })
106
+ * );
107
+ *
108
+ * // method 4
109
+ * const { operation } = await koin.transfer({
110
+ * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
111
+ * to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
112
+ * value: "1000",
113
+ * },{
114
+ * onlyOperation: true,
115
+ * });
116
+ * await tx.pushOperation(operation)
117
+ * ```
118
+ *
119
+ */
120
+ async pushOperation(
121
+ input:
122
+ | OperationJson
123
+ | { operation: OperationJson }
124
+ | Promise<{ operation: OperationJson }>
125
+ | Contract["functions"]["x"],
126
+ args?: unknown
127
+ ): Promise<void> {
128
+ let operation: OperationJson;
129
+ if (typeof input === "function") {
130
+ const result = await input(args, { onlyOperation: true });
131
+ operation = result.operation;
132
+ } else {
133
+ let inp: OperationJson | { operation: OperationJson };
134
+ if (input instanceof Promise) {
135
+ inp = await input;
136
+ } else {
137
+ inp = input;
138
+ }
139
+ if ((inp as { operation: OperationJson }).operation) {
140
+ operation = (inp as { operation: OperationJson }).operation;
141
+ } else {
142
+ operation = input as OperationJson;
143
+ }
144
+ }
145
+
146
+ if (!this.transaction.operations) this.transaction.operations = [];
147
+ this.transaction.operations.push(operation);
148
+ }
149
+
150
+ /**
151
+ * Functon to prepare the transaction (set headers, merkle
152
+ * root, etc)
153
+ */
154
+ async prepare(options?: TransactionOptions): Promise<TransactionJson> {
155
+ if (options) {
156
+ const header = {
157
+ ...(options?.chainId && { chain_id: options.chainId }),
158
+ ...(options?.rcLimit && { rc_limit: options.rcLimit }),
159
+ ...(options?.nonce && { nonce: options.nonce }),
160
+ ...(options?.payer && { payer: options.payer }),
161
+ ...(options?.payee && { payee: options.payee }),
162
+ };
163
+ this.transaction.header = {
164
+ ...this.transaction.header,
165
+ ...header,
166
+ };
167
+ }
168
+
169
+ if (this.signer) {
170
+ this.transaction = await this.signer.prepareTransaction(this.transaction);
171
+ } else {
172
+ if (!this.transaction.header || !this.transaction.header.payer) {
173
+ throw new Error("no payer defined");
174
+ }
175
+ const signer = Signer.fromSeed("0");
176
+ signer.provider = this.provider;
177
+ this.transaction = await signer.prepareTransaction(this.transaction);
178
+ }
179
+ return this.transaction;
180
+ }
181
+
182
+ /**
183
+ * Function to sign the transaction
184
+ */
185
+ async sign(abis?: Record<string, Abi>): Promise<TransactionJson> {
186
+ if (!this.signer) throw new Error("no signer defined");
187
+ if (!this.transaction.id) await this.prepare();
188
+ return this.signer.signTransaction(
189
+ this.transaction,
190
+ this.options.sendAbis ? abis : undefined
191
+ );
192
+ }
193
+
194
+ /**
195
+ * Function to broadcast the transaction
196
+ */
197
+ async send(options?: SendTransactionOptions): Promise<TransactionReceipt> {
198
+ const opts = {
199
+ ...this.options,
200
+ ...options,
201
+ };
202
+ if (!this.transaction.id) await this.prepare();
203
+
204
+ if (this.signer && this.signer.provider) {
205
+ const { transaction: tx, receipt } = await this.signer.sendTransaction(
206
+ this.transaction,
207
+ opts
208
+ );
209
+ this.transaction = tx;
210
+ this.waitFunction = tx.wait;
211
+ return receipt;
212
+ }
213
+
214
+ if (!this.provider) throw new Error("provider not defined");
215
+ if (!this.transaction.signatures || !this.transaction.signatures.length) {
216
+ throw new Error("transaction without signatures and no signer defined");
217
+ }
218
+
219
+ if (opts.beforeSend) {
220
+ await opts.beforeSend(this.transaction, opts);
221
+ }
222
+ const { transaction: tx, receipt } = await this.provider.sendTransaction(
223
+ this.transaction,
224
+ opts.broadcast
225
+ );
226
+ this.transaction = tx;
227
+ this.waitFunction = tx.wait;
228
+ return receipt;
229
+ }
230
+
231
+ async wait(
232
+ type?: "byBlock" | "byTransactionId",
233
+ timeout?: number
234
+ ): Promise<{
235
+ blockId: string;
236
+ blockNumber?: number;
237
+ }> {
238
+ if (!this.waitFunction) throw new Error("no wait function defined");
239
+ return this.waitFunction(type, timeout);
240
+ }
241
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ /*! koilib - MIT License (c) Julian Gonzalez (joticajulian@gmail.com) */
2
+ // eslint-disable import/no-cycle
3
+ export * as utils from "./indexUtils";
4
+ export * as interfaces from "./interface";
5
+ export * from "./Contract";
6
+ export * from "./Signer";
7
+ export * from "./Provider";
8
+ export * from "./Transaction";
9
+ export * from "./Serializer";
package/src/index2.ts ADDED
@@ -0,0 +1,17 @@
1
+ /*! koilib - MIT License (c) Julian Gonzalez (joticajulian@gmail.com) */
2
+
3
+ import * as utils from "./utils";
4
+ import { Contract } from "./Contract";
5
+ import { Signer } from "./Signer";
6
+ import { Provider } from "./Provider";
7
+ import { Transaction } from "./Transaction";
8
+ import { Serializer } from "./Serializer";
9
+
10
+ declare const window: { [x: string]: unknown };
11
+
12
+ window.utils = utils;
13
+ window.Contract = Contract;
14
+ window.Signer = Signer;
15
+ window.Provider = Provider;
16
+ window.Transaction = Transaction;
17
+ window.Serializer = Serializer;
@@ -0,0 +1,2 @@
1
+ export * from "./utils";
2
+ export * from "./utilsNode";