Skip to content

Commit

Permalink
Merge branch 'main' into tx-nikola-txfusion-add-token-page
Browse files Browse the repository at this point in the history
  • Loading branch information
kiriyaga-txfusion authored Jan 28, 2025
2 parents 4da0648 + 24aecc3 commit 7359d6f
Show file tree
Hide file tree
Showing 29 changed files with 847 additions and 105 deletions.
26 changes: 15 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/app/src/components/contract/InfoTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<CopyContent :value="contract.address" />
</table-body-column>
</tr>
<tr>
<tr v-if="contract.creatorAddress && contract.creatorTxHash">
<table-body-column class="contract-info-field-label">
{{ t("contract.table.creator") }}
</table-body-column>
Expand Down
34 changes: 16 additions & 18 deletions packages/app/src/composables/useTransactionData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ref } from "vue";

import { AbiCoder, Interface } from "ethers";
import { Interface } from "ethers";

import useAddress from "./useAddress";
import useContext from "./useContext";
Expand All @@ -10,43 +10,41 @@ import type { AbiFragment } from "./useAddress";
import type { InputType } from "./useEventLog";
import type { Address } from "@/types";

const defaultAbiCoder: AbiCoder = AbiCoder.defaultAbiCoder();
import { decodeInputData } from "@/utils/helpers";

export type MethodData = {
name: string;
inputs: InputData[];
};

export type InputData = {
name: string;
type: InputType | string;
value: string;
inputs: InputData[];
encodedValue: string;
};
export type TransactionData = {
calldata: string;
contractAddress: Address | null;
value: string;
sighash: string;
method?: {
name: string;
inputs: {
name: string;
type: InputType;
value: string;
encodedValue: string;
}[];
};
method?: MethodData;
};

export function decodeDataWithABI(
transactionData: { calldata: TransactionData["calldata"]; value: TransactionData["value"] },
abi: AbiFragment[]
): TransactionData["method"] | undefined {
const contractInterface = new Interface(abi);

try {
const decodedData = contractInterface.parseTransaction({
data: transactionData.calldata,
value: transactionData.value,
})!;
return {
name: decodedData.name,
inputs: decodedData.fragment.inputs.map((input) => ({
name: input.name,
type: input.type as InputType,
value: decodedData.args[input.name]?.toString(),
encodedValue: defaultAbiCoder.encode([input.type], [decodedData.args[input.name]]).split("0x")[1],
})),
inputs: decodedData.fragment.inputs.flatMap((input) => decodeInputData(input, decodedData.args[input.name])),
};
} catch {
return undefined;
Expand Down
56 changes: 55 additions & 1 deletion packages/app/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { format } from "date-fns";
import { Interface } from "ethers";
import { AbiCoder, Interface } from "ethers";
import { utils } from "zksync-ethers";

import { DEPLOYER_CONTRACT_ADDRESS } from "./constants";
Expand All @@ -8,9 +8,13 @@ import type { DecodingType } from "@/components/transactions/infoTable/HashViewe
import type { AbiFragment } from "@/composables/useAddress";
import type { InputType, TransactionEvent, TransactionLogEntry } from "@/composables/useEventLog";
import type { TokenTransfer } from "@/composables/useTransaction";
import type { InputData } from "@/composables/useTransactionData";
import type { ParamType, Result } from "ethers";

const { BOOTLOADER_FORMAL_ADDRESS } = utils;

export const DefaultAbiCoder: AbiCoder = AbiCoder.defaultAbiCoder();

export function utcStringFromUnixTimestamp(timestamp: number) {
const isoDate = new Date(+`${timestamp}000`).toISOString();
return format(new Date(isoDate.slice(0, -1)), "yyyy-MM-dd HH:mm 'UTC'");
Expand Down Expand Up @@ -116,6 +120,56 @@ export function decodeLogWithABI(log: TransactionLogEntry, abi: AbiFragment[]):
}
}

export function decodeInputData(input: ParamType, args: Result): InputData[] {
if (input.isArray()) {
return decodeArrayInputData(input, args);
}

if (input.isTuple()) {
return decodeTupleInputData(input, args);
}

return [
{
name: input.name,
type: input.type as InputType,
value: args.toString(),
encodedValue: DefaultAbiCoder.encode([input.type], [args]).split("0x")[1],
inputs: [],
},
];
}

function decodeArrayInputData(input: ParamType, args: Result): InputData[] {
const inputs = args.flatMap((arg) => decodeInputData(input.arrayChildren!, arg));

return [
{
name: input.name,
type: `${inputs[0]?.type ? `${inputs[0]?.type}[]` : input.type}`,
value: `[${inputs.map((input) => input.value).join(",")}]`,
inputs: inputs,
encodedValue: `[${inputs.map((input) => input.encodedValue).join(",")}]`,
},
];
}

function decodeTupleInputData(input: ParamType, args: Result): InputData[] {
const inputs = input.components!.flatMap((component: ParamType, index: number) =>
decodeInputData(component, args[index])
);

return [
{
name: input.name,
type: `tuple(${inputs.map((input) => input.type).join(",")})`,
value: `(${inputs.map((input) => input.value).join(",")})`,
inputs,
encodedValue: `(${inputs.map((input) => input.encodedValue).join(",")})`,
},
];
}

export function sortTokenTransfers(transfers: TokenTransfer[]): TokenTransfer[] {
return [...transfers].sort((_, t) => {
if (t.to === BOOTLOADER_FORMAL_ADDRESS || t.from === BOOTLOADER_FORMAL_ADDRESS) {
Expand Down
4 changes: 4 additions & 0 deletions packages/app/tests/composables/useTransactionData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ const transactionDataDecodedMethod = {
inputs: [
{
name: "recipient",
inputs: [],
type: "address",
value: "0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044",
encodedValue: "000000000000000000000000a1cf087db965ab02fb3cface1f5c63935815f044",
},
{
name: "amount",
inputs: [],
type: "uint256",
value: "1",
encodedValue: "0000000000000000000000000000000000000000000000000000000000000001",
Expand Down Expand Up @@ -175,12 +177,14 @@ describe("useTransactionData:", () => {
inputs: [
{
encodedValue: "000000000000000000000000a1cf087db965ab02fb3cface1f5c63935815f044",
inputs: [],
name: "recipient",
type: "address",
value: "0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044",
},
{
encodedValue: "0000000000000000000000000000000000000000000000000000000000000001",
inputs: [],
name: "amount",
type: "uint256",
value: "1",
Expand Down
Loading

0 comments on commit 7359d6f

Please sign in to comment.