swap-ui/src/App.tsx

70 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-05-12 13:10:52 -07:00
import React, { useState, useEffect } from "react";
2021-05-13 00:28:32 -07:00
import { Typography } from "@material-ui/core";
2021-05-12 13:10:52 -07:00
import { Provider } from "@project-serum/anchor";
2021-05-12 10:57:22 -07:00
// @ts-ignore
2021-05-12 13:10:52 -07:00
import Wallet from "@project-serum/sol-wallet-adapter";
import { ConfirmOptions, Connection } from "@solana/web3.js";
import { TokenListProvider } from "@solana/spl-token-registry";
import Swap from "./components/Swap";
import "./App.css";
2021-05-12 10:57:22 -07:00
function App() {
2021-05-12 13:10:52 -07:00
const [params, setParams] = useState<any>(null);
const [isConnected, setIsConnected] = useState(false);
2021-05-12 10:57:22 -07:00
2021-05-12 13:10:52 -07:00
// Create the provider and token list.
useEffect(() => {
const opts: ConfirmOptions = {
preflightCommitment: "recent",
commitment: "recent",
};
2021-05-13 01:11:13 -07:00
const network = "https://solana-api.projectserum.com";
2021-05-12 13:10:52 -07:00
const wallet = new Wallet("https://www.sollet.io", network);
const connection = new Connection(network, opts.preflightCommitment);
const provider = new Provider(connection, wallet, opts);
new TokenListProvider().resolve().then((tokenList) => {
setParams({
provider,
tokenList,
});
wallet.connect();
});
}, [setParams]);
2021-05-12 10:57:22 -07:00
2021-05-12 13:10:52 -07:00
// Connect to the wallet.
useEffect(() => {
if (params !== null) {
params.provider.wallet.on("connect", () => {
setIsConnected(true);
});
params.provider.wallet.connect();
}
}, [params]);
2021-05-12 10:57:22 -07:00
return (
2021-05-13 00:28:32 -07:00
<div
style={{
width: "450px",
marginLeft: "auto",
marginRight: "auto",
position: "absolute",
left: 0,
right: 0,
top: 0,
bottom: 0,
display: "flex",
justifyContent: "center",
flexDirection: "column",
}}
>
{isConnected ? (
<Swap provider={params.provider} tokenList={params.tokenList} />
) : (
<Typography style={{ textAlign: "center" }}>Disconnected</Typography>
2021-05-12 13:10:52 -07:00
)}
</div>
2021-05-12 10:57:22 -07:00
);
}
export default App;