Skip to content

Commit

Permalink
Refactors deployCallReleaseBtcContract to pass the creator address
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-then authored and marcos-iov committed Nov 6, 2024
1 parent 39ae63d commit 253411e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
22 changes: 9 additions & 13 deletions lib/contractDeployer.js
Original file line number Diff line number Diff line change
@@ -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<Contract>} the deployed contract.
*/
const deployCallReleaseBtcContract = async (rskTxHelper, from) => {

const fullPath = path.resolve(__dirname, TEST_RELEASE_BTC_CONTRACT);
const source = fs.readFileSync(fullPath).toString();
Expand All @@ -25,14 +24,11 @@ const deployCallReleaseBtcContract = async (rskTxHelper) => {
[],
rskTxHelper,
{
from: address
from
}
);

return {
creatorAddress: address,
callReleaseBtcContract,
};
return callReleaseBtcContract;

};

Expand Down
10 changes: 10 additions & 0 deletions lib/rsk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -548,4 +557,5 @@ module.exports = {
sendTransaction,
getPegoutEventsInBlockRange,
setFeePerKb,
getNewFundedRskAddress,
};
10 changes: 8 additions & 2 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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 () => {
Expand Down

0 comments on commit 253411e

Please sign in to comment.