From d8e2a80db80a6dffe8a010cb61e5eaa65e3a3873 Mon Sep 17 00:00:00 2001
From: "yongen.loong"
Date: Thu, 8 Aug 2024 18:50:03 +0800
Subject: [PATCH] feat: audit report display
---
components/build-deploy-panel.tsx | 2 +-
components/workspace/use-cli-commands.tsx | 83 +++++++++++++++++++++--
data/audit.ts | 16 +++--
data/wallet.ts | 41 +++++++++++
4 files changed, 128 insertions(+), 14 deletions(-)
diff --git a/components/build-deploy-panel.tsx b/components/build-deploy-panel.tsx
index de95563..e925809 100644
--- a/components/build-deploy-panel.tsx
+++ b/components/build-deploy-panel.tsx
@@ -37,7 +37,7 @@ export function BuildDeployPanel() {
}
}}
>
- Audit
+ AI Audit
- clear - clears the terminal
- - audit - audits the current workspace
+ -
+ audit - audits the current workspace using AI
+
- build - builds the current workspace
- deploy - deploys the built smart contract
-
@@ -58,11 +62,21 @@ export function useCliCommands() {
);
try {
- const res = await uploadContractCode(files);
+ const codeHash = await uploadContractCode(files);
+
+ terminalContext.setBufferedContent(
+ <>
+
Code Hash: {codeHash}
+ >
+ );
+
+ if (!wallet) return;
+
+ const { TransactionId } = await wallet.auditTransfer(codeHash);
terminalContext.setBufferedContent(
<>
- Code Hash: {res}
+
>
);
} catch (err) {
@@ -175,14 +189,18 @@ export function useCliCommands() {
return commands;
}
-function Deploying() {
+function Loading({ children }: PropsWithChildren) {
return (
- Deploying...
+ {children}
);
}
+function Deploying() {
+ return Deploying...;
+}
+
function Deployment({ id }: { id: string }) {
const [shouldPoll, setShouldPoll] = useState(true);
const { data, error } = useTransactionResult(
@@ -247,3 +265,54 @@ function DeployedContractDetails({ id }: { id?: string }) {
return Contract Address: {data.address}
;
}
+
+function AuditReport({
+ codeHash,
+ transactionId,
+}: {
+ codeHash: string;
+ transactionId: string;
+}) {
+ const { isLoading, error } = useAudit(codeHash, transactionId);
+
+ if (isLoading || !!error) return Loading report...;
+
+ return ;
+}
+
+function AuditReportResult({ codeHash }: { codeHash: string }) {
+ const { data, isLoading } = useAuditReport(codeHash);
+
+ if (isLoading || !data) return Loading report...;
+
+ return (
+ <>
+
+
+
+ Item |
+ Suggestion |
+
+
+ {Object.entries(data).map(([k, v]) => (
+
+ {k} |
+
+ {v.map((i, index) => (
+
+ Original: {i.Detail.Original}
+ Suggested: {i.Detail.Updated}
+
+ ))}
+ |
+
+ ))}
+
+ >
+ );
+}
\ No newline at end of file
diff --git a/data/audit.ts b/data/audit.ts
index 3ad2e4e..7811c63 100644
--- a/data/audit.ts
+++ b/data/audit.ts
@@ -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 }
+ );
}
diff --git a/data/wallet.ts b/data/wallet.ts
index 3e37698..becd1c6 100644
--- a/data/wallet.ts
+++ b/data/wallet.ts
@@ -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
+ );
+ }
}
\ No newline at end of file