Skip to main content
Expand answer
Source Link
Ismael
  • 30.3k
  • 23
  • 54
  • 97

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.

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.

You can try using nested mappings

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;
   }
}

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.

Source Link
Ismael
  • 30.3k
  • 23
  • 54
  • 97

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.

You can try using nested mappings

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;
   }
}