From 785a2f8252955840f8b057201f04d4d44dc76f00 Mon Sep 17 00:00:00 2001 From: jeremy-then Date: Thu, 26 Sep 2024 11:29:44 -0400 Subject: [PATCH] Creates new get2wpBalances in 2wp-utils and assert2wpBalancesAfterSuccessfulPegin in 2wp.js --- lib/2wp-utils.js | 28 ++++++++++++++++++++++++-- lib/tests/2wp.js | 51 ++++++++++++++++++------------------------------ 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/lib/2wp-utils.js b/lib/2wp-utils.js index 1e9de72e..a477f087 100644 --- a/lib/2wp-utils.js +++ b/lib/2wp-utils.js @@ -8,7 +8,7 @@ const { waitAndUpdateBridge } = require('./rsk-utils'); const { retryWithCheck, ensure0x } = require('./utils'); -const { waitForBitcoinTxToBeInMempool, waitForBitcoinMempoolToGetTxs } = require('./btc-utils'); +const { waitForBitcoinTxToBeInMempool, waitForBitcoinMempoolToGetTxs, getBtcAddressBalanceInSatoshis } = require('./btc-utils'); const { getBridge } = require('./precompiled-abi-forks-util'); const { getBridgeState } = require('@rsksmart/bridge-state-data-parser'); const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); @@ -282,7 +282,30 @@ const getBridgeUtxosBalance = async (rskTxHelper) => { const bridgeState = await getBridgeState(rskTxHelper.getClient()); const utxosSum = bridgeState.activeFederationUtxos.reduce((sum, utxo) => sum + utxo.valueInSatoshis, 0); return utxosSum; -}; +}; + +/** + * Gets the active Federation balance in satoshis, Bridge utxos balance in Satoshis and the Bridge rsk balance in weis BN (BigNumber) + * @param {RskTransactionHelper} rskTxHelper to make transactions to the rsk network + * @param {BtcTransactionHelper} btcTxHelper to make transactions to the bitcoin network + * @returns {Promise<{federationAddressBalanceInSatoshis: number, bridgeUtxosBalanceInSatoshis: number, bridgeBalanceInWeisBN: BN}>} + */ +const get2wpBalances = async (rskTxHelper, btcTxHelper) => { + + const bridge = getBridge(rskTxHelper.getClient()); + const federationAddress = await bridge.methods.getFederationAddress().call(); + + const federationAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, federationAddress); + const bridgeUtxosBalanceInSatoshis = await getBridgeUtxosBalance(rskTxHelper); + const bridgeBalanceInWeisBN = await rskTxHelper.getBalance(BRIDGE_ADDRESS); + + return { + federationAddressBalanceInSatoshis, + bridgeUtxosBalanceInSatoshis, + bridgeBalanceInWeisBN, + }; + +}; module.exports = { sendTxToBridge, @@ -300,4 +323,5 @@ module.exports = { createSenderRecipientInfo, createExpectedPeginBtcEvent, getBridgeUtxosBalance, + get2wpBalances, }; diff --git a/lib/tests/2wp.js b/lib/tests/2wp.js index 92aa9aee..8c5488c2 100644 --- a/lib/tests/2wp.js +++ b/lib/tests/2wp.js @@ -1,4 +1,5 @@ const expect = require('chai').expect; +const BN = require('bn.js'); const { getBridge } = require('../precompiled-abi-forks-util'); const { getBtcClient } = require('../btc-client-provider'); const { getRskTransactionHelpers, getRskTransactionHelper } = require('../rsk-tx-helper-provider'); @@ -9,8 +10,7 @@ const { sendPegin, ensurePeginIsRegistered, createSenderRecipientInfo, createExpectedPeginBtcEvent, - BRIDGE_ADDRESS, - getBridgeUtxosBalance, + get2wpBalances, } = require('../2wp-utils'); const { ensure0x } = require('../utils'); const { getBtcAddressBalanceInSatoshis } = require('../btc-utils'); @@ -21,20 +21,8 @@ let rskTxHelpers; let bridge; let federationAddress; let minimumPeginValueInSatoshis; -let minimumPeginValueInBtc; let btcFeeInSatoshis; -const get2wpInitialBalances = async () => { - const initialBridgeBalanceInWeisBN = await rskTxHelper.getBalance(BRIDGE_ADDRESS); - const initialBridgeUtxosBalanceInSatoshis = await getBridgeUtxosBalance(rskTxHelper); - const initialFederationAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, federationAddress); - return { - initialBridgeBalanceInWeisBN, - initialBridgeUtxosBalanceInSatoshis, - initialFederationAddressBalanceInSatoshis, - }; -}; - const assertExpectedPeginBtcEventIsEmitted = async (btcPeginTxHash, rskRecipientAddress, peginValueInSatoshis) => { const recipient1RskAddressChecksumed = rskTxHelper.getClient().utils.toChecksumAddress(ensure0x(rskRecipientAddress)); const expectedEvent = createExpectedPeginBtcEvent(PEGIN_EVENTS.PEGIN_BTC, recipient1RskAddressChecksumed, btcPeginTxHash, peginValueInSatoshis); @@ -43,23 +31,23 @@ const assertExpectedPeginBtcEventIsEmitted = async (btcPeginTxHash, rskRecipient expect(peginBtcEvent).to.be.deep.equal(expectedEvent); }; -const assertSuccessfulPegin2wpFinalBalances = async (initial2wpBalances, peginValueInSatoshis) => { - - const { initialBridgeBalanceInWeisBN, initialBridgeUtxosBalanceInSatoshis, initialFederationAddressBalanceInSatoshis } = initial2wpBalances; +/** + * Gets the final 2wp balances (Federation, Bridge utxos and bridge rsk balances) and compares them to the `initial2wpBalances` to assert the expected values based on a successful pegin. + * Checks that after a successful pegin, the federation and Bridge utxos balances are increased and the Bridge rsk balance is decreased, by the `peginValueInSatoshis` amount. + * @param {{federationAddressBalanceInSatoshis: number, bridgeUtxosBalanceInSatoshis: number, bridgeBalanceInWeisBN: BN}} initial2wpBalances + * @param {number} peginValueInSatoshis the value of the pegin in satoshis by which the 2wp balances are expected to be updated + * @returns {Promise} + */ +const assert2wpBalancesAfterSuccessfulPegin = async (initial2wpBalances, peginValueInSatoshis) => { + + const final2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper); - // The federation balance is increased by the pegin value - const finalFederationAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, federationAddress); - expect(finalFederationAddressBalanceInSatoshis).to.be.equal(initialFederationAddressBalanceInSatoshis + peginValueInSatoshis); + expect(final2wpBalances.federationAddressBalanceInSatoshis).to.be.equal(initial2wpBalances.federationAddressBalanceInSatoshis + peginValueInSatoshis); - // After the successful pegin, the Bridge balance should be reduced by the pegin value - const finalBridgeBalanceInWeisBN = await rskTxHelper.getBalance(BRIDGE_ADDRESS); - const peginValueInWeisBN = rskTxHelper.getClient().utils.BN(satoshisToWeis(peginValueInSatoshis)); - const expectedFinalBridgeBalancesInWeisBN = initialBridgeBalanceInWeisBN.sub(peginValueInWeisBN); - expect(finalBridgeBalanceInWeisBN.eq(expectedFinalBridgeBalancesInWeisBN)).to.be.true; + expect(final2wpBalances.bridgeUtxosBalanceInSatoshis).to.be.equal(initial2wpBalances.bridgeUtxosBalanceInSatoshis + peginValueInSatoshis); - // After the successful pegin, the Bridge utxos sum should be incremented by the pegin value - const finalBridgeUtxosBalance = await getBridgeUtxosBalance(rskTxHelper); - expect(finalBridgeUtxosBalance).to.be.equal(initialBridgeUtxosBalanceInSatoshis + peginValueInSatoshis); + const expectedFinalBridgeBalancesInWeisBN = initial2wpBalances.bridgeBalanceInWeisBN.sub(new BN(satoshisToWeis(peginValueInSatoshis))); + expect(final2wpBalances.bridgeBalanceInWeisBN.eq(expectedFinalBridgeBalancesInWeisBN)).to.be.true; }; @@ -76,7 +64,6 @@ const execute = (description, getRskHost) => { federationAddress = await bridge.methods.getFederationAddress().call(); minimumPeginValueInSatoshis = Number(await bridge.methods.getMinimumLockTxValue().call()); - minimumPeginValueInBtc = Number(satoshisToBtc(minimumPeginValueInSatoshis)); btcFeeInSatoshis = btcToSatoshis(await btcTxHelper.getFee()); await btcTxHelper.importAddress(federationAddress, 'federation'); @@ -87,7 +74,7 @@ const execute = (description, getRskHost) => { // Arrange - const initial2wpBalances = await get2wpInitialBalances(); + const initial2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper); const senderRecipientInfo = await createSenderRecipientInfo(rskTxHelper, btcTxHelper); const initialSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address); const peginValueInSatoshis = minimumPeginValueInSatoshis; @@ -102,7 +89,7 @@ const execute = (description, getRskHost) => { await assertExpectedPeginBtcEventIsEmitted(btcPeginTxHash, senderRecipientInfo.rskRecipientRskAddressInfo.address, peginValueInSatoshis); - await assertSuccessfulPegin2wpFinalBalances(initial2wpBalances, peginValueInSatoshis); + await assert2wpBalancesAfterSuccessfulPegin(initial2wpBalances, peginValueInSatoshis); // The btc sender address balance is decreased by the pegin value and the btc fee const finalSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address); @@ -110,7 +97,7 @@ const execute = (description, getRskHost) => { // The recipient rsk address balance is increased by the pegin value const finalRskRecipientBalanceInWeisBN = await rskTxHelper.getBalance(senderRecipientInfo.rskRecipientRskAddressInfo.address); - const expectedRskRecipientBalancesInWeisBN = rskTxHelper.getClient().utils.BN(satoshisToWeis(peginValueInSatoshis)); + const expectedRskRecipientBalancesInWeisBN = new BN(satoshisToWeis(peginValueInSatoshis)); expect(finalRskRecipientBalanceInWeisBN.eq(expectedRskRecipientBalancesInWeisBN)).to.be.true; });