4

I'm still pretty new to ethereum but I have a decent understanding of how it works and I've written a couple different contracts.

What I am trying to figure out is if it is possible to have n different coin types in a single contract.

So for example if I took the example coin code from the go-ethereum github

contract token { 
    mapping (address => uint) public coinBalanceOf;
    event CoinTransfer(address sender, address receiver, uint amount);

    /* Initializes contract with initial supply tokens to the creator of the contract */
   function token(uint supply) {
     coinBalanceOf[msg.sender] = supply;
   }

   /* Very simple trade function */
   function sendCoin(address receiver, uint amount) returns(bool sufficient) {
     if (coinBalanceOf[msg.sender] < amount) return false;
     coinBalanceOf[msg.sender] -= amount;
     coinBalanceOf[receiver] += amount;
     CoinTransfer(msg.sender, receiver, amount);
     return true;
   }
}

and instead of just having the one coin type I wanted to have three different coin types, coin1, coin2, and coin3, what would be the best way to implement this in my contract?

My initial thought was to have an array of all of the different coin types and then each element in the array would contain another array that contains the address and balance information but I'm not exactly sure how I would write that.

  • Of course there is many ways to do what you want. I will propose you two simple ways: 1.- Have an array of structa saving the coinname and addresses. 2.- Have contract with an array of contract addresses and interact with theme from the index contract. In the solution one you will have all the tokens in one contract and in the solution two you will have one contract per token and the index contract. – Augusto Federico Lemble Jun 6 '16 at 19:47
  • I tried to write some code for you but I'm also new to Solidity so I will post it when it will be good enough. Be patient please. – Nicolas Massart Jun 7 '16 at 0:27
9

The simplest way should be to deploy the contract three times. Each instance of the contract refer to a separate coin. But in that situation is not simple to interact with different coin types.

If you want to be able to handle more than coin type in a single contract it can be done in several ways.

You can try using nested mappings. Here coinBalanceOf[k] will refer to the balances of the coin type k. And coinBalanceOf[k][address] will refer to the balance of address in the coin type k.

contract token { 
    mapping (uint => mapping (address => uint)) coinBalanceOf;
    event CoinTransfer(uint coinType, address sender, address receiver, uint amount);

    /* Initializes contract with initial supply tokens to the creator of the contract */
   function token(uint numCoinTypes, uint supply) {
     for (uint k=0; k<numCoinTypes; ++k) {
       coinBalanceOf[k][msg.sender] = supply;
     }
   }

   /* Very simple trade function */
   function sendCoin(uint coinType, address receiver, uint amount) returns(bool sufficient) {
     if (coinBalanceOf[coinType][msg.sender] < amount) return false;
     coinBalanceOf[coinType][msg.sender] -= amount;
     coinBalanceOf[coinType][receiver] += amount;
     CoinTransfer(coinType, msg.sender, receiver, amount);
     return true;
   }
}

If you have a fixed numberof coin types, instead of a nested mapping you can have a mapping to an array of balances for each key.

mapping (address => uint[3]) coinBalanceOf;

Now coinBalanceOf[address] are the balances of address, and coinBalanceOf[address] is balance of address in the coin type k.

  • Thank you for the detailed response! I think nested mappings is the way I want to go but I do have a question about launching the contract multiple times. If I launch the contract once for each coin type that I want is it possible to check the balance of the other coin types in one of the contracts? For example would I be able to check to see how many of the Coin3 coin type an address has in the Coin1 contract? – Alex Beebe Jun 9 '16 at 22:59
  • would this contract still be considered ERC20 if you have multiple coin types? – thefett Oct 11 '17 at 0:41
  • @AlexBeebe Sorry, I didn't see your questions until now. A contract can only access the public members of other contracts, if you define a public interface with the information you need you should be able to share information between contracts. – Ismael Oct 11 '17 at 3:27
  • @thefett The ERC20 standard define functionality you have to implement, ie transfer, approve, transferFrom, etc. If you define those methods then you are ERC20 compliant. A limitation/feature of ERC20 is it assumes a single token, none of the methods allow to select a token for a transaction. In my opinion it is not a good idea to manage multiple tokens through a single contract if you want to be ERC20. Lots of people (and contracts) expect a single token, and will not work if the same contract manages two tokens. – Ismael Oct 11 '17 at 3:38

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.