2

I have a problem understanding the web3 documentation at the readthedocs especially here: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#id12

myContract.methods.myMethod([param1[, param2[, ...]]]).send(options[, callback])

Will send a transaction to the smart contract and execute its method. Note this can alter the smart contract state.

I have created a smart contract like this in Truffle:

pragma solidity ^0.4.15;

contract SimpleStorage {
    bytes32 public x;

    function setX(bytes32 _x) {
        x = _x;
    }

    function getX() returns (bytes32) { //just for the sake of it...
        return x;
    }
}

Using TestRPC, the following commands work in Truffle Console:

truffle(development)> SimpleStorage.deployed().then(function(instance){contract=instance})
truffle(development)> contract.setX('aaa')
{ tx: '0xf35b0d4a840d7049f560777e47fdabdfdafdaa339dc9b17a346a14f575ba7bf9',
  receipt:
   { transactionHash: '0xf35b0d4a840d7049f560777e47fdabdfdafdaa339dc9b17a346a14f575ba7bf9',
     transactionIndex: 0,
     blockHash: '0x82408eb303b7537b03c5f3a95ec41f38b26180cf7093e9a5f403c90bed957b5e',
     blockNumber: 5,
     gasUsed: 41842,
     cumulativeGasUsed: 41842,
     contractAddress: null,
     logs: [] },
  logs: [] }
truffle(development)> contract.getX()
{ tx: '0x58a9488bf46aa92799937cca64b6507c20c4886962a6ff1476e63369ef212241',
  receipt:
   { transactionHash: '0x58a9488bf46aa92799937cca64b6507c20c4886962a6ff1476e63369ef212241',
     transactionIndex: 0,
     blockHash: '0x3e044efc580d63154a013a3534b831ea242ae42ab7f05a95d833a848c3c416c2',
     blockNumber: 6,
     gasUsed: 21537,
     cumulativeGasUsed: 21537,
     contractAddress: null,
     logs: [] },
  logs: [] }
truffle(development)> contract.getX.call()
'0x6161610000000000000000000000000000000000000000000000000000000000'

However the following commands do not work, I don't know how to make the proper call:

truffle(development)> contract.setX.send('bbb')
TypeError: contract.setX.send is not a function
    at evalmachine.<anonymous>:1:15
    at ContextifyScript.Script.runInContext (vm.js:35:29)
    at Object.exports.runInContext (vm.js:67:17)
    at TruffleInterpreter.interpret (C:\Users\gilan\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:213786:17)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:513:10)
    at emitOne (events.js:96:13)
    at REPLServer.emit (events.js:188:7)
    at REPLServer.Interface._onLine (readline.js:239:10)
truffle(development)> contract.setX('bbb').send({from:web3.eth.accounts[0]})
TypeError: contract.setX(...).send is not a function
    at evalmachine.<anonymous>:1:25
    at ContextifyScript.Script.runInContext (vm.js:35:29)
    at Object.exports.runInContext (vm.js:67:17)
    at TruffleInterpreter.interpret (C:\Users\gilan\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:213786:17)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:513:10)
    at emitOne (events.js:96:13)
    at REPLServer.emit (events.js:188:7)
    at REPLServer.Interface._onLine (readline.js:239:10)

Not working too for event handling

truffle(development)> contract.setX('bbb').on("transactionHash",function(hash) { console.log(hash) } )
TypeError: contract.setX(...).on is not a function
    at evalmachine.<anonymous>:1:25
    at ContextifyScript.Script.runInContext (vm.js:35:29)
    at Object.exports.runInContext (vm.js:67:17)
    at TruffleInterpreter.interpret (C:\Users\gilan\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:213786:17)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:513:10)
    at emitOne (events.js:96:13)
    at REPLServer.emit (events.js:188:7)
    at REPLServer.Interface._onLine (readline.js:239:10)

But works fine on promise

truffle(development)> contract.setX('bbb').then(console.log)
{ tx: '0xa453ec572e92d42d65b111447a8055dc7a2fcf59c18f487a001ce7352d0f4f0b',
  receipt:
   { transactionHash: '0xa453ec572e92d42d65b111447a8055dc7a2fcf59c18f487a001ce7352d0f4f0b',
     transactionIndex: 0,
     blockHash: '0xbeede1cabc7613a9d8c484017e1890dd9d08a4365013deb3481fb08ad1e4bf2d',
     blockNumber: 13,
     gasUsed: 26842,
     cumulativeGasUsed: 26842,
     contractAddress: null,
     logs: [] },
  logs: [] }
truffle(development)> contract.getX.call()
'0x6262620000000000000000000000000000000000000000000000000000000000'

Can you help me? Thanks

1 Answer 1

1

Truffle v3.4.11 uses its own library truffle-contract to interact with contracts. It uses web3 v0.20 as a base, but it has its own syntax, for example web3 v0.20 uses callbacks but truffle-contract uses promises.

You cannot just apply web3 documentation to truffle-contract directly, neither web3 v1.0 nor web3 v0.20.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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