I am using examples in soliditylang.org special functions
My simple contracts are extracted from the link
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.6.2 <0.9.0;
contract TestPayable {
uint x;
uint y;
fallback() external payable {
x = 1;
y = msg.value;
}
receive() external payable {
x = 2;
y = msg.value;
}
}
contract Caller {
function callTestPayable3(TestPayable test) public returns (bool) {
(bool success, ) = address(test).call{value: 2 ether}("");
require(success, "low-level call failed");
return true;
}
}
My deploy script is using hardhat-deploy
const { network } = require("hardhat");
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deploy, log } = deployments;
const { deployer } = await getNamedAccounts();
console.log("deployer : ", deployer);
for (const contractName of ["TestPayable", "Caller"]) {
const arguments = [];
const contract = await deploy(contractName, {
from: deployer,
args: arguments,
log: true,
waitConfirmations: 1,
});
const chainId = network.config.chainId;
console.log(
`deployed ${contractName} at ${contract.address}, chainId: ${chainId}`
);
}
};
module.exports.tags = ["all"];
My test script is
const { network, deployments, ethers } = require("hardhat");
!["localhost", "hardhat"].includes(network.name)
? describe.skip
: describe("Unit Tests", function () {
let testPayable, caller, deployer;
beforeEach(async () => {
accounts = await ethers.getSigners();
deployer = accounts[0];
await deployments.fixture(["all"]);
testPayable = await ethers.getContract("TestPayable", deployer);
caller = await ethers.getContract("Caller", deployer);
});
it("caller.callTestPayable3", async function () {
await caller.callTestPayable3(testPayable.address);
});
});
My hardhat.config.js is
require("@nomicfoundation/hardhat-toolbox");
require("hardhat-deploy");
module.exports = {
solidity: "0.8.18",
namedAccounts: {
deployer: {
default: 0,
},
},
};
My package.json is
{
"dependencies": {
"hardhat": "^2.14.0",
"hardhat-deploy": "^0.11.28"
},
"devDependencies": {
"@ethersproject/abi": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@nomicfoundation/hardhat-chai-matchers": "^1.0.6",
"@nomicfoundation/hardhat-network-helpers": "^1.0.8",
"@nomicfoundation/hardhat-toolbox": "^2.0.2",
"@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers",
"@nomiclabs/hardhat-etherscan": "^3.1.7",
"@typechain/ethers-v5": "^10.2.0",
"@typechain/hardhat": "^6.1.5",
"chai": "^4.3.7",
"dotenv": "^16.0.3",
"ethers": "5.7.2",
"hardhat-gas-reporter": "^1.0.9",
"solidity-coverage": "^0.8.2",
"typechain": "^8.1.1"
}
}
Deployment was fine, but somehow, when I ran "yarn hardhat test", I got error
Error: VM Exception while processing transaction: reverted with reason string 'low-level call failed'
at Caller.callTestPayable3 (contracts/fallback_call.sol:22)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at runNextTicks (node:internal/process/task_queues:64:3)
at listOnTimeout (node:internal/timers:533:9)
at processTimers (node:internal/timers:507:7)
at HardhatNode._mineBlockWithPendingTxs (C:\Users\...\node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:1840:23)
at HardhatNode.mineBlock (C:\Users\...\node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:517:16)
at EthModule._sendTransactionAndReturnHash (C:\...\node_modules\hardhat\src\internal\hardhat-network\provider\modules\eth.ts:1532:18)
This is the most basic example of low-level call from Solidity site, supposedly to work out of box. Why doesn't it work for me?