From 3663c01dacb51da9e9a4e999e4ed8a367e02da86 Mon Sep 17 00:00:00 2001 From: Philippe Maes Date: Tue, 15 Sep 2020 15:05:21 +0200 Subject: [PATCH] Initialize account when settling funds --- src/utils/send.js | 106 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 98 insertions(+), 8 deletions(-) diff --git a/src/utils/send.js b/src/utils/send.js index 73cea82..705b448 100644 --- a/src/utils/send.js +++ b/src/utils/send.js @@ -1,5 +1,57 @@ import { notify } from './notifications'; import { getDecimalCount, sleep } from './utils'; +import { Account, SystemProgram } from '@solana/web3.js'; +import { TokenInstructions } from '@project-serum/serum'; + +export async function createTokenAccount({ + connection, + wallet, + mintPublicKey, + onConfirmCallBack, +}) { + let newAccount = new Account(); + const transaction = SystemProgram.createAccount({ + fromPubkey: wallet.publicKey, + newAccountPubkey: newAccount.publicKey, + lamports: await connection.getMinimumBalanceForRentExemption(165), + space: 165, + programId: TokenInstructions.TOKEN_PROGRAM_ID, + }); + transaction.add( + TokenInstructions.initializeAccount({ + account: newAccount.publicKey, + mint: mintPublicKey, + owner: wallet.publicKey, + }), + ); + const onConfirm = (result) => { + if (result.timeout) { + notify({ + message: 'Timed out', + type: 'error', + description: 'Timed out awaiting confirmation on transaction', + }); + } else if (result.err) { + console.log(result.err); + notify({ message: 'Error creating account', type: 'error' }); + } else { + notify({ message: 'Account creation confirmed', type: 'success' }); + } + onConfirmCallBack && onConfirmCallBack(); + }; + const onBeforeSend = () => notify({ message: 'Creating account...' }); + const onAfterSend = () => + notify({ message: 'Account created', type: 'success' }); + return await sendTransaction({ + transaction, + wallet, + connection, + onBeforeSend: onBeforeSend, + onAfterSend: onAfterSend, + onConfirm, + signers: [wallet.publicKey, newAccount], + }); +} export async function settleFunds({ market, @@ -17,18 +69,56 @@ export async function settleFunds({ !baseCurrencyAccount || !quoteCurrencyAccount ) { - if ( - (baseCurrencyAccount && !quoteCurrencyAccount) || - (quoteCurrencyAccount && !baseCurrencyAccount) - ) { - notify({ - message: 'Add token account', - description: 'Add accounts for both currencies on sollet.io', + if (baseCurrencyAccount && !quoteCurrencyAccount) { + return await createTokenAccount({ + connection, + wallet, + mintPublicKey: market.quoteMintAddress, + onConfirmCallBack: async () => { + await sleep(1000); // wait to make sure currency account is available + const quoteCurrencyAccounts = await market.findQuoteTokenAccountsForOwner( + connection, + wallet.publicKey, + true, + ); + quoteCurrencyAccounts && + settleFunds({ + market, + openOrders, + connection, + wallet, + baseCurrencyAccount, + quoteCurrencyAccount: quoteCurrencyAccounts[0], + }); + }, + }); + } else if (quoteCurrencyAccount && !baseCurrencyAccount) { + return await createTokenAccount({ + connection, + wallet, + mintPublicKey: market.baseMintAddress, + onConfirmCallBack: async () => { + await sleep(1000); // wait to make sure currency account is available + const baseCurrencyAccounts = await market.findBaseTokenAccountsForOwner( + connection, + wallet.publicKey, + true, + ); + baseCurrencyAccounts && + settleFunds({ + market, + openOrders, + connection, + wallet, + baseCurrencyAccount: baseCurrencyAccounts[0], + quoteCurrencyAccount, + }); + }, }); } else { notify({ message: 'Not connected' }); + return; } - return; } const { transaction, signers } = await market.makeSettleFundsTransaction(