Skip to content

Commit

Permalink
Add Celer bridge (#18)
Browse files Browse the repository at this point in the history
* Improve stargate and wormhole

* Add celer bridge

* Add celer swap test(goerli -> avax-test)

* Refactor LibSoFeeCelerV1 to support oracle

* Fix estCelerMessageFeeAndExecutorFee

* Format codes

* Format codes

* Add trace info in CelerFacet.sol

* Impl executeMessageWithTransferRefund to refund the token to the user

* Add comments

* Refactor estCelerMessageFeeAndExecutorFee

* Split celer scripts

* Revert swap.py

* Fix RefundCelerToken event

* Add test files

* Add celer mainnet chain path

* Fix CelerFacet.sol

* Prepare mainnet config

* Add comments

* Check celer mainnet oracles

* Format codes

* Add set_celer_bnb_price_on_avax

* Remove unused config

* Revert "Improve stargate and wormhole"

This reverts commit 1b64598.

* Add setExecutorFeeTo

* Update config and abi of celer

* Update config file

* Redeploy celer faucet for test

---------

Co-authored-by: icodezjb <[email protected]>
Co-authored-by: AAweidai <[email protected]>
  • Loading branch information
3 people authored Mar 14, 2023
1 parent 358d5a6 commit b62c7a5
Show file tree
Hide file tree
Showing 29 changed files with 6,602 additions and 4,363 deletions.
347 changes: 345 additions & 2 deletions ethereum/brownie-config.yaml

Large diffs are not rendered by default.

827 changes: 827 additions & 0 deletions ethereum/contracts/Facets/CelerFacet.sol

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions ethereum/contracts/Helpers/CelerMessageReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: GPLv3
pragma solidity 0.8.13;

import "../Interfaces/ICelerMessageReceiver.sol";

abstract contract CelerMessageReceiver is ICelerMessageReceiver {
/**
* @notice Called by MessageBus to execute a message
* @param _sender The address of the source app contract
* @param _srcChainId The source chain ID where the transfer is originated from
* @param _message Arbitrary message bytes originated from and encoded by the source app contract
* @param _executor Address who called the MessageBus execution function
*/
function executeMessage(
address _sender,
uint64 _srcChainId,
bytes calldata _message,
address _executor
) external payable virtual override returns (ExecutionStatus) {}

// execute message from non-evm chain with bytes for sender address,
// otherwise same as above.
function executeMessage(
bytes calldata _sender,
uint64 _srcChainId,
bytes calldata _message,
address _executor
) external payable virtual override returns (ExecutionStatus) {}

/**
* @notice Called by MessageBus to execute a message with an associated token transfer.
* The contract is guaranteed to have received the right amount of tokens before this function is called.
* @param _sender The address of the source app contract
* @param _token The address of the token that comes out of the bridge
* @param _amount The amount of tokens received at this contract through the cross-chain bridge.
* @param _srcChainId The source chain ID where the transfer is originated from
* @param _message Arbitrary message bytes originated from and encoded by the source app contract
* @param _executor Address who called the MessageBus execution function
*/
function executeMessageWithTransfer(
address _sender,
address _token,
uint256 _amount,
uint64 _srcChainId,
bytes calldata _message,
address _executor
) external payable virtual override returns (ExecutionStatus) {}

/**
* @notice Only called by MessageBus if
* 1. executeMessageWithTransfer reverts, or
* 2. executeMessageWithTransfer returns ExecutionStatus.Fail
* The contract is guaranteed to have received the right amount of tokens before this function is called.
* @param _sender The address of the source app contract
* @param _token The address of the token that comes out of the bridge
* @param _amount The amount of tokens received at this contract through the cross-chain bridge.
* @param _srcChainId The source chain ID where the transfer is originated from
* @param _message Arbitrary message bytes originated from and encoded by the source app contract
* @param _executor Address who called the MessageBus execution function
*/
function executeMessageWithTransferFallback(
address _sender,
address _token,
uint256 _amount,
uint64 _srcChainId,
bytes calldata _message,
address _executor
) external payable virtual override returns (ExecutionStatus) {}

/**
* @notice Called by MessageBus to process refund of the original transfer from this contract.
* The contract is guaranteed to have received the refund before this function is called.
* @param _token The token address of the original transfer
* @param _amount The amount of the original transfer
* @param _message The same message associated with the original transfer
* @param _executor Address who called the MessageBus execution function
*/
function executeMessageWithTransferRefund(
address _token,
uint256 _amount,
bytes calldata _message,
address _executor
) external payable virtual override returns (ExecutionStatus) {}
}
74 changes: 74 additions & 0 deletions ethereum/contracts/Interfaces/ICelerBridge.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: GPL-3.0-only

pragma solidity 0.8.13;

interface ICelerBridge {
// liquidity events
event Send(
bytes32 transferId,
address sender,
address receiver,
address token,
uint256 amount,
uint64 dstChainId,
uint64 nonce,
uint32 maxSlippage
);

function send(
address _receiver,
address _token,
uint256 _amount,
uint64 _dstChainId,
uint64 _nonce,
uint32 _maxSlippage
) external;

function sendNative(
address _receiver,
uint256 _amount,
uint64 _dstChainId,
uint64 _nonce,
uint32 _maxSlippage
) external payable;

function relay(
bytes calldata _relayRequest,
bytes[] calldata _sigs,
address[] calldata _signers,
uint256[] calldata _powers
) external;

function transfers(bytes32 transferId) external view returns (bool);

function withdraws(bytes32 withdrawId) external view returns (bool);

function minSend(address _token) external view returns (uint256);

function maxSend(address _token) external view returns (uint256);

function minimalMaxSlippage() external view returns (uint32);

function nativeWrap() external view returns (address);

function withdraw(
bytes calldata _wdmsg,
bytes[] calldata _sigs,
address[] calldata _signers,
uint256[] calldata _powers
) external;

/**
* @notice Verifies that a message is signed by a quorum among the signers.
* @param _msg signed message
* @param _sigs list of signatures sorted by signer addresses in ascending order
* @param _signers sorted list of current signers
* @param _powers powers of current signers
*/
function verifySigs(
bytes memory _msg,
bytes[] calldata _sigs,
address[] calldata _signers,
uint256[] calldata _powers
) external view;
}
133 changes: 133 additions & 0 deletions ethereum/contracts/Interfaces/ICelerMessageBus.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// SPDX-License-Identifier: GPL-3.0-only

pragma solidity 0.8.13;

import "../Libraries/LibCelerMsgDataTypes.sol";

interface ICelerMessageBus {
/**
* @notice Send a message to a contract on another chain.
* Sender needs to make sure the uniqueness of the message Id, which is computed as
* hash(type.MessageOnly, sender, receiver, srcChainId, srcTxHash, dstChainId, message).
* If messages with the same Id are sent, only one of them will succeed at dst chain..
* A fee is charged in the native gas token.
* @param _receiver The address of the destination app contract.
* @param _dstChainId The destination chain ID.
* @param _message Arbitrary message bytes to be decoded by the destination app contract.
*/
function sendMessage(
address _receiver,
uint256 _dstChainId,
bytes calldata _message
) external payable;

// same as above, except that receiver is an non-evm chain address,
function sendMessage(
bytes calldata _receiver,
uint256 _dstChainId,
bytes calldata _message
) external payable;

/**
* @notice Send a message associated with a token transfer to a contract on another chain.
* If messages with the same srcTransferId are sent, only one of them will succeed at dst chain..
* A fee is charged in the native token.
* @param _receiver The address of the destination app contract.
* @param _dstChainId The destination chain ID.
* @param _srcBridge The bridge contract to send the transfer with.
* @param _srcTransferId The transfer ID.
* @param _dstChainId The destination chain ID.
* @param _message Arbitrary message bytes to be decoded by the destination app contract.
*/
function sendMessageWithTransfer(
address _receiver,
uint256 _dstChainId,
address _srcBridge,
bytes32 _srcTransferId,
bytes calldata _message
) external payable;

/**
* @notice Execute a message not associated with a transfer.
* @param _message Arbitrary message bytes originated from and encoded by the source app contract
* @param _sigs The list of signatures sorted by signing addresses in ascending order. A relay must be signed-off by
* +2/3 of the sigsVerifier's current signing power to be delivered.
* @param _signers The sorted list of signers.
* @param _powers The signing powers of the signers.
*/
function executeMessage(
bytes calldata _message,
LibCelerMsgDataTypes.RouteInfo calldata _route,
bytes[] calldata _sigs,
address[] calldata _signers,
uint256[] calldata _powers
) external payable;

/**
* @notice Execute a message with a successful transfer.
* @param _message Arbitrary message bytes originated from and encoded by the source app contract
* @param _transfer The transfer info.
* @param _sigs The list of signatures sorted by signing addresses in ascending order. A relay must be signed-off by
* +2/3 of the sigsVerifier's current signing power to be delivered.
* @param _signers The sorted list of signers.
* @param _powers The signing powers of the signers.
*/
function executeMessageWithTransfer(
bytes calldata _message,
LibCelerMsgDataTypes.TransferInfo calldata _transfer,
bytes[] calldata _sigs,
address[] calldata _signers,
uint256[] calldata _powers
) external payable;

/**
* @notice Execute a message with a refunded transfer.
* @param _message Arbitrary message bytes originated from and encoded by the source app contract
* @param _transfer The transfer info.
* @param _sigs The list of signatures sorted by signing addresses in ascending order. A relay must be signed-off by
* +2/3 of the sigsVerifier's current signing power to be delivered.
* @param _signers The sorted list of signers.
* @param _powers The signing powers of the signers.
*/
function executeMessageWithTransferRefund(
bytes calldata _message, // the same message associated with the original transfer
LibCelerMsgDataTypes.TransferInfo calldata _transfer,
bytes[] calldata _sigs,
address[] calldata _signers,
uint256[] calldata _powers
) external payable;

/**
* @notice Withdraws message fee in the form of native gas token.
* @param _account The address receiving the fee.
* @param _cumulativeFee The cumulative fee credited to the account. Tracked by SGN.
* @param _sigs The list of signatures sorted by signing addresses in ascending order. A withdrawal must be
* signed-off by +2/3 of the sigsVerifier's current signing power to be delivered.
* @param _signers The sorted list of signers.
* @param _powers The signing powers of the signers.
*/
function withdrawFee(
address _account,
uint256 _cumulativeFee,
bytes[] calldata _sigs,
address[] calldata _signers,
uint256[] calldata _powers
) external;

/**
* @notice Calculates the required fee for the message.
* @param _message Arbitrary message bytes to be decoded by the destination app contract.
@ @return The required fee.
*/
function calcFee(bytes calldata _message) external view returns (uint256);

function liquidityBridge() external view returns (address);

function pegBridge() external view returns (address);

function pegBridgeV2() external view returns (address);

function pegVault() external view returns (address);

function pegVaultV2() external view returns (address);
}
89 changes: 89 additions & 0 deletions ethereum/contracts/Interfaces/ICelerMessageReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-License-Identifier: GPL-3.0-only

pragma solidity 0.8.13;

interface ICelerMessageReceiver {
enum ExecutionStatus {
Fail, // execution failed, finalized
Success, // execution succeeded, finalized
Retry // execution rejected, can retry later
}

/**
* @notice Called by MessageBus to execute a message
* @param _sender The address of the source app contract
* @param _srcChainId The source chain ID where the transfer is originated from
* @param _message Arbitrary message bytes originated from and encoded by the source app contract
* @param _executor Address who called the MessageBus execution function
*/
function executeMessage(
address _sender,
uint64 _srcChainId,
bytes calldata _message,
address _executor
) external payable returns (ExecutionStatus);

// same as above, except that sender is an non-evm chain address,
// otherwise same as above.
function executeMessage(
bytes calldata _sender,
uint64 _srcChainId,
bytes calldata _message,
address _executor
) external payable returns (ExecutionStatus);

/**
* @notice Called by MessageBus to execute a message with an associated token transfer.
* The contract is guaranteed to have received the right amount of tokens before this function is called.
* @param _sender The address of the source app contract
* @param _token The address of the token that comes out of the bridge
* @param _amount The amount of tokens received at this contract through the cross-chain bridge.
* @param _srcChainId The source chain ID where the transfer is originated from
* @param _message Arbitrary message bytes originated from and encoded by the source app contract
* @param _executor Address who called the MessageBus execution function
*/
function executeMessageWithTransfer(
address _sender,
address _token,
uint256 _amount,
uint64 _srcChainId,
bytes calldata _message,
address _executor
) external payable returns (ExecutionStatus);

/**
* @notice Only called by MessageBus if
* 1. executeMessageWithTransfer reverts, or
* 2. executeMessageWithTransfer returns ExecutionStatus.Fail
* The contract is guaranteed to have received the right amount of tokens before this function is called.
* @param _sender The address of the source app contract
* @param _token The address of the token that comes out of the bridge
* @param _amount The amount of tokens received at this contract through the cross-chain bridge.
* @param _srcChainId The source chain ID where the transfer is originated from
* @param _message Arbitrary message bytes originated from and encoded by the source app contract
* @param _executor Address who called the MessageBus execution function
*/
function executeMessageWithTransferFallback(
address _sender,
address _token,
uint256 _amount,
uint64 _srcChainId,
bytes calldata _message,
address _executor
) external payable returns (ExecutionStatus);

/**
* @notice Called by MessageBus to process refund of the original transfer from this contract.
* The contract is guaranteed to have received the refund before this function is called.
* @param _token The token address of the original transfer
* @param _amount The amount of the original transfer
* @param _message The same message associated with the original transfer
* @param _executor Address who called the MessageBus execution function
*/
function executeMessageWithTransferRefund(
address _token,
uint256 _amount,
bytes calldata _message,
address _executor
) external payable returns (ExecutionStatus);
}
13 changes: 13 additions & 0 deletions ethereum/contracts/Interfaces/ILibPriceV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: BUSL-1.1

pragma solidity 0.8.13;

// Celer chain id is u64, stargate and wormhole are u16
// ILibPriceV2 for Celer bridge
interface ILibPriceV2 {
function getPriceRatio(uint64 _chainId) external view returns (uint256, bool);

function updatePriceRatio(uint64 _chainId) external returns (uint256);

function RAY() external view returns (uint256);
}
Loading

0 comments on commit b62c7a5

Please sign in to comment.