scp94373
9 months ago  Karma: 157
Mapping and Arrays

I wondered if anyone can explain to me what Mapping is in solidity and why I would use it.

The same for Arrays however I from what I understand of Arrays I'm more likely to use arrays more than mapping.

I just don't understand what mapping does or why it's needed.
I understand that mapping(Address => uint) Balances; is what is used for displaying balances.

My understanding of that mapping is, the input is address, the output is a uint (called Balances), but is that pre-determined as a global variable or do I have to define what "Balances" mean somewhere in the code?

en
#solidity
scp94373
9 months ago  Karma: 157
Mapping and Arrays

I wondered if anyone can explain to me what Mapping is in solidity and why I would use it.

The same for Arrays however I from what I understand of Arrays I'm more likely to use arrays more than mapping.

I just don't understand what mapping does or why it's needed.
I understand that mapping(Address => uint) Balances; is what is used for displaying balances.

My understanding of that mapping is, the input is address, the output is a uint (called Balances), but is that pre-determined as a global variable or do I have to define what "Balances" mean somewhere in the code?

en
#solidity

ONE ANSWER
kaijchang2
9 months ago Karma: 19864

You can define a mapping globally like this:

mapping(key => value) public name;

update it like this:

name[key] = value;

Mapping is basically a way to look up information. For example, with balances, it lets you look up the balance of an address.

For example, for the popular contract Cryptokitties, they use lots of mappings.

They use mappings to map the uint id of a cat to its owner, an address.
mapping (uint256 => address) public kittyIndexToOwner;

They also use mappings to map an address to how many cats they own, a uint.
mapping (address => uint256) ownershipTokenCount;

You can define a mapping globally like this:

mapping(key => value) public name;

update it like this:

name[key] = value;

Mapping is basically a way to look up information. For example, with balances, it lets you look up the balance of an address.

For example, for the popular contract Cryptokitties, they use lots of mappings.

They use mappings to map the uint id of a cat to its owner, an address.
mapping (uint256 => address) public kittyIndexToOwner;

They also use mappings to map an address to how many cats they own, a uint.
mapping (address => uint256) ownershipTokenCount;

scp94373

Gotcha, so I could use this for example:
Mapping(address => uint) allBalances;

allBalances[0xEthAddressOfAccount1] =
allBalances[0xEthAddressOfAccount2] =
allBalances[0xEthAddressOfAccount3] =
so on...?

Then, what I would like to do is use 'weight' to determine who has the most in their account, who's second...third etc. out of 10.

Is that possible? If so, is it then possible to use that logic for ERC20 tokens (Just one specific token).

Thanks,

Sam

Jul 20 '18 at 2:28 pm
kaijchang2

First part is correct, yes.

Do you need to access the weight in your contract or can you do it off-chain?
To keep track the top addresses in Solidity, probably do something like:

mapping(uint=>address) position;
Then add a function called whenever coins change hands on the receiver:

function updatePosition(address receiver) {
for(uint i=0; i<=10; i++) {
if (balances[position[i]] < balances[receiver]) {
position[i] = receiver;
}
}
}

I don't really know if it works, but the idea should be fine. However, it's usually not advisable to add unaudited components to templates like ERC20.

Jul 20 '18 at 6:05 pm
Earn tokens by posting and answering questions about blockchain!
Karma to eth
YOUR ANSWER