Skip to content

Commit

Permalink
Adds doc comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-then committed Sep 17, 2024
1 parent 5ffe2b4 commit dbea99a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
21 changes: 19 additions & 2 deletions lib/2wp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ const disableWhitelisting = async (rskTxHelper, btcTxHelper, blockDelay = 1) =>
}
};

/**
* Creates a btc sender and rsk recipient information (private keys and addresses) and funds the btc sender address with the specified amount.
* @param {RskTransactionHelper} rskTxHelper to make transactions to the rsk network.
* @param {BtcTransactionHelper} btcTxHelper to make transactions to the bitcoin network.
* @param {string} type the btc address type to generate. Defaults to 'legacy'.
* @param {number} initialAmountToFundInBtc the initial amount to fund the btc sender address. Defaults to 1.
* @returns {Promise<{btcSenderAddressInfo: {address: string, privateKey: string}, rskRecipientRskAddressInfo: {address: string, privateKey: string}>}}
*/
const createSenderRecipientInfo = async (rskTxHelper, btcTxHelper, type = 'legacy', initialAmountToFundInBtc = 1) => {
const btcSenderAddressInfo = await btcTxHelper.generateBtcAddress(type);
const rskRecipientRskAddressInfo = getDerivedRSKAddressInformation(btcSenderAddressInfo.privateKey, btcTxHelper.btcConfig.network);
Expand All @@ -240,13 +248,22 @@ const createSenderRecipientInfo = async (rskTxHelper, btcTxHelper, type = 'legac
};
};

const createExpectedPeginBtcEvent = (partialExpectedEvent, rskRecipientRskAddress, btcPeginTxHash, peginValueInBtc, protocolVersion = '0') => {
/**
* Creates a pegin_btc event with the specified parameters.
* @param {Object} partialExpectedEvent an object with some pegin_btc event default values.
* @param {string} rskRecipientRskAddress the rsk address that receives the funds expected to be in the event.
* @param {string} btcPeginTxHash the pegin btc tx hash expected to be in the event.
* @param {number} peginValueInSatoshis the pegin value in satoshis expected to be in the event.
* @param {string} protocolVersion the pegin protocol version expected to be in the event. Defaults to '0'.
* @returns {BridgeEvent}
*/
const createExpectedPeginBtcEvent = (partialExpectedEvent, rskRecipientRskAddress, btcPeginTxHash, peginValueInSatoshis, protocolVersion = '0') => {
const expectedEvent = {
...partialExpectedEvent,
arguments: {
receiver: ensure0x(rskRecipientRskAddress),
btcTxHash: ensure0x(btcPeginTxHash),
amount: `${btcEthUnitConverter.btcToSatoshis(peginValueInBtc)}`,
amount: `${peginValueInSatoshis}`,
protocolVersion,
},
}
Expand Down
7 changes: 7 additions & 0 deletions lib/assertions/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ const assertRejectedPeginEvent = (rejectedPeginTx, expectedRejectionReason, expe
expect(outpointValues.every(value => value in federationUtxoValues)).to.be.true;
}

/**
* Searches for and checks that the pegin_btc event is found in the block.
* @param {RskTransactionHelper} rskTxHelper to make transactions to the rsk network.
* @param {number} atBlock the block number to look for the event.
* @param {BridgeEvent} expectedEvent the expected event to be found in the block.
* @returns {Promise<void>}
*/
const findAndCheckPeginBtcEventInBlock = async (rskTxHelper, atBlock, expectedEvent) => {
const peginBtcEvent = await rskUtils.findEventInBlock(rskTxHelper, expectedEvent.name, atBlock);
expect(peginBtcEvent).to.exist;
Expand Down
4 changes: 2 additions & 2 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const expect = require('chai').expect;
const { getBridge } = require('../precompiled-abi-forks-util');
const { getBtcClient } = require('../btc-client-provider');
const { getRskTransactionHelpers, getRskTransactionHelper } = require('../rsk-tx-helper-provider');
const { satoshisToBtc, btcToWeis } = require('@rsksmart/btc-eth-unit-converter');
const { satoshisToBtc, btcToWeis, btcToSatoshis } = require('@rsksmart/btc-eth-unit-converter');
const { waitAndUpdateBridge, mineAndSync } = require('../rsk-utils');
const { PEGIN_EVENTS } = require("../constants");
const { findAndCheckPeginBtcEventInBlock } = require('../assertions/2wp');
Expand Down Expand Up @@ -69,7 +69,7 @@ const execute = (description, getRskHost) => {
const isBtcTxHashAlreadyProcessed = await bridge.methods.isBtcTxHashAlreadyProcessed(btcPeginTxHash).call();
expect(isBtcTxHashAlreadyProcessed).to.be.true;

const expectedEvent = createExpectedPeginBtcEvent(PEGIN_EVENTS.PEGIN_BTC, senderInfo.rskRecipientRskAddressInfo.address, btcPeginTxHash, minimumPeginValueInBtc, '0');
const expectedEvent = createExpectedPeginBtcEvent(PEGIN_EVENTS.PEGIN_BTC, senderInfo.rskRecipientRskAddressInfo.address, btcPeginTxHash, btcToSatoshis(minimumPeginValueInBtc), '0');
const btcTxHashProcessedHeight = Number(await bridge.methods.getBtcTxHashProcessedHeight(btcPeginTxHash).call());
await findAndCheckPeginBtcEventInBlock(rskTxHelper, btcTxHashProcessedHeight, expectedEvent);

Expand Down

0 comments on commit dbea99a

Please sign in to comment.