-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Derive multisig ownership history based on past transactions and latest discovery output #103
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { | ||
SafeMultisigTransaction, | ||
SafeTransactionDecodedData, | ||
SafeTransactionDecodedParam, | ||
} from '@lz/libs' | ||
|
@@ -113,3 +114,114 @@ export function toUTC(dateString: string): string { | |
const date = new Date(dateString) | ||
return date.toUTCString() | ||
} | ||
|
||
export function getDecodedProperties(tx: SafeMultisigTransaction) { | ||
const parsingResult = SafeTransactionDecodedData.safeParse(tx.dataDecoded) | ||
|
||
if (parsingResult.success && tx.dataDecoded) { | ||
// SafeApi kit typings are wrong, string type applies only for transport layer | ||
// string is later parsed into json object | ||
const decodedCall = decodeCall( | ||
tx.dataDecoded as unknown as NonNullable<SafeTransactionDecodedData>, | ||
) | ||
|
||
return { | ||
method: decodedCall.method, | ||
signature: decodedCall.signature, | ||
callWithParams: decodedCall.functionCall, | ||
params: decodedCall.params, | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
export type MultisigOwnershipHistory = ReturnType< | ||
typeof deriveMultisigOwnership | ||
> | ||
|
||
export function deriveMultisigOwnership( | ||
allTransactions: SafeMultisigTransaction[], | ||
latestOwners: number, | ||
) { | ||
const ownerChangingTransactions = allTransactions | ||
.map((tx) => ({ raw: tx, decoded: getDecodedProperties(tx) })) | ||
.filter( | ||
(ownerModification) => | ||
(ownerModification.decoded?.method === 'addOwnerWithThreshold' || | ||
ownerModification.decoded?.method === 'removeOwner') && | ||
Comment on lines
+151
to
+152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we also need the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We ain't need threshold since whenever transactions was successfully executed, we assume quorum has been reached There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're only recreating owner's amount history |
||
ownerModification.raw.isExecuted, | ||
) | ||
|
||
// We start with the latest data so we yet don't know | ||
// how long it will be applicable for | ||
const ownerUpdates = [ | ||
{ | ||
toBlock: Infinity, | ||
toTimestamp: Infinity, | ||
owners: latestOwners, | ||
}, | ||
] | ||
|
||
let lastAmountOfOwners = latestOwners | ||
|
||
for (const ownerModification of ownerChangingTransactions) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const toBlock = ownerModification.raw.blockNumber! | ||
const toTimestamp = new Date(ownerModification.raw.submissionDate).valueOf() | ||
|
||
// Inc/Dev reversed since we are descending down the line | ||
if (ownerModification.decoded?.method === 'addOwnerWithThreshold') { | ||
ownerUpdates.push({ | ||
toTimestamp, | ||
toBlock, | ||
owners: --lastAmountOfOwners, | ||
}) | ||
} | ||
|
||
if (ownerModification.decoded?.method === 'removeOwner') { | ||
ownerUpdates.push({ | ||
toTimestamp, | ||
toBlock, | ||
owners: ++lastAmountOfOwners, | ||
}) | ||
} | ||
} | ||
|
||
// Invert and reassign toBlock and toTimestamp | ||
// to achieve ascending order thus easier usage | ||
const reversed = [...ownerUpdates].reverse() | ||
|
||
const updateHistory = [ | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
{ fromBlock: 0, fromTimestamp: 0, owners: reversed.at(0)!.owners }, | ||
] | ||
|
||
for (let i = 1; i < reversed.length; i++) { | ||
const tx = reversed[i] | ||
updateHistory.push({ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
fromTimestamp: reversed[i - 1]!.toTimestamp, | ||
fromBlock: reversed[i - 1]!.toBlock, | ||
owners: tx!.owners, | ||
/* eslint-enable @typescript-eslint/no-non-null-assertion */ | ||
}) | ||
} | ||
|
||
return updateHistory | ||
} | ||
|
||
export function getOwnershipForTransaction( | ||
history: MultisigOwnershipHistory, | ||
submissionDate: string, | ||
) { | ||
const ownershipForTime = history | ||
.sort((a, b) => b.fromTimestamp - a.fromTimestamp) | ||
.find((update) => update.fromTimestamp < new Date(submissionDate).valueOf()) | ||
|
||
if (!ownershipForTime) { | ||
throw new Error('Ownership for given time range not found') | ||
} | ||
|
||
return ownershipForTime.owners | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure that all the transactions here have been executed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's exactly the purpose of line ~153, where we filter only executed transactions