Skip to content

Commit

Permalink
feat(mod/erc-20): add tx states, clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
stephancill committed Jan 19, 2024
1 parent ad0917b commit 856353b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 43 deletions.
12 changes: 3 additions & 9 deletions examples/api/src/app/api/erc-20/buy/route.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -82,5 +75,6 @@ export async function POST(request: NextRequest) {

return NextResponse.json({
transaction: swapCalldataJson.tx,
explorer: chain.blockExplorers.default,
});
}
7 changes: 5 additions & 2 deletions examples/api/src/app/api/erc-20/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ 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",
}),
},
},
price: {
fn: getPriceData,
options: {
headers: new Headers({
// Cache for 1 hour
"Cache-Control": "public, max-age=3600, immutable",
}),
},
Expand All @@ -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",
}),
},
},
Expand Down
129 changes: 107 additions & 22 deletions mods/erc-20/src/buying.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}}",
Expand All @@ -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: {
Expand All @@ -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}}",
Expand All @@ -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}}",
},
],
},
},
},
},
Expand Down
25 changes: 15 additions & 10 deletions mods/erc-20/src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ const view: ModElement[] = [
label: "$5.00",
onclick: {
type: "SETSTATE",
ref: "buyAmountUsd",
value: "5.00",
state: {
buyAmountUsd: "5.00",
},
},
},
},
Expand All @@ -141,8 +142,9 @@ const view: ModElement[] = [
label: "$50.00",
onclick: {
type: "SETSTATE",
ref: "buyAmountUsd",
value: "50.00",
state: {
buyAmountUsd: "50.00",
},
},
},
},
Expand All @@ -159,8 +161,9 @@ const view: ModElement[] = [
label: "$500.00",
onclick: {
type: "SETSTATE",
ref: "buyAmountUsd",
value: "500.00",
state: {
buyAmountUsd: "500.00",
},
},
},
},
Expand Down Expand Up @@ -250,17 +253,19 @@ const view: ModElement[] = [
variant: "secondary",
onclick: {
type: "SETSTATE",
ref: "isBuying",
value: "false",
state: {
isBuying: "false",
},
},
},
else: {
type: "button",
label: "Buy",
onclick: {
type: "SETSTATE",
ref: "isBuying",
value: "true",
state: {
isBuying: "true",
},
},
},
},
Expand Down

0 comments on commit 856353b

Please sign in to comment.