Skip to content

Commit

Permalink
Adds 'should do a pegout and round down the weis to satoshis as expec…
Browse files Browse the repository at this point in the history
…ted' test.
  • Loading branch information
jeremy-then committed Oct 28, 2024
1 parent 11c5790 commit 62423df
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/2wp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
50 changes: 50 additions & 0 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

});

});

};
Expand Down

0 comments on commit 62423df

Please sign in to comment.