From 62423dfbea1c14ae0e878a383b79bf9c7f99e9e8 Mon Sep 17 00:00:00 2001 From: jeremy-then Date: Mon, 28 Oct 2024 13:00:32 -0400 Subject: [PATCH] Adds 'should do a pegout and round down the weis to satoshis as expected' test. --- lib/2wp-utils.js | 2 +- lib/tests/2wp.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/2wp-utils.js b/lib/2wp-utils.js index a4e11dd1..0f855dcb 100644 --- a/lib/2wp-utils.js +++ b/lib/2wp-utils.js @@ -64,7 +64,7 @@ const sendTxToBridge = async (rskTxHelper, amountInRbtc, rskFromAddress, mine = const txPromise = rskTxHelper.sendTransaction({ from: rskFromAddress, to: BRIDGE_ADDRESS, - value: Number(btcEthUnitConverter.btcToWeis(amountInRbtc)), + value: Number(btcEthUnitConverter.ethToWeis(amountInRbtc)), gasPrice: TO_BRIDGE_GAS_PRICE, }); if(!mine) { diff --git a/lib/tests/2wp.js b/lib/tests/2wp.js index ce456245..dde61add 100644 --- a/lib/tests/2wp.js +++ b/lib/tests/2wp.js @@ -863,6 +863,56 @@ const execute = (description, getRskHost) => { }); + it('should do a pegout and round down the weis to satoshis as expected', async () => { + + // Arrange + + // Create a pegin for the sender to ensure there is enough funds to pegout and because this is the natural process + const senderRecipientInfo = await createSenderRecipientInfo(rskTxHelper, btcTxHelper); + const peginValueInSatoshis = btcToSatoshis(0.5); + const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, senderRecipientInfo.btcSenderAddressInfo, satoshisToBtc(peginValueInSatoshis)); + await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash); + + const initial2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper); + const initialSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address); + const pegoutValueInRbtc = 0.041234567891234567; + const expectedPegoutValueInRbtc = 0.04123456; // rounded down + + // Act + + const pegoutTransaction = await sendTxToBridge(rskTxHelper, pegoutValueInRbtc, senderRecipientInfo.rskRecipientRskAddressInfo.address); + + // Assert + + let bridgeStateAfterPegoutCreation; + + // Callback to get the bridge state after the pegout is created + const pegoutCreatedCallback = async () => { + bridgeStateAfterPegoutCreation = await getBridgeState(rskTxHelper.getClient()); + }; + + const callbacks = { + pegoutCreatedCallback + }; + + await triggerRelease(rskTxHelpers, btcTxHelper, callbacks); + + // 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 assert2wpBalanceAfterSuccessfulPegout(initial2wpBalances, expectedPegoutValueInRbtc); + + // Assert that the sender address balance is increased by the actual pegout value + const finalSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address); + const releaseBtcEvent = pegoutsEvents[pegoutsEvents.length - 1]; + const releaseBtcTransaction = bitcoinJsLib.Transaction.fromHex(removePrefix0x(releaseBtcEvent.arguments.btcRawTransaction)); + const actualPegoutValueReceivedInSatoshis = releaseBtcTransaction.outs[0].value; + expect(finalSenderAddressBalanceInSatoshis).to.be.equal(initialSenderAddressBalanceInSatoshis + actualPegoutValueReceivedInSatoshis); + + }); + }); };