UNPKG

@exromany/lido-csm-sdk

Version:

[![GitHub license](https://img.shields.io/github/license/lidofinance/lido-csm-sdk?color=limegreen)](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [![Version npm](https://img.shields.io/npm/v/@lidofinance/lido-csm-sdk?label=version)](h

466 lines 24 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.TxSDK = void 0; const lido_ethereum_sdk_1 = require("@lidofinance/lido-ethereum-sdk"); const VersionCheck_js_1 = require("../abi/VersionCheck.js"); const csm_sdk_module_js_1 = require("../common/class-primitives/csm-sdk-module.js"); const error_handler_js_1 = require("../common/decorators/error-handler.js"); const logger_js_1 = require("../common/decorators/logger.js"); const index_js_1 = require("../common/index.js"); const is_capability_supported_js_1 = require("../common/utils/is-capability-supported.js"); const on_error_js_1 = require("../common/utils/on-error.js"); const consts_js_1 = require("./consts.js"); const parse_spending_props_js_1 = require("./parse-spending-props.js"); const prep_call_js_1 = require("./prep-call.js"); const strip_permit_js_1 = require("./strip-permit.js"); const types_js_1 = require("./types.js"); let TxSDK = (() => { var _a; let _classSuper = csm_sdk_module_js_1.CsmSDKModule; let _instanceExtraInitializers = []; let _isAbstractAccount_decorators; let _isMultisig_decorators; let _checkContractVersion_decorators; let _allowance_decorators; let _checkAllowance_decorators; let _signPermit_decorators; let _approve_decorators; let _signPermitOrApprove_decorators; return _a = class TxSDK extends _classSuper { get spender() { return this.core.getContractAddress(index_js_1.CONTRACT_NAMES.accounting); } getTokenContract(token) { return this.core.getContract(token); } async isAbstractAccount(account) { try { const capabilities = await this.core.walletClient.getCapabilities({ account, }); return (0, is_capability_supported_js_1.isCapabilitySupported)(capabilities, this.core.chainId, 'atomic'); } catch { return false; } } async isMultisig(_account) { const account = await this.core.core.useAccount(_account); return this.core.core.isContract(account.address); } async checkContractVersion(callResult) { const contractName = this.core.getContractNameByAddress(callResult.to); if (!contractName) return; const versionRange = this.core.supportedVersions[contractName]; if (!versionRange) return; let actualVersion; try { actualVersion = await this.core .getContractWithAbi(contractName, VersionCheck_js_1.VersionCheckAbi) .read.getInitializedVersion(); } catch (error) { actualVersion = (0, on_error_js_1.onVersionError)(error); if (actualVersion === 0n) return; throw error; } const [minVersion, maxVersion] = versionRange; if (actualVersion < minVersion || actualVersion > maxVersion) { throw this.core.core.error({ code: lido_ethereum_sdk_1.ERROR_CODE.NOT_SUPPORTED, message: `Contract ${contractName} version ${actualVersion} not supported. Required: ${minVersion}-${maxVersion}`, }); } } async internalTransaction(props) { const { callback = lido_ethereum_sdk_1.NOOP, getGasLimit, sendTransaction, decodeResult, waitForTransactionReceiptParameters = {}, } = props; const account = await this.core.core.useAccount(props.account); const isContract = await this.core.core.isContract(account.address); let overrides = { account, chain: this.core.chain, gas: undefined, maxFeePerGas: undefined, maxPriorityFeePerGas: undefined, }; if (isContract) { overrides = { ...overrides, gas: 21000n, maxFeePerGas: 1n, maxPriorityFeePerGas: 1n, nonce: 1, }; } else { await callback({ stage: types_js_1.TransactionCallbackStage.GAS_LIMIT }); const feeData = await this.core.core.getFeeData(); overrides.maxFeePerGas = feeData.maxFeePerGas; overrides.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas; try { overrides.gas = await getGasLimit({ ...overrides }); } catch { await (0, lido_ethereum_sdk_1.withSDKError)(getGasLimit({ ...overrides, maxFeePerGas: undefined, maxPriorityFeePerGas: undefined, }), lido_ethereum_sdk_1.ERROR_CODE.TRANSACTION_ERROR); throw this.core.core.error({ code: lido_ethereum_sdk_1.ERROR_CODE.TRANSACTION_ERROR, message: 'Not enough ether for gas', }); } } const customGas = await callback({ stage: types_js_1.TransactionCallbackStage.SIGN, payload: { gas: overrides.gas }, }); if (typeof customGas === 'bigint') overrides.gas = customGas; const hash = await (0, lido_ethereum_sdk_1.withSDKError)(sendTransaction({ ...overrides, }), lido_ethereum_sdk_1.ERROR_CODE.TRANSACTION_ERROR); if (isContract) { await callback({ stage: types_js_1.TransactionCallbackStage.MULTISIG_DONE }); return { hash }; } await callback({ stage: types_js_1.TransactionCallbackStage.RECEIPT, payload: { hash }, }); const receipt = await (0, lido_ethereum_sdk_1.withSDKError)(this.core.core.rpcProvider.waitForTransactionReceipt({ hash, timeout: 120_000, ...waitForTransactionReceiptParameters, }), lido_ethereum_sdk_1.ERROR_CODE.TRANSACTION_ERROR); await callback({ stage: types_js_1.TransactionCallbackStage.CONFIRMATION, payload: { receipt, hash }, }); const confirmations = await this.core.core.rpcProvider.getTransactionConfirmations({ hash: receipt.transactionHash, }); const result = await decodeResult?.(receipt); await callback({ stage: types_js_1.TransactionCallbackStage.DONE, payload: { result: result, confirmations, receipt, hash, }, }); return { hash, receipt, result, confirmations, }; } async internalCall(props) { const { callback = lido_ethereum_sdk_1.NOOP, calls, decodeResult, waitForTransactionReceiptParameters = {}, } = props; const account = await this.core.core.useAccount(props.account); await callback({ stage: types_js_1.TransactionCallbackStage.SIGN, payload: { gas: undefined }, }); const callData = await (0, lido_ethereum_sdk_1.withSDKError)(this.core.walletClient.sendCalls({ account, calls, experimental_fallback: true, }), lido_ethereum_sdk_1.ERROR_CODE.TRANSACTION_ERROR); await callback({ stage: types_js_1.TransactionCallbackStage.RECEIPT, payload: { id: callData.id }, }); const callStatus = await (0, lido_ethereum_sdk_1.withSDKError)(this.core.walletClient.waitForCallsStatus({ id: callData.id, pollingInterval: consts_js_1.AA_POLLING_INTERVAL, timeout: consts_js_1.AA_TX_POLLING_TIMEOUT, ...waitForTransactionReceiptParameters, }), lido_ethereum_sdk_1.ERROR_CODE.TRANSACTION_ERROR); if (callStatus.status === 'failure') { throw this.core.core.error({ code: lido_ethereum_sdk_1.ERROR_CODE.TRANSACTION_ERROR, message: 'Transaction failed. Check your wallet for details.', }); } if (callStatus.receipts?.find((receipt) => receipt.status === 'reverted')) { throw this.core.core.error({ code: lido_ethereum_sdk_1.ERROR_CODE.TRANSACTION_ERROR, message: 'Some operations were reverted. Check your wallet for details.', }); } const receipt = callStatus.receipts?.[callStatus.receipts.length - 1]; const txHash = receipt?.transactionHash; if (!txHash) { throw this.core.core.error({ code: lido_ethereum_sdk_1.ERROR_CODE.TRANSACTION_ERROR, message: 'Transaction hash is missing. Check your wallet for details.', }); } const result = await decodeResult?.(receipt); const confirmations = txHash ? await this.core.publicClient.getTransactionConfirmations({ hash: txHash, }) : 0n; await callback({ stage: types_js_1.TransactionCallbackStage.DONE, payload: { result: result, confirmations, receipt: receipt, hash: txHash, id: callData.id, }, }); return { hash: txHash, receipt: receipt, result, confirmations, }; } async perform(props) { const account = await this.core.core.useAccount(props.account); const isAA = await this.isAbstractAccount(account.address); if (isAA) { return this.performCall(props); } return this.performTransaction(props); } async performCall(props) { const calls = []; const approveCall = await this.getApproveCallIfNeeded(props); if (approveCall) { calls.push(approveCall); } const call = await this.prepareCall(props); await this.checkContractVersion(call); calls.push(call); return this.internalCall({ ...props, calls, }); } async performTransaction(props) { const { hash, permit } = await this.getPermit(props); if (hash) return { hash }; const call = await this.prepareCall(props, { permit }); await this.checkContractVersion(call); return this.internalTransaction({ ...props, ...this.callToInternalTransaction(call), }); } async prepareCall(props, options) { const account = await this.core.core.useAccount(props.account); return props.call({ from: account.address, permit: index_js_1.EMPTY_PERMIT, ...options, }); } callToInternalTransaction(call) { return { getGasLimit: this.callToGetGasLimit(call), sendTransaction: this.callToSendTransaction(call), }; } callToGetGasLimit(call) { return (options) => this.core.publicClient.estimateGas({ ...options, to: call.to, data: call.data, value: call.value, }); } callToSendTransaction(call) { return (options) => this.core.walletClient.sendTransaction({ ...options, to: call.to, data: call.data, value: call.value, }); } async getPermit(props) { if (!props.spend) { return {}; } const { permit } = props.spend; if (permit) return { permit: (0, strip_permit_js_1.stripPermit)(permit) }; const result = await this.signPermitOrApprove(props); return { hash: result.hash, permit: (0, strip_permit_js_1.stripPermit)(result.permit), }; } async getApproveCallIfNeeded(props) { if (!props.spend) { return undefined; } const { needsApprove } = await this.checkAllowance(props); if (!needsApprove) return undefined; return this.getApproveCall(props.spend); } async allowance({ account: accountProp, token, }) { const account = await this.core.core.useAccount(accountProp); const contract = this.getTokenContract(token); return contract.read.allowance([account.address, this.spender]); } async checkAllowance(props) { const { amount, token } = (0, parse_spending_props_js_1.parseSpendingProps)(props.spend); if (amount === 0n) { return { allowance: 0n, needsApprove: false, }; } const allowance = await this.allowance({ token, account: props.account }); const needsApprove = allowance < amount; return { allowance, needsApprove, }; } async signPermit(props) { const { token, amount, deadline } = (0, parse_spending_props_js_1.parseSpendingProps)(props.spend); await props.callback?.({ stage: types_js_1.TransactionCallbackStage.PERMIT_SIGN, payload: { token, amount }, }); return this.core.core.signPermit({ amount, token, deadline, spender: this.spender, account: props.account, }); } async approve(props) { const call = await this.getApproveCall(props.spend); return this.internalTransaction({ ...props, ...this.callToInternalTransaction(call), }); } async getApproveCall(props) { const { amount, token } = (0, parse_spending_props_js_1.parseSpendingProps)(props); return (0, prep_call_js_1.prepCall)(this.getTokenContract(token), 'approve', [ this.spender, amount, ]); } async signPermitOrApprove(props) { const [{ needsApprove }, isMultisig] = await Promise.all([ this.checkAllowance(props), this.isMultisig(props.account), ]); if (!needsApprove) { return { permit: index_js_1.EMPTY_PERMIT }; } if (isMultisig) { const { hash } = await this.approve({ ...props, callback: this.wrapApproveCallback(props), }); return { permit: index_js_1.EMPTY_PERMIT, hash }; } else { const permit = await this.signPermit(props); return { permit }; } } wrapApproveCallback({ callback, spend, }) { if (!callback) return undefined; const { token, amount } = (0, parse_spending_props_js_1.parseSpendingProps)(spend); return (args) => { switch (args.stage) { case types_js_1.TransactionCallbackStage.SIGN: return callback({ stage: types_js_1.TransactionCallbackStage.APPROVE_SIGN, payload: { token, amount }, }); case types_js_1.TransactionCallbackStage.RECEIPT: return callback({ stage: types_js_1.TransactionCallbackStage.APPROVE_RECEIPT, payload: { token, amount, hash: args.payload.hash }, }); case types_js_1.TransactionCallbackStage.MULTISIG_DONE: return callback(args); default: } }; } constructor() { super(...arguments); __runInitializers(this, _instanceExtraInitializers); } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; _isAbstractAccount_decorators = [(0, logger_js_1.Logger)('Views:')]; _isMultisig_decorators = [(0, logger_js_1.Logger)('Views:')]; _checkContractVersion_decorators = [(0, logger_js_1.Logger)('Utils:')]; _allowance_decorators = [(0, logger_js_1.Logger)('Views:'), (0, error_handler_js_1.ErrorHandler)()]; _checkAllowance_decorators = [(0, logger_js_1.Logger)('Utils:')]; _signPermit_decorators = [(0, logger_js_1.Logger)('Permit:'), (0, error_handler_js_1.ErrorHandler)()]; _approve_decorators = [(0, logger_js_1.Logger)('Call:'), (0, error_handler_js_1.ErrorHandler)()]; _signPermitOrApprove_decorators = [(0, logger_js_1.Logger)('Utils:')]; __esDecorate(_a, null, _isAbstractAccount_decorators, { kind: "method", name: "isAbstractAccount", static: false, private: false, access: { has: obj => "isAbstractAccount" in obj, get: obj => obj.isAbstractAccount }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _isMultisig_decorators, { kind: "method", name: "isMultisig", static: false, private: false, access: { has: obj => "isMultisig" in obj, get: obj => obj.isMultisig }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _checkContractVersion_decorators, { kind: "method", name: "checkContractVersion", static: false, private: false, access: { has: obj => "checkContractVersion" in obj, get: obj => obj.checkContractVersion }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _allowance_decorators, { kind: "method", name: "allowance", static: false, private: false, access: { has: obj => "allowance" in obj, get: obj => obj.allowance }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _checkAllowance_decorators, { kind: "method", name: "checkAllowance", static: false, private: false, access: { has: obj => "checkAllowance" in obj, get: obj => obj.checkAllowance }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _signPermit_decorators, { kind: "method", name: "signPermit", static: false, private: false, access: { has: obj => "signPermit" in obj, get: obj => obj.signPermit }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _approve_decorators, { kind: "method", name: "approve", static: false, private: false, access: { has: obj => "approve" in obj, get: obj => obj.approve }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _signPermitOrApprove_decorators, { kind: "method", name: "signPermitOrApprove", static: false, private: false, access: { has: obj => "signPermitOrApprove" in obj, get: obj => obj.signPermitOrApprove }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), _a; })(); exports.TxSDK = TxSDK; //# sourceMappingURL=tx-sdk.js.map