Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasiarods committed Oct 31, 2023
1 parent e9fbaab commit e6f2449
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 43 deletions.
19 changes: 8 additions & 11 deletions apps/web/src/app/contract/[contract]/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,24 @@ import {
} from "@/components/ui/table";
import React from "react";
import { DecodedTx, Interpreter } from "@3loop/transaction-decoder";
import {
findAndRunInterpreter,
defaultInterpretors,
} from "@/lib/interpreter";
import { findAndRunInterpreter, defaultinterpreters } from "@/lib/interpreter";

function getAvaliableInterpretors() {
function getAvaliableinterpreters() {
if (typeof window === "undefined") return undefined;

let res: Interpreter[] = [];

for (const interpretor of defaultInterpretors) {
const stored = window.localStorage.getItem(interpretor.id);
for (const interpreter of defaultinterpreters) {
const stored = window.localStorage.getItem(interpreter.id);
if (stored) {
const updatedSchema = JSON.parse(stored);

res.push({
...interpretor,
...interpreter,
schema: updatedSchema,
});
} else {
res.push(interpretor);
res.push(interpreter);
}
}

Expand All @@ -44,7 +41,7 @@ export default function TxTable({ txs }: { txs: DecodedTx[] }) {
interpretation: any;
}[]
>([]);
const [intepretors] = React.useState(getAvaliableInterpretors);
const [intepretors] = React.useState(getAvaliableinterpreters);

React.useEffect(() => {
async function run() {
Expand All @@ -53,7 +50,7 @@ export default function TxTable({ txs }: { txs: DecodedTx[] }) {
const withIntepretations = await Promise.all(
txs.map((tx) => {
return findAndRunInterpreter(tx, intepretors);
})
}),
);

setResult(withIntepretations);
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/app/tx/[hash]/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { useRouter } from "next/navigation";
import { DecodedTx, Interpreter } from "@3loop/transaction-decoder";
import { interpretTx} from "@/lib/interpreter";
import { interpretTx } from "@/lib/interpreter";

export const sidebarNavItems = transactions.map((tx) => {
return {
Expand All @@ -38,7 +38,7 @@ export default function DecodingForm({
const [result, setResult] = React.useState<string>();
const [schema, setSchema] = useLocalStorage(
defaultInterpreter?.id ?? "unknown",
defaultInterpreter?.schema
defaultInterpreter?.schema,
);

const router = useRouter();
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/app/tx/[hash]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react";
import DecodingForm from "./form";
import { decodeTransaction } from "@/lib/decode";
import { defaultInterpretors, emptyInterpretor } from "@/lib/interpreter";
import { defaultinterpreters, emptyinterpreter } from "@/lib/interpreter";
import { findInterpreter } from "@3loop/transaction-decoder";

export default async function TransactionPage({
Expand All @@ -20,14 +20,14 @@ export default async function TransactionPage({

const intepretor = await findInterpreter({
decodedTx: decoded,
interpretors: defaultInterpretors,
interpreters: defaultinterpreters,
});

if (!intepretor) {
return (
<DecodingForm
decoded={decoded}
defaultInterpreter={emptyInterpretor}
defaultInterpreter={emptyinterpreter}
currentHash={params.hash}
/>
);
Expand Down
10 changes: 5 additions & 5 deletions apps/web/src/lib/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
runInterpreter,
} from "@3loop/transaction-decoder";

export const emptyInterpretor: Interpreter = {
export const emptyinterpreter: Interpreter = {
id: "default",
contractAddress: "",
schema: "",
filter: "txHash ? true : false",
chainID: 1,
};

export const defaultInterpretors: Interpreter[] = [
export const defaultinterpreters: Interpreter[] = [
{
id: "aave-repay",
contractAddress: "0x7d2768de32b0b80b7a3454c06bdac94a69ddc7a9",
Expand Down Expand Up @@ -78,7 +78,7 @@ export const defaultInterpretors: Interpreter[] = [

export async function interpretTx(
decodedTx: DecodedTx,
interpreter: Interpreter
interpreter: Interpreter,
) {
try {
const res = await runInterpreter({ decodedTx, interpreter });
Expand All @@ -91,9 +91,9 @@ export async function interpretTx(

export async function findAndRunInterpreter(
decodedTx: DecodedTx,
interpretors: Interpreter[]
interpreters: Interpreter[],
) {
const interpreter = await findInterpreter({ decodedTx, interpretors });
const interpreter = await findInterpreter({ decodedTx, interpreters });

if (!interpreter) {
return {
Expand Down
46 changes: 25 additions & 21 deletions packages/transaction-decoder/src/interpreters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,47 @@ import { sameAddress } from '../helpers/address.js'
import type { DecodedTx, Interpreter } from '../types.js'
import jsonata from 'jsonata'

function findInterpretorsForContract(
function findinterpretersForContract(
contractAddress: string,
chainID: number,
interpretors: Interpreter[],
interpreters: Interpreter[],
): Interpreter[] {
return interpretors.filter(
(interpretor) => sameAddress(interpretor.contractAddress, contractAddress) && interpretor.chainID === chainID,
return interpreters.filter(
(interpreter) => sameAddress(interpreter.contractAddress, contractAddress) && interpreter.chainID === chainID,
)
}

export async function findInterpreter({
decodedTx,
interpretors,
interpreters,
}: {
decodedTx: DecodedTx
interpretors: Interpreter[]
interpreters: Interpreter[]
}): Promise<Interpreter | undefined> {
const contractAddress = decodedTx.toAddress
const chainID = decodedTx.chainID
if (!contractAddress) {
return undefined
}
try {
const contractAddress = decodedTx.toAddress
const chainID = decodedTx.chainID
if (!contractAddress) {
return undefined
}

const contractInterpreters = findInterpretorsForContract(contractAddress, chainID, interpretors)
const contractInterpreters = findinterpretersForContract(contractAddress, chainID, interpreters)

if (!contractInterpreters) {
return undefined
}
if (!contractInterpreters) {
return undefined
}

for (const interpreter of contractInterpreters) {
const canInterpret = jsonata(interpreter.filter)
const canInterpretResult = await canInterpret.evaluate(decodedTx)
for (const interpreter of contractInterpreters) {
const canInterpret = jsonata(interpreter.filter)
const canInterpretResult = await canInterpret.evaluate(decodedTx)

if (!canInterpretResult) {
continue
if (!canInterpretResult) {
continue
}
return interpreter
}
return interpreter
} catch (e) {
throw new Error(`Failed to find interpreter: ${e}`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/transaction-decoder/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.build.json",
"include": ["src/**/*", "test/**/*", "examples/**/*"]
"include": ["src/**/*", "test/**/*", "examples/**/*", "tsup.config.ts"]
}

0 comments on commit e6f2449

Please sign in to comment.