Skip to content

Commit

Permalink
feat(pilot-app): make sure a role update updates also the route endpo…
Browse files Browse the repository at this point in the history
…int (#568)
  • Loading branch information
frontendphil authored Jan 17, 2025
1 parent 8849d1b commit dfe843a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 4 deletions.
71 changes: 71 additions & 0 deletions packages/modules/src/updateRolesWaypoint.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Chain } from '@zodiac/chains'
import {
createMockEndWaypoint,
createMockExecutionRoute,
createMockOwnsConnection,
createMockRoleWaypoint,
createMockWaypoints,
randomAddress,
} from '@zodiac/test-utils'
import { formatPrefixedAddress } from 'ser-kit'
import { describe, expect, it } from 'vitest'
import { createEnabledConnection } from './createEnabledConnection'
import { getWaypoints } from './getWaypoints'
import { updateRolesWaypoint } from './updateRolesWaypoint'

describe('updateRolesWaypoint', () => {
describe('Create', () => {
it('changes an existing endpoint to a IS_ENABLED connection', () => {
const route = createMockExecutionRoute({
waypoints: createMockWaypoints({
end: createMockEndWaypoint({
connection: createMockOwnsConnection(),
}),
}),
})

const address = randomAddress()

const updatedRoute = updateRolesWaypoint(route, {
moduleAddress: address,
multisend: [],
version: 1,
})

const [, endPoint] = getWaypoints(updatedRoute)

expect(endPoint).toHaveProperty(
'connection',
createEnabledConnection(formatPrefixedAddress(Chain.ETH, address)),
)
})
})

describe('Update', () => {
it('changes an existing endpoint to a IS_ENABLED connection', () => {
const route = createMockExecutionRoute({
waypoints: createMockWaypoints({
waypoints: [createMockRoleWaypoint()],
end: createMockEndWaypoint({
connection: createMockOwnsConnection(),
}),
}),
})

const address = randomAddress()

const updatedRoute = updateRolesWaypoint(route, {
moduleAddress: address,
multisend: [],
version: 1,
})

const [, endPoint] = getWaypoints(updatedRoute)

expect(endPoint).toHaveProperty(
'connection',
createEnabledConnection(formatPrefixedAddress(Chain.ETH, address)),
)
})
})
})
36 changes: 32 additions & 4 deletions packages/modules/src/updateRolesWaypoint.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { invariant } from '@epic-web/invariant'
import { getChainId } from '@zodiac/chains'
import type { ExecutionRoute, HexAddress, Waypoints } from '@zodiac/schema'
import { AccountType } from 'ser-kit'
import type {
ExecutionRoute,
HexAddress,
Waypoint,
Waypoints,
} from '@zodiac/schema'
import { AccountType, formatPrefixedAddress } from 'ser-kit'
import { createEnabledConnection } from './createEnabledConnection'
import { createRolesWaypoint } from './createRolesWaypoint'
import { createSafeWaypoint } from './createSafeWaypoint'

type RoleUpdatePayload = {
moduleAddress: HexAddress
Expand Down Expand Up @@ -31,7 +38,7 @@ export const updateRolesWaypoint = (
if (hasRolesWaypoint(route.waypoints)) {
const newWaypoints = waypoints.map((waypoint) => {
if (waypoint.account.type !== AccountType.ROLES) {
return waypoint
return updateSafeConnection(waypoint, moduleAddress)
}

return createRolesWaypoint({
Expand Down Expand Up @@ -60,10 +67,31 @@ export const updateRolesWaypoint = (
version,
from: startingPoint.account.prefixedAddress,
}),
...waypoints,
...waypoints.map((waypoint) =>
updateSafeConnection(waypoint, moduleAddress),
),
],
}
}

const hasRolesWaypoint = (waypoints: Waypoints) =>
waypoints.some((waypoint) => waypoint.account.type === AccountType.ROLES)

const updateSafeConnection = (
waypoint: Waypoint,
moduleAddress: HexAddress,
): Waypoint => {
const { account } = waypoint

if (account.type !== AccountType.SAFE) {
return waypoint
}

return createSafeWaypoint({
chainId: account.chain,
safe: account.address,
connection: createEnabledConnection(
formatPrefixedAddress(account.chain, moduleAddress),
),
})
}

0 comments on commit dfe843a

Please sign in to comment.