Web3.js: Send transaction to smart contract don't parse "from" option

Created on 7 Feb 2019  路  7Comments  路  Source: ChainSafe/web3.js

Expected behavior

Send transaction to smart contract method

Actual behavior

Throws a strange error

Steps to reproduce the behavior

const contractInstance = new web3.eth.Contract(abi,address );
 return new Promise((resolve, reject) => {
            contractInstance .methods.addData(data).send({ from: "0x3b0282D31174B927e102bA71d80dF34F4e596Fc4", gas:300000},(error, result) => {
                if (!error) {
                    resolve(result)
                } else {
                    reject(error);
                }
            })
        })

Error Logs

`TypeError: Cannot create property 'from' on string '0xf9056d808509502f9000830493e09439126b2a8801909a7b379e503c5ee270ff700e0580b905043a03cbec0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000340000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000440000000000000000000000000000000000000000000000000000000000000001044442d313534393535393330313536320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000074d61726f75656e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000644626f75626100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006504153503031000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000073033313435363400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000653454a3031320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000354554e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e4d454c4c49544120444a4552424100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034652410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a30332d30372d3230313700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c627572656175456e747265650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c9000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000034555520000000000000000000000000000000000000000000000000000000000820bfaa06b1d4370a2ab603dbf83b51c342643bff86a997b2ffbd24877a1dda7a8623a46a05ead679cbd26ed8f4353e1aeb4df24290334fbb0a7caed20dd81bdef96735c0e'

`

Versions

npm v6.4.1
node v10.13.0
web3js v1.0.0-beta.41
OS windows 10

bug

All 7 comments

Could you add the complete code example? thanks :)

This is a complete example, the instance is created in an other class ,
NB: getData works fine!

const abi = [] // [..] abi of contract
const address = "0x39126b2a8801909a7b379e503c5ee270ff700e05";

class MyContract {

    constructor (web3) {
        this.instance = new web3.eth.Contract(abi,address);
    }


    getData (ref) {
        return this.instance.methods.getData(ref).call()
    }


    setData (data) {
        return new Promise((resolve, reject) => {
            this.instance.methods.setData(data).send({from: "0x3b0282D31174B927e102bA71d80dF34F4e596Fc4", gas:300000},(error, result) => {
                if (!error) {
                    resolve(result)
                } else {
                    reject(error);
                }
            })

        })
    }
}

module.exports = MyContract;

This class Web3API that provide web3 instance and add accounts:

const Web3 = require('web3');

class Web3API {

    constructor() {
        this.web3 =  new Web3(new Web3.providers.WebsocketProvider('ws://localhost:3000'))
    }

      getInstance() {
        return this.web3;
    }

    addAccountToWallet (account) {
        this.web3.eth.accounts.wallet.add(account);
    }


}

// default account
const instance = new Web3API();
const privateKey = '0xb10dffbae01f7caeb15465daa0cd4110e363e5fc0e88e10bf214a9a367d19e49';
let account = instance.web3.eth.accounts.privateKeyToAccount(privateKey);
instance.addAccountToWallet(account);

module.exports = instance;

And this is the main class:

const Web3API = require('./Web3API');
const MyContract = require('./MyContract');

const web3 = Web3API.getInstance();
const myContract = new MyContract(web3);

myContract.getData("01").then(console.log) // it works!

myContract.setData({some: "json data"}).then(console.log).catch(console.log)// it throws the error

The same code works with web3js v1.0.0-beta.37

same error in 1.0.0-beta.46
weird...

I debugged this issue and found some inconsistency in types of web3-core-method.

The execute function puts rawTransaction into parameters here, but in beforeExecution, the inputTransactionFormatter accepts to receive a txObject. Not sure it needs to call the beforeExecution at all.
FYI @nivida

same error on 1.0.0-beta.46
downgraded to 1.0.0-beta.37 and it works

I guess this code has merged into web3.js 1.0.0-beta.47 incorrectly (maybe the fix has overwritten by #2367).
The same problem happens on web3.js 1.0.0-beta.47

-- updated --
Oh I just tried putting defaultOptions on send(defaultOptions) instead of Contract(abi, network, defaultOptions) and that worked.
It seems #2393 fixed the issue on send() function, but not on Contract constructor.

-- updated --
now it stuck with a bug: https://github.com/ethereum/web3.js/issues/2447 lol

Was this page helpful?
0 / 5 - 0 ratings