0

Dear blockchain developers, I have a ERC20 with restriction of minting only on Polygon chain:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

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

contract MyPolygonToken is ERC20 {
    uint256 public constant MINT_CHAIN_ID = 137;
    error ZeroAddress();

    constructor(
        string memory _name,
        string memory _symbol,
    ) ERC20(_name, _symbol) {
        if (getChainId() == MINT_CHAIN_ID) {
          _mint(msg.sender, 1_000_000 ether);
        }
    }

    function getChainId() public view returns (uint256) {
        uint256 chainId;
        assembly {
            chainId := chainid()
        }
        return chainId;
    }
}

I want to create unit test to test the correct execution, but have problem. I tried to create second local network with id "137" by fork Polygon network, but I can't get the signers of the chain. Do you have any idea how I can easily do it, or maybe some extension for hardhat which can help me?

Thanks in advance.

1 Answer 1

0

You do not hardcode MINT_CHAIN_ID but pass it as a constructor parameter.

6
  • Thanks for response, but it doesn't work for me, because I must be sure that the token will mint only on one chain. If we pass the chainID value in constructor arguments, then we can pass any chainId and the token will be minted on any chain. Or do you mean to create mock token for tests? Do I understand you correctly?
    – Arkenstone
    Commented Mar 20 at 11:50
  • f we pass the chainID value in constructor arguments, then we can pass any chainId and the token will be minted on any chain. You do not understand correctly. If the value is only settable in the constructor, there is no way to change the value after the deployment, making it not different for your use case. Commented Mar 20 at 11:52
  • If the value is only settable in the constructor, there is no way to change the value after the deployment yes, you are correct, but I need to check the mint on deployment. I need to restrict initial minting. As you can see I use function _mint in constructor
    – Arkenstone
    Commented Mar 20 at 14:23
  • As you can see I use function _mint in constructor does make any sense. When you deploy the contract, and it runs the constructor, you know the chain it is deploying. There is no sense in this check. You cannot deploy a contract and it suddenly runs the constructor on some other chain. Commented Mar 20 at 14:35
  • When you deploy the contract, and it runs the constructor, you know the chain it is deploying yes, you correct, but actually I want to create LayerZero OFT token, and the token is deployed on several chains.
    – Arkenstone
    Commented Mar 20 at 15:08

Your Answer

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

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