MoralisWeb3 / Moralis-JS-SDK

Moralis Official Javascript SDK
https://docs.moralis.io
Other
366 stars 257 forks source link

Web3API.native.runContractFunction on moralis/node always failed with "Error: ABI is required!" #486

Closed arku3 closed 2 years ago

arku3 commented 2 years ago

New Bug Report

Checklist

Issue Description

When using moralis/node, Moralis.Web3API.native.runContractFunction will always return "Error: ABI is required!" because MoralisWeb3Api.js > fetchFromApi() do not actually send the request body in the axios request due to a misconfiguration.

// line:191
const response = await axios(this.baseURL + parameterizedUrl, {
    params,
   method,
-  body,
+  data:body
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      'x-api-key': this.apiKey
    }
  });

Steps + code to reproduce

await Moralis.start({
  serverUrl: process.env.NEXT_PUBLIC_MORALIS_SERVER_URL,
  appId: process.env.NEXT_PUBLIC_MORALIS_APPLICATION_ID,
  masterKey: process.env.MORALIS_MASTER_KEY,
  moralisSecret: process.env.MORALIS_SECRET,
});
await Moralis.enableWeb3({ chainId: CHAIN_ID, privateKey: process.env.MORALIS_PRIVATE_KEY });
const result = await Moralis.Web3API.native.runContractFunction({
    chain: CHAIN_ID,
    address: NFT_ADDRESS,
    abi: [
      {
        "inputs": [
          {
            "internalType": "uint256",
            "name": "tokenId",
            "type": "uint256"
          }
        ],
        "name": "ownerOf",
        "outputs": [
          {
            "internalType": "address",
            "name": "",
            "type": "address"
          }
        ],
        "stateMutability": "view",
        "type": "function"
      }],
    function_name,
    params: { tokenId: '1' },
  });

Actual Outcome

Error: ABI is required!
    at Function.fetchFromApi (C:\workspace\nodejs\xxxxx\node_modules\moralis\lib\node\MoralisWeb3Api.js:200:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async runMoaCultivationFunction (webpack-internal:///./src/app/web3/helpers/moralis-node.js:46:20)
    at async getServerSideProps (webpack-internal:///./pages/moa/[tokenId].js:747:190)
    at async Object.renderToHTML (C:\workspace\nodejs\xxxxx\node_modules\next\dist\server\render.js:573:20)
    at async doRender (C:\workspace\nodejs\xxxxx\node_modules\next\dist\server\base-server.js:912:38)
    at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (C:\workspace\nodejs\xxxxx\node_modules\next\dist\server\base-server.js:1017:28)
    at async C:\workspace\nodejs\xxxxx\node_modules\next\dist\server\response-cache.js:69:36

Expected Outcome

Contract Function successfully executed and return the result.

Environment

Server

Logs

N/A

shreethejaBandit commented 2 years ago

Seems like same error i encountered but handling it was again crashing check this out: #448

ErnoW commented 2 years ago

This was related to a bug in the way we used axios. This has been fixed in v1.10