0

I'm trying to test function (stake) which contains reference to another contract function (transferFrom). But when I test it I have a mistake. But MyToken has deployed and has address to hardhat network.

Here is a contract

import "./MyToken.sol"; contract Staking { MyToken public myToken; function stake(uint256 _value) public { myToken.transferFrom(msg.sender, address(this), _value); } } 

Here is a test

import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; import { expect } from "chai"; import { assert } from "console"; import { ethers } from "hardhat"; import { MyToken, MyToken__factory, Staking, Staking__factory } from "../typechain"; // import { MyToken, MyToken__factory } from "../typechain"; describe("MyToken", function () { let staking: Staking; let myToken: MyToken; let bob: SignerWithAddress, alice: SignerWithAddress; before(async () => { [bob, alice] = await ethers.getSigners(); }) beforeEach(async () => { const MyToken = await ethers.getContractFactory("MyToken") as MyToken__factory; myToken = await MyToken.deploy() as MyToken; await myToken.deployed(); const Staking = await ethers.getContractFactory("Staking") as Staking__factory; staking = await Staking.deploy() as Staking; await staking.deployed(); }) **it("staking", async function () { const value = 100; await staking.stake(value); })** 

enter image description here

1 Answer 1

0

If you Staking only looks as you shared in the question then you are not setting the address of myToken. You need to explicitly set the address either in the constructor or with the definition.

For example:

import "./MyToken.sol"; contract Staking { MyToken public myToken; constructor(address token) { myToken = token; } function stake(uint256 _value) public { myToken.transferFrom(msg.sender, address(this), _value); } } 

In your test you need to then pass the address of myToken to the Staking.deploy function:

staking = await Staking.deploy(myToken.address) as Staking; 

One way to debug this is to check in your test that myToken is initialized correctly:

expect(await staking.myToken()).to.be.eq(myToken.address) 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.