{"jsonrpc":"2.0","id":44,"error":{"code":-32601,"message":"eth_sendTransaction 方法不存在/不可用"}}

问题描述 投票:0回答:0

我有一个 ERC20 代币,我已经创建了一个智能合约来买卖该代币。我创建了一个用于买卖的 API。我正在邮递员桌面上检查我的 api,但出现此错误。我也试过炼金术的端点仍然有同样的错误。

我的 api buy_token.js

import {ethers} from 'ethers';
import { createTokenSwapInstance ,provider } from '../../../helpers/contractInstance/tokenSwapInstance';
import { tokenSwapAddress } from '../../../config';

export default async function handler(req,res){
    if(req.method === 'POST'){
        //Check if data is present
        const numberOfTokens = req.body.numberOfTokens;
        const userAddress = req.body.userAddress;
        console.log("numberOfTokens", numberOfTokens)
        console.log("userAddress", userAddress)
        if(numberOfTokens === undefined ||numberOfTokens === ''|| numberOfTokens === 0){
            res.status(400).json({success: false, msg: 'Number of tokens is required.'});
            return;
        }
        
        const feePercent = 3;
        const tokenPrice = 0.0168;

        //Calculating total fee amount to pay
        const numberOfTokensinNum = Number(numberOfTokens)
        const numberOftokensinWei = ethers.utils.parseEther(String(numberOfTokens))
        const totalfee = (numberOfTokensinNum * feePercent)/100;
        const totalAmount = (numberOfTokensinNum - totalfee) * tokenPrice
        const totalAmountinWei = ethers.utils.parseEther(String(totalAmount))
        const signer = provider.getSigner(userAddress)

        const tokenSwapInstance = createTokenSwapInstance(tokenSwapAddress,signer)

        //Buy tokens
        try{
           const tx = await tokenSwapInstance.functions.buyTokens(numberOftokensinWei,{
            value: totalAmountinWei,
            gasLimit: 5000000
           })
           await tx.wait();
           res.status(200).json({success: true, msg:'Transaction Successful.'})
        }
        catch(error){
            console.log(error);
            res.status(500).json({success: false, msg:'Transaction Failed. Please try again.'})
        }
    }else{
        res.status(400).json({success: false, msg: 'Bad request.'})
    }
}

我的 createTokenSwapInstance.js

import { ethers } from 'ethers';
//import Web3Modal from 'web3modal';
import path from 'path';
import fs from 'fs';
require("dotenv").config({ path: path.resolve(__dirname, '.env.local') })

export const infuraId = `https://goerli.infura.io/v3/${process.env.NEXT_PUBLIC_INFURA_KEY}`

console.log(process.env.NEXT_PUBLIC_INFURA_KEY)

export const provider = new ethers.providers.StaticJsonRpcProvider(infuraId)

const tokenSwapContractAbi = () => {
    try {
      const dir = path.resolve(
        './',
        './artifacts/contracts/tokenswap.sol/tokenSwap.json'
      )
      const file = fs.readFileSync(dir, 'utf8')
      const json = JSON.parse(file)
      const abi = json.abi
      return abi
    } catch (e) {
      console.log(`e`, e)
    }
  }

  export const createTokenSwapInstance = (contractAddress, signer) => {
    return new ethers.Contract(
      contractAddress,
      tokenSwapContractAbi(),
      signer
    );
  }
  

我的错误:

Error: processing response error (body="{\"jsonrpc\":\"2.0\",\"id\":44,\"error\":{\"code\":-32601,\"message\":\"The method eth_sendTransaction does not exist/is not available\"}}", error={"code":-32601}, requestBody="{\"method\":\"eth_sendTransaction\",\"params\":[{\"gas\":\"0x4c4b40\",\"value\":\"0x39e5202b427ffe\",\"from\":\"0xb2165a8b792cf090f9ebde4206f1c76cbcb92546\",\"to\":\"0x8275407d49a3c22d326f6c09893267daaa2c7bf5\",\"data\":\"0x3610724e0000000000000000000000000000000000000000000000000de0b6b3a7640000\"}],\"id\":44,\"jsonrpc\":\"2.0\"}", requestMethod="POST", url="https://goerli.infura.io/v3/e7631065f3c044f69188f39628368bd7", code=SERVER_ERROR, version=web/5.7.1)
    at Logger.makeError (C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\logger\lib\index.js:238:21)
    at Logger.throwError (C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\logger\lib\index.js:247:20)
    at C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\web\lib\index.js:313:32
    at step (C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\web\lib\index.js:33:23)
    at Object.next (C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\web\lib\index.js:14:53)
    at fulfilled (C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\web\lib\index.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  reason: 'processing response error',
  code: 'SERVER_ERROR',
  body: '{"jsonrpc":"2.0","id":44,"error":{"code":-32601,"message":"The method eth_sendTransaction does not exist/is not available"}}',
  error: Error: The method eth_sendTransaction does not exist/is not available
      at getResult (C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:191:21)
      at processJsonFunc (C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\web\lib\index.js:356:22)
      at C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\web\lib\index.js:288:46
      at step (C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\web\lib\index.js:33:23)
      at Object.next (C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\web\lib\index.js:14:53)
      at fulfilled (C:\Users\gauth\OneDrive\Desktop\A\Blockchain\node_modules\@ethersproject\web\lib\index.js:5:58)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
    code: -32601,
    data: undefined
  },
  requestBody: '{"method":"eth_sendTransaction","params":[{"gas":"0x4c4b40","value":"0x39e5202b427ffe","from":"0xb2165a8b792cf090f9ebde4206f1c76cbcb92546","to":"0x8275407d49a3c22d326f6c09893267daaa2c7bf5","data":"0x3610724e0000000000000000000000000000000000000000000000000de0b6b3a7640000"}],"id":44,"jsonrpc":"2.0"}',
  requestMethod: 'POST',
  url: 'https://goerli.infura.io/v3/{api-key}'
}
node.js web3
© www.soinside.com 2019 - 2024. All rights reserved.