0

I'm trying to read an ownersNum public uint from my contract.

I instantiate in node:

l = console.log;

var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
let c = new web3.eth.Contract(abi, '0x6f4cf704c86da0afeb3cc28cda77f3c69d6e5e0d');

let buyParams = {sender: '0x08df113cf3cec557397e471452d38b84d912e4be',value: 1000000000000000};

l(c.methods.buyKey.call(buyParams));
let on = c.methods.ownersNum;
l(on.call());

The last log prints out a huge function:

{ [Function: Contract]
  setProvider: [Function],
  defaultAccount: null,
  defaultBlock: 'latest',
  currentProvider: [Getter/Setter],
  _requestManager:
   RequestManager {
     provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
     providers:
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     subscriptions: {} },
  givenProvider: null,
  providers:
   { WebsocketProvider: [Function: WebsocketProvider],
     HttpProvider: [Function: HttpProvider],
     IpcProvider: [Function: IpcProvider] },
  _provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
  BatchRequest: [Function: bound Batch],
  extend:
   { [Function: ex]
     formatters:
      { inputDefaultBlockNumberFormatter: [Function: inputDefaultBlockNumberFormatter],
        inputBlockNumberFormatter: [Function: inputBlockNumberFormatter],
        inputCallFormatter: [Function: inputCallFormatter],
        inputTransactionFormatter: [Function: inputTransactionFormatter],
        inputAddressFormatter: [Function: inputAddressFormatter],
        inputPostFormatter: [Function: inputPostFormatter],
        inputLogFormatter: [Function: inputLogFormatter],
        inputSignFormatter: [Function: inputSignFormatter],
        outputBigNumberFormatter: [Function: outputBigNumberFormatter],
        outputTransactionFormatter: [Function: outputTransactionFormatter],
        outputTransactionReceiptFormatter: [Function: outputTransactionReceiptFormatter],
        outputBlockFormatter: [Function: outputBlockFormatter],
        outputLogFormatter: [Function: outputLogFormatter],
        outputPostFormatter: [Function: outputPostFormatter],
        outputSyncingFormatter: [Function: outputSyncingFormatter] },
     utils:
      { _fireError: [Function: _fireError],
        _jsonInterfaceMethodToString: [Function: _jsonInterfaceMethodToString],
        randomHex: [Function: randomHex],
        _: [Object],
        BN: [Object],
        isBN: [Function: isBN],
        isBigNumber: [Function: isBigNumber],
        isHex: [Function: isHex],
        isHexStrict: [Function: isHexStrict],
        sha3: [Object],
        keccak256: [Object],
        soliditySha3: [Function: soliditySha3],
        isAddress: [Function: isAddress],
        checkAddressChecksum: [Function: checkAddressChecksum],
        toChecksumAddress: [Function: toChecksumAddress],
        toHex: [Function: toHex],
        toBN: [Function: toBN],
        bytesToHex: [Function: bytesToHex],
        hexToBytes: [Function: hexToBytes],
        hexToNumberString: [Function: hexToNumberString],
        hexToNumber: [Function: hexToNumber],
        toDecimal: [Function: hexToNumber],
        numberToHex: [Function: numberToHex],
        fromDecimal: [Function: numberToHex],
        hexToUtf8: [Function: hexToUtf8],
        hexToString: [Function: hexToUtf8],
        toUtf8: [Function: hexToUtf8],
        utf8ToHex: [Function: utf8ToHex],
        stringToHex: [Function: utf8ToHex],
        fromUtf8: [Function: utf8ToHex],
        hexToAscii: [Function: hexToAscii],
        toAscii: [Function: hexToAscii],
        asciiToHex: [Function: asciiToHex],
        fromAscii: [Function: asciiToHex],
        unitMap: [Object],
        toWei: [Function: toWei],
        fromWei: [Function: fromWei],
        padLeft: [Function: leftPad],
        leftPad: [Function: leftPad],
        padRight: [Function: rightPad],
        rightPad: [Function: rightPad],
        toTwosComplement: [Function: toTwosComplement] },
     Method: [Function: Method] },
  _ethAccounts:
   Accounts {
     currentProvider: [Getter/Setter],
     _requestManager: RequestManager { provider: [Object], providers: [Object], subscriptions: {} },
     givenProvider: null,
     providers:
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     _provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
     setProvider: [Function],
     _ethereumCall:
      { getId: [Object],
        getGasPrice: [Object],
        getTransactionCount: [Object] },
     wallet:
      Wallet {
        _accounts: [Circular],
        length: 0,
        defaultKeyName: 'web3js_wallet' } } }
{ call: { [Function: bound _executeMethod] request: [Function: bound _executeMethod] },
  send: { [Function: bound _executeMethod] request: [Function: bound _executeMethod] },
  encodeABI: [Function: bound _encodeMethodABI],
  estimateGas: [Function: bound _executeMethod],
  arguments: [],
  _method:
   { constant: false,
     inputs: [],
     name: 'buyKey',
     outputs: [],
     payable: true,
     stateMutability: 'payable',
     type: 'function',
     signature: '0xe95db6f9' },
  _parent:
   Contract {
     currentProvider: [Getter/Setter],
     _requestManager: RequestManager { provider: [Object], providers: [Object], subscriptions: {} },
     givenProvider: null,
     providers:
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     _provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
     setProvider: [Function],
     BatchRequest: [Function: bound Batch],
     extend:
      { [Function: ex]
        formatters: [Object],
        utils: [Object],
        Method: [Function: Method] },
     clearSubscriptions: [Function],
     options: { address: [Getter/Setter], jsonInterface: [Getter/Setter] },
     defaultAccount: [Getter/Setter],
     defaultBlock: [Getter/Setter],
     methods:
      { owners: [Function: bound _createTxObject],
        '0x025e7c27': [Function: bound _createTxObject],
        'owners(uint256)': [Function: bound _createTxObject],
        ownersNum: [Function: bound _createTxObject],
        '0x195fa903': [Function: bound _createTxObject],
        'ownersNum()': [Function: bound _createTxObject],
        buyKey: [Function: bound _createTxObject],
        '0xe95db6f9': [Function: bound _createTxObject],
        'buyKey()': [Function: bound _createTxObject] },
     events: { allEvents: [Function: bound ] },
     _address: '0x6f4Cf704c86DA0afeB3cC28Cda77F3c69D6E5E0d',
     _jsonInterface: [ [Object], [Object], [Object] ] },
  _ethAccounts: undefined }
{ call: { [Function: bound _executeMethod] request: [Function: bound _executeMethod] },
  send: { [Function: bound _executeMethod] request: [Function: bound _executeMethod] },
  encodeABI: [Function: bound _encodeMethodABI],
  estimateGas: [Function: bound _executeMethod],
  arguments: [],
  _method:
   { constant: true,
     inputs: [],
     name: 'ownersNum',
     outputs: [ [Object] ],
     payable: false,
     stateMutability: 'view',
     type: 'function',
     signature: '0x195fa903' },
  _parent:
   Contract {
     currentProvider: [Getter/Setter],
     _requestManager: RequestManager { provider: [Object], providers: [Object], subscriptions: {} },
     givenProvider: null,
     providers:
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     _provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
     setProvider: [Function],
     BatchRequest: [Function: bound Batch],
     extend:
      { [Function: ex]
        formatters: [Object],
        utils: [Object],
        Method: [Function: Method] },
     clearSubscriptions: [Function],
     options: { address: [Getter/Setter], jsonInterface: [Getter/Setter] },
     defaultAccount: [Getter/Setter],
     defaultBlock: [Getter/Setter],
     methods:
      { owners: [Function: bound _createTxObject],
        '0x025e7c27': [Function: bound _createTxObject],
        'owners(uint256)': [Function: bound _createTxObject],
        ownersNum: [Function: bound _createTxObject],
        '0x195fa903': [Function: bound _createTxObject],
        'ownersNum()': [Function: bound _createTxObject],
        buyKey: [Function: bound _createTxObject],
        '0xe95db6f9': [Function: bound _createTxObject],
        'buyKey()': [Function: bound _createTxObject] },
     events: { allEvents: [Function: bound ] },
     _address: '0x6f4Cf704c86DA0afeB3cC28Cda77F3c69D6E5E0d',
     _jsonInterface: [ [Object], [Object], [Object] ] },
  _ethAccounts: undefined }

which is unexpected.

In Truffle, instantiating and calling this variable returns a map with the correct value, an integer, inside of it.

I am also confused about buyParams which is a payable. I can't call sendTransaction, method not found, but I can use call..

How do I read a state variable?

  • l(c.methods.buyKey.call(buyParams)); -> l(c.methods.buyKey(byuParams).call({options)); – Florian Castelain Jan 12 '18 at 3:00
  • where options is optional? – quantumpotato Jan 12 '18 at 3:12
  • "invalid parameters for buyKey, got 1 expected 0 – quantumpotato Jan 12 '18 at 3:13
  • Check this: web3js.readthedocs.io/en/1.0/… – Florian Castelain Jan 12 '18 at 3:18
  • I try let buyParams = {sender: '0x08df113cf3cec557397e471452d38b84d912e4be',value: 1000000000000000}; c.methods.buyKey().call(buyParams); c.methods.ownersNum.call(function(a,b) { l(a); l(b); }); , nothing prints @Andromelus – quantumpotato Jan 12 '18 at 3:26
1

Using web3js, you need to pass the options in the call function and use a promise or a callback to handle the response, for exemple:

    contract.methods.buyKey().call({from: 0x, <somemore params if required>},
function(error, result){
    if(error)
    {
    do stuff
    } else {
    //your result is the result variable
    }

    })

With a promise:

contract.methods.buyKey().call({from: 0x, <somemore params if required>})
.then(function(result){
console.log(result);
});

Edit: Someone proposed an edit with send instead of call. Here we are talking about reading a variable from a constant function. So we need a call because it does not change the EVM (unless the OP forgot to specify it, and then, yes, we need a transaction)

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.