solana.js: fix wrap amount param

This commit is contained in:
Conner Gallagher 2022-12-12 15:47:37 -07:00
parent d38d675a04
commit 19ef0f9e6e
1 changed files with 14 additions and 16 deletions

View File

@ -395,34 +395,32 @@ export class NativeMint extends Mint {
const owner = user ? user.publicKey : payer; const owner = user ? user.publicKey : payer;
const ownerBalance = new Big(await this.connection.getBalance(owner)); const userBalance = new Big(await this.connection.getBalance(owner));
const userAddress = this.getAssociatedAddress(owner); const userTokenAddress = this.getAssociatedAddress(owner);
const userAccountInfo = await this.connection.getAccountInfo(userAddress); const userAccount = await this.getAccount(userTokenAddress);
const userAccount = const userTokenBalance =
userAccountInfo === null userAccount === null
? null ? new Big(0)
: spl.unpackAccount(userAddress, userAccountInfo); : new Big(this.fromTokenAmount(userAccount.amount));
const tokenBalance = userAccount
? new Big(this.fromTokenAmount(userAccount.amount))
: new Big(0);
let wrapAmount: Big; let wrapAmount: Big;
if ('fundUpTo' in params) { if ('fundUpTo' in params) {
if (tokenBalance.gte(params.fundUpTo)) { if (userTokenBalance.gte(params.fundUpTo)) {
return new TransactionObject(payer, [], []); return new TransactionObject(payer, [], []);
} }
wrapAmount = params.fundUpTo.sub(tokenBalance); wrapAmount = params.fundUpTo.sub(userTokenBalance);
} else if ('amount' in params) { } else if ('amount' in params) {
wrapAmount = tokenBalance.add(params.amount); wrapAmount = new Big(params.amount).mul(
new Big(10).pow(this.mint.decimals)
);
} else { } else {
throw new Error( throw new Error(
`Must specify fundUpTo or amount to perform this actions` `Must specify fundUpTo or amount to perform this actions`
); );
} }
if (ownerBalance.lte(wrapAmount)) { if (userBalance.lte(wrapAmount)) {
throw new InsufficientFundsError(); throw new InsufficientFundsError();
} }
@ -448,7 +446,7 @@ export class NativeMint extends Mint {
spl.createSyncNativeInstruction(ephemeralWallet), spl.createSyncNativeInstruction(ephemeralWallet),
spl.createTransferInstruction( spl.createTransferInstruction(
ephemeralWallet, ephemeralWallet,
userAddress, userTokenAddress,
ephemeralAccount.publicKey, ephemeralAccount.publicKey,
wrapAmountLamports wrapAmountLamports
), ),