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

Audit Fixes & Generalised Events #20

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
97 changes: 82 additions & 15 deletions contracts/upgradeable-Bridge/FiberRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
address targetAddress
);

event Withdraw(
address token,
address receiver,
uint256 amount,
bytes32 salt,
bytes signature
);

event NonEvmSwap(
address sourceToken,
string targetToken,
uint256 sourceChainId,
string targetChainId,
uint256 sourceAmount,
address sourceAddress,
string targetAddress
);

/**
@notice The payable receive method
*/
Expand Down Expand Up @@ -149,6 +167,15 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
targetToken,
targetAddress
);
NonEvmSwap(
token,
targetToken,
block.chainid,
targetNetwork,
amount,
_msgSender(),
targetAddress
);
}

/*
Expand All @@ -170,7 +197,8 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
address[] calldata path,
uint256 deadline,
uint256 crossTargetNetwork,
address crossTargetToken
address crossTargetToken,
address crossTargetAddress
) external nonReentrant {
amountIn = SafeAmount.safeTransferFrom(
path[0],
Expand All @@ -188,8 +216,15 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
deadline,
crossTargetNetwork,
crossTargetToken
// crossSwapTargetTokenTo
// crossTargetAddress
);
Swap(
path[0],
crossTargetToken,
block.chainid,
crossTargetNetwork,
amountIn,
_msgSender(),
crossTargetAddress
);
}

Expand All @@ -213,7 +248,7 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
uint256 deadline,
string memory crossTargetNetwork,
string memory crossTargetToken,
string memory receiver
string memory crossTargetAddress
) external nonReentrant {
amountIn = SafeAmount.safeTransferFrom(
path[0],
Expand All @@ -223,16 +258,23 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
);
IERC20Upgradeable(path[0]).approve(swapRouter, amountIn);
_nonEvmSwapAndCross(
receiver,
crossTargetAddress,
swapRouter,
amountIn,
amountCrossMin,
path,
deadline,
crossTargetNetwork,
crossTargetToken
// crossSwapTargetTokenTo
// crossTargetAddress
);
NonEvmSwap(
path[0],
crossTargetToken,
block.chainid,
crossTargetNetwork,
amountIn,
_msgSender(),
crossTargetAddress
);
}

Expand All @@ -253,8 +295,9 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
address[] calldata path,
uint256 deadline,
uint256 crossTargetNetwork,
address crossTargetToken
) external payable {
address crossTargetToken,
address crossTargetAddress
) external payable nonReentrant {
uint256 amountIn = msg.value;
address weth = IUniswapV2Router01(swapRouter).WETH();
// approveIfRequired(weth, swapRouter, amountIn);
Expand All @@ -269,8 +312,15 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
deadline,
crossTargetNetwork,
crossTargetToken
// crossSwapTargetTokenTo
// crossTargetAddress
);
Swap(
path[0],
crossTargetToken,
block.chainid,
crossTargetNetwork,
amountIn,
_msgSender(),
crossTargetAddress
);
}

Expand Down Expand Up @@ -298,14 +348,16 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
salt,
multiSignature
);

emit Withdraw(token, payee, amount, salt, multiSignature);
}

/*
@notice Withdraws funds based on a multisig
@notice Withdraw funds based on a multisig
@dev For signature swapToToken must be the same as token
@param token The token to withdraw
@param payee Address for where to send the tokens to
@param amount The mount
@param amount The amount
@param sourceChainId The source chain initiating the tx
@param swapTxId The txId for the swap from the source chain
@param multiSignature The multisig validator signature
Expand All @@ -317,7 +369,7 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
uint256 amount,
bytes32 salt,
bytes memory multiSignature
) external {
) external nonReentrant {
uint256 bridgeFoundryPrice = getFoundryTokenPrice(bridgeFoundry);
uint256 targetFoundryPrice = getFoundryTokenPrice(targetFoundry);
uint256 amountOut = (amount * bridgeFoundryPrice) / targetFoundryPrice;
Expand All @@ -328,6 +380,7 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
salt,
multiSignature
);
emit Withdraw(targetFoundry, payee, amount, salt, multiSignature);
}

/*
Expand Down Expand Up @@ -370,6 +423,13 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
to,
deadline
);
emit Withdraw(
path[path.length - 1],
to,
amountOutMin,
salt,
multiSignature
);
}

/*
Expand Down Expand Up @@ -410,6 +470,13 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
to,
deadline
);
emit Withdraw(
path[path.length - 1],
to,
amountOutMin,
salt,
multiSignature
);
}

/*
Expand Down Expand Up @@ -495,4 +562,4 @@ contract FiberRouter is ReentrancyGuardUpgradeable, OwnableUpgradeable {
to
);
}
}
}
46 changes: 27 additions & 19 deletions contracts/upgradeable-Bridge/FundManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/cryptography/draft-EIP712Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import "../common/signature/SigCheckable.sol";
import "../common/SafeAmount.sol";
import "../common/WithAdmin.sol";
import "../taxing/IGeneralTaxDistributor.sol";
import "hardhat/console.sol";

contract FundManager is SigCheckable, WithAdmin {
contract FundManager is SigCheckable, WithAdmin, ReentrancyGuardUpgradeable {
using SafeERC20Upgradeable for IERC20Upgradeable;

address public router;
Expand All @@ -33,13 +33,21 @@ contract FundManager is SigCheckable, WithAdmin {

event TransferBySignature(
address signer,
address receiver,
address indexed receiver,
address token,
uint256 amount,
uint256 fee
);
event BridgeLiquidityAdded(address actor, address token, uint256 amount);
event BridgeLiquidityRemoved(address actor, address token, uint256 amount);
event BridgeLiquidityAdded(
address indexed actor,
address token,
uint256 amount
);
event BridgeLiquidityRemoved(
address indexed actor,
address token,
uint256 amount
);
event BridgeSwap(
address from,
address indexed token,
Expand All @@ -66,6 +74,7 @@ contract FundManager is SigCheckable, WithAdmin {
function initialize() external initializer {
__EIP712_init(NAME, VERSION);
__Ownable_init();
__ReentrancyGuard_init();
}

/**
Expand Down Expand Up @@ -104,6 +113,10 @@ contract FundManager is SigCheckable, WithAdmin {
@notice add address as an fee distributor
*/
function setFeeDistributor(address _feeDistributor) external onlyOwner {
require(
_feeDistributor != address(0),
"Invalid address: address cannot be 0."
);
feeDistributor = _feeDistributor;
}

Expand Down Expand Up @@ -239,10 +252,6 @@ contract FundManager is SigCheckable, WithAdmin {
address targetToken,
address targetAddress
) external onlyRouter returns (uint256) {
require(
targetAddress != address(0),
"BridgePool: targetAddress is required"
);
return
_swap(
msg.sender,
Expand Down Expand Up @@ -298,18 +307,17 @@ contract FundManager is SigCheckable, WithAdmin {
) external onlyRouter returns (uint256) {
require(token != address(0), "BP: bad token");
require(payee != address(0), "BP: bad payee");
require(salt != 0, "BP: bad salt");
require(amount != 0, "BP: bad amount");
require(salt != 0, "BP: bad salt");
bytes32 message = withdrawSignedMessage(token, payee, amount, salt);
address _signer = signerUnique(message, signature);
console.log(_signer);
require(signers[_signer], "BridgePool: Invalid signer");

uint256 fee = 0;
address _feeDistributor = feeDistributor;
if (_feeDistributor != address(0)) {
fee = (amount * fees[token]) / 10000;
amount = amount - fee;
amount -= fee;
if (fee != 0) {
IERC20Upgradeable(token).safeTransfer(_feeDistributor, fee);
IGeneralTaxDistributor(_feeDistributor).distributeTax(token);
Expand All @@ -333,15 +341,13 @@ contract FundManager is SigCheckable, WithAdmin {
isFoundryAsset[token] == true,
"Only foundry assets can be added"
);
liquidities[token][msg.sender] += amount;
amount = SafeAmount.safeTransferFrom(
token,
msg.sender,
address(this),
amount
);
liquidities[token][msg.sender] =
liquidities[token][msg.sender] +
amount;
emit BridgeLiquidityAdded(msg.sender, token, amount);
}

Expand All @@ -365,9 +371,7 @@ contract FundManager is SigCheckable, WithAdmin {
require(liq >= amount, "Not enough liquidity");
uint256 balance = IERC20Upgradeable(token).balanceOf(address(this));
uint256 actualLiq = balance > amount ? amount : balance;
liquidities[token][msg.sender] =
liquidities[token][msg.sender] -
actualLiq;
liquidities[token][msg.sender] -= actualLiq;
if (actualLiq != 0) {
IERC20Upgradeable(token).safeTransfer(msg.sender, actualLiq);
emit BridgeLiquidityRemoved(msg.sender, token, amount);
Expand All @@ -376,7 +380,7 @@ contract FundManager is SigCheckable, WithAdmin {
}

/**
@dev callable by only router
@dev external used function for singnature verification
@param token the tokens want to withdraw
@param payee address of beneficiary
@param amount the amount to be withdrawn
Expand Down Expand Up @@ -426,6 +430,10 @@ contract FundManager is SigCheckable, WithAdmin {
address targetToken,
address targetAddress
) internal returns (uint256) {
require(
targetAddress != address(0),
"BridgePool: targetAddress is required"
);
require(from != address(0), "BP: bad from");
require(token != address(0), "BP: bad token");
require(targetNetwork != 0, "BP: targetNetwork is requried");
Expand Down