5

I understand that the following function is used to link the ico contract to the token. Appreciate if someone could explain how to use the function, and exactly how it works. Thank you.

/**
 * @title Token
 * @dev API interface for interacting with the Token contract 
 */
interface Token {
  function transfer(address _to, uint256 _value) returns (bool);
  function balanceOf(address _owner) constant returns (uint256 balance);
}

3 Answers 3

10

It's an "Abstract" a.k.a. "Interface" contract.

It's used to define the interface, meaning the functions that exist, their exact names and inputs/outputs while being silent about how they work internally.

The idea is to use inheritance to create a compliant implementation - a contract with the exact same functions implemented.

You go about it like this.

contract Interface {
  // undefined functions with names and arguments
}

contract Implementation is Interface {
  // now you are forced to have matching functions with definition
}
```

You will not be able to deploy Implementation unless it implements each function defined in Interface.

Hope it helps.

3
  • Thank you. That's really helpful. I've done some further research and it clearer. One thing i'm still confused about is the Ed Zygga video (link below - 4:20 onwards) he includes the following lines of text in the contract: Jan 1, 2018 at 18:27
  • I've seen this somewhere...how does this tie into the explanation above?The instantiation of IEnergy is a bit strange - contract IEnergy { function transfer(address _to, uint256 _amount) external; } contract EnergyImpl is SomeOtherContract { IEnergy constant Energy = IEnergy(uint160(bytes6("Energy")));
    – sat
    Dec 26, 2019 at 21:06
  • Where can we see that repo? Dec 26, 2019 at 22:54
2

This is an interface for interacting with the Token Contract. If you read the solidity doc, you will notice the characteristics for the interface.

Let me write about the properties of an interface again:

1) Interfaces cannot have any functions implemented

2) Interfaces cannot inherit other contracts or interfaces

3) Interfaces cannot define a constructor

4) Interfaces cannot define variables

5) Interfaces cannot define structs

6) Interfaces cannot define enums.

7) Interfaces are expressed using the interface keyword.

Now if we analyze the code :

 interface Token {
   function transfer(address _to, uint256 _value) returns (bool);
   function balanceOf(address _owner) constant returns (uint256 balance);
}

Token interface has two functions. Both of these functions are not implemented here. But anyone can implement these functions later.That's the fun part.

0

The balance of the users is stored in a mapping called balances;

mapping(address => uint) balances;

When you call the transfer function with the receiver's address and the amount to be transferred, you reduce your balance by that amount and increase the receiver's amount. Like this:

balances[msg.sender] -= _value;
balances[_to] += _value;

BalanceOf function simply returns your balance.

return balances[msg.sender]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.