UNPKG

@lidofinance/lido-ethereum-sdk

Version:

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

326 lines 20.4 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; }; import { LidoSDKModule } from '../common/class-primitives/sdk-module.js'; import { LidoSDKL2Steth, LidoSDKL2Wsteth } from './tokens.js'; import { getContract, encodeFunctionData, decodeEventLog, isAddressEqual, getAbiItem, toEventHash, } from 'viem'; import { LIDO_L2_CONTRACT_ADDRESSES, LIDO_L2_CONTRACT_NAMES, NOOP, } from '../common/constants.js'; import { Cache, Logger, ErrorHandler } from '../common/decorators/index.js'; import { rebasableL2StethAbi } from './abi/rebasableL2Steth.js'; import { parseValue } from '../common/utils/parse-value.js'; import { invariant, ERROR_CODE } from '../common/index.js'; let LidoSDKL2 = (() => { var _a; let _classSuper = LidoSDKModule; let _instanceExtraInitializers = []; let _contractAddress_decorators; let _getContract_decorators; let _approveWstethForWrap_decorators; let _approveWstethForWrapEstimateGas_decorators; let _approveWstethForWrapPopulateTx_decorators; let _getWstethForWrapAllowance_decorators; let _wrapWstethToSteth_decorators; let _wrapWstethToStethPopulateTx_decorators; let _wrapWstethToStethEstimateGas_decorators; let _wrapWstethToStethParseEvents_decorators; let _wrapWstethToStethSimulateTx_decorators; let _unwrapStethToWsteth_decorators; let _unwrapStethPopulateTx_decorators; let _unwrapStethSimulateTx_decorators; let _unwrapStethEstimateGas_decorators; let _unwrapParseEvents_decorators; let _parseProps_decorators; return _a = class LidoSDKL2 extends _classSuper { static isContractAvailableOn(contract, chain) { return !!LIDO_L2_CONTRACT_ADDRESSES[chain]?.[contract]; } constructor(props) { super(props); Object.defineProperty(this, "wsteth", { enumerable: true, configurable: true, writable: true, value: __runInitializers(this, _instanceExtraInitializers) }); Object.defineProperty(this, "steth", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.wsteth = new LidoSDKL2Wsteth({ core: this.core }); this.steth = new LidoSDKL2Steth({ core: this.core }); } async contractAddress() { return this.core.getL2ContractAddress(LIDO_L2_CONTRACT_NAMES.steth); } async getContract() { const address = await this.contractAddress(); return getContract({ address, abi: rebasableL2StethAbi, client: { public: this.core.rpcProvider, wallet: this.core.web3Provider, }, }); } // Wrap wstETH to stETH async approveWstethForWrap(props) { const stethAddress = await this.contractAddress(); return this.wsteth.approve({ ...props, amount: props.value, to: stethAddress, }); } async approveWstethForWrapEstimateGas(props, options) { const stethAddress = await this.contractAddress(); return this.wsteth.estimateApprove({ ...props, amount: props.value, to: stethAddress, }, options); } async approveWstethForWrapPopulateTx(props) { const stethAddress = await this.contractAddress(); return this.wsteth.populateApprove({ ...props, amount: props.value, to: stethAddress, }); } async getWstethForWrapAllowance(account) { const stethAddress = await this.contractAddress(); return this.wsteth.allowance({ account, to: stethAddress }); } async wrapWstethToSteth(props) { this.core.useWeb3Provider(); const { account, callback, value, ...rest } = await this.parseProps(props); const contract = await this.getContract(); return this.core.performTransaction({ ...rest, account, callback, getGasLimit: (options) => contract.estimateGas.wrap([value], options), sendTransaction: (options) => contract.write.wrap([value], options), decodeResult: (receipt) => this.wrapWstethToStethParseEvents(receipt, account.address), }); } async wrapWstethToStethPopulateTx(props) { const { value, account } = await this.parseProps(props); const address = await this.contractAddress(); return { to: address, from: account.address, data: encodeFunctionData({ abi: rebasableL2StethAbi, functionName: 'wrap', args: [value], }), }; } async wrapWstethToStethEstimateGas(props, options) { const { value, account } = await this.parseProps(props); const contract = await this.getContract(); return contract.estimateGas.wrap([value], { account, ...options, }); } async wrapWstethToStethParseEvents(receipt, address) { let stethReceived; let wstethWrapped; for (const log of receipt.logs) { // skips non-relevant events if (log.topics[0] !== _a.TRANSFER_SIGNATURE) continue; const parsedLog = decodeEventLog({ abi: rebasableL2StethAbi, strict: true, eventName: 'Transfer', data: log.data, topics: log.topics, }); if (isAddressEqual(parsedLog.args.from, address)) { wstethWrapped = parsedLog.args.value; } else if (isAddressEqual(parsedLog.args.to, address)) { stethReceived = parsedLog.args.value; } } invariant(stethReceived, 'could not find Transfer event in wrap transaction', ERROR_CODE.TRANSACTION_ERROR); invariant(wstethWrapped, 'could not find Transfer event in wrap transaction', ERROR_CODE.TRANSACTION_ERROR); return { stethReceived, wstethWrapped, }; } async wrapWstethToStethSimulateTx(props) { const { value, account } = await this.parseProps(props); const contract = await this.getContract(); const { request } = await contract.simulate.wrap([value], { account, }); return request; } // unwrap stETH to wstETH async unwrapStethToWsteth(props) { this.core.useWeb3Provider(); const { account, callback, value, ...rest } = await this.parseProps(props); const contract = await this.getContract(); return this.core.performTransaction({ ...rest, account, callback, getGasLimit: (options) => contract.estimateGas.unwrap([value], options), sendTransaction: (options) => contract.write.unwrap([value], options), decodeResult: (receipt) => this.unwrapParseEvents(receipt, account.address), }); } async unwrapStethPopulateTx(props) { const { value, account } = await this.parseProps(props); const to = await this.contractAddress(); return { to, from: account.address, data: encodeFunctionData({ abi: rebasableL2StethAbi, functionName: 'unwrap', args: [value], }), }; } async unwrapStethSimulateTx(props) { const { value, account } = await this.parseProps(props); const contract = await this.getContract(); const { request } = await contract.simulate.unwrap([value], { account, }); return request; } async unwrapStethEstimateGas(props, options) { const { value, account } = await this.parseProps(props); const contract = await this.getContract(); return contract.estimateGas.unwrap([value], { account, ...options, }); } async unwrapParseEvents(receipt, address) { let stethUnwrapped; let wstethReceived; for (const log of receipt.logs) { // skips non-relevant events if (log.topics[0] !== _a.TRANSFER_SIGNATURE) continue; const parsedLog = decodeEventLog({ abi: rebasableL2StethAbi, strict: true, topics: log.topics, data: log.data, eventName: 'Transfer', }); if (isAddressEqual(parsedLog.args.from, address)) { stethUnwrapped = parsedLog.args.value; } else if (isAddressEqual(parsedLog.args.to, address)) { wstethReceived = parsedLog.args.value; } } invariant(stethUnwrapped, 'could not find Transfer event in unwrap transaction', ERROR_CODE.TRANSACTION_ERROR); invariant(wstethReceived, 'could not find Transfer event in unwrap transaction', ERROR_CODE.TRANSACTION_ERROR); return { stethUnwrapped, wstethReceived, }; } // Utils async parseProps(props) { return { ...props, account: await this.core.useAccount(props.account), value: parseValue(props.value), callback: props.callback ?? NOOP, }; } }, (() => { 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'])]; _getContract_decorators = [Logger('Contracts:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _approveWstethForWrap_decorators = [Logger('Call:'), ErrorHandler()]; _approveWstethForWrapEstimateGas_decorators = [Logger('Call:'), ErrorHandler()]; _approveWstethForWrapPopulateTx_decorators = [Logger('Utils:')]; _getWstethForWrapAllowance_decorators = [Logger('Utils:'), ErrorHandler()]; _wrapWstethToSteth_decorators = [Logger('Call:'), ErrorHandler()]; _wrapWstethToStethPopulateTx_decorators = [Logger('Utils:')]; _wrapWstethToStethEstimateGas_decorators = [Logger('Utils:')]; _wrapWstethToStethParseEvents_decorators = [Logger('Utils:')]; _wrapWstethToStethSimulateTx_decorators = [Logger('Call:'), ErrorHandler()]; _unwrapStethToWsteth_decorators = [Logger('Call:'), ErrorHandler()]; _unwrapStethPopulateTx_decorators = [Logger('Utils:'), ErrorHandler()]; _unwrapStethSimulateTx_decorators = [Logger('Call:'), ErrorHandler()]; _unwrapStethEstimateGas_decorators = [Logger('Utils:'), ErrorHandler()]; _unwrapParseEvents_decorators = [Logger('Utils:')]; _parseProps_decorators = [Logger('Utils:')]; __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, _getContract_decorators, { kind: "method", name: "getContract", static: false, private: false, access: { has: obj => "getContract" in obj, get: obj => obj.getContract }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _approveWstethForWrap_decorators, { kind: "method", name: "approveWstethForWrap", static: false, private: false, access: { has: obj => "approveWstethForWrap" in obj, get: obj => obj.approveWstethForWrap }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _approveWstethForWrapEstimateGas_decorators, { kind: "method", name: "approveWstethForWrapEstimateGas", static: false, private: false, access: { has: obj => "approveWstethForWrapEstimateGas" in obj, get: obj => obj.approveWstethForWrapEstimateGas }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _approveWstethForWrapPopulateTx_decorators, { kind: "method", name: "approveWstethForWrapPopulateTx", static: false, private: false, access: { has: obj => "approveWstethForWrapPopulateTx" in obj, get: obj => obj.approveWstethForWrapPopulateTx }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getWstethForWrapAllowance_decorators, { kind: "method", name: "getWstethForWrapAllowance", static: false, private: false, access: { has: obj => "getWstethForWrapAllowance" in obj, get: obj => obj.getWstethForWrapAllowance }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _wrapWstethToSteth_decorators, { kind: "method", name: "wrapWstethToSteth", static: false, private: false, access: { has: obj => "wrapWstethToSteth" in obj, get: obj => obj.wrapWstethToSteth }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _wrapWstethToStethPopulateTx_decorators, { kind: "method", name: "wrapWstethToStethPopulateTx", static: false, private: false, access: { has: obj => "wrapWstethToStethPopulateTx" in obj, get: obj => obj.wrapWstethToStethPopulateTx }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _wrapWstethToStethEstimateGas_decorators, { kind: "method", name: "wrapWstethToStethEstimateGas", static: false, private: false, access: { has: obj => "wrapWstethToStethEstimateGas" in obj, get: obj => obj.wrapWstethToStethEstimateGas }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _wrapWstethToStethParseEvents_decorators, { kind: "method", name: "wrapWstethToStethParseEvents", static: false, private: false, access: { has: obj => "wrapWstethToStethParseEvents" in obj, get: obj => obj.wrapWstethToStethParseEvents }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _wrapWstethToStethSimulateTx_decorators, { kind: "method", name: "wrapWstethToStethSimulateTx", static: false, private: false, access: { has: obj => "wrapWstethToStethSimulateTx" in obj, get: obj => obj.wrapWstethToStethSimulateTx }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _unwrapStethToWsteth_decorators, { kind: "method", name: "unwrapStethToWsteth", static: false, private: false, access: { has: obj => "unwrapStethToWsteth" in obj, get: obj => obj.unwrapStethToWsteth }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _unwrapStethPopulateTx_decorators, { kind: "method", name: "unwrapStethPopulateTx", static: false, private: false, access: { has: obj => "unwrapStethPopulateTx" in obj, get: obj => obj.unwrapStethPopulateTx }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _unwrapStethSimulateTx_decorators, { kind: "method", name: "unwrapStethSimulateTx", static: false, private: false, access: { has: obj => "unwrapStethSimulateTx" in obj, get: obj => obj.unwrapStethSimulateTx }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _unwrapStethEstimateGas_decorators, { kind: "method", name: "unwrapStethEstimateGas", static: false, private: false, access: { has: obj => "unwrapStethEstimateGas" in obj, get: obj => obj.unwrapStethEstimateGas }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _unwrapParseEvents_decorators, { kind: "method", name: "unwrapParseEvents", static: false, private: false, access: { has: obj => "unwrapParseEvents" in obj, get: obj => obj.unwrapParseEvents }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _parseProps_decorators, { kind: "method", name: "parseProps", static: false, private: false, access: { has: obj => "parseProps" in obj, get: obj => obj.parseProps }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), Object.defineProperty(_a, "TRANSFER_SIGNATURE", { enumerable: true, configurable: true, writable: true, value: toEventHash(getAbiItem({ abi: rebasableL2StethAbi, name: 'Transfer' })) }), _a; })(); export { LidoSDKL2 }; //# sourceMappingURL=l2.js.map