From 6783fbe8c889eea79bdf070b4b311eff7c02b58a Mon Sep 17 00:00:00 2001 From: Nathaniel Parke Date: Tue, 13 Oct 2020 10:21:34 +0800 Subject: [PATCH] Allow specifying preferred token account --- ...play.jsx => StandaloneBalancesDisplay.tsx} | 67 ++++++++++--- .../StandaloneTokenAccountSelect.tsx | 76 +++++++++++++++ .../{TradeForm.jsx => TradeForm.tsx} | 97 +++++++++++++------ src/pages/{TradePage.jsx => TradePage.tsx} | 10 +- src/utils/markets.tsx | 70 ++++++++----- src/utils/preferences.tsx | 5 +- src/utils/send.tsx | 41 ++++++-- src/utils/types.tsx | 7 ++ src/utils/utils.tsx | 59 +++++++---- src/utils/wallet.tsx | 8 +- 10 files changed, 339 insertions(+), 101 deletions(-) rename src/components/{StandaloneBalancesDisplay.jsx => StandaloneBalancesDisplay.tsx} (62%) create mode 100644 src/components/StandaloneTokenAccountSelect.tsx rename src/components/{TradeForm.jsx => TradeForm.tsx} (74%) rename src/pages/{TradePage.jsx => TradePage.tsx} (97%) diff --git a/src/components/StandaloneBalancesDisplay.jsx b/src/components/StandaloneBalancesDisplay.tsx similarity index 62% rename from src/components/StandaloneBalancesDisplay.jsx rename to src/components/StandaloneBalancesDisplay.tsx index 4dc7ab9..76d4b5f 100644 --- a/src/components/StandaloneBalancesDisplay.jsx +++ b/src/components/StandaloneBalancesDisplay.tsx @@ -1,5 +1,5 @@ -import { Button, Col, Divider, Row } from 'antd'; -import React, { useState } from 'react'; +import {Button, Col, Divider, Row} from 'antd'; +import React, {useState} from 'react'; import FloatingElement from './layout/FloatingElement'; import styled from 'styled-components'; import { @@ -8,13 +8,16 @@ import { useSelectedBaseCurrencyAccount, useSelectedOpenOrdersAccount, useSelectedQuoteCurrencyAccount, + useTokenAccounts, } from '../utils/markets'; import DepositDialog from './DepositDialog'; -import { useWallet } from '../utils/wallet'; +import {useWallet} from '../utils/wallet'; import Link from './Link'; -import { settleFunds } from '../utils/send'; -import { useSendConnection } from '../utils/connection'; -import { notify } from '../utils/notifications'; +import {settleFunds} from '../utils/send'; +import {useSendConnection} from '../utils/connection'; +import {notify} from '../utils/notifications'; +import {Balances} from "../utils/types"; +import StandaloneTokenAccountsSelect from "./StandaloneTokenAccountSelect"; const RowBox = styled(Row)` padding-bottom: 20px; @@ -36,16 +39,50 @@ export default function StandaloneBalancesDisplay() { const balances = useBalances(); const openOrdersAccount = useSelectedOpenOrdersAccount(true); const connection = useSendConnection(); - const { providerUrl, providerName, wallet } = useWallet(); + const { providerUrl, providerName, wallet, connected } = useWallet(); const [baseOrQuote, setBaseOrQuote] = useState(''); const baseCurrencyAccount = useSelectedBaseCurrencyAccount(); const quoteCurrencyAccount = useSelectedQuoteCurrencyAccount(); + const [tokenAccounts] = useTokenAccounts(); const baseCurrencyBalances = balances && balances.find((b) => b.coin === baseCurrency); const quoteCurrencyBalances = balances && balances.find((b) => b.coin === quoteCurrency); async function onSettleFunds() { + if (!market) { + notify({ + message: 'Error settling funds', + description: 'market is undefined', + type: 'error', + }); + return; + } + if (!openOrdersAccount) { + notify({ + message: 'Error settling funds', + description: 'Open orders account is undefined', + type: 'error', + }); + return; + } + if (!baseCurrencyAccount) { + notify({ + message: 'Error settling funds', + description: 'Open orders account is undefined', + type: 'error', + }); + return; + } + if (!quoteCurrencyAccount) { + notify({ + message: 'Error settling funds', + description: 'Open orders account is undefined', + type: 'error', + }); + return; + } + try { await settleFunds({ market, @@ -64,14 +101,22 @@ export default function StandaloneBalancesDisplay() { } } + const formattedBalances: [string | undefined, Balances | undefined, string, string | undefined][] = [ + [baseCurrency, baseCurrencyBalances, 'base', market?.baseMintAddress.toBase58()], + [quoteCurrency, quoteCurrencyBalances, 'quote', market?.quoteMintAddress.toBase58()], + ] + return ( - {[ - [baseCurrency, baseCurrencyBalances, 'base'], - [quoteCurrency, quoteCurrencyBalances, 'quote'], - ].map(([currency, balances, baseOrQuote], index) => ( + {formattedBalances.map(([currency, balances, baseOrQuote, mint], index) => ( {currency} + {connected && ( + account.effectiveMint.toBase58() === mint)} + mint={mint} + /> + )} 0) { + selectedValue = accounts[0].pubkey.toBase58(); + } else { + selectedValue = undefined; + } + + const setTokenAccountForCoin = (value) => { + if (!mint) { + notify({ + message: 'Error selecting token account', + description: 'Mint is undefined', + type: 'error', + }) + return; + } + const newSelectedTokenAccounts = {...selectedTokenAccounts}; + newSelectedTokenAccounts[mint] = value; + setSelectedTokenAccounts(newSelectedTokenAccounts); + } + + return ( + + + + Token account: + + + + + +