UNPKG

@uniswap/smart-wallet-sdk

Version:

⚒️ An SDK for building applications with smart wallets on Uniswap

85 lines 3.13 kB
import { encodeFunctionData } from 'viem'; import abi from '../abis/MinimalDelegationEntry.json'; import { ModeType, SMART_WALLET_ADDRESSES } from './constants'; import { CallPlanner } from './utils'; import { BatchedCallPlanner } from './utils/batchedCallPlanner'; /** * Main SDK class for interacting with Uniswap smart wallet contracts */ export class SmartWallet { /** * Creates method parameters for executing a simple batch of calls through a smart wallet * @param calls Array of calls to encode * @param options Basic options for the execution * @returns Method parameters with calldata and value */ static encodeBatchedCall(calls, options = {}) { const planner = new CallPlanner(calls); const batchedCallPlanner = new BatchedCallPlanner(planner, options.revertOnFailure); const encoded = encodeFunctionData({ abi, functionName: '0x99e1d016', // execute(((address,uint256,bytes)[],bool)) args: [batchedCallPlanner.toBatchedCall()] }); return { calldata: encoded, value: planner.value }; } /** * ERC7821 compatible entrypoint for executing batched calls through the contract * @deprecated use encodeBatchedCall instead unless you need to use the ERC7821 entrypoint */ static encodeERC7821BatchedCall(calls, options = {}) { const mode = this.getModeFromOptions(options); if (mode != ModeType.BATCHED_CALL && mode != ModeType.BATCHED_CALL_CAN_REVERT) { throw new Error(`Invalid mode: ${mode}`); } const planner = new CallPlanner(calls); const executionData = planner.encode(); const encoded = this._encodeERC7821Execute(mode, executionData); return { calldata: encoded, value: planner.value }; } /** * Creates a call to execute a method through a smart wallet * @dev can be refactored to return a Transaction object as well * @param methodParameters The method parameters to execute * @param chainId The chain ID for the smart wallet * @returns The call to execute */ static createExecute(methodParameters, chainId) { const address = SMART_WALLET_ADDRESSES[chainId]; if (!address) { throw new Error(`Smart wallet not found for chainId: ${chainId}`); } return { to: address, data: methodParameters.calldata, value: methodParameters.value }; } /** * Get the mode type from the options */ static getModeFromOptions(options) { if (options.revertOnFailure) { return ModeType.BATCHED_CALL; } return ModeType.BATCHED_CALL_CAN_REVERT; } /** Internal methods */ static _encodeERC7821Execute(mode, data) { return encodeFunctionData({ abi, functionName: '0xe9ae5c53', // execute(bytes32,bytes) args: [ mode, data ] }); } } //# sourceMappingURL=smartWallet.js.map