Skip to content

Commit

Permalink
prepare bridge to add interchain adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
iammrjude committed Dec 17, 2023
1 parent 6bc533e commit 82e3e97
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 40 deletions.
80 changes: 80 additions & 0 deletions src/adapters/LayerZeroInterChainAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import "../core/base/Controllable.sol";
import "../interfaces/IInterChainAdapter.sol";
import "../interfaces/IChildTokenFactory.sol";
import "./lzApp/NonblockingLzApp.sol";

/// @author Jude (https://github.com/iammrjude)
contract LayerZeroInterChainAdapter is Controllable, IInterChainAdapter, NonblockingLzApp {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

bytes public constant PAYLOAD = "\x01\x02\x03\x04";

/// @dev Version of LayerZeroInterChainAdapter implementation
string public constant VERSION = "1.0.0";

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INITIALIZATION */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

function initialize(address platform_, address _lzEndpoint) external initializer {

Check warning

Code scanning / Slither

Conformance to Solidity naming conventions Warning

__Controllable_init(platform_);
__NonblockingLzApp_init(_lzEndpoint);

Check warning on line 26 in src/adapters/LayerZeroInterChainAdapter.sol

View check run for this annotation

Codecov / codecov/patch

src/adapters/LayerZeroInterChainAdapter.sol#L25-L26

Added lines #L25 - L26 were not covered by tests
}

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* RESTRICTED ACTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

/// @inheritdoc IInterChainAdapter
function sendMessage(Message memory message) external {}

/// @inheritdoc IInterChainAdapter
function interChainAdapterId() external returns (string memory) {}

function setOracle(uint16 dstChainId, address oracle) external onlyOwner {
uint TYPE_ORACLE = 6;

Check warning on line 40 in src/adapters/LayerZeroInterChainAdapter.sol

View check run for this annotation

Codecov / codecov/patch

src/adapters/LayerZeroInterChainAdapter.sol#L40

Added line #L40 was not covered by tests
// set the Oracle
lzEndpoint.setConfig(lzEndpoint.getSendVersion(address(this)), dstChainId, TYPE_ORACLE, abi.encode(oracle));

Check warning on line 42 in src/adapters/LayerZeroInterChainAdapter.sol

View check run for this annotation

Codecov / codecov/patch

src/adapters/LayerZeroInterChainAdapter.sol#L42

Added line #L42 was not covered by tests
}

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* VIEW FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

/// @inheritdoc IInterChainAdapter
function endpoint() external view returns (address) {}

function estimateFee(
uint16 _dstChainId,

Check warning

Code scanning / Slither

Conformance to Solidity naming conventions Warning

bool _useZro,

Check warning

Code scanning / Slither

Conformance to Solidity naming conventions Warning

bytes calldata _adapterParams

Check warning

Code scanning / Slither

Conformance to Solidity naming conventions Warning

) public view returns (uint nativeFee, uint zroFee) {
return lzEndpoint.estimateFees(_dstChainId, address(this), PAYLOAD, _useZro, _adapterParams);

Check warning on line 57 in src/adapters/LayerZeroInterChainAdapter.sol

View check run for this annotation

Codecov / codecov/patch

src/adapters/LayerZeroInterChainAdapter.sol#L57

Added line #L57 was not covered by tests
}

function getOracle(uint16 remoteChainId) external view returns (address _oracle) {
bytes memory bytesOracle =
lzEndpoint.getConfig(lzEndpoint.getSendVersion(address(this)), remoteChainId, address(this), 6);

Check warning on line 62 in src/adapters/LayerZeroInterChainAdapter.sol

View check run for this annotation

Codecov / codecov/patch

src/adapters/LayerZeroInterChainAdapter.sol#L61-L62

Added lines #L61 - L62 were not covered by tests
assembly {
_oracle := mload(add(bytesOracle, 32))

Check warning on line 64 in src/adapters/LayerZeroInterChainAdapter.sol

View check run for this annotation

Codecov / codecov/patch

src/adapters/LayerZeroInterChainAdapter.sol#L64

Added line #L64 was not covered by tests
}
}

Check warning

Code scanning / Slither

Assembly usage Warning


/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL LOGIC */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

function _nonblockingLzReceive(uint16, bytes memory, uint64, bytes memory _payload) internal override {
(address toAddress, uint amountOrTokenId, address token, bool nft, bool mint) =
abi.decode(_payload, (address, uint, address, bool, bool));

Check warning on line 74 in src/adapters/LayerZeroInterChainAdapter.sol

View check run for this annotation

Codecov / codecov/patch

src/adapters/LayerZeroInterChainAdapter.sol#L73-L74

Added lines #L73 - L74 were not covered by tests
// address childToken = IChildTokenFactory(_getStorage().childTokenFactory).getChildTokenOf(token);
// address parentToken = IChildTokenFactory(_getStorage().childTokenFactory).getParentTokenOf(token);
// if (mint) mintToken(childToken, toAddress, amountOrTokenId, nft);
// if (!mint) unlockToken(parentToken, toAddress, amountOrTokenId, nft);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
45 changes: 5 additions & 40 deletions src/bridge/Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ import "../core/base/Controllable.sol";
import "../interfaces/IBridge.sol";
import "../interfaces/IChildERC20.sol";
import "../interfaces/IChildERC721.sol";
import "../interfaces/IChildTokenFactory.sol";
import "./lzApp/NonblockingLzApp.sol";
import "../interfaces/IInterChainAdapter.sol";

/// @title Stability Bridge
/// @author Jude (https://github.com/iammrjude)
contract Bridge is Controllable, IBridge, NonblockingLzApp {
contract Bridge is Controllable, IBridge {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

bytes public constant PAYLOAD = "\x01\x02\x03\x04";

/// @inheritdoc IControllable
string public constant VERSION = "1.0.0";

Expand All @@ -42,9 +39,8 @@ contract Bridge is Controllable, IBridge, NonblockingLzApp {
/* INITIALIZATION */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

function initialize(address platform_, address _lzEndpoint) external initializer {
function initialize(address platform_) external initializer {
__Controllable_init(platform_);
__NonblockingLzApp_init(_lzEndpoint);
}

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
Expand Down Expand Up @@ -75,22 +71,6 @@ contract Bridge is Controllable, IBridge, NonblockingLzApp {
/// @inheritdoc IBridge
function getTarget(address token, uint64 chainTo) external view returns (address targetToken, bytes32 linkHash) {}

function estimateFee(
uint16 _dstChainId,
bool _useZro,
bytes calldata _adapterParams
) public view returns (uint nativeFee, uint zroFee) {
return lzEndpoint.estimateFees(_dstChainId, address(this), PAYLOAD, _useZro, _adapterParams);
}

function getOracle(uint16 remoteChainId) external view returns (address _oracle) {
bytes memory bytesOracle =
lzEndpoint.getConfig(lzEndpoint.getSendVersion(address(this)), remoteChainId, address(this), 6);
assembly {
_oracle := mload(add(bytesOracle, 32))
}
}

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* RESTRICTED ACTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
Expand Down Expand Up @@ -135,7 +115,7 @@ contract Bridge is Controllable, IBridge, NonblockingLzApp {
if (!success) revert TokenTransferFailed();
}
bytes memory payload = abi.encode(msg.sender, amountOrTokenId, token, nft, true);
_lzSend(chainTo, payload, payable(msg.sender), address(0x0), bytes(""), msg.value);
// _lzSend(chainTo, payload, payable(msg.sender), address(0x0), bytes(""), msg.value);
}

function burnToken(address token, uint amountOrTokenId, uint16 chainTo, bool nft) public payable {
Expand All @@ -145,13 +125,7 @@ contract Bridge is Controllable, IBridge, NonblockingLzApp {
IChildERC20(token).burn(msg.sender, amountOrTokenId);
}
bytes memory payload = abi.encode(msg.sender, amountOrTokenId, token, nft, false);
_lzSend(chainTo, payload, payable(msg.sender), address(0x0), bytes(""), msg.value);
}

function setOracle(uint16 dstChainId, address oracle) external onlyOwner {
uint TYPE_ORACLE = 6;
// set the Oracle
lzEndpoint.setConfig(lzEndpoint.getSendVersion(address(this)), dstChainId, TYPE_ORACLE, abi.encode(oracle));
// _lzSend(chainTo, payload, payable(msg.sender), address(0x0), bytes(""), msg.value);
}

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
Expand Down Expand Up @@ -181,13 +155,4 @@ contract Bridge is Controllable, IBridge, NonblockingLzApp {
if (!success) revert TokenTransferFailed();
}
}

function _nonblockingLzReceive(uint16, bytes memory, uint64, bytes memory _payload) internal override {
(address toAddress, uint amountOrTokenId, address token, bool nft, bool mint) =
abi.decode(_payload, (address, uint, address, bool, bool));
address childToken = IChildTokenFactory(_getStorage().childTokenFactory).getChildTokenOf(token);
address parentToken = IChildTokenFactory(_getStorage().childTokenFactory).getParentTokenOf(token);
if (mint) mintToken(childToken, toAddress, amountOrTokenId, nft);
if (!mint) unlockToken(parentToken, toAddress, amountOrTokenId, nft);
}
}

Check warning

Code scanning / Slither

Contracts that lock Ether Medium

0 comments on commit 82e3e97

Please sign in to comment.