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

145 lines
3.4 KiB
JavaScript
Raw Normal View History

// @flow
2021-02-05 18:59:00 -08:00
import {expect} from 'chai';
import {
Account,
Connection,
Transaction,
SystemProgram,
LAMPORTS_PER_SOL,
} from '../src';
2021-02-05 18:59:00 -08:00
import {MOCK_PORT, url} from './url';
import {
helpers,
mockErrorMessage,
mockErrorResponse,
uniqueSignature,
uniqueBlockhash,
mockRpcResponse,
mockServer,
} from './mocks/rpc-http';
import {
stubRpcWebSocket,
restoreRpcWebSocket,
mockRpcMessage,
} from './mocks/rpc-websockets';
import base58 from 'bs58';
describe('Transaction Payer', () => {
let connection: Connection;
beforeEach(() => {
connection = new Connection(url);
});
if (!process.env.TEST_LIVE) {
beforeEach(() => {
mockServer.start(MOCK_PORT);
stubRpcWebSocket(connection);
});
afterEach(() => {
mockServer.stop();
restoreRpcWebSocket(connection);
});
}
it('transaction-payer', async () => {
const accountPayer = new Account();
const accountFrom = new Account();
const accountTo = new Account();
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: 'singleGossip',
});
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',
2020-12-24 10:43:45 -08:00
params: [accountPayer.publicKey.toBase58(), {commitment: 'singleGossip'}],
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,
'singleGossip',
);
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',
2020-12-24 10:43:45 -08:00
params: [accountFrom.publicKey.toBase58(), {commitment: 'singleGossip'}],
2021-02-05 18:59:00 -08:00
value: minimumAmount + 2,
withContext: true,
});
expect(
await connection.getBalance(accountFrom.publicKey, 'singleGossip'),
).to.eq(minimumAmount + 2);
});
});