Skip to content

Commit

Permalink
Asserting that the bridge own balance and bridge utxos sum is as expe…
Browse files Browse the repository at this point in the history
…cted after a successful legacy pegin.
  • Loading branch information
jeremy-then committed Sep 25, 2024
1 parent a81d285 commit 58deab1
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ const { getBridge } = require('../precompiled-abi-forks-util');
const { getBtcClient } = require('../btc-client-provider');
const { getRskTransactionHelpers, getRskTransactionHelper } = require('../rsk-tx-helper-provider');
const { satoshisToBtc, btcToSatoshis, satoshisToWeis } = require('@rsksmart/btc-eth-unit-converter');
const { waitAndUpdateBridge, mineAndSync, findEventInBlock } = require('../rsk-utils');
const { findEventInBlock } = require('../rsk-utils');
const { PEGIN_EVENTS } = require("../constants");
const { sendPegin,
ensurePeginIsRegistered,
donateToBridge,
createSenderRecipientInfo,
createExpectedPeginBtcEvent
createExpectedPeginBtcEvent,
BRIDGE_ADDRESS
} = require('../2wp-utils');
const { ensure0x } = require('../utils');
const { getBtcAddressBalanceInSatoshis } = require('../btc-utils');

const DONATION_AMOUNT = 250;
const { getBridgeState } = require('@rsksmart/bridge-state-data-parser');

let btcTxHelper;
let rskTxHelper;
Expand All @@ -25,11 +24,10 @@ let minimumPeginValueInSatoshis;
let minimumPeginValueInBtc;
let btcFeeInSatoshis;

const setupBridgeDonation = async (rskTxHelpers, btcTxHelper) => {
const donatingBtcAddressInformation = await btcTxHelper.generateBtcAddress('legacy');
await mineAndSync(rskTxHelpers);
await btcTxHelper.fundAddress(donatingBtcAddressInformation.address, DONATION_AMOUNT + btcTxHelper.getFee());
await donateToBridge(rskTxHelpers[0], btcTxHelper, donatingBtcAddressInformation, DONATION_AMOUNT);
const getBridgeUtxosBalance = async (rskTxHelper) => {
const bridgeState = await getBridgeState(rskTxHelper.getClient());
const utxosSum = bridgeState.activeFederationUtxos.reduce((sum, utxo) => sum + utxo.valueInSatoshis, 0);
return utxosSum;
};

const execute = (description, getRskHost) => {
Expand All @@ -49,15 +47,15 @@ const execute = (description, getRskHost) => {
btcFeeInSatoshis = btcToSatoshis(await btcTxHelper.getFee());

await btcTxHelper.importAddress(federationAddress, 'federation');
await waitAndUpdateBridge(rskTxHelper);
await setupBridgeDonation(rskTxHelpers, btcTxHelper);

});

it('should do a basic legacy pegin with the exact minimum value', async () => {

// Arrange

const initialBridgeBalance = Number(await rskTxHelper.getBalance(BRIDGE_ADDRESS));
const initialBridgeUtxosBalance = await getBridgeUtxosBalance(rskTxHelper);
const initialFederationAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, federationAddress);
const senderRecipientInfo = await createSenderRecipientInfo(rskTxHelper, btcTxHelper);
const initialSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address);
Expand Down Expand Up @@ -93,6 +91,14 @@ const execute = (description, getRskHost) => {
const finalRskRecipientBalance = Number(await rskTxHelper.getBalance(senderRecipientInfo.rskRecipientRskAddressInfo.address));
expect(finalRskRecipientBalance).to.be.equal(Number(satoshisToWeis(peginValueInSatoshis)));

// After the successful pegin, the Bridge balance should be reduced by the pegin value
const finalBridgeBalance = Number(await rskTxHelper.getBalance(BRIDGE_ADDRESS));
expect(finalBridgeBalance).to.be.equal(initialBridgeBalance - satoshisToWeis(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(initialBridgeUtxosBalance + peginValueInSatoshis);

});

});
Expand Down

0 comments on commit 58deab1

Please sign in to comment.