Skip to content

Commit

Permalink
added bento bridge contract
Browse files Browse the repository at this point in the history
  • Loading branch information
sarangparikh22 committed Apr 18, 2022
1 parent e7161fc commit a2e857f
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 1 deletion.
89 changes: 88 additions & 1 deletion contracts/BentoboxBridgeStargate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,91 @@

pragma solidity 0.8.11;

contract BentoboxBridgeStargate {}
import "@openzeppelin/contracts/interfaces/IERC20.sol";
import "./interfaces/IBentoBoxMinimal.sol";
import "./interfaces/IStargateRouter.sol";
import "./interfaces/IStargateReceiver.sol";

contract BentoboxBridgeStargate is IStargateReceiver {
struct BridgeParams {
uint16 dstChainId;
address token;
uint256 srcPoolId;
uint256 dstPoolId;
uint256 amount;
uint256 amountMin;
address receiver;
address to;
bool fromBento;
bool toBento;
}

IBentoBoxMinimal public immutable bentoBox;
IStargateRouter public immutable stargateRouter;

constructor(IBentoBoxMinimal _bentoBox, IStargateRouter _stargateRouter) {
stargateRouter = _stargateRouter;
bentoBox = _bentoBox;
_bentoBox.registerProtocol();
}

function approveToStargateRouter(IERC20 token) external {
token.approve(address(stargateRouter), type(uint256).max);
}

function teleport(BridgeParams memory bridgeParams) external payable {
if (bridgeParams.fromBento) {
bentoBox.withdraw(
bridgeParams.token,
msg.sender,
address(this),
bridgeParams.amount,
0
);
} else {
IERC20(bridgeParams.token).transferFrom(
msg.sender,
address(this),
bridgeParams.amount
);
}

bytes memory payload = abi.encode(
bridgeParams.toBento,
bridgeParams.to
);

stargateRouter.swap{value: address(this).balance}(
bridgeParams.dstChainId,
bridgeParams.srcPoolId,
bridgeParams.dstPoolId,
payable(msg.sender),
bridgeParams.amount,
bridgeParams.amountMin,
IStargateRouter.lzTxObj(
500000,
0,
abi.encodePacked(bridgeParams.receiver)
),
abi.encodePacked(bridgeParams.receiver),
payload
);
}

function sgReceive(
uint16 _chainId,
bytes memory _srcAddress,
uint256 _nonce,
address _token,
uint256 amountLD,
bytes memory payload
) external override {
(bool toBento, address to) = abi.decode(payload, (bool, address));
if (toBento) {
IERC20(_token).transfer(address(bentoBox), amountLD);
bentoBox.deposit(_token, address(bentoBox), to, amountLD, 0);
} else {
IERC20(_token).transfer(to, amountLD);
}
}
}
86 changes: 86 additions & 0 deletions contracts/interfaces/IBentoBoxMinimal.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity 0.8.11;

/// @notice Minimal BentoBox vault interface.
/// @dev `token` is aliased as `address` from `IERC20` for simplicity.
interface IBentoBoxMinimal {
/// @notice Balance per ERC-20 token per account in shares.
function balanceOf(address, address) external view returns (uint256);

/// @dev Helper function to represent an `amount` of `token` in shares.
/// @param token The ERC-20 token.
/// @param amount The `token` amount.
/// @param roundUp If the result `share` should be rounded up.
/// @return share The token amount represented in shares.
function toShare(
address token,
uint256 amount,
bool roundUp
) external view returns (uint256 share);

/// @dev Helper function to represent shares back into the `token` amount.
/// @param token The ERC-20 token.
/// @param share The amount of shares.
/// @param roundUp If the result should be rounded up.
/// @return amount The share amount back into native representation.
function toAmount(
address token,
uint256 share,
bool roundUp
) external view returns (uint256 amount);

/// @notice Registers this contract so that users can approve it for BentoBox.
function registerProtocol() external;

/// @notice Deposit an amount of `token` represented in either `amount` or `share`.
/// @param token_ The ERC-20 token to deposit.
/// @param from which account to pull the tokens.
/// @param to which account to push the tokens.
/// @param amount Token amount in native representation to deposit.
/// @param share Token amount represented in shares to deposit. Takes precedence over `amount`.
/// @return amountOut The amount deposited.
/// @return shareOut The deposited amount represented in shares.
function deposit(
address token_,
address from,
address to,
uint256 amount,
uint256 share
) external payable returns (uint256 amountOut, uint256 shareOut);

/// @notice Withdraws an amount of `token` from a user account.
/// @param token_ The ERC-20 token to withdraw.
/// @param from which user to pull the tokens.
/// @param to which user to push the tokens.
/// @param amount of tokens. Either one of `amount` or `share` needs to be supplied.
/// @param share Like above, but `share` takes precedence over `amount`.
function withdraw(
address token_,
address from,
address to,
uint256 amount,
uint256 share
) external returns (uint256 amountOut, uint256 shareOut);

/// @notice Transfer shares from a user account to another one.
/// @param token The ERC-20 token to transfer.
/// @param from which user to pull the tokens.
/// @param to which user to push the tokens.
/// @param share The amount of `token` in shares.
function transfer(
address token,
address from,
address to,
uint256 share
) external;

function setMasterContractApproval(
address user,
address masterContract,
bool approved,
uint8 v,
bytes32 r,
bytes32 s
) external;
}
14 changes: 14 additions & 0 deletions contracts/interfaces/IStargateReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.11;

interface IStargateReceiver {
function sgReceive(
uint16 _chainId,
bytes memory _srcAddress,
uint256 _nonce,
address _token,
uint256 amountLD,
bytes memory payload
) external;
}
32 changes: 32 additions & 0 deletions contracts/interfaces/IStargateRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.11;

interface IStargateRouter {

struct lzTxObj {
uint256 dstGasForCall;
uint256 dstNativeAmount;
bytes dstNativeAddr;
}

function swap(
uint16 _dstChainId,
uint256 _srcPoolId,
uint256 _dstPoolId,
address payable _refundAddress,
uint256 _amountLD,
uint256 _minAmountLD,
lzTxObj memory _lzTxParams,
bytes calldata _to,
bytes calldata _payload
) external payable;

function quoteLayerZeroFee(
uint16 _dstChainId,
uint8 _functionType,
bytes calldata _toAddress,
bytes calldata _transferAndCallPayload,
lzTxObj memory _lzTxParams
) external view returns (uint256, uint256);
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,8 @@
"ts-node": "^10.4.0",
"typechain": "^6.1.0",
"typescript": "^4.5.0"
},
"dependencies": {
"@openzeppelin/contracts": "^4.5.0"
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,11 @@
"@types/sinon-chai" "^3.2.3"
"@types/web3" "1.0.19"

"@openzeppelin/contracts@^4.5.0":
version "4.5.0"
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.5.0.tgz#3fd75d57de172b3743cdfc1206883f56430409cc"
integrity sha512-fdkzKPYMjrRiPK6K4y64e6GzULR7R7RwxSigHS8DDp7aWDeoReqsQI+cxHV1UuhAqX69L1lAaWDxenfP+xiqzA==

"@resolver-engine/core@^0.3.3":
version "0.3.3"
resolved "https://registry.yarnpkg.com/@resolver-engine/core/-/core-0.3.3.tgz#590f77d85d45bc7ecc4e06c654f41345db6ca967"
Expand Down

0 comments on commit a2e857f

Please sign in to comment.