rhlsthrm / hardhat-typechain

TypeChain tasks for Buidler
MIT License
18 stars 13 forks source link

ethers.js - problem with function overloading in Solidity contracts #14

Open SvenMeyer opened 3 years ago

SvenMeyer commented 3 years ago

I have a simple Solidity contract. The functions which do not exist with different parameter signatures (no function overloading) work fine in a Hardhat , Waffle , Chai , TypeScript test setup, but fail if the function exists with different function signatures ...

pragma solidity ^0.7.0;

contract StakingContract {

    function getStakedAmount(address user) public pure returns (uint) {
        // return uint(user) & 0x7; // return 0..7
        return (user == address(0x0)) ? 0 : 1; // one ticket for every account, no matter how much staked
    }

    function getStakedAmount() public view returns (uint) {
        return getStakedAmount(msg.sender);
    }
}
import { Signer } from "@ethersproject/abstract-signer";
import { ethers, waffle } from "hardhat";
import { expect } from "chai";
import { Accounts, Signers } from "../types";

import StakingContractArtifact from "../artifacts/contracts/StakingContract.sol/StakingContract.json";
import ProbTokenSaleArtifact   from "../artifacts/contracts/ProbTokenSale.sol/ProbTokenSale.json";

import { StakingContract } from "../typechain/StakingContract";
import { ProbTokenSale }   from "../typechain/ProbTokenSale";

const { deployContract } = waffle;

const max_tickets = 1000;
const tickets_per_stake = 1;

describe("ProbTokenSale", function () {

  before(async function () {
    this.accounts = {} as Accounts;
    this.signers  = {} as Signers;

    const signers: Signer[] = await ethers.getSigners();
    this.signers.admin = signers[0];
    this.accounts.admin = await signers[0].getAddress();
    console.log("account:", this.accounts.admin);

    this.stake = (await deployContract(this.signers.admin, StakingContractArtifact, [])) as StakingContract;
    this.pts   = (await deployContract(this.signers.admin, ProbTokenSaleArtifact, [max_tickets, tickets_per_stake])) as ProbTokenSale;
  });

  it("user account can call StakingContract.getStakedAmount", async function () {
    expect(await this.stake.connect(this.signers.admin).getStakedAmount()).to.equal(1);
  });

});
     TypeError: this.stake.connect(...).getStakedAmount is not a function
      at Context.<anonymous> (test/ProbTokenSale.ts:33:57)
      at step (test/ProbTokenSale.ts:33:23)
      at Object.next (test/ProbTokenSale.ts:14:53)
      at /home/sum/DEV/ETH/ZeroSwap/zeroswap-dto-lottery-nft/test/ProbTokenSale.ts:8:71
      at new Promise (<anonymous>)
      at __awaiter (test/ProbTokenSale.ts:4:12)
      at Context.<anonymous> (test/ProbTokenSale.ts:81:16)

Any idea what is wrong ?

SvenMeyer commented 3 years ago

Looks like it is more an issue with ethers.js

https://github.com/tasitlabs/tasit-sdk/issues/172

https://github.com/ethers-io/ethers.js/issues/950

https://github.com/ethers-io/ethers.js/issues/1160

Any chance that a TypeScript approach may provide a better solution ?