-
Notifications
You must be signed in to change notification settings - Fork 3
/
swap.tsx
72 lines (63 loc) · 2.75 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
import {FC, useCallback, useEffect, useState} from "react";
import { useWallet } from "@solana/wallet-adapter-react";
import { PublicKey } from "@solana/web3.js";
import useOctaneConfigStore from "../stores/useOctaneConfigStore";
import {TokenAmountInput} from "../components/TokenAmountInput";
import {SubmitButton} from "../components/SubmitButton";
import {OctaneFeesInfo} from "../components/OctaneFeesInfo";
import {MintInput} from "../components/MintInput";
import {
buildWhirlpoolsSwapTransaction,
sendWhirlpoolsSwapTransaction
} from "../utils/octane";
import {notify} from "../utils/notifications";
import {Title} from "../components/Title";
export const SwapView: FC = ({ }) => {
const { publicKey, signTransaction } = useWallet();
const octaneConfig = useOctaneConfigStore((s) => s.config);
const { fetchOctaneConfig, getSwapFeeConfig } = useOctaneConfigStore();
useEffect(fetchOctaneConfig, [fetchOctaneConfig]);
const [mint, setMint] = useState<string | null>(null);
const [amount, setAmount] = useState<string>("");
const submitSwap = useCallback(async () => {
const feeConfig = getSwapFeeConfig( mint);
const mintAsPublicKey = new PublicKey(mint);
const amountAsDecimals = Math.floor(parseFloat(amount) * (10 ** feeConfig.decimals));
// TODO: Preview quote
const {transaction, messageToken} = await buildWhirlpoolsSwapTransaction(
publicKey, mintAsPublicKey, amountAsDecimals
);
const signedTransaction = await signTransaction(transaction);
const txid = await sendWhirlpoolsSwapTransaction(signedTransaction, messageToken);
notify({ type: 'success', message: 'Swap is successful!', txid: txid });
}, [getSwapFeeConfig, mint, amount, publicKey, signTransaction]);
const enableSubmit = mint !== null && amount !== '';
return (
<div className="md:hero mx-auto p-4">
<div className="md:hero-content flex flex-col">
<Title text={"Gasless Swap to SOL"} />
{ octaneConfig && (
<div className="text-center flex flex-col space-y-1">
<MintInput
currentMint={mint}
onChange={e => {
setMint(e.target.value);
}}
availableMints={octaneConfig.endpoints.whirlpoolsSwap.tokens.map(tokenFee => tokenFee.mint)}
/>
<TokenAmountInput currentAmount={amount} onChange={(e) => setAmount(e.target.value)} />
{ enableSubmit && (
<OctaneFeesInfo fees={[{name: 'Transaction fees', fee: getSwapFeeConfig(mint)}]} />
)}
<SubmitButton
onClick={submitSwap}
disabled={!enableSubmit}
text={"Send transaction"}
disabledText={"Select mint"}
/>
</div>
)}
</div>
</div>
);
};