Skip to content

Commit

Permalink
rank routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jfschwarz committed Jul 1, 2024
1 parent 855aa1f commit 6b85e88
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/rank.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AccountType, ConnectionType, type Route } from './types'

const countExtraSignaturesRequired = (route: Route): number =>
route.waypoints.reduce<number>((result, waypoint) => {
if (
waypoint.account.type === AccountType.SAFE &&
'connection' in waypoint &&
waypoint.connection.type === ConnectionType.OWNS
) {
return result + waypoint.account.threshold - 1
}
return result
}, 0)

/**
* Orders the given routes so that the first route is the most "frictionless" execution path.
* The following criteria are used to determine the order:
* - Fewest extra signatures required
* - Shortest path length
*/
export const rankRoutes = (routes: Route[]): Route[] => {
return routes.sort((a, b) => {
const aExtraSignatures = countExtraSignaturesRequired(a)
const bExtraSignatures = countExtraSignaturesRequired(b)
if (aExtraSignatures !== bExtraSignatures) {
return aExtraSignatures - bExtraSignatures
}
return a.waypoints.length - b.waypoints.length
})
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
},
"exclude": ["dist"]
}

0 comments on commit 6b85e88

Please sign in to comment.