diff --git a/lib/tests/2wp.js b/lib/tests/2wp.js index dde61add..284c3b20 100644 --- a/lib/tests/2wp.js +++ b/lib/tests/2wp.js @@ -4,7 +4,7 @@ const { createPeginV1TxData } = require('pegin-address-verificator'); const { getBridge } = require('../precompiled-abi-forks-util'); const { getBtcClient } = require('../btc-client-provider'); const { getRskTransactionHelper, getRskTransactionHelpers } = require('../rsk-tx-helper-provider'); -const { satoshisToBtc, btcToSatoshis, satoshisToWeis, btcToWeis } = require('@rsksmart/btc-eth-unit-converter'); +const { satoshisToBtc, btcToSatoshis, satoshisToWeis, btcToWeis, ethToWeis } = require('@rsksmart/btc-eth-unit-converter'); const { findEventInBlock, triggerRelease, @@ -875,7 +875,7 @@ const execute = (description, getRskHost) => { const initial2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper); const initialSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address); - const pegoutValueInRbtc = 0.041234567891234567; + const pegoutValueInRbtc = 0.04123456789123456; const expectedPegoutValueInRbtc = 0.04123456; // rounded down // Act @@ -902,7 +902,7 @@ const execute = (description, getRskHost) => { const pegoutsEvents = await getPegoutEventsInBlockRange(rskTxHelper, pegoutTransaction.blockNumber, blockNumberAfterPegoutRelease); await assertSuccessfulPegoutEventsAreEmitted(pegoutsEvents, pegoutTransaction.transactionHash, senderRecipientInfo, expectedPegoutValueInRbtc, bridgeStateAfterPegoutCreation); - await assert2wpBalanceAfterSuccessfulPegout(initial2wpBalances, expectedPegoutValueInRbtc); + await assert2wpBalanceAfterSuccessfulPegoutWithLargeWeis(initial2wpBalances, pegoutValueInRbtc, btcToSatoshis(expectedPegoutValueInRbtc)); // Assert that the sender address balance is increased by the actual pegout value const finalSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address); @@ -1082,6 +1082,30 @@ const assert2wpBalanceAfterSuccessfulPegout = async (initial2wpBalances, pegoutV }; +/** + * Asserts the 2wp balances after a successful pegout, ensuring that the federation balance is decreased by the expected rounded pegout value in satoshis, + * the bridge rsk balance is increased by the pegout value with many decimals and the bridge utxos balance is decreased by the expected rounded pegout value in satoshis. + * When there pegout with an RBTC value with many decimals like '0.04123456789123456', that are not fully converted to btc/satoshis and part of it needs to be trimmed (like this: 0.04123456) + * in order to convert it to satoshis, the Bridge rsk balance will be slightly bigger than the federation balance due to this mismatch, since the Bridge will keep + * those trimmed cents while the federation will not. + * @param {{federationAddressBalanceInSatoshis: number, bridgeUtxosBalanceInSatoshis: number, bridgeBalanceInWeisBN: BN}} initial2wpBalances + * @param {number} pegoutValueInRbtc the value in RBTC of the pegout + * @param {number} expectedPegoutValueInSatoshis the expected pegout value in satoshis the user will receive, rounded down. + */ +const assert2wpBalanceAfterSuccessfulPegoutWithLargeWeis = async (initial2wpBalances, pegoutValueInRbtc, expectedPegoutValueInSatoshis) => { + + const final2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper); + + expect(final2wpBalances.federationAddressBalanceInSatoshis).to.be.equal(initial2wpBalances.federationAddressBalanceInSatoshis - expectedPegoutValueInSatoshis); + + const expectedFinalBridgeBalancesInWeisBN = initial2wpBalances.bridgeBalanceInWeisBN.add(new BN(ethToWeis(pegoutValueInRbtc))); + + expect(final2wpBalances.bridgeBalanceInWeisBN.eq(expectedFinalBridgeBalancesInWeisBN)).to.be.true; + + expect(final2wpBalances.bridgeUtxosBalanceInSatoshis).to.be.equal(initial2wpBalances.bridgeUtxosBalanceInSatoshis - expectedPegoutValueInSatoshis); + +}; + const assertExpectedReleaseRequestRejectedEventIsEmitted = async (rskSenderAddress, amountInSatoshis, rejectionReason) => { const rskSenderAddressChecksummed = rskTxHelper.getClient().utils.toChecksumAddress(ensure0x(rskSenderAddress)); const expectedEvent = createExpectedReleaseRequestRejectedEvent(rskSenderAddressChecksummed, amountInSatoshis, rejectionReason);