1

I try to deploy a ERC721 contract. Which is from OpenZeppelin.

My contract constructor wants two input string fields.

constructor(string _name, string _symbol) public {
    name_ = _name;
    symbol_ = _symbol;

    // register the supported interfaces to conform to ERC721 via ERC165
    _registerInterface(InterfaceId_ERC721Enumerable);
    _registerInterface(InterfaceId_ERC721Metadata);
}

However, when I'm deploying the contract, it tells me that the parameters I set must be HEX encoded data.

This is the error message i got: /Users/amos_mac/721_server/node_modules/solc/soljson.js:1 (function (exports, require, module, __filename, __dirname) { var Module;if(!Module)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOverrides={};for(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}var ENVIRONMENT_IS_WEB=typeof window==="object";var ENVIRONMENT_IS_WORKER=typeof importScripts==="function";var ENVIRONMENT_IS_NODE=typeof process==="object"&&typeof require==="function"&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;var ENVIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WORKER;if(ENVIRONMENT_IS_NODE){if(!Module["print"])Module["print"]=function print(x){process["stdout"].write(x+"\n")};if(!Module["printErr"])Module["printErr"]=function printErr(x){process["stderr"].write(x+"\n")};var nodeFS=require("fs");var nodePath=require("path");Module["read"]=function read(filename,binary){filename=nodePath["normalize"](filename);var ret=nodeFS["readFileSync"](filename);if(!

Error: The data field must be HEX encoded data.

So I used web3.utils.asciiToHex(), but it doesn't help.

Here is my code of deploying contract:

MyToken.deploy({
    arguments: [[web3.utils.asciiToHex('Top 10 Students')],[web3.utils.asciiToHex('Top10')]]
}).send({
    from: address,
    gasPrice: gasPrice,
    gas: gas + 500000
}).then((instance) => {
    console.log("Contract mined at " + instance.options.address);
});

Please help! Thanks a lot!


After checking my code, I realized I added additional '0x' to my bytecode. Thats what cause the bug.

  • try replacing arguments: [[web3.utils.asciiToHex('Top 10 Students')],[web3.utils.asciiToHex('Top10')]] with arguments: [web3.utils.asciiToHex('Top 10 Students'),web3.utils.asciiToHex('Top10')] – Henk Aug 1 '18 at 8:35
  • Hello Henk, thanks for leaving comment. I tried arguments: [web3.utils.asciiToHex('Top 10 Students'),web3.utils.asciiToHex('Top10')], but I still get the same error. Error: The data field must be HEX encoded data. – Amos Wu Aug 1 '18 at 8:41
1

The documentation of web3.eth.Contract.deploy states that you have to provide the bytecode of the compiled smart contract along with the arguments:

MyToken.deploy({
    data: '0x[INSERT THE BYTECODE HERE]',
    // You can omit the asciiToHex calls, as the contstructor takes strings. 
    // Web3 will do the conversion for you.
    arguments: ['Top 10 Students','Top10'] 
}).send({
    from: address,
    gasPrice: gasPrice,
    gas: gas + 500000
}).then((instance) => {
    console.log("Contract mined at " + instance.options.address);
});
  • Henk, thanks again. I provided my bytecode while declaring my contract: let MyToken = new web3.eth.Contract(JSON.parse(abi), null, { data: '0x' + bytecode, }); Sorry that I didn't state this in my question. – Amos Wu Aug 1 '18 at 8:48
  • Does bytecode by itself start with 0x? I – Henk Aug 1 '18 at 8:52
  • Ahh! I never notice that I already added 0x to the bytecode! Thanks a lot! – Amos Wu Aug 1 '18 at 8:56

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.