[Ethereum] Smart contract to buy/sell ETH/ERCToken for backed token and fee on each transaction

contract-developmentfeestokens

I did quite a lot of research to find something like this with no success.
I would like to create a smart contract that allows any token to be backed by ETH. The user can buy the token directly by sending ETH and a 1% fee goes to a third address. Anytime, he can send back the token and receive the ETH, again the fee is involved in this transaction too.
It should be possible to modify the list of tokens accepted.
The exchange rate ETH/Token for each accepted token can be modified too.

Best Answer

What you probably need is to have a buy and sell functions. A sample could be like:

pragma solidity ^0.4.18;
contract ERC20 {
    function totalSupply() public constant returns (uint);
    function balanceOf(address tokenOwner) public constant returns (uint balance);
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);

    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}

contract ButAndSell{

    uint public buyPrice = 1;

    function buy(address _tokenAddress) public payable{
        ERC20 token = ERC20(_tokenAddress);
        uint tokens = msg.value * buyPrice;
        require(token.balanceOf(this) >= tokens);
        uint commission = msg.value/100; // 1% of wei tx
       require(address(this).send(commission));
        token.transfer(msg.sender, tokens);
    }
}

Similarly, you can use sell function.

To support multiple tokens you just need to have token balance in your contract. That's all.

Related Topic