Skip to content

Commit

Permalink
Refactors to replace var with let and const
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-then authored and marcos-iov committed Dec 11, 2023
1 parent ba1c983 commit 925bb4d
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions tests/01_06_52-post_fingerroot500-2wp_with_locking_cap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const expect = require('chai').expect
var { sequentialPromise, wait } = require('../lib/utils');
const { sequentialPromise, wait } = require('../lib/utils');
const CustomError = require('../lib/CustomError');
const peglib = require('peglib');

Expand All @@ -12,15 +12,16 @@ const rskUtilsLegacy = require('../lib/rsk-utils-legacy');
const rskUtils = require('../lib/rsk-utils');
const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');

var federationAddress;
var btcClient;
var rskClient;
var rskClients;
var pegClient;
var test;
var expectedLockingCap;
let federationAddress;
let btcClient;
let rskClient;
let rskClients;
let pegClient;
let test;
let expectedLockingCap;
let whitelistTestLegacy;
let rskTxHelpers;
let utils;

const NETWORK = bitcoin.networks.testnet;
const BTC_TX_FEE = bitcoin.btcToSatoshis(0.001);
Expand All @@ -47,23 +48,23 @@ const BTC_TX_FEE = bitcoin.btcToSatoshis(0.001);
federationAddress = await rskClient.rsk.bridge.methods.getFederationAddress().call();
await btcClient.importAddress(federationAddress, 'federations');

var initialFedBalance = Number((await btcClient.getAddressBalance(federationAddress))[federationAddress]) || 0;
var currentLockingCap = Number(await rskClient.rsk.bridge.methods.getLockingCap().call());
const initialFedBalance = Number((await btcClient.getAddressBalance(federationAddress))[federationAddress]) || 0;
let currentLockingCap = Number(await rskClient.rsk.bridge.methods.getLockingCap().call());
// leave a 50% margin for these tests
expectedLockingCap = initialFedBalance * 1.5;
var authAddress = await rskClient.eth.personal.importRawKey("da6a5451bfd74829307ec6d4a8c55174d4859169f162a8ed8fcba8f7636e77cc", '');
const authAddress = await rskClient.eth.personal.importRawKey("da6a5451bfd74829307ec6d4a8c55174d4859169f162a8ed8fcba8f7636e77cc", '');
await utils.sendFromCow(authAddress, rskClient.utils.toWei('100'));
while (expectedLockingCap > currentLockingCap) {
// I have to increment the locking cap up to 200% each time
var nextIncrement = Number(currentLockingCap) * 2;
let nextIncrement = Number(currentLockingCap) * 2;
if (nextIncrement > expectedLockingCap) {
nextIncrement = expectedLockingCap;
}
var callResult = rskClient.rsk.bridge.methods.increaseLockingCap(nextIncrement).send({ from: authAddress });
const callResult = rskClient.rsk.bridge.methods.increaseLockingCap(nextIncrement).send({ from: authAddress });
await rskUtils.mineAndSync(rskTxHelpers);
await callResult;

var newValue = Number(await rskClient.rsk.bridge.methods.getLockingCap().call());
const newValue = Number(await rskClient.rsk.bridge.methods.getLockingCap().call());
expect(newValue).to.be.at.least(currentLockingCap);
currentLockingCap = newValue;
}
Expand All @@ -86,12 +87,12 @@ const BTC_TX_FEE = bitcoin.btcToSatoshis(0.001);

it('should return BTC using same UTXOs', async () => {
try {
var initialFedBalance = (await btcClient.getAddressBalance(federationAddress))[federationAddress] || 0;
const initialFedBalance = (await btcClient.getAddressBalance(federationAddress))[federationAddress] || 0;
expect(initialFedBalance).to.be.lessThan(expectedLockingCap);

const INITIAL_BTC_BALANCE = expectedLockingCap + bitcoin.btcToSatoshis(2);

var addresses = await pegClient.generateNewAddress('test');
const addresses = await pegClient.generateNewAddress('test');
expect(addresses.inRSK).to.be.true;

await whitelistTestLegacy.assertAddLimitedLockWhitelistAddress(addresses.btc, INITIAL_BTC_BALANCE)();
Expand All @@ -104,11 +105,11 @@ const BTC_TX_FEE = bitcoin.btcToSatoshis(0.001);
//TODO: This should be 0.001 when this test runs before feePerKb change
const MAX_EXPECTED_FEE = bitcoin.btcToSatoshis(0.001);

lockOutputs = {}
lockOutputs[federationAddress] = AMOUNT_TO_TRY_TO_LOCK
const lockOutputs = {};
lockOutputs[federationAddress] = AMOUNT_TO_TRY_TO_LOCK;

var lockBtcTxId = await btcClient.sendFromTo(addresses.btc, lockOutputs, BTC_TX_FEE, 1);
var lockBtcTx = await btcClient.getTransaction(lockBtcTxId);
const lockBtcTxId = await btcClient.sendFromTo(addresses.btc, lockOutputs, BTC_TX_FEE, 1);
const lockBtcTx = await btcClient.getTransaction(lockBtcTxId);

await btcClient.generate(3);

Expand All @@ -120,26 +121,26 @@ const BTC_TX_FEE = bitcoin.btcToSatoshis(0.001);
await rskClient.fed.updateBridge();
await rskUtils.mineAndSync(rskTxHelpers);

var currentRskBalance = await rskClient.eth.getBalance(addresses.rsk);
const currentRskBalance = await rskClient.eth.getBalance(addresses.rsk);

expect(Number(currentRskBalance), 'Wrong RSK balance').to.equal(0);

await rskUtilsLegacy.triggerRelease(rskClients, btcClient);

btcBalance = (await btcClient.getAddressBalance(addresses.btc))[addresses.btc] || 0;
var difference = INITIAL_BTC_BALANCE - btcBalance;
const btcBalance = (await btcClient.getAddressBalance(addresses.btc))[addresses.btc] || 0;
const difference = INITIAL_BTC_BALANCE - btcBalance;

// Consider the lock and paying the return fee as well, hence times 2
expect(difference).to.be.at.most(MAX_EXPECTED_FEE * 2);
await test.assertBitcoinBalance(federationAddress, initialFedBalance);

var nonLockUtxos = (await btcClient.getUnspentForAddress(addresses.btc))
const nonLockUtxos = (await btcClient.getUnspentForAddress(addresses.btc))
.filter(utxo => utxo.txid !== lockBtcTxId);
expect(nonLockUtxos.length).to.equal(1);

var returnUtxo = nonLockUtxos[0];
const returnUtxo = nonLockUtxos[0];
expect(AMOUNT_TO_TRY_TO_LOCK - returnUtxo.amount).to.be.at.most(MAX_EXPECTED_FEE);
var returnTx = await btcClient.getTransaction(returnUtxo.txid);
const returnTx = await btcClient.getTransaction(returnUtxo.txid);
expect(returnTx.vin.length).to.equal(lockBtcTx.vout.length - 1); // Don't consider the change output
returnTx.vin.forEach((txInput) => {
expect(txInput.txid).to.equal(lockBtcTxId);
Expand Down

0 comments on commit 925bb4d

Please sign in to comment.