Skip to content

Commit 856353b

Browse files
committed
feat(mod/erc-20): add tx states, clean up
1 parent ad0917b commit 856353b

File tree

4 files changed

+130
-43
lines changed

4 files changed

+130
-43
lines changed

examples/api/src/app/api/erc-20/buy/route.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
import { createPublicClient, http, parseEther } from "viem2";
3-
import { chainByName, parseTokenParam } from "../lib/utils";
4-
import { getEthUsdPrice } from "../lib/utils";
2+
import { parseEther } from "viem2";
3+
import { chainByName, getEthUsdPrice, parseTokenParam } from "../lib/utils";
54

65
export async function POST(request: NextRequest) {
76
// TODO: Expose separate execution/receiver addresses
@@ -31,15 +30,9 @@ export async function POST(request: NextRequest) {
3130
});
3231
}
3332

34-
// Get eth balance on blockchain
3533
const chain = chainByName[blockchain];
36-
const client = createPublicClient({
37-
transport: http(),
38-
chain,
39-
});
4034

4135
const ethPriceUsd = await getEthUsdPrice();
42-
4336
const ethInputAmount = parseEther((buyAmountUsd / ethPriceUsd).toString());
4437

4538
const swapCalldataParams: {
@@ -82,5 +75,6 @@ export async function POST(request: NextRequest) {
8275

8376
return NextResponse.json({
8477
transaction: swapCalldataJson.tx,
78+
explorer: chain.blockExplorers.default,
8579
});
8680
}

examples/api/src/app/api/erc-20/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ export async function GET(request: NextRequest) {
3030
fn: getFollowingHolderInfo,
3131
options: {
3232
headers: new Headers({
33-
"Cache-Control": "public, max-age=3600, immutable",
33+
// Cache for 1 day
34+
"Cache-Control": "public, max-age=86400, immutable",
3435
}),
3536
},
3637
},
3738
price: {
3839
fn: getPriceData,
3940
options: {
4041
headers: new Headers({
42+
// Cache for 1 hour
4143
"Cache-Control": "public, max-age=3600, immutable",
4244
}),
4345
},
@@ -46,7 +48,8 @@ export async function GET(request: NextRequest) {
4648
fn: getTokenInfo,
4749
options: {
4850
headers: new Headers({
49-
"Cache-Control": "public, max-age=3600, immutable",
51+
// Cache for 1 month
52+
"Cache-Control": "public, max-age=2592000, immutable",
5053
}),
5154
},
5255
},

mods/erc-20/src/buying.ts

Lines changed: 107 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,10 @@ const buy: ModElement[] = [
44
{
55
type: "padding",
66
elements: [
7+
// If no buy amount, go back to #view
78
{
89
type: "horizontal-layout",
910
elements: [
10-
{
11-
type: "circular-progress",
12-
},
13-
{
14-
type: "text",
15-
label:
16-
"Buying ~${{refs.buyAmountUsd}} of {{refs.tokenReq.response.data.name}}...",
17-
variant: "secondary",
18-
},
19-
],
20-
},
21-
22-
{
23-
type: "horizontal-layout",
24-
elements: [
25-
// If there is no buyAmountUsd value, go to #view
2611
{
2712
if: {
2813
value: "{{refs.buyAmountUsd}}",
@@ -31,12 +16,40 @@ const buy: ModElement[] = [
3116
},
3217
},
3318
then: {
34-
type: "horizontal-layout",
19+
type: "vertical-layout",
3520
onload: "#view",
3621
},
3722
},
3823
],
3924
},
25+
//
26+
{
27+
if: [
28+
{
29+
value: "{{refs.swapTx.isSuccess}}",
30+
match: {
31+
NOT: {
32+
equals: "true",
33+
},
34+
},
35+
},
36+
],
37+
then: {
38+
type: "horizontal-layout",
39+
elements: [
40+
{
41+
type: "circular-progress",
42+
},
43+
{
44+
type: "text",
45+
label:
46+
"Buying ~${{refs.buyAmountUsd}} of {{refs.tokenReq.response.data.name}}...",
47+
variant: "secondary",
48+
},
49+
],
50+
},
51+
},
52+
4053
{
4154
type: "vertical-layout",
4255
onload: {
@@ -45,7 +58,7 @@ const buy: ModElement[] = [
4558
ref: "swapTxDataReq",
4659
onsuccess: {
4760
type: "SENDETHTRANSACTION",
48-
ref: "swapTxReq",
61+
ref: "swapTx",
4962
txData: {
5063
from: "{{refs.swapTxDataReq.response.data.transaction.from}}",
5164
to: "{{refs.swapTxDataReq.response.data.transaction.to}}",
@@ -55,14 +68,86 @@ const buy: ModElement[] = [
5568
chainId: "{{refs.balancesReq.response.data.chain.id}}",
5669
onerror: {
5770
type: "SETSTATE",
58-
ref: "buyAmountUsd",
59-
value: "",
71+
state: {
72+
buyAmountUsd: "",
73+
},
6074
},
6175
},
6276
onerror: {
6377
type: "SETSTATE",
64-
ref: "buyAmountUsd",
65-
value: "",
78+
state: {
79+
buyAmountUsd: "",
80+
},
81+
},
82+
},
83+
},
84+
{
85+
if: {
86+
value: "{{refs.swapTx.hash}}",
87+
match: {
88+
NOT: {
89+
equals: "",
90+
},
91+
},
92+
},
93+
then: {
94+
if: {
95+
value: "{{refs.swapTx.isSuccess}}",
96+
match: {
97+
equals: "true",
98+
},
99+
},
100+
then: {
101+
type: "horizontal-layout",
102+
elements: [
103+
{
104+
type: "button",
105+
label: "Back",
106+
variant: "secondary",
107+
onclick: {
108+
type: "SETSTATE",
109+
state: {
110+
isBuying: "false",
111+
buyAmountUsd: "",
112+
},
113+
},
114+
},
115+
{
116+
type: "text",
117+
label: "Transaction successful",
118+
variant: "secondary",
119+
},
120+
{
121+
type: "link",
122+
label: "Explorer",
123+
url: "{{refs.swapTxDataReq.response.data.explorer.url}}/tx/{{refs.swapTx.hash}}",
124+
},
125+
],
126+
},
127+
else: {
128+
if: {
129+
value: "{{refs.swapTx.isSuccess}}",
130+
match: {
131+
equals: "false",
132+
},
133+
},
134+
then: {
135+
type: "link",
136+
label: "Failed",
137+
variant: "link",
138+
url: "{{refs.swapTxDataReq.response.data.explorer.url}}/tx/{{refs.swapTx.hash}}",
139+
},
140+
else: {
141+
type: "horizontal-layout",
142+
elements: [
143+
{
144+
type: "link",
145+
label: "Confirming...",
146+
variant: "link",
147+
url: "{{refs.swapTxDataReq.response.data.explorer.url}}/tx/{{refs.swapTx.hash}}",
148+
},
149+
],
150+
},
66151
},
67152
},
68153
},

mods/erc-20/src/view.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ const view: ModElement[] = [
123123
label: "$5.00",
124124
onclick: {
125125
type: "SETSTATE",
126-
ref: "buyAmountUsd",
127-
value: "5.00",
126+
state: {
127+
buyAmountUsd: "5.00",
128+
},
128129
},
129130
},
130131
},
@@ -141,8 +142,9 @@ const view: ModElement[] = [
141142
label: "$50.00",
142143
onclick: {
143144
type: "SETSTATE",
144-
ref: "buyAmountUsd",
145-
value: "50.00",
145+
state: {
146+
buyAmountUsd: "50.00",
147+
},
146148
},
147149
},
148150
},
@@ -159,8 +161,9 @@ const view: ModElement[] = [
159161
label: "$500.00",
160162
onclick: {
161163
type: "SETSTATE",
162-
ref: "buyAmountUsd",
163-
value: "500.00",
164+
state: {
165+
buyAmountUsd: "500.00",
166+
},
164167
},
165168
},
166169
},
@@ -250,17 +253,19 @@ const view: ModElement[] = [
250253
variant: "secondary",
251254
onclick: {
252255
type: "SETSTATE",
253-
ref: "isBuying",
254-
value: "false",
256+
state: {
257+
isBuying: "false",
258+
},
255259
},
256260
},
257261
else: {
258262
type: "button",
259263
label: "Buy",
260264
onclick: {
261265
type: "SETSTATE",
262-
ref: "isBuying",
263-
value: "true",
266+
state: {
267+
isBuying: "true",
268+
},
264269
},
265270
},
266271
},

0 commit comments

Comments
 (0)