iotexproject / web-iotube

The front-end of ioTube V4
https://tube-v4.iotex.io
2 stars 1 forks source link

Support Ether deposit #153

Open CoderZhi opened 4 years ago

CoderZhi commented 4 years ago
contract EthCashier is Ownable {
    TokenCashier public tokenCashier;
    WrappedEther public wrappedEther;

    constructor(address _tokenCashier, address _wrappedEther) public {
        tokenCashier = TokenCashier(_tokenCashier);
        wrappedEther = WrappedEther(_wrappedEther);
    }

    function deposit() public payable {
        depositTo(msg.sender);
    }

    function depositTo(address _to) public payable {
        wrappedEther.deposit.value(msg.value)();
        require(wrappedEther.approve(address(tokenCashier), msg.value), "approve failure");
        tokenCashier.depositTo(address(wrappedEther), _to, msg.value);
    }

    function upgrade(address _newTokenCashier) public onlyOwner {
        tokenCashier = TokenCashier(_newTokenCashier);
    }
}

This contract is a wrap of token cashier and the weth. It enables user to deposit Ether via tube (token cashier) to IoTeX, without an extra manual step to exchange to WEth. As implemented in function depositTo, the deposited ether will be exchanged to WEth in the function first, and then deposit to token cashier.

The front-end side, we need to distinguish ether from the other tokens. If user choosing ether, we should call this contract EthCashier; otherwise, we call the TokenCashier.

Test contract on Kovan: 0xe958ed5d5d29e0910cf53b6d161d9295a93c2ab7

ABI:

[
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "_tokenCashier",
                "type": "address"
            },
            {
                "internalType": "address",
                "name": "_wrappedEther",
                "type": "address"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    },
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": true,
                "internalType": "address",
                "name": "previousOwner",
                "type": "address"
            },
            {
                "indexed": true,
                "internalType": "address",
                "name": "newOwner",
                "type": "address"
            }
        ],
        "name": "OwnershipTransferred",
        "type": "event"
    },
    {
        "constant": false,
        "inputs": [],
        "name": "deposit",
        "outputs": [],
        "payable": true,
        "stateMutability": "payable",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "internalType": "address",
                "name": "_to",
                "type": "address"
            }
        ],
        "name": "depositTo",
        "outputs": [],
        "payable": true,
        "stateMutability": "payable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "owner",
        "outputs": [
            {
                "internalType": "address",
                "name": "",
                "type": "address"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "tokenCashier",
        "outputs": [
            {
                "internalType": "contract TokenCashier",
                "name": "",
                "type": "address"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "internalType": "address",
                "name": "newOwner",
                "type": "address"
            }
        ],
        "name": "transferOwnership",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "internalType": "address",
                "name": "_newTokenCashier",
                "type": "address"
            }
        ],
        "name": "upgrade",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "wrappedEther",
        "outputs": [
            {
                "internalType": "contract WrappedEther",
                "name": "",
                "type": "address"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
]
guo commented 4 years ago

gas limit: 204,449 -> can set as 220,000.

remember if people wants to send all ETH over, we need to minus the gas required as the MAX.

guo commented 4 years ago

production mainnet contract: 0xfbe9a4138afdf1fa639a8c2818a0c4513fc4ce4b