@metamask/delegation-toolkit
Version:
The Delegation Toolkit built on top of Viem - a library for interacting with DeleGator Smart Accounts
1 lines • 5.7 kB
Source Map (JSON)
{"version":3,"sources":["/home/runner/work/delegation-toolkit/delegation-toolkit/packages/delegation-toolkit/dist/chunk-T6PSFUOZ.cjs","../src/executions.ts"],"names":["ExecutionMode"],"mappings":"AAAA;ACAA;AAGE;AACA;AAAA,4BACK;AAsBA,IAAM,gBAAA,EAAkB,CAAC;AAAA,EAC9B,MAAA;AAAA,EACA,MAAA,EAAQ,EAAA;AAAA,EACR,SAAA,EAAW;AACb,CAAA,EAAA,GAAA,CAA6C;AAAA,EAC3C,MAAA;AAAA,EACA,KAAA;AAAA,EACA;AACF,CAAA,CAAA;AAIO,IAAK,cAAA,kBAAL,CAAA,CAAKA,cAAAA,EAAAA,GAAL;AACL,EAAAA,cAAAA,CAAA,eAAA,EAAA,EAAgB,oEAAA;AAChB,EAAAA,cAAAA,CAAA,WAAA,EAAA,EAAY,oEAAA;AACZ,EAAAA,cAAAA,CAAA,cAAA,EAAA,EAAe,oEAAA;AACf,EAAAA,cAAAA,CAAA,UAAA,EAAA,EAAW,oEAAA;AAJD,EAAA,OAAAA,cAAAA;AAAA,CAAA,CAAA,CAAA,cAAA,GAAA,CAAA,CAAA,CAAA;AAUL,IAAM,8BAAA,EAAgC;AAAA,EAC3C,EAAE,IAAA,EAAM,SAAA,EAAW,IAAA,EAAM,SAAS,CAAA;AAAA,EAClC,EAAE,IAAA,EAAM,SAAA,EAAW,IAAA,EAAM,QAAQ,CAAA;AAAA,EACjC,EAAE,IAAA,EAAM,OAAA,EAAS,IAAA,EAAM,WAAW;AACpC,CAAA;AAOO,IAAM,sBAAA,EAAwB,CAAC,SAAA,EAAA,GAAoC;AACxE,EAAA,OAAO,gCAAA;AAAA,IACL,CAAC,SAAA,EAAW,SAAA,EAAW,OAAO,CAAA;AAAA,IAC9B,CAAC,SAAA,CAAU,MAAA,EAAQ,SAAA,CAAU,KAAA,EAAO,SAAA,CAAU,QAAQ;AAAA,EACxD,CAAA;AACF,CAAA;AAOO,IAAM,qBAAA,EAAuB,CAAC,UAAA,EAAA,GAAuC;AAC1E,EAAA,OAAO,uCAAA;AAAA,IACL;AAAA,MACE;AAAA,QACE,UAAA,EAAY,6BAAA;AAAA,QACZ,IAAA,EAAM,YAAA;AAAA,QACN,IAAA,EAAM;AAAA,MACR;AAAA,IACF,CAAA;AAAA,IACA,CAAC,UAAU;AAAA,EACb,CAAA;AACF,CAAA;AAOO,IAAM,wBAAA,EAA0B,CAAC,UAAA,EAAA,GAAuC;AAC7E,EAAA,GAAA,CAAI,UAAA,CAAW,OAAA,IAAW,CAAA,EAAG;AAC3B,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,IACF,CAAA;AAAA,EACF;AACA,EAAA,GAAA,CAAI,UAAA,CAAW,OAAA,IAAW,CAAA,EAAG;AAC3B,IAAA,MAAM,UAAA,EAAY,UAAA,CAAW,CAAC,CAAA;AAC9B,IAAA,GAAA,CAAI,CAAC,SAAA,EAAW;AACd,MAAA,MAAM,IAAI,KAAA,CAAM,qBAAqB,CAAA;AAAA,IACvC;AACA,IAAA,OAAO,qBAAA,CAAsB,SAAS,CAAA;AAAA,EACxC;AAEA,EAAA,OAAO,oBAAA,CAAqB,UAAU,CAAA;AACxC,CAAA;AAOO,IAAM,yBAAA,EAA2B,CACtC,eAAA,EAAA,GACU;AACV,EAAA,GAAA,CAAI,eAAA,CAAgB,OAAA,IAAW,CAAA,EAAG;AAChC,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,IACF,CAAA;AAAA,EACF;AACA,EAAA,OAAO,eAAA,CAAgB,GAAA,CAAI,uBAAuB,CAAA;AACpD,CAAA;ADtDA;AACA;AACE;AACA;AACA;AACA;AACA;AACA;AACF,qTAAC","file":"/home/runner/work/delegation-toolkit/delegation-toolkit/packages/delegation-toolkit/dist/chunk-T6PSFUOZ.cjs","sourcesContent":[null,"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"]}