koilib 2.6.1 → 2.7.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 +106 -96
- 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/Signer.d.ts +4 -4
- package/lib/Signer.js +11 -3
- package/lib/Signer.js.map +1 -1
- package/lib/browser/Contract.d.ts +9 -11
- package/lib/browser/Contract.js +12 -12
- package/lib/browser/Contract.js.map +1 -1
- package/lib/browser/Provider.d.ts +24 -17
- package/lib/browser/Provider.js +83 -81
- package/lib/browser/Provider.js.map +1 -1
- package/lib/browser/Signer.d.ts +4 -4
- package/lib/browser/Signer.js +11 -3
- package/lib/browser/Signer.js.map +1 -1
- package/lib/browser/interface.d.ts +22 -21
- package/lib/interface.d.ts +22 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -105,14 +105,14 @@ a transaction, and read contracts.
|
|
|
105
105
|
});
|
|
106
106
|
|
|
107
107
|
// Transfer
|
|
108
|
-
const { transaction
|
|
108
|
+
const { transaction } = await koin.transfer({
|
|
109
109
|
to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
|
|
110
110
|
value: "10.0001",
|
|
111
111
|
});
|
|
112
112
|
console.log(`Transaction id ${transaction.id} submitted`);
|
|
113
113
|
|
|
114
114
|
// wait to be mined
|
|
115
|
-
const blockNumber = await
|
|
115
|
+
const blockNumber = await transaction.wait();
|
|
116
116
|
console.log(`Transaction mined. Block number: ${blockNumber}`);
|
|
117
117
|
|
|
118
118
|
// read the balance
|
|
@@ -133,10 +133,10 @@ It's also possible to upload contracts. First, follow the instructions in [koino
|
|
|
133
133
|
|
|
134
134
|
// create contract and deploy
|
|
135
135
|
const contract = new Contract({ signer, provider, bytecode });
|
|
136
|
-
const {
|
|
136
|
+
const { transaction } = await contract.deploy();
|
|
137
137
|
// wait to be mined
|
|
138
|
-
const blockNumber = await
|
|
139
|
-
console.log(`Contract uploaded in block
|
|
138
|
+
const blockNumber = await transaction.wait();
|
|
139
|
+
console.log(`Contract uploaded in block number ${blockNumber}`);
|
|
140
140
|
})();
|
|
141
141
|
```
|
|
142
142
|
|
package/dist/koinos.js
CHANGED
|
@@ -9796,15 +9796,15 @@ const utils_1 = __webpack_require__(8593);
|
|
|
9796
9796
|
* console.log(result)
|
|
9797
9797
|
*
|
|
9798
9798
|
* // Transfer
|
|
9799
|
-
* const { transaction
|
|
9799
|
+
* const { transaction } = await koin.transfer({
|
|
9800
9800
|
* to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
|
|
9801
9801
|
* value: "10.0001",
|
|
9802
9802
|
* });
|
|
9803
9803
|
* console.log(`Transaction id ${transaction.id} submitted`);
|
|
9804
9804
|
*
|
|
9805
9805
|
* // wait to be mined
|
|
9806
|
-
* const
|
|
9807
|
-
* console.log(`Transaction mined. Block
|
|
9806
|
+
* const blockNumber = await transaction.wait();
|
|
9807
|
+
* console.log(`Transaction mined. Block number: ${blockNumber}`);
|
|
9808
9808
|
* }
|
|
9809
9809
|
*
|
|
9810
9810
|
* main();
|
|
@@ -9882,7 +9882,7 @@ class Contract {
|
|
|
9882
9882
|
// write contract (sign and send)
|
|
9883
9883
|
if (!this.signer)
|
|
9884
9884
|
throw new Error("signer not found");
|
|
9885
|
-
const
|
|
9885
|
+
const tx = await this.signer.encodeTransaction({
|
|
9886
9886
|
...opts,
|
|
9887
9887
|
operations: [operation],
|
|
9888
9888
|
});
|
|
@@ -9891,8 +9891,8 @@ class Contract {
|
|
|
9891
9891
|
const contractId = (0, utils_1.encodeBase58)(this.id);
|
|
9892
9892
|
abis[contractId] = this.abi;
|
|
9893
9893
|
}
|
|
9894
|
-
const
|
|
9895
|
-
return { operation, transaction
|
|
9894
|
+
const transaction = await this.signer.sendTransaction(tx, abis);
|
|
9895
|
+
return { operation, transaction };
|
|
9896
9896
|
};
|
|
9897
9897
|
});
|
|
9898
9898
|
}
|
|
@@ -9921,10 +9921,10 @@ class Contract {
|
|
|
9921
9921
|
* const signer = new Signer({ privateKey, provider });
|
|
9922
9922
|
* const bytecode = new Uint8Array([1, 2, 3, 4]);
|
|
9923
9923
|
* const contract = new Contract({ signer, provider, bytecode });
|
|
9924
|
-
* const {
|
|
9924
|
+
* const { transaction } = await contract.deploy();
|
|
9925
9925
|
* // wait to be mined
|
|
9926
|
-
* const
|
|
9927
|
-
* console.log(`Contract uploaded in block
|
|
9926
|
+
* const blockNumber = await transaction.wait();
|
|
9927
|
+
* console.log(`Contract uploaded in block number ${blockNumber}`);
|
|
9928
9928
|
* ```
|
|
9929
9929
|
*/
|
|
9930
9930
|
async deploy(options) {
|
|
@@ -9945,12 +9945,12 @@ class Contract {
|
|
|
9945
9945
|
// return operation if send is false
|
|
9946
9946
|
if (!(opts === null || opts === void 0 ? void 0 : opts.sendTransaction))
|
|
9947
9947
|
return { operation };
|
|
9948
|
-
const
|
|
9948
|
+
const tx = await this.signer.encodeTransaction({
|
|
9949
9949
|
...opts,
|
|
9950
9950
|
operations: [operation],
|
|
9951
9951
|
});
|
|
9952
|
-
const
|
|
9953
|
-
return { operation, transaction
|
|
9952
|
+
const transaction = await this.signer.sendTransaction(tx);
|
|
9953
|
+
return { operation, transaction };
|
|
9954
9954
|
}
|
|
9955
9955
|
/**
|
|
9956
9956
|
* Encondes a contract operation using Koinos serialization
|
|
@@ -10202,93 +10202,95 @@ class Provider {
|
|
|
10202
10202
|
return (await this.getBlocks(height, 1))[0];
|
|
10203
10203
|
}
|
|
10204
10204
|
/**
|
|
10205
|
-
* Function to
|
|
10206
|
-
*
|
|
10207
|
-
*
|
|
10208
|
-
*
|
|
10209
|
-
*
|
|
10205
|
+
* Function to wait for a transaction to be mined.
|
|
10206
|
+
* @param txId - transaction id
|
|
10207
|
+
* @param type - Type must be "byBlock" (default) or "byTransactionId".
|
|
10208
|
+
* _byBlock_ will query the blockchain to get blocks and search for the
|
|
10209
|
+
* transaction there. _byTransactionId_ will query the "transaction store"
|
|
10210
|
+
* microservice to search the transaction by its id. If non of them is
|
|
10211
|
+
* specified the function will use "byBlock" (as "byTransactionId"
|
|
10212
|
+
* requires the transaction store, which is an optional microservice).
|
|
10213
|
+
*
|
|
10214
|
+
* When _byBlock_ is used it returns the block number.
|
|
10215
|
+
*
|
|
10216
|
+
* When _byTransactionId_ is used it returns the block id.
|
|
10217
|
+
*
|
|
10218
|
+
* @param timeout - Timeout in milliseconds. By default it is 30000
|
|
10210
10219
|
* @example
|
|
10211
10220
|
* ```ts
|
|
10212
|
-
* const
|
|
10213
|
-
*
|
|
10214
|
-
*
|
|
10215
|
-
* signatureData: "...",
|
|
10216
|
-
* });
|
|
10217
|
-
* console.log("Transaction submitted to the mempool");
|
|
10218
|
-
* // wait to be mined
|
|
10219
|
-
* const blockNumber = await transactionResponse.wait();
|
|
10220
|
-
* // const blockNumber = await transactionResponse.wait("byBlock", 30000);
|
|
10221
|
-
* // const blockId = await transactionResponse.wait("byTransactionId", 30000);
|
|
10221
|
+
* const blockNumber = await provider.wait(txId);
|
|
10222
|
+
* // const blockNumber = await provider.wait(txId, "byBlock", 30000);
|
|
10223
|
+
* // const blockId = await provider.wait(txId, "byTransactionId", 30000);
|
|
10222
10224
|
* console.log("Transaction mined")
|
|
10223
10225
|
* ```
|
|
10224
10226
|
*/
|
|
10225
|
-
async
|
|
10226
|
-
|
|
10227
|
-
|
|
10228
|
-
|
|
10229
|
-
|
|
10230
|
-
|
|
10231
|
-
|
|
10232
|
-
|
|
10233
|
-
|
|
10234
|
-
|
|
10235
|
-
|
|
10236
|
-
|
|
10237
|
-
|
|
10238
|
-
|
|
10239
|
-
|
|
10240
|
-
|
|
10241
|
-
|
|
10242
|
-
|
|
10243
|
-
|
|
10244
|
-
|
|
10245
|
-
|
|
10246
|
-
|
|
10247
|
-
|
|
10248
|
-
|
|
10249
|
-
|
|
10250
|
-
|
|
10251
|
-
|
|
10252
|
-
|
|
10253
|
-
|
|
10254
|
-
if (tx)
|
|
10255
|
-
bNum = Number(block.block_height);
|
|
10256
|
-
});
|
|
10257
|
-
const lastId = blocks[blocks.length - 1].block_id;
|
|
10258
|
-
return [bNum, lastId];
|
|
10259
|
-
};
|
|
10260
|
-
let blockNumber = 0;
|
|
10261
|
-
let iniBlock = 0;
|
|
10262
|
-
let previousId = "";
|
|
10263
|
-
while (Date.now() < iniTime + timeout) {
|
|
10264
|
-
await sleep(1000);
|
|
10265
|
-
const { head_topology: headTopology } = await this.getHeadInfo();
|
|
10266
|
-
if (blockNumber === 0) {
|
|
10267
|
-
blockNumber = Number(headTopology.height);
|
|
10268
|
-
iniBlock = blockNumber;
|
|
10269
|
-
}
|
|
10270
|
-
if (Number(headTopology.height) === blockNumber - 1 &&
|
|
10271
|
-
previousId &&
|
|
10272
|
-
previousId !== headTopology.id) {
|
|
10273
|
-
const [bNum, lastId] = await findTxInBlocks(iniBlock, Number(headTopology.height) - iniBlock + 1, headTopology.id);
|
|
10274
|
-
if (bNum)
|
|
10275
|
-
return bNum;
|
|
10276
|
-
previousId = lastId;
|
|
10277
|
-
blockNumber = Number(headTopology.height) + 1;
|
|
10278
|
-
}
|
|
10279
|
-
// eslint-disable-next-line no-continue
|
|
10280
|
-
if (blockNumber > Number(headTopology.height))
|
|
10281
|
-
continue;
|
|
10282
|
-
const [bNum, lastId] = await findTxInBlocks(blockNumber, 1, headTopology.id);
|
|
10283
|
-
if (bNum)
|
|
10284
|
-
return bNum;
|
|
10285
|
-
if (!previousId)
|
|
10286
|
-
previousId = lastId;
|
|
10287
|
-
blockNumber += 1;
|
|
10288
|
-
}
|
|
10289
|
-
throw new Error(`Transaction not mined after ${timeout} ms. Blocks checked from ${iniBlock} to ${blockNumber}`);
|
|
10290
|
-
},
|
|
10227
|
+
async wait(txId, type = "byBlock", timeout = 30000) {
|
|
10228
|
+
const iniTime = Date.now();
|
|
10229
|
+
if (type === "byTransactionId") {
|
|
10230
|
+
while (Date.now() < iniTime + timeout) {
|
|
10231
|
+
await sleep(1000);
|
|
10232
|
+
const { transactions } = await this.getTransactionsById([txId]);
|
|
10233
|
+
if (transactions &&
|
|
10234
|
+
transactions[0] &&
|
|
10235
|
+
transactions[0].containing_blocks)
|
|
10236
|
+
return transactions[0].containing_blocks[0];
|
|
10237
|
+
}
|
|
10238
|
+
throw new Error(`Transaction not mined after ${timeout} ms`);
|
|
10239
|
+
}
|
|
10240
|
+
// byBlock
|
|
10241
|
+
const findTxInBlocks = async (ini, numBlocks, idRef) => {
|
|
10242
|
+
const blocks = await this.getBlocks(ini, numBlocks, idRef);
|
|
10243
|
+
let bNum = 0;
|
|
10244
|
+
blocks.forEach((block) => {
|
|
10245
|
+
if (!block ||
|
|
10246
|
+
!block.block ||
|
|
10247
|
+
!block.block_id ||
|
|
10248
|
+
!block.block.transactions)
|
|
10249
|
+
return;
|
|
10250
|
+
const tx = block.block.transactions.find((t) => t.id === txId);
|
|
10251
|
+
if (tx)
|
|
10252
|
+
bNum = Number(block.block_height);
|
|
10253
|
+
});
|
|
10254
|
+
const lastId = blocks[blocks.length - 1].block_id;
|
|
10255
|
+
return [bNum, lastId];
|
|
10291
10256
|
};
|
|
10257
|
+
let blockNumber = 0;
|
|
10258
|
+
let iniBlock = 0;
|
|
10259
|
+
let previousId = "";
|
|
10260
|
+
while (Date.now() < iniTime + timeout) {
|
|
10261
|
+
await sleep(1000);
|
|
10262
|
+
const { head_topology: headTopology } = await this.getHeadInfo();
|
|
10263
|
+
if (blockNumber === 0) {
|
|
10264
|
+
blockNumber = Number(headTopology.height);
|
|
10265
|
+
iniBlock = blockNumber;
|
|
10266
|
+
}
|
|
10267
|
+
if (Number(headTopology.height) === blockNumber - 1 &&
|
|
10268
|
+
previousId &&
|
|
10269
|
+
previousId !== headTopology.id) {
|
|
10270
|
+
const [bNum, lastId] = await findTxInBlocks(iniBlock, Number(headTopology.height) - iniBlock + 1, headTopology.id);
|
|
10271
|
+
if (bNum)
|
|
10272
|
+
return bNum;
|
|
10273
|
+
previousId = lastId;
|
|
10274
|
+
blockNumber = Number(headTopology.height) + 1;
|
|
10275
|
+
}
|
|
10276
|
+
// eslint-disable-next-line no-continue
|
|
10277
|
+
if (blockNumber > Number(headTopology.height))
|
|
10278
|
+
continue;
|
|
10279
|
+
const [bNum, lastId] = await findTxInBlocks(blockNumber, 1, headTopology.id);
|
|
10280
|
+
if (bNum)
|
|
10281
|
+
return bNum;
|
|
10282
|
+
if (!previousId)
|
|
10283
|
+
previousId = lastId;
|
|
10284
|
+
blockNumber += 1;
|
|
10285
|
+
}
|
|
10286
|
+
throw new Error(`Transaction not mined after ${timeout} ms. Blocks checked from ${iniBlock} to ${blockNumber}`);
|
|
10287
|
+
}
|
|
10288
|
+
/**
|
|
10289
|
+
* Function to call "chain.submit_transaction" to send a signed
|
|
10290
|
+
* transaction to the blockchain.
|
|
10291
|
+
*/
|
|
10292
|
+
async sendTransaction(transaction) {
|
|
10293
|
+
return this.call("chain.submit_transaction", { transaction });
|
|
10292
10294
|
}
|
|
10293
10295
|
/**
|
|
10294
10296
|
* Function to call "chain.read_contract" to read a contract.
|
|
@@ -10734,7 +10736,15 @@ class Signer {
|
|
|
10734
10736
|
await this.signTransaction(tx);
|
|
10735
10737
|
if (!this.provider)
|
|
10736
10738
|
throw new Error("provider is undefined");
|
|
10737
|
-
|
|
10739
|
+
await this.provider.sendTransaction(tx);
|
|
10740
|
+
return {
|
|
10741
|
+
...tx,
|
|
10742
|
+
wait: async (type = "byBlock", timeout = 30000) => {
|
|
10743
|
+
if (!this.provider)
|
|
10744
|
+
throw new Error("provider is undefined");
|
|
10745
|
+
return this.provider.wait(tx.id, type, timeout);
|
|
10746
|
+
},
|
|
10747
|
+
};
|
|
10738
10748
|
}
|
|
10739
10749
|
/**
|
|
10740
10750
|
* Function to recover the public key from a signed
|
|
@@ -10869,7 +10879,7 @@ class Signer {
|
|
|
10869
10879
|
* Function to encode a transaction
|
|
10870
10880
|
* @param activeData - Active data consists of nonce, rc_limit, and
|
|
10871
10881
|
* operations. Do not set the nonce to get it from the blockchain
|
|
10872
|
-
* using the provider. The rc_limit is
|
|
10882
|
+
* using the provider. The rc_limit is 1e8 by default.
|
|
10873
10883
|
* @returns A transaction encoded. The active field is encoded in
|
|
10874
10884
|
* base64url
|
|
10875
10885
|
*/
|
|
@@ -10882,7 +10892,7 @@ class Signer {
|
|
|
10882
10892
|
// this depends on the final architecture for names on Koinos
|
|
10883
10893
|
nonce = await this.provider.getNonce(this.getAddress());
|
|
10884
10894
|
}
|
|
10885
|
-
const rcLimit = activeData.rc_limit === undefined ?
|
|
10895
|
+
const rcLimit = activeData.rc_limit === undefined ? 1e8 : activeData.rc_limit;
|
|
10886
10896
|
const operations = activeData.operations ? activeData.operations : [];
|
|
10887
10897
|
const activeData2 = {
|
|
10888
10898
|
rc_limit: rcLimit,
|