Skip to content
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
46a0a0f
added a test for the getEstimatedFeesForNextPegoutEvent
julianlen Apr 9, 2026
24679f8
renamed test 01_07_01
julianlen Apr 9, 2026
f93c36d
added a test for getEstimatedFeesForPegoutAomunt
julianlen Apr 9, 2026
7b6cb5b
improved tests names
julianlen Apr 9, 2026
036cad4
extracted the call to getEstimatedFeesForPegOutAmount
julianlen Apr 10, 2026
30c16f7
merge both getEstimatedFees methods in one file
julianlen Apr 10, 2026
d3ede49
activate vetiver900 from block 1
julianlen Apr 13, 2026
5356a97
getEstimatedFeesForPegoutAmount method is called once
julianlen Apr 13, 2026
3192676
instead of calling getRskTransactionHelper and getRskTransactionHelpe…
julianlen Apr 13, 2026
18b783f
deleted unused import
julianlen Apr 13, 2026
6b657f4
it's not needed to trigger a release before and after each test, afte…
julianlen Apr 13, 2026
1111867
added comment to explain why triggerRelease is called after each test
julianlen Apr 13, 2026
16a933f
renamed assertReleaseRequestsAreProcessed to processReleaseRequestsAn…
julianlen Apr 13, 2026
0a35438
renamed setupFedUtxosAndCreateAPegoutRequest to setupFedUtxosAndAddAP…
julianlen Apr 13, 2026
6335839
changed estimatedFeesForNextPegout to estimatedFeesForNextPegOut
julianlen Apr 14, 2026
7488540
removed unnecessary try/catch
julianlen Apr 14, 2026
3b7148f
Extracted getEstimatedFeesForPegOutAmount
julianlen Apr 14, 2026
d8a1d24
extracted sendPegoutWithEstimatedFees
julianlen Apr 14, 2026
856bf83
fix estimatedFeesForPegout
julianlen Apr 14, 2026
1f2080e
renaming test for consistency
julianlen Apr 14, 2026
98fd3d2
Added a test to check getEstimatedFees with no requests enqueue
julianlen Apr 14, 2026
e937fc3
changed test name for consistency
julianlen Apr 14, 2026
313572e
added a trailing semi colon
julianlen Apr 14, 2026
1254091
changed pegout for pegOut for consistency
julianlen Apr 14, 2026
6ea1829
fixed tests title
julianlen Apr 14, 2026
d99c752
added error messages
julianlen Apr 16, 2026
7855484
shouldReturnGreaterThanZeroErrorMessage used also with getEstimatedFe…
julianlen Apr 16, 2026
15cf620
changed the file name
julianlen Apr 16, 2026
44cefb7
deleted sending from cow to the pegin's recipient
julianlen Apr 16, 2026
9bf91f0
assert there is one pegout request after sending a tx to the bridge
julianlen Apr 16, 2026
ea8b9bd
changed estimatedFees parameter to estimatedFeesInSatoshis
julianlen Apr 16, 2026
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
148 changes: 148 additions & 0 deletions lib/tests/get_estimated_fees_methods.js
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing a bunch of test cases: 0 requests, multiple requests, different pegout amounts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this wasn't the idea, it is an integration test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

98fd3d2

added a second part of each test as discussed checking getEstimatedFees with no pegout requests

Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
const expect = require('chai').expect;
const BN = require('bn.js');
const { getBridgeState } = require('@rsksmart/bridge-state-data-parser');
const { btcToWeis, satoshisToBtc, satoshisToWeis } = require('@rsksmart/btc-eth-unit-converter');
const { assertContractCallFails } = require('../assertions/contractMethods');
const { getBridge } = require('../bridge-provider');
const {
createSenderRecipientInfo,
ensurePeginIsRegistered,
sendPeginToActiveFederation,
sendTxToBridge,
} = require('../2wp-utils');
const { getBtcClient } = require('../btc-client-provider');
const { MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS } = require('../constants/pegout-constants');
const { getRskTransactionHelpers } = require('../rsk-tx-helper-provider');
const { sendFromCow, triggerRelease } = require('../rsk-utils');

const FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS = 500_000;
const FEDERATION_PEGIN_AMOUNT_IN_BTC = Number(satoshisToBtc(FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS));
const BTC_FUNDING_AMOUNT_IN_BTC = 2;
const PEGOUT_SENDER_FUNDING_IN_BTC = 2;

/**
* @param {string} [description]
*/
const execute = (description) => {
let bridge;
let rskTxHelper;
let rskTxHelpers;
let btcTxHelper;
let senderInfo;

const getQueuedPegoutsCount = async () =>
Number(await bridge.methods.getQueuedPegoutsCount().call());

const getEstimatedFeesForNextPegOutEvent = async () =>
Number(await bridge.methods.getEstimatedFeesForNextPegOutEvent().call());

const getEstimatedFeesForPegOutAmount = async (satoshis) => await bridge.methods
.getEstimatedFeesForPegOutAmount(satoshisToWeis(satoshis))

const setupFedUtxosAndAddAPegoutRequestToTheBridge = async () => {
senderInfo = await createSenderRecipientInfo(
rskTxHelper,
btcTxHelper,
'legacy',
BTC_FUNDING_AMOUNT_IN_BTC
);

const peginBtcTxHash = await sendPeginToActiveFederation(
rskTxHelper,
btcTxHelper,
senderInfo.btcSenderAddressInfo,
FEDERATION_PEGIN_AMOUNT_IN_BTC
);
await ensurePeginIsRegistered(rskTxHelper, peginBtcTxHash);

const bridgeStateAfterPegin = await getBridgeState(rskTxHelper.getClient());
const peginUtxoInBridgeState = bridgeStateAfterPegin.activeFederationUtxos.find(
(utxo) =>
utxo.btcTxHash === peginBtcTxHash &&
utxo.valueInSatoshis === FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS
);
expect(peginUtxoInBridgeState).to.not.be.undefined;

await sendFromCow(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to send funds from the cow address? Weren't those funds already received from the peg-in?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 44cefb7

rskTxHelper,
senderInfo.rskRecipientRskAddressInfo.address,
Number(btcToWeis(PEGOUT_SENDER_FUNDING_IN_BTC))
);

await sendTxToBridge(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to get the bridge state after this and assert there is a pegout request?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 9bf91f0

rskTxHelper,
new BN(satoshisToWeis(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS)),
senderInfo.rskRecipientRskAddressInfo.address
);
};

const processReleaseRequestsAndAssertQueueCleared = async () => {
Comment thread
julia-zack marked this conversation as resolved.
Outdated
const countBeforeRelease = await getQueuedPegoutsCount();
const expectedPegoutCount = 2;
expect(countBeforeRelease).to.equal(expectedPegoutCount);

await triggerRelease(rskTxHelpers, btcTxHelper);

const countAfterRelease = await getQueuedPegoutsCount();
expect(countAfterRelease).to.equal(0);
};

const sendPegoutWithEstimatedFees = async (estimatedFees) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const sendPegoutWithEstimatedFees = async (estimatedFees) => {
const sendPegoutWithEstimatedFees = async (estimatedFeesInSatoshis) => {

Adding the unit helps, it can get confusing vey fast

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ea8b9bd

const secondPegoutAmountInSatoshis =
MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS + estimatedFees;
await sendTxToBridge(
rskTxHelper,
new BN(satoshisToWeis(secondPegoutAmountInSatoshis)),
senderInfo.rskRecipientRskAddressInfo.address
);
};

describe(description, () => {
before(async () => {
rskTxHelpers = getRskTransactionHelpers();
rskTxHelper = rskTxHelpers[0];
btcTxHelper = getBtcClient();
bridge = await getBridge(rskTxHelper.getClient());
});

beforeEach(async () => {
await setupFedUtxosAndAddAPegoutRequestToTheBridge();
});

// Ensuring there's no pegout pending in the Bridge contract
// after each test to avoid interference between tests
afterEach(async () => {
await triggerRelease(rskTxHelpers, btcTxHelper);
Comment thread
jeremy-then marked this conversation as resolved.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get the bridge state and assert there are no pending pegout requests after this?

});

describe('getEstimatedFeesForNextPegOutEvent', () => {
it('should allow constructing a second pegout request using the fees from getEstimatedFeesForNextPegOutEvent and process both', async () => {
Comment thread
julia-zack marked this conversation as resolved.
Outdated
const estimatedFeesForNextPegOut = await getEstimatedFeesForNextPegOutEvent();
expect(estimatedFeesForNextPegOut).to.be.greaterThan(0);

await sendPegoutWithEstimatedFees(estimatedFeesForNextPegOut);
await processReleaseRequestsAndAssertQueueCleared();
});
});

describe('getEstimatedFeesForPegOutAmount', () => {
it('should allow constructing a second pegout request using the estimated fees from getEstimatedFeesForPegOutAmount and process both', async () => {
const estimatedFeesForPegout = Number(getEstimatedFeesForPegOutAmount(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS).call());
expect(estimatedFeesForPegout).to.be.greaterThan(0);

await sendPegoutWithEstimatedFees(estimatedFeesForPegout);
await processReleaseRequestsAndAssertQueueCleared();
});

it('should revert when peg-out amount in weis is below minimum peg-out', async () => {
await assertContractCallFails(
getEstimatedFeesForPegOutAmount(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS - 1)
);
});
});
});
};

module.exports = {
execute,
};
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ global.Runners = {
arrowhead631: createForkObject('arrowhead631', 1),
lovell700: createForkObject('lovell700', 1),
reed800: createForkObject('reed800', 1),
vetiver900: createForkObject('vetiver900', 1),
},
additionalFederationAddresses: [],
},
Expand Down
2 changes: 1 addition & 1 deletion tests/01_07_00-activate-latest-fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ const activateForkTest = require('../lib/tests/activate-fork');

// Skipped activate-fork.js. When there is a new fork to be tested pre and post, unskip it in the activate-fork.js file
// activateForkTest.execute(
// Runners.common.forks.reed800
// Runners.common.forks.vetiver900
// );
3 changes: 3 additions & 0 deletions tests/01_07_01-get_estimated_fees_methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const getEstimatedFeesMethodsTests = require('../lib/tests/get_estimated_fees_methods');

getEstimatedFeesMethodsTests.execute('Bridge peg-out fee estimation methods');
Loading