0

I'm facing issue of callback is not a function in web3js 0.20.7 when i call the method of sendRawTransaction to deploy smart contract with web3.

When i used the same method just to call deployed smart contract functions so it works fine it just throwing error when deploying contract web3js v0.20.7.

As this is necessary to use the same version so i can't upgrade it to 1.0.0. Although it works fine on version 1.0.0 but i need it in 0.20.7.

Please see the code:

const express = require('express');
const router = express.Router();
const Tx = require("ethereumjs-tx");
const Web3 = require('web3');
const web3 = new Web3();
const validateIdentityInput = require('../validations/identity')

const {
interface,
bytecode
} = require('./compile');


web3.setProvider(
new web3.providers.HttpProvider(
    "https://ropsten.infura.io/t2utzUdkSyp5DgSxasQX"
)
);

router.post('/deploy', async function (req, res) {

let privateKey = req.body.from_private_key;
let fromAddress = req.body.from_address;

const {
    errors,
    isValid
} = validateIdentityInput(req.body);


const balance = await web3.eth.getBalance(fromAddress);

// Check Validation
if (!isValid) {
    return res.status(400).json(errors);
} else if (parseInt(balance) == 0) {
    return res.status(400).json("You have unsufficient balance to deploy the smart contract")
}
const sleep = (milliseconds) => {
    return new Promise(resolve => setTimeout(resolve, milliseconds))
}

web3.eth.defaultAccount = fromAddress;

let count = await web3.eth.getTransactionCount(fromAddress);

let gasPrice = await web3.eth.getGasPrice();
let gasLimit = 5000000;

// const result = await new web3.eth.Contract(JSON.parse(interface));
let result = web3.eth.contract(JSON.parse(interface));

const deployData = result.new.getData({
    data: '0x' + bytecode
});

let rawTransaction = {
    "nonce": web3.toHex(count),
    "from": fromAddress,
    "gasPrice": web3.toHex(gasPrice),
    "gasLimit": web3.toHex(gasLimit),
    "data": deployData,
    "chainId": 0x03
};
privateKey = Buffer.from(privateKey, "hex");
let tx = new Tx(rawTransaction);

tx.sign(privateKey);
let serializedTx = tx.serialize();

web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
    if (!err)
        console.log(hash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385"
});

And Response is :

/home/hamza/Desktop/CaseManging/Deployment_Scrypt/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["readFi

TypeError: callback is not a function
    at /home/hamza/Desktop/CaseManging/Deployment_Scrypt/node_modules/web3/lib/web3/property.js:119:13
    at /home/hamza/Desktop/CaseManging/Deployment_Scrypt/node_modules/web3/lib/web3/requestmanager.js:89:9
    at XMLHttpRequest.request.onreadystatechange (/home/hamza/Desktop/CaseManging/Deployment_Scrypt/node_modules/web3/lib/web3/httpprovider.js:129:7)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/hamza/Desktop/CaseManging/Deployment_Scrypt/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (/home/hamza/Desktop/CaseManging/Deployment_Scrypt/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (/home/hamza/Desktop/CaseManging/Deployment_Scrypt/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (/home/hamza/Desktop/CaseManging/Deployment_Scrypt/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
    at emitNone (events.js:110:20)
    at IncomingMessage.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1045:12)
  • are you missing a '}' at the end code ? – Majd TL Jun 14 at 13:28
  • No i dont think so – Hamza Yasin Jun 14 at 19:18
  • I see that you are awaiting on some web3 methods calls. That doesn't work because web3 v0.20 doesn't support for promises. – Ismael Jun 16 at 6:10
  • oh yes exactly like ISmael said, like this await web3.eth.getBalance(fromAddress) and some others – Majd TL Jun 16 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.