Skip to content

Commit

Permalink
Refactors sendTxToBridge to receive weisBN. Using MINIMUM_PEGOUT_AMOU…
Browse files Browse the repository at this point in the history
…NT_IN_SATOSHIS and DEFAULT_RSK_ADDRESS_FUNDING_IN_BTC.
  • Loading branch information
jeremy-then committed Oct 30, 2024
1 parent 8f4a567 commit 55cadae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
26 changes: 21 additions & 5 deletions lib/2wp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ const {
waitForRskMempoolToGetNewTxs,
waitAndUpdateBridge
} = require('./rsk-utils');
const BN = require('bn.js');
const { retryWithCheck, ensure0x } = require('./utils');
const { waitForBitcoinTxToBeInMempool, waitForBitcoinMempoolToGetTxs, getBtcAddressBalanceInSatoshis } = require('./btc-utils');
const { getBridge } = require('./precompiled-abi-forks-util');
const { getBridgeState } = require('@rsksmart/bridge-state-data-parser');
const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation');
const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter');
const { PEGIN_EVENTS } = require("./constants");
const { PEGIN_EVENTS, DEFAULT_RSK_ADDRESS_FUNDING_IN_BTC } = require("./constants");

const peginVerifier = require('pegin-address-verificator');
const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');
Expand Down Expand Up @@ -55,16 +56,16 @@ const assertRefundUtxosSameAsPeginUtxos = async(rskTxHelper, btcTxHelper, peginT
/**
* Sends a tx to the bridge
* @param {RskTransactionHelper} rskTxHelper
* @param {number} amountInRbtc
* @param {BN} amountInWeisBN
* @param {string} rskFromAddress
* @param {boolean} mine If true, mines 1 block after sending the transaction. If false, it will not mine the tx and will return undefined. Defaults to true.
* @returns {web3.eth.TransactionReceipt | undefined}
*/
const sendTxToBridge = async (rskTxHelper, amountInRbtc, rskFromAddress, mine = true) => {
const sendTxToBridge = async (rskTxHelper, amountInWeisBN, rskFromAddress, mine = true) => {
const txPromise = rskTxHelper.sendTransaction({
from: rskFromAddress,
to: BRIDGE_ADDRESS,
value: Number(btcEthUnitConverter.btcToWeis(amountInRbtc)),
value: amountInWeisBN,
gasPrice: TO_BRIDGE_GAS_PRICE,
});
if(!mine) {
Expand Down Expand Up @@ -95,7 +96,7 @@ const createPegoutRequest = async (rskTxHelper, amountInRBTC, requestSize = 1) =
await sendFromCow(rskTxHelper, rskAddress, PEGOUT_AMOUNT_PLUS_FEE);
await rskTxHelper.unlockAccount(rskAddress);
for (let i = 0; i < requestSize; i++) {
await sendTxToBridge(rskTxHelper, amountInRBTC, rskAddress, false);
await sendTxToBridge(rskTxHelper, new BN(ethToWeis(amountInRBTC)), rskAddress, false);
}
await mineAndSync(getRskTransactionHelpers());
};
Expand Down Expand Up @@ -337,6 +338,20 @@ const createExpectedUnrefundablePeginEvent = (btcPeginTxHash, rejectionReason) =
return expectedEvent;
};

/**
* Funds an
* @param {RskTransactionHelper} rskTxHelper
* @param {BtcTransactionHelper} btcTxHelper
* @param {object} btcSenderAddressInformation the btc object information that contains the btc address and private key to spend funds from
* @param {number} amountInBtcToFundInBtc defaults to DEFAULT_RSK_ADDRESS_FUNDING_IN_BTC
* @returns {Promise<string>} the pegin tx hash
*/
const fundRskAccountThroughAPegin = async (rskTxHelper, btcTxHelper, btcSenderAddressInfo, amountInBtcToFundInBtc = DEFAULT_RSK_ADDRESS_FUNDING_IN_BTC) => {
const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, btcSenderAddressInfo, amountInBtcToFundInBtc);
await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash);
return btcPeginTxHash;
};

module.exports = {
sendTxToBridge,
assertRefundUtxosSameAsPeginUtxos,
Expand All @@ -356,4 +371,5 @@ module.exports = {
get2wpBalances,
createExpectedRejectedPeginEvent,
createExpectedUnrefundablePeginEvent,
fundRskAccountThroughAPegin,
};
5 changes: 5 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ const PEGIN_EVENTS = {

const PEGIN_V1_RSKT_PREFIX_HEX = '52534b54';

const MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS = 250_000;
const DEFAULT_RSK_ADDRESS_FUNDING_IN_BTC = 0.5;

module.exports = {
KEY_TYPE_BTC,
KEY_TYPE_RSK,
Expand All @@ -131,4 +134,6 @@ module.exports = {
PEGIN_EVENTS,
PEGIN_UNREFUNDABLE_REASONS,
PEGIN_V1_RSKT_PREFIX_HEX,
MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS,
DEFAULT_RSK_ADDRESS_FUNDING_IN_BTC,
};
3 changes: 2 additions & 1 deletion lib/tests/2wp-legacy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const expect = require('chai').expect
const BN = require('bn.js');
const { ensure0x} = require('../utils');
const whitelistingAssertions = require('../assertions/whitelisting');
const rskUtils = require('../rsk-utils');
Expand Down Expand Up @@ -71,7 +72,7 @@ const execute = (description, getRskHost) => {
const unlocked = await rskTxHelper.unlockAccount(recipientRskAddressInfo.address);
expect(unlocked, 'Account was not unlocked').to.be.true;

const pegoutTransaction = await sendTxToBridge(rskTxHelper, PEGOUT_UNDER_MINIMUM_VALUE_IN_BTC, recipientRskAddressInfo.address);
const pegoutTransaction = await sendTxToBridge(rskTxHelper, new BN(ethToWeis(PEGOUT_UNDER_MINIMUM_VALUE_IN_BTC)), recipientRskAddressInfo.address);

const pegoutRejectedEvent = await rskUtils.findEventInBlock(rskTxHelper, PEGOUT_EVENTS.RELEASE_REQUEST_REJECTED.name);
expect(pegoutRejectedEvent).to.not.be.null;
Expand Down
33 changes: 19 additions & 14 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ const { createPeginV1TxData } = require('pegin-address-verificator');
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, btcToWeis } = require('@rsksmart/btc-eth-unit-converter');
const { satoshisToBtc, btcToSatoshis, satoshisToWeis } = require('@rsksmart/btc-eth-unit-converter');
const { findEventInBlock, triggerRelease, getPegoutEventsInBlockRange } = require('../rsk-utils');
const { PEGIN_REJECTION_REASONS, PEGIN_UNREFUNDABLE_REASONS, PEGOUT_EVENTS, PEGIN_V1_RSKT_PREFIX_HEX } = require("../constants");
const {
PEGIN_REJECTION_REASONS,
PEGIN_UNREFUNDABLE_REASONS,
PEGOUT_EVENTS,
PEGIN_V1_RSKT_PREFIX_HEX,
MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS,
} = require("../constants");
const { sendPegin,
ensurePeginIsRegistered,
createSenderRecipientInfo,
Expand All @@ -16,6 +22,7 @@ const { sendPegin,
createExpectedUnrefundablePeginEvent,
assertRefundUtxosSameAsPeginUtxos,
sendTxToBridge,
fundRskAccountThroughAPegin,
} = require('../2wp-utils');
const { getBtcAddressBalanceInSatoshis } = require('../btc-utils');
const { ensure0x, removePrefix0x } = require('../utils');
Expand Down Expand Up @@ -688,17 +695,15 @@ const execute = (description, getRskHost) => {

// Create a pegin for the serder to ensure there is enough funds to pegout and because this is the natural process
const senderRecipientInfo = await createSenderRecipientInfo(rskTxHelper, btcTxHelper);
const peginValueInSatoshis = btcToSatoshis(0.5);
const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, senderRecipientInfo.btcSenderAddressInfo, satoshisToBtc(peginValueInSatoshis));
await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash);
await fundRskAccountThroughAPegin(rskTxHelper, btcTxHelper, senderRecipientInfo.btcSenderAddressInfo);

const initial2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper);
const initialSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address);
const pegoutValueInRbtc = 0.0025;
const pegoutValueInSatoshis = MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS;

// Act

const pegoutTransaction = await sendTxToBridge(rskTxHelper, pegoutValueInRbtc, senderRecipientInfo.rskRecipientRskAddressInfo.address);
const pegoutTransaction = await sendTxToBridge(rskTxHelper, new BN(satoshisToWeis(pegoutValueInSatoshis)), senderRecipientInfo.rskRecipientRskAddressInfo.address);

// Assert

Expand All @@ -718,9 +723,9 @@ const execute = (description, getRskHost) => {
// Checking all the pegout events are emitted and in order
const blockNumberAfterPegoutRelease = await rskTxHelper.getBlockNumber();
const pegoutsEvents = await getPegoutEventsInBlockRange(rskTxHelper, pegoutTransaction.blockNumber, blockNumberAfterPegoutRelease);
await assertSuccessfulPegoutEventsAreEmitted(pegoutsEvents, pegoutTransaction.transactionHash, senderRecipientInfo, pegoutValueInRbtc, bridgeStateAfterPegoutCreation);
await assertSuccessfulPegoutEventsAreEmitted(pegoutsEvents, pegoutTransaction.transactionHash, senderRecipientInfo, pegoutValueInSatoshis, bridgeStateAfterPegoutCreation);

await assert2wpBalanceAfterSuccessfulPegout(initial2wpBalances, pegoutValueInRbtc);
await assert2wpBalanceAfterSuccessfulPegout(initial2wpBalances, pegoutValueInSatoshis);

// Assert that the sender address balance is increased by the actual pegout value
const finalSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address);
Expand Down Expand Up @@ -824,7 +829,7 @@ const assertBtcPeginTxHashProcessed = async (btcPeginTxHash) => {
expect(isBtcTxHashAlreadyProcessed).to.be.true;
};

const assertSuccessfulPegoutEventsAreEmitted = async (pegoutsEvents, pegoutRequestReceivedTransactionHash, senderRecipientInfo, pegoutValueInRbtc, bridgeStateAfterPegoutCreation) => {
const assertSuccessfulPegoutEventsAreEmitted = async (pegoutsEvents, pegoutRequestReceivedTransactionHash, senderRecipientInfo, pegoutValueInSatoshis, bridgeStateAfterPegoutCreation) => {

const pegoutWaitingForConfirmationWhenPegoutWasCreated = bridgeStateAfterPegoutCreation.pegoutsWaitingForConfirmations[0];
const btcTransaction = bitcoinJsLib.Transaction.fromHex(pegoutWaitingForConfirmationWhenPegoutWasCreated.btcRawTx);
Expand All @@ -833,15 +838,15 @@ const assertSuccessfulPegoutEventsAreEmitted = async (pegoutsEvents, pegoutReque
// release_request_received event
const releaseRequestReceivedEvent = pegoutsEvents[0];
expect(releaseRequestReceivedEvent.arguments.sender).to.be.equal(rskSenderAddress);
expect(releaseRequestReceivedEvent.arguments.amount).to.be.equal(btcToSatoshis(pegoutValueInRbtc));
expect(releaseRequestReceivedEvent.arguments.amount).to.be.equal(pegoutValueInSatoshis);
expect(releaseRequestReceivedEvent.arguments.btcDestinationAddress).to.be.equal(senderRecipientInfo.btcSenderAddressInfo.address);

// release_requested event
const pegoutCreationRskTransactionHash = ensure0x(pegoutWaitingForConfirmationWhenPegoutWasCreated.rskTxHash.padStart(64, '0'));
const releaseRequestedEvent = pegoutsEvents[1];
expect(releaseRequestedEvent.arguments.rskTxHash).to.be.equal(pegoutCreationRskTransactionHash);
expect(removePrefix0x(releaseRequestedEvent.arguments.btcTxHash)).to.be.equal(btcTransaction.getId());
expect(releaseRequestReceivedEvent.arguments.amount).to.be.equal(btcToSatoshis(pegoutValueInRbtc));
expect(releaseRequestReceivedEvent.arguments.amount).to.be.equal(pegoutValueInSatoshis);

// batch_pegout_created event
const batchPegoutCreatedEvent = pegoutsEvents[2];
Expand Down Expand Up @@ -874,13 +879,13 @@ const assertSuccessfulPegoutEventsAreEmitted = async (pegoutsEvents, pegoutReque
* @param {{federationAddressBalanceInSatoshis: number, bridgeUtxosBalanceInSatoshis: number, bridgeBalanceInWeisBN: BN}} initial2wpBalances
* @param {number} pegoutValueInRbtc the value in RBTC of the pegout
*/
const assert2wpBalanceAfterSuccessfulPegout = async (initial2wpBalances, pegoutValueInRbtc) => {
const assert2wpBalanceAfterSuccessfulPegout = async (initial2wpBalances, pegoutValueInSatoshis) => {

const final2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper);

expect(final2wpBalances.federationAddressBalanceInSatoshis).to.be.equal(initial2wpBalances.federationAddressBalanceInSatoshis - btcToSatoshis(pegoutValueInRbtc));

const expectedFinalBridgeBalancesInWeisBN = initial2wpBalances.bridgeBalanceInWeisBN.add(new BN(btcToWeis(pegoutValueInRbtc)));
const expectedFinalBridgeBalancesInWeisBN = initial2wpBalances.bridgeBalanceInWeisBN.add(new BN(satoshisToWeis(pegoutValueInSatoshis)));
expect(final2wpBalances.bridgeBalanceInWeisBN.eq(expectedFinalBridgeBalancesInWeisBN)).to.be.true;

expect(final2wpBalances.bridgeUtxosBalanceInSatoshis).to.be.equal(initial2wpBalances.bridgeUtxosBalanceInSatoshis - btcToSatoshis(pegoutValueInRbtc));
Expand Down

0 comments on commit 55cadae

Please sign in to comment.