Skip to content

Commit

Permalink
Merge pull request #14 from rsksmart/update-btc-eth-unit-converter
Browse files Browse the repository at this point in the history
Updated btc-eth-unit-converter dependency
  • Loading branch information
marcos-iov authored Oct 20, 2023
2 parents d939477 + 300cdc9 commit 8315e5b
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 98 deletions.
9 changes: 5 additions & 4 deletions lib/2wp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const { sendFromCow, mineAndSync, sendTxWithCheck } = require('./rsk-utils');
const { wait, retryWithCheck } = require('./utils');
const { getBridge, getLatestActiveForkName } = require('./precompiled-abi-forks-util');
const { getBridgeState } = require('@rsksmart/bridge-state-data-parser');
const btcEthUnitConverter = require('btc-eth-unit-converter');
const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter');

const peginVerifier = require('pegin-address-verificator');
const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');
const { WHITELIST_CHANGE_PK, WHITELIST_CHANGE_ADDR} = require('../lib/assertions/whitelisting')
Expand Down Expand Up @@ -55,7 +56,7 @@ const sendTxToBridge = async (rskTxHelper, amountInRbtc, rskFromAddress, mine =
const txPromise = rskTxHelper.sendTransaction({
from: rskFromAddress,
to: BRIDGE_ADDRESS,
value: btcEthUnitConverter.btcToWeis(amountInRbtc),
value: Number(btcEthUnitConverter.btcToWeis(amountInRbtc)),
gasPrice: TO_BRIDGE_GAS_PRICE,
});
if(!mine) {
Expand All @@ -76,8 +77,8 @@ const sendTxToBridge = async (rskTxHelper, amountInRbtc, rskFromAddress, mine =
* @returns {Promise<string>} the rsk tx hash that mined the pegout request
*/
const createPegoutRequest = async (rskTxHelper, amountInRBTC, requestSize = 1) => {
const AMOUNT_IN_WEIS = btcEthUnitConverter.btcToWeis(amountInRBTC);
const RSK_TX_FEE_IN_WEIS = btcEthUnitConverter.btcToWeis(1);
const AMOUNT_IN_WEIS = Number(btcEthUnitConverter.btcToWeis(amountInRBTC));
const RSK_TX_FEE_IN_WEIS = Number(btcEthUnitConverter.btcToWeis(1));
const PEGOUT_AMOUNT_PLUS_FEE = (AMOUNT_IN_WEIS + RSK_TX_FEE_IN_WEIS) * requestSize;
const rskAddress = await rskTxHelper.newAccountWithSeed('test');
await sendFromCow(rskTxHelper, rskAddress, PEGOUT_AMOUNT_PLUS_FEE);
Expand Down
4 changes: 2 additions & 2 deletions lib/liquidity-bridge-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { getRskTransactionHelper } = require('../lib/rsk-tx-helper-provider');

const solUtils = require('../lib/sol-utils');
const rskUtils = require('../lib/rsk-utils');
const btcEthUnitConverter = require('btc-eth-unit-converter');
const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter');

const INITIAL_RSK_BALANCE_IN_BTC = 1;
const BRIDGE_ADDRESS = '0x0000000000000000000000000000000001000006';
Expand All @@ -16,7 +16,7 @@ let contractInstance;
const deployLiquidityBridgeContract = async (host = null) => {
const rskTransactionHelper = getRskTransactionHelper(host);
const fromAddress = await rskTransactionHelper.newAccountWithSeed('');
await rskUtils.sendFromCow(rskTransactionHelper, fromAddress, btcEthUnitConverter.btcToWeis(INITIAL_RSK_BALANCE_IN_BTC));
await rskUtils.sendFromCow(rskTransactionHelper, fromAddress, Number(btcEthUnitConverter.btcToWeis(INITIAL_RSK_BALANCE_IN_BTC)));
await rskTransactionHelper.unlockAccount(fromAddress, '');

try {
Expand Down
36 changes: 18 additions & 18 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { getBridge, getLatestActiveForkName } = require('../precompiled-abi-forks
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 btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter');
const { sendTxToBridge, sendPegin, ensurePeginIsRegistered, donateToBridge } = require('../2wp-utils');

const DONATION_AMOUNT = 250;
Expand All @@ -31,7 +31,7 @@ const execute = (description, getRskHost) => {
federationAddress = await bridge.methods.getFederationAddress().call();

const minimumPeginValueInSatoshis = await bridge.methods.getMinimumLockTxValue().call();
minimumPeginValueInBtc = btcEthUnitConverter.satoshisToBtc(minimumPeginValueInSatoshis);
minimumPeginValueInBtc = Number(btcEthUnitConverter.satoshisToBtc(minimumPeginValueInSatoshis));

await btcTxHelper.importAddress(federationAddress, 'federations');

Expand All @@ -45,7 +45,7 @@ const execute = (description, getRskHost) => {
// Those tests are not doing a pegin if needed, so we need to donate to the bridge to ensure it has enough balance.
// This will be removed after all pegout tests are updated to do their own pegin if needed.
const donatingBtcAddressInformation = await btcTxHelper.generateBtcAddress('legacy');
await whitelistingAssertions.assertAddLimitedLockWhitelistAddress(rskTxHelper, donatingBtcAddressInformation.address, btcEthUnitConverter.btcToSatoshis(DONATION_AMOUNT));
await whitelistingAssertions.assertAddLimitedLockWhitelistAddress(rskTxHelper, donatingBtcAddressInformation.address, Number(btcEthUnitConverter.btcToSatoshis(DONATION_AMOUNT)));
await rskUtils.mineAndSync(rskTxHelpers);
await btcTxHelper.fundAddress(donatingBtcAddressInformation.address, DONATION_AMOUNT + btcTxHelper.getFee());

Expand All @@ -58,7 +58,7 @@ const execute = (description, getRskHost) => {
try {
const peginSenderAddressInfo = await btcTxHelper.generateBtcAddress('legacy');

await whitelistingAssertions.assertAddLimitedLockWhitelistAddress(rskTxHelper, peginSenderAddressInfo.address, btcEthUnitConverter.btcToSatoshis(minimumPeginValueInBtc));
await whitelistingAssertions.assertAddLimitedLockWhitelistAddress(rskTxHelper, peginSenderAddressInfo.address, Number(btcEthUnitConverter.btcToSatoshis(minimumPeginValueInBtc)));
await rskUtils.mineAndSync(rskTxHelpers);

const recipientRskAddressInfo = getDerivedRSKAddressInformation(peginSenderAddressInfo.privateKey, btcTxHelper.btcConfig.network);
Expand All @@ -72,7 +72,7 @@ const execute = (description, getRskHost) => {
const finalRskAddressBalanceInWeis = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address));

// Asserting that the received pegin amount in rsk is as expected
const peginValueInWeis = btcEthUnitConverter.btcToWeis(minimumPeginValueInBtc);
const peginValueInWeis = Number(btcEthUnitConverter.btcToWeis(minimumPeginValueInBtc));
expect(finalRskAddressBalanceInWeis).to.equal(initialRskAddressBalanceInWeis + peginValueInWeis);
} catch (err) {
throw new CustomError('Transfer BTC to RBTC', err);
Expand All @@ -87,7 +87,7 @@ const execute = (description, getRskHost) => {

const peginSenderAddressInfo = await btcTxHelper.generateBtcAddress('legacy');

await whitelistingAssertions.assertAddLimitedLockWhitelistAddress(rskTxHelper, peginSenderAddressInfo.address, btcEthUnitConverter.btcToSatoshis(EXPECTED_RSK_BALANCE_IN_RBTC));
await whitelistingAssertions.assertAddLimitedLockWhitelistAddress(rskTxHelper, peginSenderAddressInfo.address, Number(btcEthUnitConverter.btcToSatoshis(EXPECTED_RSK_BALANCE_IN_RBTC)));
await rskUtils.mineAndSync(rskTxHelpers);
await btcTxHelper.fundAddress(peginSenderAddressInfo.address, INITIAL_BTC_BALANCE + btcTxHelper.getFee());

Expand All @@ -102,7 +102,7 @@ const execute = (description, getRskHost) => {
const finalRskAddressBalanceInWeis = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address));

// Asserting that the received pegin amount in rsk is as expected
const peginValueInWeis = btcEthUnitConverter.btcToWeis(EXPECTED_RSK_BALANCE_IN_RBTC);
const peginValueInWeis = Number(btcEthUnitConverter.btcToWeis(EXPECTED_RSK_BALANCE_IN_RBTC));
expect(finalRskAddressBalanceInWeis).to.equal(initialRskAddressBalanceInWeis + peginValueInWeis);

}
Expand All @@ -116,10 +116,10 @@ const execute = (description, getRskHost) => {
const INITIAL_RSK_BALANCE = 1;
const PEGOUT_VALUE_IN_RBTC = 0.5;
const MAX_EXPECTED_FEE = 0.001;
const pegoutValueInSatoshis = btcEthUnitConverter.btcToSatoshis(PEGOUT_VALUE_IN_RBTC);
const maxExpectedFeeInSatoshis = btcEthUnitConverter.btcToSatoshis(MAX_EXPECTED_FEE);
const pegoutValueInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(PEGOUT_VALUE_IN_RBTC));
const maxExpectedFeeInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(MAX_EXPECTED_FEE));

const initialFederationBalanceInSatoshis = btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(federationAddress));
const initialFederationBalanceInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(federationAddress)));

const btcAddressInformation = await btcTxHelper.generateBtcAddress('legacy');

Expand All @@ -129,7 +129,7 @@ const execute = (description, getRskHost) => {
const unlocked = await rskTxHelper.unlockAccount(recipientRskAddressInfo.address);
expect(unlocked, 'Account was not unlocked').to.be.true;

await rskUtils.sendFromCow(rskTxHelper, recipientRskAddressInfo.address, btcEthUnitConverter.btcToWeis(INITIAL_RSK_BALANCE));
await rskUtils.sendFromCow(rskTxHelper, recipientRskAddressInfo.address, Number(btcEthUnitConverter.btcToWeis(INITIAL_RSK_BALANCE)));

const pegoutTransaction = await sendTxToBridge(rskTxHelper, PEGOUT_VALUE_IN_RBTC, recipientRskAddressInfo.address);

Expand Down Expand Up @@ -178,8 +178,8 @@ const execute = (description, getRskHost) => {
};

await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper, callbacks);
const finalFederationBalanceInSatoshis = btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(federationAddress));
const finalDestinationAddressBalanceInSatoshis = btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddressInformation.address));
const finalFederationBalanceInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(federationAddress)));
const finalDestinationAddressBalanceInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddressInformation.address)));
const difference = pegoutValueInSatoshis - finalDestinationAddressBalanceInSatoshis;
expect(difference).to.be.at.most(maxExpectedFeeInSatoshis);
expect(finalFederationBalanceInSatoshis).to.equal(initialFederationBalanceInSatoshis - pegoutValueInSatoshis);
Expand All @@ -191,13 +191,13 @@ const execute = (description, getRskHost) => {
const INITIAL_RSK_BALANCE = 2;
const PEGOUT_UNDER_MINIMUM_VALUE_IN_BTC = 0.002;

const initialFederationBalanceInSatoshis = btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(federationAddress));
const initialFederationBalanceInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(federationAddress)));

const btcAddressInformation = await btcTxHelper.generateBtcAddress('legacy');
const recipientRskAddressInfo = getDerivedRSKAddressInformation(btcAddressInformation.privateKey, btcTxHelper.btcConfig.network);

await rskTxHelper.importAccount(recipientRskAddressInfo.privateKey);
await rskUtils.sendFromCow(rskTxHelper, recipientRskAddressInfo.address, btcEthUnitConverter.btcToWeis(INITIAL_RSK_BALANCE));
await rskUtils.sendFromCow(rskTxHelper, recipientRskAddressInfo.address, Number(btcEthUnitConverter.btcToWeis(INITIAL_RSK_BALANCE)));
const initialRskAddressBalance = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address));
const unlocked = await rskTxHelper.unlockAccount(recipientRskAddressInfo.address);
expect(unlocked, 'Account was not unlocked').to.be.true;
Expand All @@ -207,7 +207,7 @@ const execute = (description, getRskHost) => {
if (isIris300AlreadyActive) {
const pegoutRejectedEvent = await rskUtils.findEventInBlock(rskTxHelper, 'release_request_rejected');
expect(pegoutRejectedEvent).to.not.be.null;
const pegoutValueInSatoshis = btcEthUnitConverter.btcToSatoshis(PEGOUT_UNDER_MINIMUM_VALUE_IN_BTC);
const pegoutValueInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(PEGOUT_UNDER_MINIMUM_VALUE_IN_BTC));
expect(Number(pegoutRejectedEvent.arguments.amount)).to.equal(pegoutValueInSatoshis);
expect(pegoutRejectedEvent.arguments.sender.toLowerCase()).to.equal(ensure0x(recipientRskAddressInfo.address));
expect(Number(pegoutRejectedEvent.arguments.reason)).to.equal(REJECTED_REASON);
Expand All @@ -216,9 +216,9 @@ const execute = (description, getRskHost) => {
} else {
const finalRskAddressBalance = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address));
// The transaction should have been rejected and the RBTC should be lost
expect(finalRskAddressBalance).to.be.at.most(initialRskAddressBalance - btcEthUnitConverter.btcToWeis(PEGOUT_UNDER_MINIMUM_VALUE_IN_BTC));
expect(finalRskAddressBalance).to.be.at.most(initialRskAddressBalance - Number(btcEthUnitConverter.btcToWeis(PEGOUT_UNDER_MINIMUM_VALUE_IN_BTC)));
}
const finalFederationBalanceInSatoshis = btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(federationAddress));
const finalFederationBalanceInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(federationAddress)));
expect(finalFederationBalanceInSatoshis).to.equal(initialFederationBalanceInSatoshis);
}
catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions lib/tests/bridge-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const CustomError = require('../CustomError');
const { wait } = require('../utils');
const rskUtils = require('../rsk-utils');
const solUtils = require('../sol-utils');
const btcEthUnitConverter = require('btc-eth-unit-converter');
const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter');
const { getRskTransactionHelpers, getRskTransactionHelper } = require('../rsk-tx-helper-provider');

const { getBridge } = require('../precompiled-abi-forks-util');
Expand All @@ -28,7 +28,7 @@ const execute = (description, getRskHost, bridgeCallsAllowed) => {
rskTransactionHelper = getRskTransactionHelper(getRskHost());
rskClient = rskTransactionHelper.getClient();
address = await rskClient.eth.personal.newAccount('');
await rskUtils.sendFromCow(rskTransactionHelper, address, btcEthUnitConverter.btcToWeis(INITIAL_RSK_BALANCE_IN_BTC));
await rskUtils.sendFromCow(rskTransactionHelper, address, Number(btcEthUnitConverter.btcToWeis(INITIAL_RSK_BALANCE_IN_BTC)));
await rskClient.eth.personal.unlockAccount(address, '');
});

Expand Down
20 changes: 14 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
"license": "GPL3",
"dependencies": {
"@rsksmart/bridge-state-data-parser": "^1.1.0",
"@rsksmart/btc-eth-unit-converter": "^1.0.0",
"@rsksmart/btc-rsk-derivation": "^0.0.2",
"@rsksmart/pmt-builder": "^3.0.0",
"@rsksmart/powpeg-redeemscript-parser": "^1.0.0",
"bn.js": "^4.11.8",
"bridge-transaction-parser-hop400": "github:rsksmart/bridge-transaction-parser#v0.4.0-beta",
"bridge-transaction-parser-fingerroot500": "github:rsksmart/bridge-transaction-parser#v0.5.0-beta",
"btc-eth-unit-converter": "github:rsksmart/btc-eth-unit-converter#v0.3.0-beta",
"bridge-transaction-parser-hop400": "github:rsksmart/bridge-transaction-parser#v0.4.0-beta",
"btc-transaction-helper": "git+https://[email protected]/rsksmart/btc-transaction-helper#v3.4.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
Expand Down
Loading

0 comments on commit 8315e5b

Please sign in to comment.