diff --git a/lib/2wp-utils-legacy.js b/lib/2wp-utils-legacy.js index 6ba0fbad..77d11b54 100644 --- a/lib/2wp-utils-legacy.js +++ b/lib/2wp-utils-legacy.js @@ -7,7 +7,6 @@ const { getBridge, getLatestActiveForkName } = require('./precompiled-abi-forks- const { getBridgeState } = require('@rsksmart/bridge-state-data-parser'); const BTC_TO_RSK_MINIMUM_CONFIRMATIONS = 3; -const MINIMUM_PEGIN_VALUE_IN_BTC = 1; const assertEventFound = (rskClient) => async(eventName, callback, callBackParams, maxPastBlocksToCheck) => { let eventFound = await rskUtilsLegacy.getBridgeEventAndRunAssertions( @@ -239,5 +238,4 @@ module.exports = { isUtxoRegisteredInBridge, donateToBridge, BTC_TO_RSK_MINIMUM_CONFIRMATIONS, - MINIMUM_PEGIN_VALUE_IN_BTC, }; diff --git a/lib/2wp-utils.js b/lib/2wp-utils.js index 3f48228f..1ad2fb93 100644 --- a/lib/2wp-utils.js +++ b/lib/2wp-utils.js @@ -13,7 +13,6 @@ const ADDRESS_TYPES_CODES = { }; const BTC_TO_RSK_MINIMUM_CONFIRMATIONS = 3; -const MINIMUM_PEGIN_VALUE_IN_BTC = 1; const TO_BRIDGE_GAS_PRICE = 2; const BRIDGE_ADDRESS = '0x0000000000000000000000000000000001000006'; @@ -248,7 +247,6 @@ module.exports = { isUtxoRegisteredInBridge, donateToBridge, BTC_TO_RSK_MINIMUM_CONFIRMATIONS, - MINIMUM_PEGIN_VALUE_IN_BTC, BRIDGE_ADDRESS, createPeginV1TxData, mineForPeginRegistration, diff --git a/lib/tests/2wp.js b/lib/tests/2wp.js index 439ea36e..90648e72 100644 --- a/lib/tests/2wp.js +++ b/lib/tests/2wp.js @@ -8,15 +8,16 @@ const { getBtcClient } = require('../btc-client-provider'); const { getRskTransactionHelpers, getRskTransactionHelper } = require('../rsk-tx-helper-provider'); const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); const btcEthUnitConverter = require('btc-eth-unit-converter'); -const { sendTxToBridge, sendPegin, ensurePeginIsRegistered, MINIMUM_PEGIN_VALUE_IN_BTC, donateToBridge } = require('../2wp-utils'); +const { sendTxToBridge, sendPegin, ensurePeginIsRegistered, donateToBridge } = require('../2wp-utils'); const DONATION_AMOUNT = 250; const REJECTED_REASON = 1; -let federationAddress; let btcTxHelper; let rskTxHelper; let rskTxHelpers; +let federationAddress; +let minimumPeginValueInBtc; const execute = (description, getRskHost) => { @@ -29,6 +30,9 @@ const execute = (description, getRskHost) => { const bridge = getBridge(rskTxHelper.getClient(), await getLatestActiveForkName()); federationAddress = await bridge.methods.getFederationAddress().call(); + const minimumPeginValueInSatoshis = await bridge.methods.getMinimumLockTxValue().call(); + minimumPeginValueInBtc = btcEthUnitConverter.satoshisToBtc(minimumPeginValueInSatoshis); + await btcTxHelper.importAddress(federationAddress, 'federations'); rskTxHelpers = getRskTransactionHelpers(); @@ -54,26 +58,22 @@ const execute = (description, getRskHost) => { try { const peginSenderAddressInfo = await btcTxHelper.generateBtcAddress('legacy'); - await whitelistingAssertions.assertAddLimitedLockWhitelistAddress(rskTxHelper, peginSenderAddressInfo.address, btcEthUnitConverter.btcToSatoshis(MINIMUM_PEGIN_VALUE_IN_BTC)); + await whitelistingAssertions.assertAddLimitedLockWhitelistAddress(rskTxHelper, peginSenderAddressInfo.address, btcEthUnitConverter.btcToSatoshis(minimumPeginValueInBtc)); await rskUtils.mineAndSync(rskTxHelpers); const recipientRskAddressInfo = getDerivedRSKAddressInformation(peginSenderAddressInfo.privateKey, btcTxHelper.btcConfig.network); - const initialRskAddressBalanceInWeis = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); - await btcTxHelper.fundAddress(peginSenderAddressInfo.address, MINIMUM_PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); + await btcTxHelper.fundAddress(peginSenderAddressInfo.address, minimumPeginValueInBtc + btcTxHelper.getFee()); - const peginBtcTxHash = await sendPegin(rskTxHelper, btcTxHelper, peginSenderAddressInfo, MINIMUM_PEGIN_VALUE_IN_BTC); - + const peginBtcTxHash = await sendPegin(rskTxHelper, btcTxHelper, peginSenderAddressInfo, minimumPeginValueInBtc); await ensurePeginIsRegistered(rskTxHelper, peginBtcTxHash); const finalRskAddressBalanceInWeis = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); // Asserting that the received pegin amount in rsk is as expected - - const peginValueInWeis = btcEthUnitConverter.btcToWeis(MINIMUM_PEGIN_VALUE_IN_BTC); + const peginValueInWeis = btcEthUnitConverter.btcToWeis(minimumPeginValueInBtc); expect(finalRskAddressBalanceInWeis).to.equal(initialRskAddressBalanceInWeis + peginValueInWeis); - } catch (err) { throw new CustomError('Transfer BTC to RBTC', err); } diff --git a/tests/01_03_01-lock_whitelist_pre_papyrus.js b/tests/01_03_01-lock_whitelist_pre_papyrus.js index 6c07c58b..72ebf740 100644 --- a/tests/01_03_01-lock_whitelist_pre_papyrus.js +++ b/tests/01_03_01-lock_whitelist_pre_papyrus.js @@ -11,16 +11,16 @@ const { const rskUtils = require('../lib/rsk-utils'); const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); const { getBtcClient } = require('../lib/btc-client-provider'); -const { sendPegin, ensurePeginIsRegistered, MINIMUM_PEGIN_VALUE_IN_BTC } = require('../lib/2wp-utils'); +const { sendPegin, ensurePeginIsRegistered } = require('../lib/2wp-utils'); const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); const { btcToWeis, btcToSatoshis, satoshisToBtc } = require('btc-eth-unit-converter'); const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util'); -let federationAddress; let rskTxHelpers; let btcTxHelper; let rskTxHelper; let bridge; +let federationAddress; const EXPECTED_UNSUCCESSFUL_RESULT = -10; const FUND_AMOUNT_IN_WEIS = 1000000000; @@ -35,50 +35,56 @@ const WHITELIST_ADDRESSES = { const WHITELIST_RANDOM_PUBLIC_KEY = 'msJRGyaYvT8YNjvU3q9nPgBpZj9umAgetn'; +/** + * Takes the blockchain to the required state for this test file to run in isolation. + */ +const fulfillRequirementsToRunAsSingleTestFile = async () => { + await rskUtils.activateFork(Runners.common.forks.wasabi100); +}; + const assertLockCreatingWhiteListAddress = async (rskTxHelper, btcTxHelper, useUnlimitedWhitelist) => { + const minPeginValueInSatoshis = await bridge.methods.getMinimumLockTxValue().call(); + const minPeginValueInBtc = satoshisToBtc(minPeginValueInSatoshis); const btcAddressInformation = await btcTxHelper.generateBtcAddress('legacy'); - const recipientRskAddressInfo = getDerivedRSKAddressInformation(btcAddressInformation.privateKey, btcTxHelper.btcConfig.network); - const initialRskAddressBalanceInWeis = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); - - await btcTxHelper.fundAddress(btcAddressInformation.address, MINIMUM_PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); + await btcTxHelper.fundAddress(btcAddressInformation.address, minPeginValueInBtc + btcTxHelper.getFee()); if (useUnlimitedWhitelist) { await assertAddUnlimitedWhitelistAddress(rskTxHelper, btcAddressInformation.address); } else { - await assertAddOneOffWhitelistAddress(rskTxHelper, btcAddressInformation.address, btcToSatoshis(MINIMUM_PEGIN_VALUE_IN_BTC)); + await assertAddOneOffWhitelistAddress(rskTxHelper, btcAddressInformation.address, minPeginValueInSatoshis); } - await assertWhitelistAddressPresence(rskTxHelper, btcAddressInformation.address, true); - const peginBtcTxHash = await sendPegin(rskTxHelper, btcTxHelper, btcAddressInformation, MINIMUM_PEGIN_VALUE_IN_BTC); - + const peginBtcTxHash = await sendPegin(rskTxHelper, btcTxHelper, btcAddressInformation, minPeginValueInBtc); await ensurePeginIsRegistered(rskTxHelper, peginBtcTxHash); await assertWhitelistAddressPresence(rskTxHelper, btcAddressInformation.address, useUnlimitedWhitelist); const finalRskAddressBalanceInWeis = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); - - const peginValueInWeis = btcToWeis(MINIMUM_PEGIN_VALUE_IN_BTC); - + const peginValueInWeis = btcToWeis(minPeginValueInBtc); expect(finalRskAddressBalanceInWeis).to.equal(initialRskAddressBalanceInWeis + peginValueInWeis); - - }; +}; describe('Lock whitelisting', () => { before(async () => { + if(process.env.RUNNING_SINGLE_TEST_FILE) { + await fulfillRequirementsToRunAsSingleTestFile(); + } + rskTxHelpers = getRskTransactionHelpers(); btcTxHelper = getBtcClient(); rskTxHelper = rskTxHelpers[0]; bridge = getBridge(rskTxHelper.getClient(), await getLatestActiveForkName()); + federationAddress = await bridge.methods.getFederationAddress().call(); await btcTxHelper.importAddress(federationAddress, 'federations'); }); it(`should prevent calling addOneOffLockWhitelistAddress without a correct key`, async () => { - const addOneOffLockWhitelistAddressMethod = bridge.methods.addOneOffLockWhitelistAddress(WHITELIST_RANDOM_PUBLIC_KEY, 1000000000); + const addOneOffLockWhitelistAddressMethod = bridge.methods.addOneOffLockWhitelistAddress(WHITELIST_RANDOM_PUBLIC_KEY, FUND_AMOUNT_IN_WEIS); const rskTxSenderAddress = await rskTxHelper.newAccountWithSeed('test'); await rskUtils.sendFromCow(rskTxHelper, rskTxSenderAddress, FUND_AMOUNT_IN_WEIS); const checkCallback = callResult => { @@ -138,7 +144,7 @@ describe('Lock whitelisting', () => { const nonWhitelistedCasesAmounts = [[15], [5, 7], [1, 23, 4]]; nonWhitelistedCasesAmounts.forEach(testCaseAmounts => { - it(`should return BTC from non-whitelisted addresses using same UTXOs as for lock attempt (${testCaseAmounts.length} UTXOs)`, async () => { + it(`should return BTC from non-whitelisted addresses using same UTXOs as for peg-in attempt (${testCaseAmounts.length} UTXOs)`, async () => { const AMOUNT_TO_TRY_TO_LOCK = testCaseAmounts.reduce((a, b) => a + b); const MAX_EXPECTED_FEE = 0.001; // The extra 0.5 is to get some change back on the lock tx. This shouldn't really @@ -146,37 +152,28 @@ describe('Lock whitelisting', () => { const INITIAL_PEGIN_BALANCE = AMOUNT_TO_TRY_TO_LOCK + MAX_EXPECTED_FEE + 0.5; const btcAddressInformation = await btcTxHelper.generateBtcAddress('legacy'); - const recipientRskAddressInfo = getDerivedRSKAddressInformation(btcAddressInformation.privateKey, btcTxHelper.btcConfig.network); - await btcTxHelper.fundAddress(btcAddressInformation.address, INITIAL_PEGIN_BALANCE); - const initialFederationBalance = await btcTxHelper.getAddressBalance(federationAddress); const peginBtcTxHash = await sendPegin(rskTxHelper, btcTxHelper, btcAddressInformation, testCaseAmounts); - const peginBtcTx = await btcTxHelper.getTransaction(peginBtcTxHash); + const btcBalanceAfterPegin = await btcTxHelper.getAddressBalance(btcAddressInformation.address); + expect(btcToSatoshis(btcBalanceAfterPegin)).to.equal(btcToSatoshis(INITIAL_PEGIN_BALANCE - AMOUNT_TO_TRY_TO_LOCK - MAX_EXPECTED_FEE), 'Lock BTC debit'); - const btcBalance = await btcTxHelper.getAddressBalance(btcAddressInformation.address); - - expect(btcToSatoshis(btcBalance)).to.equal(btcToSatoshis(INITIAL_PEGIN_BALANCE - AMOUNT_TO_TRY_TO_LOCK - MAX_EXPECTED_FEE), 'Lock BTC debit'); - - const federationBalance = await btcTxHelper.getAddressBalance(federationAddress); - - expect(btcToSatoshis(federationBalance)).to.equal(btcToSatoshis(initialFederationBalance + AMOUNT_TO_TRY_TO_LOCK), `Lock BTC federation ${federationAddress} credit`); + const federationBalanceAfterPegin = await btcTxHelper.getAddressBalance(federationAddress); + expect(btcToSatoshis(federationBalanceAfterPegin)).to.equal(btcToSatoshis(initialFederationBalance + AMOUNT_TO_TRY_TO_LOCK), `Lock BTC federation ${federationAddress} credit`); await rskTxHelper.updateBridge(); await rskUtils.mineAndSync(rskTxHelpers); + const initialBlockNumber = await rskTxHelper.getBlockNumber(); - await rskUtils.mineAndSync(rskTxHelpers); - const currentBlockNumber = await rskTxHelper.getBlockNumber(); expect(currentBlockNumber).to.equal(initialBlockNumber + 1); - const currentRskBalance = await rskTxHelper.getBalance(recipientRskAddressInfo.address); - - expect(Number(currentRskBalance), 'Wrong RSK balance').to.equal(0); + const currentRskBalance = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); + expect(currentRskBalance, 'Wrong RSK balance').to.equal(0); await rskUtils.triggerRelease(rskTxHelpers, getBtcClient()); @@ -189,13 +186,10 @@ describe('Lock whitelisting', () => { expect(finalFederationBalance).to.equal(initialFederationBalance); const utxos = await btcTxHelper.getUtxos(btcAddressInformation.address); - const nonLockUtxos = utxos.filter(utxo => utxo.txid !== peginBtcTxHash); - expect(nonLockUtxos.length).to.equal(1); const returnUtxo = nonLockUtxos[0]; - expect(AMOUNT_TO_TRY_TO_LOCK - returnUtxo.amount).to.be.at.most(MAX_EXPECTED_FEE); const returnTx = await btcTxHelper.getTransaction(returnUtxo.txid); @@ -214,52 +208,43 @@ describe('Lock whitelisting', () => { }); expect(nonMatchedAmounts.length).to.equal(0); - }); }); it('should prevent locking RBTC when transfer is above maximum whitelisted', async () => { - - const WHITELISTED_MAX_VALUE = MINIMUM_PEGIN_VALUE_IN_BTC - 0.1; + const PEGIN_VALUE_IN_BTC = 1; + const WHITELISTED_MAX_VALUE = PEGIN_VALUE_IN_BTC - 0.1; const btcAddressInformation = await btcTxHelper.generateBtcAddress('legacy'); const recipientRskAddressInfo = getDerivedRSKAddressInformation(btcAddressInformation.privateKey, btcTxHelper.btcConfig.network); - - await btcTxHelper.fundAddress(btcAddressInformation.address, MINIMUM_PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); + await btcTxHelper.fundAddress(btcAddressInformation.address, PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); const initialFederationBalance = await btcTxHelper.getAddressBalance(federationAddress); await assertAddOneOffWhitelistAddress(rskTxHelper, btcAddressInformation.address, btcToSatoshis(WHITELISTED_MAX_VALUE)); - await sendPegin(rskTxHelper, btcTxHelper, btcAddressInformation, MINIMUM_PEGIN_VALUE_IN_BTC); - - const midBtcAddressBalance = await btcTxHelper.getAddressBalance(btcAddressInformation.address); - - expect(midBtcAddressBalance).to.equal(0, 'At this point the btc address should not have any balance'); + await sendPegin(rskTxHelper, btcTxHelper, btcAddressInformation, PEGIN_VALUE_IN_BTC); - const finalFederationBalance = await btcTxHelper.getAddressBalance(federationAddress); + const btcAddressBalanceAfterPegin = await btcTxHelper.getAddressBalance(btcAddressInformation.address); + expect(btcAddressBalanceAfterPegin).to.equal(0, 'At this point the btc address should not have any balance'); - expect(finalFederationBalance).to.equal(initialFederationBalance + MINIMUM_PEGIN_VALUE_IN_BTC, 'The federation address should have its balance increased by the pegin amount'); + const federationBalanceAfterPegin = await btcTxHelper.getAddressBalance(federationAddress); + expect(federationBalanceAfterPegin).to.equal(initialFederationBalance + PEGIN_VALUE_IN_BTC, 'The federation address should have its balance increased by the pegin amount'); await rskTxHelper.updateBridge(); await rskUtils.mineAndSync(rskTxHelpers); - const currentRskBalance = await rskTxHelper.getBalance(recipientRskAddressInfo.address); - - expect(Number(currentRskBalance)).to.equal(0, 'The recipient rsk address should not have any balance'); + const recipientRskAddressBalance = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); + expect(recipientRskAddressBalance).to.equal(0, 'The recipient rsk address should not have any balance'); // At this point there is a pegout waiting for confirmations, let's release it. await rskUtils.triggerRelease(rskTxHelpers, getBtcClient()); - const finalBtcAddressBalance = await btcTxHelper.getAddressBalance(btcAddressInformation.address); - - const expectedFinalBtcAddressBalance = MINIMUM_PEGIN_VALUE_IN_BTC - btcTxHelper.getFee(); - - expect(expectedFinalBtcAddressBalance).to.be.at.most(finalBtcAddressBalance, 'The btc address should have its balance decreased by about the pegin amount and the fee'); - + const btcAddressBalanceAfterRefund = await btcTxHelper.getAddressBalance(btcAddressInformation.address); + expect(btcAddressBalanceAfterRefund).to.be.within(PEGIN_VALUE_IN_BTC - btcTxHelper.getFee(), PEGIN_VALUE_IN_BTC, 'The btc address should have its balance decreased by about the pegin amount and the fee'); }); it('should transfer BTC to RBTC from ONE-OFF whitelisted addresses', async () => { await assertLockCreatingWhiteListAddress(rskTxHelper, btcTxHelper, false); }); -}); \ No newline at end of file +}); diff --git a/tests/01_03_04-2wp_segwit_not_compatible.js b/tests/01_03_04-2wp_segwit_not_compatible.js index 6b36d9b6..0334c8a7 100644 --- a/tests/01_03_04-2wp_segwit_not_compatible.js +++ b/tests/01_03_04-2wp_segwit_not_compatible.js @@ -1,9 +1,9 @@ const expect = require('chai').expect +const { satoshisToBtc } = require('btc-eth-unit-converter'); const rskUtils = require('../lib/rsk-utils'); const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); - const { getBtcClient } = require('../lib/btc-client-provider'); -const { sendPegin, MINIMUM_PEGIN_VALUE_IN_BTC } = require('../lib/2wp-utils'); +const { sendPegin } = require('../lib/2wp-utils'); const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util'); const { isUtxoRegisteredInBridge } = require('../lib/2wp-utils'); @@ -27,20 +27,22 @@ describe('Lock p2sh-p2wpkh address', () => { }); it('lock should fail when using p2sh-p2wpkh address', async () => { - const senderAddress = await btcTxHelper.generateBtcAddress('p2sh-segwit') - const latestActiveForkName = await getLatestActiveForkName(); const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName); - const federationAddress = await bridge.methods.getFederationAddress().call(); + const minimumPeginValueInSatoshis = await bridge.methods.getMinimumLockTxValue().call(); + const minimumPeginValueInBtc = satoshisToBtc(minimumPeginValueInSatoshis); + + const federationAddress = await bridge.methods.getFederationAddress().call(); const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress)); - await btcTxHelper.fundAddress(senderAddress.address, MINIMUM_PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); + const senderAddress = await btcTxHelper.generateBtcAddress('p2sh-segwit') + await btcTxHelper.fundAddress(senderAddress.address, minimumPeginValueInBtc + btcTxHelper.getFee()); - const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, senderAddress, MINIMUM_PEGIN_VALUE_IN_BTC); + const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, senderAddress, minimumPeginValueInBtc); const federationAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(federationAddress)); - expect(Number(federationAddressBalanceAfterPegin)).to.be.equal(Number(federationAddressBalanceInitial + MINIMUM_PEGIN_VALUE_IN_BTC)) + expect(Number(federationAddressBalanceAfterPegin)).to.be.equal(Number(federationAddressBalanceInitial + minimumPeginValueInBtc)) await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper); @@ -48,7 +50,7 @@ describe('Lock p2sh-p2wpkh address', () => { expect(isPeginUtxoRegistered).to.be.false; const federationAddressBalanceFinal = Number(await btcTxHelper.getAddressBalance(federationAddress)); - expect(Number(federationAddressBalanceFinal)).to.be.equal(Number(federationAddressBalanceInitial + MINIMUM_PEGIN_VALUE_IN_BTC)) + expect(Number(federationAddressBalanceFinal)).to.be.equal(Number(federationAddressBalanceInitial + minimumPeginValueInBtc)) const senderAddressBalanceFinal = await btcTxHelper.getAddressBalance(senderAddress.address); expect(Number(senderAddressBalanceFinal)).to.be.equal(0) diff --git a/tests/01_03_05-2wp_multisig_not_compatible.js b/tests/01_03_05-2wp_multisig_not_compatible.js index ef6bc051..cd771613 100644 --- a/tests/01_03_05-2wp_multisig_not_compatible.js +++ b/tests/01_03_05-2wp_multisig_not_compatible.js @@ -1,9 +1,9 @@ const expect = require('chai').expect +const { satoshisToBtc } = require('btc-eth-unit-converter'); const rskUtils = require('../lib/rsk-utils'); const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); - const { getBtcClient } = require('../lib/btc-client-provider'); -const { sendPegin, MINIMUM_PEGIN_VALUE_IN_BTC } = require('../lib/2wp-utils'); +const { sendPegin } = require('../lib/2wp-utils'); const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util'); const { isUtxoRegisteredInBridge } = require('../lib/2wp-utils'); @@ -15,7 +15,7 @@ const fulfillRequirementsToRunAsSingleTestFile = async () => { await rskUtils.activateFork(Runners.common.forks.wasabi100); }; -describe('Lock multisig address', () => { +describe('Peg-in multisig address', () => { before(async () => { rskTxHelpers = getRskTransactionHelpers(); rskTxHelper = rskTxHelpers[0]; @@ -27,30 +27,32 @@ describe('Lock multisig address', () => { }); // Should fail and not refund when sending a pengin from a multisig address pre papyrus - it('lock should fail when using multisig address', async () => { - const senderAddress = await btcTxHelper.generateMultisigAddress(3, 2, 'legacy'); - + it('Peg-in should fail when using multisig address', async () => { const latestActiveForkName = await getLatestActiveForkName(); const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName); - const federationAddress = await bridge.methods.getFederationAddress().call(); + const minimumPeginValueInSatoshis = await bridge.methods.getMinimumLockTxValue().call(); + const minimumPeginValueInBtc = satoshisToBtc(minimumPeginValueInSatoshis); + + const federationAddress = await bridge.methods.getFederationAddress().call(); const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress)); - await btcTxHelper.fundAddress(senderAddress.address, MINIMUM_PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); + const senderAddress = await btcTxHelper.generateMultisigAddress(3, 2, 'legacy'); + await btcTxHelper.fundAddress(senderAddress.address, minimumPeginValueInBtc + btcTxHelper.getFee()); - const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, senderAddress, MINIMUM_PEGIN_VALUE_IN_BTC); + const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, senderAddress, minimumPeginValueInBtc); const federationAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(federationAddress)); - expect(Number(federationAddressBalanceAfterPegin)).to.be.equal(Number(federationAddressBalanceInitial + MINIMUM_PEGIN_VALUE_IN_BTC)); + expect(Number(federationAddressBalanceAfterPegin)).to.be.equal(Number(federationAddressBalanceInitial + minimumPeginValueInBtc)); const isPeginUtxoRegistered = await isUtxoRegisteredInBridge(rskTxHelper, btcPeginTxHash); expect(isPeginUtxoRegistered).to.be.false; const federationAddressBalanceFinal = Number(await btcTxHelper.getAddressBalance(federationAddress)); - expect(Number(federationAddressBalanceFinal)).to.be.equal(Number(federationAddressBalanceInitial + MINIMUM_PEGIN_VALUE_IN_BTC)); + expect(Number(federationAddressBalanceFinal)).to.be.equal(Number(federationAddressBalanceInitial + minimumPeginValueInBtc)); const senderAddressBalanceFinal = await btcTxHelper.getAddressBalance(senderAddress.address); expect(Number(senderAddressBalanceFinal)).to.be.equal(0); - }) + }); }); diff --git a/tests/01_03_06-2wp_no_locking_cap.js b/tests/01_03_06-2wp_no_locking_cap.js index ab3e3670..066ca88c 100644 --- a/tests/01_03_06-2wp_no_locking_cap.js +++ b/tests/01_03_06-2wp_no_locking_cap.js @@ -2,7 +2,7 @@ const expect = require('chai').expect const rskUtils = require('../lib/rsk-utils'); const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); const { getBtcClient } = require('../lib/btc-client-provider'); -const { sendPegin, ensurePeginIsRegistered, sendTxToBridge, MINIMUM_PEGIN_VALUE_IN_BTC } = require('../lib/2wp-utils'); +const { sendPegin, ensurePeginIsRegistered, sendTxToBridge } = require('../lib/2wp-utils'); const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util'); const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); const btcEthUnitConverter = require('btc-eth-unit-converter'); @@ -14,7 +14,7 @@ describe('Transfer BTC to RBTC before papyrus200', function() { let btcTxHelper; let rskTxHelper; - const PEGIN_VALUE_IN_BTC = 3 * MINIMUM_PEGIN_VALUE_IN_BTC; + const PEGIN_VALUE_IN_BTC = 3; const PEGOUT_VALUE_IN_RBTC = PEGIN_VALUE_IN_BTC / 3; const RSK_TX_FEE_IN_RBTC = 0.001; diff --git a/tests/02_00_02-2wp_segwit_compatible.js b/tests/02_00_02-2wp_segwit_compatible.js index 64edc27c..636e5a5d 100644 --- a/tests/02_00_02-2wp_segwit_compatible.js +++ b/tests/02_00_02-2wp_segwit_compatible.js @@ -1,18 +1,16 @@ const expect = require('chai').expect +const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); +const btcEthUnitConverter = require('btc-eth-unit-converter'); const rskUtils = require('../lib/rsk-utils'); const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); -const { sendPegin, MINIMUM_PEGIN_VALUE_IN_BTC, ensurePeginIsRegistered } = require('../lib/2wp-utils'); +const { sendPegin, ensurePeginIsRegistered } = require('../lib/2wp-utils'); const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util'); const { getBtcClient } = require('../lib/btc-client-provider'); -const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); -const btcEthUnitConverter = require('btc-eth-unit-converter'); - let rskTxHelpers; let rskTxHelper; let btcTxHelper; - const fulfillRequirementsToRunAsSingleTestFile = async () => { const latestForkName = rskUtils.getLatestForkName() await rskUtils.activateFork(latestForkName); @@ -30,30 +28,33 @@ describe('Lock using p2sh-p2wpkh address', () => { }); it('should do a legacy pegin using p2sh-p2wpkh address', async () => { - const senderAddressInfo = await btcTxHelper.generateBtcAddress('p2sh-segwit'); - const latestActiveForkName = await getLatestActiveForkName(); const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName); + + const minimumPeginValueInSatoshis = await bridge.methods.getMinimumLockTxValue().call(); + const minimumPeginValueInBtc = btcEthUnitConverter.satoshisToBtc(minimumPeginValueInSatoshis); + const federationAddress = await bridge.methods.getFederationAddress().call(); const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress)); + const senderAddressInfo = await btcTxHelper.generateBtcAddress('p2sh-segwit'); const recipientRskAddressInfo = getDerivedRSKAddressInformation(senderAddressInfo.privateKey, btcTxHelper.btcConfig.network); await rskTxHelper.importAccount(recipientRskAddressInfo.privateKey); const unlocked = await rskTxHelper.unlockAccount(recipientRskAddressInfo.address); expect(unlocked, 'Account was not unlocked').to.be.true; - await btcTxHelper.fundAddress(senderAddressInfo.address, MINIMUM_PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); + await btcTxHelper.fundAddress(senderAddressInfo.address, minimumPeginValueInBtc + btcTxHelper.getFee()); - const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, senderAddressInfo, MINIMUM_PEGIN_VALUE_IN_BTC); + 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 + MINIMUM_PEGIN_VALUE_IN_BTC)); + expect(federationAddressBalanceAfterPegin).to.be.equal(Number(federationAddressBalanceInitial + minimumPeginValueInBtc)); const senderAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(senderAddressInfo.address)); expect(senderAddressBalanceAfterPegin).to.be.equal(0); const recipientRskAddressBalanceAfterPegin = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); - expect(recipientRskAddressBalanceAfterPegin).to.be.equal(btcEthUnitConverter.btcToWeis(MINIMUM_PEGIN_VALUE_IN_BTC)); + expect(recipientRskAddressBalanceAfterPegin).to.be.equal(btcEthUnitConverter.btcToWeis(minimumPeginValueInBtc)); }); }); diff --git a/tests/02_00_03-2wp_multisig_compatible.js b/tests/02_00_03-2wp_multisig_compatible.js index 371a6b99..20c17b3e 100644 --- a/tests/02_00_03-2wp_multisig_compatible.js +++ b/tests/02_00_03-2wp_multisig_compatible.js @@ -1,8 +1,9 @@ const expect = require('chai').expect +const { satoshisToBtc } = require('btc-eth-unit-converter'); const rskUtils = require('../lib/rsk-utils'); const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); const { getBtcClient } = require('../lib/btc-client-provider'); -const { sendPegin, MINIMUM_PEGIN_VALUE_IN_BTC } = require('../lib/2wp-utils'); +const { sendPegin } = require('../lib/2wp-utils'); const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util'); let rskTxHelpers; @@ -31,17 +32,19 @@ describe('Lock multisig address', () => { it('lock should work when using multisig address', async () => { const latestActiveForkName = await getLatestActiveForkName(); const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName); + + const minimumPeginValueInSatoshis = await bridge.methods.getMinimumLockTxValue().call(); + const minimumPeginValueInBtc = satoshisToBtc(minimumPeginValueInSatoshis); + const federationAddress = await bridge.methods.getFederationAddress().call(); + const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress)); const senderAddressInfo = await btcTxHelper.generateMultisigAddress(3, 2, 'legacy'); - - const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress)); - - await btcTxHelper.fundAddress(senderAddressInfo.address, MINIMUM_PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); - await sendPegin(rskTxHelper, btcTxHelper, senderAddressInfo, MINIMUM_PEGIN_VALUE_IN_BTC); + await btcTxHelper.fundAddress(senderAddressInfo.address, minimumPeginValueInBtc + btcTxHelper.getFee()); + await sendPegin(rskTxHelper, btcTxHelper, senderAddressInfo, minimumPeginValueInBtc); const federationAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(federationAddress)); - expect(Number(federationAddressBalanceAfterPegin)).to.be.equal(Number(federationAddressBalanceInitial + MINIMUM_PEGIN_VALUE_IN_BTC)) + expect(Number(federationAddressBalanceAfterPegin)).to.be.equal(Number(federationAddressBalanceInitial + minimumPeginValueInBtc)) const senderAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(senderAddressInfo.address)); expect(Number(senderAddressBalanceAfterPegin)).to.be.equal(0) @@ -52,7 +55,6 @@ describe('Lock multisig address', () => { expect(Number(finalFederationAddressBalance)).to.be.equal(Number(federationAddressBalanceInitial)) const finalSenderAddressBalance = await btcTxHelper.getAddressBalance(senderAddressInfo.address); - expect(Number(finalSenderAddressBalance)).to.be.above(MINIMUM_PEGIN_VALUE_IN_BTC - btcTxHelper.getFee()).and.below(MINIMUM_PEGIN_VALUE_IN_BTC) - } - ); + expect(Number(finalSenderAddressBalance)).to.be.above(minimumPeginValueInBtc - btcTxHelper.getFee()).and.below(minimumPeginValueInBtc) + }); });