From 2d3b3f1490e1b7ca92d1347415df637d96ba29b9 Mon Sep 17 00:00:00 2001 From: Maximilian Schneider Date: Thu, 29 Jul 2021 15:55:45 +0200 Subject: [PATCH] fix bignum issue when depositing billis --- stores/useWalletStore.tsx | 18 +++++++++++------- utils/balance.tsx | 10 ++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/stores/useWalletStore.tsx b/stores/useWalletStore.tsx index 6cddea1..035168e 100644 --- a/stores/useWalletStore.tsx +++ b/stores/useWalletStore.tsx @@ -21,6 +21,7 @@ import { TOKEN_PROGRAM_ID } from '@solana/spl-token' import { createAssociatedTokenAccount } from '../utils/associated' import { sendTransaction } from '../utils/send' import { DEFAULT_PROVIDER } from '../utils/wallet-adapters' +import { calculateNativeAmountUnsafe } from '../utils/balance' export const ENDPOINTS: EndpointInfo[] = [ { @@ -34,8 +35,8 @@ export const ENDPOINTS: EndpointInfo[] = [ name: 'devnet', url: 'https://cache.devnet.rpcpool.com', websocket: 'https://cache.devnet.rpcpool.com', - // programId: '2oBtRS2AAQfsMxXQfg41fKFY9zjvHwSSD7G5idrCFziV', // owned by devnet key - programId: 'CRU6hX2GgtdabESgkoMswMrUdRFxHhCVYmS292VN1Nnn', // owned by governance + programId: '2oBtRS2AAQfsMxXQfg41fKFY9zjvHwSSD7G5idrCFziV', // owned by devnet key + // programId: 'CRU6hX2GgtdabESgkoMswMrUdRFxHhCVYmS292VN1Nnn', // owned by governance //poolKey: 'GvSyVjGwLBeWdURMLDmSffQPqA8g547A6TURbbBnDpa4', // governance test // poolKey: '82ndgp58GXpwuLrEc9svHFdhiEsPaZoNUEWwgc79WHqk', // already over poolKey: '5heMyYtJK1Us9Hx2w6s5rLDNj8RufeyCR1ZUJAVFLQL7', // long deposits @@ -227,7 +228,6 @@ const useWalletStore = create((set, get) => ({ current: wallet, connection: { current: connection }, } = get() - const usdcDecimals = mints[usdcVault.mint.toBase58()].decimals const redeemable = findLargestBalanceAccountForMint( mints, tokenAccounts, @@ -246,8 +246,10 @@ const useWalletStore = create((set, get) => ({ ) if (difference > 0) { - const depositAmount = new anchor.BN( - difference * Math.pow(10, usdcDecimals) + const depositAmount = calculateNativeAmountUnsafe( + mints, + usdcVault.mint, + difference ) console.log(depositAmount.toString(), 'exchangeUsdcForReemable') @@ -279,8 +281,10 @@ const useWalletStore = create((set, get) => ({ ) await sendTransaction({ transaction, wallet, connection }) } else if (difference < 0) { - const withdrawAmount = new anchor.BN( - difference * -1 * Math.pow(10, usdcDecimals) + const withdrawAmount = calculateNativeAmountUnsafe( + mints, + usdcVault.mint, + -1 * difference ) console.log(withdrawAmount.toString(), 'exchangeRedeemableForUsdc') await program.rpc.exchangeRedeemableForUsdc(withdrawAmount, { diff --git a/utils/balance.tsx b/utils/balance.tsx index 53e5e28..d970fb2 100644 --- a/utils/balance.tsx +++ b/utils/balance.tsx @@ -24,3 +24,13 @@ export function calculateSupply( const mint = mints[pk.toBase58()] return mint && fixedPointToNumber(mint.supply, mint.decimals) } + +export function calculateNativeAmountUnsafe( + mints: { [pk: string]: MintAccount }, + pk: PublicKey, + amount: number +): BN { + const mint = mints[pk.toBase58()] + const nativeAmount = amount * Math.pow(10, mint.decimals) + return new BN(nativeAmount.toString()) +}