fix bignum issue when depositing billis

This commit is contained in:
Maximilian Schneider 2021-07-29 15:55:45 +02:00
parent 4b68635405
commit 2d3b3f1490
2 changed files with 21 additions and 7 deletions

View File

@ -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<WalletStore>((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<WalletStore>((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<WalletStore>((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, {

View File

@ -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())
}