mango-v4/ts/client/src/utils/rpc.ts

94 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-11-15 20:10:03 -08:00
import { AnchorProvider } from '@project-serum/anchor';
import {
AddressLookupTableAccount,
2022-11-15 20:10:03 -08:00
MessageV0,
TransactionInstruction,
2022-11-15 20:10:03 -08:00
VersionedTransaction,
} from '@solana/web3.js';
export async function sendTransaction(
provider: AnchorProvider,
ixs: TransactionInstruction[],
alts: AddressLookupTableAccount[],
opts: any = {},
): Promise<string> {
const connection = provider.connection;
const latestBlockhash = await connection.getLatestBlockhash(
opts.preflightCommitment,
);
const payer = (provider as AnchorProvider).wallet;
2022-11-15 20:10:03 -08:00
const message = MessageV0.compile({
payerKey: (provider as AnchorProvider).wallet.publicKey,
instructions: ixs,
recentBlockhash: latestBlockhash.blockhash,
addressLookupTableAccounts: alts,
});
2022-11-15 20:10:03 -08:00
const vtx = new VersionedTransaction(message);
if (opts?.additionalSigners?.length) {
vtx.sign([...opts?.additionalSigners]);
}
// if (payer instanceof Wallet) {
const tx = await payer.signTransaction(vtx as any);
// } else {
// tx.sign([((provider as AnchorProvider).wallet as any).payer as Signer]);
// }
const signature = await connection.sendTransaction(
tx as any as VersionedTransaction,
{
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 (
latestBlockhash.blockhash != null &&
latestBlockhash.lastValidBlockHeight != null
2022-08-26 12:42:10 -07:00
) {
status = (
await connection.confirmTransaction(
{
signature: signature,
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
2022-08-26 12:42:10 -07:00
},
'processed',
)
).value;
} else {
status = (await connection.confirmTransaction(signature, 'processed'))
.value;
}
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;
}
}