Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor 02_00_06-2wp-new-minimum tests to compare values using the base unit #16

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});

Loading