diff --git a/ts/client/src/accounts/healthCache.ts b/ts/client/src/accounts/healthCache.ts index 87be5c719..6efbe6fbd 100644 --- a/ts/client/src/accounts/healthCache.ts +++ b/ts/client/src/accounts/healthCache.ts @@ -200,6 +200,23 @@ export class HealthCache { const sourceBank: Bank = group.getFirstBankByMint(sourceMintPk); const targetBank: Bank = group.getFirstBankByMint(targetMintPk); + if (sourceMintPk.equals(targetMintPk)) { + return ZERO_I80F48; + } + + if (!sourceBank.price || sourceBank.price.lte(ZERO_I80F48)) { + return ZERO_I80F48; + } + + if ( + sourceBank.initLiabWeight + .sub(targetBank.initAssetWeight) + .abs() + .lte(ZERO_I80F48) + ) { + return ZERO_I80F48; + } + // The health_ratio is a nonlinear based on swap amount. // For large swap amounts the slope is guaranteed to be negative, but small amounts // can have positive slope (e.g. using source deposits to pay back target borrows). diff --git a/ts/client/src/client.ts b/ts/client/src/client.ts index 87d6174b5..cdb26b868 100644 --- a/ts/client/src/client.ts +++ b/ts/client/src/client.ts @@ -541,14 +541,22 @@ export class MangoClient { accountNumber?: number, name?: string, ): Promise { - return await this.program.methods + const transaction = await this.program.methods .accountCreate(accountNumber ?? 0, 8, 0, 0, 0, name ?? '') .accounts({ group: group.publicKey, owner: (this.program.provider as AnchorProvider).wallet.publicKey, payer: (this.program.provider as AnchorProvider).wallet.publicKey, }) - .rpc({ skipPreflight: true }); + .transaction(); + + return await sendTransaction( + this.program.provider as AnchorProvider, + transaction, + { + postSendTxCallback: this.postSendTxCallback, + }, + ); } public async expandMangoAccount( @@ -576,14 +584,22 @@ export class MangoClient { name?: string, delegate?: PublicKey, ): Promise { - return await this.program.methods + const transaction = await this.program.methods .accountEdit(name ?? null, delegate ?? null) .accounts({ group: group.publicKey, account: mangoAccount.publicKey, owner: (this.program.provider as AnchorProvider).wallet.publicKey, }) - .rpc({ skipPreflight: true }); + .transaction(); + + return await sendTransaction( + this.program.provider as AnchorProvider, + transaction, + { + postSendTxCallback: this.postSendTxCallback, + }, + ); } public async getMangoAccount(mangoAccount: MangoAccount) { @@ -768,7 +784,7 @@ export class MangoClient { [bank], ); - return await this.program.methods + const transaction = await this.program.methods .tokenDeposit(new BN(nativeAmount)) .accounts({ group: group.publicKey, @@ -787,7 +803,16 @@ export class MangoClient { .preInstructions(preInstructions) .postInstructions(postInstructions) .signers(additionalSigners) - .rpc({ skipPreflight: true }); + .transaction(); + + return await sendTransaction( + this.program.provider as AnchorProvider, + transaction, + { + additionalSigners, + postSendTxCallback: this.postSendTxCallback, + }, + ); } public async tokenWithdraw( diff --git a/ts/client/src/debug-scripts/mb-debug-banks.ts b/ts/client/src/debug-scripts/mb-debug-banks.ts index 3a8d8f7f7..9e7bed3d4 100644 --- a/ts/client/src/debug-scripts/mb-debug-banks.ts +++ b/ts/client/src/debug-scripts/mb-debug-banks.ts @@ -2,7 +2,7 @@ import { AnchorProvider, Wallet } from '@project-serum/anchor'; import { coder } from '@project-serum/anchor/dist/cjs/spl/token'; import { Connection, Keypair } from '@solana/web3.js'; import fs from 'fs'; -import { ZERO_I80F48 } from '../accounts/I80F48'; +import { I80F48, ZERO_I80F48 } from '../accounts/I80F48'; import { MangoClient } from '../client'; import { MANGO_V4_ID } from '../constants'; @@ -69,6 +69,24 @@ async function main() { for (const bank of await Array.from(banksMapUsingTokenIndex.values()).sort( (a, b) => a.tokenIndex - b.tokenIndex, )) { + const vault = I80F48.fromNumber( + coder() + .accounts.decode( + 'token', + await ( + await client.program.provider.connection.getAccountInfo(bank.vault) + ).data, + ) + .amount.toNumber(), + ); + + const error = vault.sub( + (bank as any).indexedDepositsByMangoAccounts + .sub((bank as any).indexedBorrowsByMangoAccounts) + .add(bank.collectedFeesNative) + .add(bank.dust), + ); + let res = `${bank.name}`; res = res + @@ -76,6 +94,7 @@ async function main() { `\n ${'bank'.padEnd(40)} ${bank.publicKey}` + `\n ${'vault'.padEnd(40)} ${bank.vault}` + `\n ${'mint'.padEnd(40)} ${bank.mint}` + + `\n ${'error'.padEnd(40)} ${error}` + `\n ${'collectedFeesNative'.padEnd(40)} ${bank.collectedFeesNative}` + `\n ${'dust'.padEnd(40)} ${bank.dust}` + `\n ${'deposits'.padEnd(40)} ${bank.indexedDeposits.mul( diff --git a/ts/client/src/debug-scripts/mb-debug-user.ts b/ts/client/src/debug-scripts/mb-debug-user.ts index 66172eb81..0c9044512 100644 --- a/ts/client/src/debug-scripts/mb-debug-user.ts +++ b/ts/client/src/debug-scripts/mb-debug-user.ts @@ -91,6 +91,7 @@ async function debugUser(client, group, mangoAccount) { ); } getMaxSourceForTokenSwapWrapper('SOL', 'BTC'); + getMaxSourceForTokenSwapWrapper('USDC', 'USDC'); } async function main() { diff --git a/ts/client/src/utils/rpc.ts b/ts/client/src/utils/rpc.ts index 82f2c8c67..fa4b47a72 100644 --- a/ts/client/src/utils/rpc.ts +++ b/ts/client/src/utils/rpc.ts @@ -12,6 +12,9 @@ export async function sendTransaction( await connection.getLatestBlockhash(opts.preflightCommitment) ).blockhash; transaction.feePayer = payer.publicKey; + if (opts.additionalSigners?.length > 0) { + transaction.partialSign(...opts.additionalSigners); + } await payer.signTransaction(transaction); const rawTransaction = transaction.serialize();