How to inject Binance Wallet into Web3 instead of Metamask? - ethereum

I started using Binance Chain Wallet in my dapp, so I can now use:
import { BscConnector } from '#binance-chain/bsc-connector'
export const bsc = new BscConnector({
supportedChainIds: [56, 97] // later on 1 ethereum mainnet and 3 ethereum ropsten will be supported
})
// invoke method on bsc e.g.
await bsc.activate();
await bsc.getAccount();
await bsc.getChainId();
but in API docs it says to do some chain operations I need to iject :
The biggest difference between Binance Chain Wallet and MetaMask is we inject BinanceChain rather than ethereum (or web3) to the web page. So user could keep two extensions at the same time.
BinanceChain.request({method: "eth_sign", params: ["address", "message"])
with metamsk I use
ethereum.request(...)
can you explain to me how to do this?
BinanceChain obj is not declared :)

So I figured out that these steps are necessarily :
// create instance of bsc wallet
const bsc = new BscConnector({
supportedChainIds: [56, 97, 1] // later on 1 ethereum mainnet and 3 ethereum ropsten will be supported
})
// get provider out of this instance:
const bsc_provider = await bsc.getProvider()
provider = new ethers.providers.Web3Provider(bsc_provider)
// from here u can use all binance wallet functions:
provider.provider.switchNetwork('eth-mainnet')
see docs:
binance wallet docs

Related

Ethers.js metamask request limit exceeded

I’m trying to connect to contracts on BSC, it worked fine for half a year, now it’s starting to give errors, it’s with contract.on, when reading something from the contract it works fine
To connect the provider and the signer to work with contracts, I used the default entry for connecting to the metamask from the ethers.js documentation:
const provider = new ethers.providers.Web3Provider(window.ethereum); const signer = provider.getSigner();
started getting errorenter image description here
tried doing this:
await window.ethereum.enable() const provider = new ethers.providers.Web3Provider(window.ethereum); const signer = provider.getSigner();
I also tried to connect via rps, but it didn’t happen at all what I needed:
export const provider = new ethers.providers.JsonRpcProvider(settings.bsc.rpcNodeUrl); export const wallet = new ethers.Wallet(settings.wallet.privateKey, provider) export const signer = wallet.connect(provider);
You need to get a paid RPC node.
Binance has cut down the APIs and limits of free nodes.
Even better run your own node.

Automatically pointing contract address to solidity interface

I'm trying to use a interface on remix IDE and my only issue is that I have to copy and paste the contract's address.
Is there a way that I can point it automatically?
ty.
In Remix IDE, you need to copy and paste the newly deployed address manually.
With frameworks, such as Hardhat, you can retrieve the address of the deployed contract in the code and then pass it to the other contract.
In the example below, we're passing the address of First instance to Second's constructor, using the Hardhat JS framework.
const firstFactory = await ethers.getContractFactory("First");
const first = await firstFactory.deploy();
await first.deployed();
const secondFactory = await ethers.getContractFactory("Second");
const second = await secondFactory.deploy(first.address);
await second.deployed();
pragma solidity ^0.8;
contract First {}
contract Second {
constructor(address firstAddress) {}
}

Override the gasPrice in ethers.js contract interaction

How can I set the gasPrice in a contract interaction using ethers.js? I'm trying to override the gasPrice in the code below:
let txPromise = contract.populateTransaction.runAdventureVRF(0, false, { gasPrice: 800000 })
walletSigner.sendTransaction(txPromise)
and i'm receiving the error transaction underpriced. If i try to log txPromise.overrides it is undefined, which makes me think the gas price is never being set.
docs
additional code
const provider = new ethers.providers.AlchemyProvider('matic', process.env.ALCHEMY_API_KEY)
const wallet = new ethers.Wallet(process.env.PK)
const abi = '[{"inputs":[{"internalType":"uint256","name":"_teamId","type":"uint256"},{"internalType":"bool","name":"_energy","type":"bool"}],"name":"runAdventureVRF","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"}]'
const contract = new ethers.Contract(address, abi, provider);
let walletSigner = wallet.connect(provider)
One solution is to add the gas price as an override to your transaction:
walletSigner.sendTransaction(txPromise, {gasPrice: ethers.utils.parseUnits('100', 'gwei'), gasLimit: 1000000});
This is the only solution I could find as getGasPrice() and estimateGas() were returning a empty json array. I don't know if it has to do with the provider (alchemy) or why those are failing.
The specific syntax to override the gas price is here:
Ethers Override Transaction Gas Price Manually
It's also in the following Ethers docs, but not as clear (if you don't know the syntax):
Ethers Contract Functions Send
At the time of this post, the default gas value of all my transactions is 1.5 gwei which is nowhere near enough on the matic network. This will also hang up the transaction indefinitely until you send a new transaction with the same nonce value. Even if you resolve your gas issue, until you deal with the hung transaction, other transactions will stack up behind it.
If you just want to increase gasLimit by a certain amount, you can make a helper function to do so:
const increaseGasLimit = (estimatedGasLimit: BigNumber) => {
return estimatedGasLimit.mul(130).div(100) // increase by 30%
}
And use it like this:
const estimatedGas = await contract.estimateGas.method(args)
const tx = await contract.method(args, { gasLimit: increaseGasLimit(estimatedGas) })

How to generate arbitrary wallet seeded with eth in hardhat tests using ethers.js?

I'm currently trying to run a test in hardhat/waffle that requires hundreds of unique wallets to call a contract, using new ethers.Wallet.createRandom(). However, none of these wallets are supplied with eth, so I can't call/send transactions with them.
What would be the simplest/most effective way to supply an arbitrary amount of randomly generated wallets with eth? Thanks!
I think you'll probably want to use the hardhat network method hardhat_setBalance, the docs use an example like this:
await network.provider.send("hardhat_setBalance", [
"<ACCOUNT ADDRESS>",
"0x1000", # 4096 wei
]);
I'm not developing in javascript though, but I've been doing something similar in python using web3.py and eth-account with code like this:
from web3 import Web3
from eth_account import Account
chain = Web3(HTTPProvider("http://127.0.0.1:8545"))
acct = Account.create('<RANDOM VALUE>')
address = Web3.toChecksumAddress(acct.address)
print(chain.eth.get_balance(address)) # 0 wei
# add a balance of eth tokens to the address
chain.provider.make_request("hardhat_setBalance", [address, "0x1000"])
print(chain.eth.get_balance(address)) # 4096 wei
Hardhat allows to seed a custom amount of prefunded addresses using the accounts.count config option of the built-in hardhat network.
Example:
module.exports = {
networks: {
hardhat: {
accounts: {
count: 1000
}
}
}
}
Docs: https://hardhat.org/hardhat-network/reference/#config
The new ethers.Wallet.createRandom() function only creates an account from a random private key in the context of the JS application, but it doesn't communicate with the provider (Hardhat in your case).
saw this answer, and i think it solves the problem:
https://github.com/ethers-io/ethers.js/issues/686
const wallet = ethers.Wallet.createRandom().connect(provider);

How do I solve the Smart Contract error "returned values aren't valid"?

I am trying to read basic information from a smart contract using web3.js (GRAPH Token):
https://etherscan.io/address/0xc944e90c64b2c07662a292be6244bdf05cda44a7#code
This is my super simple react web3.js setup:
import Web3 from 'web3';
...
useEffect(() => {
const start = async () => {
const web3 = new Web3('https://bsc-dataseed1.binance.org:443')
const abi = [{"inputs":[{"internalType":"uint256","name":"_initialSupply","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"}],"name":"MinterAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"}],"name":"MinterRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"}],"name":"NewOwnership","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"}],"name":"NewPendingOwnership","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"acceptOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"addMinter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"burn","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"burnFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"governor","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"isMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"nonces","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingGovernor","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"},{"internalType":"uint8","name":"_v","type":"uint8"},{"internalType":"bytes32","name":"_r","type":"bytes32"},{"internalType":"bytes32","name":"_s","type":"bytes32"}],"name":"permit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"removeMinter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceMinter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newGovernor","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}]
const address = '0xc944E90C64B2c07662A292be6244BDf05Cda44a7'
const contract = new web3.eth.Contract(abi, address)
console.log(contract);
const i = await contract.methods.getName().call()
console.log(i); // => ERROR
}
start()
}, [])
And it throws error:
Error: 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.
This is a known and misleading error which usually means that the address or the contract abi is invalid but I verified it is correct.
I researched this error for two days but didn't manage to solve this issue... any suggestions?
You're trying to interact with an Ethereum contract, using a BSC (Binance Smart Chain) Web3 provider (in your case https://bsc-dataseed1.binance.org:443). This results in trying to call the getName() function on the BSC address (which doesn't contain any contract).
Ethereum and BSC are different networks, unrelated to each other.
Solution: Use an Ethereum mainnet provider (for example Infura is widely used).