1

I'm trying to set up a simple token contract and deploy it but i'm having some trouble. This is the token contract

pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract Token {
    string public name;
    string public symbol;
    uint256 public decimals = 18;
    uint256 public totalSupply;

    mapping(address => uint256) public balanceOf;

    function transfer(address _to, uint256 _value) public returns (bool success) {
        balanceOf[msg.sender] = balanceOf[msg.sender] - _value;
        balanceOf[_to] = balanceOf[_to] + _value;
    }

    constructor(
        string memory _name, 
        string memory _symbol, 
        uint256 _totalSupply
        ) {
        name = _name;
        symbol = _symbol;
        totalSupply = _totalSupply * (10**decimals);
        balanceOf[msg.sender] = totalSupply;
    }
}

And this is the deployment script:


async function main() {
  //Fetch contract to deploy
  const Token = await ethers.getContractFactory("Token");

  //Deploy contract
  const token = await Token.deploy("Token", "MTK", 1000000);
  await token.deployed();
  console.log(`Token deployed to: ${token.address}`);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

The contract deploys okay and gives me this output:

Token deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3

However when i try to run the the following command i get an error:

const token = await ethers.getContractAt("Token", "0x5FbDB2315678afecb367f032d93F642f64180aa3")
Uncaught:
NomicLabsHardhatPluginError: 0x5FbDB2315678afecb367f032d93F642f64180aa3 is not a contract 
account.
    at REPL12:1:41
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at getContractAt (....\node_modules\@nomiclabs\hardhat-ethers\src\internal\helpers.ts:307:11)

I don't understand what i'm doing wrong.

3 Answers 3

0

Based on the contract and deployment script you provided, there doesn't seem to be any issues with the code itself.

It's possible that the network you are deploying to is not configured correctly. Please ensure that you are deploying to a valid and supported network, and that you have correctly specified the network in your deployment script.

Additionally, you can try checking the deployment transaction on the blockchain explorer to see if it was successful and to confirm that the contract was indeed deployed at the address that you received.

Hope this helps.

0

Contract looks good -- did you create one of your own or did you grab one from a library like OpenZeppelin?

It sounds like a deployment issue -- I'd recommend using either Remix or Hardhat if you're not already. Remix tends to be the most straightforward if it's your first time.

Since it's likely a network issue, I'd deploy by hooking up an external RPC endpoint to your Metamask first. You can do this by doing the following:


First, check that your contract code is actually fine by hitting "compile" in Remix.


Then, get ready to deploy:

  1. Log into Metamask
  2. Click "Add a network"
  3. Grab your external API key (e.g., sign up for a free provider like Alchemy and create and copy your API key)
  4. Paste in the API key and put in the chain ID (would recommend the Sepolia testnet which is "11155111" as it won't cost real money like if you deployed to Ethereum testnet)
  5. Add in test Sepolia Ether from a faucet like sepoliafaucet.com
  6. Select "Web3 Provider" as your deployer in Remix
  7. Hit "deploy" and wait for it to say successful
2
  • I created this on my own, and i'm already using npx hardhat node. I'll try to use Remix. Commented Mar 30, 2023 at 15:22
  • It worked on Remix, any particular reason why it didn't work on my local system? Something wrong with the deployment code? Commented Apr 1, 2023 at 12:33
0

The reason this doesn't work on hardhat may be that we add the network flag while executing your test file or task.

hardhat test --network goerli 

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.