Hello,
I created a simple erc20 token on remix. When a call the approve function in web3 js i’m getting error “The method eth_sendTransaction does not exist/is not available”.
The approve function works well on remix. Compiler version 0.8.0,
pragma solidity >=0.7.0 <0.9.0;
import “@openzeppelin/contracts/token/ERC20/ERC20.sol”;
import “@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol”;
contract SimpleToken is ERC20, ERC20Burnable {
constructor () ERC20("SimpleToken", "Sim") {
_mint(msg.sender, 1000000000 * (10 ** uint256(decimals())));
}
}
My js code looks like below
Web3Connect: async () => {
try {
const provider = await App.Provider()
if (provider) {
App.web3 = new Web3(provider)
} else {
App.web3 = new Web3(new Web3.providers.HttpProvider(App.infuraNetwork + App.infuraToken))
}
} catch (error) {
alert("Enable to access to Metamask")
console.log(error)
}
}
Provider: async () => {
const {
ethereum
} = window
if (ethereum) {
try {
await ethereum.enable()
return ethereum
} catch (error) {
//throw
}
}
//legacy provider
const {
web3
} = window
if (web3 && web3.currentProvider) {
return web3.currentProvider
}
return null
}
----approve code
await token.methods.approve(
contractaddress,
amount
).send({ from: account })
I tried using the latest version of web3 js still got the same error. I’m using ropsten infura
Any help would be appreciated
You will need to connect first to a wallet that can Send Transaction.
You may have skipped the provider step?
You’ll need to first get some accounts like so
var accountsFromMetaMask = await window.ethereum.send('eth_requestAccounts');
Read some web3js tutorials
https://cypherpunks-core.github.io/ethereumbook/appdx-web3js-tutorial.html
Sorry didn’t mentioned that I have already done the provider step. I will edit post now
Please post the full code. I don’t see the step where you get the ABI. You will need to get the contract’s ABI first before you can use its function. Otherwise the code will not know what to do.
Please use proper post formatting. Use ``` for code blocks.
I got it working now. I found out the issue was due to Metamask deprecated function.
@EththeNoob can you explain how you solved this issue? I'm having the same problem while using MetaMask.
@Yukigeshiki sorry for the late reply, I changed the ethereum.enable() part of code to
await window.ethereum.request({
method: "eth_requestAccounts"
});
return ethereum
I hope that helps
@EththeNoob @Yukigeshiki Hi, I have a simple script that also encountered the same problem. How to solve it please
import Web3 from 'web3'
let contractAddress = ''contractAddress"
let privateKey = 'myKey'
let account1 = 'account1'
let account2 = 'account2'
const web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/v3/xxxxxxxxx'))
let myContract = new web3.eth.Contract(abi, contractAddress);
let func = await myContract.methods.transferFrom(account2,account1,1).send({ from: account2 }, function (err, res) {
if (err) {
console.log("An error occured", err)
return
}
console.log("Hash of the transaction: " + res)
})
console.log(await func)
env:
"web3": "^1.7.4"
"node": v18.6.0
Hi @allen_guo,
In my case, the issue was with the provider instance i.e ethereum.enable() is deprecated. So had to change like the snippet below
From
if (ethereum) {
try
{
await ethereum.enable() return ethereum
}
catch (error) { //throw } }
To
if (window.ethereum) {
const ethereum = window.ethereum;
try {
await window.ethereum.request({
method: "eth_requestAccounts"
});
return ethereum
} catch (error) {
//throw
}
}
I have similar code
async function trySwap(){
// Grant the allowance target (the 0x Exchange Proxy) an allowance to spend our tokens. Note that this is a txn that incurs fees.
const tx = await ERC20TokenContract.methods.approve(
swapQuoteJSON.allowanceTarget,
maxApproval,
)
.send({ from: takerAddress })
.then(tx => {
console.log("tx: ", tx)
});
}
for wallet connect using this
if (typeof window.ethereum !== "undefined") {
try {
console.log("connecting");
await ethereum.request({ method: "eth_requestAccounts" });
} catch (error) {
console.log(error);
}
const myacc = await ethereum.request({ method: "eth_accounts" });
document.getElementById("login_button").innerHTML = "Connected to " + myacc[0];
// const accounts = await ethereum.request({ method: "eth_accounts" });
document.getElementById("swap_button").disabled = false;
} else {
document.getElementById("login_button").innerHTML = "Please install MetaMask";
}
On approve method getting this Error: Returned error: The method eth_sendTransaction does not exist/is not available. @EththeNoob @Yukigeshiki can any one help?