MoralisWeb3 / Moralis-JS-SDK

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

Unexpected error returned for getWalletHistory #1203

Closed 0xchaosbi closed 4 months ago

0xchaosbi commented 4 months ago

New Bug Report

Checklist

Issue Description

When using getWalletHistory on mainnet, I am encountering the following error: MoralisError [Moralis SDK Core Error]: [C0005] Invalid address provided: null

However, the address being passed is a correctly checksummed address copied directly from Etherscan.

I did some poking around and what actually seems to be happening is an error regarding the limit query param passed into the API call.

If there is no limit set and there are more than 20 results, or the limit is 20 or above, I get the error saying an incorrect address is being passed.

However, if no limit is set and the number of results returned is below 20, or the limit is set to below 20, then the API call completes as expected.

Steps + code to reproduce

First make a call with the SDK using the following parameters:

      const response = await Moralis.EvmApi.wallets.getWalletHistory({
        address: "0xE62588196F3a4F4b653c388EA4BcEa7F9820E782",
        chain: "eth",
        cursor: "",
        order: "DESC"
      });

This call should return results as expected, because the wallet has fewer than 20 transactions.

Next we will see the bug in action using a different wallet.

Make the following call:

      const response = await Moralis.EvmApi.wallets.getWalletHistory({
        address: "0x1E9112601F9fA78E978C660d1b68071A3c332500",
        chain: "eth",
        cursor: "",
        order: "DESC"
      });

There is no limit set and the account has hundreds of transactions. We get a nonsensical error that the address is incorrectly formatted, but it isn't.

Make one more call:

      const response = await Moralis.EvmApi.wallets.getWalletHistory({
        address: "0x1E9112601F9fA78E978C660d1b68071A3c332500",
        chain: "eth",
        cursor: cursor,
        order: "DESC",
        limit: 10
      });

If we use the exact same wallet address but set the limit to 10, we get results returned as expected.

Actual Outcome

See above

Expected Outcome

I would not expect that a problem with the limit query param manifests as an error related to the address input, which is clearly correct in all of the calls above. If there is an actual error relating to the limit or something else, I would like to see that properly displayed so I can troubleshoot it correctly.

Environment

Server

Client

Logs

MoralisError [Moralis SDK Core Error]: [C0005] Invalid address provided: null
    at Function.EvmAddress.parse (/Users/redacted/coding/machinewallet-handover/node_modules/@moralisweb3/common-evm-utils/lib/cjs/index.cjs:229:19)
    at new EvmAddress (/Users/redacted/coding/machinewallet-handover/node_modules/@moralisweb3/common-evm-utils/lib/cjs/index.cjs:195:34)
    at Function.EvmAddress.fromJSON (/Users/redacted/coding/machinewallet-handover/node_modules/@moralisweb3/common-evm-utils/lib/cjs/index.cjs:225:16)
    at Function.EvmWalletHistoryTransaction.fromJSON (/Users/redacted/coding/machinewallet-handover/node_modules/@moralisweb3/common-evm-utils/lib/cjs/index.cjs:6805:35)
    at /Users/redacted/coding/machinewallet-handover/node_modules/@moralisweb3/common-evm-utils/lib/cjs/index.cjs:6878:90
    at Array.map (<anonymous>)
    at Function.EvmWalletHistory.fromJSON (/Users/redacted/coding/machinewallet-handover/node_modules/@moralisweb3/common-evm-utils/lib/cjs/index.cjs:6878:33)
    at Object.parseResponse (/Users/redacted/coding/machinewallet-handover/node_modules/@moralisweb3/common-evm-utils/lib/cjs/index.cjs:8246:33)
    at OperationV3Resolver.<anonymous> (/Users/redacted/coding/machinewallet-handover/node_modules/@moralisweb3/api-utils/lib/cjs/index.cjs:439:58)
    at step (/Users/redacted/coding/machinewallet-handover/node_modules/@moralisweb3/api-utils/lib/cjs/index.cjs:151:23)
    at Object.next (/Users/redacted/coding/machinewallet-handover/node_modules/@moralisweb3/api-utils/lib/cjs/index.cjs:132:53)
    at fulfilled (/Users/redacted/coding/machinewallet-handover/node_modules/@moralisweb3/api-utils/lib/cjs/index.cjs:122:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  isMoralisError: true,
  code: 'C0005',
  details: undefined,
  [cause]: undefined
}
ankur-dahiya commented 4 months ago

i have faced the same issue it is because moralis sometimes returns null in to_address so its not user's issue:

Lets understand why? If there's an transaction where an address created a smart contract then you'll get null in to_address (and moralis sdk expects an address) and if you carefully observe in "node_modules\@moralisweb3\common-evm-utils\lib\cjs\index.cjs" you'll see that address is being parsed and checked for its validity and if its null then error is being thrown "throw new commonCore.CoreError({ code: commonCore.CoreErrorCode.INVALID_ARGUMENT, message: "Invalid address provided: ".concat(address$1),"

So whats quick fix? you can fix this issue for your code by going in "node_modules\@moralisweb3\common-evm-utils\lib\cjs\index.cjs" and replace the EvmAddress constructor with the following code in line 194 "function EvmAddress(address) { if(address===null){ this._value = address; } else{ this._value = EvmAddress.parse(address); } }"

this will solve the issue for now.

b4rtaz commented 4 months ago

We fixed the toAddress field, now it's optional. Could you check the 2.26.2 version?

0xchaosbi commented 4 months ago

It seems to be fixed now, thanks so much @b4rtaz 🙏 🎉