0

I am working on some ERC20 tokens in which user have to send it to get tokens, i am getting some error while running it!!

i am stuck at the

const txHash = await addr1.sendTransaction({
      to: Contract_address,
      value: ethers.utils.parseEther("1.0"),
    });

getting an long error :

 Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (reason="Transaction reverted: function selector was not recognized and there's no fallback nor receive function", method="estimateGas", transaction={"from":"0x70997970C51812dc3A010C7d01b50e0d17dc79C8","to":"0x5FbDB2315678afecb367f032d93F642f64180aa3","value":{"type":"BigNumber","hex":"0x0de0b6b3a7640000"}............

Here is the code :

  1. ERC20Token.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

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

contract INR is ERC20 {

    mapping(address => uint256) _balancesETH;
    uint256 public initailPrice;
    address public owner;

    constructor(uint256 price) ERC20("Golden", "GLD") {
        initailPrice = price;
        owner = msg.sender;
    }

    function transfer(address to, uint256 amount) public override returns (bool) {  
        address owner_S = _msgSender();
        uint256 percentage = 350; // 3% of per 10,000 eg: 300 is of 3%

        uint256 value = percentageCalculate(percentage, amount);
        value = value >=100 ? value / 100 : value / 10 ;
        _burn(msg.sender, value);
        uint256 valuetoRecevier = amount - value;
        console.log("Value Recevied by recevier is :");
        console.log(valuetoRecevier);
        _transfer(owner_S, to, valuetoRecevier);
        return true;
    }

    function percentageCalculate(uint256 percentageInBasePoints, uint256 amountValue) public pure returns(uint256){
        // base point means 3% is 300 and 3.5% is 350
        uint256 newFormula;
        if(amountValue >= 100){
              newFormula = percentageInBasePoints * ((amountValue) / 100 );

        } else {
          //  amountValue = amountValue / 10;
          percentageInBasePoints /= 10;
            newFormula  = (percentageInBasePoints * (amountValue)) / 100;
            
        }
        return newFormula;
    }   

    function getEthtoToken() payable external returns(bool){
        console.log("Initial Price is :-");
        console.log(initailPrice); 

        uint256 ETHrecevied = msg.value;

        _balancesETH[msg.sender] = ETHrecevied;

        uint256 noETH = ETHrecevied / 1000000000000000; // 10^15 

        console.log("ETH recevied in finney(milliether) :-");
        console.log(noETH);

        uint256 priceCalulate = (initailPrice * noETH )/ 1000; 

        console.log("Numbers of tokens will be transfer :-");
        console.log(priceCalulate);

            _mint(msg.sender, priceCalulate);

            uint256 getSupply = initailPrice / 10;
            initailPrice -= getSupply; 

            console.log("Initial Price will be :-");
            console.log(initailPrice); 

            payable(owner).transfer(msg.value);
            return true;
    }

    // receive() external payable {
    //     _balancesETH[msg.sender] = msg.value;
    //     getEthtoToken();
    // }

    // fallback() external payable {
    //     // React to receiving ether
    //     _balancesETH[msg.sender] = msg.value;
    //     // getEthtoToken();
    // }
}

here is my testcase file 2.sample-test.js :

const { expect } = require("chai");
const { ethers, waffle } = require("hardhat");

describe("Token", function () {
  it("Deploy and run functions to get token", async function () {
    // 
    const provider = waffle.provider;
    const [owner1, addr1, addr2, addr3, addr4] = await ethers.getSigners();

    //
    const ERC20Token = await ethers.getContractFactory("INR");
    const Token = await ERC20Token.deploy(100);
    await Token.deployed();
    const Contract_address = await Token.address;
    console.log("address of contract : "+Contract_address);

    expect(await Token.initailPrice()).to.equal(100);

    //
    const owner_address = await Token.owner();
    console.log("Deployer address : "+owner_address);

    expect(await owner_address).to.equal(owner1.address);

    const Owner_Balance = await provider.getBalance(owner_address);
    ethers.utils.formatEther(Owner_Balance)
    console.log("Balance of Owner is : "+Owner_Balance);

    
   
    const addr1_balance = await provider.getBalance(addr1.address);
    console.log("balance of address 1 before transaction : "+ethers.utils.formatEther(addr1_balance));


    const txHash = await addr1.sendTransaction({
      to: Contract_address,
      value: ethers.utils.parseEther("1.0"),
    });

    console.log(txHash);
    // const params = { to: addr2.address, value: ethers.utils.parseEther("1.0").toHexString()};
    // const txHash = await addr1.sendTransaction(params);
    // console.log("transactionHash is " + txHash);  

    const addr1_balance2 = await provider.getBalance(addr1.address);
    console.log("Balance of address 1 after transaction : "+ethers.utils.formatEther(addr1_balance2));

    const ethToToken = await Token.getEthtoToken();
    // wait until the transaction is mined
    await ethToToken.wait();

    const balanceOfContract = await provider.getBalance(Contract_address);
    console.log("Balance of contract is : "+ethers.utils.formatEther(balanceOfContract));
    console.log("Balance of Contract owner is : "+ethers.utils.formatEther(Owner_Balance)); 

  });
});
7
  • When you call sendTransaction without data, it will trigger receive() or fallback() but they are all commented out. I think you just need to uncomment one of them.
    – turboza
    Jun 14, 2022 at 18:23
  • i want to use the payable function function getEthtoToken() payable external returns(bool){ which is in my contract
    – DereK
    Jun 15, 2022 at 3:42
  • I think you should call Token.getEthtoToken() in that case?
    – turboza
    Jun 15, 2022 at 3:57
  • can i use override params here like Token.getEthtoToken({from: addr1, value: ethers.utils.parseEther("1.0"), }) like these or how should i ??
    – DereK
    Jun 15, 2022 at 4:05
  • hey thanks for help i have solved the issue by using this code const sendEth = await Token.connect(addr1).getEthtoToken({value: ethers.utils.parseEther("1.0")}); // wait until the transaction is mined await sendEth.wait();
    – DereK
    Jun 15, 2022 at 4:43

1 Answer 1

0

Way of sending eth through payable functions in hardhat testcases.

const sendEth = await Token.connect(addr1).getEthtoToken({value: ethers.utils.parseEther("1.0")});      // wait until the transaction is mined      await sendEth.wait();

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.