koilib 9.1.1 → 9.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/README.md +1 -0
- package/dist/koinos.js +1682 -393
- package/dist/koinos.min.js +1 -1
- package/lib/Multicall.d.ts +79 -0
- package/lib/Multicall.js +104 -0
- package/lib/Multicall.js.map +1 -0
- package/lib/Provider.d.ts +205 -0
- package/lib/Provider.js +247 -0
- package/lib/Provider.js.map +1 -1
- package/lib/abis/multicallAbi.d.ts +2 -0
- package/lib/abis/multicallAbi.js +127 -0
- package/lib/abis/multicallAbi.js.map +1 -0
- package/lib/browser/Multicall.d.ts +79 -0
- package/lib/browser/Multicall.js +104 -0
- package/lib/browser/Multicall.js.map +1 -0
- package/lib/browser/Provider.d.ts +205 -0
- package/lib/browser/Provider.js +247 -0
- package/lib/browser/Provider.js.map +1 -1
- package/lib/browser/abis/multicallAbi.d.ts +2 -0
- package/lib/browser/abis/multicallAbi.js +127 -0
- package/lib/browser/abis/multicallAbi.js.map +1 -0
- package/lib/browser/index.d.ts +1 -0
- package/lib/browser/index.js +1 -0
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index2.js +2 -0
- package/lib/browser/index2.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/index2.js +2 -0
- package/lib/index2.js.map +1 -1
- package/package.json +1 -1
- package/src/Multicall.ts +139 -0
- package/src/Provider.ts +389 -0
- package/src/abis/multicallAbi.ts +127 -0
- package/src/index.ts +1 -0
- package/src/index2.ts +2 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Contract } from "./Contract";
|
|
2
|
+
import { OperationJson } from "./interface";
|
|
3
|
+
import { ProviderInterface } from "./Provider";
|
|
4
|
+
import { Transaction } from "./Transaction";
|
|
5
|
+
export declare class Multicall {
|
|
6
|
+
/**
|
|
7
|
+
* Multicall contract address
|
|
8
|
+
*/
|
|
9
|
+
id: string;
|
|
10
|
+
transaction: Transaction;
|
|
11
|
+
contracts: Contract[];
|
|
12
|
+
/**
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const tokens = [
|
|
16
|
+
* "19GYjDBVXU7keLbYvMLazsGQn3GTWHjHkK", // koin
|
|
17
|
+
* "12Y5vW6gk8GceH53YfRkRre2Rrcsgw7Naq", // vhp
|
|
18
|
+
* "12VoHz41a4HtfiyhTWbg9RXqGMRbYk6pXh", // vortex usdt
|
|
19
|
+
* "1Tf1QKv3gVYLjq34yURSHw5ErTYbFjqTG", // vortex eth
|
|
20
|
+
* ];
|
|
21
|
+
*
|
|
22
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
23
|
+
*
|
|
24
|
+
* // create an array of the different contracts
|
|
25
|
+
* // used in the multicall
|
|
26
|
+
* const contracts = [];
|
|
27
|
+
* for (const token of tokens) {
|
|
28
|
+
* contracts.push(
|
|
29
|
+
* new Contract({
|
|
30
|
+
* id: token,
|
|
31
|
+
* provider,
|
|
32
|
+
* abi: utils.tokenAbi,
|
|
33
|
+
* })
|
|
34
|
+
* );
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* // create the multicall instance
|
|
38
|
+
* const multicall = new Multicall({
|
|
39
|
+
* provider,
|
|
40
|
+
* contracts
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // add operations to the multicall
|
|
44
|
+
* for (const contract of contracts) {
|
|
45
|
+
* await multicall.add(contract.functions.balanceOf, {
|
|
46
|
+
* owner: "1MdqwaSBy6rbasPJ9vmg2pZFJSVZ29GFpZ",
|
|
47
|
+
* });
|
|
48
|
+
* }
|
|
49
|
+
*
|
|
50
|
+
* // call the multicall
|
|
51
|
+
* const results = await multicall.call();
|
|
52
|
+
* console.log(results);
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* For testnet set the id to the multicall contract address for testnet
|
|
56
|
+
* ```ts
|
|
57
|
+
* const multicall = new Multicall({
|
|
58
|
+
* id: "1MM9ydA8SdgXvCTH3LdQ5FS8JvHijCmGSg",
|
|
59
|
+
* provider: new Provider("https://testnet-api.koinos.io"),
|
|
60
|
+
* contracts: [ koinContract, fogataPoolContract ],
|
|
61
|
+
* });
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
constructor(c: {
|
|
65
|
+
/**
|
|
66
|
+
* Multicall contract address. Leave it undefined
|
|
67
|
+
* to use the mainnet multicall contract address.
|
|
68
|
+
*/
|
|
69
|
+
id?: string;
|
|
70
|
+
provider?: ProviderInterface;
|
|
71
|
+
contracts?: Contract[];
|
|
72
|
+
});
|
|
73
|
+
add(input: OperationJson | {
|
|
74
|
+
operation: OperationJson;
|
|
75
|
+
} | Promise<{
|
|
76
|
+
operation: OperationJson;
|
|
77
|
+
}> | Contract["functions"]["x"], args?: unknown): Promise<void>;
|
|
78
|
+
call<T = Record<string, unknown>>(): Promise<(T | Error)[]>;
|
|
79
|
+
}
|
package/lib/Multicall.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Multicall = void 0;
|
|
4
|
+
const Contract_1 = require("./Contract");
|
|
5
|
+
const Transaction_1 = require("./Transaction");
|
|
6
|
+
const multicallAbi_1 = require("./abis/multicallAbi");
|
|
7
|
+
class Multicall {
|
|
8
|
+
/**
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const tokens = [
|
|
12
|
+
* "19GYjDBVXU7keLbYvMLazsGQn3GTWHjHkK", // koin
|
|
13
|
+
* "12Y5vW6gk8GceH53YfRkRre2Rrcsgw7Naq", // vhp
|
|
14
|
+
* "12VoHz41a4HtfiyhTWbg9RXqGMRbYk6pXh", // vortex usdt
|
|
15
|
+
* "1Tf1QKv3gVYLjq34yURSHw5ErTYbFjqTG", // vortex eth
|
|
16
|
+
* ];
|
|
17
|
+
*
|
|
18
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
19
|
+
*
|
|
20
|
+
* // create an array of the different contracts
|
|
21
|
+
* // used in the multicall
|
|
22
|
+
* const contracts = [];
|
|
23
|
+
* for (const token of tokens) {
|
|
24
|
+
* contracts.push(
|
|
25
|
+
* new Contract({
|
|
26
|
+
* id: token,
|
|
27
|
+
* provider,
|
|
28
|
+
* abi: utils.tokenAbi,
|
|
29
|
+
* })
|
|
30
|
+
* );
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* // create the multicall instance
|
|
34
|
+
* const multicall = new Multicall({
|
|
35
|
+
* provider,
|
|
36
|
+
* contracts
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* // add operations to the multicall
|
|
40
|
+
* for (const contract of contracts) {
|
|
41
|
+
* await multicall.add(contract.functions.balanceOf, {
|
|
42
|
+
* owner: "1MdqwaSBy6rbasPJ9vmg2pZFJSVZ29GFpZ",
|
|
43
|
+
* });
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* // call the multicall
|
|
47
|
+
* const results = await multicall.call();
|
|
48
|
+
* console.log(results);
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* For testnet set the id to the multicall contract address for testnet
|
|
52
|
+
* ```ts
|
|
53
|
+
* const multicall = new Multicall({
|
|
54
|
+
* id: "1MM9ydA8SdgXvCTH3LdQ5FS8JvHijCmGSg",
|
|
55
|
+
* provider: new Provider("https://testnet-api.koinos.io"),
|
|
56
|
+
* contracts: [ koinContract, fogataPoolContract ],
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
constructor(c) {
|
|
61
|
+
if (c.id)
|
|
62
|
+
this.id = c.id;
|
|
63
|
+
else
|
|
64
|
+
this.id = "18dVCqPG3gwgL7DWQAciKYNuXfzEg5LW7"; // multicontract address mainnet
|
|
65
|
+
this.transaction = new Transaction_1.Transaction({ provider: c.provider });
|
|
66
|
+
this.contracts = c.contracts || [];
|
|
67
|
+
}
|
|
68
|
+
async add(input, args) {
|
|
69
|
+
await this.transaction.pushOperation(input, args);
|
|
70
|
+
}
|
|
71
|
+
async call() {
|
|
72
|
+
const multicallContract = new Contract_1.Contract({
|
|
73
|
+
id: this.id,
|
|
74
|
+
provider: this.transaction.provider,
|
|
75
|
+
abi: multicallAbi_1.multicallAbi,
|
|
76
|
+
});
|
|
77
|
+
const calls = this.transaction.transaction.operations.map((op) => op.call_contract);
|
|
78
|
+
const { result: multicallResult } = await multicallContract.functions.get({
|
|
79
|
+
calls,
|
|
80
|
+
});
|
|
81
|
+
const results = [];
|
|
82
|
+
for (let i = 0; i < calls.length; i += 1) {
|
|
83
|
+
const contract = this.contracts.find((c) => c.getId() === calls[i].contract_id);
|
|
84
|
+
if (contract && contract.abi) {
|
|
85
|
+
const method = Object.values(contract.abi.methods).find((method) => {
|
|
86
|
+
return method.entry_point === calls[i].entry_point;
|
|
87
|
+
});
|
|
88
|
+
if (method) {
|
|
89
|
+
const decoded = (await contract.serializer.deserialize(multicallResult.results[i].res.object, method.return));
|
|
90
|
+
results.push(decoded);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
results.push(new Error(`Method ${calls[i].entry_point} not found in contract ${calls[i].contract_id}`));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
results.push(new Error(`Contract ${calls[i].contract_id} not found`));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return results;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.Multicall = Multicall;
|
|
104
|
+
//# sourceMappingURL=Multicall.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Multicall.js","sourceRoot":"","sources":["../src/Multicall.ts"],"names":[],"mappings":";;;AAAA,yCAAsC;AAGtC,+CAA4C;AAC5C,sDAAmD;AAEnD,MAAa,SAAS;IAUpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmDG;IACH,YAAY,CAQX;QACC,IAAI,CAAC,CAAC,EAAE;YAAE,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC;;YACpB,IAAI,CAAC,EAAE,GAAG,mCAAmC,CAAC,CAAC,gCAAgC;QACpF,IAAI,CAAC,WAAW,GAAG,IAAI,yBAAW,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC7D,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC,SAAS,IAAI,EAAE,CAAC;IACrC,CAAC;IAED,KAAK,CAAC,GAAG,CACP,KAI8B,EAC9B,IAAc;QAEd,MAAM,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACpD,CAAC;IAED,KAAK,CAAC,IAAI;QACR,MAAM,iBAAiB,GAAG,IAAI,mBAAQ,CAAC;YACrC,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,QAAQ;YACnC,GAAG,EAAE,2BAAY;SAClB,CAAC,CAAC;QAEH,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,UAAW,CAAC,GAAG,CACxD,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,aAAc,CAC1B,CAAC;QACF,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,GAAG,MAAM,iBAAiB,CAAC,SAAS,CAAC,GAAG,CAAC;YACxE,KAAK;SACN,CAAC,CAAC;QAEH,MAAM,OAAO,GAAkB,EAAE,CAAC;QAElC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;YACxC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAClC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,CAC1C,CAAC;YACF,IAAI,QAAQ,IAAI,QAAQ,CAAC,GAAG,EAAE;gBAC5B,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;oBACjE,OAAO,MAAM,CAAC,WAAW,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;gBACrD,CAAC,CAAC,CAAC;gBACH,IAAI,MAAM,EAAE;oBACV,MAAM,OAAO,GAAG,CAAC,MAAM,QAAS,CAAC,UAAW,CAAC,WAAW,CACtD,eAAgB,CAAC,OAAO,CAAC,CAAC,CAAE,CAAC,GAAG,CAAC,MAAM,EACvC,MAAM,CAAC,MAAM,CACd,CAAM,CAAC;oBACR,OAAO,CAAC,IAAI,CAAC,OAAY,CAAC,CAAC;iBAC5B;qBAAM;oBACL,OAAO,CAAC,IAAI,CACV,IAAI,KAAK,CACP,UAAU,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,0BAA0B,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAC/E,CACF,CAAC;iBACH;aACF;iBAAM;gBACL,OAAO,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,YAAY,CAAC,CAAC,CAAC;aACvE;SACF;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;CACF;AApID,8BAoIC"}
|
package/lib/Provider.d.ts
CHANGED
|
@@ -83,6 +83,48 @@ export interface ProviderInterface {
|
|
|
83
83
|
address: string;
|
|
84
84
|
};
|
|
85
85
|
} | undefined>;
|
|
86
|
+
invokeGetObject?: (args: {
|
|
87
|
+
address: string;
|
|
88
|
+
id: number;
|
|
89
|
+
key?: string;
|
|
90
|
+
system?: boolean;
|
|
91
|
+
zone?: string;
|
|
92
|
+
}) => Promise<string | undefined>;
|
|
93
|
+
invokeGetNextObject?: (args: {
|
|
94
|
+
address: string;
|
|
95
|
+
id: number;
|
|
96
|
+
key?: string;
|
|
97
|
+
system?: boolean;
|
|
98
|
+
zone?: string;
|
|
99
|
+
}) => Promise<{
|
|
100
|
+
exists: boolean;
|
|
101
|
+
value: string;
|
|
102
|
+
key: string;
|
|
103
|
+
} | undefined>;
|
|
104
|
+
invokeGetPrevObject?: (args: {
|
|
105
|
+
address: string;
|
|
106
|
+
id: number;
|
|
107
|
+
key?: string;
|
|
108
|
+
system?: boolean;
|
|
109
|
+
zone?: string;
|
|
110
|
+
}) => Promise<{
|
|
111
|
+
exists: boolean;
|
|
112
|
+
value: string;
|
|
113
|
+
key: string;
|
|
114
|
+
} | undefined>;
|
|
115
|
+
invokeGetManyObjects?: (args: {
|
|
116
|
+
address: string;
|
|
117
|
+
id: number;
|
|
118
|
+
key?: string;
|
|
119
|
+
limit?: number;
|
|
120
|
+
direction?: "ascending" | "descending";
|
|
121
|
+
system?: boolean;
|
|
122
|
+
zone?: string;
|
|
123
|
+
}) => Promise<{
|
|
124
|
+
exists: boolean;
|
|
125
|
+
value: string;
|
|
126
|
+
key: string;
|
|
127
|
+
}[]>;
|
|
86
128
|
}
|
|
87
129
|
/**
|
|
88
130
|
* Class to connect with the RPC node
|
|
@@ -571,5 +613,168 @@ export declare class Provider implements ProviderInterface {
|
|
|
571
613
|
address: string;
|
|
572
614
|
};
|
|
573
615
|
} | undefined>;
|
|
616
|
+
invokeGetObjectGeneric({ systemCallIdOrName, address, id, key, system, zone, }: {
|
|
617
|
+
systemCallIdOrName: string | number;
|
|
618
|
+
address: string;
|
|
619
|
+
id: number;
|
|
620
|
+
key?: string;
|
|
621
|
+
system?: boolean;
|
|
622
|
+
zone?: string;
|
|
623
|
+
}): Promise<{
|
|
624
|
+
exists: boolean;
|
|
625
|
+
value: string;
|
|
626
|
+
key: string;
|
|
627
|
+
} | undefined>;
|
|
628
|
+
/**
|
|
629
|
+
* Function to get an object from the database via the get_object system call.
|
|
630
|
+
* @param args - object space and key
|
|
631
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
632
|
+
* @param args.id - object space id
|
|
633
|
+
* @param args.key - object key (defaults to empty)
|
|
634
|
+
* @param args.zone - object space zone (defaults to address)
|
|
635
|
+
*
|
|
636
|
+
* @example
|
|
637
|
+
* ```ts
|
|
638
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
639
|
+
* const result = await provider.invokeGetObject({
|
|
640
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
641
|
+
* id: 0,
|
|
642
|
+
* });
|
|
643
|
+
* console.log(result);
|
|
644
|
+
*
|
|
645
|
+
* // {
|
|
646
|
+
* // exists: true,
|
|
647
|
+
* // value: '...',
|
|
648
|
+
* // key: ''
|
|
649
|
+
* // }
|
|
650
|
+
* ```
|
|
651
|
+
*/
|
|
652
|
+
invokeGetObject({ address, id, key, system, zone, }: {
|
|
653
|
+
address: string;
|
|
654
|
+
id: number;
|
|
655
|
+
key?: string;
|
|
656
|
+
system?: boolean;
|
|
657
|
+
zone?: string;
|
|
658
|
+
}): Promise<string | undefined>;
|
|
659
|
+
/**
|
|
660
|
+
* Function to get the next object from the database via the
|
|
661
|
+
* get_next_object system call.
|
|
662
|
+
* @param args - object space and key
|
|
663
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
664
|
+
* @param args.id - object space id
|
|
665
|
+
* @param args.key - object key to seek from (defaults to empty)
|
|
666
|
+
* @param args.system - whether the space is a system space
|
|
667
|
+
* @param args.zone - object space zone (defaults to address)
|
|
668
|
+
*
|
|
669
|
+
* @example
|
|
670
|
+
* ```ts
|
|
671
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
672
|
+
* const result = await provider.invokeGetNextObject({
|
|
673
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
674
|
+
* id: 0,
|
|
675
|
+
* });
|
|
676
|
+
* console.log(result);
|
|
677
|
+
*
|
|
678
|
+
* // {
|
|
679
|
+
* // exists: true,
|
|
680
|
+
* // value: '...',
|
|
681
|
+
* // key: '...'
|
|
682
|
+
* // }
|
|
683
|
+
* ```
|
|
684
|
+
*/
|
|
685
|
+
invokeGetNextObject({ address, id, key, system, zone, }: {
|
|
686
|
+
address: string;
|
|
687
|
+
id: number;
|
|
688
|
+
key?: string;
|
|
689
|
+
system?: boolean;
|
|
690
|
+
zone?: string;
|
|
691
|
+
}): Promise<{
|
|
692
|
+
exists: boolean;
|
|
693
|
+
value: string;
|
|
694
|
+
key: string;
|
|
695
|
+
} | undefined>;
|
|
696
|
+
/**
|
|
697
|
+
* Function to get the previous object from the database via the
|
|
698
|
+
* get_prev_object system call.
|
|
699
|
+
* @param args - object space and key
|
|
700
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
701
|
+
* @param args.id - object space id
|
|
702
|
+
* @param args.key - object key to seek from (defaults to empty)
|
|
703
|
+
* @param args.system - whether the space is a system space
|
|
704
|
+
* @param args.zone - object space zone (defaults to address)
|
|
705
|
+
*
|
|
706
|
+
* @example
|
|
707
|
+
* ```ts
|
|
708
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
709
|
+
* const result = await provider.invokeGetPrevObject({
|
|
710
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
711
|
+
* id: 0,
|
|
712
|
+
* key: "...",
|
|
713
|
+
* });
|
|
714
|
+
* console.log(result);
|
|
715
|
+
*
|
|
716
|
+
* // {
|
|
717
|
+
* // exists: true,
|
|
718
|
+
* // value: '...',
|
|
719
|
+
* // key: '...'
|
|
720
|
+
* // }
|
|
721
|
+
* ```
|
|
722
|
+
*/
|
|
723
|
+
invokeGetPrevObject({ address, id, key, system, zone, }: {
|
|
724
|
+
address: string;
|
|
725
|
+
id: number;
|
|
726
|
+
key?: string;
|
|
727
|
+
system?: boolean;
|
|
728
|
+
zone?: string;
|
|
729
|
+
}): Promise<{
|
|
730
|
+
exists: boolean;
|
|
731
|
+
value: string;
|
|
732
|
+
key: string;
|
|
733
|
+
} | undefined>;
|
|
734
|
+
/**
|
|
735
|
+
* Function to get many objects from the database by repeatedly
|
|
736
|
+
* calling get_next_object or get_prev_object.
|
|
737
|
+
* @param args - object space and pagination options
|
|
738
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
739
|
+
* @param args.id - object space id
|
|
740
|
+
* @param args.key - offset key to seek from (defaults to empty)
|
|
741
|
+
* @param args.limit - maximum number of objects to return
|
|
742
|
+
* @param args.direction - traversal direction ("ascending" or "descending")
|
|
743
|
+
* @param args.system - whether the space is a system space
|
|
744
|
+
* @param args.zone - object space zone (defaults to address)
|
|
745
|
+
*
|
|
746
|
+
* @example
|
|
747
|
+
* ```ts
|
|
748
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
749
|
+
* const result = await provider.invokeGetManyObjects({
|
|
750
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
751
|
+
* id: 0,
|
|
752
|
+
* limit: 10,
|
|
753
|
+
* });
|
|
754
|
+
* console.log(result);
|
|
755
|
+
*
|
|
756
|
+
* // [
|
|
757
|
+
* // {
|
|
758
|
+
* // exists: true,
|
|
759
|
+
* // value: '...',
|
|
760
|
+
* // key: '...'
|
|
761
|
+
* // },
|
|
762
|
+
* // ...
|
|
763
|
+
* // ]
|
|
764
|
+
* ```
|
|
765
|
+
*/
|
|
766
|
+
invokeGetManyObjects({ address, id, key, limit, direction, system, zone, }: {
|
|
767
|
+
address: string;
|
|
768
|
+
id: number;
|
|
769
|
+
key?: string;
|
|
770
|
+
limit?: number;
|
|
771
|
+
direction?: "ascending" | "descending";
|
|
772
|
+
system?: boolean;
|
|
773
|
+
zone?: string;
|
|
774
|
+
}): Promise<{
|
|
775
|
+
exists: boolean;
|
|
776
|
+
value: string;
|
|
777
|
+
key: string;
|
|
778
|
+
}[]>;
|
|
574
779
|
}
|
|
575
780
|
export default Provider;
|
package/lib/Provider.js
CHANGED
|
@@ -10,6 +10,65 @@ const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
|
10
10
|
const protocol_proto_js_1 = require("./protoModules/protocol-proto.js");
|
|
11
11
|
const utils_1 = require("./utils");
|
|
12
12
|
const Serializer_1 = require("./Serializer");
|
|
13
|
+
const getObjectTypes = {
|
|
14
|
+
nested: {
|
|
15
|
+
get_object_arguments: {
|
|
16
|
+
fields: {
|
|
17
|
+
space: {
|
|
18
|
+
type: "object_space",
|
|
19
|
+
id: 1,
|
|
20
|
+
},
|
|
21
|
+
key: {
|
|
22
|
+
type: "bytes",
|
|
23
|
+
id: 2,
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
get_object_result: {
|
|
28
|
+
fields: {
|
|
29
|
+
value: {
|
|
30
|
+
type: "database_object",
|
|
31
|
+
id: 1,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
object_space: {
|
|
36
|
+
fields: {
|
|
37
|
+
system: {
|
|
38
|
+
type: "bool",
|
|
39
|
+
id: 1,
|
|
40
|
+
},
|
|
41
|
+
zone: {
|
|
42
|
+
type: "bytes",
|
|
43
|
+
id: 2,
|
|
44
|
+
options: {
|
|
45
|
+
"(koinos.btype)": "ADDRESS",
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
id: {
|
|
49
|
+
type: "uint32",
|
|
50
|
+
id: 3,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
database_object: {
|
|
55
|
+
fields: {
|
|
56
|
+
exists: {
|
|
57
|
+
type: "bool",
|
|
58
|
+
id: 1,
|
|
59
|
+
},
|
|
60
|
+
value: {
|
|
61
|
+
type: "bytes",
|
|
62
|
+
id: 2,
|
|
63
|
+
},
|
|
64
|
+
key: {
|
|
65
|
+
type: "bytes",
|
|
66
|
+
id: 3,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
};
|
|
13
72
|
/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
|
|
14
73
|
async function sleep(ms) {
|
|
15
74
|
return new Promise((r) => setTimeout(r, ms));
|
|
@@ -763,6 +822,194 @@ class Provider {
|
|
|
763
822
|
});
|
|
764
823
|
return this.invokeSystemCall(serializer, "get_contract_address", { name });
|
|
765
824
|
}
|
|
825
|
+
async invokeGetObjectGeneric({ systemCallIdOrName, address, id, key = "", system = false, zone = address, }) {
|
|
826
|
+
const serializer = new Serializer_1.Serializer(getObjectTypes, {
|
|
827
|
+
argumentsTypeName: "get_object_arguments",
|
|
828
|
+
returnTypeName: "get_object_result",
|
|
829
|
+
});
|
|
830
|
+
const result = await this.invokeSystemCall(serializer, systemCallIdOrName, {
|
|
831
|
+
space: {
|
|
832
|
+
system,
|
|
833
|
+
zone,
|
|
834
|
+
id,
|
|
835
|
+
},
|
|
836
|
+
key,
|
|
837
|
+
}, {
|
|
838
|
+
caller: address,
|
|
839
|
+
caller_privilege: system ? 0 : 1,
|
|
840
|
+
});
|
|
841
|
+
return result === null || result === void 0 ? void 0 : result.value;
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Function to get an object from the database via the get_object system call.
|
|
845
|
+
* @param args - object space and key
|
|
846
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
847
|
+
* @param args.id - object space id
|
|
848
|
+
* @param args.key - object key (defaults to empty)
|
|
849
|
+
* @param args.zone - object space zone (defaults to address)
|
|
850
|
+
*
|
|
851
|
+
* @example
|
|
852
|
+
* ```ts
|
|
853
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
854
|
+
* const result = await provider.invokeGetObject({
|
|
855
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
856
|
+
* id: 0,
|
|
857
|
+
* });
|
|
858
|
+
* console.log(result);
|
|
859
|
+
*
|
|
860
|
+
* // {
|
|
861
|
+
* // exists: true,
|
|
862
|
+
* // value: '...',
|
|
863
|
+
* // key: ''
|
|
864
|
+
* // }
|
|
865
|
+
* ```
|
|
866
|
+
*/
|
|
867
|
+
async invokeGetObject({ address, id, key = "", system = false, zone = address, }) {
|
|
868
|
+
const result = await this.invokeGetObjectGeneric({
|
|
869
|
+
systemCallIdOrName: "get_object",
|
|
870
|
+
address,
|
|
871
|
+
id,
|
|
872
|
+
key,
|
|
873
|
+
system,
|
|
874
|
+
zone,
|
|
875
|
+
});
|
|
876
|
+
if (!result || !result.exists)
|
|
877
|
+
return undefined;
|
|
878
|
+
return result.value;
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* Function to get the next object from the database via the
|
|
882
|
+
* get_next_object system call.
|
|
883
|
+
* @param args - object space and key
|
|
884
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
885
|
+
* @param args.id - object space id
|
|
886
|
+
* @param args.key - object key to seek from (defaults to empty)
|
|
887
|
+
* @param args.system - whether the space is a system space
|
|
888
|
+
* @param args.zone - object space zone (defaults to address)
|
|
889
|
+
*
|
|
890
|
+
* @example
|
|
891
|
+
* ```ts
|
|
892
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
893
|
+
* const result = await provider.invokeGetNextObject({
|
|
894
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
895
|
+
* id: 0,
|
|
896
|
+
* });
|
|
897
|
+
* console.log(result);
|
|
898
|
+
*
|
|
899
|
+
* // {
|
|
900
|
+
* // exists: true,
|
|
901
|
+
* // value: '...',
|
|
902
|
+
* // key: '...'
|
|
903
|
+
* // }
|
|
904
|
+
* ```
|
|
905
|
+
*/
|
|
906
|
+
async invokeGetNextObject({ address, id, key = "", system = false, zone = address, }) {
|
|
907
|
+
return this.invokeGetObjectGeneric({
|
|
908
|
+
systemCallIdOrName: "get_next_object",
|
|
909
|
+
address,
|
|
910
|
+
id,
|
|
911
|
+
key,
|
|
912
|
+
system,
|
|
913
|
+
zone,
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Function to get the previous object from the database via the
|
|
918
|
+
* get_prev_object system call.
|
|
919
|
+
* @param args - object space and key
|
|
920
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
921
|
+
* @param args.id - object space id
|
|
922
|
+
* @param args.key - object key to seek from (defaults to empty)
|
|
923
|
+
* @param args.system - whether the space is a system space
|
|
924
|
+
* @param args.zone - object space zone (defaults to address)
|
|
925
|
+
*
|
|
926
|
+
* @example
|
|
927
|
+
* ```ts
|
|
928
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
929
|
+
* const result = await provider.invokeGetPrevObject({
|
|
930
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
931
|
+
* id: 0,
|
|
932
|
+
* key: "...",
|
|
933
|
+
* });
|
|
934
|
+
* console.log(result);
|
|
935
|
+
*
|
|
936
|
+
* // {
|
|
937
|
+
* // exists: true,
|
|
938
|
+
* // value: '...',
|
|
939
|
+
* // key: '...'
|
|
940
|
+
* // }
|
|
941
|
+
* ```
|
|
942
|
+
*/
|
|
943
|
+
async invokeGetPrevObject({ address, id, key = "", system = false, zone = address, }) {
|
|
944
|
+
return this.invokeGetObjectGeneric({
|
|
945
|
+
systemCallIdOrName: "get_prev_object",
|
|
946
|
+
address,
|
|
947
|
+
id,
|
|
948
|
+
key,
|
|
949
|
+
system,
|
|
950
|
+
zone,
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* Function to get many objects from the database by repeatedly
|
|
955
|
+
* calling get_next_object or get_prev_object.
|
|
956
|
+
* @param args - object space and pagination options
|
|
957
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
958
|
+
* @param args.id - object space id
|
|
959
|
+
* @param args.key - offset key to seek from (defaults to empty)
|
|
960
|
+
* @param args.limit - maximum number of objects to return
|
|
961
|
+
* @param args.direction - traversal direction ("ascending" or "descending")
|
|
962
|
+
* @param args.system - whether the space is a system space
|
|
963
|
+
* @param args.zone - object space zone (defaults to address)
|
|
964
|
+
*
|
|
965
|
+
* @example
|
|
966
|
+
* ```ts
|
|
967
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
968
|
+
* const result = await provider.invokeGetManyObjects({
|
|
969
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
970
|
+
* id: 0,
|
|
971
|
+
* limit: 10,
|
|
972
|
+
* });
|
|
973
|
+
* console.log(result);
|
|
974
|
+
*
|
|
975
|
+
* // [
|
|
976
|
+
* // {
|
|
977
|
+
* // exists: true,
|
|
978
|
+
* // value: '...',
|
|
979
|
+
* // key: '...'
|
|
980
|
+
* // },
|
|
981
|
+
* // ...
|
|
982
|
+
* // ]
|
|
983
|
+
* ```
|
|
984
|
+
*/
|
|
985
|
+
async invokeGetManyObjects({ address, id, key = "", limit = Number.MAX_SAFE_INTEGER, direction = "ascending", system = false, zone = address, }) {
|
|
986
|
+
const result = [];
|
|
987
|
+
let currentKey = key;
|
|
988
|
+
let done = false;
|
|
989
|
+
do {
|
|
990
|
+
const obj = direction === "ascending"
|
|
991
|
+
? await this.invokeGetNextObject({
|
|
992
|
+
address,
|
|
993
|
+
id,
|
|
994
|
+
key: currentKey,
|
|
995
|
+
system,
|
|
996
|
+
zone,
|
|
997
|
+
})
|
|
998
|
+
: await this.invokeGetPrevObject({
|
|
999
|
+
address,
|
|
1000
|
+
id,
|
|
1001
|
+
key: currentKey,
|
|
1002
|
+
system,
|
|
1003
|
+
zone,
|
|
1004
|
+
});
|
|
1005
|
+
if (obj && obj.exists) {
|
|
1006
|
+
result.push(obj);
|
|
1007
|
+
currentKey = obj.key;
|
|
1008
|
+
}
|
|
1009
|
+
done = !obj || !obj.exists || result.length >= limit;
|
|
1010
|
+
} while (!done);
|
|
1011
|
+
return result;
|
|
1012
|
+
}
|
|
766
1013
|
}
|
|
767
1014
|
exports.Provider = Provider;
|
|
768
1015
|
exports.default = Provider;
|