与已部署的智能合约交互时出错

Error Interacting with deployed smart contract

提问人:Pratik Jussal 提问时间:11/16/2023 更新时间:11/16/2023 访问量:9

问:

我已经在本地区块链中部署了一个简单的 ERC20 智能合约,当用户使用帐户地址向我的服务器发出 get 请求时,我正在尝试获取帐户余额

使用用户提供的帐户地址,我正在尝试获取帐户的余额并将其发送回给用户。

代币智能合约如下:

pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";

contract TestToken is ERC20, Ownable, ERC20Permit {
    constructor(address initialOwner)
        ERC20("TestToken", "TTK")
        Ownable(initialOwner)
        ERC20Permit("TestToken")
    {}

    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }

    function getBalance(address account) public view returns(uint256){
        return(balanceOf(account));
    }
}

服务器端代码如下:

const express = require('express');
const hre = require('hardhat');
var bodyParser = require('body-parser')
const app = express();
const port = 3000;
const contadd = '0x5FbDB2315678afecb367f032d93F642f64180aa3';

async function loadcontract(address){
    const [signer] = await hre.ethers.getSigners();
    const testToken = await hre.ethers.getContractAt('TestToken',contadd,signer);
    const transaction = await testToken.getBalance(address);
    const data = Promise.resolve(transaction);
    return data
}

app.use(bodyParser.urlencoded({ extended: true }));

app.get("/", async (req,res)=>{
    console.log(req.body);
    const address = req.body.address;
    const balance = await loadcontract(address);
    res.send(balance);
});

app.listen(port,()=>{
    console.log(`Server is running on ${port}`);
});

我面临的错误:当我尝试获取合约时,出现以下错误。

enter image description here

我的部署脚本:

let TestToken;
async function main () {
    // We get the contract to deploy
    const [Deployer] = await hre.ethers.getSigners();
    console.log("Deploying contracts with the account:", Deployer.address);
    const DeployerAdd = await Deployer.getAddress();
    TestToken = await hre.ethers.deployContract("TestToken",[DeployerAdd]);
    console.log('TestToken deployed to:', TestToken.address);
  }

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
     process.exit(1);
});

我尝试对此进行调试,似乎使用“getBalance”函数获取令牌余额导致了问题,但不确定原因。

我尝试过的事情:

  • 我确保合约已经部署,并且我能够获取合约实例。
  • 试图在我的部署脚本中获取余额,它似乎在那里工作,我得到的响应很大。

如果需要任何其他信息,请告诉我。

JavaScript Express 智能合约 以太坊 .js

评论


答: 暂无答案