Skip to content

Commit

Permalink
Refactor ChainXBridge by precompile contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
icodezjb committed Jun 16, 2023
1 parent ac842e6 commit daf6289
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 156 deletions.
38 changes: 19 additions & 19 deletions contracts/ChainXBridge.sol
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

import "./SystemWithdraw.sol";

interface IBitcoinAssets {
function burnFrom(address account, uint256 amount) external;
}

contract ChainXBridge {
uint constant MIN_BTC_TRANSFER_VALUE = 10_000_000_000;
address public cold;
uint64 public chainId;
uint64 public nonce;

event Balance(uint256);

event SwapOut(
uint64 fromChainId,
uint64 toChainId,
event Withdraw(
address sender,
string receiver,
uint256 amount,
Expand All @@ -28,31 +24,37 @@ contract ChainXBridge {
_;
}

constructor(
address _cold,
uint64 _chainId
){
constructor(address _cold){
require(_cold != address(0), "InvalidCold");
require(_chainId != 0, "InvalidChainId");

cold = _cold;
chainId = _chainId;
nonce = 0;
}

function withdrawBTC(
uint256 value,
string calldata btcAddr
) external autoIncreaseNonce returns (bool) {
return SystemWithdraw.withdrawBTC(value, btcAddr);
}

function withdrawPCX(
uint256 value,
bytes32 chainxPubkey
) external autoIncreaseNonce returns (bool) {
return SystemWithdraw.withdrawPCX(value, chainxPubkey);
}

function swap_out(
uint64 toChainId,
function withdraw(
string calldata receiver,
address token,
uint256 amount,
uint256 estGas
) external payable autoIncreaseNonce {
require((cold != address(0)), "InvalidCold");
// Less than MIN_BTC_TRANSFER_VALUE as 0
uint256 MIN_BTC_TRANSFER_VALUE = SystemWithdraw.MIN_BTC_TRANSFER_VALUE;
estGas = estGas / MIN_BTC_TRANSFER_VALUE * MIN_BTC_TRANSFER_VALUE;
emit Balance(estGas);
require(estGas >= MIN_BTC_TRANSFER_VALUE, "InvalidEstGas");
require(msg.value >= estGas, "ValueErr");

Expand All @@ -64,9 +66,7 @@ contract ChainXBridge {

IBitcoinAssets(token).burnFrom(msg.sender, amount);

emit SwapOut(
chainId,
toChainId,
emit Withdraw(
msg.sender,
receiver,
amount,
Expand Down
37 changes: 37 additions & 0 deletions contracts/SystemWithdraw.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

library SystemWithdraw {
address constant private precompile = address(0x403);
uint256 constant MIN_BTC_TRANSFER_VALUE = 10_000_000_000;

event WithdrawBTC(address from, uint256 amount, string to);
event WithdrawPCX(address from, uint256 amount, bytes32 to);

function withdrawBTC(
uint256 value,
string calldata btcAddr
) public returns (bool) {
(bool success, bytes memory returnData) = precompile.delegatecall(abi.encodePacked(false, value, btcAddr));

require(success, string(returnData));

emit WithdrawBTC(msg.sender, value / MIN_BTC_TRANSFER_VALUE, btcAddr);

return success;
}

function withdrawPCX(
uint256 value,
bytes32 chainxPubkey
) public returns (bool) {
(bool success, bytes memory returnData) = precompile.delegatecall(abi.encodePacked(true, value, chainxPubkey));

require(success, string(returnData));

emit WithdrawPCX(msg.sender, value, chainxPubkey);

return success;
}
}
2 changes: 2 additions & 0 deletions runtime/dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ use pallet_evm::{
use sp_core::{H160, U256};
use sp_runtime::traits::{Dispatchable, PostDispatchInfoOf};
mod precompiles;
mod withdraw;

pub use precompiles::ChainXPrecompiles;

/// This runtime version.
Expand Down
9 changes: 7 additions & 2 deletions runtime/dev/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
/// Return all addresses that contain precompiles. This can be used to populate dummy code
/// under the precompile.
pub fn used_addresses() -> sp_std::vec::Vec<H160> {
sp_std::vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026]
sp_std::vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 1027]
.into_iter()
.map(hash)
.collect()
Expand All @@ -38,7 +38,9 @@ where
/// 2048-4095 ChainX specific precompiles
impl<R> PrecompileSet for ChainXPrecompiles<R>
where
R: pallet_evm::Config,
R: xpallet_assets_bridge::Config
+ xpallet_gateway_common::Config
+ xpallet_gateway_records::Config,
Dispatch<R>: Precompile,
{
fn execute(
Expand Down Expand Up @@ -70,6 +72,9 @@ where
a if a == hash(1026) => Some(ECRecoverPublicKey::execute(
input, target_gas, context, is_static,
)),
a if a == hash(1027) => Some(crate::withdraw::Withdraw::<R>::execute(
input, target_gas, context, is_static,
)),
_ => None,
}
}
Expand Down
Loading

0 comments on commit daf6289

Please sign in to comment.