Basic UI scaffolding
This commit is contained in:
parent
d7a84292a4
commit
2489952e89
10
package.json
10
package.json
|
@ -3,6 +3,8 @@
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@material-ui/core": "^4.11.4",
|
||||||
|
"@material-ui/icons": "^4.11.2",
|
||||||
"@project-serum/sol-wallet-adapter": "^0.2.0",
|
"@project-serum/sol-wallet-adapter": "^0.2.0",
|
||||||
"@project-serum/swap": "^0.1.0-alpha.2",
|
"@project-serum/swap": "^0.1.0-alpha.2",
|
||||||
"@solana/spl-token": "^0.1.4",
|
"@solana/spl-token": "^0.1.4",
|
||||||
|
@ -15,7 +17,7 @@
|
||||||
"@types/node": "^12.0.0",
|
"@types/node": "^12.0.0",
|
||||||
"@types/react": "^17.0.0",
|
"@types/react": "^17.0.0",
|
||||||
"@types/react-dom": "^17.0.0",
|
"@types/react-dom": "^17.0.0",
|
||||||
"antd": "^4.15.5",
|
"bs58": "^4.0.1",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
"react-scripts": "4.0.3",
|
"react-scripts": "4.0.3",
|
||||||
|
@ -26,7 +28,8 @@
|
||||||
"start": "react-scripts start",
|
"start": "react-scripts start",
|
||||||
"build": "react-scripts build",
|
"build": "react-scripts build",
|
||||||
"test": "react-scripts test",
|
"test": "react-scripts test",
|
||||||
"eject": "react-scripts eject"
|
"eject": "react-scripts eject",
|
||||||
|
"lint:fix": "prettier src/** -w"
|
||||||
},
|
},
|
||||||
"eslintConfig": {
|
"eslintConfig": {
|
||||||
"extends": [
|
"extends": [
|
||||||
|
@ -45,5 +48,8 @@
|
||||||
"last 1 firefox version",
|
"last 1 firefox version",
|
||||||
"last 1 safari version"
|
"last 1 safari version"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"prettier": "^2.3.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import React from 'react';
|
import React from "react";
|
||||||
import { render, screen } from '@testing-library/react';
|
import { render, screen } from "@testing-library/react";
|
||||||
import App from './App';
|
import App from "./App";
|
||||||
|
|
||||||
test('renders learn react link', () => {
|
test("renders learn react link", () => {
|
||||||
render(<App />);
|
render(<App />);
|
||||||
const linkElement = screen.getByText(/learn react/i);
|
const linkElement = screen.getByText(/learn react/i);
|
||||||
expect(linkElement).toBeInTheDocument();
|
expect(linkElement).toBeInTheDocument();
|
||||||
|
|
40
src/App.tsx
40
src/App.tsx
|
@ -1,11 +1,11 @@
|
||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from "react";
|
||||||
import { Provider } from '@project-serum/anchor';
|
import { Provider } from "@project-serum/anchor";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import Wallet from '@project-serum/sol-wallet-adapter';
|
import Wallet from "@project-serum/sol-wallet-adapter";
|
||||||
import { ConfirmOptions, Connection } from '@solana/web3.js';
|
import { ConfirmOptions, Connection } from "@solana/web3.js";
|
||||||
import { TokenListProvider } from '@solana/spl-token-registry';
|
import { TokenListProvider } from "@solana/spl-token-registry";
|
||||||
import Swap from './components/Swap';
|
import Swap from "./components/Swap";
|
||||||
import './App.css';
|
import "./App.css";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [params, setParams] = useState<any>(null);
|
const [params, setParams] = useState<any>(null);
|
||||||
|
@ -14,14 +14,14 @@ function App() {
|
||||||
// Create the provider and token list.
|
// Create the provider and token list.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const opts: ConfirmOptions = {
|
const opts: ConfirmOptions = {
|
||||||
preflightCommitment: 'recent',
|
preflightCommitment: "recent",
|
||||||
commitment: 'recent',
|
commitment: "recent",
|
||||||
};
|
};
|
||||||
const network = 'https://api.mainnet-beta.solana.com';
|
const network = "https://api.mainnet-beta.solana.com";
|
||||||
const wallet = new Wallet('https://www.sollet.io', network);
|
const wallet = new Wallet("https://www.sollet.io", network);
|
||||||
const connection = new Connection(network, opts.preflightCommitment);
|
const connection = new Connection(network, opts.preflightCommitment);
|
||||||
const provider = new Provider(connection, wallet, opts);
|
const provider = new Provider(connection, wallet, opts);
|
||||||
new TokenListProvider().resolve().then(tokenList => {
|
new TokenListProvider().resolve().then((tokenList) => {
|
||||||
setParams({
|
setParams({
|
||||||
provider,
|
provider,
|
||||||
tokenList,
|
tokenList,
|
||||||
|
@ -33,7 +33,7 @@ function App() {
|
||||||
// Connect to the wallet.
|
// Connect to the wallet.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (params !== null) {
|
if (params !== null) {
|
||||||
params.provider.wallet.on('connect', () => {
|
params.provider.wallet.on("connect", () => {
|
||||||
setIsConnected(true);
|
setIsConnected(true);
|
||||||
});
|
});
|
||||||
params.provider.wallet.connect();
|
params.provider.wallet.connect();
|
||||||
|
@ -43,10 +43,16 @@ function App() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{isConnected && (
|
{isConnected && (
|
||||||
<Swap
|
<div
|
||||||
provider={params.provider}
|
style={{
|
||||||
tokenList={params.tokenList}
|
width: "450px",
|
||||||
/>
|
marginTop: "64px",
|
||||||
|
marginLeft: "auto",
|
||||||
|
marginRight: "auto",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Swap provider={params.provider} tokenList={params.tokenList} />
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
@ -0,0 +1,211 @@
|
||||||
|
import React, { useContext, useState, useEffect } from "react";
|
||||||
|
import { Swap as SwapClient } from "@project-serum/swap";
|
||||||
|
import { PublicKey, Account } from "@solana/web3.js";
|
||||||
|
import {
|
||||||
|
AccountInfo as TokenAccount,
|
||||||
|
MintInfo,
|
||||||
|
Token,
|
||||||
|
TOKEN_PROGRAM_ID,
|
||||||
|
} from "@solana/spl-token";
|
||||||
|
import { TokenListContainer, TokenInfo } from "@solana/spl-token-registry";
|
||||||
|
import { getOwnedTokenAccounts } from "../utils/tokens";
|
||||||
|
|
||||||
|
const SRM_MINT = new PublicKey("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt");
|
||||||
|
const USDC_MINT = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
|
||||||
|
|
||||||
|
const SwapContext = React.createContext<null | SwapContext>(null);
|
||||||
|
|
||||||
|
export function SwapContextProvider(props: any) {
|
||||||
|
const swapClient = props.swapClient;
|
||||||
|
const [fromMint, setFromMint] = useState(SRM_MINT);
|
||||||
|
const [toMint, setToMint] = useState(USDC_MINT);
|
||||||
|
const [fromAmount, setFromAmount] = useState(0);
|
||||||
|
const [toAmount, setToAmount] = useState(0);
|
||||||
|
const [fromBalance, setFromBalance] = useState(undefined);
|
||||||
|
const [toBalance, setToBalance] = useState(undefined);
|
||||||
|
const [minExpectedAmount, setMinExpectedAmount] = useState(0);
|
||||||
|
const [ownedTokenAccounts, setOwnedTokenAccounts] = useState(undefined);
|
||||||
|
const [mintCache, setMintCache] = useState(new Map<string, MintInfo>());
|
||||||
|
|
||||||
|
// Fetch all the owned token accounts for the wallet.
|
||||||
|
useEffect(() => {
|
||||||
|
getOwnedTokenAccounts(
|
||||||
|
swapClient.program.provider.connection,
|
||||||
|
swapClient.program.provider.wallet.publicKey
|
||||||
|
).then(setOwnedTokenAccounts);
|
||||||
|
}, [
|
||||||
|
swapClient.program.provider.wallet.publicKey,
|
||||||
|
swapClient.program.provider.connection,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Fetch the mint account infos not already in the cache.
|
||||||
|
useEffect(() => {
|
||||||
|
const fromMintClient = new Token(
|
||||||
|
swapClient.program.provider.connection,
|
||||||
|
fromMint,
|
||||||
|
TOKEN_PROGRAM_ID,
|
||||||
|
new Account()
|
||||||
|
);
|
||||||
|
const toMintClient = new Token(
|
||||||
|
swapClient.program.provider.connection,
|
||||||
|
toMint,
|
||||||
|
TOKEN_PROGRAM_ID,
|
||||||
|
new Account()
|
||||||
|
);
|
||||||
|
|
||||||
|
let promises = [];
|
||||||
|
if (mintCache.get(fromMint.toString())) {
|
||||||
|
promises.push(
|
||||||
|
(async (): Promise<MintInfo> => {
|
||||||
|
return mintCache.get(fromMint.toString()) as MintInfo;
|
||||||
|
})()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
promises.push(fromMintClient.getMintInfo());
|
||||||
|
}
|
||||||
|
if (mintCache.get(toMint.toString())) {
|
||||||
|
promises.push(
|
||||||
|
(async (): Promise<MintInfo> => {
|
||||||
|
return mintCache.get(toMint.toString()) as MintInfo;
|
||||||
|
})()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
promises.push(toMintClient.getMintInfo());
|
||||||
|
}
|
||||||
|
|
||||||
|
Promise.all(promises as [Promise<MintInfo>, Promise<MintInfo>]).then(
|
||||||
|
([fromMintInfo, toMintInfo]: [MintInfo, MintInfo]) => {
|
||||||
|
let cache = new Map(mintCache);
|
||||||
|
cache.set(fromMint.toString(), fromMintInfo);
|
||||||
|
cache.set(toMint.toString(), toMintInfo);
|
||||||
|
setMintCache(cache);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}, [fromMint, toMint]);
|
||||||
|
|
||||||
|
const swapToFromMints = () => {
|
||||||
|
const oldFrom = fromMint;
|
||||||
|
const oldFromAmount = fromAmount;
|
||||||
|
const oldTo = toMint;
|
||||||
|
const oldToAmount = toAmount;
|
||||||
|
setFromMint(oldTo);
|
||||||
|
setToMint(oldFrom);
|
||||||
|
setFromAmount(oldToAmount);
|
||||||
|
setToAmount(oldFromAmount);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SwapContext.Provider
|
||||||
|
value={{
|
||||||
|
swapClient,
|
||||||
|
fromMint,
|
||||||
|
setFromMint,
|
||||||
|
toMint,
|
||||||
|
setToMint,
|
||||||
|
fromAmount,
|
||||||
|
setFromAmount,
|
||||||
|
toAmount,
|
||||||
|
setToAmount,
|
||||||
|
minExpectedAmount,
|
||||||
|
swapToFromMints,
|
||||||
|
fromBalance,
|
||||||
|
toBalance,
|
||||||
|
ownedTokenAccounts,
|
||||||
|
mintCache,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{props.children}
|
||||||
|
</SwapContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useSwapContext(): SwapContext {
|
||||||
|
const ctx = useContext(SwapContext);
|
||||||
|
if (ctx === null) {
|
||||||
|
throw new Error("Context not available");
|
||||||
|
}
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SwapContext = {
|
||||||
|
swapClient: SwapClient;
|
||||||
|
fromMint: PublicKey;
|
||||||
|
setFromMint: (m: PublicKey) => void;
|
||||||
|
toMint: PublicKey;
|
||||||
|
setToMint: (m: PublicKey) => void;
|
||||||
|
fromAmount: number;
|
||||||
|
setFromAmount: (a: number) => void;
|
||||||
|
toAmount: number;
|
||||||
|
setToAmount: (a: number) => void;
|
||||||
|
minExpectedAmount: number;
|
||||||
|
swapToFromMints: () => void;
|
||||||
|
fromBalance?: number;
|
||||||
|
toBalance?: number;
|
||||||
|
fromMintAccount?: MintInfo;
|
||||||
|
toMintAccount?: MintInfo;
|
||||||
|
ownedTokenAccounts:
|
||||||
|
| { publicKey: PublicKey; account: TokenAccount }[]
|
||||||
|
| undefined;
|
||||||
|
mintCache: Map<string, MintInfo>;
|
||||||
|
};
|
||||||
|
|
||||||
|
const TokenListContext = React.createContext<null | TokenListContext>(null);
|
||||||
|
|
||||||
|
export function TokenListContextProvider(props: any) {
|
||||||
|
return (
|
||||||
|
<TokenListContext.Provider value={{ tokenList: props.tokenList }}>
|
||||||
|
{props.children}
|
||||||
|
</TokenListContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
type TokenListContext = {
|
||||||
|
tokenList: TokenListContainer;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function useTokenList(): TokenInfo[] {
|
||||||
|
const ctx = useContext(TokenListContext);
|
||||||
|
if (ctx === null) {
|
||||||
|
throw new Error("Context not available");
|
||||||
|
}
|
||||||
|
return ctx.tokenList.getList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Null => none exists.
|
||||||
|
// Undefined => loading.
|
||||||
|
export function useOwnedTokenAccount(
|
||||||
|
mint: PublicKey
|
||||||
|
): { publicKey: PublicKey; account: TokenAccount } | null | undefined {
|
||||||
|
const ctx = useContext(SwapContext);
|
||||||
|
if (ctx === null) {
|
||||||
|
throw new Error("Context not available");
|
||||||
|
}
|
||||||
|
if (ctx.ownedTokenAccounts === undefined) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const tokenAccounts = ctx.ownedTokenAccounts.filter((account) =>
|
||||||
|
account.account.mint.equals(mint)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (tokenAccounts.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Take the account with the most tokens in it.
|
||||||
|
tokenAccounts.sort((a, b) =>
|
||||||
|
a.account.amount < a.account.amount
|
||||||
|
? -1
|
||||||
|
: a.account.amount > b.account.amount
|
||||||
|
? 1
|
||||||
|
: 0
|
||||||
|
);
|
||||||
|
return tokenAccounts[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useMintAccount(mint: PublicKey): MintInfo | undefined {
|
||||||
|
const ctx = useContext(SwapContext);
|
||||||
|
if (ctx === null) {
|
||||||
|
throw new Error("Context not available");
|
||||||
|
}
|
||||||
|
return ctx.mintCache.get(mint.toString());
|
||||||
|
}
|
|
@ -1,13 +1,324 @@
|
||||||
import { Provider } from '@project-serum/anchor';
|
import React, { useState, useContext } from "react";
|
||||||
import { Swap as SwapClient } from '@project-serum/swap';
|
import { BN } from "@project-serum/anchor";
|
||||||
import { TokenListContainer } from '@solana/spl-token-registry';
|
import { PublicKey } from "@solana/web3.js";
|
||||||
|
import { TokenListContainer } from "@solana/spl-token-registry";
|
||||||
|
import { Provider } from "@project-serum/anchor";
|
||||||
|
import { Swap as SwapClient } from "@project-serum/swap";
|
||||||
|
import {
|
||||||
|
makeStyles,
|
||||||
|
Card,
|
||||||
|
Button,
|
||||||
|
Tabs,
|
||||||
|
Tab,
|
||||||
|
IconButton,
|
||||||
|
Paper,
|
||||||
|
Typography,
|
||||||
|
TextField,
|
||||||
|
} from "@material-ui/core";
|
||||||
|
import { Settings, Info, ExpandMore } from "@material-ui/icons";
|
||||||
|
import {
|
||||||
|
SwapContextProvider,
|
||||||
|
TokenListContextProvider,
|
||||||
|
useSwapContext,
|
||||||
|
useTokenList,
|
||||||
|
useOwnedTokenAccount,
|
||||||
|
useMintAccount,
|
||||||
|
} from "./Context";
|
||||||
|
|
||||||
export default function Swap({ provider, tokenList }: { provider: Provider; tokenList: TokenListContainer; }) {
|
const useStyles = makeStyles((theme) => ({
|
||||||
const client = new SwapClient(provider, tokenList);
|
card: {
|
||||||
console.log('client', client);
|
width: "450px",
|
||||||
|
borderRadius: "10px",
|
||||||
|
},
|
||||||
|
cardContent: {
|
||||||
|
marginLeft: "6px",
|
||||||
|
marginRight: "6px",
|
||||||
|
marginBottom: "6px",
|
||||||
|
},
|
||||||
|
tab: {
|
||||||
|
width: "50%",
|
||||||
|
},
|
||||||
|
settings: {
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "row-reverse",
|
||||||
|
},
|
||||||
|
settingsButton: {
|
||||||
|
padding: 0,
|
||||||
|
},
|
||||||
|
swapButton: {
|
||||||
|
width: "100%",
|
||||||
|
borderRadius: "15px",
|
||||||
|
},
|
||||||
|
swapToFromButton: {
|
||||||
|
display: "block",
|
||||||
|
marginLeft: "auto",
|
||||||
|
marginRight: "auto",
|
||||||
|
},
|
||||||
|
auxilliaryLabel: {
|
||||||
|
marginTop: "10px",
|
||||||
|
marginBottom: "10px",
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
marginLeft: "5px",
|
||||||
|
marginRight: "5px",
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
export default function Swap({
|
||||||
|
style,
|
||||||
|
provider,
|
||||||
|
tokenList,
|
||||||
|
}: {
|
||||||
|
style?: any;
|
||||||
|
provider: Provider;
|
||||||
|
tokenList: TokenListContainer;
|
||||||
|
}) {
|
||||||
|
const swapClient = new SwapClient(provider, tokenList);
|
||||||
return (
|
return (
|
||||||
<div>
|
<SwapContextProvider swapClient={swapClient}>
|
||||||
Swapping
|
<TokenListContextProvider tokenList={tokenList}>
|
||||||
|
<SwapInner style={style} />
|
||||||
|
</TokenListContextProvider>
|
||||||
|
</SwapContextProvider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function SwapInner({ style }: { style?: any }) {
|
||||||
|
const styles = useStyles();
|
||||||
|
return (
|
||||||
|
<div style={style}>
|
||||||
|
<Card className={styles.card}>
|
||||||
|
<SwapHeader />
|
||||||
|
<div className={styles.cardContent}>
|
||||||
|
<SwapFromForm />
|
||||||
|
<SwapToFromButton />
|
||||||
|
<SwapToForm />
|
||||||
|
<AuxilliaryLabel />
|
||||||
|
<SwapButton />
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function SwapHeader() {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
margin: "8px",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Typography
|
||||||
|
style={{
|
||||||
|
fontWeight: "bold",
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "center",
|
||||||
|
flexDirection: "column",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Swap
|
||||||
|
</Typography>
|
||||||
|
<SettingsButton />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function AuxilliaryLabel() {
|
||||||
|
const styles = useStyles();
|
||||||
|
|
||||||
|
const { fromMint, toMint, fromAmount, toAmount } = useSwapContext();
|
||||||
|
const toPrice = (fromAmount / toAmount).toFixed(6); // TODO: decimals per mint type.
|
||||||
|
|
||||||
|
const tokenList = useTokenList();
|
||||||
|
let fromTokenInfo = tokenList.filter(
|
||||||
|
(t) => t.address === fromMint.toString()
|
||||||
|
)[0];
|
||||||
|
let toTokenInfo = tokenList.filter((t) => t.address === toMint.toString())[0];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={styles.auxilliaryLabel}>
|
||||||
|
<Typography color="textSecondary">Serum</Typography>
|
||||||
|
<div style={{ display: "flex" }}>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
marginRight: "10px",
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "center",
|
||||||
|
flexDirection: "column",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{fromAmount !== 0 && toAmount !== 0
|
||||||
|
? `1 ${toTokenInfo.symbol} = ${toPrice} ${fromTokenInfo.symbol}`
|
||||||
|
: `-`}
|
||||||
|
</div>
|
||||||
|
<Info />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function TabSelector() {
|
||||||
|
const styles = useStyles();
|
||||||
|
return (
|
||||||
|
<Tabs>
|
||||||
|
<Tab label="Trade" className={styles.tab} />
|
||||||
|
<Tab label="Accounts" className={styles.tab} />
|
||||||
|
</Tabs>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SwapToFromButton() {
|
||||||
|
const styles = useStyles();
|
||||||
|
const { swapToFromMints } = useSwapContext();
|
||||||
|
return (
|
||||||
|
<Button className={styles.swapToFromButton} onClick={swapToFromMints}>
|
||||||
|
⇅
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function SettingsButton() {
|
||||||
|
const styles = useStyles();
|
||||||
|
return (
|
||||||
|
<div className={styles.settings}>
|
||||||
|
<IconButton className={styles.settingsButton}>
|
||||||
|
<Settings />
|
||||||
|
</IconButton>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function SwapFromForm() {
|
||||||
|
const { fromMint, setFromMint, fromAmount, setFromAmount } = useSwapContext();
|
||||||
|
return (
|
||||||
|
<SwapTokenForm
|
||||||
|
isEstimate={false}
|
||||||
|
mint={fromMint}
|
||||||
|
setMint={setFromMint}
|
||||||
|
amount={fromAmount}
|
||||||
|
setAmount={setFromAmount}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function SwapToForm() {
|
||||||
|
const { toMint, setToMint, toAmount, setToAmount } = useSwapContext();
|
||||||
|
return (
|
||||||
|
<SwapTokenForm
|
||||||
|
isEstimate={true}
|
||||||
|
mint={toMint}
|
||||||
|
setMint={setToMint}
|
||||||
|
amount={toAmount}
|
||||||
|
setAmount={setToAmount}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function SwapTokenForm({
|
||||||
|
isEstimate,
|
||||||
|
mint,
|
||||||
|
setMint,
|
||||||
|
amount,
|
||||||
|
setAmount,
|
||||||
|
}: {
|
||||||
|
isEstimate: boolean;
|
||||||
|
mint: PublicKey;
|
||||||
|
setMint: (m: PublicKey) => void;
|
||||||
|
amount: number;
|
||||||
|
setAmount: (a: number) => void;
|
||||||
|
}) {
|
||||||
|
const tokenAccount = useOwnedTokenAccount(mint);
|
||||||
|
const mintAccount = useMintAccount(mint);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Paper elevation={0} variant="outlined">
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
height: "50px",
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<TokenButton mint={mint} />
|
||||||
|
<TextField
|
||||||
|
type="number"
|
||||||
|
value={amount}
|
||||||
|
onChange={(e) => setAmount(parseFloat(e.target.value))}
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "center",
|
||||||
|
flexDirection: "column",
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div style={{ marginLeft: "10px", height: "30px" }}>
|
||||||
|
<Typography color="textSecondary" style={{ fontSize: "14px" }}>
|
||||||
|
{tokenAccount && mintAccount
|
||||||
|
? `Balance: ${(
|
||||||
|
tokenAccount.account.amount.toNumber() /
|
||||||
|
10 ** mintAccount.decimals
|
||||||
|
).toFixed(mintAccount.decimals)}`
|
||||||
|
: `-`}
|
||||||
|
</Typography>
|
||||||
|
</div>
|
||||||
|
</Paper>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function TokenButton({ mint }: { mint: PublicKey }) {
|
||||||
|
return (
|
||||||
|
<Button style={{ width: "116px" }}>
|
||||||
|
<TokenIcon mint={mint} />
|
||||||
|
<TokenName mint={mint} />
|
||||||
|
<ExpandMore />
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function TokenIcon({ mint }: { mint: PublicKey }) {
|
||||||
|
const tokenList = useTokenList();
|
||||||
|
let tokenInfo = tokenList.filter((t) => t.address === mint.toString())[0];
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "center",
|
||||||
|
flexDirection: "column",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<img style={{ width: "25px" }} src={tokenInfo.logoURI} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function TokenName({ mint }: { mint: PublicKey }) {
|
||||||
|
const tokenList = useTokenList();
|
||||||
|
let tokenInfo = tokenList.filter((t) => t.address === mint.toString())[0];
|
||||||
|
return (
|
||||||
|
<Typography style={{ marginLeft: "5px" }}>{tokenInfo.symbol}</Typography>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function SwapButton() {
|
||||||
|
const styles = useStyles();
|
||||||
|
const { fromMint, toMint, fromAmount, minExpectedAmount } = useSwapContext();
|
||||||
|
|
||||||
|
const sendSwapTransaction = async () => {
|
||||||
|
console.log("sending swap");
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
className={styles.swapButton}
|
||||||
|
onClick={sendSwapTransaction}
|
||||||
|
>
|
||||||
|
Swap
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function TokenSelector() {
|
||||||
|
return <div></div>;
|
||||||
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
body {
|
body {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
|
||||||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
|
||||||
sans-serif;
|
sans-serif;
|
||||||
-webkit-font-smoothing: antialiased;
|
-webkit-font-smoothing: antialiased;
|
||||||
-moz-osx-font-smoothing: grayscale;
|
-moz-osx-font-smoothing: grayscale;
|
||||||
}
|
}
|
||||||
|
|
||||||
code {
|
code {
|
||||||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
|
||||||
monospace;
|
monospace;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
import React from 'react';
|
import React from "react";
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from "react-dom";
|
||||||
import './index.css';
|
import "./index.css";
|
||||||
import App from './App';
|
import App from "./App";
|
||||||
import reportWebVitals from './reportWebVitals';
|
import reportWebVitals from "./reportWebVitals";
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<App />
|
<App />
|
||||||
</React.StrictMode>,
|
</React.StrictMode>,
|
||||||
document.getElementById('root')
|
document.getElementById("root")
|
||||||
);
|
);
|
||||||
|
|
||||||
// If you want to start measuring performance in your app, pass a function
|
// If you want to start measuring performance in your app, pass a function
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3"><g fill="#61DAFB"><path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/><circle cx="420.9" cy="296.5" r="45.7"/><path d="M520.5 78.1z"/></g></svg>
|
|
Before Width: | Height: | Size: 2.6 KiB |
|
@ -1,8 +1,8 @@
|
||||||
import { ReportHandler } from 'web-vitals';
|
import { ReportHandler } from "web-vitals";
|
||||||
|
|
||||||
const reportWebVitals = (onPerfEntry?: ReportHandler) => {
|
const reportWebVitals = (onPerfEntry?: ReportHandler) => {
|
||||||
if (onPerfEntry && onPerfEntry instanceof Function) {
|
if (onPerfEntry && onPerfEntry instanceof Function) {
|
||||||
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
|
import("web-vitals").then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
|
||||||
getCLS(onPerfEntry);
|
getCLS(onPerfEntry);
|
||||||
getFID(onPerfEntry);
|
getFID(onPerfEntry);
|
||||||
getFCP(onPerfEntry);
|
getFCP(onPerfEntry);
|
||||||
|
|
|
@ -2,4 +2,4 @@
|
||||||
// allows you to do things like:
|
// allows you to do things like:
|
||||||
// expect(element).toHaveTextContent(/react/i)
|
// expect(element).toHaveTextContent(/react/i)
|
||||||
// learn more: https://github.com/testing-library/jest-dom
|
// learn more: https://github.com/testing-library/jest-dom
|
||||||
import '@testing-library/jest-dom';
|
import "@testing-library/jest-dom";
|
||||||
|
|
|
@ -0,0 +1,105 @@
|
||||||
|
// TODO: replace this whole file with something more modern. This is all copied
|
||||||
|
// from sollet..
|
||||||
|
|
||||||
|
import * as BufferLayout from "buffer-layout";
|
||||||
|
import { BN } from "@project-serum/anchor";
|
||||||
|
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
|
||||||
|
import { Connection, PublicKey } from "@solana/web3.js";
|
||||||
|
import * as bs58 from "bs58";
|
||||||
|
|
||||||
|
export async function getOwnedTokenAccounts(
|
||||||
|
connection: Connection,
|
||||||
|
publicKey: PublicKey
|
||||||
|
) {
|
||||||
|
let filters = getOwnedAccountsFilters(publicKey);
|
||||||
|
// @ts-ignore
|
||||||
|
let resp = await connection._rpcRequest("getProgramAccounts", [
|
||||||
|
TOKEN_PROGRAM_ID.toBase58(),
|
||||||
|
{
|
||||||
|
commitment: connection.commitment,
|
||||||
|
filters,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
if (resp.error) {
|
||||||
|
throw new Error(
|
||||||
|
"failed to get token accounts owned by " +
|
||||||
|
publicKey.toBase58() +
|
||||||
|
": " +
|
||||||
|
resp.error.message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return resp.result
|
||||||
|
.map(({ pubkey, account: { data, executable, owner, lamports } }: any) => ({
|
||||||
|
publicKey: new PublicKey(pubkey),
|
||||||
|
accountInfo: {
|
||||||
|
data: bs58.decode(data),
|
||||||
|
executable,
|
||||||
|
owner: new PublicKey(owner),
|
||||||
|
lamports,
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
.filter(({ accountInfo }: any) => {
|
||||||
|
// TODO: remove this check once mainnet is updated
|
||||||
|
return filters.every((filter) => {
|
||||||
|
if (filter.dataSize) {
|
||||||
|
return accountInfo.data.length === filter.dataSize;
|
||||||
|
} else if (filter.memcmp) {
|
||||||
|
let filterBytes = bs58.decode(filter.memcmp.bytes);
|
||||||
|
return accountInfo.data
|
||||||
|
.slice(
|
||||||
|
filter.memcmp.offset,
|
||||||
|
filter.memcmp.offset + filterBytes.length
|
||||||
|
)
|
||||||
|
.equals(filterBytes);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.map(({ publicKey, accountInfo }: any) => {
|
||||||
|
return { publicKey, account: parseTokenAccountData(accountInfo.data) };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ACCOUNT_LAYOUT = BufferLayout.struct([
|
||||||
|
BufferLayout.blob(32, "mint"),
|
||||||
|
BufferLayout.blob(32, "owner"),
|
||||||
|
BufferLayout.nu64("amount"),
|
||||||
|
BufferLayout.blob(93),
|
||||||
|
]);
|
||||||
|
|
||||||
|
export const MINT_LAYOUT = BufferLayout.struct([
|
||||||
|
BufferLayout.blob(44),
|
||||||
|
BufferLayout.u8("decimals"),
|
||||||
|
BufferLayout.blob(37),
|
||||||
|
]);
|
||||||
|
|
||||||
|
export function parseTokenAccountData(data: Buffer) {
|
||||||
|
// @ts-ignore
|
||||||
|
let { mint, owner, amount } = ACCOUNT_LAYOUT.decode(data);
|
||||||
|
return {
|
||||||
|
mint: new PublicKey(mint),
|
||||||
|
owner: new PublicKey(owner),
|
||||||
|
amount: new BN(amount),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseMintData(data: Buffer) {
|
||||||
|
// @ts-ignore
|
||||||
|
let { decimals } = MINT_LAYOUT.decode(data);
|
||||||
|
return { decimals };
|
||||||
|
}
|
||||||
|
|
||||||
|
function getOwnedAccountsFilters(publicKey: PublicKey) {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
memcmp: {
|
||||||
|
// @ts-ignore
|
||||||
|
offset: ACCOUNT_LAYOUT.offsetOf("owner"),
|
||||||
|
bytes: publicKey.toBase58(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
dataSize: ACCOUNT_LAYOUT.span,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
|
@ -18,7 +18,8 @@
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
"jsx": "react-jsx"
|
"jsx": "react-jsx",
|
||||||
|
"typeRoots": ["types/", "node_modules/@types"]
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"src"
|
"src"
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
declare module 'buffer-layout' {
|
||||||
|
// TODO: remove `any`.
|
||||||
|
export class Layout<T = any> {
|
||||||
|
span: number;
|
||||||
|
property?: string;
|
||||||
|
|
||||||
|
constructor(span: number, property?: string);
|
||||||
|
|
||||||
|
decode(b: Buffer, offset?: number): T;
|
||||||
|
encode(src: T, b: Buffer, offset?: number): number;
|
||||||
|
getSpan(b: Buffer, offset?: number): number;
|
||||||
|
replicate(name: string): this;
|
||||||
|
}
|
||||||
|
// TODO: remove any.
|
||||||
|
export class Structure<T = any> extends Layout<T> {
|
||||||
|
span: any;
|
||||||
|
}
|
||||||
|
export function greedy(
|
||||||
|
elementSpan?: number,
|
||||||
|
property?: string,
|
||||||
|
): Layout<number>;
|
||||||
|
export function offset<T>(
|
||||||
|
layout: Layout<T>,
|
||||||
|
offset?: number,
|
||||||
|
property?: string,
|
||||||
|
): Layout<T>;
|
||||||
|
export function u8(property?: string): Layout<number>;
|
||||||
|
export function u16(property?: string): Layout<number>;
|
||||||
|
export function u24(property?: string): Layout<number>;
|
||||||
|
export function u32(property?: string): Layout<number>;
|
||||||
|
export function u40(property?: string): Layout<number>;
|
||||||
|
export function u48(property?: string): Layout<number>;
|
||||||
|
export function nu64(property?: string): Layout<number>;
|
||||||
|
export function u16be(property?: string): Layout<number>;
|
||||||
|
export function u24be(property?: string): Layout<number>;
|
||||||
|
export function u32be(property?: string): Layout<number>;
|
||||||
|
export function u40be(property?: string): Layout<number>;
|
||||||
|
export function u48be(property?: string): Layout<number>;
|
||||||
|
export function nu64be(property?: string): Layout<number>;
|
||||||
|
export function s8(property?: string): Layout<number>;
|
||||||
|
export function s16(property?: string): Layout<number>;
|
||||||
|
export function s24(property?: string): Layout<number>;
|
||||||
|
export function s32(property?: string): Layout<number>;
|
||||||
|
export function s40(property?: string): Layout<number>;
|
||||||
|
export function s48(property?: string): Layout<number>;
|
||||||
|
export function ns64(property?: string): Layout<number>;
|
||||||
|
export function s16be(property?: string): Layout<number>;
|
||||||
|
export function s24be(property?: string): Layout<number>;
|
||||||
|
export function s32be(property?: string): Layout<number>;
|
||||||
|
export function s40be(property?: string): Layout<number>;
|
||||||
|
export function s48be(property?: string): Layout<number>;
|
||||||
|
export function ns64be(property?: string): Layout<number>;
|
||||||
|
export function f32(property?: string): Layout<number>;
|
||||||
|
export function f32be(property?: string): Layout<number>;
|
||||||
|
export function f64(property?: string): Layout<number>;
|
||||||
|
export function f64be(property?: string): Layout<number>;
|
||||||
|
export function struct<T>(
|
||||||
|
fields: Layout<any>[],
|
||||||
|
property?: string,
|
||||||
|
decodePrefixes?: boolean,
|
||||||
|
): Layout<T>;
|
||||||
|
export function bits(
|
||||||
|
word: Layout<number>,
|
||||||
|
msb?: boolean,
|
||||||
|
property?: string,
|
||||||
|
): any;
|
||||||
|
export function seq<T>(
|
||||||
|
elementLayout: Layout<T>,
|
||||||
|
count: number | Layout<number>,
|
||||||
|
property?: string,
|
||||||
|
): Layout<T[]>;
|
||||||
|
export function union(
|
||||||
|
discr: Layout<any>,
|
||||||
|
defaultLayout?: any,
|
||||||
|
property?: string,
|
||||||
|
): any;
|
||||||
|
export function unionLayoutDiscriminator(
|
||||||
|
layout: Layout<any>,
|
||||||
|
property?: string,
|
||||||
|
): any;
|
||||||
|
export function blob(
|
||||||
|
length: number | Layout<number>,
|
||||||
|
property?: string,
|
||||||
|
): Layout<Buffer>;
|
||||||
|
export function cstr(property?: string): Layout<string>;
|
||||||
|
export function utf8(maxSpan: number, property?: string): Layout<string>;
|
||||||
|
}
|
Loading…
Reference in New Issue