@metamask/delegation-toolkit
Version:
The Delegation Toolkit built on top of Viem - a library for interacting with DeleGator Smart Accounts
1 lines • 4.76 kB
Source Map (JSON)
{"version":3,"sources":["../src/executions.ts"],"sourcesContent":["import {\n type Address,\n type Hex,\n encodeAbiParameters,\n encodePacked,\n} from 'viem';\n\nexport type ExecutionStruct = {\n target: Address;\n value: bigint;\n callData: Hex;\n};\n\nexport type CreateExecutionArgs = {\n target: Address;\n value?: bigint;\n callData?: Hex;\n};\n\n/**\n * Creates an execution data structure.\n * @param args - The arguments for creating an execution.\n * @param args.target - The address to invoke some calldata on.\n * @param args.value - ETH to send to the address.\n * @param args.callData - The calldata to invoke on the address.\n * @returns The created execution data structure.\n */\nexport const createExecution = ({\n target,\n value = 0n,\n callData = '0x',\n}: CreateExecutionArgs): ExecutionStruct => ({\n target,\n value,\n callData,\n});\n\n// Encoded modes\n// https://github.com/erc7579/erc7579-implementation/blob/main/src/lib/ModeLib.sol\nexport enum ExecutionMode {\n SingleDefault = '0x0000000000000000000000000000000000000000000000000000000000000000',\n SingleTry = '0x0001000000000000000000000000000000000000000000000000000000000000',\n BatchDefault = '0x0100000000000000000000000000000000000000000000000000000000000000',\n BatchTry = '0x0101000000000000000000000000000000000000000000000000000000000000',\n}\n\n/**\n * The ABI type components of an Execution.\n */\nexport const EXECUTION_ABI_TYPE_COMPONENTS = [\n { type: 'address', name: 'target' },\n { type: 'uint256', name: 'value' },\n { type: 'bytes', name: 'callData' },\n];\n\n/**\n * Encodes a single Execution. Used for executing a single Execution in a DeleGator SCA.\n * @param execution - The execution to encode.\n * @returns The encoded execution.\n */\nexport const encodeSingleExecution = (execution: ExecutionStruct): Hex => {\n return encodePacked(\n ['address', 'uint256', 'bytes'],\n [execution.target, execution.value, execution.callData],\n );\n};\n\n/**\n * Encodes a batch of Executions. Used for executing a batch of Executions in a DeleGator SCA.\n * @param executions - The executions to encode.\n * @returns The encoded executions.\n */\nexport const encodeBatchExecution = (executions: ExecutionStruct[]): Hex => {\n return encodeAbiParameters(\n [\n {\n components: EXECUTION_ABI_TYPE_COMPONENTS,\n name: 'executions',\n type: 'tuple[]',\n },\n ],\n [executions],\n );\n};\n\n/**\n * Encodes the calldata for interacting with the advanced `execute` DeleGator method.\n * @param executions - The execution(s) to encode.\n * @returns The encoded execution(s).\n */\nexport const encodeExecutionCalldata = (executions: ExecutionStruct[]): Hex => {\n if (executions.length === 0) {\n throw new Error(\n 'Error while getting the execution calldatas, executions is empty',\n );\n }\n if (executions.length === 1) {\n const execution = executions[0];\n if (!execution) {\n throw new Error('Execution not found');\n }\n return encodeSingleExecution(execution);\n }\n\n return encodeBatchExecution(executions);\n};\n\n/**\n * Encodes the calldata for interacting with `redeemDelegations`.\n * @param executionsBatch - The executions to encode.\n * @returns The encoded executions.\n */\nexport const encodeExecutionCalldatas = (\n executionsBatch: ExecutionStruct[][],\n): Hex[] => {\n if (executionsBatch.length === 0) {\n throw new Error(\n 'Error while getting the execution calldatas, executionsBatch is empty',\n );\n }\n return executionsBatch.map(encodeExecutionCalldata);\n};\n"],"mappings":";AAAA;AAAA,EAGE;AAAA,EACA;AAAA,OACK;AAsBA,IAAM,kBAAkB,CAAC;AAAA,EAC9B;AAAA,EACA,QAAQ;AAAA,EACR,WAAW;AACb,OAA6C;AAAA,EAC3C;AAAA,EACA;AAAA,EACA;AACF;AAIO,IAAK,gBAAL,kBAAKA,mBAAL;AACL,EAAAA,eAAA,mBAAgB;AAChB,EAAAA,eAAA,eAAY;AACZ,EAAAA,eAAA,kBAAe;AACf,EAAAA,eAAA,cAAW;AAJD,SAAAA;AAAA,GAAA;AAUL,IAAM,gCAAgC;AAAA,EAC3C,EAAE,MAAM,WAAW,MAAM,SAAS;AAAA,EAClC,EAAE,MAAM,WAAW,MAAM,QAAQ;AAAA,EACjC,EAAE,MAAM,SAAS,MAAM,WAAW;AACpC;AAOO,IAAM,wBAAwB,CAAC,cAAoC;AACxE,SAAO;AAAA,IACL,CAAC,WAAW,WAAW,OAAO;AAAA,IAC9B,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