Skip to content

Commit

Permalink
Merge pull request #43 from dwhiffing/error-transaction-link
Browse files Browse the repository at this point in the history
Shows link to transaction when it fails and support different blockchain explorers based on chainId
  • Loading branch information
dwhiffing authored Sep 5, 2024
2 parents 066b359 + 4d29622 commit b167603
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 29 deletions.
54 changes: 34 additions & 20 deletions components/ChatMessageBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Badge } from "./ui/badge";
import { ToolArgsTable } from "./ToolArgsTable";
import { useContracts } from "@/utils/useContracts";
import { CHAINS } from "@/constants";
import { IContract } from "@/types";

type IToolCall = {
name: string;
Expand Down Expand Up @@ -61,8 +62,10 @@ type IToolCallResponse = {

function ToolCallSuccessBadge({
toolCallResponse,
contract,
}: {
toolCallResponse: IToolCallResponse | null;
contract: IContract | null;
}) {
// Hack to just get around ts for now.
if (!toolCallResponse)
Expand All @@ -76,33 +79,30 @@ function ToolCallSuccessBadge({
return (
<div>
<Badge className="bg-rose-500">Error</Badge>
<br />
<span className="mt-2 text-xs opacity-70 break-all">
{toolCallResponse.payload.transactionHash && contract && (
<TransactionLink
hash={toolCallResponse.payload.transactionHash}
contract={contract}
/>
)}
<div className="mt-2 text-xs opacity-70 break-all">
{toolCallResponse.message}
</span>
<br />
<span className="mt-2 text-xs opacity-70 break-all">
</div>
<div className="mt-2 text-xs opacity-70 break-all">
{JSON.stringify(toolCallResponse.payload)}
</span>
</div>
</div>
);
}
return (
<div>
<Badge className="bg-emerald-500">Success</Badge>
<span className="mt-2 text-xs opacity-70 break-all">
{toolCallResponse.payload.transactionHash && (
<div className="mt-2">
<Link
target="_blank"
href={`https://sepolia.etherscan.io/tx/${toolCallResponse.payload.transactionHash}`}
className="underline"
>
View your Transaction
</Link>
</div>
)}
</span>
{toolCallResponse.payload.transactionHash && contract && (
<TransactionLink
hash={toolCallResponse.payload.transactionHash}
contract={contract}
/>
)}
</div>
);
}
Expand Down Expand Up @@ -202,7 +202,10 @@ export function ToolCallMessageBubble(props: { message: Message }) {
)}
</Button>
) : (
<ToolCallSuccessBadge toolCallResponse={toolCallResponse} />
<ToolCallSuccessBadge
toolCallResponse={toolCallResponse}
contract={contract}
/>
)}
</div>
</>
Expand Down Expand Up @@ -236,3 +239,14 @@ export function ChatMessageBubble(props: { message: Message }) {
return <ToolCallMessageBubble {...props} />;
}
}

const TransactionLink = (props: { hash: string; contract: IContract }) => {
const uri = CHAINS[props.contract.chainId].explorerURI;
return (
<div className="mt-2 text-xs opacity-70 break-all">
<Link target="_blank" href={`${uri}/${props.hash}`} className="underline">
View your Transaction
</Link>
</div>
);
};
17 changes: 13 additions & 4 deletions constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { IContract } from "@/types";

export const CHAINS = {
11155111: { name: "ETH Sepolia" },
1: { name: "ETH Mainnet" },
137: { name: "POLY Mainnet" },
80001: { name: "POLY Mubai" },
11155111: {
name: "ETH Sepolia",
explorerURI: "https://sepolia.etherscan.io/tx/",
},
1: { name: "ETH Mainnet", explorerURI: "https://etherscan.io/tx/" },
137: {
name: "POLY Mainnet",
explorerURI: "https://polygonscan.com/tx/",
},
80002: {
name: "POLY Amoy",
explorerURI: "https://amoy.polygonscan.com/tx/",
},
};

export const FEATURED_CONTRACTS: IContract[] = [
Expand Down
2 changes: 1 addition & 1 deletion utils/abi/polygonscan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export const getABIFromPolygonscan = async (

export const polygonscanChains: Record<number, string> = {
137: "mainnet",
80001: "mumbai",
80002: "amoy",
};
5 changes: 3 additions & 2 deletions utils/generateToolFromABI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ export const generateToolFromABI =
)
) {
// Just to get around TS
if (error instanceof Error) {
if (error instanceof TransactionError) {
console.error(`${error.constructor.name}:`, error.message);
const transactionHash = error.context.hash;
return JSON.stringify({
message: error.message,
status: "failure",
payload: {},
payload: transactionHash ? { transactionHash } : {},
});
}
}
Expand Down
5 changes: 3 additions & 2 deletions utils/tee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ export async function getTransactionReceipt({

console.log({ signedTx });

let tx: ethers.ethers.TransactionResponse | null = null;
try {
const tx = await provider.broadcastTransaction(signedTx);
tx = await provider.broadcastTransaction(signedTx);
const txReceipt = await tx.wait();
const transactionHash = txReceipt?.hash ?? "";
return {
Expand All @@ -219,7 +220,7 @@ export async function getTransactionReceipt({
} catch (error) {
console.error("Error Broadcasting or waiting for transaction", error);
if (error instanceof Error) {
throw new TransactionError(error.message);
throw new TransactionError(error.message, tx);
}
throw error;
}
Expand Down

0 comments on commit b167603

Please sign in to comment.