2

I am trying to understand the openzeppelin code for writing ICO contract.

In MintedCrowdsale, mint() function in ERC20Mintable is called as mentioned below.

ERC20Mintable(address(token())).mint(beneficiary, tokenAmount));

Could you please let me know how this is being called without inheriting it while defining in the contract MintedCrowdsale ?

Also Why address(token()) is used in ERC20Mintable(address(token())) ? I do not see any such type of constructor in ERC20Mintable. Could you please help me to understand this as well ?

MintedCrowdsale

contract MintedCrowdsale is Crowdsale {
constructor() internal {}

/**
* @dev Overrides delivery by minting tokens upon purchase.
* @param beneficiary Token purchaser
* @param tokenAmount Number of tokens to be minted
*/
function _deliverTokens(
address beneficiary,
uint256 tokenAmount
)
internal
{
// Potentially dangerous assumption about the type of the token.
require(
ERC20Mintable(address(token())).mint(beneficiary, tokenAmount));
}
}

ERC20Mintable

contract ERC20Mintable is ERC20, MinterRole {
/**
* @dev Function to mint tokens
* @param to The address that will receive the minted tokens.
* @param value The amount of tokens to mint.
* @return A boolean that indicates if the operation was successful.
*/
function mint(
address to,
uint256 value
)
public
onlyMinter
returns (bool)
{
_mint(to, value);
return true;
}
}

1 Answer 1

1

You can use the functions of another contract by two ways. One way is that you import the contract,inherit it. If it has a constructor then handle it and use its functions. In this case, you will also be able to overload function and can use the functionality of super.

Another way is that you copy the complete contract or add specific function which you want to use on the top of same file. And then creates instance of imported contract as :

ContractName c = ContractName(ContractAddress);

You see the similar example here : https://ethereum.org/token . See The Code section. Instance of tokenRecipientis getting created in approveAndCall function. This may also be helpful: Basic example of interaction between 2 contracts?

In MintedCrowdsale, this approach is being used.

ERC20Mintable(address(token())).mint(beneficiary, tokenAmount));

In this line, token() is a function defined in Crowdsale contract here. It returns the address of token. So, ERC20Mintable(address(token())) part creates an instance of ERC20Mintable contract and then mint of that contract is getting called.I hope this answers you.

1
  • 1
    Brilliant Answer !
    – iappmaker
    Nov 28, 2018 at 14:17

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.