Ich habe ein Problem, die web3-Dokumentation in den readthedocs zu verstehen, insbesondere hier: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#id12
myContract.methods.myMethod([param1[, param2[, ...]]]).send(options[, callback])
Sendet eine Transaktion an den Smart Contract und führt seine Methode aus. Beachten Sie, dass dies den Smart-Contract-Status ändern kann.
Ich habe in Truffle einen Smart Contract wie diesen erstellt:
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;
}
}
Mit TestRPC funktionieren die folgenden Befehle 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'
Die folgenden Befehle funktionieren jedoch nicht, ich weiß nicht, wie ich den richtigen Aufruf tätigen soll:
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)
Funktioniert auch nicht für die Ereignisbehandlung
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)
Funktioniert aber gut wie versprochen
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'
Kannst du mir helfen? Danke
Truffle v3.4.11 verwendet seine eigene Bibliothek truffle-contract , um mit Verträgen zu interagieren. Es verwendet web3 v0.20 als Basis, hat aber seine eigene Syntax, zum Beispiel verwendet web3 v0.20 Callbacks, aber Truffle-Contract verwendet Promises.
Sie können nicht einfach die web3-Dokumentation direkt auf den Truffle-Contract anwenden, weder web3 v1.0 noch web3 v0.20.