Skip to content

Commit

Permalink
Using MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-then committed Oct 30, 2024
1 parent cbba8d3 commit 913b560
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const { getBtcClient } = require('../btc-client-provider');
const { getRskTransactionHelper, getRskTransactionHelpers } = require('../rsk-tx-helper-provider');
const { satoshisToBtc, btcToSatoshis, satoshisToWeis } = require('@rsksmart/btc-eth-unit-converter');
const { findEventInBlock, triggerRelease, getPegoutEventsInBlockRange, setFeePerKb, sendTransaction } = require('../rsk-utils');
const { satoshisToBtc, btcToSatoshis, satoshisToWeis, btcToWeis } = require('@rsksmart/btc-eth-unit-converter');
const BridgeTransactionParser = require('@rsksmart/bridge-transaction-parser');
const {
PEGIN_REJECTION_REASONS,
Expand Down Expand Up @@ -827,27 +826,27 @@ const execute = (description, getRskHost) => {
// Arrange

const initial2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper);
const pegoutValueInRbtc = MINIMUM_PEGOUT_AMOUNT_IN_RBTC;
const pegoutValueInSatoshis = MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS;

const { callReleaseBtcContract, creatorAddress } = await deployCallReleaseBtcContract(rskTxHelper);
const initialRskSenderBalanceInWeisBN = await rskTxHelper.getBalance(creatorAddress);

// Act

const callBridgeReleaseBtcMethod = callReleaseBtcContract.methods.callBridgeReleaseBtc();
const contractCallTxReceipt = await sendTransaction(rskTxHelper, callBridgeReleaseBtcMethod, creatorAddress, btcToWeis(pegoutValueInRbtc));
const contractCallTxReceipt = await sendTransaction(rskTxHelper, callBridgeReleaseBtcMethod, creatorAddress, satoshisToWeis(pegoutValueInSatoshis));

// Assert

const rskSenderAddressChecksummed = rskTxHelper.getClient().utils.toChecksumAddress(ensure0x(callReleaseBtcContract.options.address));
const expectedEvent = createExpectedReleaseRequestRejectedEvent(rskSenderAddressChecksummed, btcToSatoshis(pegoutValueInRbtc), PEGOUT_REJECTION_REASONS.CALLER_CONTRACT);
const expectedEvent = createExpectedReleaseRequestRejectedEvent(rskSenderAddressChecksummed, pegoutValueInSatoshis, PEGOUT_REJECTION_REASONS.CALLER_CONTRACT);
const actualReleaseRequestRejectedEvent = getReleaseRequestRejectedEventFromContractCallTxReceipt(contractCallTxReceipt);
expect(actualReleaseRequestRejectedEvent).to.be.deep.equal(expectedEvent);

await assert2wpBalancesAfterPegoutFromContract(initial2wpBalances, pegoutValueInRbtc);
await assert2wpBalancesAfterPegoutFromContract(initial2wpBalances, pegoutValueInSatoshis);

// The rsk sender should lose the funds since there's no refund when a smart contract is trying to do a pegout
const expectedRskSenderBalanceInWeisBN = initialRskSenderBalanceInWeisBN.sub(new BN(`${btcToWeis(pegoutValueInRbtc)}`));
const expectedRskSenderBalanceInWeisBN = initialRskSenderBalanceInWeisBN.sub(new BN(`${satoshisToWeis(pegoutValueInSatoshis)}`));
const finalRskSenderBalanceInWeisBN = await rskTxHelper.getBalance(creatorAddress);
expect(finalRskSenderBalanceInWeisBN.eq(expectedRskSenderBalanceInWeisBN)).to.be.true;

Expand Down Expand Up @@ -941,15 +940,15 @@ const assert2wpBalanceIsUnchanged = async (initial2wpBalances) => {
expect(final2wpBalances).to.be.deep.equal(initial2wpBalances);
};

const assert2wpBalancesAfterPegoutFromContract = async (initial2wpBalances, pegoutValueInRbtc) => {
const assert2wpBalancesAfterPegoutFromContract = async (initial2wpBalances, pegoutValueInSatoshis) => {
const final2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper);

expect(final2wpBalances.federationAddressBalanceInSatoshis).to.be.equal(initial2wpBalances.federationAddressBalanceInSatoshis);

expect(final2wpBalances.bridgeUtxosBalanceInSatoshis).to.be.equal(initial2wpBalances.bridgeUtxosBalanceInSatoshis);
// When a contract sends funds to the Bridge to try to do a pegout, the pegout is rejected and the Bridge rsk balance is increased by the pegout value
// because there's no refund when a contract is trying to do a pegout.
const expectedBridgeBalanceInWeisBN = initial2wpBalances.bridgeBalanceInWeisBN.add(new BN(btcToWeis(pegoutValueInRbtc)));
const expectedBridgeBalanceInWeisBN = initial2wpBalances.bridgeBalanceInWeisBN.add(new BN(satoshisToWeis(pegoutValueInSatoshis)));

expect(final2wpBalances.bridgeBalanceInWeisBN.eq(expectedBridgeBalanceInWeisBN)).to.be.true;
};
Expand Down

0 comments on commit 913b560

Please sign in to comment.