[Ethereum] Error: The data field must be HEX encoded data

blockchainconnectionsdappstestrpcweb3js

I am trying to make a simple dApp by using a tutorial. I am using the following command ;

npm install ganache-cli web3

so that we can use web3 to get fake ethereum nodes/accounts by connecting it to localhost:8545 by using following commands in node command prob;

Web3 = require('web3')

web3 = new Web3("http://localhost:8545")

web3.eth.getAccounts(console.log)

The last command gives me 10 fake ethereum accounts as follows;

Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events:
      [Object: null prototype] {
        removeListener: [Function: updateExceptionCapture],
        newListener: [Function: updateExceptionCapture],
        error: [Function: debugDomainError] },
     _eventsCount: 3,
     _maxListeners: undefined,
     members: [],
     [Symbol(kWeak)]: WeakReference {} } }
false [ '0xc9d5c49cD217F029EfE35Ffb1267653c254d6acc',
  '0xa3d63dF29FA51Ef04c599D3D5C00eC2249DC8876',
  '0x1425a8a2E2Ebb65CaBa1E35420f96a7b2edb1009',
  '0x97CCe4de30Eb9c5191D3F84eF41b3B20d34be1A8',
  '0xA919898D9dB7BbF942a66894068D56e0e5e89f5c',
  '0x406AEE80806E082847b9901E89174935056A0812',
  '0x08e6129D50d62f14406a56b7CCFAd58eD11D91ba',
  '0x6352b6DE7a3130D373da16282098bA15dD0C2170',
  '0xA227CCa9C89074bDCa0BC96B74B9ae61CD81aa30',
  '0xFe7301768E90b63611Aa78396157363b623a82aC' ]

After that i strored the abi and byte-code of my solidity code in the two variable by using following commands;

bytecode = fs.readFileSync('Voting_sol_Voting.bin').toString()

abi = JSON.parse(fs.readFileSync('Voting_sol_Voting.abi').toString())

I used the above abi to create a deployed contract and also I created list of candidates for voting by using following commands;

deployedContract = new web3.eth.Contract(abi)

listOfCandidates = ['Rama', 'Nick', 'Jose']

But when i used one of the above accounts as follows it gives me error that The data field must be HEX encoded data ;

deployedContract.deploy({
...   data: bytecode,
...   arguments: [listOfCandidates.map(name => web3.utils.asciiToHex(name))]
... }).send({
...   from: `0xc9d5c49cd217f029efe35ffb1267653c254d6acc`,
...   gas: 1500000,
...   gasPrice: web3.utils.toWei('0.00003', 'ether')
... }).then((newContractInstance) => {
...   deployedContract.options.address = newContractInstance.options.address
...   console.log(newContractInstance.options.address)
... });
Thrown:
Error: The data field must be HEX encoded data.
    at txInputFormatter (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\web3-core-helpers\dist\web3-core-helpers.cjs.js:50:11)
    at Object.inputTransactionFormatter (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\web3-core-helpers\dist\web3-core-helpers.cjs.js:74:14)
    at ContractDeployMethod.beforeExecution (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\web3-core-method\dist\web3-core-method.cjs.js:1115:44)
    at ContractDeployMethod.beforeExecution (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\web3-core-method\dist\web3-core-method.cjs.js:1141:92)
    at ContractDeployMethod.beforeExecution (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:744:88)
    at ContractDeployMethod.execute (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\web3-core-method\dist\web3-core-method.cjs.js:1045:12)
    at ContractDeployMethod.execute (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\web3-core-method\dist\web3-core-method.cjs.js:1177:89)
    at MethodsProxy.executeMethod (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:546:21)
    at Function.ContractMethod.send (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:505:29)

Connection to localhost:8545 is open in another command prob.

Edit:

here are values of bytecode and abi;

console.log(bytecode)

{
        "linkReferences": {},
        "object": "608060405234801561001057600080fd5b506040516103e23803806103e28339818101604052602081101561003357600080fd5b81019080805164010000000081111561004b57600080fd5b8281019050602081018481111561006157600080fd5b815185602082028301116401000000008211171561007e57600080fd5b5050929190505050806001908051906020019061009c9291906100a3565b5050610115565b8280548282559060005260206000209081019282156100df579160200282015b828111156100de5782518255916020019190600101906100c3565b5b5090506100ec91906100f0565b5090565b61011291905b8082111561010e5760008160009055506001016100f6565b5090565b90565b6102be806101246000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c80632f265cf71461005c578063392e66781461009e5780637021939f146100e4578063b13c744b14610126578063cc9ab26714610168575b600080fd5b6100886004803603602081101561007257600080fd5b8101908080359060200190929190505050610196565b6040518082815260200191505060405180910390f35b6100ca600480360360208110156100b457600080fd5b81019080803590602001909291905050506101c4565b604051808215151515815260200191505060405180910390f35b610110600480360360208110156100fa57600080fd5b810190808035906020019092919050505061021a565b6040518082815260200191505060405180910390f35b6101526004803603602081101561013c57600080fd5b8101908080359060200190929190505050610232565b6040518082815260200191505060405180910390f35b6101946004803603602081101561017e57600080fd5b8101908080359060200190929190505050610253565b005b60006101a1826101c4565b6101aa57600080fd5b600080838152602001908152602001600020549050919050565b600080600090505b60018054905081101561020f5782600182815481106101e757fe5b90600052602060002001541415610202576001915050610215565b80806001019150506101cc565b50600090505b919050565b60006020528060005260406000206000915090505481565b6001818154811061023f57fe5b906000526020600020016000915090505481565b61025c816101c4565b61026557600080fd5b6001600080838152602001908152602001600020600082825401925050819055505056fea265627a7a723058201051d58b7a6fc11adab0915a1ac238953bcbd3451c3febc2002fefb2c8e06f4664736f6c63430005090032",
        "opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x40 MLOAD PUSH2 0x3E2 CODESIZE SUB DUP1 PUSH2 0x3E2 DUP4 CODECOPY DUP2 DUP2 ADD PUSH1 0x40 MSTORE PUSH1 0x20 DUP2 LT ISZERO PUSH2 0x33 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 MLOAD PUSH5 0x100000000 DUP2 GT ISZERO PUSH2 0x4B JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP3 DUP2 ADD SWAP1 POP PUSH1 0x20 DUP2 ADD DUP5 DUP2 GT ISZERO PUSH2 0x61 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 MLOAD DUP6 PUSH1 0x20 DUP3 MUL DUP4 ADD GT PUSH5 0x100000000 DUP3 GT OR ISZERO PUSH2 0x7E JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP POP SWAP3 SWAP2 SWAP1 POP POP POP DUP1 PUSH1 0x1 SWAP1 DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 PUSH2 0x9C SWAP3 SWAP2 SWAP1 PUSH2 0xA3 JUMP JUMPDEST POP POP PUSH2 0x115 JUMP JUMPDEST DUP3 DUP1 SLOAD DUP3 DUP3 SSTORE SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 DUP2 ADD SWAP3 DUP3 ISZERO PUSH2 0xDF JUMPI SWAP2 PUSH1 0x20 MUL DUP3 ADD JUMPDEST DUP3 DUP2 GT ISZERO PUSH2 0xDE JUMPI DUP3 MLOAD DUP3 SSTORE SWAP2 PUSH1 0x20 ADD SWAP2 SWAP1 PUSH1 0x1 ADD SWAP1 PUSH2 0xC3 JUMP JUMPDEST JUMPDEST POP SWAP1 POP PUSH2 0xEC SWAP2 SWAP1 PUSH2 0xF0 JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST PUSH2 0x112 SWAP2 SWAP1 JUMPDEST DUP1 DUP3 GT ISZERO PUSH2 0x10E JUMPI PUSH1 0x0 DUP2 PUSH1 0x0 SWAP1 SSTORE POP PUSH1 0x1 ADD PUSH2 0xF6 JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST SWAP1 JUMP JUMPDEST PUSH2 0x2BE DUP1 PUSH2 0x124 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x4 CALLDATASIZE LT PUSH2 0x57 JUMPI PUSH1 0x0 CALLDATALOAD PUSH1 0xE0 SHR DUP1 PUSH4 0x2F265CF7 EQ PUSH2 0x5C JUMPI DUP1 PUSH4 0x392E6678 EQ PUSH2 0x9E JUMPI DUP1 PUSH4 0x7021939F EQ PUSH2 0xE4 JUMPI DUP1 PUSH4 0xB13C744B EQ PUSH2 0x126 JUMPI DUP1 PUSH4 0xCC9AB267 EQ PUSH2 0x168 JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH2 0x88 PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH2 0x72 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x196 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH2 0xCA PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH2 0xB4 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x1C4 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 ISZERO ISZERO ISZERO ISZERO DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH2 0x110 PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH2 0xFA JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x21A JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH2 0x152 PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH2 0x13C JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x232 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH2 0x194 PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH2 0x17E JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x253 JUMP JUMPDEST STOP JUMPDEST PUSH1 0x0 PUSH2 0x1A1 DUP3 PUSH2 0x1C4 JUMP JUMPDEST PUSH2 0x1AA JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH1 0x0 DUP1 DUP4 DUP2 MSTORE PUSH1 0x20 ADD SWAP1 DUP2 MSTORE PUSH1 0x20 ADD PUSH1 0x0 KECCAK256 SLOAD SWAP1 POP SWAP2 SWAP1 POP JUMP JUMPDEST PUSH1 0x0 DUP1 PUSH1 0x0 SWAP1 POP JUMPDEST PUSH1 0x1 DUP1 SLOAD SWAP1 POP DUP2 LT ISZERO PUSH2 0x20F JUMPI DUP3 PUSH1 0x1 DUP3 DUP2 SLOAD DUP2 LT PUSH2 0x1E7 JUMPI INVALID JUMPDEST SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 ADD SLOAD EQ ISZERO PUSH2 0x202 JUMPI PUSH1 0x1 SWAP2 POP POP PUSH2 0x215 JUMP JUMPDEST DUP1 DUP1 PUSH1 0x1 ADD SWAP2 POP POP PUSH2 0x1CC JUMP JUMPDEST POP PUSH1 0x0 SWAP1 POP JUMPDEST SWAP2 SWAP1 POP JUMP JUMPDEST PUSH1 0x0 PUSH1 0x20 MSTORE DUP1 PUSH1 0x0 MSTORE PUSH1 0x40 PUSH1 0x0 KECCAK256 PUSH1 0x0 SWAP2 POP SWAP1 POP SLOAD DUP2 JUMP JUMPDEST PUSH1 0x1 DUP2 DUP2 SLOAD DUP2 LT PUSH2 0x23F JUMPI INVALID JUMPDEST SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 ADD PUSH1 0x0 SWAP2 POP SWAP1 POP SLOAD DUP2 JUMP JUMPDEST PUSH2 0x25C DUP2 PUSH2 0x1C4 JUMP JUMPDEST PUSH2 0x265 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH1 0x1 PUSH1 0x0 DUP1 DUP4 DUP2 MSTORE PUSH1 0x20 ADD SWAP1 DUP2 MSTORE PUSH1 0x20 ADD PUSH1 0x0 KECCAK256 PUSH1 0x0 DUP3 DUP3 SLOAD ADD SWAP3 POP POP DUP2 SWAP1 SSTORE POP POP JUMP INVALID LOG2 PUSH6 0x627A7A723058 KECCAK256 LT MLOAD 0xd5 DUP12 PUSH27 0x6FC11ADAB0915A1AC238953BCBD3451C3FEBC2002FEFB2C8E06F46 PUSH5 0x736F6C6343 STOP SDIV MULMOD STOP ORIGIN ",
        "sourceMap": "111:1549:0:-;;;827:95;8:9:-1;5:2;;;30:1;27;20:12;5:2;827:95:0;;;;;;;;;;;;;;;13:2:-1;8:3;5:11;2:2;;;29:1;26;19:12;2:2;827:95:0;;;;;;19:11:-1;14:3;11:20;8:2;;;44:1;41;34:12;8:2;71:11;66:3;62:21;55:28;;123:4;118:3;114:14;159:9;141:16;138:31;135:2;;;182:1;179;172:12;135:2;219:3;213:10;331:9;325:2;311:12;307:21;289:16;285:44;282:59;261:11;247:12;244:29;233:116;230:2;;;362:1;359;352:12;230:2;0:373;;827:95:0;;;;;;902:14;886:13;:30;;;;;;;;;;;;:::i;:::-;;827:95;111:1549;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;:::o;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;"
}
undefined

console.log(abi)

[ { constant: true,
    inputs: [ [Object] ],
    name: 'totalVotesFor',
    outputs: [ [Object] ],
    payable: false,
    stateMutability: 'view',
    type: 'function',
    funcName: 'totalVotesFor(bytes32)',
    signature: '0x2f265cf7' },
  { constant: true,
    inputs: [ [Object] ],
    name: 'validCandidate',
    outputs: [ [Object] ],
    payable: false,
    stateMutability: 'view',
    type: 'function',
    funcName: 'validCandidate(bytes32)',
    signature: '0x392e6678' },
  { constant: true,
    inputs: [ [Object] ],
    name: 'votesReceived',
    outputs: [ [Object] ],
    payable: false,
    stateMutability: 'view',
    type: 'function',
    funcName: 'votesReceived(bytes32)',
    signature: '0x7021939f' },
  { constant: true,
    inputs: [ [Object] ],
    name: 'candidateList',
    outputs: [ [Object] ],
    payable: false,
    stateMutability: 'view',
    type: 'function',
    funcName: 'candidateList(uint256)',
    signature: '0xb13c744b' },
  { constant: false,
    inputs: [ [Object] ],
    name: 'voteForCandidate',
    outputs: [],
    payable: false,
    stateMutability: 'nonpayable',
    type: 'function',
    funcName: 'voteForCandidate(bytes32)',
    signature: '0xcc9ab267' },
  { inputs: [ [Object] ],
    payable: false,
    stateMutability: 'nonpayable',
    type: 'constructor',
    constant: undefined,
    signature: 'constructor' } ]
undefined

Edit2:

I changed data:bytecode to data:'0x'+bytecode as follows which resolved the above error as follows;

deployedContract.deploy({
...   data: '0x'+bytecode,
...   arguments: [listOfCandidates.map(name => web3.utils.asciiToHex(name))]
... }).send({
...   from: '0x93ae3de492138e1ff186e2bf320483c188657785',
...   gas: 1500000,
...   gasPrice: web3.utils.toWei('0.00003', 'ether')
... }).then((newContractInstance) => {
...   deployedContract.options.address = newContractInstance.options.address
...   console.log(newContractInstance.options.address)
... });
Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events:
      [Object: null prototype] {
        removeListener: [Function: updateExceptionCapture],
        newListener: [Function: updateExceptionCapture],
        error: [Function: debugDomainError] },
     _eventsCount: 3,
     _maxListeners: undefined,
     members: [],
     [Symbol(kWeak)]: WeakReference {} } }

After that i added the address of deployedContract by following command;

deployedContract.options.address='0x93ae3de492138e1ff186e2bf320483c188657785'

then i am interacting with the contract which is giving me following error;

deployedContract.methods.totalVotesFor(web3.utils.asciiToHex('Rama')).call(console.log)

Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events:
      [Object: null prototype] {
        removeListener: [Function: updateExceptionCapture],
        newListener: [Function: updateExceptionCapture],
        error: [Function: debugDomainError] },
     _eventsCount: 3,
     _maxListeners: undefined,
     members: [],
     [Symbol(kWeak)]: WeakReference {} } }
> (node:7736) UnhandledPromiseRejectionWarning: Error: Node error: {"message":"Cannot read property 'from' of null","code":-32000,"data":{"stack":"TypeError: Cannot read property 'from' of null\n    at a.handleRequest (D:\\Notes\\BCS\\Semester 7\\FYP\\etherjs\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:52:516250)\n    at t (D:\\Notes\\BCS\\Semester 7\\FYP\\etherjs\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:52:380864)\n    at s.f.handleRequest (D:\\Notes\\BCS\\Semester 7\\FYP\\etherjs\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:52:509350)\n    at s.handleRequest (D:\\Notes\\BCS\\Semester 7\\FYP\\etherjs\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:52:507176)\n    at t (D:\\Notes\\BCS\\Semester 7\\FYP\\etherjs\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:52:380864)\n    at c.handleRequest (D:\\Notes\\BCS\\Semester 7\\FYP\\etherjs\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:52:515234)\n    at t (D:\\Notes\\BCS\\Semester 7\\FYP\\etherjs\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:52:380864)\n    at a.handleRequest (D:\\Notes\\BCS\\Semester 7\\FYP\\etherjs\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:52:514681)\n    at t (D:\\Notes\\BCS\\Semester 7\\FYP\\etherjs\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:52:380864)\n    at d._handleAsync (D:\\Notes\\BCS\\Semester 7\\FYP\\etherjs\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:52:380900)\n    at Timeout._onTimeout (D:\\Notes\\BCS\\Semester 7\\FYP\\etherjs\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:52:380325)\n    at ontimeout (timers.js:436:11)\n    at tryOnTimeout (timers.js:300:5)\n    at listOnTimeout (timers.js:263:5)\n    at Timer.processTimers (timers.js:223:10)","name":"TypeError"}}
    at Function.validate (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\web3-providers\dist\web3-providers.cjs.js:114:18)
    at HttpProvider._callee$ (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\web3-providers\dist\web3-providers.cjs.js:710:61)
    at tryCatch (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\regenerator-runtime\runtime.js:45:40)
    at Generator.invoke [as _invoke] (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\regenerator-runtime\runtime.js:271:22)
    at Generator.prototype.(anonymous function) [as next] (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\regenerator-runtime\runtime.js:97:21)
    at asyncGeneratorStep (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\@babel\runtime\helpers\asyncToGenerator.js:3:24)
    at _next (D:\Notes\BCS\Semester 7\FYP\etherjs\node_modules\@babel\runtime\helpers\asyncToGenerator.js:25:9)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:7736) 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: 10)

The bytecode contains only object this time:

console.log(bytecode)

608060405234801561001057600080fd5b506040516103e23803806103e28339818101604052602081101561003357600080fd5b81019080805164010000000081111561004b57600080fd5b8281019050602081018481111561006157600080fd5b815185602082028301116401000000008211171561007e57600080fd5b5050929190505050806001908051906020019061009c9291906100a3565b5050610115565b8280548282559060005260206000209081019282156100df579160200282015b828111156100de5782518255916020019190600101906100c3565b5b5090506100ec91906100f0565b5090565b61011291905b8082111561010e5760008160009055506001016100f6565b5090565b90565b6102be806101246000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c80632f265cf71461005c578063392e66781461009e5780637021939f146100e4578063b13c744b14610126578063cc9ab26714610168575b600080fd5b6100886004803603602081101561007257600080fd5b8101908080359060200190929190505050610196565b6040518082815260200191505060405180910390f35b6100ca600480360360208110156100b457600080fd5b81019080803590602001909291905050506101c4565b604051808215151515815260200191505060405180910390f35b610110600480360360208110156100fa57600080fd5b810190808035906020019092919050505061021a565b6040518082815260200191505060405180910390f35b6101526004803603602081101561013c57600080fd5b8101908080359060200190929190505050610232565b6040518082815260200191505060405180910390f35b6101946004803603602081101561017e57600080fd5b8101908080359060200190929190505050610253565b005b60006101a1826101c4565b6101aa57600080fd5b600080838152602001908152602001600020549050919050565b600080600090505b60018054905081101561020f5782600182815481106101e757fe5b90600052602060002001541415610202576001915050610215565b80806001019150506101cc565b50600090505b919050565b60006020528060005260406000206000915090505481565b6001818154811061023f57fe5b906000526020600020016000915090505481565b61025c816101c4565b61026557600080fd5b6001600080838152602001908152602001600020600082825401925050819055505056fea265627a7a723058201051d58b7a6fc11adab0915a1ac238953bcbd3451c3febc2002fefb2c8e06f4664736f6c63430005090032
undefined

Best Answer

Edit: Better solution is to compile .sol files using truffle and get bytecode of compiled JSON files.

You can use solc npm library to compile and get working bytecode variable:

const output = JSON.parse(solc.compile(JSON.stringify(input)));
const bytecode =
  output.contracts["./contracts/Lottery.sol"]["Lottery"].evm.bytecode.object;

input variable is going to look like this:

const input = {
  language: "Solidity",
  sources: {
    "./contracts/Lottery.sol": {
      content: `pragma solidity 0.5.13;
      contract Lottery {
          address public manager;
          address payable[] public players;

          constructor() public {
              manager = msg.sender;
          }

          function getPlayers() public view returns(address payable[] memory) {
              return players;
          }
      }`
    }
  },

  settings: {
    outputSelection: {
      "*": {
        "*": ["*"]
      }
    }
  }
};
  • input.sources[0] key is your contract directory relative to the executable .js file
  • input.sources['<contract dir>'].content is your contract stringified code (you should use better methods than just naive pasting to extract that code)
  • MUST use .object at the end of bytecode
Related Topic