koilib 3.1.1 → 4.1.1
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 +23 -20
- package/dist/koinos.js +76 -75
- package/dist/koinos.min.js +1 -1
- package/lib/Contract.d.ts +8 -8
- package/lib/Contract.js +42 -46
- package/lib/Contract.js.map +1 -1
- package/lib/Signer.js +1 -1
- package/lib/Signer.js.map +1 -1
- package/lib/browser/Contract.d.ts +8 -8
- package/lib/browser/Contract.js +42 -46
- package/lib/browser/Contract.js.map +1 -1
- package/lib/browser/Signer.js +1 -1
- package/lib/browser/Signer.js.map +1 -1
- package/lib/browser/interface.d.ts +24 -24
- package/lib/browser/utils.js +33 -28
- package/lib/browser/utils.js.map +1 -1
- package/lib/interface.d.ts +24 -24
- package/lib/utils.js +33 -28
- package/lib/utils.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -99,10 +99,12 @@ a transaction, and read contracts.
|
|
|
99
99
|
const koin = koinContract.functions;
|
|
100
100
|
|
|
101
101
|
// optional: preformat input/output
|
|
102
|
-
koinContract.abi.methods.balanceOf.
|
|
103
|
-
|
|
102
|
+
koinContract.abi.methods.balanceOf.preformat_argument = (owner) => ({
|
|
103
|
+
owner,
|
|
104
|
+
});
|
|
105
|
+
koinContract.abi.methods.balanceOf.preformat_return = (res) =>
|
|
104
106
|
utils.formatUnits(res.value, 8);
|
|
105
|
-
koinContract.abi.methods.transfer.
|
|
107
|
+
koinContract.abi.methods.transfer.preformat_argument = (input) => ({
|
|
106
108
|
from: signer.getAddress(),
|
|
107
109
|
to: input.to,
|
|
108
110
|
value: utils.parseUnits(input.value, 8),
|
|
@@ -222,28 +224,28 @@ const tokenJson = require("./token-proto.json");
|
|
|
222
224
|
const abiToken = {
|
|
223
225
|
methods: {
|
|
224
226
|
balanceOf: {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
227
|
+
entry_point: 0x5c721497,
|
|
228
|
+
argument: "balance_of_arguments",
|
|
229
|
+
return: "balance_of_result",
|
|
230
|
+
read_only: true,
|
|
231
|
+
default_output: { value: "0" },
|
|
230
232
|
},
|
|
231
233
|
transfer: {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
234
|
+
entry_point: 0x27f576ca,
|
|
235
|
+
argument: "transfer_arguments",
|
|
236
|
+
return: "transfer_result",
|
|
235
237
|
},
|
|
236
238
|
mint: {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
239
|
+
entry_point: 0xdc6f17bb,
|
|
240
|
+
argument: "mint_argumnets",
|
|
241
|
+
return: "mint_result",
|
|
240
242
|
},
|
|
241
243
|
},
|
|
242
|
-
|
|
244
|
+
koilib_types: tokenJson,
|
|
243
245
|
};
|
|
244
246
|
```
|
|
245
247
|
|
|
246
|
-
Note that this example uses "
|
|
248
|
+
Note that this example uses "default_output" for the method
|
|
247
249
|
"balanceOf". This is used when the smart contract returns an
|
|
248
250
|
empty response (for instance when there are no balance records
|
|
249
251
|
for a specific address) and you require a default output in
|
|
@@ -267,13 +269,14 @@ such cases.
|
|
|
267
269
|
For the ABI you need the .proto file and the library
|
|
268
270
|
[protobufjs](https://www.npmjs.com/package/protobufjs). Then follow the format
|
|
269
271
|
for the ABI as described in the previous section. It's important to note that
|
|
270
|
-
this ABI
|
|
271
|
-
In particular,
|
|
272
|
-
|
|
272
|
+
this ABI has a diffence with respect to the ABI used in [koinos-cli](https://docs.koinos.io/architecture/contract-abi.html).
|
|
273
|
+
In particular, koilib takes the descriptor from `koilib_types`, which is a
|
|
274
|
+
descriptor in json format, while the ABI in koinos-cli takes the descriptor from
|
|
275
|
+
`types`, which is a descriptor in binary format.
|
|
273
276
|
|
|
274
277
|
4. Can this library be used to interact with smart contracts?
|
|
275
278
|
|
|
276
|
-
Yes. You can use it to call
|
|
279
|
+
Yes. You can use it to call read_only functions, or send transactions
|
|
277
280
|
to the contract by calling write functions.
|
|
278
281
|
|
|
279
282
|
## Documentation
|
package/dist/koinos.js
CHANGED
|
@@ -9791,15 +9791,15 @@ const utils_1 = __webpack_require__(8593);
|
|
|
9791
9791
|
* });
|
|
9792
9792
|
* const koin = koinContract.functions;
|
|
9793
9793
|
*
|
|
9794
|
-
* // optional: preformat
|
|
9795
|
-
* koinContract.abi.methods.balanceOf.
|
|
9794
|
+
* // optional: preformat argument/return
|
|
9795
|
+
* koinContract.abi.methods.balanceOf.preformat_argument = (owner) =>
|
|
9796
9796
|
* ({ owner });
|
|
9797
|
-
* koinContract.abi.methods.balanceOf.
|
|
9797
|
+
* koinContract.abi.methods.balanceOf.preformat_return = (res) =>
|
|
9798
9798
|
* utils.formatUnits(res.value, 8);
|
|
9799
|
-
* koinContract.abi.methods.transfer.
|
|
9799
|
+
* koinContract.abi.methods.transfer.preformat_argument = (arg) => ({
|
|
9800
9800
|
* from: signer.getAddress(),
|
|
9801
|
-
* to:
|
|
9802
|
-
* value: utils.parseUnits(
|
|
9801
|
+
* to: arg.to,
|
|
9802
|
+
* value: utils.parseUnits(arg.value, 8),
|
|
9803
9803
|
* });
|
|
9804
9804
|
*
|
|
9805
9805
|
* async funtion main() {
|
|
@@ -9839,8 +9839,8 @@ class Contract {
|
|
|
9839
9839
|
if (c.serializer) {
|
|
9840
9840
|
this.serializer = c.serializer;
|
|
9841
9841
|
}
|
|
9842
|
-
else if (c.abi && c.abi.
|
|
9843
|
-
this.serializer = new Serializer_1.Serializer(c.abi.
|
|
9842
|
+
else if (c.abi && c.abi.koilib_types) {
|
|
9843
|
+
this.serializer = new Serializer_1.Serializer(c.abi.koilib_types);
|
|
9844
9844
|
}
|
|
9845
9845
|
this.options = {
|
|
9846
9846
|
signTransaction: true,
|
|
@@ -9866,10 +9866,10 @@ class Contract {
|
|
|
9866
9866
|
...this.options,
|
|
9867
9867
|
...options,
|
|
9868
9868
|
};
|
|
9869
|
-
const { readOnly, output, defaultOutput,
|
|
9869
|
+
const { read_only: readOnly, return: output, default_output: defaultOutput, preformat_argument: preformatArgument, preformat_return: preformatReturn, } = this.abi.methods[name];
|
|
9870
9870
|
let args;
|
|
9871
|
-
if (typeof
|
|
9872
|
-
args =
|
|
9871
|
+
if (typeof preformatArgument === "function") {
|
|
9872
|
+
args = preformatArgument(argu);
|
|
9873
9873
|
}
|
|
9874
9874
|
else {
|
|
9875
9875
|
args = argu;
|
|
@@ -9884,15 +9884,15 @@ class Contract {
|
|
|
9884
9884
|
if (resultEncoded) {
|
|
9885
9885
|
result = await this.serializer.deserialize(resultEncoded, output);
|
|
9886
9886
|
}
|
|
9887
|
-
if (typeof
|
|
9888
|
-
result =
|
|
9887
|
+
if (typeof preformatReturn === "function") {
|
|
9888
|
+
result = preformatReturn(result);
|
|
9889
9889
|
}
|
|
9890
9890
|
return { operation, result };
|
|
9891
9891
|
}
|
|
9892
9892
|
// write contract (sign and send)
|
|
9893
9893
|
if (!this.signer)
|
|
9894
9894
|
throw new Error("signer not found");
|
|
9895
|
-
|
|
9895
|
+
let tx = await this.signer.prepareTransaction({
|
|
9896
9896
|
header: {
|
|
9897
9897
|
...((opts === null || opts === void 0 ? void 0 : opts.chainId) && { chain_id: opts === null || opts === void 0 ? void 0 : opts.chainId }),
|
|
9898
9898
|
...((opts === null || opts === void 0 ? void 0 : opts.rcLimit) && { rc_limit: opts === null || opts === void 0 ? void 0 : opts.rcLimit }),
|
|
@@ -9913,7 +9913,7 @@ class Contract {
|
|
|
9913
9913
|
throw new Error("This transaction was not broadcasted");
|
|
9914
9914
|
};
|
|
9915
9915
|
if (opts.signTransaction)
|
|
9916
|
-
await this.signer.signTransaction(tx, abis);
|
|
9916
|
+
tx = await this.signer.signTransaction(tx, abis);
|
|
9917
9917
|
return { operation, transaction: { ...tx, wait: noWait } };
|
|
9918
9918
|
}
|
|
9919
9919
|
const { transaction, receipt } = await this.signer.sendTransaction(tx, abis);
|
|
@@ -9982,13 +9982,26 @@ class Contract {
|
|
|
9982
9982
|
...this.options,
|
|
9983
9983
|
...options,
|
|
9984
9984
|
};
|
|
9985
|
+
const contractId = this.id
|
|
9986
|
+
? (0, utils_1.encodeBase58)(this.id)
|
|
9987
|
+
: this.signer.getAddress();
|
|
9985
9988
|
const operation = {
|
|
9986
9989
|
upload_contract: {
|
|
9987
|
-
contract_id:
|
|
9988
|
-
bytecode: this.bytecode,
|
|
9990
|
+
contract_id: contractId,
|
|
9991
|
+
bytecode: (0, utils_1.encodeBase64url)(this.bytecode),
|
|
9992
|
+
...((opts === null || opts === void 0 ? void 0 : opts.abi) && { abi: opts === null || opts === void 0 ? void 0 : opts.abi }),
|
|
9993
|
+
...((opts === null || opts === void 0 ? void 0 : opts.authorizesCallContract) && {
|
|
9994
|
+
authorizes_call_contract: opts === null || opts === void 0 ? void 0 : opts.authorizesCallContract,
|
|
9995
|
+
}),
|
|
9996
|
+
...((opts === null || opts === void 0 ? void 0 : opts.authorizesTransactionApplication) && {
|
|
9997
|
+
authorizes_transaction_application: opts === null || opts === void 0 ? void 0 : opts.authorizesTransactionApplication,
|
|
9998
|
+
}),
|
|
9999
|
+
...((opts === null || opts === void 0 ? void 0 : opts.authorizesUploadContract) && {
|
|
10000
|
+
authorizes_upload_contract: opts === null || opts === void 0 ? void 0 : opts.authorizesUploadContract,
|
|
10001
|
+
}),
|
|
9989
10002
|
},
|
|
9990
10003
|
};
|
|
9991
|
-
|
|
10004
|
+
let tx = await this.signer.prepareTransaction({
|
|
9992
10005
|
header: {
|
|
9993
10006
|
...((opts === null || opts === void 0 ? void 0 : opts.chainId) && { chain_id: opts === null || opts === void 0 ? void 0 : opts.chainId }),
|
|
9994
10007
|
...((opts === null || opts === void 0 ? void 0 : opts.rcLimit) && { rc_limit: opts === null || opts === void 0 ? void 0 : opts.rcLimit }),
|
|
@@ -9996,24 +10009,7 @@ class Contract {
|
|
|
9996
10009
|
...((opts === null || opts === void 0 ? void 0 : opts.payer) && { payer: opts === null || opts === void 0 ? void 0 : opts.payer }),
|
|
9997
10010
|
...((opts === null || opts === void 0 ? void 0 : opts.payee) && { payee: opts === null || opts === void 0 ? void 0 : opts.payee }),
|
|
9998
10011
|
},
|
|
9999
|
-
operations: [
|
|
10000
|
-
{
|
|
10001
|
-
upload_contract: {
|
|
10002
|
-
contract_id: (0, utils_1.encodeBase58)(operation.upload_contract.contract_id),
|
|
10003
|
-
bytecode: (0, utils_1.encodeBase64url)(this.bytecode),
|
|
10004
|
-
...((opts === null || opts === void 0 ? void 0 : opts.abi) && { abi: opts === null || opts === void 0 ? void 0 : opts.abi }),
|
|
10005
|
-
...((opts === null || opts === void 0 ? void 0 : opts.authorizesCallContract) && {
|
|
10006
|
-
authorizes_call_contract: opts === null || opts === void 0 ? void 0 : opts.authorizesCallContract,
|
|
10007
|
-
}),
|
|
10008
|
-
...((opts === null || opts === void 0 ? void 0 : opts.authorizesTransactionApplication) && {
|
|
10009
|
-
authorizes_transaction_application: opts === null || opts === void 0 ? void 0 : opts.authorizesTransactionApplication,
|
|
10010
|
-
}),
|
|
10011
|
-
...((opts === null || opts === void 0 ? void 0 : opts.authorizesUploadContract) && {
|
|
10012
|
-
authorizes_upload_contract: opts === null || opts === void 0 ? void 0 : opts.authorizesUploadContract,
|
|
10013
|
-
}),
|
|
10014
|
-
},
|
|
10015
|
-
},
|
|
10016
|
-
],
|
|
10012
|
+
operations: [operation],
|
|
10017
10013
|
});
|
|
10018
10014
|
// return result if the transaction will not be broadcasted
|
|
10019
10015
|
if (!(opts === null || opts === void 0 ? void 0 : opts.sendTransaction)) {
|
|
@@ -10021,7 +10017,7 @@ class Contract {
|
|
|
10021
10017
|
throw new Error("This transaction was not broadcasted");
|
|
10022
10018
|
};
|
|
10023
10019
|
if (opts.signTransaction)
|
|
10024
|
-
await this.signer.signTransaction(tx);
|
|
10020
|
+
tx = await this.signer.signTransaction(tx);
|
|
10025
10021
|
return { operation, transaction: { ...tx, wait: noWait } };
|
|
10026
10022
|
}
|
|
10027
10023
|
const { transaction, receipt } = await this.signer.sendTransaction(tx);
|
|
@@ -10061,17 +10057,17 @@ class Contract {
|
|
|
10061
10057
|
if (!this.id)
|
|
10062
10058
|
throw new Error("Contract id is not defined");
|
|
10063
10059
|
const method = this.abi.methods[op.name];
|
|
10064
|
-
let
|
|
10065
|
-
if (method.
|
|
10060
|
+
let bufferArguments = new Uint8Array(0);
|
|
10061
|
+
if (method.argument) {
|
|
10066
10062
|
if (!op.args)
|
|
10067
|
-
throw new Error(`No arguments defined for type '${method.
|
|
10068
|
-
|
|
10063
|
+
throw new Error(`No arguments defined for type '${method.argument}'`);
|
|
10064
|
+
bufferArguments = await this.serializer.serialize(op.args, method.argument);
|
|
10069
10065
|
}
|
|
10070
10066
|
return {
|
|
10071
10067
|
call_contract: {
|
|
10072
10068
|
contract_id: (0, utils_1.encodeBase58)(this.id),
|
|
10073
|
-
entry_point: method.
|
|
10074
|
-
args: (0, utils_1.encodeBase64url)(
|
|
10069
|
+
entry_point: method.entry_point,
|
|
10070
|
+
args: (0, utils_1.encodeBase64url)(bufferArguments),
|
|
10075
10071
|
},
|
|
10076
10072
|
};
|
|
10077
10073
|
}
|
|
@@ -10111,12 +10107,12 @@ class Contract {
|
|
|
10111
10107
|
for (let i = 0; i < Object.keys(this.abi.methods).length; i += 1) {
|
|
10112
10108
|
const opName = Object.keys(this.abi.methods)[i];
|
|
10113
10109
|
const method = this.abi.methods[opName];
|
|
10114
|
-
if (op.call_contract.entry_point === method.
|
|
10115
|
-
if (!method.
|
|
10110
|
+
if (op.call_contract.entry_point === method.entry_point) {
|
|
10111
|
+
if (!method.argument)
|
|
10116
10112
|
return { name: opName };
|
|
10117
10113
|
return {
|
|
10118
10114
|
name: opName,
|
|
10119
|
-
args: await this.serializer.deserialize(op.call_contract.args, method.
|
|
10115
|
+
args: await this.serializer.deserialize(op.call_contract.args, method.argument),
|
|
10120
10116
|
};
|
|
10121
10117
|
}
|
|
10122
10118
|
}
|
|
@@ -10868,7 +10864,7 @@ class Signer {
|
|
|
10868
10864
|
* // 5KEX4TMHG66fT7cM9HMZLmdp4hVq4LC4X2Fkg6zeypM5UteWmtd
|
|
10869
10865
|
* ```
|
|
10870
10866
|
*/
|
|
10871
|
-
getPrivateKey(format = "hex", compressed) {
|
|
10867
|
+
getPrivateKey(format = "hex", compressed = false) {
|
|
10872
10868
|
let stringPrivateKey;
|
|
10873
10869
|
if (this.privateKey instanceof Uint8Array) {
|
|
10874
10870
|
stringPrivateKey = (0, utils_1.toHexString)(this.privateKey);
|
|
@@ -11695,48 +11691,53 @@ exports.btypeEncode = btypeEncode;
|
|
|
11695
11691
|
exports.tokenAbi = {
|
|
11696
11692
|
methods: {
|
|
11697
11693
|
name: {
|
|
11698
|
-
|
|
11699
|
-
|
|
11700
|
-
|
|
11701
|
-
|
|
11694
|
+
entry_point: 0x82a3537f,
|
|
11695
|
+
argument: "name_arguments",
|
|
11696
|
+
return: "name_result",
|
|
11697
|
+
read_only: true,
|
|
11702
11698
|
},
|
|
11703
11699
|
symbol: {
|
|
11704
|
-
|
|
11705
|
-
|
|
11706
|
-
|
|
11707
|
-
|
|
11700
|
+
entry_point: 0xb76a7ca1,
|
|
11701
|
+
argument: "symbol_arguments",
|
|
11702
|
+
return: "symbol_result",
|
|
11703
|
+
read_only: true,
|
|
11708
11704
|
},
|
|
11709
11705
|
decimals: {
|
|
11710
|
-
|
|
11711
|
-
|
|
11712
|
-
|
|
11713
|
-
|
|
11706
|
+
entry_point: 0xee80fd2f,
|
|
11707
|
+
argument: "decimals_arguments",
|
|
11708
|
+
return: "decimals_result",
|
|
11709
|
+
read_only: true,
|
|
11714
11710
|
},
|
|
11715
11711
|
totalSupply: {
|
|
11716
|
-
|
|
11717
|
-
|
|
11718
|
-
|
|
11719
|
-
|
|
11712
|
+
entry_point: 0xb0da3934,
|
|
11713
|
+
argument: "total_supply_arguments",
|
|
11714
|
+
return: "total_supply_result",
|
|
11715
|
+
read_only: true,
|
|
11720
11716
|
},
|
|
11721
11717
|
balanceOf: {
|
|
11722
|
-
|
|
11723
|
-
|
|
11724
|
-
|
|
11725
|
-
|
|
11726
|
-
|
|
11718
|
+
entry_point: 0x5c721497,
|
|
11719
|
+
argument: "balance_of_arguments",
|
|
11720
|
+
return: "balance_of_result",
|
|
11721
|
+
read_only: true,
|
|
11722
|
+
default_output: { value: "0" },
|
|
11727
11723
|
},
|
|
11728
11724
|
transfer: {
|
|
11729
|
-
|
|
11730
|
-
|
|
11731
|
-
|
|
11725
|
+
entry_point: 0x27f576ca,
|
|
11726
|
+
argument: "transfer_arguments",
|
|
11727
|
+
return: "transfer_result",
|
|
11732
11728
|
},
|
|
11733
11729
|
mint: {
|
|
11734
|
-
|
|
11735
|
-
|
|
11736
|
-
|
|
11730
|
+
entry_point: 0xdc6f17bb,
|
|
11731
|
+
argument: "mint_arguments",
|
|
11732
|
+
return: "mint_result",
|
|
11733
|
+
},
|
|
11734
|
+
burn: {
|
|
11735
|
+
entry_point: 0x859facc5,
|
|
11736
|
+
argument: "burn_arguments",
|
|
11737
|
+
return: "burn_result",
|
|
11737
11738
|
},
|
|
11738
11739
|
},
|
|
11739
|
-
|
|
11740
|
+
koilib_types: token_proto_json_1.default,
|
|
11740
11741
|
};
|
|
11741
11742
|
//export const ProtocolTypes = protocolJson;
|
|
11742
11743
|
|