solana/web3.js/test/transaction-payer.test.ts

133 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-02-07 08:57:12 -08:00
import base58 from 'bs58';
2021-02-05 18:59:00 -08:00
import {expect} from 'chai';
import {
Keypair,
Connection,
Transaction,
SystemProgram,
LAMPORTS_PER_SOL,
} from '../src';
2022-08-11 02:10:11 -07:00
import invariant from '../src/utils/assert';
2021-02-05 18:59:00 -08:00
import {MOCK_PORT, url} from './url';
2021-02-07 08:57:12 -08:00
import {helpers, mockRpcResponse, mockServer} from './mocks/rpc-http';
import {stubRpcWebSocket, restoreRpcWebSocket} from './mocks/rpc-websockets';
2021-02-05 18:59:00 -08:00
describe('Transaction Payer', () => {
let connection: Connection;
beforeEach(() => {
connection = new Connection(url);
});
2021-03-14 22:08:10 -07:00
if (mockServer) {
const server = mockServer;
2021-02-05 18:59:00 -08:00
beforeEach(() => {
2021-03-14 22:08:10 -07:00
server.start(MOCK_PORT);
2021-02-05 18:59:00 -08:00
stubRpcWebSocket(connection);
});
afterEach(() => {
2021-03-14 22:08:10 -07:00
server.stop();
2021-02-05 18:59:00 -08:00
restoreRpcWebSocket(connection);
});
}
it('transaction-payer', async () => {
const accountPayer = Keypair.generate();
const accountFrom = Keypair.generate();
const accountTo = Keypair.generate();
2021-02-05 18:59:00 -08:00
await helpers.airdrop({
connection,
address: accountPayer.publicKey,
amount: LAMPORTS_PER_SOL,
});
await mockRpcResponse({
method: 'getMinimumBalanceForRentExemption',
2021-02-05 18:59:00 -08:00
params: [0],
value: 50,
});
const minimumAmount = await connection.getMinimumBalanceForRentExemption(0);
await helpers.airdrop({
connection,
address: accountFrom.publicKey,
amount: minimumAmount + 12,
});
await helpers.airdrop({
connection,
address: accountTo.publicKey,
amount: minimumAmount + 21,
});
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: accountFrom.publicKey,
toPubkey: accountTo.publicKey,
lamports: 10,
}),
);
await helpers.processTransaction({
connection,
transaction,
signers: [accountPayer, accountFrom],
commitment: 'confirmed',
2021-02-05 18:59:00 -08:00
});
2021-03-14 22:08:10 -07:00
invariant(transaction.signature);
2021-02-05 18:59:00 -08:00
const signature = base58.encode(transaction.signature);
await mockRpcResponse({
method: 'getSignatureStatuses',
2021-02-05 18:59:00 -08:00
params: [[signature]],
value: [
{
slot: 0,
confirmations: 11,
status: {Ok: null},
err: null,
2020-03-23 08:01:12 -07:00
},
2021-02-05 18:59:00 -08:00
],
withContext: true,
});
const {value} = await connection.getSignatureStatus(signature);
if (value !== null) {
expect(typeof value.slot).to.eq('number');
expect(value.err).to.be.null;
} else {
expect(value).not.to.be.null;
}
2021-02-05 18:59:00 -08:00
await mockRpcResponse({
method: 'getBalance',
params: [accountPayer.publicKey.toBase58(), {commitment: 'confirmed'}],
2021-02-05 18:59:00 -08:00
value: LAMPORTS_PER_SOL - 1,
withContext: true,
});
// 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,
'confirmed',
2021-02-05 18:59:00 -08:00
);
expect(balance).to.be.greaterThan(0);
expect(balance).to.be.at.most(LAMPORTS_PER_SOL);
// accountFrom should have exactly 2, since it didn't pay for the transaction
await mockRpcResponse({
method: 'getBalance',
params: [accountFrom.publicKey.toBase58(), {commitment: 'confirmed'}],
2021-02-05 18:59:00 -08:00
value: minimumAmount + 2,
withContext: true,
});
expect(
await connection.getBalance(accountFrom.publicKey, 'confirmed'),
2021-02-05 18:59:00 -08:00
).to.eq(minimumAmount + 2);
2022-01-21 13:01:48 -08:00
}).timeout(30 * 1000);
});