Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase locking cap to 21 million as part of the setup start #77

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions lib/rsk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,20 +333,22 @@ const triggerRelease = async (rskTransactionHelpers, btcClient, callbacks = {})
* Executes a method 'call' and calls the callback with the result of the call, then calls 'send' and waits for the transaction receipt to be available and returns it
* @param {RskTransactionHelper} rskTxHelper to make transactions to the rsk network
* @param {web3.eth.Contract.ContractSendMethod} method contract method to be invoked
* @param {function} checkCallback callback to check the result of the method 'call' before calling 'send'
* @param {string} from rsk address to send the transaction from
* @param {function} checkCallback callback to check the result of the method 'call' before calling 'send'
* @param {object} options optional. Object with the options for the transaction. It can contain the following properties: value, gasPrice, gas
* @returns {web3.eth.TransactionReceipt} txReceipt
*/
const sendTxWithCheck = async (rskTxHelper, method, from, checkCallback) => {

const callResult = await method.call({ from });
const sendTxWithCheck = async (rskTxHelper, method, from, checkCallback, options = { value: 0, gasPrice: 0, gas: 0 }) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe receive options param first and checkCallback last? Mainly to avoid having to pass null when calling from sendTransaction

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.


if(checkCallback) {
const callResult = await method.call({ from });
await checkCallback(callResult);
}

const estimatedGas = await method.estimateGas({ from });
const txReceiptPromise = method.send({ from, value: 0, gasPrice: 0, gas: estimatedGas });
const { value, gasPrice, gas } = options;

const estimatedGas = gas ? gas : await method.estimateGas({ from });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check Sonar report here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

const txReceiptPromise = method.send({ from, value, gasPrice, gas: estimatedGas });

await waitForRskMempoolToGetNewTxs(rskTxHelper);
await mineAndSync(getRskTransactionHelpers());
Expand All @@ -355,6 +357,18 @@ const sendTxWithCheck = async (rskTxHelper, method, from, checkCallback) => {

};

/**
* Calls the `method` as a `send` transaction and wait for the transaction receipt to be available.
* @param {RskTransactionHelper} rskTxHelper to make transactions to the rsk network
* @param {web3.eth.Contract.ContractSendMethod} method contract method to be invoked
* @param {string} from rsk address to send the transaction from
* @param {object} options optional. Object with the options for the transaction. It can contain the following properties: value, gasPrice, gas
* @returns {web3.eth.TransactionReceipt} txReceipt
*/
const sendTransaction = async (rskTxHelper, method, from, options = { value: 0, gasPrice: 0, gas: 0 }) => {
return await sendTxWithCheck(rskTxHelper, method, from, null, options);
};

/**
*
* @param {RskTransactionHelper} rskTxHelper
Expand Down Expand Up @@ -486,4 +500,5 @@ module.exports = {
waitForRskTxToBeInTheMempool,
waitForRskMempoolToGetNewTxs,
waitAndUpdateBridge,
sendTransaction,
};
57 changes: 57 additions & 0 deletions tests/00_00_3-vote_for_locking_cap_to_21m.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const rskUtils = require('../lib/rsk-utils');
const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');
const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util');
const { btcToWeis, btcToSatoshis } = require('@rsksmart/btc-eth-unit-converter');
const { expect } = require('chai');

const lockingCapAuthorizerPrivateKey = 'da6a5451bfd74829307ec6d4a8c55174d4859169f162a8ed8fcba8f7636e77cc';

describe('Vote for locking cap to the max 21 million btc', function() {

let rskTxHelpers;

before(async () => {
rskTxHelpers = getRskTransactionHelpers();
});

it('should increase locking cap to the max 21 million btc', async () => {

const rskTxHelper = rskTxHelpers[0];

const authAddress = await rskTxHelper.getClient().eth.personal.importRawKey(lockingCapAuthorizerPrivateKey, '');
await rskUtils.sendFromCow(rskTxHelper, authAddress, btcToWeis(1));

const bridge = getBridge(rskTxHelper.getClient(), await getLatestActiveForkName());

const MAX_BTC = 21_000_000;

const targetLockingCapInSatoshis = Number(btcToSatoshis(MAX_BTC));

let currentLockingCapValueInSatoshis = Number(await bridge.methods.getLockingCap().call());

let nextIncrement = 0;

while(nextIncrement < targetLockingCapInSatoshis) {

nextIncrement = currentLockingCapValueInSatoshis * 2;

// Ensuring that the next increment is not greater than the target locking cap.
nextIncrement = Math.min(nextIncrement, targetLockingCapInSatoshis);

const increaseLockingCapMethod = bridge.methods.increaseLockingCap(nextIncrement);

await rskUtils.sendTransaction(rskTxHelper, increaseLockingCapMethod, authAddress);

currentLockingCapValueInSatoshis = Number(await bridge.methods.getLockingCap().call());

// Ensuring that the locking cap is being increased on every iteration.
expect(currentLockingCapValueInSatoshis).to.be.equal(nextIncrement, 'The new locking cap value should be equal to the set value');

}

const finalLockingCapValueInSatoshis = Number(await bridge.methods.getLockingCap().call());

expect(finalLockingCapValueInSatoshis).to.be.equal(targetLockingCapInSatoshis);

});
});