diff --git a/web3.js/examples/budget-timestamp.js b/web3.js/examples/budget-timestamp.js index 8d1fa8c802..b70bd68248 100644 --- a/web3.js/examples/budget-timestamp.js +++ b/web3.js/examples/budget-timestamp.js @@ -13,7 +13,7 @@ const contractState = new solanaWeb3.Account(); let url; url = 'http://localhost:8899'; -const connection = new solanaWeb3.Connection(url); +const connection = new solanaWeb3.Connection(url, 'recent'); function showBalance() { console.log(`\n== Account State`); @@ -50,9 +50,9 @@ function confirmTransaction(signature) { } function airDrop() { - console.log(`\n== Requesting airdrop of 100 to ${account1.publicKey}`); + console.log(`\n== Requesting airdrop of 100000 to ${account1.publicKey}`); return connection - .requestAirdrop(account1.publicKey, 100) + .requestAirdrop(account1.publicKey, 100000) .then(confirmTransaction); } diff --git a/web3.js/examples/budget-two-approvers.js b/web3.js/examples/budget-two-approvers.js index a9e679fd0f..5d50279a96 100644 --- a/web3.js/examples/budget-two-approvers.js +++ b/web3.js/examples/budget-two-approvers.js @@ -16,7 +16,7 @@ const approver2 = new solanaWeb3.Account(); let url; url = 'http://localhost:8899'; //url = 'http://testnet.solana.com:8899'; -const connection = new solanaWeb3.Connection(url); +const connection = new solanaWeb3.Connection(url, 'recent'); function getTransactionFee() { return connection.getRecentBlockhash().then(response => { @@ -59,7 +59,7 @@ function confirmTransaction(signature) { } function airDrop(feeCalculator) { - const airdrop = 100 + 3 * feeCalculator.targetLamportsPerSignature; + const airdrop = 100 + 5 * feeCalculator.targetLamportsPerSignature; console.log(`\n== Requesting airdrop of ${airdrop} to ${account1.publicKey}`); return connection .requestAirdrop(account1.publicKey, airdrop) diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 57f91cc57a..bab7e4cc44 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -47,6 +47,8 @@ declare module '@solana/web3.js' { /* TODO */ // === src/connection.js === + declare export type Commitment = 'max' | 'recent'; + declare export type AccountInfo = { executable: boolean, owner: PublicKey, @@ -104,28 +106,39 @@ declare module '@solana/web3.js' { }; declare export class Connection { - constructor(endpoint: string): Connection; - getAccountInfo(publicKey: PublicKey): Promise; + constructor(endpoint: string, commitment: ?Commitment): Connection; + getAccountInfo( + publicKey: PublicKey, + commitment: ?Commitment, + ): Promise; getProgramAccounts( programId: PublicKey, + commitment: ?Commitment, ): Promise>; - getBalance(publicKey: PublicKey): Promise; + getBalance(publicKey: PublicKey, commitment: ?Commitment): Promise; getClusterNodes(): Promise>; - getVoteAccounts(): Promise; - confirmTransaction(signature: TransactionSignature): Promise; - getSlot(): Promise; - getSlotLeader(): Promise; + getVoteAccounts(commitment: ?Commitment): Promise; + confirmTransaction( + signature: TransactionSignature, + commitment: ?Commitment, + ): Promise; + getSlot(commitment: ?Commitment): Promise; + getSlotLeader(commitment: ?Commitment): Promise; getSignatureStatus( signature: TransactionSignature, + commitment: ?Commitment, ): Promise; - getTransactionCount(): Promise; - getTotalSupply(): Promise; - getInflation(): Promise; + getTransactionCount(commitment: ?Commitment): Promise; + getTotalSupply(commitment: ?Commitment): Promise; + getInflation(commitment: ?Commitment): Promise; getEpochSchedule(): Promise; - getRecentBlockhash(): Promise<[Blockhash, FeeCalculator]>; + getRecentBlockhash( + commitment: ?Commitment, + ): Promise<[Blockhash, FeeCalculator]>; requestAirdrop( to: PublicKey, amount: number, + commitment: ?Commitment, ): Promise; sendTransaction( transaction: Transaction, @@ -287,10 +300,17 @@ declare module '@solana/web3.js' { ...signers: Array ): Promise; + declare export function sendAndConfirmRecentTransaction( + connection: Connection, + transaction: Transaction, + ...signers: Array + ): Promise; + // === src/util/send-and-confirm-raw-transaction.js === declare export function sendAndConfirmRawTransaction( connection: Connection, wireTransaction: Buffer, + commitment: ?Commitment, ): Promise; // === src/util/testnet.js === diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index 4a8ac2b1a8..ee59088d82 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -18,6 +18,15 @@ import type {TransactionSignature} from './transaction'; type RpcRequest = (methodName: string, args: Array) => any; +/** + * The level of commitment desired when querying state + * 'max': Query the most recent block which has reached max voter lockout + * 'recent': Query the most recent block + * + * @typedef {'max' | 'recent'} Commitment + */ +export type Commitment = 'max' | 'recent'; + /** * Information describing a cluster node * @@ -488,6 +497,7 @@ export class Connection { _rpcWebSocket: RpcWebSocketClient; _rpcWebSocketConnected: boolean = false; + _commitment: ?Commitment; _blockhashInfo: { recentBlockhash: Blockhash | null, seconds: number, @@ -505,11 +515,13 @@ export class Connection { * Establish a JSON RPC connection * * @param endpoint URL to the fullnode JSON RPC endpoint + * @param commitment optional default commitment level */ - constructor(endpoint: string) { + constructor(endpoint: string, commitment: ?Commitment) { let url = urlParse(endpoint); this._rpcRequest = createRpcRequest(url.href); + this._commitment = commitment; this._blockhashInfo = { recentBlockhash: null, seconds: -1, @@ -542,10 +554,12 @@ export class Connection { /** * Fetch the balance for the specified public key */ - async getBalance(publicKey: PublicKey): Promise { - const unsafeRes = await this._rpcRequest('getBalance', [ - publicKey.toBase58(), - ]); + async getBalance( + publicKey: PublicKey, + commitment: ?Commitment, + ): Promise { + const args = this._argsWithCommitment([publicKey.toBase58()], commitment); + const unsafeRes = await this._rpcRequest('getBalance', args); const res = GetBalanceRpcResult(unsafeRes); if (res.error) { throw new Error(res.error.message); @@ -557,10 +571,12 @@ export class Connection { /** * Fetch all the account info for the specified public key */ - async getAccountInfo(publicKey: PublicKey): Promise { - const unsafeRes = await this._rpcRequest('getAccountInfo', [ - publicKey.toBase58(), - ]); + async getAccountInfo( + publicKey: PublicKey, + commitment: ?Commitment, + ): Promise { + const args = this._argsWithCommitment([publicKey.toBase58()], commitment); + const unsafeRes = await this._rpcRequest('getAccountInfo', args); const res = GetAccountInfoRpcResult(unsafeRes); if (res.error) { throw new Error(res.error.message); @@ -582,10 +598,10 @@ export class Connection { */ async getProgramAccounts( programId: PublicKey, + commitment: ?Commitment, ): Promise> { - const unsafeRes = await this._rpcRequest('getProgramAccounts', [ - programId.toBase58(), - ]); + const args = this._argsWithCommitment([programId.toBase58()], commitment); + const unsafeRes = await this._rpcRequest('getProgramAccounts', args); const res = GetProgramAccountsRpcResult(unsafeRes); if (res.error) { throw new Error(res.error.message); @@ -610,8 +626,12 @@ export class Connection { /** * Confirm the transaction identified by the specified signature */ - async confirmTransaction(signature: TransactionSignature): Promise { - const unsafeRes = await this._rpcRequest('confirmTransaction', [signature]); + async confirmTransaction( + signature: TransactionSignature, + commitment: ?Commitment, + ): Promise { + const args = this._argsWithCommitment([signature], commitment); + const unsafeRes = await this._rpcRequest('confirmTransaction', args); const res = ConfirmTransactionRpcResult(unsafeRes); if (res.error) { throw new Error(res.error.message); @@ -654,8 +674,9 @@ export class Connection { /** * Return the list of nodes that are currently participating in the cluster */ - async getVoteAccounts(): Promise { - const unsafeRes = await this._rpcRequest('getVoteAccounts', []); + async getVoteAccounts(commitment: ?Commitment): Promise { + const args = this._argsWithCommitment([], commitment); + const unsafeRes = await this._rpcRequest('getVoteAccounts', args); const res = GetVoteAccounts(unsafeRes); //const res = unsafeRes; if (res.error) { @@ -668,8 +689,9 @@ export class Connection { /** * Fetch the current slot that the node is processing */ - async getSlot(): Promise { - const unsafeRes = await this._rpcRequest('getSlot', []); + async getSlot(commitment: ?Commitment): Promise { + const args = this._argsWithCommitment([], commitment); + const unsafeRes = await this._rpcRequest('getSlot', args); const res = GetSlot(unsafeRes); if (res.error) { throw new Error(res.error.message); @@ -681,8 +703,9 @@ export class Connection { /** * Fetch the current slot leader of the cluster */ - async getSlotLeader(): Promise { - const unsafeRes = await this._rpcRequest('getSlotLeader', []); + async getSlotLeader(commitment: ?Commitment): Promise { + const args = this._argsWithCommitment([], commitment); + const unsafeRes = await this._rpcRequest('getSlotLeader', args); const res = GetSlotLeader(unsafeRes); if (res.error) { throw new Error(res.error.message); @@ -696,8 +719,10 @@ export class Connection { */ async getSignatureStatus( signature: TransactionSignature, + commitment: ?Commitment, ): Promise { - const unsafeRes = await this._rpcRequest('getSignatureStatus', [signature]); + const args = this._argsWithCommitment([signature], commitment); + const unsafeRes = await this._rpcRequest('getSignatureStatus', args); const res = GetSignatureStatusRpcResult(unsafeRes); if (res.error) { throw new Error(res.error.message); @@ -709,8 +734,9 @@ export class Connection { /** * Fetch the current transaction count of the cluster */ - async getTransactionCount(): Promise { - const unsafeRes = await this._rpcRequest('getTransactionCount', []); + async getTransactionCount(commitment: ?Commitment): Promise { + const args = this._argsWithCommitment([], commitment); + const unsafeRes = await this._rpcRequest('getTransactionCount', args); const res = GetTransactionCountRpcResult(unsafeRes); if (res.error) { throw new Error(res.error.message); @@ -722,8 +748,9 @@ export class Connection { /** * Fetch the current total currency supply of the cluster in lamports */ - async getTotalSupply(): Promise { - const unsafeRes = await this._rpcRequest('getTotalSupply', []); + async getTotalSupply(commitment: ?Commitment): Promise { + const args = this._argsWithCommitment([], commitment); + const unsafeRes = await this._rpcRequest('getTotalSupply', args); const res = GetTotalSupplyRpcResult(unsafeRes); if (res.error) { throw new Error(res.error.message); @@ -735,8 +762,9 @@ export class Connection { /** * Fetch the cluster Inflation parameters */ - async getInflation(): Promise { - const unsafeRes = await this._rpcRequest('getInflation', []); + async getInflation(commitment: ?Commitment): Promise { + const args = this._argsWithCommitment([], commitment); + const unsafeRes = await this._rpcRequest('getInflation', args); const res = GetInflationRpcResult(unsafeRes); if (res.error) { throw new Error(res.error.message); @@ -748,8 +776,9 @@ export class Connection { /** * Fetch the Epoch Info parameters */ - async getEpochInfo(): Promise { - const unsafeRes = await this._rpcRequest('getEpochInfo', []); + async getEpochInfo(commitment: ?Commitment): Promise { + const args = this._argsWithCommitment([], commitment); + const unsafeRes = await this._rpcRequest('getEpochInfo', args); const res = GetEpochInfoRpcResult(unsafeRes); if (res.error) { throw new Error(res.error.message); @@ -775,10 +804,14 @@ export class Connection { * Fetch the minimum balance needed to exempt an account of `dataLength` * size from rent */ - async getMinimumBalanceForRentExemption(dataLength: number): Promise { + async getMinimumBalanceForRentExemption( + dataLength: number, + commitment: ?Commitment, + ): Promise { + const args = this._argsWithCommitment([dataLength], commitment); const unsafeRes = await this._rpcRequest( 'getMinimumBalanceForRentExemption', - [dataLength], + args, ); const res = GetMinimumBalanceForRentExemptionRpcResult(unsafeRes); if (res.error) { @@ -792,8 +825,11 @@ export class Connection { /** * Fetch a recent blockhash from the cluster */ - async getRecentBlockhash(): Promise { - const unsafeRes = await this._rpcRequest('getRecentBlockhash', []); + async getRecentBlockhash( + commitment: ?Commitment, + ): Promise { + const args = this._argsWithCommitment([], commitment); + const unsafeRes = await this._rpcRequest('getRecentBlockhash', args); // Legacy v0.16 response. TODO: Remove in September 2019 try { @@ -823,17 +859,15 @@ export class Connection { async requestAirdrop( to: PublicKey, amount: number, + commitment: ?Commitment, ): Promise { - const unsafeRes = await this._rpcRequest('requestAirdrop', [ - to.toBase58(), - amount, - ]); + const args = this._argsWithCommitment([to.toBase58(), amount], commitment); + const unsafeRes = await this._rpcRequest('requestAirdrop', args); const res = RequestAirdropRpcResult(unsafeRes); if (res.error) { throw new Error(res.error.message); } assert(typeof res.result !== 'undefined'); - await sleep(500); return res.result; } @@ -1170,4 +1204,12 @@ export class Connection { throw new Error(`Unknown account change id: ${id}`); } } + + _argsWithCommitment(args: Array, override: ?Commitment): Array { + const commitment = override || this._commitment; + if (commitment) { + args.push({commitment}); + } + return args; + } } 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 ec405fca65..e1c0c6df1a 100644 --- a/web3.js/src/util/send-and-confirm-raw-transaction.js +++ b/web3.js/src/util/send-and-confirm-raw-transaction.js @@ -1,6 +1,7 @@ // @flow import {Connection} from '../connection'; +import type {Commitment} from '../connection'; import {sleep} from './sleep'; import type {TransactionSignature} from '../transaction'; import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from '../timing'; @@ -11,6 +12,7 @@ import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from '../timing'; export async function sendAndConfirmRawTransaction( connection: Connection, rawTransaction: Buffer, + commitment: ?Commitment, ): Promise { const start = Date.now(); let signature = await connection.sendRawTransaction(rawTransaction); @@ -19,7 +21,7 @@ export async function sendAndConfirmRawTransaction( let status = null; let statusRetries = 6; for (;;) { - status = await connection.getSignatureStatus(signature); + status = await connection.getSignatureStatus(signature, commitment); if (status) { break; } diff --git a/web3.js/src/util/send-and-confirm-transaction.js b/web3.js/src/util/send-and-confirm-transaction.js index 48d16b6a3f..f3ed5a3122 100644 --- a/web3.js/src/util/send-and-confirm-transaction.js +++ b/web3.js/src/util/send-and-confirm-transaction.js @@ -3,12 +3,29 @@ import invariant from 'assert'; import {Connection} from '../connection'; +import type {Commitment} from '../connection'; import {Transaction} from '../transaction'; import {sleep} from './sleep'; import type {Account} from '../account'; import type {TransactionSignature} from '../transaction'; import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from '../timing'; +/** + * Sign, send and confirm a transaction with recent commitment level + */ +export async function sendAndConfirmRecentTransaction( + connection: Connection, + transaction: Transaction, + ...signers: Array +): Promise { + return await _sendAndConfirmTransaction( + connection, + transaction, + signers, + 'recent', + ); +} + /** * Sign, send and confirm a transaction */ @@ -16,6 +33,15 @@ export async function sendAndConfirmTransaction( connection: Connection, transaction: Transaction, ...signers: Array +): Promise { + return await _sendAndConfirmTransaction(connection, transaction, signers); +} + +async function _sendAndConfirmTransaction( + connection: Connection, + transaction: Transaction, + signers: Array, + commitment: ?Commitment, ): Promise { let sendRetries = 10; let signature; @@ -27,7 +53,7 @@ export async function sendAndConfirmTransaction( let status = null; let statusRetries = 6; for (;;) { - status = await connection.getSignatureStatus(signature); + status = await connection.getSignatureStatus(signature, commitment); if (status) { break; } diff --git a/web3.js/test/bpf-loader.test.js b/web3.js/test/bpf-loader.test.js index b69c5bd0dc..3eeeb065e4 100644 --- a/web3.js/test/bpf-loader.test.js +++ b/web3.js/test/bpf-loader.test.js @@ -27,7 +27,7 @@ test('load BPF C program', async () => { const data = await fs.readFile('test/fixtures/noop-c/noop.so'); - const connection = new Connection(url); + const connection = new Connection(url, 'recent'); const [, feeCalculator] = await connection.getRecentBlockhash(); const fees = feeCalculator.lamportsPerSignature * @@ -52,7 +52,7 @@ test('load BPF Rust program', async () => { 'test/fixtures/noop-rust/solana_bpf_rust_noop.so', ); - const connection = new Connection(url); + const connection = new Connection(url, 'recent'); const [, feeCalculator] = await connection.getRecentBlockhash(); const fees = feeCalculator.lamportsPerSignature * diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index 95863cfd6c..59b6715f2b 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -15,7 +15,7 @@ import {url} from './url'; import {sleep} from '../src/util/sleep'; if (!mockRpcEnabled) { - // The default of 5 seconds is too slow for live testing sometimes + // Testing max commitment level takes around 20s to complete jest.setTimeout(30000); } @@ -51,7 +51,7 @@ test('get program accounts', async () => { return; } - const connection = new Connection(url); + const connection = new Connection(url, 'recent'); const account0 = new Account(); const account1 = new Account(); const programId = new Account(); @@ -171,18 +171,18 @@ test('get inflation', async () => { }); test('get epoch info', async () => { - const connection = new Connection(url); + const connection = new Connection(url, 'recent'); mockRpc.push([ url, { method: 'getEpochInfo', - params: [], + params: [{commitment: 'recent'}], }, { error: null, result: { - epoch: 1, + epoch: 0, slotIndex: 1, slotsInEpoch: 8192, absoluteSlot: 1, @@ -194,7 +194,7 @@ test('get epoch info', async () => { for (const key of ['epoch', 'slotIndex', 'slotsInEpoch', 'absoluteSlot']) { expect(epochInfo).toHaveProperty(key); - expect(epochInfo[key]).toBeGreaterThan(0); + expect(epochInfo[key]).toBeGreaterThanOrEqual(0); } }); @@ -434,13 +434,13 @@ test('get recent blockhash', async () => { test('request airdrop', async () => { const account = new Account(); - const connection = new Connection(url); + const connection = new Connection(url, 'recent'); mockRpc.push([ url, { method: 'requestAirdrop', - params: [account.publicKey.toBase58(), 40], + params: [account.publicKey.toBase58(), 40, {commitment: 'recent'}], }, { error: null, @@ -452,7 +452,7 @@ test('request airdrop', async () => { url, { method: 'requestAirdrop', - params: [account.publicKey.toBase58(), 2], + params: [account.publicKey.toBase58(), 2, {commitment: 'recent'}], }, { error: null, @@ -464,7 +464,7 @@ test('request airdrop', async () => { url, { method: 'getBalance', - params: [account.publicKey.toBase58()], + params: [account.publicKey.toBase58(), {commitment: 'recent'}], }, { error: null, @@ -482,7 +482,7 @@ test('request airdrop', async () => { url, { method: 'getAccountInfo', - params: [account.publicKey.toBase58()], + params: [account.publicKey.toBase58(), {commitment: 'recent'}], }, { error: null, @@ -534,16 +534,54 @@ test('request airdrop', async () => { expect(accountInfo.owner).toEqual(SystemProgram.programId); }); -test('transaction', async () => { - const accountFrom = new Account(); - const accountTo = new Account(); - const connection = new Connection(url); +// expected to take around 20s +test('request airdrop - max commitment', async () => { + const account = new Account(); + const connection = new Connection(url, 'max'); mockRpc.push([ url, { method: 'requestAirdrop', - params: [accountFrom.publicKey.toBase58(), 100010], + params: [account.publicKey.toBase58(), 40, {commitment: 'max'}], + }, + { + error: null, + result: + '1WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', + }, + ]); + mockRpc.push([ + url, + { + method: 'getBalance', + params: [account.publicKey.toBase58(), {commitment: 'max'}], + }, + { + error: null, + result: 40, + }, + ]); + + await connection.requestAirdrop(account.publicKey, 40); + const balance = await connection.getBalance(account.publicKey); + expect(balance).toBe(40); +}); + +test('transaction', async () => { + const accountFrom = new Account(); + const accountTo = new Account(); + const connection = new Connection(url, 'recent'); + + mockRpc.push([ + url, + { + method: 'requestAirdrop', + params: [ + accountFrom.publicKey.toBase58(), + 100010, + {commitment: 'recent'}, + ], }, { error: null, @@ -555,7 +593,7 @@ test('transaction', async () => { url, { method: 'getBalance', - params: [accountFrom.publicKey.toBase58()], + params: [accountFrom.publicKey.toBase58(), {commitment: 'recent'}], }, { error: null, @@ -569,7 +607,7 @@ test('transaction', async () => { url, { method: 'requestAirdrop', - params: [accountTo.publicKey.toBase58(), 21], + params: [accountTo.publicKey.toBase58(), 21, {commitment: 'recent'}], }, { error: null, @@ -581,7 +619,7 @@ test('transaction', async () => { url, { method: 'getBalance', - params: [accountTo.publicKey.toBase58()], + params: [accountTo.publicKey.toBase58(), {commitment: 'recent'}], }, { error: null, @@ -591,7 +629,7 @@ test('transaction', async () => { await connection.requestAirdrop(accountTo.publicKey, 21); expect(await connection.getBalance(accountTo.publicKey)).toBe(21); - mockGetRecentBlockhash(); + mockGetRecentBlockhash('recent'); mockRpc.push([ url, { @@ -617,6 +655,7 @@ test('transaction', async () => { method: 'confirmTransaction', params: [ '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', + {commitment: 'recent'}, ], }, { @@ -642,6 +681,7 @@ test('transaction', async () => { method: 'getSignatureStatus', params: [ '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', + {commitment: 'recent'}, ], }, { @@ -657,7 +697,7 @@ test('transaction', async () => { url, { method: 'getBalance', - params: [accountFrom.publicKey.toBase58()], + params: [accountFrom.publicKey.toBase58(), {commitment: 'recent'}], }, { error: null, @@ -674,7 +714,7 @@ test('transaction', async () => { url, { method: 'getBalance', - params: [accountTo.publicKey.toBase58()], + params: [accountTo.publicKey.toBase58(), {commitment: 'recent'}], }, { error: null, @@ -692,7 +732,7 @@ test('multi-instruction transaction', async () => { const accountFrom = new Account(); const accountTo = new Account(); - const connection = new Connection(url); + const connection = new Connection(url, 'recent'); await connection.requestAirdrop(accountFrom.publicKey, SOL_LAMPORTS); expect(await connection.getBalance(accountFrom.publicKey)).toBe(SOL_LAMPORTS); @@ -743,7 +783,7 @@ test('account change notification', async () => { return; } - const connection = new Connection(url); + const connection = new Connection(url, 'recent'); const owner = new Account(); const programAccount = new Account(); @@ -755,11 +795,16 @@ test('account change notification', async () => { ); await connection.requestAirdrop(owner.publicKey, SOL_LAMPORTS); - await Loader.load(connection, owner, programAccount, BpfLoader.programId, [ - 1, - 2, - 3, - ]); + try { + await Loader.load(connection, owner, programAccount, BpfLoader.programId, [ + 1, + 2, + 3, + ]); + } catch (err) { + await connection.removeAccountChangeListener(subscriptionId); + throw err; + } // Wait for mockCallback to receive a call let i = 0; @@ -788,7 +833,7 @@ test('program account change notification', async () => { return; } - const connection = new Connection(url); + const connection = new Connection(url, 'recent'); const owner = new Account(); const programAccount = new Account(); @@ -809,11 +854,16 @@ test('program account change notification', async () => { ); await connection.requestAirdrop(owner.publicKey, SOL_LAMPORTS); - await Loader.load(connection, owner, programAccount, BpfLoader.programId, [ - 1, - 2, - 3, - ]); + try { + await Loader.load(connection, owner, programAccount, BpfLoader.programId, [ + 1, + 2, + 3, + ]); + } catch (err) { + await connection.removeProgramAccountChangeListener(subscriptionId); + throw err; + } // Wait for mockCallback to receive a call let i = 0; diff --git a/web3.js/test/mockrpc/get-recent-blockhash.js b/web3.js/test/mockrpc/get-recent-blockhash.js index 138f17c817..c85ed41aad 100644 --- a/web3.js/test/mockrpc/get-recent-blockhash.js +++ b/web3.js/test/mockrpc/get-recent-blockhash.js @@ -1,17 +1,22 @@ // @flow import {Account} from '../../src'; +import type {Commitment} from '../../src/connection'; import {url} from '../url'; import {mockRpc} from '../__mocks__/node-fetch'; -export function mockGetRecentBlockhash() { +export function mockGetRecentBlockhash(commitment: ?Commitment) { const recentBlockhash = new Account(); + const params = []; + if (commitment) { + params.push({commitment}); + } mockRpc.push([ url, { method: 'getRecentBlockhash', - params: [], + params, }, { error: null, diff --git a/web3.js/test/transaction-payer.test.js b/web3.js/test/transaction-payer.test.js index 5b8e68e0f2..d488682a13 100644 --- a/web3.js/test/transaction-payer.test.js +++ b/web3.js/test/transaction-payer.test.js @@ -14,13 +14,17 @@ test('transaction-payer', async () => { const accountPayer = new Account(); const accountFrom = new Account(); const accountTo = new Account(); - const connection = new Connection(url); + const connection = new Connection(url, 'recent'); mockRpc.push([ url, { method: 'requestAirdrop', - params: [accountPayer.publicKey.toBase58(), SOL_LAMPORTS], + params: [ + accountPayer.publicKey.toBase58(), + SOL_LAMPORTS, + {commitment: 'recent'}, + ], }, { error: null, @@ -34,7 +38,7 @@ test('transaction-payer', async () => { url, { method: 'requestAirdrop', - params: [accountFrom.publicKey.toBase58(), 12], + params: [accountFrom.publicKey.toBase58(), 12, {commitment: 'recent'}], }, { error: null, @@ -48,7 +52,7 @@ test('transaction-payer', async () => { url, { method: 'requestAirdrop', - params: [accountTo.publicKey.toBase58(), 21], + params: [accountTo.publicKey.toBase58(), 21, {commitment: 'recent'}], }, { error: null, @@ -58,7 +62,7 @@ test('transaction-payer', async () => { ]); await connection.requestAirdrop(accountTo.publicKey, 21); - mockGetRecentBlockhash(); + mockGetRecentBlockhash('recent'); mockRpc.push([ url, { @@ -89,6 +93,7 @@ test('transaction-payer', async () => { method: 'confirmTransaction', params: [ '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', + {commitment: 'recent'}, ], }, { @@ -114,6 +119,7 @@ test('transaction-payer', async () => { method: 'getSignatureStatus', params: [ '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', + {commitment: 'recent'}, ], }, { @@ -129,7 +135,7 @@ test('transaction-payer', async () => { url, { method: 'getBalance', - params: [accountPayer.publicKey.toBase58()], + params: [accountPayer.publicKey.toBase58(), {commitment: 'recent'}], }, { error: null, @@ -148,7 +154,7 @@ test('transaction-payer', async () => { url, { method: 'getBalance', - params: [accountFrom.publicKey.toBase58()], + params: [accountFrom.publicKey.toBase58(), {commitment: 'recent'}], }, { error: null,