-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswap.tsx
195 lines (183 loc) · 6.25 KB
/
swap.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { useSwapCanisterBalances, useSwapCanisterController } from '@/hooks';
import { useSwapCanisterLists } from '@/hooks/use-swap-canister-lists';
import { selectPlugState, useAppSelector } from '@/store';
import { Token, Swap, Default } from '@psychedelic/sonic-js';
import { ChangeEvent, useCallback, useMemo, useState } from 'react';
/**
* Swap Section React Component
* Example of a component that executes a swap
*/
export const SwapSection = () => {
// Use custom hooks to get data and app states
const { pairList, tokenList } = useSwapCanisterLists();
const controller = useSwapCanisterController();
const { updateBalanceList } = useSwapCanisterBalances();
const { principal } = useAppSelector(selectPlugState);
/**
* Create states used for swap.
* Notice that we are using Token.Data type because it have amount
* and metadata keys.
*/
const [from, setFrom] = useState<Token.Data>({ amount: '0' });
const [to, setTo] = useState<Token.Data>({ amount: '0' });
const [isSwapRunning, setIsSwapRunning] = useState<boolean>(false);
/**
* One extra state will be generated every time that "from" changes,
* this state will hold the paths available to swap based on "from".
*/
const toOptionsList = useMemo(() => {
if (!pairList || !tokenList || !from.metadata) return {};
return Swap.getTokenPaths({
pairList,
tokenList,
tokenId: from.metadata.id,
amount: from.amount,
});
}, [from, pairList, tokenList]);
/**
* Create a handler for swapping tokens using the controller and
* managing app states
*/
const handleSwap = useCallback(() => {
if (!controller || !from.metadata || !to.metadata) return;
setIsSwapRunning(true);
controller
.swap({
amountIn: from.amount,
tokenIn: from.metadata.id,
tokenOut: to.metadata.id,
})
.then(() => Promise.resolve(updateBalanceList()))
.catch((error) => alert(`Swap failed: ${error}`))
.finally(() => setIsSwapRunning(false));
}, [controller, from, to]);
// If there is no principal we can't swap
if (!principal) {
return (
<section>
<h1>Swap</h1>
<span>Connect to plug to do swaps</span>
</section>
);
}
// Await fetching tokenList and pairList
if (!tokenList || !pairList) {
return (
<section>
<h1>Swap</h1>
<span>Loading...</span>
</section>
);
}
/**
* Create a handler for changing the "from" amount
*
* Notice that we are getting again the token paths to update the "to" amount
* using "Swap.getTokenPaths", now with "amount" param. The "amount" param
* can change the final result of the paths. This can happen when the reserves
* for swapping a pair of tokens is lower than the amount in the "from" token.
* The function "Swap.getTokenPaths" will always return the path that results
* in the higher amount of the resultant token.
*/
const handleFromAmountChange = (e: ChangeEvent<HTMLInputElement>) => {
const amount = e.currentTarget.value;
setFrom({ ...from, amount });
if (from.metadata && to.metadata) {
const paths = Swap.getTokenPaths({
pairList,
tokenList,
amount,
tokenId: from.metadata.id,
});
setTo({
...to,
amount: paths[to.metadata.id].amountOut.toString(),
});
}
};
// Create a handler for changing "from" token
const handleFromTokenChange = (e: ChangeEvent<HTMLSelectElement>) => {
setFrom({ metadata: tokenList[e.currentTarget.value], amount: '0' });
setTo({ metadata: undefined, amount: '0' });
};
// Create a handler for changing "to" token
const handleToTokenChange = (e: ChangeEvent<HTMLSelectElement>) => {
const metadata = tokenList[e.currentTarget.value];
setTo({
metadata,
amount: toOptionsList[metadata.id].amountOut.toString(),
});
};
return (
<section>
<h1>Swap</h1>
{isSwapRunning ? (
<span>Swap in progress...</span>
) : (
<>
<div
style={{
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'flex-end',
}}
>
<div>
From:
<select
name="from"
onChange={handleFromTokenChange}
value={from.metadata?.id || ''}
>
<option disabled value="" style={{ display: 'none' }}></option>
{/** Show all available tokens for "from" options */}
{Object.values(tokenList).map((token) => (
<option value={token.id} key={token.id}>
{token.symbol}
</option>
))}
</select>
<input
type="number"
min={0}
value={from.amount}
onChange={handleFromAmountChange}
/>
</div>
<div>
To:
<select
name="to"
onChange={handleToTokenChange}
value={to.metadata?.id || ''}
>
<option value="" style={{ display: 'none' }}></option>
{/** Show just available tokens for "to" options */}
{Object.keys(toOptionsList).map((tokenId) => (
<option value={tokenId} key={tokenId}>
{tokenList[tokenId].symbol}
</option>
))}
</select>
<input type="number" disabled value={to.amount} />
</div>
</div>
{to.metadata && (
<span>
<b>Minimum {to.metadata.symbol} received: </b>
{Swap.getAmountMin({
amount: to.amount,
decimals: to.metadata.decimals,
slippage: Default.SLIPPAGE,
}).toString()}
</span>
)}
<button onClick={handleSwap}>Swap</button>
</>
)}
</section>
);
};