0

The error is caught during await sender.sendTransaction(txData), but the functions of my contract are working correctly.

Here's the error:

ProviderError: Contract creation without any data provided

Here's my contract:

Demo.sol

// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

contract Demo3{
    address owner;

    event Paid(address indexed _from, uint _amount, uint _timestamp);

    constructor(){
        owner = msg.sender;
    }

    receive() external payable{
        pay();
    }

   function pay() public payable {
        emit Paid(msg.sender, msg.value, block.timestamp);
   }

   modifier onlyOwner(address _to){
        require(msg.sender == owner, "You aren't owner!");
        require(_to != address(0), "inccorect address!");
        _;
   }

   function withdraw(address payable  _to) external onlyOwner(_to){
        _to.transfer(address(this).balance);
   }
}

Here's my test script:

Demo.test.js

let owner
let other_addr
let demo

beforeEach(async function () {
    [owner, other_addr] = await ethers.getSigners()

    const DemoContract = await ethers.getContractFactory("Demo3", owner)
    demo = await DemoContract.deploy()
    await demo.waitForDeployment()
})

async function sendMoney(sender) {
    const amount = 100

    const txData = {
        to: demo.address,
        value: amount
    }

    const tx = await sender.sendTransaction(txData)
    await tx.wait()
    return [tx, amount]
}

it("shound allow to send money", async function () {
    const [sendMoneyTx, amount] = await sendMoney(other_addr)

    await expect(() => sendMoneyTx)
        .to.changeEtherBalance(demo, amount);

    const timestamp = (
        await ethers.provider.getBlock(sendMoneyTx.blockNumber)
    ).timestamp

    await expect(sendMoneyTx)
        .to.emit(demo, "Paid")
        .withArgs(other_addr.address, amount, timestamp)
})  

1 Answer 1

0

You need to replace demo.address with await demo.getAddress() in your txData, like:

const txData = {
    to: await demo.getAddress(),
    value: amount
}

Also, you need to import expect from chai package, by adding the following line in the beginning of your test script:

const { expect } = require("chai");

So, your revised Demo.test.js file would be:

const { expect } = require("chai");

let owner
let other_addr
let demo

beforeEach(async function () {
    [owner, other_addr] = await ethers.getSigners()

    const DemoContract = await ethers.getContractFactory("Demo3", owner)
    demo = await DemoContract.deploy()
    await demo.waitForDeployment()
})

async function sendMoney(sender) {
    const amount = 100

    const txData = {
        to: await demo.getAddress(),
        value: amount
    }

    const tx = await sender.sendTransaction(txData)
    await tx.wait()
    return [tx, amount]
}

it("shound allow to send money", async function () {
    const [sendMoneyTx, amount] = await sendMoney(other_addr)

    await expect(() => sendMoneyTx)
        .to.changeEtherBalance(demo, amount);

    const timestamp = (
        await ethers.provider.getBlock(sendMoneyTx.blockNumber)
    ).timestamp

    await expect(sendMoneyTx)
        .to.emit(demo, "Paid")
        .withArgs(other_addr.address, amount, timestamp)
})
2
  • 1
    Thank you. I've lost 3 days to find a reason. It's work for me. I repeat it from YouTube video. But it's not actual. Commented Aug 27 at 0:34
  • You’re welcome. Happy coding and keep building! Commented Aug 27 at 4:21

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.