diff --git a/web3.js/examples/budget-common.js b/web3.js/examples/budget-common.js index 549eece15a..d55b2fbcf9 100644 --- a/web3.js/examples/budget-common.js +++ b/web3.js/examples/budget-common.js @@ -6,7 +6,7 @@ function getTransactionFee(connection) { return connection.getRecentBlockhash().then(response => { - return response[1]; + return response.feeCalculator; }); } diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index 8d6389cd50..4e5a3cd40e 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -1,6 +1,7 @@ // @flow import assert from 'assert'; +import bs58 from 'bs58'; import {parse as urlParse, format as urlFormat} from 'url'; import fetch from 'node-fetch'; import jayson from 'jayson/lib/client/browser'; @@ -131,7 +132,7 @@ type VoteAccountStatus = { */ const GetInflationResult = struct({ foundation: 'number', - foundation_term: 'number', + foundationTerm: 'number', initial: 'number', storage: 'number', taper: 'number', @@ -167,11 +168,11 @@ const GetEpochInfoResult = struct({ * @property {number} first_normal_slot */ const GetEpochScheduleResult = struct({ - slots_per_epoch: 'number', - leader_schedule_slot_offset: 'number', + slotsPerEpoch: 'number', + leaderScheduleSlotOffset: 'number', warmup: 'boolean', - first_normal_epoch: 'number', - first_normal_slot: 'number', + firstNormalEpoch: 'number', + firstNormalSlot: 'number', }); /** @@ -191,13 +192,21 @@ const Version = struct({ * @property {Blockhash} blockhash Blockhash of this block * @property {Blockhash} previousBlockhash Blockhash of this block's parent * @property {number} parentSlot Slot index of this block's parent - * @property {Array>} transactions Vector of transactions paired with statuses + * @property {Array} transactions Vector of transactions and status metas */ type ConfirmedBlock = { blockhash: Blockhash, previousBlockhash: Blockhash, parentSlot: number, - transactions: Array<[Transaction, GetSignatureStatusRpcResult]>, + transactions: Array<{ + transaction: Transaction, + meta: { + fee: number, + preBalances: Array, + postBalances: Array, + status: SignatureStatusResult, + }, + }>, }; function createRpcRequest(url): RpcRequest { @@ -282,10 +291,10 @@ const GetVersionRpcResult = struct({ */ const AccountInfoResult = struct({ executable: 'boolean', - owner: 'array', + owner: 'string', lamports: 'number', - data: 'array', - rent_epoch: 'number?', + data: 'string', + rentEpoch: 'number?', }); /** @@ -306,7 +315,10 @@ const AccountNotificationResult = struct({ /** * @private */ -const ProgramAccountInfoResult = struct(['string', AccountInfoResult]); +const ProgramAccountInfoResult = struct({ + pubkey: 'string', + account: AccountInfoResult, +}); /*** * Expected JSON RPC response for the "programNotification" message @@ -441,8 +453,8 @@ export const GetConfirmedBlockRpcResult = jsonRpcResult( previousBlockhash: 'string', parentSlot: 'number', transactions: struct.array([ - struct.tuple([ - struct({ + struct({ + transaction: struct({ signatures: struct.array(['string']), message: struct({ accountKeys: struct.array(['string']), @@ -464,7 +476,7 @@ export const GetConfirmedBlockRpcResult = jsonRpcResult( recentBlockhash: 'string', }), }), - struct.union([ + meta: struct.union([ 'null', struct({ status: SignatureStatusResult, @@ -473,7 +485,7 @@ export const GetConfirmedBlockRpcResult = jsonRpcResult( postBalances: struct.array(['number']), }), ]), - ]), + }), ]), }), ); @@ -481,17 +493,19 @@ export const GetConfirmedBlockRpcResult = jsonRpcResult( /** * Expected JSON RPC response for the "getRecentBlockhash" message */ -const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext([ - 'string', +const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext( struct({ - burnPercent: 'number', - lamportsPerSignature: 'number', - maxLamportsPerSignature: 'number', - minLamportsPerSignature: 'number', - targetLamportsPerSignature: 'number', - targetSignaturesPerSlot: 'number', + blockhash: 'string', + feeCalculator: struct({ + burnPercent: 'number', + lamportsPerSignature: 'number', + maxLamportsPerSignature: 'number', + minLamportsPerSignature: 'number', + targetLamportsPerSignature: 'number', + targetSignaturesPerSlot: 'number', + }), }), -]); +); /** * Expected JSON RPC response for the "requestAirdrop" message @@ -595,12 +609,18 @@ export type TransactionError = {| /** * @ignore */ -type BlockhashAndFeeCalculator = [Blockhash, FeeCalculator]; // This type exists to workaround an esdoc parse error +type BlockhashAndFeeCalculator = { + blockhash: Blockhash, + feeCalculator: FeeCalculator, +}; // This type exists to workaround an esdoc parse error /** * @ignore */ -type PublicKeyAndAccount = [PublicKey, AccountInfo]; // This type exists to workaround an esdoc parse error +type PublicKeyAndAccount = { + pubkey: PublicKey, + account: AccountInfo, +}; // This type exists to workaround an esdoc parse error /** * A connection to a fullnode JSON RPC endpoint @@ -727,7 +747,7 @@ export class Connection { executable, owner: new PublicKey(owner), lamports, - data: Buffer.from(data), + data: bs58.decode(data), }; return { @@ -770,15 +790,15 @@ export class Connection { assert(typeof result !== 'undefined'); return result.map(result => { - return [ - result[0], - { - executable: result[1].executable, - owner: new PublicKey(result[1].owner), - lamports: result[1].lamports, - data: Buffer.from(result[1].data), + return { + pubkey: result.pubkey, + account: { + executable: result.account.executable, + owner: new PublicKey(result.account.owner), + lamports: result.account.lamports, + data: bs58.decode(result.account.data), }, - ]; + }; }); } @@ -1039,7 +1059,10 @@ export class Connection { ).toString(), parentSlot: result.result.parentSlot, transactions: result.result.transactions.map(result => { - return [Transaction.fromRpcResult(result[0]), result[1]]; + return { + transaction: Transaction.fromRpcResult(result.transaction), + meta: result.meta, + }; }), }; } @@ -1066,7 +1089,7 @@ export class Connection { } const value = NonceAccount.fromAccountData( - Buffer.from(res.result.value.data), + bs58.decode(res.result.value.data), ); return { @@ -1148,14 +1171,11 @@ export class Connection { let attempts = 0; const startTime = Date.now(); for (;;) { - const [ - recentBlockhash, - //feeCalculator, - ] = await this.getRecentBlockhash(); + const {blockhash} = await this.getRecentBlockhash(); - if (this._blockhashInfo.recentBlockhash != recentBlockhash) { + if (this._blockhashInfo.recentBlockhash != blockhash) { this._blockhashInfo = { - recentBlockhash, + recentBlockhash: blockhash, seconds: new Date().getSeconds(), transactionSignatures: [], }; @@ -1343,7 +1363,7 @@ export class Connection { executable: result.executable, owner: new PublicKey(result.owner), lamports: result.lamports, - data: Buffer.from(result.data), + data: bs58.decode(result.data), }); return true; } @@ -1412,12 +1432,12 @@ export class Connection { assert(typeof result !== 'undefined'); sub.callback({ - accountId: result[0], + accountId: result.pubkey, accountInfo: { - executable: result[1].executable, - owner: new PublicKey(result[1].owner), - lamports: result[1].lamports, - data: Buffer.from(result[1].data), + executable: result.account.executable, + owner: new PublicKey(result.account.owner), + lamports: result.account.lamports, + data: bs58.decode(result.account.data), }, }); return true; diff --git a/web3.js/test/bpf-loader.test.js b/web3.js/test/bpf-loader.test.js index 67b8e3e81c..795a88e1f0 100644 --- a/web3.js/test/bpf-loader.test.js +++ b/web3.js/test/bpf-loader.test.js @@ -28,7 +28,7 @@ test('load BPF C program', async () => { const data = await fs.readFile('test/fixtures/noop-c/noop.so'); const connection = new Connection(url, 'recent'); - const [, feeCalculator] = await connection.getRecentBlockhash(); + const {feeCalculator} = await connection.getRecentBlockhash(); const fees = feeCalculator.lamportsPerSignature * (BpfLoader.getMinNumSignatures(data.length) + NUM_RETRIES); @@ -56,7 +56,7 @@ test('load BPF Rust program', async () => { ); const connection = new Connection(url, 'recent'); - const [, feeCalculator] = await connection.getRecentBlockhash(); + const {feeCalculator} = await connection.getRecentBlockhash(); const fees = feeCalculator.lamportsPerSignature * (BpfLoader.getMinNumSignatures(data.length) + NUM_RETRIES); diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index b66423c556..a618dfd863 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -65,7 +65,7 @@ test('get program accounts', async () => { transaction = SystemProgram.assign(account1.publicKey, programId.publicKey); await sendAndConfirmTransaction(connection, transaction, account1); - const [, feeCalculator] = await connection.getRecentBlockhash(); + const {feeCalculator} = await connection.getRecentBlockhash(); const programAccounts = await connection.getProgramAccounts( programId.publicKey, @@ -76,13 +76,13 @@ test('get program accounts', async () => { expect([ account0.publicKey.toBase58(), account1.publicKey.toBase58(), - ]).toEqual(expect.arrayContaining([element[0]])); - if (element[0] == account0.publicKey) { - expect(element[1].lamports).toBe( + ]).toEqual(expect.arrayContaining([element.pubkey])); + if (element.pubkey == account0.publicKey) { + expect(element.account.lamports).toBe( LAMPORTS_PER_SOL - feeCalculator.lamportsPerSignature, ); } else { - expect(element[1].lamports).toBe( + expect(element.account.lamports).toBe( 0.5 * LAMPORTS_PER_SOL - feeCalculator.lamportsPerSignature, ); } @@ -149,7 +149,7 @@ test('get inflation', async () => { error: null, result: { foundation: 0.05, - foundation_term: 7.0, + foundationTerm: 7.0, initial: 0.15, storage: 0.1, taper: 0.15, @@ -165,7 +165,7 @@ test('get inflation', async () => { 'terminal', 'taper', 'foundation', - 'foundation_term', + 'foundationTerm', 'storage', ]) { expect(inflation).toHaveProperty(key); @@ -213,10 +213,10 @@ test('get epoch schedule', async () => { { error: null, result: { - first_normal_epoch: 8, - first_normal_slot: 8160, - leader_schedule_slot_offset: 8192, - slots_per_epoch: 8192, + firstNormalEpoch: 8, + firstNormalSlot: 8160, + leaderScheduleSlotOffset: 8192, + slotsPerEpoch: 8192, warmup: true, }, }, @@ -225,10 +225,10 @@ test('get epoch schedule', async () => { const epochSchedule = await connection.getEpochSchedule(); for (const key of [ - 'first_normal_epoch', - 'first_normal_slot', - 'leader_schedule_slot_offset', - 'slots_per_epoch', + 'firstNormalEpoch', + 'firstNormalSlot', + 'leaderScheduleSlotOffset', + 'slotsPerEpoch', ]) { expect(epochSchedule).toHaveProperty(key); expect(epochSchedule[key]).toBeGreaterThan(0); @@ -449,6 +449,7 @@ test('get confirmed block', async () => { expect(block1.previousBlockhash).toBe(blockhash0); expect(block1.blockhash).not.toBeNull(); expect(block1.parentSlot).toBe(0); + expect(block1.transactions[0].transaction).not.toBeNull(); break; } x++; @@ -460,11 +461,8 @@ test('get recent blockhash', async () => { mockGetRecentBlockhash(); - const [ - recentBlockhash, - feeCalculator, - ] = await connection.getRecentBlockhash(); - expect(recentBlockhash.length).toBeGreaterThanOrEqual(43); + const {blockhash, feeCalculator} = await connection.getRecentBlockhash(); + expect(blockhash.length).toBeGreaterThanOrEqual(43); expect(feeCalculator.lamportsPerSignature).toBeGreaterThanOrEqual(0); }); @@ -572,42 +570,9 @@ test('request airdrop', async () => { slot: 11, }, value: { - owner: [ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - ], + owner: '11111111111111111111111111111111', lamports: minimumAmount + 42, - data: [], + data: '', executable: false, }, }, diff --git a/web3.js/test/mockrpc/get-recent-blockhash.js b/web3.js/test/mockrpc/get-recent-blockhash.js index 91f08b8ea5..6f686842e6 100644 --- a/web3.js/test/mockrpc/get-recent-blockhash.js +++ b/web3.js/test/mockrpc/get-recent-blockhash.js @@ -24,9 +24,9 @@ export function mockGetRecentBlockhash(commitment: ?Commitment) { context: { slot: 11, }, - value: [ - recentBlockhash.publicKey.toBase58(), - { + value: { + blockhash: recentBlockhash.publicKey.toBase58(), + feeCalculator: { lamportsPerSignature: 42, burnPercent: 50, maxLamportsPerSignature: 42, @@ -34,7 +34,7 @@ export function mockGetRecentBlockhash(commitment: ?Commitment) { targetLamportsPerSignature: 42, targetSignaturesPerSlot: 42, }, - ], + }, }, }, ]); diff --git a/web3.js/test/nonce.test.js b/web3.js/test/nonce.test.js index 4efb2a641a..6165b8d76c 100644 --- a/web3.js/test/nonce.test.js +++ b/web3.js/test/nonce.test.js @@ -113,42 +113,9 @@ test('create and query nonce account', async () => { slot: 11, }, value: { - owner: [ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - ], + owner: '11111111111111111111111111111111', lamports: minimumAmount, - data: [...expectedData], + data: bs58.encode(expectedData), executable: false, }, },