I'm deploying my contracts via migrations and would like to show some info on the console about them just by getting data from these contracts and displaying it. I'm sending a deployed contract to the imported and shared function like this:

var ExportUtils = require("./ExportUtils.js");

var Storage = artifacts.require("./Storage");

module.exports = async function(deployer, network, accounts)
    await deployer.deploy(Storage);
    await ExportUtils.addDeployed(await Storage.deployed());

And the ExportUtils file:

module.exports.dataToDeploy = [];

module.exports.addDeployed = async function(pDeployed)
    let address = pDeployed.address;
    let name = await pDeployed.m_RegistryGroupName();
    let formattedName = web3.utils.toUtf8(name); // not working :(
    await module.exports.dataToDeploy.push({address:address, name:formattedName});

And the "14-displaying_texts.js" file:

var ExportUtils = require("./ExportUtils.js");

module.exports = async function(deployer, network, accounts)
    let arr = ExportUtils.dataToDeploy;
    let total = arr.length;
    console.log("deployed contracts: " + total);

    for(let i = 0; i < total; i++)
        let data = arr[i];
        console.log('\x1b[31m', data.address, '\x1b[36m', data.name ,'\x1b[0m');

Each contract contains a field:

bytes32 public m_RegistryGroupName;

I can get this bytes32 data in migration file, it shows something like this:

deployed contracts: 1
0xC97455e2A762A18f5849674b8ab31C53551b393B  0x73746f7261676500000000000000000000000000000000000000000000000000

But can't change it to string because if I use web3.utils.toUtf8 or web3.toUtf8 it says web3 is not defined. What's strange, in test it works perfectly.

I also tried to do this:

var Web3 = require("web3");

because web3 it undefined, tried to import it but none of the following works:

let formattedName = Web3.toUtf8(name);

Error: Error: TypeError: Web3.toUtf8 is not a function


let formattedName = Web3.utils.toUtf8(name);

Error: Error: TypeError: Cannot read property 'toUtf8' of undefined

Is there any solution of this problem?

  • 1
    The exported Web3 is a function. You have to create an instance of Web3 using new Web3(). – Sanjay S B Aug 5 at 9:21

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.