-
Notifications
You must be signed in to change notification settings - Fork 199
feat(v5): decode calls for LA and MAv2 #2258
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92c3caa
feat: add light account decode calls
adamegyed 38d3e55
test: add encode and decode tests
adamegyed 8c9a54c
fix: correctly encode self-calls for MAv2
adamegyed 4d722f5
fix: review fixes
adamegyed b44fe5b
test: test fixes
adamegyed b24c2eb
test: add missing test assertion
adamegyed 6065183
fix: test fixes
adamegyed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
101 changes: 101 additions & 0 deletions
101
packages/smart-accounts/src/light-account/accounts/calldataCodec.ts
This file contains hidden or 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,101 @@ | ||
| import { | ||
| decodeFunctionData, | ||
| encodeFunctionData, | ||
| type Address, | ||
| type Call, | ||
| type Hex, | ||
| } from "viem"; | ||
| import { LightAccountAbi_v1 } from "../abis/LightAccountAbi_v1.js"; | ||
|
|
||
| // Conveniently, all variants of LA up to v2.0.0 use the same function signatures for `execute` and `executeBatch`. | ||
|
|
||
| export function encodeCallsLA(calls: readonly Call[]): Hex { | ||
| if (calls.length === 1) { | ||
| return encodeFunctionData({ | ||
| abi: LightAccountAbi_v1, | ||
| functionName: "execute", | ||
| args: [calls[0].to, calls[0].value ?? 0n, calls[0].data ?? "0x"], | ||
| }); | ||
| } | ||
|
|
||
| const [targets, values, datas, hasValue] = calls.reduce( | ||
| (accum, curr) => { | ||
| accum[0].push(curr.to); | ||
| accum[1].push(curr.value ?? 0n); | ||
| accum[2].push(curr.data ?? "0x"); | ||
| accum[3] = accum[3] || (curr.value ?? 0n) !== 0n; | ||
| return accum; | ||
| }, | ||
| [[], [], [], false] as [Address[], bigint[], Hex[], boolean], | ||
| ); | ||
|
|
||
| return encodeFunctionData({ | ||
| abi: LightAccountAbi_v1, | ||
| functionName: "executeBatch", | ||
| args: hasValue ? [targets, values, datas] : [targets, datas], | ||
| }); | ||
| } | ||
|
|
||
| export function decodeCallsLA(data: Hex, accountAddress: Address): Call[] { | ||
| const decoded = decodeFunctionData({ | ||
| abi: LightAccountAbi_v1, | ||
| data, | ||
| }); | ||
|
|
||
| if (decoded.functionName === "execute") { | ||
| return [ | ||
| { | ||
| to: decoded.args[0], | ||
| data: decoded.args[2], | ||
| ...(decoded.args[1] !== undefined ? { value: decoded.args[1] } : {}), | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| if (decoded.functionName === "executeBatch") { | ||
| // This function is overloaded - may or may not have an array for values. | ||
|
|
||
| if (isThreeTupleArray(decoded.args)) { | ||
| const [targets, values, datas] = decoded.args; | ||
|
|
||
| if (targets.length !== values.length || targets.length !== datas.length) { | ||
| throw new Error("Invalid arguments for executeBatch"); | ||
| } | ||
|
|
||
| return targets.map((to, index) => ({ | ||
| to, | ||
| value: values[index], | ||
| data: datas[index], | ||
| })); | ||
| } | ||
|
|
||
| // Two tuple array (no values) | ||
| const [targets, datas] = decoded.args; | ||
|
|
||
| if (targets.length !== datas.length) { | ||
| throw new Error("Invalid arguments for executeBatch"); | ||
| } | ||
|
|
||
| return targets.map((to, index) => ({ | ||
| to, | ||
| data: datas[index], | ||
| })); | ||
adamegyed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Otherwise, treat the call as a single call to the account itself. | ||
| return [ | ||
| { | ||
| to: accountAddress, | ||
| data, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| // Needed to narrow types for LAv1 `executeBatch` function. | ||
| function isThreeTupleArray( | ||
| input: | ||
| | readonly [readonly Address[], readonly Hex[]] | ||
| | readonly [readonly Address[], readonly bigint[], readonly Hex[]], | ||
| ): input is readonly [readonly Address[], readonly bigint[], readonly Hex[]] { | ||
| return input.length === 3; | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
oh interesting, never realized we had an overload for no values.
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.
Yep! This is specific to LightAccount and its variants.