如何使用nodejs上的Web3J在以太坊上调用智能合约来解码响应

发布于 2025-01-30 03:36:08 字数 1441 浏览 2 评论 0原文

我的项目由

  1. 使用Web3JS与以太坊网络连接的3个部分Node.js服务器组成。
  2. 松露项目由智能合约组成。
  3. 用于运行本地以太坊网络的Ganache。

基本上,我需要从我的node.js服务器调用以太坊网络,以从区块链网络中获取数据。

这是我的主要合同,

// 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) {}
}

这是我的工厂合同

// 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];
  }
}

,这是我的node.js方法,

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

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

这是我从reward tokendata中获得的

0xcb22Ff93c8253224dBAF033376c762bC03024343

。来自新RewardToken的数据?

如果不是这样,我该如何将响应解码为可读数据?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花伊自在美 2025-02-06 03:36:08

我假设您想从所有者的令牌合同中提取数据。为了做到这一点,您必须在工厂合同中编写特定的GET功能。 是一个示例,以获取味精所有的合同中的代币供应。

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();
  }
}

这 它适用于JavaScript。

在这里您可以看到ERC20 ptalsupply()函数记录: https://docs.openzeppelin.com/contracts/2.x/api/token/token/token/erc20#ierc20-ierc20-totalsupply-

您也可以看到那里的其他功能您可以使用。没有很多。您可以创建的另一个可能的“视图”型函数是获得该令牌(getBalance())的特定地址的平衡,

但是,您始终可以在令牌合同中添加其他功能,然后可以在该功能中使用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:

​()在工厂合同中,要获取味精所有的令牌的名称:

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();
  }
}

注:我没有测试代码,可能会有语法错误。

您必须编写特定Get函数的原因是因为合同是向web3.js发送的大型方法。坚固的性将合同自动放入256位哈希,这使得在前端使用不可用(或者您显示的哈希只是代币合同的地址,我不知道,但这是无关紧要的)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文