Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

305
Vistas
How to decode response from calling smart contract on Ethereum using Web3js on nodejs

My project consists of 3 parts

  1. Node.js server using Web3js to connect with Ethereum network.
  2. Truffle project consists of smart contracts.
  3. Ganache for running local Ethereum network.

Basically, I need to call the Ethereum network from my Node.js server to get data from the Blockchain network.

This is my main Contract

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract RewardToken is ERC20 {
    constructor(string memory name, string memory symbol) ERC20(name, symbol) {}
}

This is my Factory Contract

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

import './RewardToken.sol';

contract RewardTokenFactory {
  address[] public deployedRewardTokens;
  mapping(address => RewardToken) ownerToTokens;

  function createToken(string calldata name, string calldata symbol) public {
    RewardToken newToken = new RewardToken(name, symbol);
    ownerToTokens[msg.sender] = newToken;
    deployedRewardTokens.push(address(newToken));
  }

  function getOwnerToken() public view returns(RewardToken) {
    return ownerToTokens[msg.sender];
  }
}

This is my Node.js method

const rewardTokenData = await rewardTokenContractInstance.getOwnerToken({
  from: address,
});

console.log(rewardTokenData, ' :rewardTokenData');

this is what I got from rewardTokenData after calling .getOwnerToken

0xcb22Ff93c8253224dBAF033376c762bC03024343

So should I suppose to get actual contract data from new RewardToken?

If not then how can I decode response to be a readable data?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I am assuming that you want to extract data from the token contract of an owner. In order to do that, you will have to write specific get functions in your Factory contract. Here is an example, to get the total supply of tokens from the contract that is owned by msg.sender:

contract RewardTokenFactory {
  address[] public deployedRewardTokens;
  mapping(address => RewardToken) ownerToTokens;

  function createToken(string calldata name, string calldata symbol) public {
    RewardToken newToken = new RewardToken(name, symbol);
    ownerToTokens[msg.sender] = newToken;
    deployedRewardTokens.push(address(newToken));
  }

  function getTotalSupply() public view returns(uint256) {
    return ownerToTokens[msg.sender].totalSupply();
  }
}

In the frontend, you will have to convert it to a smallerbit-sized number with .toNumber(), so that it works for JavaScript.

Here you can see the erc20 totalSupply() function documented: https://docs.openzeppelin.com/contracts/2.x/api/token/erc20#IERC20-totalSupply--

You can also see the other functions there that you can use. There aren't many. The other possible "view"-type functions that you could create is to get the balance of a specific address of that token (getBalance()),

However, you can always add additional functions in your token contract, that you can then use in your factorycontract to write get functions:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract RewardToken is ERC20 {
    constructor(string memory name, string memory symbol) ERC20(name, symbol) {}
function getTokenName() public {
 return _name;
}
}

You can find all the private variables of ERC20 on their github: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol

And then you can use getTokenName() in the factory contract, to get the name of the token that is owned by the msg.sender:

contract RewardTokenFactory {
  address[] public deployedRewardTokens;
  mapping(address => RewardToken) ownerToTokens;

  function createToken(string calldata name, string calldata symbol) public {
    RewardToken newToken = new RewardToken(name, symbol);
    ownerToTokens[msg.sender] = newToken;
    deployedRewardTokens.push(address(newToken));
  }

  function getTokenNameOfMsgSender() public view returns(uint256) {
    return ownerToTokens[msg.sender].getTokenName();
  }
}

Note: I didn't test the code, there might be syntax errors.

The reason why you have to write specific get functions is because the contract is way to large to send to web3.js. Solidity automatically hashes the contract into a 256 bit hash, which makes it unusable to use in the frontend (or maybe that hash that you showed is just the address of the token contract, I don't know exactly, but that's irrelevant).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda