2

I'm trying to call a contract method from web3js. The contract is deployed in a local ganache. This is my full code

// Instantiate contract
const provider = new Web3.providers.HttpProvider("http://127.0.0.1:7545");
const web3 = new Web3(provider);
const contract = new web3.eth.Contract(conf.contract.abi, "0x7167868c5e3161D9e6D0d8be564F05a432B02F5e", { gasPrice: 1, gas: 4500000 });

// Import account and add to wallet
const account = web3.eth.accounts.privateKeyToAccount("0xa415318b6ead0170a5dab11bcdc58d4bdbf7fdcbbdb7af19f85f5da985df1d13");
// console.log(account.address) >> "0xa9C0540014C4D53E783aA94a0aE17a90b02eAe51"
web3.eth.accounts.wallet.add(account);

// Estimate gas
var estimatedGas  = await contract.methods.createRecord().estimateGas({ from: account.address });
// console.log(estimatedGas) >> 26328

// Call contract method: ERROR
var result = await contract.methods.createRecord().send({ from: account.address, gas: estimatedGas });

The last line raises this error

TypeError: Cannot create property 'from' on string '0xf90247800182fbd8947167868c5e3161d9e6d0d8be564f05a432b02f5e80b901e45170f1fa00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000b736572766572312e636f6d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007312e302e302e310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000093137322e302e302e31000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000074d4143303030310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020111000000000000000000000000000000000000000000000000000000000000822d45a042ba0d363180c9ca85a10fe919536909e2ca78f0e5dd3409c43db902c85ac09ba024c54001ace92edf9438fc91cbef70b7caf5aeba68f8048b39a1018c3fc47fe5'
    at Object.inputTransactionFormatter (d:\...\node_modules\web3-core-helpers\dist\web3-core-helpers.cjs.js:78:21)
    at SendContractMethod.beforeExecution (d:\...\node_modules\web3-core-method\dist\web3-core-method.cjs.js:1075:44)
    at SendContractMethod.execute (d:\...\node_modules\web3-core-method\dist\web3-core-method.cjs.js:1006:12)
    at d:\...\node_modules\web3-core-method\dist\web3-core-method.cjs.js:1100:85
    at process._tickCallback (internal/process/next_tick.js:68:7)

Any clue about the error? I've got very similar code running ok in other projects, but here there seems to be something wrong and I can't find it.

Thanks

  • dont know why the string from accout.address is so long, it should be 40-length hex only. Can you check it! – Ha ĐANG Feb 27 at 8:23
  • I think that string is not the address, is the object where it's trying to assign the "from" property. I suppose is the transaction object. Instead of being an object is a string. That could be the root of the error, but I don't know why. – klautern Feb 27 at 8:34
  • account.address must be string address base on this document web3js.readthedocs.io/en/1.0/web3-eth-accounts.html#id3 – Ha ĐANG Feb 27 at 8:36
  • Account.address is a string, and is ok. The long string you refer to is not the address, is where is trying to create the property 'from'. At least is what I understand from the error text – klautern Feb 27 at 8:42
  • I mean we need to figure out why the string to create from on the error text is so long, it's very strange – Ha ĐANG Feb 27 at 8:46
0

This issue from web3js may help

https://github.com/ethereum/web3.js/issues/2376

version 36 works well to, and if you have a typescript base, you need to use 36. But there will be a new release soon i hope

  • 1
    Thanks Ha ĐANG, that seems to be the issue. Not yet resolved, but still identified. – klautern Feb 27 at 10:47
  • welcome, please let us know when you can resolve the problem! – Ha ĐANG Feb 27 at 10:54
  • 1
    Downgrading web3js from 1.0.0-beta.46 to 1.0.0-beta.36 it works – klautern Feb 28 at 10:23
0

web3.eth.accounts.privateKeyToAccount(privateKey) creates an account object from privateKey.

Its possible to read the address from the account object.

Did you provide the correct the private key?

  • yeah, should confirm the privkey and the error happen on estimateGas(); not from send() as @klautem thought – Ha ĐANG Feb 27 at 9:08
  • @HaĐANG you should also confirm about the account object. – Crissi Mariam Robert Feb 27 at 9:11
  • 1
    I think the account object is correct by this document web3js.readthedocs.io/en/1.0/web3-eth-accounts.html#id3 – Ha ĐANG Feb 27 at 9:12
  • 1
    As they are ganache addresses I think there's no problem printing them here. Debugging the code I get: the imported private key with privateKeyToAccount is "0xa415318b6ead0170a5dab11bcdc58d4bdbf7fdcbbdb7af19f85f5da985df1d13" and generated account address (account.address) is "0xa9C0540014C4D53E783aA94a0aE17a90b02eAe51" (wich matchs the ganache auto-generated address. – klautern Feb 27 at 9:13
  • @HaĐANG Did you able to get the account adddress – Crissi Mariam Robert Feb 27 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.