Truffle test fails with TypeError - solidity

I have a very simple contract which works perfectly fine in remix editor.
I just wanted to learn truffle.I initiated an empty truffle project, placed the contract and get that compiled successfully.
however truffle test gives below error
Contract:
pragma solidity ^0.4.18;
contract Greetings{
string public message;
constructor() public {
message = "Hello";
}
function getGreeting() public view returns (string){
return message;
}
}
Test:
var Greetings = artifacts.require("Greetings");
contract('Greetings Test', async (accounts) => {
it("check for greetings message", async () => {
let greeting = await Greetings.deployed();
let message = await greeting.getGreeting().call();
console.log(message);
});
});
Error:
Contract: Greetings Test
1) check for greetings message
> No events were emitted
0 passing (103ms)
1 failing
1) Contract: Greetings Test
check for greetings message:
TypeError: greeting.getGreeting(...).call is not a function
at Context.it (test/campaignfactory.js:7:52)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
Note: I would like to use asnyc/await.

There is an error on the way you invoke the function. Either you use
let message = await greeting.getGreeting.call();
or
let message = await greeting.getGreeting()
You can't mix the syntax. When you call the method (eg getGreeting()) web3 will check whether is a call or a transaction and will use the right one for you. doc
In case you'd like to be explicit then you should use the fist way.

Related

How to test the Solidity fallback() function via Hardhat?

I have a Solidity smart contract Demo which I am developing in Hardhat and testing on the RSK Testnet.
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract Demo {
event Error(string);
fallback() external {
emit Error("call of a non-existent function");
}
}
I want to make sure that the fallback function is called and the event Error is emitted. To this end, I am trying to call a nonExistentFunction on the smart contract:
const { expect } = require('chai');
const { ethers } = require('hardhat');
describe('Demo', () => {
let deployer;
let demoContract;
before(async () => {
[deployer] = await ethers.getSigners();
const factory = await ethers.getContractFactory('Demo');
demoContract = await factory.deploy().then((res) => res.deployed());
});
it('should invoke the fallback function', async () => {
const tx = demoContract.nonExistentFunction();
await expect(tx)
.to.emit(demoContract, 'Error')
.withArgs('call of a non-existent function');
});
});
However Hardhat throws a TypeError even before it actually connects to the smart contract on RSK:
Demo
1) should invoke the fallback function
0 passing (555ms)
1 failing
1) Demo
should invoke the fallback function:
TypeError: demoContract.nonExistentFunction is not a function
at Context.<anonymous> (test/Demo.js:13:29)
at processImmediate (internal/timers.js:461:21)
How can I outsmart Hardhat/Ethers.js and finally be able to call non-existent function thus invoking the fallback function in the smart contract?
For reference, this is my hardhat.config.js
require('#nomiclabs/hardhat-waffle');
const { mnemonic } = require('./.secret.json');
module.exports = {
solidity: '0.8.4',
networks: {
hardhat: {},
rsktestnet: {
chainId: 31,
url: 'https://public-node.testnet.rsk.co/',
accounts: {
mnemonic,
path: "m/44'/60'/0'/0",
},
},
},
mocha: {
timeout: 600000,
},
};
You can use the approach of injecting a non-existent function signature
into the smart contract object (ethers.Contract).
Create a function signature for nonExistentFunction:
const nonExistentFuncSignature =
'nonExistentFunction(uint256,uint256)';
Note that the parameter list should contain no whitespace,
and consists of parameter types only (no parameter names).
Then instantiate a new smart contract object.
When you do this you need to modify the ABI for Demo
such that it includes this additional function signature.:
const fakeDemoContract = new ethers.Contract(
demoContract.address,
[
...demoContract.interface.fragments,
`function ${nonExistentFuncSignature}`,
],
deployer,
);
Note that the contract that is deployed is the same as before -
it does not contain this new function.
However the client interacting with this smart contract -
the tests in this case -
does think that the smart contract has this function now.
At this point, you'll be able to run your original test,
with a minor modification:
const tx = fakeDemoContract[nonExistentFuncSignature](8, 9);
await expect(tx)
.to.emit(demoContract, 'Error')
.withArgs('call of a non-existent function');
Full test:
it('should invoke the fallback function', async () => {
const nonExistentFuncSignature = 'nonExistentFunc(uint256,uint256)';
const fakeDemoContract = new ethers.Contract(
demoContract.address,
[
...demoContract.interface.fragments,
`function ${nonExistentFuncSignature}`,
],
deployer,
);
const tx = fakeDemoContract[nonExistentFuncSignature](8, 9);
await expect(tx)
.to.emit(demoContract, 'Error')
.withArgs('call of a non-existent function');
});
Test result:
Demo
✔ should invoke the fallback function (77933ms)
1 passing (2m)
A transaction executing a function contains the function selector following its (ABI-encoded) input params in the data field.
The fallback() function gets executed when the transaction data field starts with a selector that does not match any existing function. For example an empty selector.
So you can generate a transaction to the contract address, with empty data field, which invokes the fallback() function.
it('should invoke the fallback function', async () => {
const tx = deployer.sendTransaction({
to: demoContract.address,
data: "0x",
});
await expect(tx)
.to.emit(demoContract, 'Error')
.withArgs('call of a non-existent function');
});
Note: If you also declared the receive() function, it takes precedence over fallback() in case of empty data field. However, fallback() still gets executed for every non-empty mismatching selector, while receive() is only invoked when the selector is empty.

File won't get deployed? (HardhatError: HH700: Artifact not found.)

I'm following this tutorial here: https://ethereum.org/en/developers/tutorials/hello-world-smart-contract-fullstack/ and I'm stuck with this error message:
HardhatError: HH700: Artifact for contract "HelloWorld" not found.
From what I found on the forums it seems to be a naming problem but the name for the contract & what is being deployed is the same:
pragma solidity >=0.7.3;
contract HelloWorld {
event UpdatedMessages(string oldStr, string newStr);
string public message;
constructor(string memory initMessage) {
message = initMessage;
}
function update(string memory newMessage) public {
string memory oldMsg = message;
message = newMessage;
emit UpdatedMessages(oldMsg, newMessage);
}
}
and this is the deploy.js file:
async function main() {
const HelloWorld = await ethers.getContractFactory("HelloWorld")
// Start deployment, returning a promise that resolves to a contract object
const hello_world = await HelloWorld.deploy("Hello World!")
console.log("Contract deployed to address:", hello_world.address)
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})
When I compile it just says "Nothing to compile" and running this command: npx hardhat run scripts/deploy.js --network ropsten given mentioned HH700 error.
Can anyone please help?
const hello_world = await HelloWorld.deploy("Hello World!")
should be
const hello_world = await deploy("HelloWorld!")
Could be that you are missing the “npx hardhat compile” before the test. The artifacts are created when you compile.

Test Smart Contract with arguments in constructor with truffle

I want to test smart contract with parameter in constructor but have error.
Here is my smart contract and test files:
pragma solidity >=0.4.25 <0.7.0;
contract Test {
string public test;
constructor(string memory _test) public {
test = _test;
}
}
const Test = artifacts.require("Test");
contract('Test', (accounts) => {
it('should init', async () => {
const instance = await Test.new("test");
const result = await instance.test;
assert.equal("test", result, "info is not equals");
});
});
And log:
Error: while migrating Test: Invalid number of parameters for "undefined". Got 0 expected 1!
at /usr/local/lib/node_modules/truffle/build/webpack:/packages/deployer/src/deployment.js:365:1
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at Migration._deploy (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:68:1)
at Migration._load (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:55:1)
at Migration.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:171:1)
at Object.runMigrations (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:150:1)
at Object.runFrom (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:110:1)
at Object.runAll (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:114:1)
at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:79:1)
at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/Test.js:109:1)
at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/test/index.js:192:1)
at Command.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/command.js:136:1)
Truffle v5.1.64 (core: 5.1.64)
Node v12.16.3
How to solve it?
Your forgot to pass the transaction params including the sender (deployer) adddres.
const instance = await Test.new("test");
shoud be
const txParams = {
from: accounts[0]
};
const instance = await Test.new("test", txParams);
More info on the new() function: https://www.trufflesuite.com/docs/truffle/reference/contract-abstractions#-code-mycontract-new-arg1-arg2-tx-params-code-

Truffle and Ganache-cli test case fails

So I have this setup : truffle and ganache-cli
I'm sending some ether to my contract, here is the related part of my contract:
mapping(address => uint256) public balanceOf;
function () payable public {
uint amount = msg.value;
balanceOf[msg.sender] += amount;
}
In truffle this is how I send the ether.
it("Test if can be payed", function(){
return web3.eth.sendTransaction({
from:fromAddr,
to:MyContract.address,
value:amountToSend
}).then(function(res){
expect(res).to.not.be.an("error"); // test passed
});
});
it("Test if contract received ether", function(){
return web3.eth.getBalance(MyContract.address,
function(err, res){
expect(parseInt(res)).to.be.at.least(1000000000000000000); // test passed
});
});
it("Catch if balanceOf "+fromAddr, function(){
return sale.balanceOf.call(fromAddr).then(function(res){
expect(parseInt(res)).to.be.at.least(1); // fails the test
});
});
Am I doing it right? What could be the reason for failed test?
truffle test output :
AssertionError: expected 0 to be at least 1
+ expected - actual
-0
+1
I can provide more info if needed.
UPDATE :
for clarification on sale which is global variable.
it("Test if MyContract is deployed", function(){
return MyContract.deployed().then(function(instance){
sale = instance;
});
});
I think this is what you are looking for:
File path:
test/vault.js
const Vault = artifacts.require("Vault");
contract("Vault test", async accounts => {
// Rely on one instance for all tests.
let vault;
let fromAccount = accounts[0];
let oneEtherInWei = web3.utils.toWei('1', 'ether');
// Runs before all tests.
// https://mochajs.org/#hooks
before(async () => {
vault = await Vault.deployed();
});
// The `receipt` will return boolean.
// https://web3js.readthedocs.io/en/1.0/web3-eth.html#gettransactionreceipt
it("Test if 1 ether can be paid", async () => {
let receipt = await web3.eth.sendTransaction({
from: fromAccount,
to: vault.address,
value: oneEtherInWei
});
expect(receipt.status).to.equal(true);
});
it("Test if contract received 1 ether", async () => {
let balance = await web3.eth.getBalance(vault.address);
expect(balance).to.equal(oneEtherInWei);
});
// In Web3JS v1.0, `fromWei` will return string.
// In order to use `at.least`, string needs to be parsed to integer.
it("Test if balanceOf fromAccount is at least 1 ether in the contract", async () => {
let balanceOf = await vault.balanceOf.call(fromAccount);
let balanceOfInt = parseInt(web3.utils.fromWei(balanceOf, 'ether'));
expect(balanceOfInt).to.be.at.least(1);
});
});
You can see the full project here. Do note that I'm using Truffle v5 and Ganache v2. See the README file inside that GitLab repository.
Back to your question, there were 2 mistakes:
The sale is not defined. I have a feeling that you were actually referring to MyContract.
In order to use the least method in ChaiJS, you need to make sure you are passing integers. The balanceOf call is returning BigNumber or BN object and you can't use it with .least method.
FYI, Truffle v5 is now using BN by default (previously BigNumber). More about it here.
Let me know if this helps.

Getting new BigNumber() error when calling a function in solidity using truffle. How do I fix the error?

I get this error when I try to call my solidity function using truffle.
My solidity code is as :
pragma solidity ^0.4.14;
contract SimpleDemo {
function returnNumber () public view returns (uint) {
return 500;
}
}
The way I'm calling returnNumber() is by :
this.state.web3.eth.getAccounts((error, accounts) => {
simpleDemo.deployed().then((instance) => {
simpleDemoInstance = instance
// Below line runs with the error ...
return simpleDemoInstance.returnNumber.call()
}).then((result) => {
console.log(result)
})
})
Also, this solution did not help at all. Hence, I asked separately.
It should be:
simpleDemoInstance.methods.returnNumber().call({
from: accounts[0]
});
if it is a function that takes gas(assuming you want to send from metamask).
If it is not a payable function you use:
simpleDemoInstance.methods.returnNumber().call()
Also use es6. Trying to write this stuff without async await is terrible IMO.