TransWikia.com

Solidity: How to do 2D mapping?/Syntax Error

Ethereum Asked on February 14, 2021

I am getting a syntax error. I am not sure if its a problem related to 2D mapping or 2D array. My code is given below:

contract Stealing{
   
   mapping (address => uint) balances;
   mapping (address => bool) frozenAccount;
   mapping (address => uint) allowed;
   function transferFrom ( address from , address to , uint value )public returns ( bool success ) {
      // checking account is freeze or not
      if( frozenAccount [ msg. sender ]) return false ;
         // checking the from should have enough coins
      if( balances [ from ] < value ) return false ;
         // checking for allowance
      if( allowed [ from ][ msg. sender ] >= value ) return false ;
         // checking for overflows
      if( balances [to] + value < balances [to ]) return false ;
      balances [ from ] -= value ;
      allowed [ from ][ msg . sender ] -= value ;
      balances [to] += value ; // Notify anyone listening that this transfer took place
      transferFrom (from , to , value );
    return true ;
 }
}

The error message is:

prg9.sol:37:11: Error: Indexed expression has to be a type, mapping or
array (is uint256)
if( allowed [ from ][ msg. sender ] >= value ) return false ;

Somebody please guide me.
Zulfi.

One Answer

You haven't declared allowed as a 2D mapping, but you're trying to use it as one.

You need: mapping (address => mapping (address => uint256)) private allowed;

Correct answer by Richard Horrocks on February 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP