2018-11-28 11:56:50 -08:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import {Connection} from '../connection';
|
|
|
|
import type {TransactionSignature} from '../transaction';
|
2020-06-03 19:55:42 +08:00
|
|
|
import type {ConfirmOptions} from '../connection';
|
2018-11-28 11:56:50 -08:00
|
|
|
|
|
|
|
/**
|
2020-05-20 17:13:21 +08:00
|
|
|
* Send and confirm a raw transaction
|
2020-06-15 18:32:57 +08:00
|
|
|
*
|
2020-09-08 13:12:47 +08:00
|
|
|
* If `commitment` option is not specified, defaults to 'max' commitment.
|
2020-06-15 18:32:57 +08:00
|
|
|
*
|
|
|
|
* @param {Connection} connection
|
|
|
|
* @param {Buffer} rawTransaction
|
|
|
|
* @param {ConfirmOptions} [options]
|
|
|
|
* @returns {Promise<TransactionSignature>}
|
2018-11-28 11:56:50 -08:00
|
|
|
*/
|
|
|
|
export async function sendAndConfirmRawTransaction(
|
|
|
|
connection: Connection,
|
|
|
|
rawTransaction: Buffer,
|
2020-06-03 19:55:42 +08:00
|
|
|
options?: ConfirmOptions,
|
2018-11-28 11:56:50 -08:00
|
|
|
): Promise<TransactionSignature> {
|
2020-06-03 19:55:42 +08:00
|
|
|
const signature = await connection.sendRawTransaction(
|
|
|
|
rawTransaction,
|
|
|
|
options,
|
|
|
|
);
|
2020-09-08 13:12:47 +08:00
|
|
|
|
2020-06-03 19:55:42 +08:00
|
|
|
const status = (
|
|
|
|
await connection.confirmTransaction(
|
|
|
|
signature,
|
2020-09-08 13:12:47 +08:00
|
|
|
options && options.commitment,
|
2020-06-03 19:55:42 +08:00
|
|
|
)
|
|
|
|
).value;
|
2018-11-28 11:56:50 -08:00
|
|
|
|
2020-09-08 13:12:47 +08:00
|
|
|
if (status.err) {
|
|
|
|
throw new Error(
|
|
|
|
`Raw transaction ${signature} failed (${JSON.stringify(status)})`,
|
|
|
|
);
|
2018-11-28 11:56:50 -08:00
|
|
|
}
|
|
|
|
|
2020-09-08 13:12:47 +08:00
|
|
|
return signature;
|
2018-11-28 11:56:50 -08:00
|
|
|
}
|