From f37793bbe0dfe1bd4a696fc7e433f3570f012eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crist=C3=B3v=C3=A3o?= Date: Fri, 20 Dec 2024 19:04:18 +0100 Subject: [PATCH] fix: Safe as Initiator (#22) Allow safes as initiators in Route --- src/execute/plan.test.ts | 8 +------- src/execute/plan.ts | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/execute/plan.test.ts b/src/execute/plan.test.ts index 43bc1d7..340d99b 100644 --- a/src/execute/plan.test.ts +++ b/src/execute/plan.test.ts @@ -7,13 +7,7 @@ import { OperationType } from '@safe-global/types-kit' import { formatPrefixedAddress } from '../addresses' -import { - AccountType, - ConnectionType, - MetaTransactionRequest, - PrefixedAddress, - Route, -} from '../types' +import { MetaTransactionRequest } from '../types' import { ExecutionActionType, ExecuteTransactionAction, diff --git a/src/execute/plan.ts b/src/execute/plan.ts index a01d2b4..0042a32 100644 --- a/src/execute/plan.ts +++ b/src/execute/plan.ts @@ -153,14 +153,16 @@ const planAsSafe = async ( index: number, options: Options ): Promise => { - const { waypoint, left, right } = pointers(waypoints, index) + const { waypoint, connection, left, right } = pointers(waypoints, index) + assert(waypoint.account.type == AccountType.SAFE) - assert( - 'connection' in waypoint && - (waypoint.connection.type == ConnectionType.IS_ENABLED || - waypoint.connection.type == ConnectionType.OWNS) - ) + if (left !== null) { + assert( + connection?.type == ConnectionType.IS_ENABLED || + connection?.type == ConnectionType.OWNS + ) + } assert( request.type == ExecutionActionType.SAFE_TRANSACTION || @@ -219,7 +221,11 @@ const planAsSafe = async ( } // OUT - if (waypoint.connection.type == ConnectionType.OWNS) { + const isInitiator = left == null + const isUpstreamOwner = connection?.type == ConnectionType.OWNS + const isUpstreamModule = connection?.type == ConnectionType.IS_ENABLED + + if (isUpstreamOwner) { return [ { type: shouldPropose(waypoint, options) @@ -241,8 +247,7 @@ const planAsSafe = async ( }, ...result, ] - } else { - assert(waypoint.connection.type == ConnectionType.IS_ENABLED) + } else if (isUpstreamModule) { return [ { type: ExecutionActionType.EXECUTE_TRANSACTION, @@ -256,6 +261,17 @@ const planAsSafe = async ( }, ...result, ] + } else { + assert(isInitiator) + return [ + { + type: ExecutionActionType.EXECUTE_TRANSACTION, + chain: waypoint.account.chain, + from: waypoint.account.address, + transaction, + }, + ...result, + ] } } @@ -397,7 +413,7 @@ function pointers(waypoints: Route['waypoints'], index: number) { if (left) { assert( 'connection' in waypoint && - waypoint.connection.from == left?.account.prefixedAddress + waypoint.connection.from == left.account.prefixedAddress ) } @@ -414,9 +430,11 @@ function pointers(waypoints: Route['waypoints'], index: number) { waypoint, left, right, + connection: left ? (waypoint as Waypoint).connection! : null, } as { waypoint: StartingPoint | Waypoint left: StartingPoint | Waypoint | null right: Waypoint | null + connection: Connection | null } }