Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zapaz committed Nov 2, 2024
1 parent 98a5bf4 commit 7ab5a9f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 22 deletions.
4 changes: 2 additions & 2 deletions svelte5/src/lib/onchain-ai/components/Explorer.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script lang="ts">
import { createOnchainAI } from "../runes/contract.svelte";
const { tx = "", address = "", requestId = "" } = $props();
const { txHash = "", address = "", requestId = "" } = $props();
const { config } = $derived.by(createOnchainAI);
const etherscanLinkAddress = $derived(address ? `${config.explorer}/address/${address}` : "");
const etherscanLinkTx = $derived(tx ? `${config.explorer}/tx/${tx}` : "");
const etherscanLinkTx = $derived(txHash ? `${config.explorer}/tx/${txHash}` : "");
const chainlinkLink = $derived(
`https://functions.chain.link/${config.chainName}/${config.subscriptionId}` +
Expand Down
13 changes: 9 additions & 4 deletions svelte5/src/lib/onchain-ai/components/Form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
import { InputBase } from "$lib/scaffold-eth/components";
import { createWriteOnchainAI } from "$lib/onchain-ai/runes";
let { tx = $bindable() } = $props();
let { txHash = $bindable() }: { txHash?: `0x${string}` } = $props();
let txReceipt = $state();
let prompt: string = $state("");
const writeContract = $derived(
const { send, wait, lastTxHash } = $derived(
createWriteOnchainAI({ functionName: "sendRequest", args: [prompt], value: 10n ** 14n })
);
const handleSend = async () => {
if (!writeContract) return;
txHash = await send();
if (!txHash) return;
tx = await writeContract.send();
txReceipt = await wait(txHash);
};
$inspect("txHash:", txHash);
$inspect("txReceipt:", txReceipt);
</script>

<div class="flex justify-center">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
import { isAddress, isEns, type CommonInputProps } from "$lib/scaffold-eth/ts";
import { createEnsAddress, createEnsName, createEnsAvatar } from "$lib/wagmi/runes";
import InputBase from "./InputBase.svelte";
import { untrack } from "svelte";
let {
value = $bindable(),
address = $bindable(),
name,
placeholder,
onchange,
disabled
}: CommonInputProps<Address | string> & {
address?: string | null | undefined;
ens?: string | undefined;
} = $props();
Expand All @@ -30,19 +29,20 @@
if (validEnsAddressFromValue) value = ensAddressFromValue!;
});
const handleChange = (newValue: Address | string) => {
onchange?.(newValue);
};
$effect(() => {
if (isAddress(value)) onchange?.(value);
});
const reFocus = $derived(isAddress(value));
$inspect("AddressInput ~ value:", value);
</script>

<InputBase
bind:value={value as Address}
{name}
{placeholder}
error={ensAddress === null}
onchange={handleChange}
{disabled}
{reFocus}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
let form = $state(getInitialFormState(abiFunction));
let txValue = $state<bigint | string>("");
let txResult = $state();
let txHash: `0x${string}` | undefined = $state();
let txReceipt = $state();
const { account } = $derived(createAccount());
const { chain } = $derived(account);
Expand All @@ -40,7 +41,7 @@
const writeDisabled = $derived(!chain || chain?.id !== targetNetwork.id);
let writeTxn = $derived.by(createTransactor());
let { send, waitingTxHash, waitingTxReceipt } = $derived(
let { send, wait, waitingTxHash, waitingTxReceipt } = $derived(
createWriteContract({
address: contractAddress,
abi,
Expand All @@ -52,12 +53,16 @@
let tx = $state();
const handleWrite = async () => {
txResult = await send();
txHash = await send();
if (txHash) txReceipt = await wait(txHash);
};
const transformedFunction = transformAbiFunction(abiFunction);
const zeroInputs =
transformedFunction.inputs.length === 0 && abiFunction.stateMutability !== "payable";
$inspect("txHash:", txHash);
$inspect("txReceipt:", txReceipt);
</script>

<div class="space-y-3 py-5 first:pt-0 last:pb-1">
Expand Down Expand Up @@ -96,7 +101,7 @@
{#if !zeroInputs}
<div class="flex-grow basis-0">
{#if tx}
<DisplayTxResult content={txResult} />
<DisplayTxResult content={txReceipt} />
{/if}
</div>
{/if}
Expand All @@ -110,7 +115,7 @@
disabled={writeDisabled || !send}
onclick={handleWrite}
>
{#if !waitingTxReceipt}
{#if waitingTxReceipt}
<span class="loading loading-spinner loading-xs"></span>
{/if}
Send 💸
Expand All @@ -120,7 +125,7 @@
</div>
{#if zeroInputs && tx}
<div class="flex-grow basis-0">
<DisplayTxResult content={txResult} />
<DisplayTxResult content={txReceipt} />
</div>
{/if}
</div>
5 changes: 4 additions & 1 deletion svelte5/src/lib/wagmi/runes/write.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ const createWriteContract = ({
if (!hash) {
throw Error("writeContract error: no hash");
}

return hash;
};
const wait = async (hash: `0x${string}`) => {
waitingTxReceipt = true;
let receipt = await waitForTransactionReceipt(config, { hash });

Expand All @@ -60,6 +62,7 @@ const createWriteContract = ({

return {
send,
wait,
get lastTxHash() {
return lastTxHash;
},
Expand Down
6 changes: 3 additions & 3 deletions svelte5/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { createOnchainAI } from "$lib/onchain-ai/runes/contract.svelte";
const { address } = $derived.by(createOnchainAI);
let tx: string = $state("");
let txHash: `0x${string}` | undefined = $state();
</script>

<div class="flex flex-col items-center">
Expand All @@ -14,11 +14,11 @@
</h1>

<div class="p-2 w-full max-w-lg">
<Form bind:tx />
<Form bind:txHash />
</div>

<div class="text-center">
<Explorer {tx} {address} />
<Explorer {txHash} {address} />
</div>

<div class="pt-4 w-full max-w-lg">
Expand Down
File renamed without changes.

0 comments on commit 7ab5a9f

Please sign in to comment.