0
// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20("MyToken", "MY") {
    /**
     * @dev Constructor.
     * @param totalSupply total supply of tokens in lowest units (depending on decimals)
     * @param tokenOwnerAddress address that gets 100% of token supply
     */
    constructor(uint256 totalSupply, address tokenOwnerAddress) public payable {

      // set tokenOwnerAddress as owner of all tokens
      _mint(tokenOwnerAddress, totalSupply);
    }

    /**
     * @dev Burns a specific amount of tokens.
     * @param value The amount of lowest token units to be burned.
     */
    function burn(uint256 value) public {
      _burn(msg.sender, value);
    }
}

From a hardhat console I'm running npx hardhat console --network hardhat and then

> const token = await (await ethers.getContractFactory("MyToken")).attach("0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82")

> token.functions
{
  'allowance(address,address)': [Function (anonymous)],
  'approve(address,uint256)': [Function (anonymous)],
  'balanceOf(address)': [Function (anonymous)],
  'burn(uint256)': [Function (anonymous)],
  'decimals()': [Function (anonymous)],
  'decreaseAllowance(address,uint256)': [Function (anonymous)],
  'increaseAllowance(address,uint256)': [Function (anonymous)],
  'name()': [Function (anonymous)],
  'symbol()': [Function (anonymous)],
  'totalSupply()': [Function (anonymous)],
  'transfer(address,uint256)': [Function (anonymous)],
  'transferFrom(address,address,uint256)': [Function (anonymous)],
  allowance: [Function (anonymous)],
  approve: [Function (anonymous)],
  balanceOf: [Function (anonymous)],
  burn: [Function (anonymous)],
  decimals: [Function (anonymous)],
  decreaseAllowance: [Function (anonymous)],
  increaseAllowance: [Function (anonymous)],
  name: [Function (anonymous)],
  symbol: [Function (anonymous)],
  totalSupply: [Function (anonymous)],
  transfer: [Function (anonymous)],
  transferFrom: [Function (anonymous)]
}
> token.totalSupply().catch(console.error)
Promise { <pending> }
> Error: call revert exception (method="totalSupply()", errorSignature=null, errorArgs=[null], reason=null, code=CALL_EXCEPTION, version=abi/5.0.12) 
...{
  reason: null,
  code: 'CALL_EXCEPTION',
  method: 'totalSupply()',
  errorSignature: null,
  errorArgs: [ null ],
  address: '0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82',
  args: [],
  transaction: {
    data: '0x18160ddd',
    to: '0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82',
    from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
    gasLimit: BigNumber { _hex: '0x820638', _isBigNumber: true }
  }
}
> token.name().catch(console.error)
Promise { <pending> }
> Error: call revert exception (method="name()", errorSignature=null, errorArgs=[null], reason=null, code=CALL_EXCEPTION, version=abi/5.0.12) 
...{
  reason: null,
  code: 'CALL_EXCEPTION',
  method: 'name()',
  errorSignature: null,
  errorArgs: [ null ],
  address: '0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82',
  args: [],
  transaction: {
    data: '0x06fdde03',
    to: '0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82',
    from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
    gasLimit: BigNumber { _hex: '0x820638', _isBigNumber: true }
  }
}
> token.burn(100).then(console.log)
Promise { <pending> }
> {
  hash: '0xabb5c37b542275d0e725aed816edec6903124de4d86df8d3ba85a7293c4189c9',
  blockHash: '0xf72d083f070c0c35b2fff8d648ae3684ad86d790df78609e341664e7a3534b32',
  blockNumber: 2,
  transactionIndex: 0,
  confirmations: 1,
  from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
  gasPrice: BigNumber { _hex: '0x01dcd65000', _isBigNumber: true },
  gasLimit: BigNumber { _hex: '0x8206f8', _isBigNumber: true },
  to: '0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82',
  value: BigNumber { _hex: '0x00', _isBigNumber: true },
  nonce: 1,
  data: '0x42966c680000000000000000000000000000000000000000000000000000000000000064',
  r: '0x1d05a3929b1eee0ea67437f8d657f798dbacef32510e4c8e4ac67f2064cb18bd',
  s: '0x6b557914aa291dbec9d690a4fd38674c3f9232f5fd3e5592e9124339a184b30c',
  v: 62709,
  creates: null,
  chainId: 31337,
  wait: [Function (anonymous)]
}

The only function that works is burn(uint256 value). Is the inheritance of my contract screwed up somehow?

1

The issue seems to have been related to the hardhat-deploy plugin: https://github.com/wighawag/hardhat-deploy/issues/95#issuecomment-814623214

The solution was to specify the network as localhost when attaching the console:

npx hardhat console --network localhost

I still don't fully understand the difference between the hardhat network and and localhost, since it is in theory being deployed to 'hardhat' when running npx hardhat node, but requires that I specify 'localhost' to operate on it.

I assume the inherited contracts were only deployed to localhost and not hardhat then?

> const token = await (await ethers.getContractFactory("MyToken")).attach("0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82")
undefined
> console.log(await token.name())
MyToken
0

You need to implement all functions of ERC20 in MyToken. This implementation doesn't consume gas. That means, the inherited functions, that are in MyToken's state, consumes gas G and when you implement them in body of MyToken it consumes gas G too. If you don't implement the mentioned functions, the compiler can't find them. Compiler just looks up entire of contract's body not contract's state. Contract plays the role of a middleman between its owned state and a compiler. Whenever a state value or function is needed, contract provides that to the compiler to be processed. So, without function implementation compiler can't find the given function in the contract's body and the smart contract can't understand it should give out its state.

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.