diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index 27949f9e9a..b66423c556 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -491,11 +491,32 @@ test('request airdrop', async () => { const account = new Account(); const connection = new Connection(url, 'recent'); + mockRpc.push([ + url, + { + method: 'getMinimumBalanceForRentExemption', + params: [0, {commitment: 'recent'}], + }, + { + error: null, + result: 50, + }, + ]); + + const minimumAmount = await connection.getMinimumBalanceForRentExemption( + 0, + 'recent', + ); + mockRpc.push([ url, { method: 'requestAirdrop', - params: [account.publicKey.toBase58(), 40, {commitment: 'recent'}], + params: [ + account.publicKey.toBase58(), + minimumAmount + 40, + {commitment: 'recent'}, + ], }, { error: null, @@ -527,16 +548,16 @@ test('request airdrop', async () => { context: { slot: 11, }, - value: 42, + value: minimumAmount + 42, }, }, ]); - await connection.requestAirdrop(account.publicKey, 40); + await connection.requestAirdrop(account.publicKey, minimumAmount + 40); await connection.requestAirdrop(account.publicKey, 2); const balance = await connection.getBalance(account.publicKey); - expect(balance).toBe(42); + expect(balance).toBe(minimumAmount + 42); mockRpc.push([ url, @@ -585,7 +606,7 @@ test('request airdrop', async () => { 0, 0, ], - lamports: 42, + lamports: minimumAmount + 42, data: [], executable: false, }, @@ -594,7 +615,7 @@ test('request airdrop', async () => { ]); const accountInfo = await connection.getAccountInfo(account.publicKey); - expect(accountInfo.lamports).toBe(42); + expect(accountInfo.lamports).toBe(minimumAmount + 42); expect(accountInfo.data).toHaveLength(0); expect(accountInfo.owner).toEqual(SystemProgram.programId); }); @@ -604,11 +625,32 @@ test('request airdrop - max commitment', async () => { const account = new Account(); const connection = new Connection(url, 'max'); + mockRpc.push([ + url, + { + method: 'getMinimumBalanceForRentExemption', + params: [0, {commitment: 'recent'}], + }, + { + error: null, + result: 50, + }, + ]); + + const minimumAmount = await connection.getMinimumBalanceForRentExemption( + 0, + 'recent', + ); + mockRpc.push([ url, { method: 'requestAirdrop', - params: [account.publicKey.toBase58(), 40, {commitment: 'max'}], + params: [ + account.publicKey.toBase58(), + minimumAmount + 40, + {commitment: 'max'}, + ], }, { error: null, @@ -628,14 +670,14 @@ test('request airdrop - max commitment', async () => { context: { slot: 11, }, - value: 40, + value: minimumAmount + 40, }, }, ]); - await connection.requestAirdrop(account.publicKey, 40); + await connection.requestAirdrop(account.publicKey, minimumAmount + 40); const balance = await connection.getBalance(account.publicKey); - expect(balance).toBe(40); + expect(balance).toBe(minimumAmount + 40); }); test('transaction', async () => { @@ -643,13 +685,30 @@ test('transaction', async () => { const accountTo = new Account(); const connection = new Connection(url, 'recent'); + mockRpc.push([ + url, + { + method: 'getMinimumBalanceForRentExemption', + params: [0, {commitment: 'recent'}], + }, + { + error: null, + result: 50, + }, + ]); + + const minimumAmount = await connection.getMinimumBalanceForRentExemption( + 0, + 'recent', + ); + mockRpc.push([ url, { method: 'requestAirdrop', params: [ accountFrom.publicKey.toBase58(), - 100010, + minimumAmount + 100010, {commitment: 'recent'}, ], }, @@ -671,18 +730,27 @@ test('transaction', async () => { context: { slot: 11, }, - value: 100010, + value: minimumAmount + 100010, }, }, ]); - await connection.requestAirdrop(accountFrom.publicKey, 100010); - expect(await connection.getBalance(accountFrom.publicKey)).toBe(100010); + await connection.requestAirdrop( + accountFrom.publicKey, + minimumAmount + 100010, + ); + expect(await connection.getBalance(accountFrom.publicKey)).toBe( + minimumAmount + 100010, + ); mockRpc.push([ url, { method: 'requestAirdrop', - params: [accountTo.publicKey.toBase58(), 21, {commitment: 'recent'}], + params: [ + accountTo.publicKey.toBase58(), + minimumAmount + 21, + {commitment: 'recent'}, + ], }, { error: null, @@ -702,12 +770,14 @@ test('transaction', async () => { context: { slot: 11, }, - value: 21, + value: minimumAmount + 21, }, }, ]); - await connection.requestAirdrop(accountTo.publicKey, 21); - expect(await connection.getBalance(accountTo.publicKey)).toBe(21); + await connection.requestAirdrop(accountTo.publicKey, minimumAmount + 21); + expect(await connection.getBalance(accountTo.publicKey)).toBe( + minimumAmount + 21, + ); mockGetRecentBlockhash('recent'); mockRpc.push([ @@ -790,7 +860,7 @@ test('transaction', async () => { context: { slot: 11, }, - value: 2, + value: minimumAmount + 2, }, }, ]); @@ -798,7 +868,7 @@ test('transaction', async () => { // accountFrom may have less than 100000 due to transaction fees const balance = await connection.getBalance(accountFrom.publicKey); expect(balance).toBeGreaterThan(0); - expect(balance).toBeLessThanOrEqual(100000); + expect(balance).toBeLessThanOrEqual(minimumAmount + 100000); mockRpc.push([ url, @@ -812,11 +882,13 @@ test('transaction', async () => { context: { slot: 11, }, - value: 31, + value: minimumAmount + 31, }, }, ]); - expect(await connection.getBalance(accountTo.publicKey)).toBe(31); + expect(await connection.getBalance(accountTo.publicKey)).toBe( + minimumAmount + 31, + ); }); test('multi-instruction transaction', async () => { @@ -834,8 +906,15 @@ test('multi-instruction transaction', async () => { LAMPORTS_PER_SOL, ); - await connection.requestAirdrop(accountTo.publicKey, 21); - expect(await connection.getBalance(accountTo.publicKey)).toBe(21); + const minimumAmount = await connection.getMinimumBalanceForRentExemption( + 0, + 'recent', + ); + + await connection.requestAirdrop(accountTo.publicKey, minimumAmount + 21); + expect(await connection.getBalance(accountTo.publicKey)).toBe( + minimumAmount + 21, + ); // 1. Move(accountFrom, accountTo) // 2. Move(accountTo, accountFrom) @@ -871,7 +950,9 @@ test('multi-instruction transaction', async () => { await connection.getBalance(accountFrom.publicKey), ).toBeLessThanOrEqual(LAMPORTS_PER_SOL); - expect(await connection.getBalance(accountTo.publicKey)).toBe(21); + expect(await connection.getBalance(accountTo.publicKey)).toBe( + minimumAmount + 21, + ); }); test('account change notification', async () => { diff --git a/web3.js/test/transaction-payer.test.js b/web3.js/test/transaction-payer.test.js index 645252c7ac..801b515cc9 100644 --- a/web3.js/test/transaction-payer.test.js +++ b/web3.js/test/transaction-payer.test.js @@ -16,6 +16,23 @@ test('transaction-payer', async () => { const accountTo = new Account(); const connection = new Connection(url, 'recent'); + mockRpc.push([ + url, + { + method: 'getMinimumBalanceForRentExemption', + params: [0, {commitment: 'recent'}], + }, + { + error: null, + result: 50, + }, + ]); + + const minimumAmount = await connection.getMinimumBalanceForRentExemption( + 0, + 'recent', + ); + mockRpc.push([ url, { @@ -38,7 +55,11 @@ test('transaction-payer', async () => { url, { method: 'requestAirdrop', - params: [accountFrom.publicKey.toBase58(), 12, {commitment: 'recent'}], + params: [ + accountFrom.publicKey.toBase58(), + minimumAmount + 12, + {commitment: 'recent'}, + ], }, { error: null, @@ -46,13 +67,17 @@ test('transaction-payer', async () => { '0WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', }, ]); - await connection.requestAirdrop(accountFrom.publicKey, 12); + await connection.requestAirdrop(accountFrom.publicKey, minimumAmount + 12); mockRpc.push([ url, { method: 'requestAirdrop', - params: [accountTo.publicKey.toBase58(), 21, {commitment: 'recent'}], + params: [ + accountTo.publicKey.toBase58(), + minimumAmount + 21, + {commitment: 'recent'}, + ], }, { error: null, @@ -60,7 +85,7 @@ test('transaction-payer', async () => { '8WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', }, ]); - await connection.requestAirdrop(accountTo.publicKey, 21); + await connection.requestAirdrop(accountTo.publicKey, minimumAmount + 21); mockGetRecentBlockhash('recent'); mockRpc.push([ @@ -148,12 +173,12 @@ test('transaction-payer', async () => { context: { slot: 11, }, - value: 99, + value: LAMPORTS_PER_SOL - 1, }, }, ]); - // accountPayer could be less than 100 as it paid for the transaction + // accountPayer should be less than LAMPORTS_PER_SOL as it paid for the transaction // (exact amount less depends on the current cluster fees) const balance = await connection.getBalance(accountPayer.publicKey); expect(balance).toBeGreaterThan(0); @@ -172,9 +197,11 @@ test('transaction-payer', async () => { context: { slot: 11, }, - value: 2, + value: minimumAmount + 2, }, }, ]); - expect(await connection.getBalance(accountFrom.publicKey)).toBe(2); + expect(await connection.getBalance(accountFrom.publicKey)).toBe( + minimumAmount + 2, + ); });