@exromany/lido-csm-sdk
Version:
[](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [](h
189 lines • 12.6 kB
JavaScript
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
var useValue = arguments.length > 2;
for (var i = 0; i < initializers.length; i++) {
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
}
return useValue ? value : void 0;
};
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
var _, done = false;
for (var i = decorators.length - 1; i >= 0; i--) {
var context = {};
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
if (kind === "accessor") {
if (result === void 0) continue;
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
if (target) Object.defineProperty(target, contextIn.name, descriptor);
done = true;
};
import { stethSharesAbi } from '@lidofinance/lido-ethereum-sdk';
import { CsmSDKModule } from '../common/class-primitives/csm-sdk-module.js';
import { Cache } from '../common/decorators/cache.js';
import { ErrorHandler } from '../common/decorators/error-handler.js';
import { Logger } from '../common/decorators/logger.js';
import { CACHE_LONG, CACHE_MID, CONTRACT_NAMES, TOKENS, } from '../common/index.js';
import { convertEthToShares, convertSharesToEth } from './convert-shares.js';
let AccountingSDK = (() => {
var _a;
let _classSuper = CsmSDKModule;
let _instanceExtraInitializers = [];
let _get_stethContract_decorators;
let _getStethPoolData_decorators;
let _getStethPoolDataByBlockNumbers_decorators;
let _sharesToEth_decorators;
let _ethToShares_decorators;
let _getBondAmountByKeysCountETH_decorators;
let _getBondAmountByKeysCountWstETH_decorators;
let _getCurve_decorators;
let _getKeysCountByBondAmount_decorators;
let _getBondForNextKeysETH_decorators;
let _getBondForNextKeysWstETH_decorators;
let _getBondForNextKeysPerToken_decorators;
return _a = class AccountingSDK extends _classSuper {
get accountingContract() {
return this.core.contractAccounting;
}
get stethContract() {
return this.core.getContractWithAbi(CONTRACT_NAMES.stETH, stethSharesAbi);
}
async getStethPoolData(blockNumber) {
const effectiveBlockNumber = this.core.skipHistoricalCalls
? undefined
: blockNumber;
const [totalPooledEther, totalShares] = await Promise.all([
this.stethContract.read.getTotalPooledEther({
blockNumber: effectiveBlockNumber,
}),
this.stethContract.read.getTotalShares({
blockNumber: effectiveBlockNumber,
}),
]);
return { totalPooledEther, totalShares };
}
async getStethPoolDataByBlockNumbers(blockNumbers) {
return new Map(await Promise.all(blockNumbers.map(async (blockNumber) => [blockNumber, await this.getStethPoolData(blockNumber)])));
}
/**
* Converts stETH shares to ETH amount using the current pool ratio.
* Also works for converting wstETH to stETH.
*/
async sharesToEth(amount, blockNumber) {
const poolData = await this.getStethPoolData(blockNumber);
return convertSharesToEth(amount, poolData);
}
/**
* Converts ETH amount to stETH shares using the current pool ratio.
* Also works for converting stETH to wstETH.
*/
async ethToShares(amount, blockNumber) {
const poolData = await this.getStethPoolData(blockNumber);
return convertEthToShares(amount, poolData);
}
async getBondAmountByKeysCountETH({ curveId, keysCount, }) {
return this.accountingContract.read.getBondAmountByKeysCount([
keysCount,
curveId,
]);
}
async getBondAmountByKeysCountWstETH({ curveId, keysCount, }) {
return this.accountingContract.read.getBondAmountByKeysCountWstETH([
keysCount,
curveId,
]);
}
async getBondAmountByKeysCountPerToken(props) {
const [eth, wsteth] = await Promise.all([
this.getBondAmountByKeysCountETH(props),
this.getBondAmountByKeysCountWstETH(props),
]);
return {
[TOKENS.eth]: eth,
[TOKENS.steth]: eth,
[TOKENS.wsteth]: wsteth,
};
}
async getCurve(curveId) {
return this.accountingContract.read.getCurveInfo([curveId]);
}
async getKeysCountByBondAmount({ amount, curveId, }) {
return this.accountingContract.read.getKeysCountByBondAmount([
amount,
curveId,
]);
}
async getBondForNextKeysETH({ nodeOperatorId, keysCount, }) {
return this.accountingContract.read.getRequiredBondForNextKeys([
nodeOperatorId,
keysCount,
]);
}
async getBondForNextKeysWstETH({ nodeOperatorId, keysCount, }) {
return this.accountingContract.read.getRequiredBondForNextKeysWstETH([
nodeOperatorId,
keysCount,
]);
}
async getBondForNextKeysPerToken(props) {
const [eth, wsteth] = await Promise.all([
this.getBondForNextKeysETH(props),
this.getBondForNextKeysWstETH(props),
]);
return {
[TOKENS.eth]: eth,
[TOKENS.steth]: eth,
[TOKENS.wsteth]: wsteth,
};
}
constructor() {
super(...arguments);
__runInitializers(this, _instanceExtraInitializers);
}
},
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
_get_stethContract_decorators = [Cache(CACHE_LONG)];
_getStethPoolData_decorators = [Logger('Views:'), Cache(CACHE_MID), ErrorHandler()];
_getStethPoolDataByBlockNumbers_decorators = [Logger('Utils:')];
_sharesToEth_decorators = [Logger('Views:'), ErrorHandler()];
_ethToShares_decorators = [Logger('Views:'), ErrorHandler()];
_getBondAmountByKeysCountETH_decorators = [Logger('Views:'), ErrorHandler()];
_getBondAmountByKeysCountWstETH_decorators = [Logger('Views:'), ErrorHandler()];
_getCurve_decorators = [Logger('Views:'), ErrorHandler()];
_getKeysCountByBondAmount_decorators = [Logger('Views:'), ErrorHandler()];
_getBondForNextKeysETH_decorators = [Logger('Views:'), ErrorHandler()];
_getBondForNextKeysWstETH_decorators = [Logger('Views:'), ErrorHandler()];
_getBondForNextKeysPerToken_decorators = [Logger('Utils:'), ErrorHandler()];
__esDecorate(_a, null, _get_stethContract_decorators, { kind: "getter", name: "stethContract", static: false, private: false, access: { has: obj => "stethContract" in obj, get: obj => obj.stethContract }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _getStethPoolData_decorators, { kind: "method", name: "getStethPoolData", static: false, private: false, access: { has: obj => "getStethPoolData" in obj, get: obj => obj.getStethPoolData }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _getStethPoolDataByBlockNumbers_decorators, { kind: "method", name: "getStethPoolDataByBlockNumbers", static: false, private: false, access: { has: obj => "getStethPoolDataByBlockNumbers" in obj, get: obj => obj.getStethPoolDataByBlockNumbers }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _sharesToEth_decorators, { kind: "method", name: "sharesToEth", static: false, private: false, access: { has: obj => "sharesToEth" in obj, get: obj => obj.sharesToEth }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _ethToShares_decorators, { kind: "method", name: "ethToShares", static: false, private: false, access: { has: obj => "ethToShares" in obj, get: obj => obj.ethToShares }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _getBondAmountByKeysCountETH_decorators, { kind: "method", name: "getBondAmountByKeysCountETH", static: false, private: false, access: { has: obj => "getBondAmountByKeysCountETH" in obj, get: obj => obj.getBondAmountByKeysCountETH }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _getBondAmountByKeysCountWstETH_decorators, { kind: "method", name: "getBondAmountByKeysCountWstETH", static: false, private: false, access: { has: obj => "getBondAmountByKeysCountWstETH" in obj, get: obj => obj.getBondAmountByKeysCountWstETH }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _getCurve_decorators, { kind: "method", name: "getCurve", static: false, private: false, access: { has: obj => "getCurve" in obj, get: obj => obj.getCurve }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _getKeysCountByBondAmount_decorators, { kind: "method", name: "getKeysCountByBondAmount", static: false, private: false, access: { has: obj => "getKeysCountByBondAmount" in obj, get: obj => obj.getKeysCountByBondAmount }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _getBondForNextKeysETH_decorators, { kind: "method", name: "getBondForNextKeysETH", static: false, private: false, access: { has: obj => "getBondForNextKeysETH" in obj, get: obj => obj.getBondForNextKeysETH }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _getBondForNextKeysWstETH_decorators, { kind: "method", name: "getBondForNextKeysWstETH", static: false, private: false, access: { has: obj => "getBondForNextKeysWstETH" in obj, get: obj => obj.getBondForNextKeysWstETH }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _getBondForNextKeysPerToken_decorators, { kind: "method", name: "getBondForNextKeysPerToken", static: false, private: false, access: { has: obj => "getBondForNextKeysPerToken" in obj, get: obj => obj.getBondForNextKeysPerToken }, metadata: _metadata }, null, _instanceExtraInitializers);
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
})(),
_a;
})();
export { AccountingSDK };
//# sourceMappingURL=accounting-sdk.js.map