Skip to content

Commit

Permalink
Adds 'should reject and refund a pegout with value exactly below mini…
Browse files Browse the repository at this point in the history
…mum' test
  • Loading branch information
jeremy-then committed Oct 22, 2024
1 parent d2040bb commit 71b037b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
15 changes: 14 additions & 1 deletion lib/2wp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { getBridge } = require('./precompiled-abi-forks-util');
const { getBridgeState } = require('@rsksmart/bridge-state-data-parser');
const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation');
const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter');
const { PEGIN_EVENTS } = require("./constants");
const { PEGIN_EVENTS, PEGOUT_EVENTS } = require("./constants");

const peginVerifier = require('pegin-address-verificator');
const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');
Expand Down Expand Up @@ -337,6 +337,18 @@ const createExpectedUnrefundablePeginEvent = (btcPeginTxHash, rejectionReason) =
return expectedEvent;
};

const createExpectedReleaseRequestRejectedEvent = (rskSenderAddress, amountInSatoshis, rejectionReason) => {
const expectedEvent = {
...PEGOUT_EVENTS.RELEASE_REQUEST_REJECTED,
arguments: {
sender: rskSenderAddress,
amount: amountInSatoshis,
reason: rejectionReason,
},
}
return expectedEvent;
};

module.exports = {
sendTxToBridge,
assertRefundUtxosSameAsPeginUtxos,
Expand All @@ -356,4 +368,5 @@ module.exports = {
get2wpBalances,
createExpectedRejectedPeginEvent,
createExpectedUnrefundablePeginEvent,
createExpectedReleaseRequestRejectedEvent,
};
10 changes: 10 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ const PEGIN_UNREFUNDABLE_REASONS = {
INVALID_AMOUNT: '3',
};

const PEGOUT_REJECTION_REASONS = {
LOW_AMOUNT: '1',
CALLER_CONTRACT: '2',
FEE_ABOVE_VALUE: '3',
};

const PEGOUT_EVENTS = {
RELEASE_REQUEST_RECEIVED: {
name: 'release_request_received',
Expand Down Expand Up @@ -108,6 +114,8 @@ const PEGIN_EVENTS = {

const PEGIN_V1_RSKT_PREFIX_HEX = '52534b54';

const MINIMUM_PEGOUT_AMOUNT_IN_RBTC = 0.0025;

module.exports = {
KEY_TYPE_BTC,
KEY_TYPE_RSK,
Expand All @@ -131,4 +139,6 @@ module.exports = {
PEGIN_EVENTS,
PEGIN_UNREFUNDABLE_REASONS,
PEGIN_V1_RSKT_PREFIX_HEX,
PEGOUT_REJECTION_REASONS,
MINIMUM_PEGOUT_AMOUNT_IN_RBTC,
};
57 changes: 55 additions & 2 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ const { getBtcClient } = require('../btc-client-provider');
const { getRskTransactionHelper, getRskTransactionHelpers } = require('../rsk-tx-helper-provider');
const { satoshisToBtc, btcToSatoshis, satoshisToWeis, btcToWeis } = require('@rsksmart/btc-eth-unit-converter');
const { findEventInBlock, triggerRelease, getPegoutEventsInBlockRange } = require('../rsk-utils');
const { PEGIN_REJECTION_REASONS, PEGIN_UNREFUNDABLE_REASONS, PEGOUT_EVENTS, PEGIN_V1_RSKT_PREFIX_HEX } = require("../constants");
const {
PEGIN_REJECTION_REASONS,
PEGIN_UNREFUNDABLE_REASONS,
PEGOUT_EVENTS,
PEGIN_V1_RSKT_PREFIX_HEX,
PEGOUT_REJECTION_REASONS,
MINIMUM_PEGOUT_AMOUNT_IN_RBTC,
} = require("../constants");
const { sendPegin,
ensurePeginIsRegistered,
createSenderRecipientInfo,
Expand All @@ -16,6 +23,7 @@ const { sendPegin,
createExpectedUnrefundablePeginEvent,
assertRefundUtxosSameAsPeginUtxos,
sendTxToBridge,
createExpectedReleaseRequestRejectedEvent,
} = require('../2wp-utils');
const { getBtcAddressBalanceInSatoshis } = require('../btc-utils');
const { ensure0x, removePrefix0x } = require('../utils');
Expand Down Expand Up @@ -694,7 +702,7 @@ const execute = (description, getRskHost) => {

const initial2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper);
const initialSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address);
const pegoutValueInRbtc = 0.0025;
const pegoutValueInRbtc = MINIMUM_PEGOUT_AMOUNT_IN_RBTC;

// Act

Expand Down Expand Up @@ -731,6 +739,44 @@ const execute = (description, getRskHost) => {

});

it('should reject and refund a pegout with value exactly below minimum', async () => {

// Arrange

// Create a pegin for the serder 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 initialBtcRecipientAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address);
const initialRskSenderBalanceInWeisBN = await rskTxHelper.getBalance(senderRecipientInfo.rskRecipientRskAddressInfo.address);
// Value exactly below minimum
const pegoutValueInRbtc = parseFloat((MINIMUM_PEGOUT_AMOUNT_IN_RBTC - 0.0001).toFixed(8));

// Act

const pegoutTransaction = await sendTxToBridge(rskTxHelper, pegoutValueInRbtc, senderRecipientInfo.rskRecipientRskAddressInfo.address);

// Assert

await assertExpectedReleaseRequestRejectedEventIsEmitted(senderRecipientInfo.rskRecipientRskAddressInfo.address, btcToSatoshis(pegoutValueInRbtc), PEGOUT_REJECTION_REASONS.LOW_AMOUNT);

await assert2wpBalanceIsUnchanged(initial2wpBalances);

// The rsk sender balance is the same as the initial balance minus the gas fee, because the pegout amount was refunded.
const finalRskSenderBalanceInWeisBN = await rskTxHelper.getBalance(senderRecipientInfo.rskRecipientRskAddressInfo.address);
const gasFee = pegoutTransaction.gasUsed * pegoutTransaction.effectiveGasPrice;
const expectedRskSenderBalanceInWeisBN = initialRskSenderBalanceInWeisBN.sub(new BN(`${gasFee}`));
expect(finalRskSenderBalanceInWeisBN.eq(expectedRskSenderBalanceInWeisBN)).to.be.true;

// The btc recipient address balance is the same as the initial balance, because the pegout didn't go though.
const finalBtcRecipientBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address);
expect(finalBtcRecipientBalanceInSatoshis).to.be.equal(initialBtcRecipientAddressBalanceInSatoshis);

});

});

};
Expand Down Expand Up @@ -887,6 +933,13 @@ const assert2wpBalanceAfterSuccessfulPegout = async (initial2wpBalances, pegoutV

};

const assertExpectedReleaseRequestRejectedEventIsEmitted = async (rskSenderAddress, amountInSatoshis, rejectionReason) => {
const rskSenderAddressChecksummed = rskTxHelper.getClient().utils.toChecksumAddress(ensure0x(rskSenderAddress));
const expectedEvent = createExpectedReleaseRequestRejectedEvent(rskSenderAddressChecksummed, amountInSatoshis, rejectionReason);
const releaseRequestRejectedEvent = await findEventInBlock(rskTxHelper, PEGOUT_EVENTS.RELEASE_REQUEST_REJECTED.name);
expect(releaseRequestRejectedEvent).to.be.deep.equal(expectedEvent);
};

module.exports = {
execute,
};
Expand Down

0 comments on commit 71b037b

Please sign in to comment.