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

Trustless bridge (EVM->native): upgradable solidity contract #61

Open
taokayan opened this issue Jan 9, 2025 · 0 comments
Open

Trustless bridge (EVM->native): upgradable solidity contract #61

taokayan opened this issue Jan 9, 2025 · 0 comments

Comments

@taokayan
Copy link
Contributor

taokayan commented Jan 9, 2025

Trustless bridge (EVM->native) developments

ref design: https://hackmd.io/@EOSNF/HkEBEJ-IJe

example code (without upgradability):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IERC20 {
    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);
    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);
    function totalSupply() external view returns (uint);
    function balanceOf(address owner) external view returns (uint);
    function allowance(address owner, address spender) external view returns (uint);
    function approve(address spender, uint value) external returns (bool);
    function transfer(address to, uint value) external returns (bool);
    function transferFrom(address from, address to, uint value) external returns (bool);
}

contract NativeBridge {
    address owner;
    bool suspended = false;
    uint8  evm_precision;
    uint8  host_precision;
    uint256 mult_factor;
    uint256 egressFee;
    address evm_address; 
    IERC20 token;

    constructor(address _evm_address, address _token_address, uint8 _host_precision, uint256 _egressFee) {
        owner = msg.sender;
        evm_address = _evm_address;
        token = IERC20( _token_address);
        evm_precision = token.decimals();
        host_precision = _host_precision;
        egressFee = _egressFee;
        require(evm_precision >= host_precision, "invalid host_precision");
        mult_factor = 10**(evm_precision - host_precision);
    }

    function transfer(address to, uint256 host_amount) external returns (bool) {
        require(msg.sender == owner, "can only called from owner");
        return token.transfer(to, host_amount * mult_factor);
    }

    function _isReservedAddress(address addr) internal pure returns (bool) {
        return ((uint160(addr) & uint160(0xFffFfFffffFfFFffffFFFffF0000000000000000)) == uint160(0xBBbbBbBbbBbbBbbbBbbbBBbb0000000000000000));
    }

    function bridgeTransfer(address to, uint256 amount, string memory memo) public payable returns (bool) {
        require(suspended == false, "bridge suspended");
        require(msg.value == egressFee, "incorrect egress bridge fee");
        require(_isReservedAddress(to), "to address must be reserved address");
        require(amount >= mult_factor, "amount too small");

        uint256 host_amount = amount / mult_factor;
        uint256 bill_amount = host_amount * mult_factor;
        if (!token.transferFrom(msg.sender, address(this), bill_amount)) { 
            revert(); 
        }

        // Call bridgeMessage of EVM Runtime
        // sha("bridgeTransferV0(address,uint256,string)") = 0x653332e5
        bytes memory receiver_msg = abi.encodeWithSignature("bridgeTransferV0(address,uint256,string)", to, host_amount, memo);
        (bool success, ) = evm_address.call{value: msg.value}(abi.encodeWithSignature("bridgeMsgV0(string,bool,bytes)", owner, true, receiver_msg ));
        if(!success) { revert(); }
        return success;
    }

    function setFee(uint256 _egressFee) public {
        require(msg.sender == owner, "only owner can set fee");
        egressFee = _egressFee;
    }

    function setSuspended(bool _suspended) public {
        require(msg.sender == owner, "only owner can set suspended");
        suspended = _suspended;
    }    
}
@github-project-automation github-project-automation bot moved this to Todo in EOS EVM Jan 9, 2025
@taokayan taokayan changed the title Trustless bridge (EVM->native): solidity contract development Trustless bridge (EVM->native) Jan 9, 2025
@taokayan taokayan changed the title Trustless bridge (EVM->native) Trustless bridge (EVM->native): upgradable sodity contract Jan 9, 2025
@taokayan taokayan changed the title Trustless bridge (EVM->native): upgradable sodity contract Trustless bridge (EVM->native): upgradable solidity contract Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

2 participants