0

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.

1

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.

  • I suppose this would set the same exchange rate for all tokens. How about different rates for each token that can be updated? Also, how do I set an external address to receive fees? Thanks – Zetal Apr 30 '18 at 20:28
  • Then probably you can have a mapping of rates for different contracts supported. When you add a new token , you need to add a rate as well. – Prashant Prabhakar Singh May 1 '18 at 5:40
  • you should divide buy price. If buy price = 2, you will receive tokens/2 amount – Vladimir Kovalchuk Dec 4 '18 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.