Skip to content

Commit

Permalink
feat: audit report display
Browse files Browse the repository at this point in the history
  • Loading branch information
yongenaelf committed Aug 8, 2024
1 parent 2c95cc7 commit d8e2a80
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 14 deletions.
2 changes: 1 addition & 1 deletion components/build-deploy-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function BuildDeployPanel() {
}
}}
>
Audit
AI Audit
</Button>
<Button
disabled={isBuilding}
Expand Down
83 changes: 76 additions & 7 deletions components/workspace/use-cli-commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { db } from "@/data/db";
import { useWallet } from "@/data/wallet";
import { Loader2 } from "lucide-react";
import { usePathname } from "next/navigation";
import { useContext, useEffect, useState } from "react";
import { PropsWithChildren, useContext, useEffect, useState } from "react";
import { TerminalContext } from "react-terminal";
import { useWorkspaceId } from "./use-workspace-id";
import { uploadContractCode } from "@/data/audit";
import { uploadContractCode, useAudit, useAuditReport } from "@/data/audit";
import Link from "next/link";
import clsx from "clsx";

export function useCliCommands() {
const terminalContext = useContext(TerminalContext);
Expand All @@ -22,7 +24,9 @@ export function useCliCommands() {
<p>These are the available commands:</p>
<ol>
<li className="ml-8">clear - clears the terminal</li>
<li className="ml-8">audit - audits the current workspace</li>
<li className="ml-8">
audit - audits the current workspace using AI
</li>
<li className="ml-8">build - builds the current workspace</li>
<li className="ml-8">deploy - deploys the built smart contract</li>
<li className="ml-8">
Expand Down Expand Up @@ -58,11 +62,21 @@ export function useCliCommands() {
);

try {
const res = await uploadContractCode(files);
const codeHash = await uploadContractCode(files);

terminalContext.setBufferedContent(
<>
<p>Code Hash: {codeHash}</p>
</>
);

if (!wallet) return;

const { TransactionId } = await wallet.auditTransfer(codeHash);

terminalContext.setBufferedContent(
<>
<p>Code Hash: {res}</p>
<AuditReport codeHash={codeHash} transactionId={TransactionId} />
</>
);
} catch (err) {
Expand Down Expand Up @@ -175,14 +189,18 @@ export function useCliCommands() {
return commands;
}

function Deploying() {
function Loading({ children }: PropsWithChildren) {
return (
<p>
<Loader2 className="h-4 w-4 animate-spin inline" /> Deploying...
<Loader2 className="h-4 w-4 animate-spin inline" /> {children}
</p>
);
}

function Deploying() {
return <Loading>Deploying...</Loading>;
}

function Deployment({ id }: { id: string }) {
const [shouldPoll, setShouldPoll] = useState(true);
const { data, error } = useTransactionResult(
Expand Down Expand Up @@ -247,3 +265,54 @@ function DeployedContractDetails({ id }: { id?: string }) {

return <p>Contract Address: {data.address}</p>;
}

function AuditReport({
codeHash,
transactionId,
}: {
codeHash: string;
transactionId: string;
}) {
const { isLoading, error } = useAudit(codeHash, transactionId);

if (isLoading || !!error) return <Loading>Loading report...</Loading>;

return <AuditReportResult codeHash={codeHash} />;
}

function AuditReportResult({ codeHash }: { codeHash: string }) {
const { data, isLoading } = useAuditReport(codeHash);

if (isLoading || !data) return <Loading>Loading report...</Loading>;

return (
<>
<table>
<thead>
<tr>
<th className="p-2">Item</th>
<th className="p-2">Suggestion</th>
</tr>
</thead>
{Object.entries(data).map(([k, v]) => (
<tr key={k} className="border border-black">
<td className="p-2">{k}</td>
<td className="p-2">
{v.map((i, index) => (
<div
key={index}
className={clsx("border border-black p-2", {
"border-t-0": index !== 0,
})}
>
<p>Original: {i.Detail.Original}</p>
<p>Suggested: {i.Detail.Updated}</p>
</div>
))}
</td>
</tr>
))}
</table>
</>
);
}
16 changes: 10 additions & 6 deletions data/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ const auditReportSchema = z.record(
);

export function useAuditReport(auditId?: string) {
return useSWR(auditId ? `audit-report-${auditId}` : undefined, async () => {
const res = await fetch(`/api/playground/report/${auditId}`);
return useSWR(
auditId ? `audit-report-${auditId}` : undefined,
async () => {
const res = await fetch(`/api/playground/report/${auditId}`);

const data = await res.json();
const data = await res.json();

const report = auditReportSchema.parse(data);
const report = auditReportSchema.parse(data);

return report;
});
return report;
},
{ errorRetryInterval: 10 }
);
}
41 changes: 41 additions & 0 deletions data/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,45 @@ class Wallet {
code,
});
}

private async getContractAddressByName(name: string) {
const genesisContract = await this.getGenesisContract();

return await genesisContract.GetContractAddressByName.call(
AElf.utils.sha256(name)
);
}

private async getTokenContractAddress() {
return await this.getContractAddressByName("AElf.ContractNames.Token");
}

private async getTokenContract() {
const address = await this.getTokenContractAddress();

return this.getContract(address);
}

async transfer(
to: string,
amount: number,
memo: string
): Promise<{ TransactionId: string }> {
const tokenContract = await this.getTokenContract();

return await tokenContract.Transfer({
symbol: "ELF",
to,
amount: String(amount),
memo,
});
}

async auditTransfer(codeHash: string) {
return await this.transfer(
`ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx`,
1,
codeHash
);
}
}

0 comments on commit d8e2a80

Please sign in to comment.