Only wrap as much SOL as we need

This commit is contained in:
Gary Wang 2020-09-01 08:04:59 -07:00
parent df24a103ec
commit f2021c0e6e
1 changed files with 15 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import {
Account, Account,
AccountInfo, AccountInfo,
Connection, Connection,
LAMPORTS_PER_SOL,
PublicKey, PublicKey,
SystemProgram, SystemProgram,
Transaction, Transaction,
@ -306,13 +307,24 @@ export class Market {
(side === 'sell' && this.baseMintAddress.equals(WRAPPED_SOL_MINT)) (side === 'sell' && this.baseMintAddress.equals(WRAPPED_SOL_MINT))
) { ) {
wrappedSolAccount = new Account(); wrappedSolAccount = new Account();
let lamports;
if (side === 'buy') {
lamports = Math.round(price * size * 1.01 * LAMPORTS_PER_SOL);
if (openOrdersAccounts.length > 0) {
lamports -= openOrdersAccounts[0].quoteTokenFree.toNumber();
}
} else {
lamports = Math.round(size * LAMPORTS_PER_SOL);
if (openOrdersAccounts.length > 0) {
lamports -= openOrdersAccounts[0].baseTokenFree.toNumber();
}
}
lamports = Math.max(lamports, 0) + 1e7;
transaction.add( transaction.add(
SystemProgram.createAccount({ SystemProgram.createAccount({
fromPubkey: ownerAddress, fromPubkey: ownerAddress,
newAccountPubkey: wrappedSolAccount.publicKey, newAccountPubkey: wrappedSolAccount.publicKey,
lamports: lamports,
// TODO: calculate the amount we need and use that instead
(await connection.getBalance(ownerAddress, 'recent')) - 100000,
space: 165, space: 165,
programId: TOKEN_PROGRAM_ID, programId: TOKEN_PROGRAM_ID,
}), }),