1

I have been scratching my head in understanding the situations and scenarios when address datatype should be used. I have seen Voting contracts as below

contract Ballot {
    // This declares a new complex type which will
    // be used for variables later.
    // It will represent a single voter.
    struct Voter {
        uint weight; // weight is accumulated by delegation
        bool voted;  // if true, that person already voted
        address delegate; // person delegated to
        uint vote;   // index of the voted proposal
    }
}

Here why is address datatype used for person, couldn't we use string or bytes?

2

According to the documentation on the Solidity ABI:

address: equivalent to uint160, except for the assumed interpretation and language typing

The Solidity documentation carries on to say:

address: Holds a 20 byte value (size of an Ethereum address). Address types also have members and serve as a base for all contracts.

You should note firstly that the address type serves as a base for all contracts, in fact contracts inherit some members and functions from the address type. As such, when one initializes an address variable, it is possible to query the account at said address in the following ways, as stated in the documentation (here & here).

<address>.balance (uint256): balance of the Address in Wei
<address>.transfer(uint256 amount): send given amount of Wei to Address, throws on failure
<address>.send(uint256 amount) returns (bool): send given amount of Wei to Address, returns false on failure
<address>.call(...) returns (bool): issue low-level CALL, returns false on failure
<address>.callcode(...) returns (bool): issue low-level CALLCODE, returns false on failure
<address>.delegatecall(...) returns (bool): issue low-level DELEGATECALL, returns false on failure

The advantage of using an address variable instead of a uint160 is therefore the advantage it gives when querying or interacting with accounts (externally owned or contract accounts).

Using the address type also serves to improve readability, in that it tells the reader of the contract that the value stored relates to a contract address.

NOTE: As of version 5.0.0 of Solidity, members of the address type will no longer be available to contract types - you will need to explicitly cast to the address type first in order to use them.

1

I would suggest to have a look at the documentation of Solidity:

According to provided links, you can see that using address datatype provides you most important functionality of the address that you should have.

0

Every account and smart contract on the Ethereum blockchain has an address and can send and receive Ether to and from this address. Usually, to store this address and address related details, address datatype is used.

address: Holds a 20 byte value which is size of an Ethereum address. Address types also have members and serve as a base for all contracts. See following example

address x = 0x345ca3e014aaf5dca488057592ee47305d9b3e10;
address myAddress = this;
if (x.balance < 100 && myAddress.balance > 100) x.transfer(100);

Following code snippet demonstrate usage of address type

contract Voting {
address[100] public voters;

// Voting
function vote(uint voterId) public view returns (uint) {
  require(alreadyVoted(voteId));

  voters[voterId] = msg.sender;

  return voterId;
}

// Retrieving the voters
function getVoters() public returns (address[16]) {
  return voters;
}
}

Things to notice:

  • We've defined a single variable: voters. Its an array of Ethereum addresses. In ethereum, Arrays contain one type and can have a fixed or variable length. In this case the type is address and the length is 100.

@vote function,

  • In Solidity the types of both the function parameters and output must be specified. In this case we'll be taking in a voterId (integer) and returning an integer.

  • We use the require() statement to ensure this voter is valid.

  • If the voterId is valid, we then add the address that made the call to our voters array. The address of the person or smart contract who called this function is denoted by msg.sender

  • At the end of the function, we return the voterId provided as a confirmation.

@ getVoters

  • Since voters is already declared, we can simply return it. Be sure to specify the return type (in this case, the type for voters) as address[100].
  • In the ballot contract example I see contract Ballot { // This declares a new complex type which will // be used for variables later. // It will represent a single voter. struct Voter { uint weight; // weight is accumulated by delegation bool voted; // if true, that person already voted address delegate; // person delegated to uint vote; // index of the voted proposal } why address type is used for the person, couldn't use string or bytes? – Ajay Dec 21 '17 at 13:05
  • why address type is used for the person: bcuz we are storing person address, not name. the voting example i used here just to explain how address type can be used, not verified against evm. my emphasis on address type, not as a whole contract. Pls relate to question and if it still doesn't make sense. let me know why we can't use address // address[100] public voters and // voters[voterId] = msg.sender; Appreciate valuable feedback. @Ajay – f-society Dec 21 '17 at 15:27
  • My confusion is when to use address data type and when not? In your example could we have also used bytes32[] for storing names instead of address and it would have still solved the purpose? – Ajay Dec 22 '17 at 6:50

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.