2022-08-12 17:15:12 -07:00
|
|
|
import { AnchorProvider } from '@project-serum/anchor';
|
2022-09-27 06:13:53 -07:00
|
|
|
import {
|
|
|
|
AddressLookupTableAccount,
|
2022-10-06 02:25:09 -07:00
|
|
|
Transaction,
|
2022-09-27 06:13:53 -07:00
|
|
|
TransactionInstruction,
|
|
|
|
} from '@solana/web3.js';
|
|
|
|
import { buildVersionedTx } from '../utils';
|
2022-08-12 17:15:12 -07:00
|
|
|
|
|
|
|
export async function sendTransaction(
|
|
|
|
provider: AnchorProvider,
|
2022-09-27 06:13:53 -07:00
|
|
|
ixs: TransactionInstruction[],
|
|
|
|
alts: AddressLookupTableAccount[],
|
2022-08-12 17:15:12 -07:00
|
|
|
opts: any = {},
|
2022-09-29 06:51:09 -07:00
|
|
|
): Promise<string> {
|
2022-08-12 17:15:12 -07:00
|
|
|
const connection = provider.connection;
|
2022-09-05 14:36:42 -07:00
|
|
|
const latestBlockhash = await connection.getLatestBlockhash(
|
|
|
|
opts.preflightCommitment,
|
|
|
|
);
|
2022-10-06 02:25:09 -07:00
|
|
|
|
|
|
|
let tx: Transaction = new Transaction();
|
|
|
|
const altsEnabled = false;
|
|
|
|
if (altsEnabled) {
|
|
|
|
tx = await buildVersionedTx(provider, ixs, opts.additionalSigners, alts);
|
|
|
|
} else {
|
|
|
|
const payer = (provider as AnchorProvider).wallet;
|
|
|
|
tx = new Transaction();
|
|
|
|
tx.recentBlockhash = latestBlockhash.blockhash;
|
|
|
|
tx.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
|
|
|
|
tx.feePayer = payer.publicKey;
|
|
|
|
tx.add(...ixs);
|
|
|
|
if (opts.additionalSigners?.length > 0) {
|
|
|
|
tx.partialSign(...opts.additionalSigners);
|
|
|
|
}
|
|
|
|
await payer.signTransaction(tx);
|
|
|
|
}
|
2022-08-12 17:15:12 -07:00
|
|
|
|
2022-09-27 06:13:53 -07:00
|
|
|
const signature = await connection.sendRawTransaction(tx.serialize(), {
|
2022-08-12 17:15:12 -07:00
|
|
|
skipPreflight: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (opts.postSendTxCallback) {
|
|
|
|
try {
|
|
|
|
opts.postSendTxCallback({ txid: signature });
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(`postSendTxCallback error ${e}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 12:42:10 -07:00
|
|
|
let status: any;
|
|
|
|
if (
|
2022-09-27 06:13:53 -07:00
|
|
|
latestBlockhash.blockhash != null &&
|
|
|
|
latestBlockhash.lastValidBlockHeight != null
|
2022-08-26 12:42:10 -07:00
|
|
|
) {
|
2022-10-07 04:52:04 -07:00
|
|
|
// TODO: tyler, can we remove these?
|
2022-08-26 12:42:10 -07:00
|
|
|
console.log('confirming via blockhash');
|
|
|
|
status = (
|
|
|
|
await connection.confirmTransaction(
|
|
|
|
{
|
|
|
|
signature: signature,
|
2022-09-27 06:13:53 -07:00
|
|
|
blockhash: latestBlockhash.blockhash,
|
|
|
|
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
|
2022-08-26 12:42:10 -07:00
|
|
|
},
|
|
|
|
'processed',
|
|
|
|
)
|
|
|
|
).value;
|
|
|
|
} else {
|
2022-10-07 04:52:04 -07:00
|
|
|
// TODO: tyler, can we remove these?
|
2022-08-26 12:42:10 -07:00
|
|
|
console.log('confirming via timeout');
|
|
|
|
status = (await connection.confirmTransaction(signature, 'processed'))
|
|
|
|
.value;
|
|
|
|
}
|
2022-08-12 17:15:12 -07:00
|
|
|
|
|
|
|
if (status.err) {
|
|
|
|
throw new MangoError({
|
|
|
|
txid: signature,
|
|
|
|
message: `Transaction ${signature} failed (${JSON.stringify(status)})`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return signature;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MangoError extends Error {
|
|
|
|
message: string;
|
|
|
|
txid: string;
|
|
|
|
|
|
|
|
constructor({ txid, message }) {
|
|
|
|
super();
|
|
|
|
this.message = message;
|
|
|
|
this.txid = txid;
|
|
|
|
}
|
|
|
|
}
|