diff --git a/lib/contractDeployer.js b/lib/contractDeployer.js index 06507052..85f47a50 100644 --- a/lib/contractDeployer.js +++ b/lib/contractDeployer.js @@ -1,19 +1,18 @@ const fs = require('fs'); const path = require('path'); -const { btcToWeis } = require('@rsksmart/btc-eth-unit-converter'); const solUtils = require('./sol-utils'); const TEST_RELEASE_BTC_CONTRACT = '../contracts/CallReleaseBtcContract.sol'; const TEST_RELEASE_BTC_CONTRACT_NAME = 'CallReleaseBtcContract'; const SOLIDITY_COMPILER_VERSION = 'v0.8.26+commit.8a97fa7a'; -const { sendFromCow } = require('./rsk-utils'); -const { DEFAULT_RSK_ADDRESS_FUNDING_IN_BTC } = require('./constants'); -const deployCallReleaseBtcContract = async (rskTxHelper) => { - - const address = await rskTxHelper.getClient().eth.personal.newAccount(''); - await sendFromCow(rskTxHelper, address, Number(btcToWeis(DEFAULT_RSK_ADDRESS_FUNDING_IN_BTC))); - await rskTxHelper.getClient().eth.personal.unlockAccount(address, ''); +/** + * Deploys the CallReleaseBtcContract contract. + * @param {RskTransactionHelper} rskTxHelper + * @param {string} from the funded rsk address from which the contract will be deployed. + * @returns {Promise} the deployed contract. + */ +const deployCallReleaseBtcContract = async (rskTxHelper, from) => { const fullPath = path.resolve(__dirname, TEST_RELEASE_BTC_CONTRACT); const source = fs.readFileSync(fullPath).toString(); @@ -25,14 +24,11 @@ const deployCallReleaseBtcContract = async (rskTxHelper) => { [], rskTxHelper, { - from: address + from } ); - return { - creatorAddress: address, - callReleaseBtcContract, - }; + return callReleaseBtcContract; }; diff --git a/lib/rsk-utils.js b/lib/rsk-utils.js index 3e536c2c..bddd7409 100644 --- a/lib/rsk-utils.js +++ b/lib/rsk-utils.js @@ -11,8 +11,10 @@ const { FEE_PER_KB_CHANGER_PRIVATE_KEY, FEE_PER_KB_CHANGER_ADDRESS, FEE_PER_KB_RESPONSE_CODES, + DEFAULT_RSK_ADDRESS_FUNDING_IN_BTC, } = require('./constants'); const BtcTransactionHelper = require('btc-transaction-helper/btc-transaction-helper'); +const { ethToWeis } = require('@rsksmart/btc-eth-unit-converter'); const BTC_TO_RSK_MINIMUM_ACCEPTABLE_CONFIRMATIONS = 3; const RSK_TO_BTC_MINIMUM_ACCEPTABLE_CONFIRMATIONS = 3; @@ -529,6 +531,13 @@ const setFeePerKb = async (rskTxHelper, feePerKbInSatoshis) => { }; +const getNewFundedRskAddress = async (rskTxHelper, fundingAmountInRbtc = DEFAULT_RSK_ADDRESS_FUNDING_IN_BTC) => { + const address = await rskTxHelper.getClient().eth.personal.newAccount(''); + await sendFromCow(rskTxHelper, address, Number(ethToWeis(fundingAmountInRbtc))); + await rskTxHelper.getClient().eth.personal.unlockAccount(address, ''); + return address; +}; + module.exports = { mineAndSync, waitForBlock, @@ -548,4 +557,5 @@ module.exports = { sendTransaction, getPegoutEventsInBlockRange, setFeePerKb, + getNewFundedRskAddress, }; diff --git a/lib/tests/2wp.js b/lib/tests/2wp.js index afa82bb4..62cfdadb 100644 --- a/lib/tests/2wp.js +++ b/lib/tests/2wp.js @@ -5,7 +5,7 @@ const { getBridge } = require('../precompiled-abi-forks-util'); 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 { findEventInBlock, triggerRelease, getPegoutEventsInBlockRange, setFeePerKb, sendTransaction, getNewFundedRskAddress } = require('../rsk-utils'); const BridgeTransactionParser = require('@rsksmart/bridge-transaction-parser'); const { PEGIN_REJECTION_REASONS, @@ -828,8 +828,10 @@ const execute = (description, getRskHost) => { const initial2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper); const pegoutValueInSatoshis = MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS; - const { callReleaseBtcContract, creatorAddress } = await deployCallReleaseBtcContract(rskTxHelper); + const creatorAddress = await getNewFundedRskAddress(rskTxHelper); + const callReleaseBtcContract = await deployCallReleaseBtcContract(rskTxHelper, creatorAddress); const initialRskSenderBalanceInWeisBN = await rskTxHelper.getBalance(creatorAddress); + const initialContractBalanceInWeisBN = await rskTxHelper.getBalance(callReleaseBtcContract.options.address); // Act @@ -850,6 +852,10 @@ const execute = (description, getRskHost) => { const finalRskSenderBalanceInWeisBN = await rskTxHelper.getBalance(creatorAddress); expect(finalRskSenderBalanceInWeisBN.eq(expectedRskSenderBalanceInWeisBN)).to.be.true; + // The contract balance should be the same as the initial balance since the contract is not paying for the pegout + const finalContractBalanceInWeisBN = await rskTxHelper.getBalance(callReleaseBtcContract.options.address); + expect(finalContractBalanceInWeisBN.eq(initialContractBalanceInWeisBN)).to.be.true; + }); it('should do a pegout and round down the weis to satoshis as expected', async () => {