Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Alvarez committed Feb 3, 2021
1 parent 6707a73 commit 239d324
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 169 deletions.
15 changes: 0 additions & 15 deletions contracts/Bridge.sol

This file was deleted.

10 changes: 2 additions & 8 deletions contracts/BridgeMock.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;

import "./Bridge.sol";

contract BridgeMock is Bridge {
contract BridgeMock {

mapping(bytes32 => uint256) private amounts;

Expand All @@ -16,7 +14,7 @@ contract BridgeMock is Bridge {
address payable liquidityBridgeContractAddress,
bytes calldata liquidityProviderBtcAddress,
bool shouldTransferToContract
) external override returns (int256) {
) external returns (int256) {
uint256 amount = amounts[derivationArgumentsHash];
amounts[derivationArgumentsHash] = 0;
(bool success, ) = liquidityBridgeContractAddress.call{value: amount}("");
Expand All @@ -26,8 +24,4 @@ contract BridgeMock is Bridge {
function setPegin(bytes32 derivationArgumentsHash) public payable {
amounts[derivationArgumentsHash] = msg.value;
}

function getPegin(bytes32 hashe) external view returns (uint256) {
return amounts[hashe];
}
}
137 changes: 57 additions & 80 deletions contracts/LiquidityBridgeContract.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma experimental ABIEncoderV2;

import './Bridge.sol';
import './BridgeMock.sol';

contract LiquidityBridgeContract {

Bridge bridge;
struct DerivationParams {
bytes fedBtcAddress;
address liquidityProviderRskAddress;
address rskRefundAddress;
address contractAddress;
bytes data;
uint penaltyFee;
uint successFee;
uint gasLimit;
uint nonce;
uint value;
}

BridgeMock bridge;
mapping(address => uint256) private balances;
mapping(address => uint256) private deposits;
mapping(bytes32 => address) private callRegistry;
mapping(bytes32 => bool) private callRegistry;
mapping(bytes32 => bool) private callSuccess;

constructor(address bridgeAddress) {
bridge = Bridge(bridgeAddress);
bridge = BridgeMock(bridgeAddress);
}

receive() external payable { }
Expand All @@ -33,111 +47,74 @@ contract LiquidityBridgeContract {
return balances[lp];
}

function callForUser(
bytes memory fedBtcAddress,
address liquidityProviderRskAddress,
address payable rskRefundAddress,
address contractAddress,
bytes memory data,
uint penaltyFee,
uint successFee,
uint gasLimit,
uint nonce,
uint value
) external payable {
require(msg.sender == liquidityProviderRskAddress, "Unauthorized");
require(deposits[liquidityProviderRskAddress] >= penaltyFee, "Insufficient collateral");

balances[liquidityProviderRskAddress] += msg.value;

require(balances[liquidityProviderRskAddress] >= value, "Insufficient funds");

bytes32 derivationHash = hash(
fedBtcAddress,
liquidityProviderRskAddress,
rskRefundAddress,
contractAddress,
data,
penaltyFee,
successFee,
gasLimit,
nonce,
value);

(bool success, bytes memory ret) = contractAddress.call{gas:gasLimit, value: value}(data);

callRegistry[derivationHash] = liquidityProviderRskAddress;
function callForUser(DerivationParams memory params) external payable returns (bool) {
require(msg.sender == params.liquidityProviderRskAddress, "Unauthorized");
require(deposits[params.liquidityProviderRskAddress] >= params.penaltyFee, "Insufficient collateral");
require(balances[params.liquidityProviderRskAddress] + msg.value >= params.value, "Insufficient funds");

bytes32 derivationHash = hash(params);

(bool success, bytes memory ret) = params.contractAddress.call{gas:params.gasLimit, value: params.value}(params.data);

balances[params.liquidityProviderRskAddress] += msg.value;
callRegistry[derivationHash] = true;

if (success) {
balances[liquidityProviderRskAddress] -= value;
balances[params.liquidityProviderRskAddress] -= params.value;
callSuccess[derivationHash] = true;
}
}
return success;
}

function registerFastBridgeBtcTransaction(
DerivationParams memory params,
bytes memory btcRawTransaction,
bytes memory partialMerkleTree,
uint256 height,
bytes memory userBtcRefundAddress,
bytes memory liquidityProviderBtcAddress,
address payable rskRefundAddress,
bytes32 preHash,
uint256 successFee,
uint256 penaltyFee
bytes memory liquidityProviderBtcAddress
) public returns (int256) {
address liquidityProviderRskAddress = callRegistry[preHash];
bytes32 derivationHash = hash(params);

int256 transferredAmount = bridge.registerFastBridgeBtcTransaction(
btcRawTransaction,
height,
partialMerkleTree,
preHash,
derivationHash,
userBtcRefundAddress,
address(this),
liquidityProviderBtcAddress,
liquidityProviderRskAddress != address(0x0)
callRegistry[derivationHash]
);

if (transferredAmount > 0 && liquidityProviderRskAddress != address(0x0)) {
if (callSuccess[preHash]) {
balances[liquidityProviderRskAddress] += uint256(transferredAmount);
callSuccess[preHash] = false;
if (transferredAmount > 0 && callRegistry[derivationHash]) {
if (callSuccess[derivationHash]) {
balances[params.liquidityProviderRskAddress] += uint256(transferredAmount);
callSuccess[derivationHash] = false;
} else {
balances[liquidityProviderRskAddress] += successFee;
(bool success, ) = rskRefundAddress.call{value : uint256(transferredAmount) - successFee}("");
balances[params.liquidityProviderRskAddress] += params.successFee;
(bool success, ) = params.rskRefundAddress.call{value : uint256(transferredAmount) - params.successFee}("");
}
callRegistry[preHash] = address(0x0);
callRegistry[derivationHash] = false;
} else if (transferredAmount > 0) {
deposits[liquidityProviderRskAddress] -= penaltyFee;
(bool success, ) = rskRefundAddress.call{value : uint256(transferredAmount) + penaltyFee}("");
deposits[params.liquidityProviderRskAddress] -= params.penaltyFee;
(bool success, ) = params.rskRefundAddress.call{value : uint256(transferredAmount) + params.penaltyFee}("");
}
return transferredAmount;
}

function hash(
bytes memory fedBtcAddress,
address liquidityProviderRskAddress,
address rskRefundAddress,
address callContract,
bytes memory callContractArguments,
uint penaltyFee,
uint successFee,
uint gasLimit,
uint nonce ,
uint valueToTransfer
) public pure returns (bytes32) {

function hash(DerivationParams memory params) public pure returns (bytes32) {
return keccak256(abi.encode(
fedBtcAddress,
liquidityProviderRskAddress,
rskRefundAddress,
callContract,
callContractArguments,
penaltyFee,
successFee,
gasLimit,
nonce,
valueToTransfer
params.fedBtcAddress,
params.liquidityProviderRskAddress,
params.rskRefundAddress,
params.contractAddress,
params.data,
params.penaltyFee,
params.successFee,
params.gasLimit,
params.nonce,
params.value
));
}
}
4 changes: 4 additions & 0 deletions contracts/Mock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ contract Mock {
function check() external view returns (int){
return status;
}

function fail() external pure {
require(false, "error");
}
}
Loading

0 comments on commit 239d324

Please sign in to comment.