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
|
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> {
|
|
|
|
const start = Date.now();
|
2020-06-03 19:55:42 +08:00
|
|
|
const signature = await connection.sendRawTransaction(
|
|
|
|
rawTransaction,
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
const status = (
|
|
|
|
await connection.confirmTransaction(
|
|
|
|
signature,
|
|
|
|
options && options.confirmations,
|
|
|
|
)
|
|
|
|
).value;
|
2018-11-28 11:56:50 -08:00
|
|
|
|
2020-05-20 17:13:21 +08:00
|
|
|
if (status) {
|
|
|
|
if (status.err) {
|
2018-11-28 11:56:50 -08:00
|
|
|
throw new Error(
|
2020-05-20 17:13:21 +08:00
|
|
|
`Raw transaction ${signature} failed (${JSON.stringify(status)})`,
|
2018-11-28 11:56:50 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return signature;
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:13:21 +08:00
|
|
|
const duration = (Date.now() - start) / 1000;
|
2019-04-23 09:53:26 -07:00
|
|
|
throw new Error(
|
2020-05-20 17:13:21 +08:00
|
|
|
`Raw transaction '${signature}' was not confirmed in ${duration.toFixed(
|
|
|
|
2,
|
|
|
|
)} seconds`,
|
2019-04-23 09:53:26 -07:00
|
|
|
);
|
2018-11-28 11:56:50 -08:00
|
|
|
}
|