HardHat and Rinkeby ProviderError: Must be authenticated Error - ethereum

When I run
npx hardhat console --network rinkeby
accounts = await ethers.provider.listAccounts();
I get
Uncaught ProviderError: Must be authenticated!
with below rinkeby network config in hardhat.config.js
rinkeby: {
url: "ALCHEMY_URL",
accounts:["YOUR_PRIVATE_KEY"],
}

Solution: update config file with below lines:
rinkeby: {
url: "ALCHEMY_URL",
accounts: ["YOUR_PRIVATE_KEY"],
gas: 2100000,
gasPrice: 8000000000,
saveDeployments: true,
}
This works like a charm. Hope this helped in saving some of your time.

Finally, your hardhat.config.js should look like this
require('#nomiclabs/hardhat-waffle');
module.exports = {
solidity: '0.8.0',
networks: {
ropsten: {
url: `${ARCHEM_APP_URL}`,
accounts: [`${ACCOUNT_KEY}`],
gas: 2100000,
gasPrice: 8000000000,
saveDeployments: true,
}
}
}

Related

How to correctly pass --network parameter to Hardhat scripts?

I'm trying to deploy to Goerli, but my deploy script seems to ignore the --network parameter.
Here is my hardhat.config.ts:
import { HardhatUserConfig } from "hardhat/config";
import "#nomicfoundation/hardhat-toolbox";
import "hardhat-gas-reporter"
import "#nomiclabs/hardhat-ethers";
import * as dotenv from 'dotenv'
dotenv.config();
const env:any = process.env;
const config: HardhatUserConfig = {
solidity: {
[...]
},
networks: {
hardhat: {
[...]
},
goerli: {
url: 'https://goerli.infura.io/v3/',
accounts: [env['DEPLOYER_PRIVATE_KEY']]
},
},
[...]
};
export default config;
Then I run:
npx hardhat run scripts/deploy.ts --network goerli
And in my deploy.ts:
async function main() {
const [deployer] = await ethers.getSigners();
console.log('Using RPC ', ethers.provider.connection.url);
console.log('Deploying from address', deployer.address);
[...] // contract deployment code
}
However it fails with error "could not detect network". It makes sense because it also logs (from my code):
Using RPC http://localhost:8545
Deploying from address 0x3a5Bd3fBc2a17f2eECf2Cff44aef38bd7dc4fd7c
My address is correct, the address logged indeed corresponds to the account that I provided with the private key from dotenv, so it's being read from the config correctly. However, the RPC URL is incorrect: it seems that it's trying to connect to my local RPC and failing.
Why isn't Hardhat respecting the url property in the config, and still trying to connect to my local instance?
Change
const config: HardhatUserConfig = {
solidity: {
[...]
},
to
module.exports = {
solidity: "0.8.4",
networks: {
goerli: {
url: 'https://goerli.infura.io/v3/',
accounts: [env['DEPLOYER_PRIVATE_KEY']]
}
}
};
If you want to deploy on Hardhat you can run npx hardhat node and remove goerli and const env:any = process.env;.

BSC Testnet addLiquiditiyETH TRANSFER_FROM_FAILED

I'm coding an bep20 token and if I want to add Liquidity with pancakeswap I get the following error:
ProviderError: Error: VM Exception while processing transaction: reverted with reason string 'TransferHelper: TRANSFER_FROM_FAILED'
Does anyone know why this is not working?
PancakeRouter address: 0xD99D1c33F9fC3444f8101754aBC46c52416550D1
PancakeFactory address: 0x6725F303b657a9451d8BA641348b6761A6CC7a17
My addLiquidity function:
function addLiquidity() public payable {
_approve(address(this), _pancakeRouterAddress, totalSupply());
_pancakeRouter.addLiquidityETH(
address(this),
totalSupply(),
0,
0,
address(this),
block.timestamp
);
}
Hardhat fork: npx hardhat node --fork https://data-seed-prebsc-1-s1.binance.org:8545
Hardhat networks config:
networks: {
localhost: {
url: 'http://localhost:8545',
chainId: 31337,
forking: {
url: " https://data-seed-prebsc-1-s1.binance.org:8545",
}
},
},
Ensure two things when you add liquidity which uses transferFrom() function internally:
You have approved the spendor (here router)
you have at least the amount you have approved the spendor to spend

React, Hardhat frontend smart contract method calling, how to do so?

I'm using hardhat locally and have a react frontend up and running but I can't call the methods without errors.
I've tried both ethers.js and web3.
Here's my code and attempts. Please let me know if you see what I'm doing wrong.
I'm trying to interact with contracts that are deployed in the local hardhat env through web3
I'm unable to get back the data from the contract, here's the info
I have:
var list = await contract.methods.getList();
console.log("list ", list );
which gets me
list {arguments: Array(0), call: ƒ, send: ƒ, encodeABI: ƒ, estimateGas: ƒ, …}
When I do
var list = await contract.methods.getList().call();
console.log("list ", list );
I get this error in the browser:
Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.
I do:
Setup in console:
npx hardhat node
>Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/
>Accounts
>========
>...
npx hardhat compile
> Nothing to compile
npx hardhat run scripts/deploy.js --network hardhat
Note: In the deploy.js file, I do a
const list = await contract.getList();
console.log("list", list ); // correctly outputs ["string", "string"]
The method:
mapping(uint256 => address) internal list;
uint256 internal listCount;
function getList() public override view returns (address[] memory) {
address[] memory assets = new address[](listCount);
for (uint256 i = 0; i < listCount; i++) {
assets[i] = list[i];
}
return assets;
}
In react App.js:
import Contract_from './data/abi/Contract_.json'; // Contract_ is a placer
var contract = new web3.eth.Contract(Contract_, address_given_on_deploy);
var contractAddress = await contract .options.address; // correctly outputs
var list= await contract.methods.getList().call();
console.log("list", list);
As you see, this doesn't return the values from the method. What am I doing wrong here?
For any reason and may be likely the issue, here's my config:
require("#nomiclabs/hardhat-waffle");
// openzeppelin adds
require("#nomiclabs/hardhat-ethers");
require('#openzeppelin/hardhat-upgrades');
//abi
require('hardhat-abi-exporter');
// 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 () => {
const accounts = await 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 = {
networks: {
hardhat: {
gas: 12000000,
blockGasLimit: 0x1fffffffffffff,
allowUnlimitedContractSize: true,
timeout: 1800000,
chainId: 1337
}
},
solidity: {
compilers: [
{
version: "0.8.0",
settings: {
optimizer: {
enabled: true,
runs: 1000
}
}
},
{
version: "0.8.2",
settings: {
optimizer: {
enabled: true,
runs: 1000
}
}
},
],
},
abiExporter: {
path: './frontend/src/data/abi',
clear: true,
flat: true,
only: [],
spacing: 2
}
}
__
I thought maybe i would try ethers.js since that is what i do my testing in but same issue.
For whatever reason, I can "get" the contracts, print the methods that belong to them, but I can't actually call the methods.
Here's my ethers.js brevity:
provider = new ethers.providers.Web3Provider(window.ethereum);
if(provider != null){
const _contract = new ethers.Contract(address, _Contract, provider);
var list= await _contract.getList().call();
console.log("list", list);
}
The error i get from this is:
Error: call revert exception (method="getList()", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.4.0)
I've tried numerous contracts in the protocol and same thing for each

Deploy contract to local hardhat node with forked Kovan chain

I am trying to write tests for my contract on Kovan network. In order to do so I am using the fork feature of hardhat and added the following to the hardhat.config.js file:
module.exports = {
solidity: "0.8.0",
defaultNetwork: "hardhat",
networks: {
hardhat: {
forking: {
url: INFURA_URL,
accounts: [`0x${PRIVATE_KEY}`]
}
}
}
};
INFURA_URL points to node on Kovan. PRIVATE_KEY is the key of an account on Kovan I would like to deploy with. This variables work well when I deploy to Kovan directly but not to forked node.
In my deployment script I do the following:
const [deployer] = await ethers.getSigners();
But my deployer is not the account that corresponds to the private key from config. It is a correct account when I deploy directly to Kovan.
Not sure why does this happen are forks of Kovan not supported on hardhat?
The way it worked for me was to forget Hardhat and rely entirely on ethers, for that I created a Signer specifying a private key and a provider like:
import { providers, Wallet } from 'ethers';
require('dotenv').config();
const main = async() => {
const providerLocal = new
providers.JsonRpcProvider("http://127.0.0.1:8545");
const owner = new Wallet(process.env.DEPLOYER_PRIVATE_KEY as string, providerLocal);
console.log(`The wallet ${owner.address} will deploy this contract`)
const MyContract = await ethers.getContractFactory('MyContract');
const mycontract = await MayContract.connect(owner).deploy();
}
main();

How to use Truffle with Solidity 6.0?

Reading official Truffle docs, I noticed Truffle not support Solidity 6.0
pragma solidity >=0.4.21 <0.6.0;
Are there any ways to use Truffle with Solidity 6.0?
Yes this works with this migration
pragma solidity >=0.4.21 <0.7.0;
contract Migrations {
address public owner;
uint public last_completed_migration;
constructor() public {
owner = msg.sender;
}
modifier restricted() {
if (msg.sender == owner) _;
}
function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}
}
And this compiler settings
compilers: {
solc: {
version: "^0.6.0",
And also need re-install Truffle to latest version
You can set the solidity version in the truffle config file.
module.exports = {
networks: {
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 7545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
},
compilers: {
solc: {
version: "0.6.0",
settings: {
optimizer: {
enabled: true, // Default: false
runs: 1000, // Default: 200
},
},
},
},
};