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.
Related
I'm trying to test a Solidity Library directly using hardhat and chaï.
This is a Library example I would like to test:
library LibTest {
function testFunc() public view returns(bool) {
return true;
}
}
and this is how I'm trying to test it.
beforeEach(async () => {
const LibTest = await ethers.getContractFactory("LibTest");
const libTest = await LibTest.deploy();
await libTest.deployed();
})
describe('Testing test()', function () {
it("is working testFunc ?", async function () {
console.log(await libTest.testFunc());
})
})
But I have the error message:
ReferenceError: libTest is not defined
I read everything I can on Chai doc and Hardhat doc but can't found any solution
I would say to use fixture and also utilize waffle and to deploy the library contract once and save the snapshot of the contract:
const {loadFixture } = require('#nomicfoundation/hardhat-network-helpers');
const {expect} = require('chai');
const {ethers, waffle} = require('hardhat');
const {deployContract} = waffle;
const LibArtifact = require('../artifacts/contracts/lib.sol/LibTest.json');
describe("Lib tests", function () {
// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshopt in every test.
async function deployOnceFixture() {
const [owner, ...otherAccounts] = await ethers.getSigners();
lib = (await deployContract(owner, LibArtifact));
return { lib, owner, otherAccounts };
}
describe("Testing test()", function () {
it("s working testFunc ?", async function () {
const { lib } = await loadFixture(deployOnceFixture);
expect(await lib.testFunc()).to.be.true;
});
});
});
Instructions to add the waffle plugin:
Hardhat-waffle
Basically, you need to install the libraries:
npm install --save-dev #nomiclabs/hardhat-waffle 'ethereum-waffle#^3.0.0' #nomiclabs/hardhat-ethers 'ethers#^5.0.0'
And then import hardhat-waffle in the hardhat-config.js file:
require("#nomiclabs/hardhat-waffle");
Also, notice I put the test file in a test directory so I needed to go back one folder to find artifacts generated by running npx hardhat compile.
For convenience I pushed the solution in a Github repo: https://github.com/Tahlil/run-solidity-lib
The best way I have found to go about this is to create a LibTest.sol contract that invokes and tests the Lib itself. And just running abstracted tests in JS/TS to invoke the LibTest contract, connecting the Lib.sol contract to it during deployment in Hardhat.
const Lib = await ethers.getContractFactory("Lib");
const lib = await Lib.deploy();
const LibTest = await ethers.getContractFactory("LibTest", {
libraries: {
Lib: lib.address,
},
});
const libTest = await LibTest.deploy();
/// later: console.log(await libTest.testLibFunc());
LibTest.sol:
import "./Lib.sol";
library LibTest {
function testLibFunc() public view returns(bool) {
bool response = Lib.testFunc();
return response;
}
}
Were you able to find a better method?
Getting the below error while trying to deploy a smart contract from hardhat. Error details
TypeError: Cannot read property 'sendTransaction' of null
at ContractFactory.<anonymous> (C:\Collection\node_modules\#ethersproject\contracts\src.ts\index.ts:1249:38)
at step (C:\Collection\node_modules\#ethersproject\contracts\lib\index.js:48:23)
at Object.next (C:\Collection\node_modules\#ethersproject\contracts\lib\index.js:29:53)
at fulfilled (C:\Collection\node_modules\#ethersproject\contracts\lib\index.js:20:58)
Here are the config files
hardhat.config.js
require('#nomiclabs/hardhat-waffle');
require("#nomiclabs/hardhat-ethers");
require("dotenv").config();
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more
/**
* #type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: "0.8.2",
networks: {
mumbai: {
url: process.env.MUMBAI_URL,
account: process.env.PRIVATE_KEY
}
}
};
deploy.js
const {ethers} = require("hardhat");
async function main() {
const SuperMario = await ethers.getContractFactory("SuperMario");
const superInstance = await SuperMario.deploy("SuperMarioCollection", "SMC");
await superInstance.deployed();
console.log("contract was deployed to:", superInstance.address());
await superInstance.mint("https://ipfs.io/ipfs/XXXXXXX");
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
I am trying to deploy it using the following command
npx hardhat run scripts/deploy.js --network mumbai
thanks
Change account to accounts in the network config
found the fix. There was an error in the hardhat.config file
instead of account:, it should have been
accounts:[process.env.PRIVATE_KEY]
I'm new to Remix and Solidity and don't understand why I recieve error message "Error from IDE : Invalid account selected".
Below line is executed successfully:
await contract.methods.SetMaxSupply("600").send({from: accounts[0]});
Below line results in above mentioned error message:
let supply = await contract.methods.current_supply().call()
Solidity code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
contract Test is Ownable {
uint public max_supply;
function SetMaxSupply(uint amount) public onlyOwner {
max_supply = amount;
}
function current_supply() public view returns(uint) {
return max_supply * 3;
}
}
Remix JS Script:
// Right click on the script name and hit "Run" to execute
(async () => {
try {
console.log('Running deployWithWeb3 script...')
// replace with contract address
const contractAddress = '0xd9145CCE52D386f254917e481eB44e9943F39138'
const contractName = 'Test' // Change this for other contract
// Note that the script needs the ABI which is generated from the compilation artifact.
// Make sure contract is compiled and artifacts are generated
const artifactsPath = `browser/contracts/StackOverflow/artifacts/${contractName}.json` // Change this for different path
const metadata = JSON.parse(await remix.call('fileManager', 'getFile', artifactsPath))
const abi = metadata.abi
// instantiate a new web3 Contract object
let contract = new web3.eth.Contract(abi, contractAddress)
const accounts = await web3.eth.getAccounts()
await contract.methods.SetMaxSupply("600").send({from: accounts[0]});
let supply = await contract.methods.current_supply().call()
console.log(supply)
} catch (e) {
console.log(e.message)
}
})()
Try changing the Account parameter in the Deploy & Run Transaction
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-
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.