Now I am developing smart contract which can buy any tokens with bnb(ether). I built smart contract and now I should check it works correctly using hardhat test script. So I have to set ether value(msg.value). But I can't set that value. Please Help Me.

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

describe("Greeter", function () {
  it("Should return the new greeting once it's changed", async function () {
    const [owner, account1, account2] = await hre.ethers.getSigners();
    const lockedSale = await hre.ethers.getContractFactory("lockedSale");
    const instance_lockedSale = await lockedSale.deploy("0x669c40dec7135f8f6dda00eb8d2e3937a83549fc", 10, 1000);
    const ERC20 = await hre.ethers.getContractFactory("ERC20");
    const instance_ERC20 = await ERC20.attach("0x669c40dec7135f8f6dda00eb8d2e3937a83549fc");
    await instance_ERC20.approve(owner.address, 1000);

    // wait until the transaction is mined
    await instance_ERC20
    await instance_lockedSale.buyTokens(1000); // google yourself. 
    // Think logically! this function has one parameter.
    // And bnb value is sent with msg.value. 
    // You should find out how to set ether value for hardhat script.
    expect(await instance_ERC20.balanceOf("0x6E3F59cfDd2E6d51738E0f1f5A5702Ae5e56eDAD")).to.equal(1000);
  • Does the function of the contract have parameters or is it payable only ?
    – jeissoni22
    Jan 14 at 19:06

1 Answer 1


Assuming the buyTokens method is marked payable, then the syntax would be along the lines of:

await instance_lockedSale.buyTokens(1000, { value: ethers.utils.parseEther("1") });

You should be aware of how the parseEther util method works

If you're using TypeScript/TypeChain, you'll notice that the Javascript methods generated based on your Solidity contract's ABI contain a parameter called overrides. This parameter is an optional object and is the last parameter for each contract method. The type of this parameter is some combination of the following interface depending on the Solidity modifiers of the method:

 Snippet from Ethers v5

export interface Overrides {
    gasLimit?: BigNumberish | Promise<BigNumberish>;
    gasPrice?: BigNumberish | Promise<BigNumberish>;
    maxFeePerGas?: BigNumberish | Promise<BigNumberish>;
    maxPriorityFeePerGas?: BigNumberish | Promise<BigNumberish>;
    nonce?: BigNumberish | Promise<BigNumberish>;
    type?: number;
    accessList?: AccessListish;
    customData?: Record<string, any>;

export interface PayableOverrides extends Overrides {
    value?: BigNumberish | Promise<BigNumberish>;

export interface CallOverrides extends PayableOverrides {
    blockTag?: BlockTag | Promise<BlockTag>;
    from?: string | Promise<string>;

Due to the lack of documentation here, the frequency these APIs are changing as Solidity and the related tooling matures, and the ease of use for consuming clients (React frontend, Node backend, etc) I would recommend enabling TypeScript support with TypeChain bindings

As far as documentation, I was able to find some incompletely related structures in Ethers v5 documentation and also found the exact code being referenced by Hardhat/TypeChain in the Ethers code repo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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