diff --git a/lib/tests/2wp.js b/lib/tests/2wp.js index 78a35725..b8e30bb1 100644 --- a/lib/tests/2wp.js +++ b/lib/tests/2wp.js @@ -117,9 +117,7 @@ const execute = (description, getRskHost) => { // Arrange - const initialBridgeBalance = Number(await rskTxHelper.getBalance(BRIDGE_ADDRESS)); - const initialBridgeUtxosBalance = await getBridgeUtxosBalance(rskTxHelper); - const initialFederationAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, federationAddress); + const initial2wpBalances = await get2wpInitialBalances(); const senderRecipientInfo = await createSenderRecipientInfo(rskTxHelper, btcTxHelper); const initialSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address); // Value above minimum @@ -128,40 +126,23 @@ const execute = (description, getRskHost) => { // Act const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, senderRecipientInfo.btcSenderAddressInfo, satoshisToBtc(peginValueInSatoshis)); - await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash); // Assert - // The btc pegin tx is already marked as processed by the bridge - const isBtcTxHashAlreadyProcessed = await bridge.methods.isBtcTxHashAlreadyProcessed(btcPeginTxHash).call(); - expect(isBtcTxHashAlreadyProcessed).to.be.true; + await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash); - // The pegin_btc event is emitted with the expected values - const recipient1RskAddressChecksumed = rskTxHelper.getClient().utils.toChecksumAddress(ensure0x(senderRecipientInfo.rskRecipientRskAddressInfo.address)); - const expectedEvent = createExpectedPeginBtcEvent(PEGIN_EVENTS.PEGIN_BTC, recipient1RskAddressChecksumed, btcPeginTxHash, peginValueInSatoshis); - const btcTxHashProcessedHeight = Number(await bridge.methods.getBtcTxHashProcessedHeight(btcPeginTxHash).call()); - const peginBtcEvent = await findEventInBlock(rskTxHelper, expectedEvent.name, btcTxHashProcessedHeight); - expect(peginBtcEvent).to.be.deep.equal(expectedEvent); + await assertExpectedPeginBtcEventIsEmitted(btcPeginTxHash, senderRecipientInfo.rskRecipientRskAddressInfo.address, peginValueInSatoshis); - // The federation balance is increased by the pegin value - const finalFederationAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, federationAddress); - expect(finalFederationAddressBalanceInSatoshis).to.be.equal(initialFederationAddressBalanceInSatoshis + peginValueInSatoshis); + await assertSuccessfulPegin2wpFinalBalances(initial2wpBalances, peginValueInSatoshis); // The 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 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); + const finalRskRecipientBalanceInWeisBN = await rskTxHelper.getBalance(senderRecipientInfo.rskRecipientRskAddressInfo.address); + const expectedRskRecipientBalancesInWeisBN = rskTxHelper.getClient().utils.BN(satoshisToWeis(peginValueInSatoshis)); + expect(finalRskRecipientBalanceInWeisBN.eq(expectedRskRecipientBalancesInWeisBN)).to.be.true; });