Skip to content

Commit

Permalink
explicit cjs import
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith committed Oct 28, 2024
1 parent 4783ff8 commit 8f54f08
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/decode/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decodeMulti, MetaTransaction } from "ethers-multisend";
import { decodeMulti } from "ethers-multisend/build/cjs/decodeMulti";
import { EIP712TypedData } from "near-ca";
import {
decodeFunctionData,
Expand Down Expand Up @@ -74,7 +74,7 @@ export function decodeUserOperation(

// Determine if singular or double!
const transactions = isMultisendTx(args)
? decodeMulti(args[2] as string)
? decodeMulti(args[2] as Hex)
: [
{
to: args[0],
Expand All @@ -90,3 +90,14 @@ export function decodeUserOperation(
transactions,
};
}

export declare enum OperationType {
Call = 0,
DelegateCall = 1,
}
export interface MetaTransaction {
readonly to: string;
readonly value: string;
readonly data: string;
readonly operation?: OperationType;
}
65 changes: 65 additions & 0 deletions src/lib/multisend.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {
Address,
decodeFunctionData,
encodeFunctionData,
encodePacked,
getAddress,
Hex,
parseAbi,
size,
toHex,
} from "viem";

import { MetaTransaction, OperationType } from "../types";
Expand Down Expand Up @@ -66,3 +69,65 @@ export function isMultisendTx(args: readonly unknown[]): boolean {
to === MULTISEND_CALLONLY_141.toLowerCase()
);
}

// import { Interface } from '@ethersproject/abi';
// import { getAddress } from '@ethersproject/address';
// import { BigNumber } from '@ethersproject/bignumber';
function unpack(
packed: Hex,
startIndex: number
): {
operation: number;
to: string;
value: string;
data: string;
endIndex: number;
} {
// read operation from first 8 bits (= 2 hex digits)
const operation = parseInt(packed.substring(startIndex, startIndex + 2), 16);
// the next 40 characters are the to address
const to = getAddress(
`0x${packed.substring(startIndex + 2, startIndex + 42)}`
);
// then comes the uint256 value (= 64 hex digits)
const value = toHex(
BigInt(`0x${packed.substring(startIndex + 42, startIndex + 106)}`)
);
// and the uint256 data length (= 64 hex digits)
const hexDataLength = parseInt(
packed.substring(startIndex + 106, startIndex + 170),
16
);
const endIndex = startIndex + 170 + hexDataLength * 2; // * 2 because each hex item is represented with 2 digits
const data = `0x${packed.substring(startIndex + 170, endIndex)}`;
return {
operation,
to,
value,
data,
endIndex,
};
}
export function decodeMultiViem(data: Hex): MetaTransaction[] {
// const multiSendContract = new Interface(MULTI_SEND_ABI);
// const tx = multiSendContract.parseTransaction({ data });
// const multiSendAbiItem = parseAbiItem({
// type: "function",
// name: "multiSend",
// inputs: [{ name: "data", type: "bytes" }],
// });

const tx = decodeFunctionData({
abi: MULTI_SEND_ABI,
data,
});
const [transactionsEncoded] = tx.args as [Hex];
const result = [];
let startIndex = 2; // skip over 0x
while (startIndex < transactionsEncoded.length) {
const { endIndex, ...tx } = unpack(transactionsEncoded, startIndex);
result.push(tx);
startIndex = endIndex;
}
return result;
}

0 comments on commit 8f54f08

Please sign in to comment.