Skip to content

Commit

Permalink
Merge pull request #16 from rsksmart/use-base-unit
Browse files Browse the repository at this point in the history
Refactor 02_00_06-2wp-new-minimum tests to compare values using the base unit
  • Loading branch information
marcos-iov authored Oct 19, 2023
2 parents 8ac1bb3 + 537f38f commit d939477
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions tests/02_00_06-2wp-new-minimum.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { expect } = require('chai');
const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');
const { getBtcClient } = require('../lib/btc-client-provider');
const rskUtils = require('../lib/rsk-utils');
const { getLatestForkName, activateFork, triggerRelease } = require('../lib/rsk-utils');
const { sendPegin, ensurePeginIsRegistered, sendTxToBridge, BRIDGE_ADDRESS, MIN_PEGOUT_VALUE_IN_RBTC } = require('../lib/2wp-utils');
const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation');
const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util');
const btcEthUnitConverter = require('btc-eth-unit-converter');
const { btcToSatoshis, btcToWeis, satoshisToBtc } = require('btc-eth-unit-converter');

describe('2wp after iris300, using new minimum values', () => {
let rskTxHelpers;
Expand All @@ -16,8 +16,8 @@ describe('2wp after iris300, using new minimum values', () => {
let federationAddress;

const fulfillRequirementsToRunAsSingleTestFile = async () => {
const latestForkName = rskUtils.getLatestForkName()
await rskUtils.activateFork(latestForkName);
const latestForkName = getLatestForkName()
await activateFork(latestForkName);
};

before(async () => {
Expand All @@ -33,7 +33,7 @@ describe('2wp after iris300, using new minimum values', () => {
const latestActiveForkName = await getLatestActiveForkName();
bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);
const minimumPeginValueInSatoshi = await bridge.methods.getMinimumLockTxValue().call();
minimumPeginValueInBTC = btcEthUnitConverter.satoshisToBtc(minimumPeginValueInSatoshi);
minimumPeginValueInBTC = satoshisToBtc(minimumPeginValueInSatoshi);

//get federation address
federationAddress = await bridge.methods.getFederationAddress().call();
Expand All @@ -53,24 +53,25 @@ describe('2wp after iris300, using new minimum values', () => {
await btcTxHelper.fundAddress(senderAddressInfo.address, minimumPeginValueInBTC + btcTxHelper.getFee());

const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress));

const bridgeAddressBalanceInitial = Number(await rskTxHelper.getBalance(BRIDGE_ADDRESS));

// Execute peg-in
const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, senderAddressInfo, minimumPeginValueInBTC);
await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash);

const federationAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(federationAddress));
expect(federationAddressBalanceAfterPegin).to.be.equal(Number(federationAddressBalanceInitial + minimumPeginValueInBTC));
const expectedFederationAddressBalanceAfterPegin = federationAddressBalanceInitial + minimumPeginValueInBTC;
expect(btcToSatoshis(federationAddressBalanceAfterPegin)).to.be.equal(btcToSatoshis(expectedFederationAddressBalanceAfterPegin));

const bridgeAddressBalanceAfterPegin = Number(await rskTxHelper.getBalance(BRIDGE_ADDRESS));
expect(bridgeAddressBalanceAfterPegin).to.be.equal(bridgeAddressBalanceInitial - btcEthUnitConverter.btcToWeis(minimumPeginValueInBTC))
const expectedBridgeAddressBalanceAfterPegin = bridgeAddressBalanceInitial - btcToWeis(minimumPeginValueInBTC);
expect(bridgeAddressBalanceAfterPegin).to.be.equal(expectedBridgeAddressBalanceAfterPegin)

const senderAddressBalanceFinal = Number(await btcTxHelper.getAddressBalance(senderAddressInfo.address));
expect(senderAddressBalanceFinal).to.equal(0);

const recipientRskAddressBalanceFinal = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address));
expect(recipientRskAddressBalanceFinal).to.equal(btcEthUnitConverter.btcToWeis(minimumPeginValueInBTC));
expect(recipientRskAddressBalanceFinal).to.equal(btcToWeis(minimumPeginValueInBTC));
});

it('should not peg-in and not refund when sending below minimum value', async () => {
Expand All @@ -88,21 +89,23 @@ describe('2wp after iris300, using new minimum values', () => {
await sendPegin(rskTxHelper, btcTxHelper, senderAddressInfo, BELOW_MIN_PEGIN_VALUE_IN_BTC);

const federationAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(federationAddress));
expect(btcEthUnitConverter.btcToSatoshis(federationAddressBalanceAfterPegin)).to.be.equal(btcEthUnitConverter.btcToSatoshis(federationAddressBalanceInitial + BELOW_MIN_PEGIN_VALUE_IN_BTC));
const expectedFederationAddressBalanceAfterPegin = federationAddressBalanceInitial + BELOW_MIN_PEGIN_VALUE_IN_BTC;
expect(btcToSatoshis(federationAddressBalanceAfterPegin)).to.be.equal(btcToSatoshis(expectedFederationAddressBalanceAfterPegin));

const bridgeAddressBalanceAfterPegin = Number(await rskTxHelper.getBalance(BRIDGE_ADDRESS));
expect(bridgeAddressBalanceAfterPegin).to.be.equal(bridgeAddressBalanceInitial);

const senderAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(senderAddressInfo.address));
expect(senderAddressBalanceAfterPegin).to.be.equal(0);

await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper);
await triggerRelease(rskTxHelpers, btcTxHelper);

const senderAddressBalanceFinal = Number(await btcTxHelper.getAddressBalance(senderAddressInfo.address));
expect(senderAddressBalanceFinal).to.be.equal(0);

const federationAddressBalanceFinal = Number(await btcTxHelper.getAddressBalance(federationAddress));
expect(btcEthUnitConverter.btcToSatoshis(federationAddressBalanceFinal)).to.be.equal(btcEthUnitConverter.btcToSatoshis(federationAddressBalanceInitial + BELOW_MIN_PEGIN_VALUE_IN_BTC));
const expectedFederationAddressBalanceFinal = federationAddressBalanceInitial + BELOW_MIN_PEGIN_VALUE_IN_BTC;
expect(btcToSatoshis(federationAddressBalanceFinal)).to.be.equal(btcToSatoshis(expectedFederationAddressBalanceFinal));

const bridgeAddressBalanceFinal = Number(await rskTxHelper.getBalance(BRIDGE_ADDRESS));
expect(bridgeAddressBalanceFinal).to.be.equal(bridgeAddressBalanceInitial)
Expand Down Expand Up @@ -132,23 +135,28 @@ describe('2wp after iris300, using new minimum values', () => {
await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash);

const recipientRskAddressBalanceAfterPegin = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address));
expect(recipientRskAddressBalanceAfterPegin).to.be.equal(btcEthUnitConverter.btcToWeis(minimumPeginValueInBTC));
expect(recipientRskAddressBalanceAfterPegin).to.be.equal(btcToWeis(minimumPeginValueInBTC));

const federationAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(federationAddress));
expect(federationAddressBalanceAfterPegin).to.be.equal(Number(federationAddressBalanceInitial + minimumPeginValueInBTC));
const expectedFederationAddressBalanceAfterPegin = federationAddressBalanceInitial + minimumPeginValueInBTC;
expect(btcToSatoshis(federationAddressBalanceAfterPegin)).to.be.equal(btcToSatoshis(expectedFederationAddressBalanceAfterPegin));

// Execute peg-out
await sendTxToBridge(rskTxHelper, MIN_PEGOUT_VALUE_IN_RBTC, recipientRskAddressInfo.address);
await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper);
await triggerRelease(rskTxHelpers, btcTxHelper);

const senderAddressBalanceFinal = Number(await btcTxHelper.getAddressBalance(senderAddressInfo.address));
expect(senderAddressBalanceFinal).to.be.above(MIN_PEGOUT_VALUE_IN_RBTC - TX_FEE_IN_RBTC).and.below(MIN_PEGOUT_VALUE_IN_RBTC);
const maxExpectedSenderAddressBalanceFinal = btcToSatoshis(MIN_PEGOUT_VALUE_IN_RBTC);
const minExpectedSenderAddressBalanceFinal = btcToSatoshis(MIN_PEGOUT_VALUE_IN_RBTC - TX_FEE_IN_RBTC);
expect(btcToSatoshis(senderAddressBalanceFinal)).to.be.above(minExpectedSenderAddressBalanceFinal).and.below(maxExpectedSenderAddressBalanceFinal);

const federationAddressBalanceFinal = Number(await btcTxHelper.getAddressBalance(federationAddress));
expect(federationAddressBalanceFinal).to.be.equal(Number(federationAddressBalanceAfterPegin - MIN_PEGOUT_VALUE_IN_RBTC));

const expectedFederationAddressBalanceFinal = federationAddressBalanceAfterPegin - MIN_PEGOUT_VALUE_IN_RBTC;
expect(btcToSatoshis(federationAddressBalanceFinal)).to.be.equal(btcToSatoshis(expectedFederationAddressBalanceFinal));

const recipientRskAddressBalanceFinal = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address));
expect(recipientRskAddressBalanceFinal).to.be.above(btcEthUnitConverter.btcToWeis(minimumPeginValueInBTC - MIN_PEGOUT_VALUE_IN_RBTC - TX_FEE_IN_RBTC)).and.below(btcEthUnitConverter.btcToWeis(minimumPeginValueInBTC - MIN_PEGOUT_VALUE_IN_RBTC));
const maxExpectedRecipientRskAddressBalanceFinal = btcToWeis(minimumPeginValueInBTC - MIN_PEGOUT_VALUE_IN_RBTC);
const minExpectedRecipientRskAddressBalanceFinal = btcToWeis(minimumPeginValueInBTC - MIN_PEGOUT_VALUE_IN_RBTC - TX_FEE_IN_RBTC);
expect(recipientRskAddressBalanceFinal).to.be.above(minExpectedRecipientRskAddressBalanceFinal).and.below(maxExpectedRecipientRskAddressBalanceFinal);
});
});

0 comments on commit d939477

Please sign in to comment.