Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: correctly set await to wait for new tx received before continuing the tests #358

Merged
merged 7 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions __tests__/integration/atomic-swap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { TestUtils } from './utils/test-utils-integration';
import { WalletHelper } from './utils/wallet-helper';
import { singleMultisigWalletData } from '../../scripts/helpers/wallet-precalculation.helper';
import { loggers } from './utils/logger.util';
import { delay } from './utils/core.util';
import settings from '../../src/settings';

describe('send tx (HTR)', () => {
Expand Down Expand Up @@ -46,10 +45,6 @@ describe('send tx (HTR)', () => {
tokenTx2 = await wallet2.createToken({ amount: 1000, name: 'Token wallet2', symbol: 'TKW2' });

fundsTx = await wallet1.injectFunds(10, 0);
await delay(500);

// Awaiting for updated balances to be received by the websocket
await TestUtils.pauseForWsUpdate();
} catch (err) {
TestUtils.logError(err.stack);
}
Expand Down Expand Up @@ -164,7 +159,6 @@ describe('send tx (HTR)', () => {
})
.set({ 'x-wallet-id': wallet1.walletId });
loggers.test.insertLineToLog('atomic-swap[utxos]: proposal', { body: response.body });
await TestUtils.pauseForWsUpdate();
expect(response.body).toEqual({
success: true,
data: expect.any(String),
Expand Down Expand Up @@ -313,7 +307,8 @@ describe('send tx (HTR)', () => {
loggers.test.insertLineToLog('atomic-swap[HTR P2PKH]: push', { body: response.body });
expect(response.body.success).toBe(true);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);
await TestUtils.waitForTxReceived(wallet2.walletId, response.body.hash);

// Get the balance state after the swap
const finalBalance1 = await wallet1.getBalance();
Expand Down Expand Up @@ -393,7 +388,8 @@ describe('send tx (HTR)', () => {
loggers.test.insertLineToLog('atomic-swap[1TK P2PKH]: push', { body: response.body });
expect(response.body.success).toBe(true);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);
await TestUtils.waitForTxReceived(wallet2.walletId, response.body.hash);

// Get the balance state after the swap
const finalBalance1 = await wallet1.getBalance(tokenTx1.hash);
Expand Down Expand Up @@ -549,7 +545,8 @@ describe('send tx (HTR)', () => {
loggers.test.insertLineToLog('atomic-swap[2TK P2PKH]: push', { body: response.body });
expect(response.body.success).toBe(true);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);
await TestUtils.waitForTxReceived(wallet2.walletId, response.body.hash);

// Get the balance state after the swap
// balances for wallet1 (HTR, token 1 and token 2)
Expand Down Expand Up @@ -769,7 +766,8 @@ describe('send tx (HTR)', () => {
loggers.test.insertLineToLog('atomic-swap[P2SH]: push', { body: response.body });
expect(response.body.success).toBe(true);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);
await TestUtils.waitForTxReceived(wallet2.walletId, response.body.hash);

// Get the balance state after the swap
// balances for wallet1 (HTR, token 1 and token 2)
Expand Down Expand Up @@ -806,7 +804,6 @@ describe('send tx (HTR)', () => {

it('should send the change to the correct address', async () => {
const tx = await wallet1.injectFunds(3, 10);
await TestUtils.pauseForWsUpdate();
const destAddr = await wallet1.getAddressAt(10);
const changeAddr = await wallet1.getAddressAt(11);
const recvAddr = await wallet1.getAddressAt(12);
Expand Down
25 changes: 15 additions & 10 deletions __tests__/integration/create-nft.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ describe('create-nft routes', () => {
})
.set({ 'x-wallet-id': wallet1.walletId });

await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

expect(response.body.success).toBe(true);
const nftTx = response.body;
expect(nftTx.hash).toBeDefined();
Expand All @@ -98,8 +100,6 @@ describe('create-nft routes', () => {
expect(nftTx.outputs[0].value).toBe(1);
expect(nftTx.outputs[0].token_data).toBe(0);

await TestUtils.pauseForWsUpdate();

// The fees (1) and deposits (1) should be deducted
const htrBalanceAfter = await wallet1.getBalance();
expect(htrBalanceAfter.available).toBe(htrBalanceBefore.available - 2);
Expand All @@ -123,6 +123,8 @@ describe('create-nft routes', () => {
})
.set({ 'x-wallet-id': wallet1.walletId });

await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

expect(response.body.success).toBe(true);
const nftTx = response.body;
expect(nftTx.hash).toBeDefined();
Expand All @@ -141,8 +143,6 @@ describe('create-nft routes', () => {
}
}

await TestUtils.pauseForWsUpdate();

// Validating balances for target addresses
const destInfo = await TestUtils.getAddressInfo(destAddr, wallet1.walletId, nftTx.hash);
expect(destInfo.total_amount_available).toBe(1);
Expand All @@ -163,6 +163,8 @@ describe('create-nft routes', () => {
expect(response.body.success).toBe(true);
const nftTx = response.body;

await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

expect(nftTx.hash).toBeDefined();

// Validating authority tokens
Expand All @@ -172,9 +174,6 @@ describe('create-nft routes', () => {
});

it('should create nft with melt authority', async () => {
// Since no pause was necessary on the last test, we will add one here to improve stability
await TestUtils.pauseForWsUpdate();

const response = await TestUtils.request
.post('/wallet/create-nft')
.send({
Expand All @@ -189,14 +188,15 @@ describe('create-nft routes', () => {

expect(nftTx.hash).toBeDefined();

await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

// Validating authority tokens
const authorityOutputs = nftTx.outputs.filter(o => TOKEN_DATA.isAuthorityToken(o.token_data));
expect(authorityOutputs.length).toBe(1);
expect(authorityOutputs[0].value).toBe(AUTHORITY_VALUE.MELT);
});

it('should create nft with mint and melt authorities', async () => {
await TestUtils.pauseForWsUpdate();
const response = await TestUtils.request
.post('/wallet/create-nft')
.send({
Expand All @@ -212,6 +212,8 @@ describe('create-nft routes', () => {

expect(nftTx.hash).toBeDefined();

await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

// Validating authority tokens
const authorityOutputs = nftTx.outputs.filter(o => TOKEN_DATA.isAuthorityToken(o.token_data));
expect(authorityOutputs.length).toBe(2);
Expand All @@ -220,7 +222,6 @@ describe('create-nft routes', () => {
});

it('should create the NFT and send authority outputs to the correct address', async done => {
await TestUtils.pauseForWsUpdate();
// By default, will mint tokens into the next unused address
const address0 = await wallet1.getAddressAt(0);
const address1 = await wallet1.getAddressAt(1);
Expand All @@ -239,6 +240,8 @@ describe('create-nft routes', () => {
const transaction = response.body;
expect(transaction.success).toBe(true);

await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

// Validating a new mint authority was created
const authorityOutputs = transaction.outputs.filter(
o => transactionUtils.isAuthorityOutput({ token_data: o.tokenData })
Expand All @@ -262,7 +265,6 @@ describe('create-nft routes', () => {
});

it('Create nft using external mint/melt address', async done => {
await TestUtils.pauseForWsUpdate();
const address2idx0 = await wallet2.getAddressAt(0);
const address2idx1 = await wallet2.getAddressAt(1);

Expand Down Expand Up @@ -309,6 +311,9 @@ describe('create-nft routes', () => {

expect(response3.body.success).toBe(true);

await TestUtils.waitForTxReceived(wallet1.walletId, response3.body.hash);
await TestUtils.waitForTxReceived(wallet2.walletId, response3.body.hash);

const transaction = response3.body;

// Validating a new mint authority was created
Expand Down
27 changes: 15 additions & 12 deletions __tests__/integration/create-token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('create token', () => {
expect(response.body.success).toBe(true);
expect(response.body.configurationString).toBe(configStringResponse.configurationString);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

const htrBalance = await wallet1.getBalance();
const tkaBalance = await wallet1.getBalance(response.body.hash);
Expand All @@ -221,7 +221,7 @@ describe('create token', () => {
const transaction = response.body;
expect(transaction.success).toBe(true);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

const addr9 = await wallet1.getAddressInfo(9, transaction.hash);
expect(addr9.total_amount_received).toBe(amountTokens);
Expand All @@ -246,7 +246,7 @@ describe('create token', () => {
const htrOutputIndex = transaction.outputs.findIndex(o => o.token_data === 0);
const htrChange = transaction.outputs[htrOutputIndex].value;

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet2.walletId, response.body.hash);

const addr5 = await wallet2.getAddressInfo(5);
expect(addr5.total_amount_received).toBe(htrChange);
Expand All @@ -272,7 +272,7 @@ describe('create token', () => {
const htrOutputIndex = transaction.outputs.findIndex(o => o.token_data === 0);
const htrChange = transaction.outputs[htrOutputIndex].value;

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet2.walletId, response.body.hash);

const addr4 = await wallet2.getAddressInfo(4);
expect(addr4.total_amount_received).toBe(htrChange);
Expand All @@ -296,6 +296,8 @@ describe('create token', () => {
expect(response.body.success).toBe(true);
const tx = response.body;

await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

expect(tx.hash).toBeDefined();

// Validating authority tokens
Expand All @@ -307,9 +309,6 @@ describe('create token', () => {
});

it('should create token with only melt authority', async () => {
// Since no pause was necessary on the last test, we will add one here to improve stability
await TestUtils.pauseForWsUpdate();

const response = await TestUtils.request
.post('/wallet/create-token')
.send({
Expand All @@ -326,6 +325,8 @@ describe('create token', () => {

expect(tx.hash).toBeDefined();

await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

// Validating authority tokens
const authorityOutputs = tx.outputs.filter(
o => transactionUtils.isAuthorityOutput({ token_data: o.tokenData })
Expand All @@ -335,7 +336,6 @@ describe('create token', () => {
});

it('should create token with mint and melt authorities', async () => {
await TestUtils.pauseForWsUpdate();
const response = await TestUtils.request
.post('/wallet/create-token')
.send({
Expand All @@ -352,6 +352,8 @@ describe('create token', () => {

expect(tx.hash).toBeDefined();

await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

// Validating authority tokens
const authorityOutputs = tx.outputs.filter(
o => transactionUtils.isAuthorityOutput({ token_data: o.tokenData })
Expand All @@ -362,8 +364,6 @@ describe('create token', () => {
});

it('should create the token and send authority outputs to the correct address', async done => {
// Since no pause was necessary on the last test, we will add one here to improve stability
await TestUtils.pauseForWsUpdate();
// By default, will mint tokens into the next unused address
const address0 = await wallet1.getAddressAt(0);
const address1 = await wallet1.getAddressAt(1);
Expand All @@ -383,6 +383,8 @@ describe('create token', () => {
const transaction = response.body;
expect(transaction.success).toBe(true);

await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

// Validating a new mint authority was created
const authorityOutputs = transaction.outputs.filter(
o => transactionUtils.isAuthorityOutput({ token_data: o.tokenData })
Expand All @@ -406,8 +408,6 @@ describe('create token', () => {
});

it('Create token using external mint/melt address', async done => {
// Since no pause was necessary on the last test, we will add one here to improve stability
await TestUtils.pauseForWsUpdate();
const address2idx0 = await wallet2.getAddressAt(0);
const address2idx1 = await wallet2.getAddressAt(1);

Expand Down Expand Up @@ -457,6 +457,9 @@ describe('create token', () => {

expect(response3.body.success).toBe(true);

await TestUtils.waitForTxReceived(wallet1.walletId, response3.body.hash);
await TestUtils.waitForTxReceived(wallet2.walletId, response3.body.hash);

const transaction = response3.body;
expect(transaction.success).toBe(true);

Expand Down
16 changes: 8 additions & 8 deletions __tests__/integration/melt-tokens.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe('melt tokens', () => {

expect(response.body.success).toBe(true);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

const addr3htr = await wallet1.getAddressInfo(3);
const addr3tka = await wallet1.getAddressInfo(3, tokenA.uid);
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('melt tokens', () => {

expect(response.body.success).toBe(true);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

const addr6htr = await wallet1.getAddressInfo(6);
const addr6tka = await wallet1.getAddressInfo(6, tokenA.uid);
Expand All @@ -246,7 +246,7 @@ describe('melt tokens', () => {

expect(response.body.success).toBe(true);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

const addr8htr = await wallet1.getAddressInfo(8);
const addr8tka = await wallet1.getAddressInfo(8, tokenA.uid);
Expand All @@ -272,7 +272,7 @@ describe('melt tokens', () => {

expect(response.body.success).toBe(true);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

const balance1htr = await wallet1.getBalance();
const balance1tka = await wallet1.getBalance(tokenA.uid);
Expand All @@ -293,7 +293,7 @@ describe('melt tokens', () => {

expect(response.body.success).toBe(true);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

const balance1htr = await wallet1.getBalance();
const balance1tka = await wallet1.getBalance(tokenA.uid);
Expand All @@ -315,7 +315,7 @@ describe('melt tokens', () => {

expect(response.body.success).toBe(true);

await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

const balance1htr = await wallet1.getBalance();
const balance1tka = await wallet1.getBalance(tokenA.uid);
Expand All @@ -339,7 +339,7 @@ describe('melt tokens', () => {

const transaction = response.body;
expect(transaction.success).toBe(true);
await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response.body.hash);

const balance1htr = await wallet1.getBalance();
const balance1tka = await wallet1.getBalance(tokenA.uid);
Expand Down Expand Up @@ -386,7 +386,7 @@ describe('melt tokens', () => {

const transaction = response2.body;
expect(transaction.success).toBe(true);
await TestUtils.pauseForWsUpdate();
await TestUtils.waitForTxReceived(wallet1.walletId, response2.body.hash);

const balance1htr = await wallet1.getBalance();
const balance1tka = await wallet1.getBalance(tokenA.uid);
Expand Down
Loading
Loading