-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
153 lines (143 loc) · 3.95 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { useCallback, useEffect, useState } from "react";
import "./App.css";
import {
AppConfig,
openContractCall,
showConnect,
UserData,
UserSession,
} from "@stacks/connect";
import { AlexSDK, Currency } from "alex-sdk";
const appConfig = new AppConfig([]);
const userSession = new UserSession({ appConfig });
const appDetails = {
name: "Alex Swap Example",
icon: "https://cdn.alexlab.co/logos/ALEX_Token.png",
};
const alex = new AlexSDK();
function App() {
const [userData, setUserData] = useState<UserData | undefined>(undefined);
useEffect(() => {
if (userSession.isSignInPending()) {
userSession.handlePendingSignIn().then((userData) => {
setUserData(userData);
});
} else if (userSession.isUserSignedIn()) {
setUserData(userSession.loadUserData());
}
}, []);
const onConnectWallet = useCallback(() => {
showConnect({
appDetails,
onFinish: () => window.location.reload(),
userSession,
});
}, []);
const [from, setFrom] = useState<Currency>(Currency.STX);
const [amount, setAmount] = useState("");
const [to, setTo] = useState<Currency>(Currency.ALEX);
const valid =
from != null &&
to != null &&
amount !== "" &&
!isNaN(Number(amount)) &&
from !== to;
return (
<div>
<div
style={{
display: "flex",
flexDirection: "column",
gap: 5,
marginBottom: 10,
}}
>
<p>From:</p>
<input
value={amount}
onChange={(e) => setAmount(e.target.value)}
placeholder="amount"
/>
<select
value={from}
onChange={(e) => setFrom(e.target.value as Currency)}
>
{Object.values(Currency).map((a) => (
<option key={a} value={a}>
{a}
</option>
))}
</select>
<p>to:</p>
<select value={to} onChange={(e) => setTo(e.target.value as Currency)}>
{Object.values(Currency).map((a) => (
<option key={a} value={a}>
{a}
</option>
))}
</select>
</div>
{userData == null ? (
<button onClick={onConnectWallet}>Connect Wallet</button>
) : (
<div style={{ display: "flex", flexDirection: "column", gap: 5 }}>
<button
disabled={!valid}
onClick={async () => {
const result = await alex.getFeeRate(from!, to!);
const feeRate = Number(result) / 1e8;
alert(
`Fee rate is ${feeRate * 100}%, fee is ${
Number(amount) * feeRate
} ${from}`
);
}}
>
Get Fee
</button>
<button
disabled={!valid}
onClick={async () => {
const result = await alex.getAmountTo(
from!,
BigInt(Number(amount) * 1e8),
to!
);
alert(`You will get ${Number(result) / 1e8} ${to}`);
}}
>
Get Rate
</button>
<button
disabled={!valid}
onClick={async () => {
const routers = await alex.getRouter(from!, to!);
alert(`Routers: ${routers.join(" -> ")}`);
}}
>
Get Route
</button>
<button
disabled={!valid}
onClick={async () => {
const stxAddress = userData!.profile.stxAddress.mainnet;
const routers = await alex.getRouter(from!, to!);
const tx = await alex.runSwap(
stxAddress,
from!,
to!,
BigInt(Number(amount) * 1e8),
BigInt(0),
routers
);
await openContractCall(tx);
}}
>
Swap
</button>
</div>
)}
</div>
);
}
export default App;