Skip to content

Commit

Permalink
Merge pull request #230 from rsksmart/bugfix/GBI-1873
Browse files Browse the repository at this point in the history
Bugfix/GBI-1873 - Merge withdraw collateral functions
  • Loading branch information
Luisfc68 committed May 20, 2024
2 parents 45bae1e + 1c48b85 commit 7bdf9fd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
with:
node-version: '19.6.0'

- name: NPM Login
run: npm config set //npm.pkg.github.com/:_authToken ${{ secrets.GITHUB_TOKEN }}

- name: Install truffle
run: npm install -g truffle

Expand Down
26 changes: 7 additions & 19 deletions contracts/LiquidityBridgeContractV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ contract LiquidityBridgeContractV2 is Initializable, OwnableUpgradeable, Reentra
event PegoutCollateralIncrease(address from, uint256 amount);
event Withdrawal(address from, uint256 amount);
event WithdrawCollateral(address from, uint256 amount);
event PegoutWithdrawCollateral(address from, uint256 amount);
event Resigned(address from);
event CallForUser(
address indexed from,
Expand Down Expand Up @@ -313,36 +312,22 @@ contract LiquidityBridgeContractV2 is Initializable, OwnableUpgradeable, Reentra
/**
@dev Used to withdraw the locked collateral
*/
function withdrawCollateral() external {
function withdrawCollateral() external nonReentrant {
require(resignationBlockNum[msg.sender] > 0, "LBC021");
require(
block.number - resignationBlockNum[msg.sender] >=
resignDelayInBlocks,
"LBC022"
);
uint amount = collateral[msg.sender];
uint amount = collateral[msg.sender] + pegoutCollateral[msg.sender];
pegoutCollateral[msg.sender] = 0;
collateral[msg.sender] = 0;
resignationBlockNum[msg.sender] = 0;
(bool success,) = msg.sender.call{value: amount}("");
require(success, "LBC020");
emit WithdrawCollateral(msg.sender, amount);
}

function withdrawPegoutCollateral() external {
require(resignationBlockNum[msg.sender] > 0, "LBC021");
require(
block.number - resignationBlockNum[msg.sender] >=
resignDelayInBlocks,
"LBC022"
);
uint amount = pegoutCollateral[msg.sender];
pegoutCollateral[msg.sender] = 0;
resignationBlockNum[msg.sender] = 0;
(bool success,) = msg.sender.call{value: amount}("");
require(success, "LBC020");
emit PegoutWithdrawCollateral(msg.sender, amount);
}

/**
@dev Used to resign as a liquidity provider
*/
Expand Down Expand Up @@ -708,7 +693,10 @@ contract LiquidityBridgeContractV2 is Initializable, OwnableUpgradeable, Reentra
"LBC049"
);
require(quote.value <= outputs[PAY_TO_ADDRESS_OUTPUT].value * (10**10), "LBC067"); // satoshi to wei
bytes memory btcTxDestination = BtcUtils.parsePayToPubKeyHash(outputs[PAY_TO_ADDRESS_OUTPUT].pkScript, mainnet);
bytes memory btcTxDestination = BtcUtils.outputScriptToAddress(
outputs[PAY_TO_ADDRESS_OUTPUT].pkScript,
mainnet
);
require(keccak256(quote.deposityAddress) == keccak256(btcTxDestination), "LBC068");

if (
Expand Down
17 changes: 10 additions & 7 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"dependencies": {
"@openzeppelin/contracts": "^4.8.0",
"@openzeppelin/contracts-upgradeable": "^4.8.2",
"@rsksmart/btc-transaction-solidity-helper": "^0.0.3",
"@rsksmart/btc-transaction-solidity-helper": "^0.1.0",
"@truffle/hdwallet-provider": "^2.1.3",
"chai": "^4.3.4",
"chai-bn": "^0.3.0",
Expand Down
43 changes: 24 additions & 19 deletions test/basic.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,31 +800,35 @@ contract("LiquidityBridgeContractV2.sol", async (accounts) => {
lpAddress
);
let initialLBCBalance = await web3.eth.getBalance(lbcAddress);
let initialLPCol = await instance.getCollateral(
lpAddress
);

const initialPeginCollateral = await instance.getCollateral(lpAddress);
const initialPegoutCollateral = await instance.getPegoutCollateral(lpAddress);
const totalInitialCollateral = web3.utils.toBN(initialPeginCollateral.add(initialPegoutCollateral));
let resignTx = await instance.resign({ from: lpAddress });
let withdrawTx = await instance.withdraw(initialLPBalance, { from: lpAddress });

let finalLPBalance = await instance.getBalance(lpAddress);
let currentLBCBalance = await web3.eth.getBalance(lbcAddress);
const lpBalanceAfterResign = await instance.getBalance(lpAddress);
const lbcBalanceAfterResign = await web3.eth.getBalance(lbcAddress);

let lbcCurrBal = web3.utils
.toBN(initialLBCBalance)
.sub(web3.utils.toBN(currentLBCBalance));
.sub(web3.utils.toBN(lbcBalanceAfterResign));
expect(initialLPBalance).to.be.a.bignumber.eq(lbcCurrBal);
expect(finalLPBalance).to.be.a.bignumber.eq(web3.utils.toBN(0));
expect(lpBalanceAfterResign).to.be.a.bignumber.eq(web3.utils.toBN(0));

await utils.mineBlocks(utils.RESIGN_DELAY_BLOCKS);

let withdrawCollateralTx = await instance.withdrawCollateral({ from: lpAddress });

let finalLPCol = await instance.getCollateral(lpAddress);
let finalLBCBalance = await web3.eth.getBalance(lbcAddress);
let lbcBal = web3.utils
.toBN(currentLBCBalance)
.sub(web3.utils.toBN(finalLBCBalance));
const initialLpRbtc = await web3.eth.getBalance(lpAddress).then(res => web3.utils.toBN(res));
const withdrawCollateralTx = await instance.withdrawCollateral({ from: lpAddress });
const finalLpRbtc = await web3.eth.getBalance(lpAddress).then(res => web3.utils.toBN(res));
const withdrawCollateralGas = web3.utils.toBN(withdrawCollateralTx.receipt.cumulativeGasUsed).mul(web3.utils.toBN(withdrawCollateralTx.receipt.effectiveGasPrice))

const finalLpPeginCollateral = await instance.getCollateral(lpAddress);
const finalLpPegoutCollateral = await instance.getPegoutCollateral(lpAddress);
const totalFinalCollateral = finalLpPeginCollateral.add(finalLpPegoutCollateral);
const finalLbcBalance = await web3.eth.getBalance(lbcAddress);
const lbcCollateral = web3.utils
.toBN(lbcBalanceAfterResign)
.sub(web3.utils.toBN(finalLbcBalance));
truffleAssertions.eventEmitted(resignTx, "Resigned", {
from: lpAddress,
});
Expand All @@ -834,10 +838,11 @@ contract("LiquidityBridgeContractV2.sol", async (accounts) => {
});
truffleAssertions.eventEmitted(withdrawCollateralTx, "WithdrawCollateral", {
from: lpAddress,
amount: initialLPCol,
amount: web3.utils.toBN(totalInitialCollateral.toString()),
});
expect(lbcBal).to.be.a.bignumber.eq(initialLPCol);
expect(web3.utils.toBN(0)).to.be.a.bignumber.eq(finalLPCol);
expect(lbcCollateral).to.be.a.bignumber.eq(totalInitialCollateral);
expect(web3.utils.toBN(0)).to.be.a.bignumber.eq(totalFinalCollateral);
expect(finalLpRbtc.sub(initialLpRbtc).add(withdrawCollateralGas)).to.be.a.bignumber.eq(totalInitialCollateral);
});

it("Should refundPegOut", async () => {
Expand Down Expand Up @@ -1368,7 +1373,7 @@ contract("LiquidityBridgeContractV2.sol", async (accounts) => {
);
await instance.resign({ from: lpAddress });
await utils.mineBlocks(utils.RESIGN_DELAY_BLOCKS);
await instance.withdrawPegoutCollateral({ from: lpAddress });
await instance.withdrawCollateral({ from: lpAddress });
const quoteHash = await instance.hashPegoutQuote(quote);
const signature = await web3.eth.sign(quoteHash, lpAddress);
const tx = instance.depositPegout(quote, signature, { value: web3.utils.toBN("500"), from: lpAddress });
Expand Down

0 comments on commit 7bdf9fd

Please sign in to comment.