diff --git a/web3.js/module.d.ts b/web3.js/module.d.ts index 86fdb5b93e..abc70c5d80 100644 --- a/web3.js/module.d.ts +++ b/web3.js/module.d.ts @@ -48,6 +48,10 @@ declare module '@solana/web3.js' { export type Commitment = 'max' | 'recent'; + export type SignatureStatusConfig = { + searchTransactionHistory: boolean; + }; + export type SignatureStatus = { slot: number; err: TransactionError | null; @@ -160,6 +164,7 @@ declare module '@solana/web3.js' { export class Connection { constructor(endpoint: string, commitment?: Commitment); + commitment?: Commitment; getAccountInfoAndContext( publicKey: PublicKey, commitment?: Commitment, @@ -192,11 +197,11 @@ declare module '@solana/web3.js' { getSlotLeader(commitment?: Commitment): Promise; getSignatureStatus( signature: TransactionSignature, - commitment?: Commitment, + config?: SignatureStatusConfig, ): Promise>; getSignatureStatuses( signatures: Array, - commitment?: Commitment, + config?: SignatureStatusConfig, ): Promise>>; getTransactionCount(commitment?: Commitment): Promise; getTotalSupply(commitment?: Commitment): Promise; diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 6484ccd8ab..7ee0b66453 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -61,6 +61,10 @@ declare module '@solana/web3.js' { declare export type Commitment = 'max' | 'recent'; + declare export type SignatureStatusConfig = { + searchTransactionHistory: boolean, + }; + declare export type SignatureStatus = { slot: number, err: TransactionError | null, @@ -173,6 +177,7 @@ declare module '@solana/web3.js' { declare export class Connection { constructor(endpoint: string, commitment: ?Commitment): Connection; + commitment: ?Commitment; getAccountInfoAndContext( publicKey: PublicKey, commitment: ?Commitment, @@ -205,11 +210,11 @@ declare module '@solana/web3.js' { getSlotLeader(commitment: ?Commitment): Promise; getSignatureStatus( signature: TransactionSignature, - commitment: ?Commitment, + config: ?SignatureStatusConfig, ): Promise>; getSignatureStatuses( signatures: Array, - commitment: ?Commitment, + config: ?SignatureStatusConfig, ): Promise>>; getTransactionCount(commitment: ?Commitment): Promise; getTotalSupply(commitment: ?Commitment): Promise; diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index e541ed779e..5547e83eac 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -96,6 +96,16 @@ function notificationResultAndContext(resultDescription: any) { */ export type Commitment = 'max' | 'recent'; +/** + * Configuration object for changing query behavior + * + * @typedef {Object} SignatureStatusConfig + * @property {boolean} searchTransactionHistory enable searching status history, not needed for recent transactions + */ +export type SignatureStatusConfig = { + searchTransactionHistory: boolean, +}; + /** * Information describing a cluster node * @@ -829,6 +839,13 @@ export class Connection { ); } + /** + * The default commitment used for requests + */ + get commitment(): ?Commitment { + return this._commitment; + } + /** * Fetch the balance for the specified public key, return with context */ @@ -1033,25 +1050,28 @@ export class Connection { */ async getSignatureStatus( signature: TransactionSignature, - commitment: ?Commitment, + config: ?SignatureStatusConfig, ): Promise> { const {context, value} = await this.getSignatureStatuses( [signature], - commitment, + config, ); assert(value.length === 1); return {context, value: value[0]}; } /** - * Fetch the current status of a signature + * Fetch the current statuses of a batch of signatures */ async getSignatureStatuses( signatures: Array, - commitment: ?Commitment, + config: ?SignatureStatusConfig, ): Promise>> { - const args = this._argsWithCommitment([signatures], commitment); - const unsafeRes = await this._rpcRequest('getSignatureStatuses', args); + const params = [signatures]; + if (config) { + params.push(config); + } + const unsafeRes = await this._rpcRequest('getSignatureStatuses', params); const res = GetSignatureStatusesRpcResult(unsafeRes); if (res.error) { throw new Error(res.error.message); diff --git a/web3.js/src/util/send-and-confirm-raw-transaction.js b/web3.js/src/util/send-and-confirm-raw-transaction.js index 3d980dc6f9..7171fa7e93 100644 --- a/web3.js/src/util/send-and-confirm-raw-transaction.js +++ b/web3.js/src/util/send-and-confirm-raw-transaction.js @@ -15,15 +15,20 @@ export async function sendAndConfirmRawTransaction( commitment: ?Commitment, ): Promise { const start = Date.now(); + const statusCommitment = commitment || connection.commitment || 'max'; let signature = await connection.sendRawTransaction(rawTransaction); // Wait up to a couple slots for a confirmation let status = null; let statusRetries = 6; for (;;) { - status = (await connection.getSignatureStatus(signature, commitment)).value; + status = (await connection.getSignatureStatus(signature)).value; if (status) { - break; + if (statusCommitment === 'max' && status.confirmations === null) { + break; + } else if (statusCommitment === 'recent') { + break; + } } // Sleep for approximately half a slot diff --git a/web3.js/src/util/send-and-confirm-transaction.js b/web3.js/src/util/send-and-confirm-transaction.js index b0ee04b70e..a01b22ed4f 100644 --- a/web3.js/src/util/send-and-confirm-transaction.js +++ b/web3.js/src/util/send-and-confirm-transaction.js @@ -43,8 +43,11 @@ async function _sendAndConfirmTransaction( signers: Array, commitment: ?Commitment, ): Promise { + const statusCommitment = commitment || connection.commitment || 'max'; + let sendRetries = 10; let signature; + for (;;) { const start = Date.now(); signature = await connection.sendTransaction(transaction, ...signers); @@ -53,10 +56,13 @@ async function _sendAndConfirmTransaction( let status = null; let statusRetries = 6; for (;;) { - status = (await connection.getSignatureStatus(signature, commitment)) - .value; + status = (await connection.getSignatureStatus(signature)).value; if (status) { - break; + if (statusCommitment === 'max' && status.confirmations === null) { + break; + } else if (statusCommitment === 'recent') { + break; + } } if (--statusRetries <= 0) { diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index f89ab87cce..decf23ecea 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -109,7 +109,6 @@ test('get program accounts', async () => { [ '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', ], - {commitment: 'recent'}, ], }, { @@ -154,7 +153,6 @@ test('get program accounts', async () => { [ '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', ], - {commitment: 'recent'}, ], }, { @@ -905,7 +903,6 @@ test('request airdrop - max commitment', async () => { [ '1WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', ], - {commitment: 'recent'}, ], }, { @@ -926,7 +923,7 @@ test('request airdrop - max commitment', async () => { }, ]); - const {value} = await connection.getSignatureStatus(signature, 'recent'); + const {value} = await connection.getSignatureStatus(signature); if (value === null) { expect(value).not.toBeNull(); return; @@ -1050,7 +1047,6 @@ test('transaction failure', async () => { [ '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', ], - {commitment: 'recent'}, ], }, { @@ -1249,7 +1245,6 @@ test('transaction', async () => { [ '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', ], - {commitment: 'recent'}, ], }, { @@ -1297,7 +1292,6 @@ test('transaction', async () => { '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', unprocessedSignature, ], - {commitment: 'recent'}, ], }, { diff --git a/web3.js/test/transaction-payer.test.js b/web3.js/test/transaction-payer.test.js index 629d87e79c..445b1e066e 100644 --- a/web3.js/test/transaction-payer.test.js +++ b/web3.js/test/transaction-payer.test.js @@ -151,7 +151,6 @@ test('transaction-payer', async () => { [ '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', ], - {commitment: 'recent'}, ], }, {