UNPKG

@lidofinance/lido-ethereum-sdk

Version:

<div style="display: flex;" align="center"> <h1 align="center">Lido Ethereum SDK</h1> </div>

316 lines 20.9 kB
"use strict"; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LidoSDKShares = void 0; const viem_1 = require("viem"); const index_js_1 = require("../common/decorators/index.js"); const constants_js_1 = require("../common/constants.js"); const steth_shares_abi_js_1 = require("./abi/steth-shares-abi.js"); const parse_value_js_1 = require("../common/utils/parse-value.js"); const utils_js_1 = require("../rewards/utils.js"); const sdk_module_js_1 = require("../common/class-primitives/sdk-module.js"); const bigint_math_js_1 = require("../common/utils/bigint-math.js"); const index_js_2 = require("../common/index.js"); const isSharesAmountWithRoundUp = (value) => typeof value === 'object' && value !== null && 'amount' in value; let LidoSDKShares = (() => { var _a; let _classSuper = sdk_module_js_1.LidoSDKModule; let _instanceExtraInitializers = []; let _contractAddressStETH_decorators; let _getContractStETHshares_decorators; let _balance_decorators; let _transfer_decorators; let _populateTransfer_decorators; let _simulateTransfer_decorators; let _convertToShares_decorators; let _convertToSteth_decorators; let _convertBatchSharesToSteth_decorators; let _convertBatchStethToShares_decorators; let _getSharesByPooledEth_decorators; let _getPooledEthByShares_decorators; let _getPooledEthBySharesRoundUp_decorators; let _getTotalSupply_decorators; let _getShareRate_decorators; return _a = class LidoSDKShares extends _classSuper { async contractAddressStETH() { return await this.core.getContractAddress(constants_js_1.LIDO_CONTRACT_NAMES.lido); } async getContractStETHshares() { const address = await this.contractAddressStETH(); return (0, index_js_2.getEncodableContract)((0, viem_1.getContract)({ address, abi: steth_shares_abi_js_1.stethSharesAbi, client: this.core.keyedClient, })); } async balance(address) { const contract = await this.getContractStETHshares(); const account = await this.core.useAccount(address); return contract.read.sharesOf([account.address]); } async transfer({ account: accountProp, to, amount: _amount, callback = constants_js_1.NOOP, from: _from, ...rest }) { this.core.useWalletClient(); const account = await this.core.useAccount(accountProp); const from = _from ?? account.address; const amount = (0, parse_value_js_1.parseValue)(_amount); const isTransferFrom = from !== account.address; const contract = await this.getContractStETHshares(); const getGasLimit = async (overrides) => isTransferFrom ? contract.estimateGas.transferSharesFrom([from, to, amount], overrides) : contract.estimateGas.transferShares([to, amount], overrides); const sendTransaction = async (overrides) => isTransferFrom ? contract.write.transferSharesFrom([from, to, amount], overrides) : contract.write.transferShares([to, amount], overrides); return this.core.performTransaction({ ...rest, account, callback, getGasLimit, sendTransaction, }); } async populateTransfer({ account: accountProp, to, amount: _amount, from: _from, }) { const account = await this.core.useAccount(accountProp); const amount = (0, parse_value_js_1.parseValue)(_amount); const from = _from ?? account.address; const address = await this.contractAddressStETH(); const isTransferFrom = from !== account.address; return { to: address, from: account.address, data: isTransferFrom ? (0, viem_1.encodeFunctionData)({ abi: steth_shares_abi_js_1.stethSharesAbi, functionName: 'transferSharesFrom', args: [from, to, amount], }) : (0, viem_1.encodeFunctionData)({ abi: steth_shares_abi_js_1.stethSharesAbi, functionName: 'transferShares', args: [to, amount], }), }; } async simulateTransfer({ account: _account, to, amount: _amount, from: _from, }) { const amount = (0, parse_value_js_1.parseValue)(_amount); const account = await this.core.useAccount(_account); const from = _from ?? account.address; const contract = await this.getContractStETHshares(); const isTransferFrom = from !== account.address; return isTransferFrom ? contract.simulate.transferSharesFrom([from, to, amount], { account, }) : contract.simulate.transferShares([to, amount], { account, }); } async convertToShares(stethAmount) { const amount = (0, parse_value_js_1.parseValue)(stethAmount); const contract = await this.getContractStETHshares(); const amountAbs = (0, bigint_math_js_1.bigIntAbs)(amount); const sign = (0, bigint_math_js_1.bigIntSign)(amount); const result = await contract.read.getSharesByPooledEth([amountAbs]); return sign * result; } async convertToSteth(sharesAmount) { const amount = (0, parse_value_js_1.parseValue)(sharesAmount); const contract = await this.getContractStETHshares(); const amountAbs = (0, bigint_math_js_1.bigIntAbs)(amount); const sign = (0, bigint_math_js_1.bigIntSign)(amount); const result = await contract.read.getPooledEthByShares([amountAbs]); return sign * result; } async convertBatchSharesToSteth(sharesAmounts, shareRateValues) { if (sharesAmounts.length === 0) { return []; } const parsedShares = sharesAmounts.map((entry) => { if (isSharesAmountWithRoundUp(entry)) { return { amount: (0, parse_value_js_1.parseValue)(entry.amount), roundUp: Boolean(entry.roundUp), }; } return { amount: (0, parse_value_js_1.parseValue)(entry), roundUp: false, }; }); const resolvedShareRateValues = shareRateValues ?? (await this.getTotalSupply()); return Promise.all(parsedShares.map(({ amount, roundUp }) => roundUp ? this.getPooledEthBySharesRoundUp(amount, resolvedShareRateValues) : this.getPooledEthByShares(amount, resolvedShareRateValues))); } async convertBatchStethToShares(stethAmounts, shareRateValues) { if (stethAmounts.length === 0) { return []; } const parsedAmounts = stethAmounts.map((value) => (0, parse_value_js_1.parseValue)(value)); const resolvedShareRateValues = shareRateValues ?? (await this.getTotalSupply()); return Promise.all(parsedAmounts.map((amount) => this.getSharesByPooledEth(amount, resolvedShareRateValues))); } async getSharesByPooledEth(stethAmount, shareRateValues) { if (stethAmount >= viem_1.maxUint128) { throw this.core.error({ code: index_js_2.ERROR_CODE.INVALID_ARGUMENT, message: `ETH_TOO_LARGE`, }); } const { totalEther, totalShares } = shareRateValues ?? (await this.getTotalSupply()); if (totalEther === 0n) { throw this.core.error({ code: index_js_2.ERROR_CODE.INVALID_ARGUMENT, message: `ZERO_TOTAL_POOLED_ETHER`, }); } return (stethAmount * totalShares) / totalEther; } async getPooledEthByShares(sharesAmount, shareRateValues) { if (sharesAmount >= viem_1.maxUint128) { throw this.core.error({ code: index_js_2.ERROR_CODE.INVALID_ARGUMENT, message: `SHARES_TOO_LARGE`, }); } const { totalEther, totalShares } = shareRateValues ?? (await this.getTotalSupply()); if (totalShares === 0n) { throw this.core.error({ code: index_js_2.ERROR_CODE.INVALID_ARGUMENT, message: `ZERO_TOTAL_SHARES`, }); } return (sharesAmount * totalEther) / totalShares; } async getPooledEthBySharesRoundUp(sharesAmount, shareRateValues) { if (sharesAmount >= viem_1.maxUint128) { throw this.core.error({ code: index_js_2.ERROR_CODE.INVALID_ARGUMENT, message: `SHARES_TOO_LARGE`, }); } const { totalEther, totalShares } = shareRateValues ?? (await this.getTotalSupply()); if (totalShares === 0n) { throw this.core.error({ code: index_js_2.ERROR_CODE.INVALID_ARGUMENT, message: `ZERO_TOTAL_SHARES`, }); } return (0, bigint_math_js_1.bigIntCeilDiv)(sharesAmount * totalEther, totalShares); } async getTotalSupply() { const sharesContract = await this.getContractStETHshares(); const contract = { address: sharesContract.address, abi: sharesContract.abi, }; if (this.core.publicClient.multicall) { const [totalShares, totalEther] = await this.core.publicClient.multicall({ allowFailure: false, contracts: [ { ...contract, functionName: 'getTotalShares', }, { ...contract, functionName: 'getTotalPooledEther', }, ], }); return { totalEther, totalShares }; } else { const [totalShares, totalEther] = await Promise.all([ sharesContract.read.getTotalShares(), sharesContract.read.getTotalPooledEther(), ]); return { totalEther, totalShares }; } } async getShareRate() { const { totalEther, totalShares } = await this.getTotalSupply(); return (0, utils_js_1.calcShareRate)(totalEther, totalShares, _a.PRECISION); } constructor() { super(...arguments); __runInitializers(this, _instanceExtraInitializers); } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; _contractAddressStETH_decorators = [(0, index_js_1.Logger)('Contracts:'), (0, index_js_1.Cache)(30 * 60 * 1000, ['core.chain.id'])]; _getContractStETHshares_decorators = [(0, index_js_1.Logger)('Contracts:'), (0, index_js_1.Cache)(30 * 60 * 1000, ['core.chain.id', 'contractAddressStETH'])]; _balance_decorators = [(0, index_js_1.Logger)('Balances:'), (0, index_js_1.ErrorHandler)()]; _transfer_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _populateTransfer_decorators = [(0, index_js_1.Logger)('Utils:'), (0, index_js_1.ErrorHandler)()]; _simulateTransfer_decorators = [(0, index_js_1.Logger)('Utils:'), (0, index_js_1.ErrorHandler)()]; _convertToShares_decorators = [(0, index_js_1.Logger)('Views:'), (0, index_js_1.ErrorHandler)()]; _convertToSteth_decorators = [(0, index_js_1.Logger)('Views:'), (0, index_js_1.ErrorHandler)()]; _convertBatchSharesToSteth_decorators = [(0, index_js_1.Logger)('Views:'), (0, index_js_1.ErrorHandler)()]; _convertBatchStethToShares_decorators = [(0, index_js_1.Logger)('Views:'), (0, index_js_1.ErrorHandler)()]; _getSharesByPooledEth_decorators = [(0, index_js_1.Logger)('Views:'), (0, index_js_1.ErrorHandler)()]; _getPooledEthByShares_decorators = [(0, index_js_1.Logger)('Views:'), (0, index_js_1.ErrorHandler)()]; _getPooledEthBySharesRoundUp_decorators = [(0, index_js_1.Logger)('Views:'), (0, index_js_1.ErrorHandler)()]; _getTotalSupply_decorators = [(0, index_js_1.Logger)('Views:'), (0, index_js_1.ErrorHandler)()]; _getShareRate_decorators = [(0, index_js_1.Logger)('Views:'), (0, index_js_1.ErrorHandler)()]; __esDecorate(_a, null, _contractAddressStETH_decorators, { kind: "method", name: "contractAddressStETH", static: false, private: false, access: { has: obj => "contractAddressStETH" in obj, get: obj => obj.contractAddressStETH }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getContractStETHshares_decorators, { kind: "method", name: "getContractStETHshares", static: false, private: false, access: { has: obj => "getContractStETHshares" in obj, get: obj => obj.getContractStETHshares }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _balance_decorators, { kind: "method", name: "balance", static: false, private: false, access: { has: obj => "balance" in obj, get: obj => obj.balance }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _transfer_decorators, { kind: "method", name: "transfer", static: false, private: false, access: { has: obj => "transfer" in obj, get: obj => obj.transfer }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _populateTransfer_decorators, { kind: "method", name: "populateTransfer", static: false, private: false, access: { has: obj => "populateTransfer" in obj, get: obj => obj.populateTransfer }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _simulateTransfer_decorators, { kind: "method", name: "simulateTransfer", static: false, private: false, access: { has: obj => "simulateTransfer" in obj, get: obj => obj.simulateTransfer }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _convertToShares_decorators, { kind: "method", name: "convertToShares", static: false, private: false, access: { has: obj => "convertToShares" in obj, get: obj => obj.convertToShares }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _convertToSteth_decorators, { kind: "method", name: "convertToSteth", static: false, private: false, access: { has: obj => "convertToSteth" in obj, get: obj => obj.convertToSteth }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _convertBatchSharesToSteth_decorators, { kind: "method", name: "convertBatchSharesToSteth", static: false, private: false, access: { has: obj => "convertBatchSharesToSteth" in obj, get: obj => obj.convertBatchSharesToSteth }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _convertBatchStethToShares_decorators, { kind: "method", name: "convertBatchStethToShares", static: false, private: false, access: { has: obj => "convertBatchStethToShares" in obj, get: obj => obj.convertBatchStethToShares }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getSharesByPooledEth_decorators, { kind: "method", name: "getSharesByPooledEth", static: false, private: false, access: { has: obj => "getSharesByPooledEth" in obj, get: obj => obj.getSharesByPooledEth }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getPooledEthByShares_decorators, { kind: "method", name: "getPooledEthByShares", static: false, private: false, access: { has: obj => "getPooledEthByShares" in obj, get: obj => obj.getPooledEthByShares }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getPooledEthBySharesRoundUp_decorators, { kind: "method", name: "getPooledEthBySharesRoundUp", static: false, private: false, access: { has: obj => "getPooledEthBySharesRoundUp" in obj, get: obj => obj.getPooledEthBySharesRoundUp }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getTotalSupply_decorators, { kind: "method", name: "getTotalSupply", static: false, private: false, access: { has: obj => "getTotalSupply" in obj, get: obj => obj.getTotalSupply }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getShareRate_decorators, { kind: "method", name: "getShareRate", static: false, private: false, access: { has: obj => "getShareRate" in obj, get: obj => obj.getShareRate }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), Object.defineProperty(_a, "PRECISION", { enumerable: true, configurable: true, writable: true, value: 10n ** 27n }), _a; })(); exports.LidoSDKShares = LidoSDKShares; //# sourceMappingURL=shares.js.map