2

Hi for a while now i have been testing ethereum capabilities and there is something i wanted to test and i cant figure how to do. When creating a contract of a custom token (using the code given in the ethereum official site) there is a sell price to set and a buy price for a token, in what way can i use this to give a token sell price of another token?

What I mean is, is there a way to give token A price in token B? essentialy making it possible to a user who have Token B to buy token A? and could a contract hold more than 1 kind of a token? like a contract that holds both token A and B? and maybe even more?

2

The Token contract has a function that allows the token owner to approve an address for a certain amount. This other address will be able to send up to this amount of the approver's tokens:

function approve(address _spender, uint256 _value) {}

So let's say you have 2 people Alice and Bob. Alice has 3 TokenA and Bob has 4 TokenB. They agree to exchange them. You create the re-usable Trade contract to make that happen.

contract Trade {
    function exchange(address alice, 
                    address tokenA,
                    uint qtyTokenA,
                    address bob,
                    address tokenB,
                    uint qtyTokenB) 
        returns (bool success) {
        success = Token(tokenA).transferFrom(alice, bob, qtyTokenA)
            && Token(tokenB).transferFrom(bob, alice, qtyTokenB);
        if (!success) throw;
    }
}

In TokenA, Alice approves the trade contract for 3. In TokenB, Bob approves the trade contract for 4. Then someone calls the exchange function with the proper values.

I understand that, as it stands, there is a security risk whereby Carol calls the method before others, to steal from Alice. IRL, you would have Alice and Bob to both approve the exchange function call.

  • so in order to do that i must have another contract in place? it cannot be done in a token contract right? like have 10 tokens and 1 main token which all of the others have a price according to? (is this example possible ?alice has token A bob has token B and my system runs token P ---> token A and B both have exchange function for token P in them and that exchange ratio would be defined by me, now when they trade they can get the P tokens and exchange then replace them with A or B? i know its a bit hard to understand me here ^^) thanks btw! – Omry Rozenfeld Sep 7 '16 at 11:16
  • It is better to keep TokenA and TokenB "simple", and work with other systems. If you introduce TokenP that is the "currency of exchange", then you need to modify function exchange to take more parameters. I suppose tokenP and alicePay and bobPay. Or some other combination. – Xavier Leprêtre B9lab Sep 7 '16 at 11:23
  • ye but given I might need someday to exchange between many different tokens wouldnt it be smarter to have them all point to P somehow? – Omry Rozenfeld Sep 7 '16 at 11:33
  • You can indeed include this function within your TokenP. – Xavier Leprêtre B9lab Sep 7 '16 at 11:41

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.