Skip to content

Commit

Permalink
Fixin decimal and separating the assertions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-then committed Oct 28, 2024
1 parent 62423df commit b5984e2
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b5984e2

Please sign in to comment.