question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting error "... is not a function" for Write Methods

See original GitHub issue

I am using ethers.js in hardhat (buidler) testing and cannot call Write Methods. I can access contracts Properties and Read-Only Methods, but when I call Write Methods I keep getting "... is not a function" error:

In my sample is an ERC20 Token. I can see the method also when calling contract.interface:

const { expect } = require("chai");
describe("Test", function () {

  let FZBAT, AddressProvider, ZBAT, owner, addr1, addr2;
  const mintAmount = (50 * (10 ** 18)).toString(); // 50 Tokens

  before(async function () {

    const FZBATArt = await ethers.getContractFactory("FZBAT");
    FZBAT = await upgrades.deployProxy(FZBATArt, ["FZBAT", "FZ BA Token", 16]);
    await FZBAT.deployed();
    console.log('vDeployed FZBAT', FZBAT.address);
  });

  it("Should mint tokens from FZBAT", async () => {
    let result = await FZBAT.mint(addr1.address, mintAmount);
  });
});

console:


  Test
vDeployed FZBAT 0xCBF508609D841D2dA4E31cFAA504e2922e625e31
    1) Should mint tokens from FZBAT


  0 passing (5s)
  1 failing

  1) Test
       Should mint tokens from FZBAT:
     TypeError: FZBAT.mint is not a function

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
ricmoocommented, Nov 19, 2020

If you have two methods with the same name, you must specify the fully qualified signature to access it. You can use either of the following, depending on which you want:

contract["mint(uint256)"](amount)
// or
contract["mint(address,uint256)"](account, amount)

If you only use one of those methods, you can simply drop the unused one from the ABI you pass into the Contract constructor, then you can use the name mint to access it.

For more details, see https://github.com/ethers-io/ethers.js/issues/950#issuecomment-657803378. 😃

1reaction
ricmoocommented, Dec 26, 2020

@SvenMeyer

If the result is ambiguous, you must specify the signature. It is not, in general, safe to simply use the “first” one, since various compilers, versions and tools manipulate and output the ABI in different orders. Orders could be alphabetical, ordered by the selector, argument count, dependency order, etc. You won’t want switching the compiler version to cause your JavaScript to break. 😃

In general I highly recommend that people only include the fragments they use, and to use the Human-Readable ABI (which is much smaller than the equivalent JSON). If there is a method that is completely unused, excluding it keeps the code cleaner and the bundle size smaller. An ABI can easily be 10kb-500kb, while most things only require 1-10 signatures, which fits in about 50-600 bytes. 😃

const ABI = [
  "function mint(address owner, uint amount) payable",
  "function balanceOf(address owner) view returns (uint)",
  "event Transfer(address indexed from, address indexed to, uint amount)"
];
const contract = new Contract(tokenAddressOrEnsName, ABI, signer);
const balance = await contract.balanceOf(signer.getAddress());
const tx = await contract.mint(owner, amount);
contract.on("Transfer", (from, to, amount, event) => {
  // Tra la la...
});

Does that make sense?

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: "x" is not a function - JavaScript - MDN Web Docs
The JavaScript exception "is not a function" occurs when there was an attempt to call a value from a function, but the value...
Read more >
How to Handle JavaScript Uncaught TypeError: “x” is Not a ...
The Javascript error TypeError: "x" is not a function occurs when there is an attempt to call a function on a value or...
Read more >
JavaScript error: "is not a function" - Stack Overflow
It was attempted to call a value like a function, but the value is not actually a function. Some code expects you to...
Read more >
JavaScript: Uncaught TypeError: n is not a function
Uncaught TypeError : 'n' is not a function: This is a standard JavaScript error when trying to call a function before it is...
Read more >
How to solve the "is not a function" error in JavaScript
How to solve the "is not a function" error in JavaScript. Published in 2020 ... I write JavaScript without semicolons. ... Let's look...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found