-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
32 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters