Cache fee discount key in local storage (#56)
* Cache fee discount key in local storage * fix
This commit is contained in:
parent
cdcc8c6f5c
commit
98fac2e158
|
@ -9,7 +9,7 @@
|
|||
"@project-serum/associated-token": "0.1.0",
|
||||
"@project-serum/awesome-serum": "1.0.1",
|
||||
"@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",
|
||||
"@solana/web3.js": "0.86.1",
|
||||
"@testing-library/jest-dom": "^4.2.4",
|
||||
|
|
|
@ -10,7 +10,7 @@ import {
|
|||
getSelectedTokenAccountForMint,
|
||||
MarketProvider,
|
||||
useBalances,
|
||||
useCustomMarkets,
|
||||
useCustomMarkets, useLocallyStoredFeeDiscountKey,
|
||||
useMarket,
|
||||
useTokenAccounts,
|
||||
} from '../utils/markets';
|
||||
|
@ -181,6 +181,7 @@ function ConvertFormSubmit({
|
|||
const balances = useBalances();
|
||||
const [fromAmount, setFromAmount] = useState<number | undefined>();
|
||||
const [toAmount, setToAmount] = useState<number | undefined>();
|
||||
const { storedFeeDiscountKey: feeDiscountKey } = useLocallyStoredFeeDiscountKey();
|
||||
|
||||
const connection = useConnection();
|
||||
const sendConnection = useSendConnection();
|
||||
|
@ -278,6 +279,7 @@ function ConvertFormSubmit({
|
|||
wallet,
|
||||
baseCurrencyAccount: baseCurrencyAccount?.pubkey,
|
||||
quoteCurrencyAccount: quoteCurrencyAccount?.pubkey,
|
||||
feeDiscountPubkey: feeDiscountKey,
|
||||
});
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
|
|
|
@ -8,7 +8,7 @@ import {
|
|||
useMarkPrice,
|
||||
useSelectedOpenOrdersAccount,
|
||||
useSelectedBaseCurrencyAccount,
|
||||
useSelectedQuoteCurrencyAccount,
|
||||
useSelectedQuoteCurrencyAccount, useFeeDiscountKeys, useLocallyStoredFeeDiscountKey,
|
||||
} from '../utils/markets';
|
||||
import { useWallet } from '../utils/wallet';
|
||||
import { notify } from '../utils/notifications';
|
||||
|
@ -63,6 +63,8 @@ export default function TradeForm({
|
|||
const { wallet, connected } = useWallet();
|
||||
const sendConnection = useSendConnection();
|
||||
const markPrice = useMarkPrice();
|
||||
useFeeDiscountKeys();
|
||||
const { storedFeeDiscountKey: feeDiscountKey } = useLocallyStoredFeeDiscountKey();
|
||||
|
||||
const [postOnly, setPostOnly] = useState(false);
|
||||
const [ioc, setIoc] = useState(false);
|
||||
|
@ -100,23 +102,27 @@ export default function TradeForm({
|
|||
|
||||
useEffect(() => {
|
||||
const warmUpCache = async () => {
|
||||
if (!wallet || !wallet.publicKey || !market) {
|
||||
console.log(`Skipping refreshing accounts`);
|
||||
return;
|
||||
try {
|
||||
if (!wallet || !wallet.publicKey || !market) {
|
||||
console.log(`Skipping refreshing accounts`);
|
||||
return;
|
||||
}
|
||||
const startTime = getUnixTs();
|
||||
console.log(`Refreshing accounts for ${market.address}`);
|
||||
await market?.findOpenOrdersAccountsForOwner(
|
||||
sendConnection,
|
||||
wallet.publicKey,
|
||||
);
|
||||
await market?.findBestFeeDiscountKey(sendConnection, wallet.publicKey);
|
||||
const endTime = getUnixTs();
|
||||
console.log(
|
||||
`Finished refreshing accounts for ${market.address} after ${
|
||||
endTime - startTime
|
||||
}`,
|
||||
);
|
||||
} catch (e) {
|
||||
console.log(`Encountered error when refreshing trading accounts: ${e}`);
|
||||
}
|
||||
const startTime = getUnixTs();
|
||||
console.log(`Refreshing accounts for ${market.address}`);
|
||||
await market.findOpenOrdersAccountsForOwner(
|
||||
sendConnection,
|
||||
wallet.publicKey,
|
||||
);
|
||||
await market.findBestFeeDiscountKey(sendConnection, wallet.publicKey);
|
||||
const endTime = getUnixTs();
|
||||
console.log(
|
||||
`Finished refreshing accounts for ${market.address} after ${
|
||||
endTime - startTime
|
||||
}`,
|
||||
);
|
||||
};
|
||||
warmUpCache();
|
||||
const id = setInterval(warmUpCache, 30_000);
|
||||
|
@ -246,6 +252,7 @@ export default function TradeForm({
|
|||
wallet,
|
||||
baseCurrencyAccount: baseCurrencyAccount?.pubkey,
|
||||
quoteCurrencyAccount: quoteCurrencyAccount?.pubkey,
|
||||
feeDiscountPubkey: feeDiscountKey
|
||||
});
|
||||
refreshCache(tuple('getTokenAccounts', wallet, connected));
|
||||
setPrice(undefined);
|
||||
|
|
|
@ -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(): [
|
||||
(
|
||||
| {
|
||||
|
@ -584,14 +595,19 @@ export function useFeeDiscountKeys(): [
|
|||
const { market } = useMarket();
|
||||
const { connected, wallet } = useWallet();
|
||||
const connection = useConnection();
|
||||
async function getFeeDiscountKeys() {
|
||||
const { setStoredFeeDiscountKey } = useLocallyStoredFeeDiscountKey();
|
||||
let getFeeDiscountKeys = async () => {
|
||||
if (!connected) {
|
||||
return null;
|
||||
}
|
||||
if (!market) {
|
||||
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(
|
||||
getFeeDiscountKeys,
|
||||
|
|
|
@ -331,6 +331,7 @@ export async function placeOrder({
|
|||
wallet,
|
||||
baseCurrencyAccount,
|
||||
quoteCurrencyAccount,
|
||||
feeDiscountPubkey = undefined,
|
||||
}: {
|
||||
side: 'buy' | 'sell';
|
||||
price: number;
|
||||
|
@ -341,6 +342,7 @@ export async function placeOrder({
|
|||
wallet: Wallet;
|
||||
baseCurrencyAccount: PublicKey | undefined;
|
||||
quoteCurrencyAccount: PublicKey | undefined;
|
||||
feeDiscountPubkey: PublicKey | undefined;
|
||||
}) {
|
||||
let formattedMinOrderSize =
|
||||
market?.minOrderSize?.toFixed(getDecimalCount(market.minOrderSize)) ||
|
||||
|
@ -437,6 +439,7 @@ export async function placeOrder({
|
|||
price,
|
||||
size,
|
||||
orderType,
|
||||
feeDiscountPubkey: feeDiscountPubkey || null,
|
||||
};
|
||||
console.log(params);
|
||||
|
||||
|
|
|
@ -36,6 +36,7 @@ export function WalletProvider({ children }) {
|
|||
console.log('trying to connect');
|
||||
wallet.on('connect', () => {
|
||||
console.log('connected');
|
||||
localStorage.removeItem('feeDiscountKey')
|
||||
setConnected(true);
|
||||
let walletPublicKey = wallet.publicKey.toBase58();
|
||||
let keyToDisplay =
|
||||
|
@ -56,6 +57,7 @@ export function WalletProvider({ children }) {
|
|||
message: 'Wallet update',
|
||||
description: 'Disconnected from wallet',
|
||||
});
|
||||
localStorage.removeItem('feeDiscountKey')
|
||||
});
|
||||
return () => {
|
||||
wallet.disconnect();
|
||||
|
|
11
yarn.lock
11
yarn.lock
|
@ -1582,7 +1582,16 @@
|
|||
bn.js "^5.1.2"
|
||||
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"
|
||||
resolved "https://registry.yarnpkg.com/@project-serum/serum/-/serum-0.13.14.tgz#f39e6d3d1aaab811fbec17a304c926522e477299"
|
||||
integrity sha512-a/Irc8L+K2rOsJcl2xRKWJtc8WCqf6O4iWhlOOKhI+e0mTEvRUmecXn0yzqnUIUwgRmtavmirJAzXFnjCpbKww==
|
||||
|
|
Loading…
Reference in New Issue