ts: Add new convenience sendAndConfirmTx function

This commit is contained in:
Christian Kamm 2023-02-03 13:55:46 +01:00
parent 6c8204e9ef
commit 3b9e8c7d2b
1 changed files with 100 additions and 173 deletions

View File

@ -104,15 +104,14 @@ export class MangoClient {
} }
/// Transactions /// Transactions
private async sendAndConfirmTransaction( public async sendAndConfirmTransaction(
ixs: TransactionInstruction[], ixs: TransactionInstruction[],
alts: AddressLookupTableAccount[] = [],
opts: any = {}, opts: any = {},
): Promise<string> { ): Promise<string> {
return await sendTransaction( return await sendTransaction(
this.program.provider as AnchorProvider, this.program.provider as AnchorProvider,
ixs, ixs,
alts, opts.alts ?? [],
{ {
postSendTxCallback: this.postSendTxCallback, postSendTxCallback: this.postSendTxCallback,
prioritizationFee: this.prioritizationFee, prioritizationFee: this.prioritizationFee,
@ -122,6 +121,17 @@ export class MangoClient {
); );
} }
private async sendAndConfirmTransactionForGroup(
group: Group,
ixs: TransactionInstruction[],
opts: any = {},
): Promise<string> {
return await this.sendAndConfirmTransaction(ixs, {
alts: group.addressLookupTablesList,
...opts,
});
}
// Group // Group
public async groupCreate( public async groupCreate(
groupNum: number, groupNum: number,
@ -164,10 +174,7 @@ export class MangoClient {
admin: (this.program.provider as AnchorProvider).wallet.publicKey, admin: (this.program.provider as AnchorProvider).wallet.publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async ixGateSet( public async ixGateSet(
@ -181,10 +188,7 @@ export class MangoClient {
admin: (this.program.provider as AnchorProvider).wallet.publicKey, admin: (this.program.provider as AnchorProvider).wallet.publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async groupClose(group: Group): Promise<TransactionSignature> { public async groupClose(group: Group): Promise<TransactionSignature> {
@ -199,10 +203,7 @@ export class MangoClient {
.publicKey, .publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async getGroup(groupPk: PublicKey): Promise<Group> { public async getGroup(groupPk: PublicKey): Promise<Group> {
@ -314,10 +315,7 @@ export class MangoClient {
rent: SYSVAR_RENT_PUBKEY, rent: SYSVAR_RENT_PUBKEY,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async tokenRegisterTrustless( public async tokenRegisterTrustless(
@ -339,10 +337,7 @@ export class MangoClient {
rent: SYSVAR_RENT_PUBKEY, rent: SYSVAR_RENT_PUBKEY,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async tokenEdit( public async tokenEdit(
@ -396,10 +391,7 @@ export class MangoClient {
} as AccountMeta, } as AccountMeta,
]) ])
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async tokenDeregister( public async tokenDeregister(
@ -443,10 +435,10 @@ export class MangoClient {
) )
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [
[...preInstructions, ix], ...preInstructions,
group.addressLookupTablesList, ix,
); ]);
} }
public async getBanksForGroup(group: Group): Promise<Bank[]> { public async getBanksForGroup(group: Group): Promise<Bank[]> {
@ -519,10 +511,7 @@ export class MangoClient {
payer: (this.program.provider as AnchorProvider).wallet.publicKey, payer: (this.program.provider as AnchorProvider).wallet.publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async stubOracleClose( public async stubOracleClose(
@ -538,10 +527,7 @@ export class MangoClient {
.publicKey, .publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async stubOracleSet( public async stubOracleSet(
@ -557,10 +543,7 @@ export class MangoClient {
oracle: oraclePk, oracle: oraclePk,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async getStubOracle( public async getStubOracle(
@ -639,10 +622,7 @@ export class MangoClient {
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async createAndFetchMangoAccount( public async createAndFetchMangoAccount(
@ -690,10 +670,7 @@ export class MangoClient {
payer: (this.program.provider as AnchorProvider).wallet.publicKey, payer: (this.program.provider as AnchorProvider).wallet.publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async editMangoAccount( public async editMangoAccount(
@ -711,10 +688,37 @@ export class MangoClient {
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix], }
group.addressLookupTablesList,
); public async computeAccountData(
group: Group,
mangoAccount: MangoAccount,
): Promise<TransactionSignature> {
const healthRemainingAccounts: PublicKey[] =
this.buildHealthRemainingAccounts(
AccountRetriever.Fixed,
group,
[mangoAccount],
[],
[],
);
const ix = await this.program.methods
.computeAccountData()
.accounts({
group: group.publicKey,
account: mangoAccount.publicKey,
})
.remainingAccounts(
healthRemainingAccounts.map(
(pk) =>
({ pubkey: pk, isWritable: false, isSigner: false } as AccountMeta),
),
)
.instruction();
return await this.sendAndConfirmTransactionForGroup(group, [ix]);
} }
public async toggleMangoAccountFreeze( public async toggleMangoAccountFreeze(
@ -730,10 +734,7 @@ export class MangoClient {
admin: (this.program.provider as AnchorProvider).wallet.publicKey, admin: (this.program.provider as AnchorProvider).wallet.publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async getMangoAccount( public async getMangoAccount(
@ -908,10 +909,7 @@ export class MangoClient {
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async emptyAndCloseMangoAccount( public async emptyAndCloseMangoAccount(
@ -983,10 +981,7 @@ export class MangoClient {
.instruction(); .instruction();
instructions.push(closeIx); instructions.push(closeIx);
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, instructions);
[...instructions],
group.addressLookupTablesList,
);
} }
public async tokenDeposit( public async tokenDeposit(
@ -1082,9 +1077,9 @@ export class MangoClient {
) )
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(
group,
[...preInstructions, ix, ...postInstructions], [...preInstructions, ix, ...postInstructions],
group.addressLookupTablesList,
{ additionalSigners }, { additionalSigners },
); );
} }
@ -1105,10 +1100,7 @@ export class MangoClient {
allowBorrow, allowBorrow,
); );
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, ixes);
[...ixes],
group.addressLookupTablesList,
);
} }
public async tokenWithdrawNativeIx( public async tokenWithdrawNativeIx(
@ -1204,10 +1196,7 @@ export class MangoClient {
allowBorrow, allowBorrow,
healthAccountsToExclude, healthAccountsToExclude,
); );
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, ixs);
ixs,
group.addressLookupTablesList,
);
} }
// Serum // Serum
@ -1232,10 +1221,7 @@ export class MangoClient {
payer: (this.program.provider as AnchorProvider).wallet.publicKey, payer: (this.program.provider as AnchorProvider).wallet.publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async serum3deregisterMarket( public async serum3deregisterMarket(
@ -1263,10 +1249,7 @@ export class MangoClient {
.publicKey, .publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async serum3GetMarkets( public async serum3GetMarkets(
@ -1334,10 +1317,7 @@ export class MangoClient {
payer: (this.program.provider as AnchorProvider).wallet.publicKey, payer: (this.program.provider as AnchorProvider).wallet.publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async serum3CreateOpenOrdersIx( public async serum3CreateOpenOrdersIx(
@ -1571,10 +1551,10 @@ export class MangoClient {
externalMarketPk, externalMarketPk,
); );
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [
[...placeOrderIxes, settleIx], ...placeOrderIxes,
group.addressLookupTablesList, settleIx,
); ]);
} }
public async serum3CancelAllOrders( public async serum3CancelAllOrders(
@ -1608,10 +1588,7 @@ export class MangoClient {
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async serum3SettleFundsIx( public async serum3SettleFundsIx(
@ -1674,10 +1651,7 @@ export class MangoClient {
externalMarketPk, externalMarketPk,
); );
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async serum3CancelOrderIx( public async serum3CancelOrderIx(
@ -1732,10 +1706,7 @@ export class MangoClient {
this.serum3SettleFundsIx(group, mangoAccount, externalMarketPk), this.serum3SettleFundsIx(group, mangoAccount, externalMarketPk),
]); ]);
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, ixes);
ixes,
group.addressLookupTablesList,
);
} }
/// perps /// perps
@ -1856,9 +1827,9 @@ export class MangoClient {
newAccountPubkey: eventQueue.publicKey, newAccountPubkey: eventQueue.publicKey,
}), }),
]; ];
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(
group,
[...preInstructions, ix], [...preInstructions, ix],
group.addressLookupTablesList,
{ {
additionalSigners: [bids, asks, eventQueue], additionalSigners: [bids, asks, eventQueue],
}, },
@ -1912,10 +1883,7 @@ export class MangoClient {
perpMarket: perpMarket.publicKey, perpMarket: perpMarket.publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async perpCloseMarket( public async perpCloseMarket(
@ -1937,10 +1905,7 @@ export class MangoClient {
.publicKey, .publicKey,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async perpGetMarkets(group: Group): Promise<PerpMarket[]> { public async perpGetMarkets(group: Group): Promise<PerpMarket[]> {
@ -2002,10 +1967,7 @@ export class MangoClient {
mangoAccount, mangoAccount,
perpMarketIndex, perpMarketIndex,
); );
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async perpZeroOutForMarket( public async perpZeroOutForMarket(
@ -2023,10 +1985,7 @@ export class MangoClient {
admin: group.admin, admin: group.admin,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
// perpPlaceOrder ix returns an optional, custom order id, // perpPlaceOrder ix returns an optional, custom order id,
@ -2061,10 +2020,7 @@ export class MangoClient {
limit, limit,
); );
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async perpPlaceOrderIx( public async perpPlaceOrderIx(
@ -2155,10 +2111,7 @@ export class MangoClient {
limit, limit,
); );
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async perpPlaceOrderPeggedIx( public async perpPlaceOrderPeggedIx(
@ -2254,10 +2207,7 @@ export class MangoClient {
orderId, orderId,
); );
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async perpCancelAllOrders( public async perpCancelAllOrders(
@ -2273,10 +2223,7 @@ export class MangoClient {
limit, limit,
); );
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async perpCancelAllOrdersIx( public async perpCancelAllOrdersIx(
@ -2338,10 +2285,7 @@ export class MangoClient {
) )
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
async perpSettleFees( async perpSettleFees(
@ -2378,10 +2322,7 @@ export class MangoClient {
) )
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async perpConsumeEvents( public async perpConsumeEvents(
@ -2405,10 +2346,7 @@ export class MangoClient {
), ),
) )
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async perpConsumeAllEvents( public async perpConsumeAllEvents(
@ -2603,14 +2541,15 @@ export class MangoClient {
]) ])
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(
group,
[ [
...preInstructions, ...preInstructions,
flashLoanBeginIx, flashLoanBeginIx,
...userDefinedInstructions.filter((ix) => ix.keys.length > 2), ...userDefinedInstructions.filter((ix) => ix.keys.length > 2),
flashLoanEndIx, flashLoanEndIx,
], ],
[...group.addressLookupTablesList, ...userDefinedAlts], { alts: [...group.addressLookupTablesList, ...userDefinedAlts] },
); );
} }
@ -2637,10 +2576,7 @@ export class MangoClient {
} as AccountMeta, } as AccountMeta,
]) ])
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
/// liquidations /// liquidations
@ -2690,10 +2626,7 @@ export class MangoClient {
.remainingAccounts(parsedHealthAccounts) .remainingAccounts(parsedHealthAccounts)
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async altSet( public async altSet(
@ -2710,10 +2643,7 @@ export class MangoClient {
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async altExtend( public async altExtend(
@ -2731,10 +2661,7 @@ export class MangoClient {
addressLookupTable, addressLookupTable,
}) })
.instruction(); .instruction();
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(group, [ix]);
[ix],
group.addressLookupTablesList,
);
} }
public async healthRegionBeginIx( public async healthRegionBeginIx(
@ -3057,9 +2984,9 @@ export class MangoClient {
]); ]);
transactionInstructions.push(cancelOrderIx, placeOrderIx); transactionInstructions.push(cancelOrderIx, placeOrderIx);
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(
group,
transactionInstructions, transactionInstructions,
group.addressLookupTablesList,
); );
} }
public async modifySerum3Order( public async modifySerum3Order(
@ -3100,9 +3027,9 @@ export class MangoClient {
]); ]);
transactionInstructions.push(cancelOrderIx, settleIx, ...placeOrderIx); transactionInstructions.push(cancelOrderIx, settleIx, ...placeOrderIx);
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransactionForGroup(
group,
transactionInstructions, transactionInstructions,
group.addressLookupTablesList,
); );
} }
} }