Skip to content

Commit

Permalink
proposal id
Browse files Browse the repository at this point in the history
  • Loading branch information
yongenaelf committed Jul 24, 2024
1 parent a4660f7 commit fa4eb26
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
25 changes: 23 additions & 2 deletions components/workspace/cli.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import { build } from "@/data/build";
import { db } from "@/data/db";
import { useDeploy, useTransactionResult } from "@/data/wallet";
import { useDeploy, useLogs, useTransactionResult } from "@/data/wallet";
import { useTheme } from "next-themes";
import { useParams } from "next/navigation";
import { ReactTerminal } from "react-terminal";
import { z } from "zod";

export default function Cli() {
const { id } = useParams();
const deploy = useDeploy();
const getResult = useTransactionResult();
const getLogs = useLogs();
const commands = {
help: () => (
<div>
Expand Down Expand Up @@ -60,7 +62,26 @@ export default function Cli() {
if (!id) return `Please enter the Transaction ID.`;
try {
const result = await getResult(id);
return `TransactionId: ${id}, Status: ${result.Status}`;
const logs = await getLogs(id);
const { data } = z.object({ proposalId: z.string() }).safeParse(logs);
return (
<>
<table className="mt-4">
<tr>
<td className="pr-4">TransactionId: </td>
<td>{id}</td>
</tr>
<tr>
<td>Status: </td>
<td>{result.Status}</td>
</tr>
<tr>
<td>ProposalId: </td>
<td>{data?.proposalId}</td>
</tr>
</table>
</>
);
} catch (err) {
return JSON.stringify(err, undefined, 2);
}
Expand Down
35 changes: 35 additions & 0 deletions data/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { db } from "./db";
// @ts-ignore
import AElf from "aelf-sdk";
import BigNumber from "bignumber.js";
const { deserializeLog } = AElf.pbUtils;

const aelf = new AElf(
new AElf.providers.HttpProvider("https://tdvw-test-node.aelf.io")
Expand Down Expand Up @@ -128,3 +129,37 @@ export function useTransactionResult() {
return await aelf.chain.getTxResult(id);
};
}

let cached: Record<string, any> = {};

async function getProto(address: string) {
const key = aelf.currentProvider.host + "_" + address;
if (!cached[key])
cached[key] = await aelf.chain.getContractFileDescriptorSet(address);
return AElf.pbjs.Root.fromDescriptor(cached[key]);
}

export function useLogs() {
return async (txId: string) => {
try {
const txResult = await aelf.chain.getTxResult(txId);

const deserializeLogs = async () => {
const services = await Promise.all(
txResult.Logs.map(
async ({ Address }: { Address: string }) => await getProto(Address)
)
);

const deserializedLogs: Array<{ proposalId: string }> =
await deserializeLog(txResult.Logs, services);

return deserializedLogs.reduce((acc, cur) => ({ ...acc, ...cur }), {});
};

return await deserializeLogs();
} catch (err: unknown) {
return err;
}
};
}

0 comments on commit fa4eb26

Please sign in to comment.