UNPKG

@lidofinance/lido-ethereum-sdk

Version:

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

324 lines 21.1 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.LidoSDKL2 = void 0; const sdk_module_js_1 = require("../common/class-primitives/sdk-module.js"); const tokens_js_1 = require("./tokens.js"); const viem_1 = require("viem"); const constants_js_1 = require("../common/constants.js"); const index_js_1 = require("../common/decorators/index.js"); const rebasableL2Steth_js_1 = require("./abi/rebasableL2Steth.js"); const parse_value_js_1 = require("../common/utils/parse-value.js"); const index_js_2 = require("../common/index.js"); let LidoSDKL2 = (() => { var _a; let _classSuper = sdk_module_js_1.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 !!constants_js_1.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 tokens_js_1.LidoSDKL2Wsteth({ core: this.core }); this.steth = new tokens_js_1.LidoSDKL2Steth({ core: this.core }); } async contractAddress() { return this.core.getL2ContractAddress(constants_js_1.LIDO_L2_CONTRACT_NAMES.steth); } async getContract() { const address = await this.contractAddress(); return (0, viem_1.getContract)({ address, abi: rebasableL2Steth_js_1.rebasableL2StethAbi, client: { public: this.core.rpcProvider, wallet: this.core.web3Provider, }, }); } 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: (0, viem_1.encodeFunctionData)({ abi: rebasableL2Steth_js_1.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) { if (log.topics[0] !== _a.TRANSFER_SIGNATURE) continue; const parsedLog = (0, viem_1.decodeEventLog)({ abi: rebasableL2Steth_js_1.rebasableL2StethAbi, strict: true, eventName: 'Transfer', data: log.data, topics: log.topics, }); if ((0, viem_1.isAddressEqual)(parsedLog.args.from, address)) { wstethWrapped = parsedLog.args.value; } else if ((0, viem_1.isAddressEqual)(parsedLog.args.to, address)) { stethReceived = parsedLog.args.value; } } (0, index_js_2.invariant)(stethReceived, 'could not find Transfer event in wrap transaction', index_js_2.ERROR_CODE.TRANSACTION_ERROR); (0, index_js_2.invariant)(wstethWrapped, 'could not find Transfer event in wrap transaction', index_js_2.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; } 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: (0, viem_1.encodeFunctionData)({ abi: rebasableL2Steth_js_1.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) { if (log.topics[0] !== _a.TRANSFER_SIGNATURE) continue; const parsedLog = (0, viem_1.decodeEventLog)({ abi: rebasableL2Steth_js_1.rebasableL2StethAbi, strict: true, topics: log.topics, data: log.data, eventName: 'Transfer', }); if ((0, viem_1.isAddressEqual)(parsedLog.args.from, address)) { stethUnwrapped = parsedLog.args.value; } else if ((0, viem_1.isAddressEqual)(parsedLog.args.to, address)) { wstethReceived = parsedLog.args.value; } } (0, index_js_2.invariant)(stethUnwrapped, 'could not find Transfer event in unwrap transaction', index_js_2.ERROR_CODE.TRANSACTION_ERROR); (0, index_js_2.invariant)(wstethReceived, 'could not find Transfer event in unwrap transaction', index_js_2.ERROR_CODE.TRANSACTION_ERROR); return { stethUnwrapped, wstethReceived, }; } async parseProps(props) { return { ...props, account: await this.core.useAccount(props.account), value: (0, parse_value_js_1.parseValue)(props.value), callback: props.callback ?? constants_js_1.NOOP, }; } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; _contractAddress_decorators = [(0, index_js_1.Logger)('Contracts:'), (0, index_js_1.Cache)(30 * 60 * 1000, ['core.chain.id'])]; _getContract_decorators = [(0, index_js_1.Logger)('Contracts:'), (0, index_js_1.Cache)(30 * 60 * 1000, ['core.chain.id'])]; _approveWstethForWrap_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _approveWstethForWrapEstimateGas_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _approveWstethForWrapPopulateTx_decorators = [(0, index_js_1.Logger)('Utils:')]; _getWstethForWrapAllowance_decorators = [(0, index_js_1.Logger)('Utils:'), (0, index_js_1.ErrorHandler)()]; _wrapWstethToSteth_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _wrapWstethToStethPopulateTx_decorators = [(0, index_js_1.Logger)('Utils:')]; _wrapWstethToStethEstimateGas_decorators = [(0, index_js_1.Logger)('Utils:')]; _wrapWstethToStethParseEvents_decorators = [(0, index_js_1.Logger)('Utils:')]; _wrapWstethToStethSimulateTx_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _unwrapStethToWsteth_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _unwrapStethPopulateTx_decorators = [(0, index_js_1.Logger)('Utils:'), (0, index_js_1.ErrorHandler)()]; _unwrapStethSimulateTx_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _unwrapStethEstimateGas_decorators = [(0, index_js_1.Logger)('Utils:'), (0, index_js_1.ErrorHandler)()]; _unwrapParseEvents_decorators = [(0, index_js_1.Logger)('Utils:')]; _parseProps_decorators = [(0, index_js_1.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: (0, viem_1.toEventHash)((0, viem_1.getAbiItem)({ abi: rebasableL2Steth_js_1.rebasableL2StethAbi, name: 'Transfer' })) }), _a; })(); exports.LidoSDKL2 = LidoSDKL2; //# sourceMappingURL=l2.js.map