Web3.js: `TypeError: Unknown encoding: 10` on ERC20.allowance call

Created on 25 Jan 2017  路  9Comments  路  Source: ChainSafe/web3.js

I am using the latest web3.js 0.18.2 and getting the below error when calling and ERC20.allowance function (asynchronously), might be my fault in the end but the output should be more debug friendly:

buffer.js:479
          throw new TypeError('Unknown encoding: ' + encoding);
          ^

TypeError: Unknown encoding: 10
    at Buffer.slowToString (buffer.js:479:17)
    at Buffer.toString (buffer.js:492:27)
    at toBigNumber (C:\Users\sbuergel\Desktop\DREL\node_modules\web3\lib\utils\utils.js:367:33)
    at Object.toTwosComplement (C:\Users\sbuergel\Desktop\DREL\node_modules\web3\lib\utils\utils.js:378:21)
    at SolidityTypeAddress.formatInputInt [as _inputFormatter] (C:\Users\sbuergel\Desktop\DREL\node_modules\web3\lib\sol
idity\formatters.js:40:38)
    at SolidityTypeAddress.SolidityType.encode (C:\Users\sbuergel\Desktop\DREL\node_modules\web3\lib\solidity\type.js:18
8:17)
    at C:\Users\sbuergel\Desktop\DREL\node_modules\web3\lib\solidity\coder.js:91:29
    at Array.map (native)
    at SolidityCoder.encodeParams (C:\Users\sbuergel\Desktop\DREL\node_modules\web3\lib\solidity\coder.js:90:34)
    at SolidityFunction.toPayload (C:\Users\sbuergel\Desktop\DREL\node_modules\web3\lib\web3\function.js:70:52)

Most helpful comment

Thanks, web3 1.0 will have better error reporting. You can watch the progress on the 1.0 branch

All 9 comments

Ok the obvious first: This was my bad!

This bug seems to surface when passing an address to a smart-contract as an object and not as a string. This happened because I had a deploy script writing the output address to a file and then reading it via:

var contractAddress = fs.readFileSync('deployEth/out/contract.adr')

which is an object and not a string, timply adding .toString() to the line above solves the issue. Still maybe worth considering to treat this with a somewhat more meaningful error message than just having buffer crash. I suggest always checking that the input type is a string.

Thanks, web3 1.0 will have better error reporting. You can watch the progress on the 1.0 branch

The new error would look like:

Error: Provided address "[object Object]" is invalid, the capitalization checksum test failed, or its an indrect IBAN address which can't be converted.

Would have made my day (literally!) - thanks and looking forward to the new version!

haha yeah im on it

There are a lot of other improvements.
Now i need to write docs mainly. You can find the new docs here: http://web3js.readthedocs.io/en/1.0/

@frozeman I have a huge favor to ask regarding local wallets hooked to web3. I'm employing infura as an http provider and trying to link an old geth wallet. The approach I use to assign default account as web3.eth.defaultAccount = web3.eth.accounts.wallet[0]['address']; seems inelegant, as well as possibly incorrect.

const web3 = new Web3(new Web3.providers.HttpProvider(rinkebyURI));

const compiledContract = require('../build/myContract.json');

const gethWallet = require('../test_wallet/UTC--2018-01-01T01-11-11.001115111Z--0xxxxxxxxxxxxxxxxxxxxxxxxxd.json');

web3.eth.accounts.wallet.decrypt(gethWallet, 'password');
web3.eth.accounts.wallet.create(2); 
web3.eth.defaultAccount = web3.eth.accounts.wallet[0]['address'];

const address1 = web3.eth.accounts.wallet[1]['address'];
const address2 = web3.eth.accounts.wallet[2]['address'];

Also, I would love to know when to use web3.eth.accounts as opposed to web3.eth.accounts.wallet. Many thanks.

I didn't test the web3.eth.defaultAccount = web3.eth.accounts.wallet[0]['address'];, but should work.
web3.eth.accounts, allows you to generate account and sign tx or messages. web3.eth.accounts.wallet Allows you to store accounts in a "wallet", so that when you use from: ... in tx in web3.js it will use the local account and sign in the browser. You can also use the index like: from: 2 (should work, please test index 0 :) )

Does this answer your questions?

Thanks for the reply @frozeman . I did test all day and try all sorts of voodoo, but all contract creation efforts failed! Simple value transfers did work strangely... I can move this to a new issue if you like. Here is what I did:

 const Web3 = require('web3');
        const web3 = new Web3(new Web3.providers.HttpProvider(infuraURI));
        const compiledContract = require('../build/myContract.json');
        const gethWallet = require('../test_wallet/UTC--2018-01-01T01-01-01.001705111Z--aaa7aaaaaaa77aa7aaaaaaaaaaaaaaaaaaaaaaaaaa.json');

        web3.eth.accounts.wallet.decrypt(gethWallet, 'password');

        web3.eth.accounts.wallet.create(2); //create two more keys

    // problem here:
        // want to be able to use contract methods instead of sendTransaction, but can't. Feature request:) Ok moving on...
        // web3.eth.defaultAccount = web3.eth.accounts.wallet[0]['address'];

        const address1 = web3.eth.accounts.wallet[1]['address'];
        const address2 = web3.eth.accounts.wallet[2]['address'];

web3Func = async () => {
          myContractEncoded = await new web3.eth.Contract(JSON.parse(compiledContract.interface))
            .deploy({ data: compiledContract.bytecode, arguments: [web3.utils.toWei('10', 'ether'), address1, address2] })
            .encodeABI();

    await web3.eth.sendTransaction({ from: 0, data: myContractEncoded, value: '1', gas: '1500000' })
    .on('receipt', receipt => console.log(receipt)) });
}; 

web3Func();
Was this page helpful?
0 / 5 - 0 ratings