@defillama/sdk 5.0.245 → 5.0.246
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/build/abi/multicall3.js +2 -2
- package/build/chains/icon.d.ts +84 -0
- package/build/chains/icon.js +155 -0
- package/build/chains/index.d.ts +3 -0
- package/build/chains/index.js +4 -1
- package/build/chains/ontology.d.ts +88 -0
- package/build/chains/ontology.js +93 -0
- package/build/chains/tezos.js +2 -1
- package/build/chains/zilliqa.d.ts +149 -0
- package/build/chains/zilliqa.js +326 -0
- package/build/providers.json +1 -1
- package/build/util/LlamaProvider.js +10 -10
- package/build/util/cache.d.ts +2 -0
- package/build/util/cache.js +2 -2
- package/build/util/chainUtils/data.json +9 -1
- package/build/util/elastic.d.ts +18 -0
- package/build/util/elastic.js +24 -0
- package/build/util/env.js +1 -1
- package/build/util/index.d.ts +1 -1
- package/build/util/indexer.d.ts +2 -1
- package/build/util/indexer.js +127 -54
- package/build/util/logs.d.ts +3 -2
- package/build/util/logs.js +22 -2
- package/build/util/r2.d.ts +7 -4
- package/build/util/r2.js +75 -16
- package/package.json +1 -1
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BECH32_HRP = exports.QA_PER_ZIL = exports.DEFAULT_ENDPOINTS = exports.CHAIN = void 0;
|
|
4
|
+
exports.getEndpoints = getEndpoints;
|
|
5
|
+
exports.bech32Decode = bech32Decode;
|
|
6
|
+
exports.bech32Encode = bech32Encode;
|
|
7
|
+
exports.isBech32Address = isBech32Address;
|
|
8
|
+
exports.isHexAddress = isHexAddress;
|
|
9
|
+
exports.toHexAddress = toHexAddress;
|
|
10
|
+
exports.toBech32Address = toBech32Address;
|
|
11
|
+
exports.qaToZil = qaToZil;
|
|
12
|
+
exports.rpc = rpc;
|
|
13
|
+
exports.rpcBatch = rpcBatch;
|
|
14
|
+
exports.getAccount = getAccount;
|
|
15
|
+
exports.getBalance = getBalance;
|
|
16
|
+
exports.getContractInit = getContractInit;
|
|
17
|
+
exports.initToObject = initToObject;
|
|
18
|
+
exports.getContractState = getContractState;
|
|
19
|
+
exports.getContractSubState = getContractSubState;
|
|
20
|
+
exports.getContractSubStates = getContractSubStates;
|
|
21
|
+
exports.getTokenBalance = getTokenBalance;
|
|
22
|
+
exports.getTokenBalances = getTokenBalances;
|
|
23
|
+
exports.getTokenTotalSupply = getTokenTotalSupply;
|
|
24
|
+
exports.getTokenDecimals = getTokenDecimals;
|
|
25
|
+
exports.getTokenInfo = getTokenInfo;
|
|
26
|
+
exports.getLatestBlock = getLatestBlock;
|
|
27
|
+
exports.getBlock = getBlock;
|
|
28
|
+
/**
|
|
29
|
+
* Zilliqa native (Scilla) JSON-RPC client.
|
|
30
|
+
*
|
|
31
|
+
* Replaces the chain access parts of:
|
|
32
|
+
* - DefiLlama-Adapters projects/helper/chain/zilliqa.js (call, formQuery, getContractState,
|
|
33
|
+
* getBalance, getZilliqaBalance, getBalances)
|
|
34
|
+
* - peggedassets-server src/adapters/peggedAssets/helper/zilliqa.ts (getZilliqaTokenSupply)
|
|
35
|
+
*
|
|
36
|
+
* Endpoints: `ZILLIQA_NATIVE_RPC` (comma separated) overrides the default. The chain's
|
|
37
|
+
* `ZILLIQA_RPC` key is its EVM endpoint in providers.json, so it is deliberately not used here,
|
|
38
|
+
* the same way cosmos uses `<CHAIN>_LCD`. api.zilliqa.com serves both.
|
|
39
|
+
*
|
|
40
|
+
* Addresses: the API wants bare 40-char hex. Every function accepts `0x`-prefixed hex, bare hex
|
|
41
|
+
* or bech32 (`zil1...`) and normalises with `toHexAddress`. Balances come back as raw base units
|
|
42
|
+
* (Qa for ZIL, 1e12 per ZIL; token base units for ZRC-2) as decimal strings.
|
|
43
|
+
*
|
|
44
|
+
* Usage: `sdk.chains.zilliqa.getTokenBalance({ token: '0x818c...', address: 'zil1...' })`
|
|
45
|
+
*/
|
|
46
|
+
const rpc_1 = require("./rpc");
|
|
47
|
+
exports.CHAIN = 'zilliqa';
|
|
48
|
+
exports.DEFAULT_ENDPOINTS = ['https://api.zilliqa.com'];
|
|
49
|
+
exports.QA_PER_ZIL = 1000000000000;
|
|
50
|
+
exports.BECH32_HRP = 'zil';
|
|
51
|
+
/** Configured endpoints (env `ZILLIQA_NATIVE_RPC` first, then `DEFAULT_ENDPOINTS`). */
|
|
52
|
+
function getEndpoints() {
|
|
53
|
+
return (0, rpc_1.getEndpoints)(exports.CHAIN, exports.DEFAULT_ENDPOINTS, { envKey: 'ZILLIQA_NATIVE_RPC' });
|
|
54
|
+
}
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// bech32 (BIP-173) - zilliqa uses hrp `zil` with a 20 byte payload
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
const BECH32_CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
|
|
59
|
+
const BECH32_GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
|
|
60
|
+
function bech32Polymod(values) {
|
|
61
|
+
let chk = 1;
|
|
62
|
+
for (const v of values) {
|
|
63
|
+
const top = chk >>> 25;
|
|
64
|
+
chk = ((chk & 0x1ffffff) << 5) ^ v;
|
|
65
|
+
for (let i = 0; i < 5; i++)
|
|
66
|
+
if ((top >>> i) & 1)
|
|
67
|
+
chk ^= BECH32_GENERATOR[i];
|
|
68
|
+
}
|
|
69
|
+
return chk >>> 0;
|
|
70
|
+
}
|
|
71
|
+
function bech32HrpExpand(hrp) {
|
|
72
|
+
const res = [];
|
|
73
|
+
for (const c of hrp)
|
|
74
|
+
res.push(c.charCodeAt(0) >>> 5);
|
|
75
|
+
res.push(0);
|
|
76
|
+
for (const c of hrp)
|
|
77
|
+
res.push(c.charCodeAt(0) & 31);
|
|
78
|
+
return res;
|
|
79
|
+
}
|
|
80
|
+
function convertBits(data, from, to, pad) {
|
|
81
|
+
let acc = 0;
|
|
82
|
+
let bits = 0;
|
|
83
|
+
const res = [];
|
|
84
|
+
const maxv = (1 << to) - 1;
|
|
85
|
+
for (const value of data) {
|
|
86
|
+
if (value < 0 || value >>> from !== 0)
|
|
87
|
+
throw new Error('bech32: invalid value');
|
|
88
|
+
acc = (acc << from) | value;
|
|
89
|
+
bits += from;
|
|
90
|
+
while (bits >= to) {
|
|
91
|
+
bits -= to;
|
|
92
|
+
res.push((acc >>> bits) & maxv);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (pad) {
|
|
96
|
+
if (bits > 0)
|
|
97
|
+
res.push((acc << (to - bits)) & maxv);
|
|
98
|
+
}
|
|
99
|
+
else if (bits >= from || ((acc << (to - bits)) & maxv)) {
|
|
100
|
+
throw new Error('bech32: invalid padding');
|
|
101
|
+
}
|
|
102
|
+
return res;
|
|
103
|
+
}
|
|
104
|
+
/** Decode a bech32 string into `{ hrp, data }` where `data` is the 8-bit payload. */
|
|
105
|
+
function bech32Decode(str) {
|
|
106
|
+
if (str !== str.toLowerCase() && str !== str.toUpperCase())
|
|
107
|
+
throw new Error('bech32: mixed case');
|
|
108
|
+
str = str.toLowerCase();
|
|
109
|
+
const pos = str.lastIndexOf('1');
|
|
110
|
+
if (pos < 1 || pos + 7 > str.length)
|
|
111
|
+
throw new Error('bech32: invalid separator position');
|
|
112
|
+
const hrp = str.slice(0, pos);
|
|
113
|
+
const values = [];
|
|
114
|
+
for (const c of str.slice(pos + 1)) {
|
|
115
|
+
const v = BECH32_CHARSET.indexOf(c);
|
|
116
|
+
if (v === -1)
|
|
117
|
+
throw new Error(`bech32: invalid character ${c}`);
|
|
118
|
+
values.push(v);
|
|
119
|
+
}
|
|
120
|
+
if (bech32Polymod([...bech32HrpExpand(hrp), ...values]) !== 1)
|
|
121
|
+
throw new Error('bech32: bad checksum');
|
|
122
|
+
return { hrp, data: Buffer.from(convertBits(values.slice(0, -6), 5, 8, false)) };
|
|
123
|
+
}
|
|
124
|
+
/** Encode an 8-bit payload as bech32 with `hrp`. */
|
|
125
|
+
function bech32Encode(hrp, data) {
|
|
126
|
+
const values = convertBits([...data], 8, 5, true);
|
|
127
|
+
const polymod = bech32Polymod([...bech32HrpExpand(hrp), ...values, 0, 0, 0, 0, 0, 0]) ^ 1;
|
|
128
|
+
const checksum = [];
|
|
129
|
+
for (let i = 0; i < 6; i++)
|
|
130
|
+
checksum.push((polymod >>> (5 * (5 - i))) & 31);
|
|
131
|
+
return hrp + '1' + [...values, ...checksum].map(v => BECH32_CHARSET[v]).join('');
|
|
132
|
+
}
|
|
133
|
+
// ---------------------------------------------------------------------------
|
|
134
|
+
// addresses
|
|
135
|
+
// ---------------------------------------------------------------------------
|
|
136
|
+
const HEX_ADDRESS_RE = /^(0x)?[0-9a-f]{40}$/i;
|
|
137
|
+
function isBech32Address(str) {
|
|
138
|
+
if (typeof str !== 'string' || !str.toLowerCase().startsWith(exports.BECH32_HRP + '1') || str.length !== 42)
|
|
139
|
+
return false;
|
|
140
|
+
try {
|
|
141
|
+
return bech32Decode(str).data.length === 20;
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function isHexAddress(str) {
|
|
148
|
+
return typeof str === 'string' && HEX_ADDRESS_RE.test(str);
|
|
149
|
+
}
|
|
150
|
+
/** bech32 (`zil1...`), `0x` hex or bare hex -> bare lowercase hex (what the API expects). */
|
|
151
|
+
function toHexAddress(address) {
|
|
152
|
+
if (typeof address !== 'string')
|
|
153
|
+
throw new Error(`zilliqa: invalid address ${address}`);
|
|
154
|
+
const str = address.trim();
|
|
155
|
+
if (str.toLowerCase().startsWith(exports.BECH32_HRP + '1')) {
|
|
156
|
+
const { hrp, data } = bech32Decode(str);
|
|
157
|
+
if (hrp !== exports.BECH32_HRP || data.length !== 20)
|
|
158
|
+
throw new Error(`zilliqa: invalid bech32 address ${address}`);
|
|
159
|
+
return data.toString('hex');
|
|
160
|
+
}
|
|
161
|
+
if (!HEX_ADDRESS_RE.test(str))
|
|
162
|
+
throw new Error(`zilliqa: invalid address ${address}`);
|
|
163
|
+
return (str.startsWith('0x') || str.startsWith('0X') ? str.slice(2) : str).toLowerCase();
|
|
164
|
+
}
|
|
165
|
+
/** Any accepted address form -> `zil1...` bech32. */
|
|
166
|
+
function toBech32Address(address) {
|
|
167
|
+
return bech32Encode(exports.BECH32_HRP, Buffer.from(toHexAddress(address), 'hex'));
|
|
168
|
+
}
|
|
169
|
+
/** Qa (1e-12 ZIL) -> ZIL as a JS number. */
|
|
170
|
+
function qaToZil(qa) {
|
|
171
|
+
return Number(BigInt(qa)) / exports.QA_PER_ZIL;
|
|
172
|
+
}
|
|
173
|
+
/** Raw JSON-RPC call against the configured endpoints (rotated + retried). */
|
|
174
|
+
async function rpc(method, params = [], options = {}) {
|
|
175
|
+
var _a;
|
|
176
|
+
return (0, rpc_1.jsonRpc)(method, params, { ...options, chain: exports.CHAIN, endpoints: getEndpoints(), id: (_a = options.id) !== null && _a !== void 0 ? _a : '1' });
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* JSON-RPC batch, results in call order. Failed entries resolve to `undefined`
|
|
180
|
+
* (the API answers errors per item, e.g. an unknown substate).
|
|
181
|
+
*/
|
|
182
|
+
async function rpcBatch(calls, options = {}) {
|
|
183
|
+
return (0, rpc_1.jsonRpcBatch)(calls.map((c, i) => { var _a; return ({ ...c, id: (_a = c.id) !== null && _a !== void 0 ? _a : String(i) }); }), { ...options, chain: exports.CHAIN, endpoints: getEndpoints(), permitFailure: true });
|
|
184
|
+
}
|
|
185
|
+
function isAccountNotCreated(e) {
|
|
186
|
+
return e instanceof rpc_1.JsonRpcError && /not created/i.test(e.message);
|
|
187
|
+
}
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
// accounts
|
|
190
|
+
// ---------------------------------------------------------------------------
|
|
191
|
+
/** `GetBalance`: `{ balance (Qa string), nonce }`; `{ balance: '0', nonce: 0 }` for an unused address. */
|
|
192
|
+
async function getAccount({ address }) {
|
|
193
|
+
var _a, _b;
|
|
194
|
+
try {
|
|
195
|
+
const res = await rpc('GetBalance', [toHexAddress(address)]);
|
|
196
|
+
return { balance: String((_a = res === null || res === void 0 ? void 0 : res.balance) !== null && _a !== void 0 ? _a : '0'), nonce: Number((_b = res === null || res === void 0 ? void 0 : res.nonce) !== null && _b !== void 0 ? _b : 0) };
|
|
197
|
+
}
|
|
198
|
+
catch (e) {
|
|
199
|
+
if (isAccountNotCreated(e))
|
|
200
|
+
return { balance: '0', nonce: 0 };
|
|
201
|
+
throw e;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Native ZIL balance in Qa as a decimal string. */
|
|
205
|
+
async function getBalance({ address }) {
|
|
206
|
+
return (await getAccount({ address })).balance;
|
|
207
|
+
}
|
|
208
|
+
// ---------------------------------------------------------------------------
|
|
209
|
+
// contracts
|
|
210
|
+
// ---------------------------------------------------------------------------
|
|
211
|
+
/** `GetSmartContractInit`: the immutable init params as `[{ vname, type, value }]`. */
|
|
212
|
+
async function getContractInit({ contract }) {
|
|
213
|
+
return rpc('GetSmartContractInit', [toHexAddress(contract)]);
|
|
214
|
+
}
|
|
215
|
+
/** `[{ vname, value }]` -> `{ vname: value }`. */
|
|
216
|
+
function initToObject(init) {
|
|
217
|
+
const res = {};
|
|
218
|
+
for (const { vname, value } of init !== null && init !== void 0 ? init : [])
|
|
219
|
+
res[vname] = value;
|
|
220
|
+
return res;
|
|
221
|
+
}
|
|
222
|
+
/** `GetSmartContractState`: the whole mutable state. Large for tokens; prefer `getContractSubState`. */
|
|
223
|
+
async function getContractState({ contract }) {
|
|
224
|
+
return rpc('GetSmartContractState', [toHexAddress(contract)]);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* `GetSmartContractSubState`: one state variable, optionally narrowed by map `indices`.
|
|
228
|
+
* Resolves to `{ [variable]: value }` or `null` when the variable / index does not exist.
|
|
229
|
+
*/
|
|
230
|
+
async function getContractSubState({ contract, variable, indices = [] }) {
|
|
231
|
+
return rpc('GetSmartContractSubState', [toHexAddress(contract), variable, indices]);
|
|
232
|
+
}
|
|
233
|
+
/** Several `GetSmartContractSubState` in one batch; missing entries are `undefined`. */
|
|
234
|
+
async function getContractSubStates(items, options) {
|
|
235
|
+
return rpcBatch(items.map(i => { var _a; return ({ method: 'GetSmartContractSubState', params: [toHexAddress(i.contract), i.variable, (_a = i.indices) !== null && _a !== void 0 ? _a : []] }); }), options);
|
|
236
|
+
}
|
|
237
|
+
/** Raw ZRC-2 balance of `address` for `token`; `'0'` when the holder is unknown. */
|
|
238
|
+
async function getTokenBalance({ token, address }) {
|
|
239
|
+
var _a, _b;
|
|
240
|
+
const holder = '0x' + toHexAddress(address);
|
|
241
|
+
const res = await getContractSubState({ contract: token, variable: 'balances', indices: [holder] });
|
|
242
|
+
return String((_b = (_a = res === null || res === void 0 ? void 0 : res.balances) === null || _a === void 0 ? void 0 : _a[holder]) !== null && _b !== void 0 ? _b : '0');
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Raw ZRC-2 balances for every `token` x `address` pair, one batch request.
|
|
246
|
+
* Returns `{ [tokenHex]: { [addressHex]: balance } }` with `0x`-prefixed lowercase keys.
|
|
247
|
+
*/
|
|
248
|
+
async function getTokenBalances({ tokens, addresses }) {
|
|
249
|
+
const pairs = [];
|
|
250
|
+
for (const token of tokens)
|
|
251
|
+
for (const address of addresses)
|
|
252
|
+
pairs.push({ token: '0x' + toHexAddress(token), holder: '0x' + toHexAddress(address) });
|
|
253
|
+
const results = await getContractSubStates(pairs.map(p => ({ contract: p.token, variable: 'balances', indices: [p.holder] })));
|
|
254
|
+
const res = {};
|
|
255
|
+
pairs.forEach(({ token, holder }, i) => {
|
|
256
|
+
var _a, _b, _c, _d;
|
|
257
|
+
res[token] = (_a = res[token]) !== null && _a !== void 0 ? _a : {};
|
|
258
|
+
res[token][holder] = String((_d = (_c = (_b = results[i]) === null || _b === void 0 ? void 0 : _b.balances) === null || _c === void 0 ? void 0 : _c[holder]) !== null && _d !== void 0 ? _d : '0');
|
|
259
|
+
});
|
|
260
|
+
return res;
|
|
261
|
+
}
|
|
262
|
+
function findSupplyInState(state) {
|
|
263
|
+
if (!state || typeof state !== 'object')
|
|
264
|
+
return;
|
|
265
|
+
if (state.total_supply !== undefined)
|
|
266
|
+
return String(state.total_supply);
|
|
267
|
+
const key = Object.keys(state).find(k => k.toLowerCase().includes('supply'));
|
|
268
|
+
if (!key)
|
|
269
|
+
return;
|
|
270
|
+
const raw = state[key];
|
|
271
|
+
if (raw === null || raw === undefined)
|
|
272
|
+
return;
|
|
273
|
+
if (typeof raw === 'object')
|
|
274
|
+
return raw._balance !== undefined ? String(raw._balance) : undefined;
|
|
275
|
+
return String(raw);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Raw ZRC-2 total supply. Reads the `total_supply` substate; contracts that name the
|
|
279
|
+
* field differently fall back to a full state scan for a `*supply*` key.
|
|
280
|
+
*/
|
|
281
|
+
async function getTokenTotalSupply({ token }) {
|
|
282
|
+
const sub = await getContractSubState({ contract: token, variable: 'total_supply' });
|
|
283
|
+
if ((sub === null || sub === void 0 ? void 0 : sub.total_supply) !== undefined)
|
|
284
|
+
return String(sub.total_supply);
|
|
285
|
+
const supply = findSupplyInState(await getContractState({ contract: token }));
|
|
286
|
+
if (supply === undefined)
|
|
287
|
+
throw new Error(`zilliqa: no total supply field on ${token}`);
|
|
288
|
+
return supply;
|
|
289
|
+
}
|
|
290
|
+
/** `decimals` from the init params (ZRC-2); undefined when the contract has none. */
|
|
291
|
+
async function getTokenDecimals({ token }) {
|
|
292
|
+
const init = initToObject(await getContractInit({ contract: token }));
|
|
293
|
+
if (init.decimals === undefined || init.decimals === null || init.decimals === '')
|
|
294
|
+
return;
|
|
295
|
+
const n = Number(init.decimals);
|
|
296
|
+
return Number.isFinite(n) ? n : undefined;
|
|
297
|
+
}
|
|
298
|
+
/** symbol / name / decimals (init params) + raw total supply. */
|
|
299
|
+
async function getTokenInfo({ token }) {
|
|
300
|
+
const [init, totalSupply] = await Promise.all([getContractInit({ contract: token }), getTokenTotalSupply({ token })]);
|
|
301
|
+
const params = initToObject(init);
|
|
302
|
+
const decimals = params.decimals === undefined ? undefined : Number(params.decimals);
|
|
303
|
+
return {
|
|
304
|
+
contract: '0x' + toHexAddress(token),
|
|
305
|
+
symbol: params.symbol,
|
|
306
|
+
name: params.name,
|
|
307
|
+
decimals: Number.isFinite(decimals) ? decimals : undefined,
|
|
308
|
+
totalSupply,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function toBlockInfo(block) {
|
|
312
|
+
var _a, _b, _c;
|
|
313
|
+
return {
|
|
314
|
+
number: Number((_a = block === null || block === void 0 ? void 0 : block.header) === null || _a === void 0 ? void 0 : _a.BlockNum),
|
|
315
|
+
timestamp: Math.floor(Number((_b = block === null || block === void 0 ? void 0 : block.header) === null || _b === void 0 ? void 0 : _b.Timestamp) / 1e6),
|
|
316
|
+
hash: (_c = block === null || block === void 0 ? void 0 : block.body) === null || _c === void 0 ? void 0 : _c.BlockHash,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
/** `GetLatestTxBlock` */
|
|
320
|
+
async function getLatestBlock() {
|
|
321
|
+
return toBlockInfo(await rpc('GetLatestTxBlock', ['']));
|
|
322
|
+
}
|
|
323
|
+
/** `GetTxBlock` by number. */
|
|
324
|
+
async function getBlock({ number }) {
|
|
325
|
+
return toBlockInfo(await rpc('GetTxBlock', [String(number)]));
|
|
326
|
+
}
|