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

Feat/arbitrum perps liquidity depositer #23

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 0 additions & 4 deletions .env

This file was deleted.

89 changes: 56 additions & 33 deletions contracts/BridgeEntrance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import "./libsv2/common/ZeroCopySink.sol";
import "./libsv2/utils/Utils.sol";

interface ICCM {
function crossChain(uint64 _toChainId, bytes calldata _toContract, bytes calldata _method, bytes calldata _txData) external returns (bool);
function crossChain(
uint64 _toChainId,
bytes calldata _toContract,
bytes calldata _method,
bytes calldata _txData
) external returns (bool);
}

interface ICCMProxy {
Expand All @@ -18,7 +23,9 @@ interface ICCMProxy {

interface ILockProxy {
function registry(address _assetHash) external view returns (bytes32);

function ccmProxy() external view returns (ICCMProxy);

function counterpartChainId() external view returns (uint64);
}

Expand Down Expand Up @@ -71,12 +78,7 @@ contract BridgeEntrance is ReentrancyGuard {
address _assetHash,
bytes[] calldata _bytesValues,
uint256[] calldata _uint256Values
)
external
payable
nonReentrant
returns (bool)
{
) external payable nonReentrant returns (bool) {
// it is very important that this function validates the success of a transfer correctly
// since, once this line is passed, the deposit is assumed to be successful
// which will eventually result in the specified amount of tokens being minted for the
Expand All @@ -96,16 +98,15 @@ contract BridgeEntrance is ReentrancyGuard {
address _assetHash,
bytes memory _proxyAddress,
bytes memory _fromAssetDenom
)
private
view
{
) private view {
require(_proxyAddress.length == 20, "Invalid proxyAddress");
bytes32 value = keccak256(abi.encodePacked(
_proxyAddress,
_fromAssetDenom
));
require(lockProxy.registry(_assetHash) == value, "Asset not registered");
bytes32 value = keccak256(
abi.encodePacked(_proxyAddress, _fromAssetDenom)
);
require(
lockProxy.registry(_assetHash) == value,
"Asset not registered"
);
}

/// @dev validates the asset registration and calls the CCM contract
Expand All @@ -121,9 +122,7 @@ contract BridgeEntrance is ReentrancyGuard {
address _fromAssetAddress,
bytes[] calldata _bytesValues,
uint256[] calldata _uint256Values
)
private
{
) private {
bytes memory _targetProxyHash = _bytesValues[0];
bytes memory _recoveryAddress = _bytesValues[1];
bytes memory _fromAssetDenom = _bytesValues[2];
Expand All @@ -137,9 +136,16 @@ contract BridgeEntrance is ReentrancyGuard {
require(_bytesValues[4].length > 0, "Empty toAddress");
require(_bytesValues[5].length > 0, "Empty toAssetDenom");
require(_amount > 0, "Amount must be more than zero");
require(_withdrawFeeAmount < _amount, "Fee amount cannot be greater than amount");
require(
_withdrawFeeAmount < _amount,
"Fee amount cannot be greater than amount"
);

_validateAssetRegistration(_fromAssetAddress, _targetProxyHash, _fromAssetDenom);
_validateAssetRegistration(
_fromAssetAddress,
_targetProxyHash,
_fromAssetDenom
);

TransferTxArgs memory txArgs = TransferTxArgs({
fromAssetAddress: Utils.addressToBytes(_fromAssetAddress),
Expand All @@ -156,17 +162,28 @@ contract BridgeEntrance is ReentrancyGuard {
ICCM ccm = _getCcm();
uint64 counterpartChainId = lockProxy.counterpartChainId();
require(
ccm.crossChain(counterpartChainId, _targetProxyHash, "unlock", txData),
ccm.crossChain(
counterpartChainId,
_targetProxyHash,
"unlock",
txData
),
"EthCrossChainManager crossChain executed error!"
);

emit LockEvent(_fromAssetAddress, counterpartChainId, _fromAssetDenom, _recoveryAddress, txData);
emit LockEvent(
_fromAssetAddress,
counterpartChainId,
_fromAssetDenom,
_recoveryAddress,
txData
);
}

function _getCcm() private view returns (ICCM) {
ICCMProxy ccmProxy = lockProxy.ccmProxy();
ICCM ccm = ICCM(ccmProxy.getEthCrossChainManager());
return ccm;
ICCMProxy ccmProxy = lockProxy.ccmProxy();
ICCM ccm = ICCM(ccmProxy.getEthCrossChainManager());
return ccm;
}

/// @dev transfers funds from an address into this contract
Expand All @@ -178,12 +195,13 @@ contract BridgeEntrance is ReentrancyGuard {
address _assetHash,
uint256 _amount,
uint256 _callAmount
)
private
{
) private {
if (_assetHash == ETH_ASSET_HASH) {
require(msg.value == _amount, "ETH transferred does not match the expected amount");
(bool sent,) = address(lockProxy).call{value: msg.value}("");
require(
msg.value == _amount,
"ETH transferred does not match the expected amount"
);
(bool sent, ) = address(lockProxy).call{value: msg.value}("");
require(sent, "Failed to send Ether to LockProxy");
return;
}
Expand All @@ -192,10 +210,15 @@ contract BridgeEntrance is ReentrancyGuard {
uint256 before = token.balanceOf(address(lockProxy));
token.safeTransferFrom(msg.sender, address(lockProxy), _callAmount);
uint256 transferred = token.balanceOf(address(lockProxy)).sub(before);
require(transferred == _amount, "Tokens transferred does not match the expected amount");
require(
transferred == _amount,
"Tokens transferred does not match the expected amount"
);
}

function _serializeTransferTxArgs(TransferTxArgs memory args) private pure returns (bytes memory) {
function _serializeTransferTxArgs(
TransferTxArgs memory args
) private pure returns (bytes memory) {
bytes memory buff;
buff = abi.encodePacked(
ZeroCopySink.WriteVarBytes(args.fromAssetAddress),
Expand Down
Loading