dappuniversity / web3_examples

Learn Web3.js by Example With This Ethereum Blockchain Developer Crash Course!
157 stars 113 forks source link

second example: (node:91538) UnhandledPromiseRejectionWarning: Error: Invalid bytes string given: 0x #2

Open lebed2045 opened 5 years ago

lebed2045 commented 5 years ago
$ node test2.js
(node:91538) UnhandledPromiseRejectionWarning: Error: Invalid bytes string given: 0x
    at AbiCoder.decodeParameters (/Users/username/programming/learning_blockchain/web3_basic/node_modules/web3-eth-abi/dist/web3-eth-abi.cjs.js:64:15)
    at CallContractMethod.afterExecution (/Users/username/programming/learning_blockchain/web3_basic/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:740:32)
    at CallContractMethod._callee$ (/Users/username/programming/learning_blockchain/web3_basic/node_modules/web3-core-method/dist/web3-core-method.cjs.js:354:35)
    at tryCatch (/Users/username/programming/learning_blockchain/web3_basic/node_modules/regenerator-runtime/runtime.js:62:40)
    at Generator.invoke [as _invoke] (/Users/username/programming/learning_blockchain/web3_basic/node_modules/regenerator-runtime/runtime.js:288:22)
    at Generator.prototype.(anonymous function) [as next] (/Users/username/programming/learning_blockchain/web3_basic/node_modules/regenerator-runtime/runtime.js:114:21)
    at asyncGeneratorStep (/Users/username/programming/learning_blockchain/web3_basic/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/Users/username/programming/learning_blockchain/web3_basic/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:182:7)
(node:91538) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:91538) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
jmlon commented 5 years ago

Same problem here. I am using web3 version 1.0.0-beta.48 and node.js version v10.11.0 on windows 10.

jmlon commented 5 years ago

I found a solution by chaining the .then(onSuccess, onFailure), like this: contract.methods.totalSupply().call() .then( (res)=>{ console.log(res) }, (error)=>{ console.log(error) } )

Karandeep-singh-saini commented 5 years ago

@jmlon i am still getting Error: Invalid bytes string given: 0x . Have you done any other changes also?

jmlon commented 5 years ago

@jmlon i am still getting Error: Invalid bytes string given: 0x . Have you done any other changes also?

Yes, when converting the private key into a buffer, I had to trim off the '0x' from the start of the string.

Karandeep-singh-saini commented 5 years ago

@jmlon i am still getting Error: Invalid bytes string given: 0x . Have you done any other changes also?

Yes, when converting the private key into a buffer, I had to trim off the '0x' from the start of the string.

Yes ,but it only required for sending sign Transaction. I am getting this error on calling any method on smartContract Eg: contract.methods.balanceOf(account1). Please ignore the Promise returned I have Handled that part.

I even tried using JSON_RPC eth_call to get result but i received {"jsonrpc":"2.0","id":1,"result":"0x"} as a response.

jmlon commented 5 years ago

@jmlon i am still getting Error: Invalid bytes string given: 0x . Have you done any other changes also?

Yes, when converting the private key into a buffer, I had to trim off the '0x' from the start of the string.

Yes ,but it only required for sending sign Transaction. I am getting this error on calling any method on smartContract Eg: contract.methods.balanceOf(account1). Please ignore the Promise returned I have Handled that part.

I even tried using JSON_RPC eth_call to get result but i received {"jsonrpc":"2.0","id":1,"result":"0x"} as a response.

I don't remember any other problems with this example. Here is the example with the corrections that worked for me

`const Web3 = require('web3') const infura = "https://mainnet.infura.io/" const web3 = new Web3(infura) const omgAddress = '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07' const omgABI = [{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_releaseTime","type":"uint256"}],"name":"mintTimelocked","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]

const contract = new web3.eth.Contract(omgABI, omgAddress)

contract.methods.name().call().then( res=>console.log(res), err=>console.log(err) ) contract.methods.symbol().call().then( res=>console.log(res), err=>console.log(err) ) contract.methods.totalSupply().call().then( res=>console.log(res), err=>console.log(err) ) contract.methods.mintingFinished().call().then( res=>console.log(res), err=>console.log(err) ) contract.methods.balanceOf("0x417e631d2934704a73b8ff6f0adf16d9dbaff2a7").call().then( res=>console.log(res), err=>console.log(err) ) `

vimuktj commented 5 years ago

I am getting this error after calling the totalSupply function err