From 0222ca89545b08865bdf5c756e28bb58a673bf87 Mon Sep 17 00:00:00 2001 From: microwavedcola1 <89031858+microwavedcola1@users.noreply.github.com> Date: Thu, 1 Sep 2022 11:25:58 +0200 Subject: [PATCH] ts fixes (#207) * ts: use gpa path if rest services has not caught up yet Signed-off-by: microwavedcola1 * ts: convert wrapped sol to sol when withdrawing Signed-off-by: microwavedcola1 Signed-off-by: microwavedcola1 --- ts/client/src/accounts/group.ts | 6 +-- ts/client/src/client.ts | 50 ++++++++++++++++---- ts/client/src/debug-scripts/mb-debug-user.ts | 6 +-- ts/client/src/scripts/mb-example1-admin.ts | 2 +- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/ts/client/src/accounts/group.ts b/ts/client/src/accounts/group.ts index 24a6a22a8..e743d6254 100644 --- a/ts/client/src/accounts/group.ts +++ b/ts/client/src/accounts/group.ts @@ -114,7 +114,7 @@ export class Group { public async reloadBanks(client: MangoClient, ids?: Id) { let banks: Bank[]; - if (ids) { + if (ids && ids.getBanks().length) { banks = ( await client.program.account.bank.fetchMultiple(ids.getBanks()) ).map((account, index) => @@ -143,7 +143,7 @@ export class Group { public async reloadMintInfos(client: MangoClient, ids?: Id) { let mintInfos: MintInfo[]; - if (ids) { + if (ids && ids.getMintInfos().length) { mintInfos = ( await client.program.account.mintInfo.fetchMultiple(ids.getMintInfos()) ).map((account, index) => @@ -212,7 +212,7 @@ export class Group { public async reloadPerpMarkets(client: MangoClient, ids?: Id) { let perpMarkets: PerpMarket[]; - if (ids) { + if (ids && ids.getPerpMarkets().length) { perpMarkets = ( await client.program.account.perpMarket.fetchMultiple( ids.getPerpMarkets(), diff --git a/ts/client/src/client.ts b/ts/client/src/client.ts index 730304879..9cd674f2b 100644 --- a/ts/client/src/client.ts +++ b/ts/client/src/client.ts @@ -857,6 +857,45 @@ export class MangoClient { mangoAccount.owner, ); + const preInstructions: TransactionInstruction[] = [ + // ensure withdraws don't fail with missing ATAs + await createAssociatedTokenAccountIdempotentInstruction( + mangoAccount.owner, + mangoAccount.owner, + bank.mint, + ), + ]; + + let wrappedSolAccount: Keypair | undefined; + const postInstructions: TransactionInstruction[] = []; + const additionalSigners: Signer[] = []; + if (mintPk.equals(WRAPPED_SOL_MINT)) { + wrappedSolAccount = new Keypair(); + const lamports = nativeAmount + 1e7; + preInstructions.push( + SystemProgram.createAccount({ + fromPubkey: mangoAccount.owner, + newAccountPubkey: wrappedSolAccount.publicKey, + lamports, + space: 165, + programId: TOKEN_PROGRAM_ID, + }), + initializeAccount({ + account: wrappedSolAccount.publicKey, + mint: WRAPPED_SOL_MINT, + owner: mangoAccount.owner, + }), + ); + postInstructions.push( + closeAccount({ + source: wrappedSolAccount.publicKey, + destination: mangoAccount.owner, + owner: mangoAccount.owner, + }), + ); + additionalSigners.push(wrappedSolAccount); + } + const healthRemainingAccounts: PublicKey[] = this.buildHealthRemainingAccounts( AccountRetriever.Fixed, @@ -883,14 +922,9 @@ export class MangoClient { ({ pubkey: pk, isWritable: false, isSigner: false } as AccountMeta), ), ) - .preInstructions([ - // ensure withdraws don't fail with missing ATAs - await createAssociatedTokenAccountIdempotentInstruction( - mangoAccount.owner, - mangoAccount.owner, - bank.mint, - ), - ]) + .preInstructions(preInstructions) + .postInstructions(postInstructions) + .signers(additionalSigners) .transaction(); // .rpc({ skipPreflight: true }); diff --git a/ts/client/src/debug-scripts/mb-debug-user.ts b/ts/client/src/debug-scripts/mb-debug-user.ts index 50c2da8cf..47d856dd7 100644 --- a/ts/client/src/debug-scripts/mb-debug-user.ts +++ b/ts/client/src/debug-scripts/mb-debug-user.ts @@ -177,9 +177,9 @@ async function main() { for (const mangoAccount of mangoAccounts) { console.log(`MangoAccount ${mangoAccount.publicKey}`); - if (mangoAccount.name === '2nd Account') { - await debugUser(client, group, mangoAccount); - } + // if (mangoAccount.name === '2nd Account') { + await debugUser(client, group, mangoAccount); + // } } } diff --git a/ts/client/src/scripts/mb-example1-admin.ts b/ts/client/src/scripts/mb-example1-admin.ts index 7308c6c07..c80f204af 100644 --- a/ts/client/src/scripts/mb-example1-admin.ts +++ b/ts/client/src/scripts/mb-example1-admin.ts @@ -16,7 +16,7 @@ const MAINNET_MINTS = new Map([ ['BTC', '9n4nbM75f5Ui33ZbPYXn59EwSgE8CGsHtAeTH5YFeJ9E'], // Wrapped Bitcoin (Sollet) ['ETH', '7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs'], // Ether (Portal) ['soETH', '2FPyTwcZLUg1MDrwsyoP4D6s1tM7hAkHYRjkNb5w6Pxk'], // Wrapped Ethereum (Sollet) - ['SOL', 'So11111111111111111111111111111111111111112'], + ['SOL', 'So11111111111111111111111111111111111111112'], // Wrapped SOL ['MSOL', 'mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So'], ['MNGO', 'MangoCzJ36AjZyKwVj3VnYU4GTonjfVEnJmvvWaxLac'], ]);