diff --git a/web3.js/module.d.ts b/web3.js/module.d.ts index 35ba490b2c..b86805c175 100644 --- a/web3.js/module.d.ts +++ b/web3.js/module.d.ts @@ -255,11 +255,13 @@ declare module '@solana/web3.js' { onAccountChange( publickey: PublicKey, callback: AccountChangeCallback, + commitment?: Commitment, ): number; removeAccountChangeListener(id: number): Promise; onProgramAccountChange( programId: PublicKey, callback: ProgramAccountChangeCallback, + commitment?: Commitment, ): number; removeProgramAccountChangeListener(id: number): Promise; onSlotChange(callback: SlotChangeCallback): number; @@ -267,6 +269,7 @@ declare module '@solana/web3.js' { onSignature( signature: TransactionSignature, callback: SignatureResultCallback, + commitment?: Commitment, ): number; removeSignatureListener(id: number): Promise; onRootChange(callback: RootChangeCallback): number; diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 46940d8bf9..7c55d59eac 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -268,11 +268,13 @@ declare module '@solana/web3.js' { onAccountChange( publickey: PublicKey, callback: AccountChangeCallback, + commitment: ?Commitment, ): number; removeAccountChangeListener(id: number): Promise; onProgramAccountChange( programId: PublicKey, callback: ProgramAccountChangeCallback, + commitment: ?Commitment, ): number; removeProgramAccountChangeListener(id: number): Promise; onSlotChange(callback: SlotChangeCallback): number; @@ -280,6 +282,7 @@ declare module '@solana/web3.js' { onSignature( signature: TransactionSignature, callback: SignatureResultCallback, + commitment: ?Commitment, ): number; removeSignatureListener(id: number): Promise; onRootChange(callback: RootChangeCallback): number; diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index 6a14802bd9..c3bf18f87b 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -759,6 +759,7 @@ type SubscriptionId = 'subscribing' | number; type AccountSubscriptionInfo = { publicKey: string, // PublicKey of the account as a base 58 string callback: AccountChangeCallback, + commitment: ?Commitment, subscriptionId: ?SubscriptionId, // null when there's no current server subscription id }; @@ -776,6 +777,7 @@ export type ProgramAccountChangeCallback = ( type ProgramAccountSubscriptionInfo = { programId: string, // PublicKey of the program as a base 58 string callback: ProgramAccountChangeCallback, + commitment: ?Commitment, subscriptionId: ?SubscriptionId, // null when there's no current server subscription id }; @@ -806,6 +808,7 @@ export type SignatureResultCallback = ( type SignatureSubscriptionInfo = { signature: TransactionSignature, // TransactionSignature as a base 58 string callback: SignatureResultCallback, + commitment: ?Commitment, subscriptionId: ?SubscriptionId, // null when there's no current server subscription id }; @@ -1753,12 +1756,20 @@ export class Connection { for (let id of accountKeys) { const sub = this._accountChangeSubscriptions[id]; - this._subscribe(sub, 'accountSubscribe', [sub.publicKey]); + this._subscribe( + sub, + 'accountSubscribe', + this._argsWithCommitment([sub.publicKey], sub.commitment), + ); } for (let id of programKeys) { const sub = this._programAccountChangeSubscriptions[id]; - this._subscribe(sub, 'programSubscribe', [sub.programId]); + this._subscribe( + sub, + 'programSubscribe', + this._argsWithCommitment([sub.programId], sub.commitment), + ); } for (let id of slotKeys) { @@ -1768,7 +1779,11 @@ export class Connection { for (let id of signatureKeys) { const sub = this._signatureSubscriptions[id]; - this._subscribe(sub, 'signatureSubscribe', [sub.signature]); + this._subscribe( + sub, + 'signatureSubscribe', + this._argsWithCommitment([sub.signature], sub.commitment), + ); } for (let id of rootKeys) { @@ -1810,18 +1825,21 @@ export class Connection { /** * Register a callback to be invoked whenever the specified account changes * - * @param publickey Public key of the account to monitor + * @param publicKey Public key of the account to monitor * @param callback Function to invoke whenever the account is changed + * @param commitment Specify the commitment level account changes must reach before notification * @return subscription id */ onAccountChange( publicKey: PublicKey, callback: AccountChangeCallback, + commitment: ?Commitment, ): number { const id = ++this._accountChangeSubscriptionCounter; this._accountChangeSubscriptions[id] = { publicKey: publicKey.toBase58(), callback, + commitment, subscriptionId: null, }; this._updateSubscriptions(); @@ -1887,16 +1905,19 @@ export class Connection { * * @param programId Public key of the program to monitor * @param callback Function to invoke whenever the account is changed + * @param commitment Specify the commitment level account changes must reach before notification * @return subscription id */ onProgramAccountChange( programId: PublicKey, callback: ProgramAccountChangeCallback, + commitment: ?Commitment, ): number { const id = ++this._programAccountChangeSubscriptionCounter; this._programAccountChangeSubscriptions[id] = { programId: programId.toBase58(), callback, + commitment, subscriptionId: null, }; this._updateSubscriptions(); @@ -2011,16 +2032,19 @@ export class Connection { * * @param signature Transaction signature string in base 58 * @param callback Function to invoke on signature notifications + * @param commitment Specify the commitment level signature must reach before notification * @return subscription id */ onSignature( signature: TransactionSignature, callback: SignatureResultCallback, + commitment: ?Commitment, ): number { const id = ++this._signatureSubscriptionCounter; this._signatureSubscriptions[id] = { signature, callback, + commitment, subscriptionId: null, }; this._updateSubscriptions(); diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index e16cdc0f00..e49bf75403 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -1840,6 +1840,7 @@ test('account change notification', async () => { const subscriptionId = connection.onAccountChange( programAccount.publicKey, mockCallback, + 'recent', ); const balanceNeeded = Math.max(