Skip to content

Commit

Permalink
Creates new get2wpBalances in 2wp-utils and assert2wpBalancesAfterSuc…
Browse files Browse the repository at this point in the history
…cessfulPegin in 2wp.js
  • Loading branch information
jeremy-then committed Sep 26, 2024
1 parent a7e1868 commit 785a2f8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
28 changes: 26 additions & 2 deletions lib/2wp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand All @@ -300,4 +323,5 @@ module.exports = {
createSenderRecipientInfo,
createExpectedPeginBtcEvent,
getBridgeUtxosBalance,
get2wpBalances,
};
51 changes: 19 additions & 32 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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<void>}
*/
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;

};

Expand All @@ -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');
Expand All @@ -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;
Expand All @@ -102,15 +89,15 @@ 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);
expect(finalSenderAddressBalanceInSatoshis).to.be.equal(initialSenderAddressBalanceInSatoshis - peginValueInSatoshis - btcFeeInSatoshis);

// 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;

});
Expand Down

0 comments on commit 785a2f8

Please sign in to comment.