Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Sending contract method through infura and wallet account doesn't work #2569

Closed
vshab opened this issue Mar 25, 2019 · 6 comments
Closed

Sending contract method through infura and wallet account doesn't work #2569

vshab opened this issue Mar 25, 2019 · 6 comments
Labels
Bug Addressing a bug

Comments

@vshab
Copy link

vshab commented Mar 25, 2019

Description

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

Expected behavior

contract.methodName.send() should send transaction.

Actual behavior

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"}
}

Steps to reproduce the behavior

// 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()

Versions

  • web3.js: 1.0.0-beta.50
  • nodejs: 11.12.0
  • browser: Chrome 73
  • ethereum node: Infura websocket

Tell me if you need more info. :)

@nivida nivida added the support label Mar 25, 2019
@nivida
Copy link
Contributor

nivida commented Mar 25, 2019

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.

@vshab
Copy link
Author

vshab commented Mar 25, 2019

The same code works on beta37 though. Hmm... okay, I’ll try to make an example

@vshab
Copy link
Author

vshab commented Mar 25, 2019

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();

@vshab
Copy link
Author

vshab commented Mar 25, 2019

@nivida examples above demonstrate the issue. You will need infura key and primary key with at least 1eth on rinkeby network.

@nivida nivida added Needs Clarification Requires additional input and removed support labels Mar 25, 2019
@nivida
Copy link
Contributor

nivida commented Mar 25, 2019

Will test and fix it if required tomorrow.

@nivida nivida added Bug Addressing a bug and removed Needs Clarification Requires additional input labels Mar 26, 2019
@nivida nivida mentioned this issue Mar 26, 2019
12 tasks
@nivida nivida closed this as completed Mar 27, 2019
@nivida nivida mentioned this issue Mar 28, 2019
12 tasks
@rubenrdp
Copy link

rubenrdp commented Apr 7, 2019

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Addressing a bug
Projects
None yet
Development

No branches or pull requests

3 participants