UNPKG

@lidofinance/lido-ethereum-sdk

Version:

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

245 lines 15 kB
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; }; /* eslint-disable sonarjs/no-identical-functions */ import { getContract, encodeFunctionData } from 'viem'; import { CHAINS, LIDO_L2_CONTRACT_NAMES, NOOP } from '../common/constants.js'; import { parseValue } from '../common/utils/parse-value.js'; import { Cache, ErrorHandler, Logger } from '../common/decorators/index.js'; import { AbstractLidoSDKErc20 } from '../erc20/erc20.js'; import { rebasableL2StethAbi } from './abi/rebasableL2Steth.js'; import { bridgedWstethAbi } from './abi/brigedWsteth.js'; import { getEncodableContract } from '../common/index.js'; let LidoSDKL2Wsteth = (() => { var _a; let _classSuper = AbstractLidoSDKErc20; let _instanceExtraInitializers = []; let _contractAddress_decorators; let _getL2Contract_decorators; let _erc721Domain_decorators; return _a = class LidoSDKL2Wsteth extends _classSuper { async contractAddress() { return this.core.getL2ContractAddress(LIDO_L2_CONTRACT_NAMES.wsteth); } async getL2Contract() { const address = await this.contractAddress(); return getEncodableContract(getContract({ address, abi: bridgedWstethAbi, client: this.core.keyedClient, })); } async erc721Domain() { const contract = await this.getL2Contract(); const [fields, name, version, chainId, verifyingContract, salt, extensions,] = await contract.read.eip712Domain(); // Testnet misconfiguration const fixedVersion = this.core.chainId === CHAINS.OptimismSepolia ? '1' : version; return { fields, name, version: fixedVersion, chainId, verifyingContract, salt, extensions, }; } constructor() { super(...arguments); __runInitializers(this, _instanceExtraInitializers); } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; _contractAddress_decorators = [Logger('Contracts:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getL2Contract_decorators = [Logger('Contracts:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _erc721Domain_decorators = [Cache(30 * 60 * 1000, ['core.chain.id'])]; __esDecorate(_a, null, _contractAddress_decorators, { kind: "method", name: "contractAddress", static: false, private: false, access: { has: obj => "contractAddress" in obj, get: obj => obj.contractAddress }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getL2Contract_decorators, { kind: "method", name: "getL2Contract", static: false, private: false, access: { has: obj => "getL2Contract" in obj, get: obj => obj.getL2Contract }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _erc721Domain_decorators, { kind: "method", name: "erc721Domain", static: false, private: false, access: { has: obj => "erc721Domain" in obj, get: obj => obj.erc721Domain }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), _a; })(); export { LidoSDKL2Wsteth }; let LidoSDKL2Steth = (() => { var _a; let _classSuper = AbstractLidoSDKErc20; let _instanceExtraInitializers = []; let _getL2Contract_decorators; let _erc721Domain_decorators; let _balanceShares_decorators; let _convertToShares_decorators; let _convertToSteth_decorators; let _totalShares_decorators; let _transferShares_decorators; let _populateTransferShares_decorators; let _simulateTransferShares_decorators; return _a = class LidoSDKL2Steth extends _classSuper { async contractAddress() { return this.core.getL2ContractAddress(LIDO_L2_CONTRACT_NAMES.steth); } async getL2Contract() { const address = await this.contractAddress(); return getEncodableContract(getContract({ address, abi: rebasableL2StethAbi, client: this.core.keyedClient, })); } async erc721Domain() { const contract = await this.getL2Contract(); const [fields, name, version, chainId, verifyingContract, salt, extensions,] = await contract.read.eip712Domain(); return { fields, name, version, chainId, verifyingContract, salt, extensions, }; } async balanceShares(address) { const contract = await this.getL2Contract(); const account = await this.core.useAccount(address); return contract.read.sharesOf([account.address]); } // convert async convertToShares(stethAmount) { const amount = parseValue(stethAmount); const contract = await this.getL2Contract(); return contract.read.getSharesByTokens([amount]); } // convert async convertToSteth(sharesAmount) { const amount = parseValue(sharesAmount); const contract = await this.getL2Contract(); return contract.read.getTokensByShares([amount]); } // convert async totalShares() { const contract = await this.getL2Contract(); return contract.read.getTotalShares(); } // TransferShares // Transfer async transferShares({ account: accountProp, to, amount: _amount, callback = NOOP, from: _from, ...rest }) { this.core.useWalletClient(); const account = await this.core.useAccount(accountProp); const from = _from ?? account.address; const amount = parseValue(_amount); const isTransferFrom = from !== account.address; const contract = await this.getL2Contract(); 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 populateTransferShares({ account: accountProp, to, amount: _amount, from: _from, }) { const account = await this.core.useAccount(accountProp); const amount = parseValue(_amount); const from = _from ?? account.address; const address = await this.contractAddress(); const isTransferFrom = from !== account.address; return { to: address, from: account.address, data: isTransferFrom ? encodeFunctionData({ abi: rebasableL2StethAbi, functionName: 'transferSharesFrom', args: [from, to, amount], }) : encodeFunctionData({ abi: rebasableL2StethAbi, functionName: 'transferShares', args: [to, amount], }), }; } async simulateTransferShares({ account: _account, to, amount: _amount, from: _from, }) { const amount = parseValue(_amount); const account = await this.core.useAccount(_account); const from = _from ?? account.address; const contract = await this.getL2Contract(); const isTransferFrom = from !== account.address; return isTransferFrom ? contract.simulate.transferSharesFrom([from, to, amount], { account, }) : contract.simulate.transferShares([to, amount], { account, }); } constructor() { super(...arguments); __runInitializers(this, _instanceExtraInitializers); } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; _getL2Contract_decorators = [Logger('Contracts:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _erc721Domain_decorators = [Cache(30 * 60 * 1000, ['core.chain.id'])]; _balanceShares_decorators = [Logger('Balances:'), ErrorHandler()]; _convertToShares_decorators = [Logger('Views:'), ErrorHandler()]; _convertToSteth_decorators = [Logger('Views:'), ErrorHandler()]; _totalShares_decorators = [Logger('Views:'), ErrorHandler()]; _transferShares_decorators = [Logger('Call:'), ErrorHandler()]; _populateTransferShares_decorators = [Logger('Utils:'), ErrorHandler()]; _simulateTransferShares_decorators = [Logger('Utils:'), ErrorHandler()]; __esDecorate(_a, null, _getL2Contract_decorators, { kind: "method", name: "getL2Contract", static: false, private: false, access: { has: obj => "getL2Contract" in obj, get: obj => obj.getL2Contract }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _erc721Domain_decorators, { kind: "method", name: "erc721Domain", static: false, private: false, access: { has: obj => "erc721Domain" in obj, get: obj => obj.erc721Domain }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _balanceShares_decorators, { kind: "method", name: "balanceShares", static: false, private: false, access: { has: obj => "balanceShares" in obj, get: obj => obj.balanceShares }, 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, _totalShares_decorators, { kind: "method", name: "totalShares", static: false, private: false, access: { has: obj => "totalShares" in obj, get: obj => obj.totalShares }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _transferShares_decorators, { kind: "method", name: "transferShares", static: false, private: false, access: { has: obj => "transferShares" in obj, get: obj => obj.transferShares }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _populateTransferShares_decorators, { kind: "method", name: "populateTransferShares", static: false, private: false, access: { has: obj => "populateTransferShares" in obj, get: obj => obj.populateTransferShares }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _simulateTransferShares_decorators, { kind: "method", name: "simulateTransferShares", static: false, private: false, access: { has: obj => "simulateTransferShares" in obj, get: obj => obj.simulateTransferShares }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), _a; })(); export { LidoSDKL2Steth }; //# sourceMappingURL=tokens.js.map