Skip to content

Commit

Permalink
Using BN
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-then committed Oct 30, 2024
1 parent 7dc7544 commit 4f507e4
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,12 +864,12 @@ const execute = (description, getRskHost) => {

const initial2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper);
const initialSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address);
const pegoutValueInRbtc = 0.04123456789123456;
const expectedPegoutValueInRbtc = 0.04123456; // rounded down
const pegoutValueInWeisBN = new BN('41234567891234560'); // 0.04123456789123456 in RBTC
const expectedPegoutValueInSatoshis = 4123456; // 0.04123456 in BTC

// Act

const pegoutTransaction = await sendTxToBridge(rskTxHelper, pegoutValueInRbtc, senderRecipientInfo.rskRecipientRskAddressInfo.address);
const pegoutTransaction = await sendTxToBridge(rskTxHelper, pegoutValueInWeisBN, senderRecipientInfo.rskRecipientRskAddressInfo.address);

// Assert

Expand All @@ -889,9 +889,9 @@ const execute = (description, getRskHost) => {
// Checking all the pegout events are emitted and in order
const blockNumberAfterPegoutRelease = await rskTxHelper.getBlockNumber();
const pegoutsEvents = await getPegoutEventsInBlockRange(rskTxHelper, pegoutTransaction.blockNumber, blockNumberAfterPegoutRelease);
await assertSuccessfulPegoutEventsAreEmitted(pegoutsEvents, pegoutTransaction.transactionHash, senderRecipientInfo, expectedPegoutValueInRbtc, bridgeStateAfterPegoutCreation);
await assertSuccessfulPegoutEventsAreEmitted(pegoutsEvents, pegoutTransaction.transactionHash, senderRecipientInfo, expectedPegoutValueInSatoshis, bridgeStateAfterPegoutCreation);

await assert2wpBalanceAfterSuccessfulPegoutWithLargeWeis(initial2wpBalances, pegoutValueInRbtc, btcToSatoshis(expectedPegoutValueInRbtc));
await assert2wpBalanceAfterSuccessfulPegoutWithLargeWeis(initial2wpBalances, pegoutValueInWeisBN, expectedPegoutValueInSatoshis);

// 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 @@ -1074,20 +1074,20 @@ 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.
* When there is a pegout with big value in weis, that is not fully converted to satoshis and part of it needs to be trimmed.
* Bridge rsk balance will be slightly bigger than the federation balance due to this mismatch, since the Bridge will keep
* those trimmed weis while the federation will not.
* @param {{federationAddressBalanceInSatoshis: number, bridgeUtxosBalanceInSatoshis: number, bridgeBalanceInWeisBN: BN}} initial2wpBalances
* @param {number} pegoutValueInRbtc the value in RBTC of the pegout
* @param {BN} pegoutValueInWeisBN the value in BN 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 assert2wpBalanceAfterSuccessfulPegoutWithLargeWeis = async (initial2wpBalances, pegoutValueInWeisBN, 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)));
const expectedFinalBridgeBalancesInWeisBN = initial2wpBalances.bridgeBalanceInWeisBN.add(pegoutValueInWeisBN);

expect(final2wpBalances.bridgeBalanceInWeisBN.eq(expectedFinalBridgeBalancesInWeisBN)).to.be.true;

Expand Down

0 comments on commit 4f507e4

Please sign in to comment.