Skip to content

Commit

Permalink
fix: move openchain api to the seperate hook
Browse files Browse the repository at this point in the history
  • Loading branch information
kiriyaga committed Dec 12, 2024
1 parent c0e0021 commit edcabe0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 46 deletions.
47 changes: 1 addition & 46 deletions packages/app/src/components/transactions/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ import TransactionDirectionTableCell from "@/components/transactions/Transaction
import TransactionNetworkSquareBlock from "@/components/transactions/TransactionNetworkSquareBlock.vue";
import useContext from "@/composables/useContext";
import { fetchMethodNames } from "@/composables/useOpenChain";
import useToken, { type Token } from "@/composables/useToken";
import { decodeDataWithABI } from "@/composables/useTransactionData";
import useTransactions, { type TransactionListItem, type TransactionSearchParams } from "@/composables/useTransactions";
Expand Down Expand Up @@ -278,52 +279,6 @@ watch(
{ immediate: true }
);
interface OpenChainMethod {
name: string;
filtered: boolean;
}
interface OpenChainResponse {
ok: boolean;
result: {
function: Record<string, OpenChainMethod[]>;
};
}
const fetchMethodNames = async (sighashes: string[]): Promise<Record<string, string>> => {
try {
const response = await $fetch<OpenChainResponse>("https://api.openchain.xyz/signature-database/v1/lookup", {
method: "GET",
params: {
function: sighashes.join(","),
filter: true,
},
headers: {
accept: "application/json",
},
});
const result = response?.result?.function ?? {};
const methodNames: Record<string, string> = {};
Object.entries(result).forEach(([sighash, methods]) => {
// Ensure methods is an array of the expected shape
if (Array.isArray(methods)) {
methods.forEach((method) => {
if (typeof method === "object" && method.name && method.name.split("(").length > 1) {
methodNames[sighash] = method.name.split("(")[0];
}
});
}
});
return methodNames;
} catch (error) {
console.error("Error fetching method names:", error);
return {};
}
};
const methodNames = ref<Record<string, string>>({});
const loadMethodNames = async () => {
Expand Down
42 changes: 42 additions & 0 deletions packages/app/src/composables/useOpenChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { $fetch } from "ohmyfetch";

interface OpenChainMethod {
name: string;
filtered: boolean;
}
interface OpenChainResponse {
ok: boolean;
result: {
function: Record<string, OpenChainMethod[]>;
};
}
export async function fetchMethodNames(sighashes: string[]): Promise<Record<string, string>> {
try {
const response = await $fetch<OpenChainResponse>("https://api.openchain.xyz/signature-database/v1/lookup", {
method: "GET",
params: {
function: sighashes.join(","),
filter: true,
},
headers: {
accept: "application/json",
},
});
const result = response?.result?.function ?? {};
const methodNames: Record<string, string> = {};
Object.entries(result).forEach(([sighash, methods]) => {
// Ensure methods is an array of the expected shape
if (Array.isArray(methods)) {
methods.forEach((method) => {
if (typeof method === "object" && method.name && method.name.split("(").length > 1) {
methodNames[sighash] = method.name.split("(")[0];
}
});
}
});
return methodNames;
} catch (error) {
console.error("Error fetching method names:", error);
return {};
}
}

0 comments on commit edcabe0

Please sign in to comment.