carbon-js-sdk 0.4.30 → 0.4.31
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/lib/clients/BatchQueryClient.d.ts +26 -0
- package/lib/clients/BatchQueryClient.js +100 -0
- package/lib/insights/delegation.d.ts +26 -0
- package/lib/insights/delegation.js +2 -0
- package/lib/insights/oracles.d.ts +17 -0
- package/lib/insights/oracles.js +2 -0
- package/lib/modules/cdp.js +12 -11
- package/lib/provider/metamask/legacy-accounts.d.ts +4 -0
- package/lib/provider/metamask/legacy-accounts.js +2174 -10
- package/package.json +1 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
|
|
2
|
+
import { HttpEndpoint } from "@cosmjs/tendermint-rpc/build/rpcclients/httpclient";
|
|
3
|
+
import { RpcClient } from "@cosmjs/tendermint-rpc/build/rpcclients/rpcclient";
|
|
4
|
+
export interface BatchQueryClientOptions {
|
|
5
|
+
/** Interval for dispatching batches (in milliseconds) */
|
|
6
|
+
dispatchInterval: number;
|
|
7
|
+
/** Max number of items sent in one request */
|
|
8
|
+
batchSizeLimit: number;
|
|
9
|
+
}
|
|
10
|
+
declare class BatchQueryClient implements RpcClient {
|
|
11
|
+
protected readonly url: string;
|
|
12
|
+
protected readonly headers: Record<string, string> | undefined;
|
|
13
|
+
protected readonly options: BatchQueryClientOptions;
|
|
14
|
+
private timer?;
|
|
15
|
+
private readonly queue;
|
|
16
|
+
constructor(endpoint: string | HttpEndpoint, options?: Partial<BatchQueryClientOptions>);
|
|
17
|
+
disconnect(): void;
|
|
18
|
+
execute(request: JsonRpcRequest): Promise<JsonRpcSuccessResponse>;
|
|
19
|
+
private validate;
|
|
20
|
+
/**
|
|
21
|
+
* This is called in an interval where promise rejections cannot be handled.
|
|
22
|
+
* So this is not async and HTTP errors need to be handled by the queued promises.
|
|
23
|
+
*/
|
|
24
|
+
private tick;
|
|
25
|
+
}
|
|
26
|
+
export default BatchQueryClient;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const json_rpc_1 = require("@cosmjs/json-rpc");
|
|
13
|
+
const http_1 = require("@cosmjs/tendermint-rpc/build/rpcclients/http");
|
|
14
|
+
const rpcclient_1 = require("@cosmjs/tendermint-rpc/build/rpcclients/rpcclient");
|
|
15
|
+
// Those values are private and can change any time.
|
|
16
|
+
// Does a user need to know them? I don't think so. You either set
|
|
17
|
+
// a custom value or leave the option field unset.
|
|
18
|
+
const defaultBatchQueryClientOptions = {
|
|
19
|
+
dispatchInterval: 20,
|
|
20
|
+
batchSizeLimit: 20,
|
|
21
|
+
};
|
|
22
|
+
class BatchQueryClient {
|
|
23
|
+
constructor(endpoint, options = {}) {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
this.queue = [];
|
|
26
|
+
this.options = {
|
|
27
|
+
batchSizeLimit: (_a = options.batchSizeLimit) !== null && _a !== void 0 ? _a : defaultBatchQueryClientOptions.batchSizeLimit,
|
|
28
|
+
dispatchInterval: (_b = options.dispatchInterval) !== null && _b !== void 0 ? _b : defaultBatchQueryClientOptions.dispatchInterval,
|
|
29
|
+
};
|
|
30
|
+
if (typeof endpoint === "string") {
|
|
31
|
+
// accept host.name:port and assume http protocol
|
|
32
|
+
this.url = rpcclient_1.hasProtocol(endpoint) ? endpoint : "http://" + endpoint;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.url = endpoint.url;
|
|
36
|
+
this.headers = endpoint.headers;
|
|
37
|
+
}
|
|
38
|
+
this.timer = (setInterval(() => this.tick(), options.dispatchInterval));
|
|
39
|
+
this.validate();
|
|
40
|
+
}
|
|
41
|
+
disconnect() {
|
|
42
|
+
this.timer && clearInterval(this.timer);
|
|
43
|
+
this.timer = undefined;
|
|
44
|
+
}
|
|
45
|
+
execute(request) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
this.queue.push({ request, resolve, reject });
|
|
49
|
+
if (this.queue.length >= this.options.batchSizeLimit) {
|
|
50
|
+
// this train is full, let's go
|
|
51
|
+
this.tick();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
validate() {
|
|
57
|
+
if (!this.options.batchSizeLimit ||
|
|
58
|
+
!Number.isSafeInteger(this.options.batchSizeLimit) ||
|
|
59
|
+
this.options.batchSizeLimit < 1) {
|
|
60
|
+
throw new Error("batchSizeLimit must be a safe integer >= 1");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* This is called in an interval where promise rejections cannot be handled.
|
|
65
|
+
* So this is not async and HTTP errors need to be handled by the queued promises.
|
|
66
|
+
*/
|
|
67
|
+
tick() {
|
|
68
|
+
// Avoid race conditions
|
|
69
|
+
const batch = this.queue.splice(0, this.options.batchSizeLimit);
|
|
70
|
+
if (!batch.length)
|
|
71
|
+
return;
|
|
72
|
+
const requests = batch.map((s) => s.request);
|
|
73
|
+
const requestIds = requests.map((request) => request.id);
|
|
74
|
+
http_1.http("POST", this.url, this.headers, requests).then((raw) => {
|
|
75
|
+
// Requests with a single entry return as an object
|
|
76
|
+
const arr = Array.isArray(raw) ? raw : [raw];
|
|
77
|
+
arr.forEach((el, i) => {
|
|
78
|
+
const req = batch[i];
|
|
79
|
+
if (!req)
|
|
80
|
+
return;
|
|
81
|
+
const { reject, resolve } = req;
|
|
82
|
+
const response = json_rpc_1.parseJsonRpcResponse(el);
|
|
83
|
+
if (json_rpc_1.isJsonRpcErrorResponse(response)) {
|
|
84
|
+
reject(new Error(JSON.stringify(response.error)));
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
resolve(response);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}, (error) => {
|
|
91
|
+
for (const requestId of requestIds) {
|
|
92
|
+
const req = batch.find((s) => s.request.id === requestId);
|
|
93
|
+
if (!req)
|
|
94
|
+
return;
|
|
95
|
+
req.reject(error);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.default = BatchQueryClient;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { QueryByPageRequest } from "./common";
|
|
2
|
+
export interface DelegationEntry {
|
|
3
|
+
blockHeight: number;
|
|
4
|
+
timestamp: string;
|
|
5
|
+
type: string;
|
|
6
|
+
denom: string;
|
|
7
|
+
amount: number;
|
|
8
|
+
amountValue: number;
|
|
9
|
+
delegator: string;
|
|
10
|
+
validator: string;
|
|
11
|
+
newValidator: string;
|
|
12
|
+
newShares: number;
|
|
13
|
+
completionTime: string;
|
|
14
|
+
}
|
|
15
|
+
export interface QueryGetDelegationsResponse {
|
|
16
|
+
entries: DelegationEntry[];
|
|
17
|
+
}
|
|
18
|
+
export interface GetDelegationsPathParams {
|
|
19
|
+
delegator: string;
|
|
20
|
+
}
|
|
21
|
+
export interface GetDelegationsQueryParams extends QueryByPageRequest {
|
|
22
|
+
time?: string;
|
|
23
|
+
denom?: string;
|
|
24
|
+
type?: string;
|
|
25
|
+
validator?: string;
|
|
26
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { QueryByTimeRequest, TimeMeta } from "./common";
|
|
2
|
+
export interface OraclesPrice {
|
|
3
|
+
denom: string;
|
|
4
|
+
index: number;
|
|
5
|
+
twap: number;
|
|
6
|
+
}
|
|
7
|
+
export interface QueryGetOraclesPriceRequest extends QueryByTimeRequest {
|
|
8
|
+
intervalDivision?: number;
|
|
9
|
+
denom?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface QueryGetOraclesPriceResponse {
|
|
12
|
+
entries: {
|
|
13
|
+
timestamp: string;
|
|
14
|
+
tokens: OraclesPrice[];
|
|
15
|
+
}[];
|
|
16
|
+
meta: TimeMeta;
|
|
17
|
+
}
|
package/lib/modules/cdp.js
CHANGED
|
@@ -647,20 +647,21 @@ class CDPModule extends base_1.default {
|
|
|
647
647
|
});
|
|
648
648
|
}
|
|
649
649
|
getModuleTotalCollateralUsdVal() {
|
|
650
|
+
var _a, _b;
|
|
650
651
|
return __awaiter(this, void 0, void 0, function* () {
|
|
651
652
|
const network = this.sdkProvider.getConfig().network;
|
|
652
653
|
const collateralPoolAddress = address_1.SWTHAddress.getModuleAddress("collateral_pool", network);
|
|
653
|
-
const
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
return
|
|
654
|
+
const collateralPoolBalances = yield this.sdkProvider.query.bank.AllBalances({ address: collateralPoolAddress });
|
|
655
|
+
const cdpTokenBalances = ((_a = collateralPoolBalances === null || collateralPoolBalances === void 0 ? void 0 : collateralPoolBalances.balances) !== null && _a !== void 0 ? _a : []).filter(balance => TokenClient_1.default.isCdpToken(balance.denom));
|
|
656
|
+
const cdpTokenBalancePromises = cdpTokenBalances.map(balance => (this.getCdpTokenUsdVal(balance.denom, number_1.bnOrZero(balance.amount))
|
|
657
|
+
.then((val) => number_1.bnOrZero(val))
|
|
658
|
+
.catch((err) => {
|
|
659
|
+
console.error(err);
|
|
660
|
+
return number_1.BN_ZERO;
|
|
661
|
+
})));
|
|
662
|
+
const cdpBalances = (_b = (yield Promise.all(cdpTokenBalancePromises))) !== null && _b !== void 0 ? _b : [];
|
|
663
|
+
const totalCollateralsUsdValue = cdpBalances.reduce((prev, curr) => (prev.plus(curr)), number_1.BN_ZERO);
|
|
664
|
+
return totalCollateralsUsdValue;
|
|
664
665
|
});
|
|
665
666
|
}
|
|
666
667
|
getCdpTokenUsdVal(cdpDenom, amount) {
|