2018-10-25 08:18:59 -07:00
|
|
|
import {Connection} from '../connection';
|
|
|
|
import {Transaction} from '../transaction';
|
2020-06-03 19:55:42 +08:00
|
|
|
import type {ConfirmOptions} from '../connection';
|
2021-05-07 16:59:51 +08:00
|
|
|
import type {Signer} from '../keypair';
|
2018-11-03 19:02:12 -07:00
|
|
|
import type {TransactionSignature} from '../transaction';
|
2020-04-09 15:28:02 -07:00
|
|
|
|
2018-10-06 10:36:59 -07:00
|
|
|
/**
|
2020-05-20 17:13:21 +08:00
|
|
|
* Sign, send and confirm a transaction.
|
|
|
|
*
|
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 {Transaction} transaction
|
2021-05-07 16:59:51 +08:00
|
|
|
* @param {Array<Signer>} signers
|
2020-06-15 18:32:57 +08:00
|
|
|
* @param {ConfirmOptions} [options]
|
|
|
|
* @returns {Promise<TransactionSignature>}
|
2018-10-06 10:36:59 -07:00
|
|
|
*/
|
|
|
|
export async function sendAndConfirmTransaction(
|
2019-11-11 13:01:10 -05:00
|
|
|
connection: Connection,
|
|
|
|
transaction: Transaction,
|
2021-05-07 16:59:51 +08:00
|
|
|
signers: Array<Signer>,
|
2020-06-03 19:55:42 +08:00
|
|
|
options?: ConfirmOptions,
|
2018-11-28 11:56:50 -08:00
|
|
|
): Promise<TransactionSignature> {
|
2020-11-16 09:15:51 -08:00
|
|
|
const sendOptions = options && {
|
|
|
|
skipPreflight: options.skipPreflight,
|
|
|
|
preflightCommitment: options.preflightCommitment || options.commitment,
|
|
|
|
};
|
|
|
|
|
2020-06-15 18:32:57 +08:00
|
|
|
const signature = await connection.sendTransaction(
|
|
|
|
transaction,
|
|
|
|
signers,
|
2020-11-16 09:15:51 -08:00
|
|
|
sendOptions,
|
2020-06-15 18:32:57 +08:00
|
|
|
);
|
2020-09-08 13:12:47 +08:00
|
|
|
|
2020-06-15 18:32:57 +08:00
|
|
|
const status = (
|
|
|
|
await connection.confirmTransaction(
|
|
|
|
signature,
|
2020-09-08 13:12:47 +08:00
|
|
|
options && options.commitment,
|
2020-06-15 18:32:57 +08:00
|
|
|
)
|
|
|
|
).value;
|
|
|
|
|
2020-09-08 13:12:47 +08:00
|
|
|
if (status.err) {
|
|
|
|
throw new Error(
|
|
|
|
`Transaction ${signature} failed (${JSON.stringify(status)})`,
|
|
|
|
);
|
2018-10-06 10:36:59 -07:00
|
|
|
}
|
2018-11-03 19:02:12 -07:00
|
|
|
|
2020-09-08 13:12:47 +08:00
|
|
|
return signature;
|
2018-10-06 10:36:59 -07:00
|
|
|
}
|