Cache fee discount key in local storage (#56)

* Cache fee discount key in local storage

* fix
This commit is contained in:
Nathaniel Parke 2020-12-18 18:37:39 -08:00 committed by GitHub
parent cdcc8c6f5c
commit 98fac2e158
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 61 additions and 22 deletions

View File

@ -9,7 +9,7 @@
"@project-serum/associated-token": "0.1.0", "@project-serum/associated-token": "0.1.0",
"@project-serum/awesome-serum": "1.0.1", "@project-serum/awesome-serum": "1.0.1",
"@project-serum/pool": "^0.2.0", "@project-serum/pool": "^0.2.0",
"@project-serum/serum": "^0.13.14", "@project-serum/serum": "^0.13.15",
"@project-serum/sol-wallet-adapter": "^0.1.1", "@project-serum/sol-wallet-adapter": "^0.1.1",
"@solana/web3.js": "0.86.1", "@solana/web3.js": "0.86.1",
"@testing-library/jest-dom": "^4.2.4", "@testing-library/jest-dom": "^4.2.4",

View File

@ -10,7 +10,7 @@ import {
getSelectedTokenAccountForMint, getSelectedTokenAccountForMint,
MarketProvider, MarketProvider,
useBalances, useBalances,
useCustomMarkets, useCustomMarkets, useLocallyStoredFeeDiscountKey,
useMarket, useMarket,
useTokenAccounts, useTokenAccounts,
} from '../utils/markets'; } from '../utils/markets';
@ -181,6 +181,7 @@ function ConvertFormSubmit({
const balances = useBalances(); const balances = useBalances();
const [fromAmount, setFromAmount] = useState<number | undefined>(); const [fromAmount, setFromAmount] = useState<number | undefined>();
const [toAmount, setToAmount] = useState<number | undefined>(); const [toAmount, setToAmount] = useState<number | undefined>();
const { storedFeeDiscountKey: feeDiscountKey } = useLocallyStoredFeeDiscountKey();
const connection = useConnection(); const connection = useConnection();
const sendConnection = useSendConnection(); const sendConnection = useSendConnection();
@ -278,6 +279,7 @@ function ConvertFormSubmit({
wallet, wallet,
baseCurrencyAccount: baseCurrencyAccount?.pubkey, baseCurrencyAccount: baseCurrencyAccount?.pubkey,
quoteCurrencyAccount: quoteCurrencyAccount?.pubkey, quoteCurrencyAccount: quoteCurrencyAccount?.pubkey,
feeDiscountPubkey: feeDiscountKey,
}); });
} catch (e) { } catch (e) {
console.warn(e); console.warn(e);

View File

@ -8,7 +8,7 @@ import {
useMarkPrice, useMarkPrice,
useSelectedOpenOrdersAccount, useSelectedOpenOrdersAccount,
useSelectedBaseCurrencyAccount, useSelectedBaseCurrencyAccount,
useSelectedQuoteCurrencyAccount, useSelectedQuoteCurrencyAccount, useFeeDiscountKeys, useLocallyStoredFeeDiscountKey,
} from '../utils/markets'; } from '../utils/markets';
import { useWallet } from '../utils/wallet'; import { useWallet } from '../utils/wallet';
import { notify } from '../utils/notifications'; import { notify } from '../utils/notifications';
@ -63,6 +63,8 @@ export default function TradeForm({
const { wallet, connected } = useWallet(); const { wallet, connected } = useWallet();
const sendConnection = useSendConnection(); const sendConnection = useSendConnection();
const markPrice = useMarkPrice(); const markPrice = useMarkPrice();
useFeeDiscountKeys();
const { storedFeeDiscountKey: feeDiscountKey } = useLocallyStoredFeeDiscountKey();
const [postOnly, setPostOnly] = useState(false); const [postOnly, setPostOnly] = useState(false);
const [ioc, setIoc] = useState(false); const [ioc, setIoc] = useState(false);
@ -100,23 +102,27 @@ export default function TradeForm({
useEffect(() => { useEffect(() => {
const warmUpCache = async () => { const warmUpCache = async () => {
try {
if (!wallet || !wallet.publicKey || !market) { if (!wallet || !wallet.publicKey || !market) {
console.log(`Skipping refreshing accounts`); console.log(`Skipping refreshing accounts`);
return; return;
} }
const startTime = getUnixTs(); const startTime = getUnixTs();
console.log(`Refreshing accounts for ${market.address}`); console.log(`Refreshing accounts for ${market.address}`);
await market.findOpenOrdersAccountsForOwner( await market?.findOpenOrdersAccountsForOwner(
sendConnection, sendConnection,
wallet.publicKey, wallet.publicKey,
); );
await market.findBestFeeDiscountKey(sendConnection, wallet.publicKey); await market?.findBestFeeDiscountKey(sendConnection, wallet.publicKey);
const endTime = getUnixTs(); const endTime = getUnixTs();
console.log( console.log(
`Finished refreshing accounts for ${market.address} after ${ `Finished refreshing accounts for ${market.address} after ${
endTime - startTime endTime - startTime
}`, }`,
); );
} catch (e) {
console.log(`Encountered error when refreshing trading accounts: ${e}`);
}
}; };
warmUpCache(); warmUpCache();
const id = setInterval(warmUpCache, 30_000); const id = setInterval(warmUpCache, 30_000);
@ -246,6 +252,7 @@ export default function TradeForm({
wallet, wallet,
baseCurrencyAccount: baseCurrencyAccount?.pubkey, baseCurrencyAccount: baseCurrencyAccount?.pubkey,
quoteCurrencyAccount: quoteCurrencyAccount?.pubkey, quoteCurrencyAccount: quoteCurrencyAccount?.pubkey,
feeDiscountPubkey: feeDiscountKey
}); });
refreshCache(tuple('getTokenAccounts', wallet, connected)); refreshCache(tuple('getTokenAccounts', wallet, connected));
setPrice(undefined); setPrice(undefined);

View File

@ -568,6 +568,17 @@ export function useTrades(limit = 100) {
})); }));
} }
export function useLocallyStoredFeeDiscountKey(): {
storedFeeDiscountKey: PublicKey | undefined,
setStoredFeeDiscountKey: (key: string) => void
} {
const [storedFeeDiscountKey, setStoredFeeDiscountKey] = useLocalStorageState<string>(`feeDiscountKey`, undefined);
return {
storedFeeDiscountKey: storedFeeDiscountKey ? new PublicKey(storedFeeDiscountKey) : undefined,
setStoredFeeDiscountKey
}
}
export function useFeeDiscountKeys(): [ export function useFeeDiscountKeys(): [
( (
| { | {
@ -584,14 +595,19 @@ export function useFeeDiscountKeys(): [
const { market } = useMarket(); const { market } = useMarket();
const { connected, wallet } = useWallet(); const { connected, wallet } = useWallet();
const connection = useConnection(); const connection = useConnection();
async function getFeeDiscountKeys() { const { setStoredFeeDiscountKey } = useLocallyStoredFeeDiscountKey();
let getFeeDiscountKeys = async () => {
if (!connected) { if (!connected) {
return null; return null;
} }
if (!market) { if (!market) {
return null; return null;
} }
return await market.findFeeDiscountKeys(connection, wallet.publicKey); const feeDiscountKey = await market.findFeeDiscountKeys(connection, wallet.publicKey);
if (feeDiscountKey) {
setStoredFeeDiscountKey(feeDiscountKey[0].pubkey.toBase58())
}
return feeDiscountKey;
} }
return useAsyncData( return useAsyncData(
getFeeDiscountKeys, getFeeDiscountKeys,

View File

@ -331,6 +331,7 @@ export async function placeOrder({
wallet, wallet,
baseCurrencyAccount, baseCurrencyAccount,
quoteCurrencyAccount, quoteCurrencyAccount,
feeDiscountPubkey = undefined,
}: { }: {
side: 'buy' | 'sell'; side: 'buy' | 'sell';
price: number; price: number;
@ -341,6 +342,7 @@ export async function placeOrder({
wallet: Wallet; wallet: Wallet;
baseCurrencyAccount: PublicKey | undefined; baseCurrencyAccount: PublicKey | undefined;
quoteCurrencyAccount: PublicKey | undefined; quoteCurrencyAccount: PublicKey | undefined;
feeDiscountPubkey: PublicKey | undefined;
}) { }) {
let formattedMinOrderSize = let formattedMinOrderSize =
market?.minOrderSize?.toFixed(getDecimalCount(market.minOrderSize)) || market?.minOrderSize?.toFixed(getDecimalCount(market.minOrderSize)) ||
@ -437,6 +439,7 @@ export async function placeOrder({
price, price,
size, size,
orderType, orderType,
feeDiscountPubkey: feeDiscountPubkey || null,
}; };
console.log(params); console.log(params);

View File

@ -36,6 +36,7 @@ export function WalletProvider({ children }) {
console.log('trying to connect'); console.log('trying to connect');
wallet.on('connect', () => { wallet.on('connect', () => {
console.log('connected'); console.log('connected');
localStorage.removeItem('feeDiscountKey')
setConnected(true); setConnected(true);
let walletPublicKey = wallet.publicKey.toBase58(); let walletPublicKey = wallet.publicKey.toBase58();
let keyToDisplay = let keyToDisplay =
@ -56,6 +57,7 @@ export function WalletProvider({ children }) {
message: 'Wallet update', message: 'Wallet update',
description: 'Disconnected from wallet', description: 'Disconnected from wallet',
}); });
localStorage.removeItem('feeDiscountKey')
}); });
return () => { return () => {
wallet.disconnect(); wallet.disconnect();

View File

@ -1582,7 +1582,16 @@
bn.js "^5.1.2" bn.js "^5.1.2"
buffer-layout "^1.2.0" buffer-layout "^1.2.0"
"@project-serum/serum@^0.13.14", "@project-serum/serum@^0.13.8": "@project-serum/serum@^0.13.15":
version "0.13.15"
resolved "https://registry.yarnpkg.com/@project-serum/serum/-/serum-0.13.15.tgz#a7a0330fbf4c077ee0cc9d3ddec64a6bf443cceb"
integrity sha512-CIBSXqTKjNqa7VH2n5692sh7gGrKRcbYhPZnh3NHfsv84JxcoachF7v2diPQPDZNE4owCfRMyb3jQNGFNAf9zA==
dependencies:
"@solana/web3.js" "0.86.1"
bn.js "^5.1.2"
buffer-layout "^1.2.0"
"@project-serum/serum@^0.13.8":
version "0.13.14" version "0.13.14"
resolved "https://registry.yarnpkg.com/@project-serum/serum/-/serum-0.13.14.tgz#f39e6d3d1aaab811fbec17a304c926522e477299" resolved "https://registry.yarnpkg.com/@project-serum/serum/-/serum-0.13.14.tgz#f39e6d3d1aaab811fbec17a304c926522e477299"
integrity sha512-a/Irc8L+K2rOsJcl2xRKWJtc8WCqf6O4iWhlOOKhI+e0mTEvRUmecXn0yzqnUIUwgRmtavmirJAzXFnjCpbKww== integrity sha512-a/Irc8L+K2rOsJcl2xRKWJtc8WCqf6O4iWhlOOKhI+e0mTEvRUmecXn0yzqnUIUwgRmtavmirJAzXFnjCpbKww==