-
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
9 changed files
with
107 additions
and
88 deletions.
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
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
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,59 @@ | ||
import { | ||
Address, | ||
encodeFunctionData, | ||
encodePacked, | ||
Hex, | ||
parseAbi, | ||
size, | ||
} from "viem"; | ||
import { MetaTransaction, OperationType } from "../types"; | ||
|
||
export const MULTI_SEND_ABI = ["function multiSend(bytes memory transactions)"]; | ||
|
||
const MULTISEND_141 = "0x38869bf66a61cF6bDB996A6aE40D5853Fd43B526"; | ||
const MULTISEND_CALLONLY_141 = "0x9641d764fc13c8B624c04430C7356C1C7C8102e2"; | ||
|
||
/// Encodes the transaction as packed bytes of: | ||
/// - `operation` as a `uint8` with `0` for a `call` or `1` for a `delegatecall` (=> 1 byte), | ||
/// - `to` as an `address` (=> 20 bytes), | ||
/// - `value` as a `uint256` (=> 32 bytes), | ||
/// - length of `data` as a `uint256` (=> 32 bytes), | ||
/// - `data` as `bytes`. | ||
const encodeMetaTx = (tx: MetaTransaction): Hex => | ||
encodePacked( | ||
["uint8", "address", "uint256", "uint256", "bytes"], | ||
[ | ||
tx.operation || OperationType.Call, | ||
tx.to as Address, | ||
BigInt(tx.value), | ||
BigInt(size(tx.data as Hex)), | ||
tx.data as Hex, | ||
] | ||
); | ||
|
||
const remove0x = (hexString: Hex) => hexString.slice(2); | ||
|
||
// Encodes a batch of module transactions into a single multiSend module transaction. | ||
// A module transaction is an object with fields corresponding to a Gnosis Safe's (i.e., Zodiac IAvatar's) `execTransactionFromModule` method parameters. | ||
export function encodeMulti( | ||
transactions: readonly MetaTransaction[], | ||
multiSendContractAddress: string = transactions.some( | ||
(t) => t.operation === OperationType.DelegateCall | ||
) | ||
? MULTISEND_141 | ||
: MULTISEND_CALLONLY_141 | ||
): MetaTransaction { | ||
const encodedTransactions = | ||
"0x" + transactions.map(encodeMetaTx).map(remove0x).join(""); | ||
|
||
return { | ||
operation: OperationType.DelegateCall, | ||
to: multiSendContractAddress, | ||
value: "0x00", | ||
data: encodeFunctionData({ | ||
abi: parseAbi(MULTI_SEND_ABI), | ||
functionName: "multiSend", | ||
args: [encodedTransactions], | ||
}), | ||
}; | ||
} |
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
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
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
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
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,22 @@ | ||
import { encodeMulti } from "../src/lib/multisend"; | ||
|
||
describe("Multisend", () => { | ||
it("encodeMulti", () => { | ||
const tx1 = { | ||
to: "0x0000000000000000000000000000000000000001", | ||
value: "1", | ||
data: "0x12", | ||
}; | ||
const tx2 = { | ||
to: "0x0000000000000000000000000000000000000002", | ||
value: "2", | ||
data: "0x34", | ||
}; | ||
expect(encodeMulti([tx1, tx2])).toEqual({ | ||
data: "0x8d80ff0a000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000ac000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000011200000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001340000000000000000000000000000000000000000", | ||
operation: 1, | ||
to: "0x9641d764fc13c8B624c04430C7356C1C7C8102e2", | ||
value: "0x00", | ||
}); | ||
}); | ||
}); |
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