Can't send contract method when using infura and wallet account. It used to work in beta.37 but fails now.
I suspect that EthSendTransactionMethod doesn't see accounts in wallet. The check here:
https://github.com/ethereum/web3.js/blob/1.0/packages/web3-core-method/src/methods/transaction/EthSendTransactionMethod.js#L156 compares moduleInstance.accounts but the wallet adds accounts into accounts.wallet.accounts:
https://github.com/ethereum/web3.js/blob/1.0/packages/web3-eth-accounts/src/models/Wallet.js#L34
contract.methodName.send() should send transaction.
contract.methodName.send() fails with
WebSocket connection to 'wss://rinkeby.infura.io/ws/v3/_token_' failed: Received a broken close frame containing a reserved status code.
And this is what's happens in websocket:
{
"jsonrpc": "2.0",
"id": 127,
"error": {"code": -32601, "message": "The method eth_sendTransaction does not exist/is not available"}
}
// Create infuara provider
const provider = new WebsocketProvider(
`wss://${INFURA_WEBSOCKET_DOMAIN}/ws/v3/${INFURA_API_KEY}`,
);
const web3 = new Web3(provider);
// Add account from private key
web3.eth.accounts.wallet.create(0, Web3Utils.randomHex(32));
const account = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY);
web3.eth.accounts.wallet.add(account);
// Setup contract
const contract = new web3.eth.Contract(abi, CONTRACT_ADDRESS);
// Send method
contract.methods.method.send()
Tell me if you need more info. :)
I think the error clearly told you what the issue is:
"error": {"code": -32601, "message": "The method eth_sendTransaction does not exist/is not available"}
The referenced check is correct.
The same code works on beta37 though. Hmm... okay, I鈥檒l try to make an example
This works (success):
1.0.0-beta.37
const Web3 = require("web3");
const { randomHex } = require("web3-utils");
const ji =
'[{"constant":false,"inputs":[{"name":"name","type":"string"}],"name":"setNick","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"verdict","type":"bool"}],"name":"resolveWord","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"text","type":"string"}],"name":"addWord","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"names","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getContractBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"words","outputs":[{"name":"text","type":"string"},{"name":"bet","type":"uint256"},{"name":"owner","type":"address"},{"name":"resolved","type":"bool"},{"name":"verdict","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getTotalWords","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"ad","type":"address"}],"name":"getNickByAddress","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"uint256"}],"name":"getWordById","outputs":[{"name":"","type":"string"},{"name":"","type":"address"},{"name":"","type":"uint256"},{"name":"","type":"bool"},{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"treasure_","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]';
const address = "0x9eDEEb93207AB62978b8D24E4ED582A13ff564EA";
const abi = JSON.parse(ji);
const infuraKey = "YOUR_INFURA_KEY";
// Create infuara provider
const provider = new Web3.providers.WebsocketProvider(
`wss://rinkeby.infura.io/ws/v3/${infuraKey}`
);
const web3 = new Web3(provider);
// Add account from private key
web3.eth.accounts.wallet.create(0, randomHex(32));
const pk = "YOUR_ACCOUNT_PRIVATE_KEY_WITH_1_ETH";
const account = web3.eth.accounts.privateKeyToAccount(pk);
web3.eth.accounts.wallet.add(account);
// Setup contract
const contract = new web3.eth.Contract(abi, address);
async function run() {
const word = "yyy";
const from = web3.eth.accounts.wallet[0].address;
const nonce = await web3.eth.getTransactionCount(from, "pending");
let gas = await contract.methods
.addWord(word)
.estimateGas({ from, gas: "10000000", value: "100000000000000000" });
gas = Math.round(gas * 1.5);
try {
const result = await contract.methods.addWord(word).send({
gas,
from,
nonce,
value: "100000000000000000"
});
console.log("success", result);
} catch (e) {
console.log("error", e);
}
}
run();
This doesn't (error):
1.0.0-beta.50
const Web3 = require("web3");
const { WebsocketProvider } = require("web3-providers");
const { randomHex } = require("web3-utils");
const ji =
'[{"constant":false,"inputs":[{"name":"name","type":"string"}],"name":"setNick","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"verdict","type":"bool"}],"name":"resolveWord","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"text","type":"string"}],"name":"addWord","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"names","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getContractBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"words","outputs":[{"name":"text","type":"string"},{"name":"bet","type":"uint256"},{"name":"owner","type":"address"},{"name":"resolved","type":"bool"},{"name":"verdict","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getTotalWords","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"ad","type":"address"}],"name":"getNickByAddress","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"uint256"}],"name":"getWordById","outputs":[{"name":"","type":"string"},{"name":"","type":"address"},{"name":"","type":"uint256"},{"name":"","type":"bool"},{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"treasure_","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]';
const address = "0x9eDEEb93207AB62978b8D24E4ED582A13ff564EA";
const abi = JSON.parse(ji);
const infuraKey = "YOUR_INFURA_KEY";
// Create infuara provider
const provider = new WebsocketProvider(
`wss://rinkeby.infura.io/ws/v3/${infuraKey}`
);
const web3 = new Web3(provider);
// Add account from private key
web3.eth.accounts.wallet.create(0, randomHex(32));
const pk = "YOUR_ACCOUNT_PRIVATE_KEY_WITH_1_ETH";
const account = web3.eth.accounts.privateKeyToAccount(pk);
web3.eth.accounts.wallet.add(account);
// Setup contract
const contract = new web3.eth.Contract(abi, address);
async function run() {
const word = "yyy";
const from = web3.eth.accounts.wallet.accounts[0].address;
const nonce = await web3.eth.getTransactionCount(from, "pending");
let gas = await contract.methods
.addWord(word)
.estimateGas({ from, gas: "10000000", value: "100000000000000000" });
gas = Math.round(gas * 1.5);
try {
const result = await contract.methods.addWord(word).send({
gas,
from,
nonce,
value: "100000000000000000"
});
console.log("success", result);
} catch (e) {
console.log("error", e);
}
}
run();
@nivida examples above demonstrate the issue. You will need infura key and primary key with at least 1eth on rinkeby network.
Will test and fix it if required tomorrow.
I have the same problem in 1.0.0-beta.36. I guess the issue is related to how is the send() method implemented internally, right? I mean it should generate a signed transaction and invoke eth_sendRawTransaction rather than trying to use eth_sendTransaction.
Would moving to 1.0.0-beta.51 solve the issue? But where can I get the built version from?
Thanks