diff --git a/examples/api/src/app/api/erc-20/buy/route.ts b/examples/api/src/app/api/erc-20/buy/route.ts index e6448db..62bb48f 100644 --- a/examples/api/src/app/api/erc-20/buy/route.ts +++ b/examples/api/src/app/api/erc-20/buy/route.ts @@ -1,7 +1,6 @@ import { NextRequest, NextResponse } from "next/server"; -import { createPublicClient, http, parseEther } from "viem2"; -import { chainByName, parseTokenParam } from "../lib/utils"; -import { getEthUsdPrice } from "../lib/utils"; +import { parseEther } from "viem2"; +import { chainByName, getEthUsdPrice, parseTokenParam } from "../lib/utils"; export async function POST(request: NextRequest) { // TODO: Expose separate execution/receiver addresses @@ -31,15 +30,9 @@ export async function POST(request: NextRequest) { }); } - // Get eth balance on blockchain const chain = chainByName[blockchain]; - const client = createPublicClient({ - transport: http(), - chain, - }); const ethPriceUsd = await getEthUsdPrice(); - const ethInputAmount = parseEther((buyAmountUsd / ethPriceUsd).toString()); const swapCalldataParams: { @@ -82,5 +75,6 @@ export async function POST(request: NextRequest) { return NextResponse.json({ transaction: swapCalldataJson.tx, + explorer: chain.blockExplorers.default, }); } diff --git a/examples/api/src/app/api/erc-20/route.ts b/examples/api/src/app/api/erc-20/route.ts index 84f0807..67e6996 100644 --- a/examples/api/src/app/api/erc-20/route.ts +++ b/examples/api/src/app/api/erc-20/route.ts @@ -30,7 +30,8 @@ export async function GET(request: NextRequest) { fn: getFollowingHolderInfo, options: { headers: new Headers({ - "Cache-Control": "public, max-age=3600, immutable", + // Cache for 1 day + "Cache-Control": "public, max-age=86400, immutable", }), }, }, @@ -38,6 +39,7 @@ export async function GET(request: NextRequest) { fn: getPriceData, options: { headers: new Headers({ + // Cache for 1 hour "Cache-Control": "public, max-age=3600, immutable", }), }, @@ -46,7 +48,8 @@ export async function GET(request: NextRequest) { fn: getTokenInfo, options: { headers: new Headers({ - "Cache-Control": "public, max-age=3600, immutable", + // Cache for 1 month + "Cache-Control": "public, max-age=2592000, immutable", }), }, }, diff --git a/mods/erc-20/src/buying.ts b/mods/erc-20/src/buying.ts index d1f9093..053695a 100644 --- a/mods/erc-20/src/buying.ts +++ b/mods/erc-20/src/buying.ts @@ -4,25 +4,10 @@ const buy: ModElement[] = [ { type: "padding", elements: [ + // If no buy amount, go back to #view { type: "horizontal-layout", elements: [ - { - type: "circular-progress", - }, - { - type: "text", - label: - "Buying ~${{refs.buyAmountUsd}} of {{refs.tokenReq.response.data.name}}...", - variant: "secondary", - }, - ], - }, - - { - type: "horizontal-layout", - elements: [ - // If there is no buyAmountUsd value, go to #view { if: { value: "{{refs.buyAmountUsd}}", @@ -31,12 +16,40 @@ const buy: ModElement[] = [ }, }, then: { - type: "horizontal-layout", + type: "vertical-layout", onload: "#view", }, }, ], }, + // + { + if: [ + { + value: "{{refs.swapTx.isSuccess}}", + match: { + NOT: { + equals: "true", + }, + }, + }, + ], + then: { + type: "horizontal-layout", + elements: [ + { + type: "circular-progress", + }, + { + type: "text", + label: + "Buying ~${{refs.buyAmountUsd}} of {{refs.tokenReq.response.data.name}}...", + variant: "secondary", + }, + ], + }, + }, + { type: "vertical-layout", onload: { @@ -45,7 +58,7 @@ const buy: ModElement[] = [ ref: "swapTxDataReq", onsuccess: { type: "SENDETHTRANSACTION", - ref: "swapTxReq", + ref: "swapTx", txData: { from: "{{refs.swapTxDataReq.response.data.transaction.from}}", to: "{{refs.swapTxDataReq.response.data.transaction.to}}", @@ -55,14 +68,86 @@ const buy: ModElement[] = [ chainId: "{{refs.balancesReq.response.data.chain.id}}", onerror: { type: "SETSTATE", - ref: "buyAmountUsd", - value: "", + state: { + buyAmountUsd: "", + }, }, }, onerror: { type: "SETSTATE", - ref: "buyAmountUsd", - value: "", + state: { + buyAmountUsd: "", + }, + }, + }, + }, + { + if: { + value: "{{refs.swapTx.hash}}", + match: { + NOT: { + equals: "", + }, + }, + }, + then: { + if: { + value: "{{refs.swapTx.isSuccess}}", + match: { + equals: "true", + }, + }, + then: { + type: "horizontal-layout", + elements: [ + { + type: "button", + label: "Back", + variant: "secondary", + onclick: { + type: "SETSTATE", + state: { + isBuying: "false", + buyAmountUsd: "", + }, + }, + }, + { + type: "text", + label: "Transaction successful", + variant: "secondary", + }, + { + type: "link", + label: "Explorer", + url: "{{refs.swapTxDataReq.response.data.explorer.url}}/tx/{{refs.swapTx.hash}}", + }, + ], + }, + else: { + if: { + value: "{{refs.swapTx.isSuccess}}", + match: { + equals: "false", + }, + }, + then: { + type: "link", + label: "Failed", + variant: "link", + url: "{{refs.swapTxDataReq.response.data.explorer.url}}/tx/{{refs.swapTx.hash}}", + }, + else: { + type: "horizontal-layout", + elements: [ + { + type: "link", + label: "Confirming...", + variant: "link", + url: "{{refs.swapTxDataReq.response.data.explorer.url}}/tx/{{refs.swapTx.hash}}", + }, + ], + }, }, }, }, diff --git a/mods/erc-20/src/view.ts b/mods/erc-20/src/view.ts index 0bfd75e..52cab16 100644 --- a/mods/erc-20/src/view.ts +++ b/mods/erc-20/src/view.ts @@ -123,8 +123,9 @@ const view: ModElement[] = [ label: "$5.00", onclick: { type: "SETSTATE", - ref: "buyAmountUsd", - value: "5.00", + state: { + buyAmountUsd: "5.00", + }, }, }, }, @@ -141,8 +142,9 @@ const view: ModElement[] = [ label: "$50.00", onclick: { type: "SETSTATE", - ref: "buyAmountUsd", - value: "50.00", + state: { + buyAmountUsd: "50.00", + }, }, }, }, @@ -159,8 +161,9 @@ const view: ModElement[] = [ label: "$500.00", onclick: { type: "SETSTATE", - ref: "buyAmountUsd", - value: "500.00", + state: { + buyAmountUsd: "500.00", + }, }, }, }, @@ -250,8 +253,9 @@ const view: ModElement[] = [ variant: "secondary", onclick: { type: "SETSTATE", - ref: "isBuying", - value: "false", + state: { + isBuying: "false", + }, }, }, else: { @@ -259,8 +263,9 @@ const view: ModElement[] = [ label: "Buy", onclick: { type: "SETSTATE", - ref: "isBuying", - value: "true", + state: { + isBuying: "true", + }, }, }, },