Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web3ValidatorError validates wrong type #6965

Closed
AlbertFiat24 opened this issue Apr 10, 2024 · 5 comments · Fixed by #6981
Closed

Web3ValidatorError validates wrong type #6965

AlbertFiat24 opened this issue Apr 10, 2024 · 5 comments · Fixed by #6981

Comments

@AlbertFiat24
Copy link

AlbertFiat24 commented Apr 10, 2024

In Arbitrum Mainnet, somehow when trying to use a smart contract method of params method(address, uint)
Web3 validator always says to me that the /1 param needs to be of type address.

The contract is: 0x88d936bba8360a560f1eae770dc7eea4a901c169 and the Read method is userLockedAmount

It used to work until 2-3 weeks ago on "web3-eth-contract": "4.2.0", when I refreshed the dependencies it began giving the error or updating to 4.3.0

In Arbiscan it works correctly.
https://arbiscan.io/address/0x88d936bba8360a560f1eae770dc7eea4a901c169#readProxyContract#F12

Steps to reproduce the behavior

`it("Claim tokens", async () => {
      const web3 = initWeb3(42161);
      const web3Context = new Web3Context({
        provider: web3.provider,
        config: { contractDataInputFill: "data" }, //  all new contracts created to populate `data` field
      });
      const fiat24lock = new Contract(FIAT24LOCK_ABI, "0x88d936bba8360a560f1eae770dc7eea4a901c169", web3Context);
      const amountToClaim = await fiat24lock.methods.userLockedAmount("0x2c5d06f591D0d8cd43Ac232c2B654475a142c7DA", 909).call()
      expect(amountToClaim).toBe(0);
    });`

where FIAT24LOCK_ABI = [{"inputs":[{"internalType":"address","name":"sender","type":"address"}],"name":"Fiat24Lock__NotLocker","type":"error"},{"inputs":[{"internalType":"address","name":"sender","type":"address"}],"name":"Fiat24Lock__NotOperator","type":"error"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"currency","type":"address"}],"name":"Fiat24Lock__NotSufficientBalance","type":"error"},{"inputs":[{"internalType":"address","name":"currency","type":"address"}],"name":"Fiat24Lock__NotValidToken","type":"error"},{"inputs":[],"name":"Fiat24Lock__Suspended","type":"error"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Fiat24Lock__TokenIdNotLive","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":true,"internalType":"address","name":"currency","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Claimed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":true,"internalType":"address","name":"currency","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Locked","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Paused","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"previousAdminRole","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"newAdminRole","type":"bytes32"}],"name":"RoleAdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleGranted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleRevoked","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Unpaused","type":"event"},{"inputs":[],"name":"CASHDESK","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"DEFAULT_ADMIN_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"LOCKER_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"OPERATOR_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"SUNDRY","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"currency_","type":"address"},{"internalType":"uint256","name":"amount_","type":"uint256"}],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"currencyTotalLockedAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"fiat24AccountAddress","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"}],"name":"getRoleAdmin","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"grantRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"hasRole","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"fiat24AccountAddress_","type":"address"},{"internalType":"address","name":"usd24_","type":"address"},{"internalType":"address","name":"eur24_","type":"address"},{"internalType":"address","name":"chf24_","type":"address"},{"internalType":"address","name":"gbp24_","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId_","type":"uint256"},{"internalType":"address","name":"currency_","type":"address"},{"internalType":"uint256","name":"amount_","type":"uint256"}],"name":"lock","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"pause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"renounceRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"revokeRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId_","type":"uint256"},{"internalType":"address","name":"currency_","type":"address"},{"internalType":"uint256","name":"amount_","type":"uint256"}],"name":"setLockedAmount","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"unpause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"userLockedAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"validXXX24Tokens","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"}]

Logs

Web3ValidatorError: Web3 validator found 1 error[s]: value "909" at "/1" must pass "address" validation

Environment

Arbitrum Mainnet / Sepolia
"web3-eth-contract": "4.3.0",

@AlbertFiat24 AlbertFiat24 changed the title Web3ValidatorError validates type incorrectly Web3ValidatorError validates wrong type Apr 10, 2024
@SantiagoDevRel
Copy link
Member

Hey @AlbertFiat24 ! moving this from discord to here for better visibility, i actually just tried to interact with that code and it returns a 0n from my side, no error:

const { Web3 } = require("web3");
const abi = require("./arbitrum-abi.json"); //ABI from here 

const web3 = new Web3("https://arbitrum.llamarpc.com");

async function main() {
  const contract = new web3.eth.Contract(abi, "0x88d936bba8360a560f1eae770dc7eea4a901c169");
  const response = await contract.methods.userLockedAmount("0x2c5d06f591D0d8cd43Ac232c2B654475a142c7DA", 909).call();
  console.log(response);
}

main();

image

@AlbertFiat24
Copy link
Author

Hello @SantiagoDevRel this issue happens with the web3 tree-shaking when using the "web3-eth-contract": "4.3.0".
With the full web3 library seems to work correctly. But I would still like to use the "web3-eth-contract" if possible.

@SantiagoDevRel
Copy link
Member

Ohh got it! funny enough, I just ran the same code again (which worked 1h ago) and now im getting the same error :/ 😅, let's see how the core devs can help here💪
image

@MaxMustermann2
Copy link
Contributor

I believe I have found the cause behind this issue. The abi.name for public variables (which aren't accessed by their functions) is blank.

mapping(address => mapping(
    string => mapping(
        address => uint256
        )
    )
) public delegations;
[
  {
    "type": "function",
    "name": "delegations",
    "inputs": [
      {
        "name": "",
        "type": "address",
        "internalType": "address"
      },
      {
        "name": "",
        "type": "string",
        "internalType": "string"
      },
      {
        "name": "",
        "type": "address",
        "internalType": "address"
      }
    ],
    "outputs": [
      {
        "name": "",
        "type": "uint256",
        "internalType": "uint256"
      }
    ],
    "stateMutability": "view"
  }
]

This results in these items not getting an id when converted to the JSON schema.

if (isAbiParameterSchema(abi)) {
abiType = abi.type;
abiName = abi.name;
abiComponents = abi.components as FullValidationSchema;

Since they do not get an ID, the following check fails (the set has just one blank id and the number of items is more than 1).

&& new Set(schema.items.map((item: JsonSchema) => item.$id)).size === schema.items.length) {

Consequently, only the first element of the array is considered within the schema. In your case that is address, and the address format requirement is not satisfied by the second element which is a uint256.

const nextSchema = Array.isArray(schema.items) ? schema.items[0] : schema.items;

The fix is simple and I will send it in a pull request.

MaxMustermann2 added a commit to MaxMustermann2/web3.js that referenced this issue Apr 16, 2024
In the case of public mappings, the ABI generated has a blank name set.
This results in blank ids for such inputs within the JSON schema. Post
1f81ff0, the number of unique ids is
used to convert the JSON scheme into Zod, and hence the presence of
blank ids is a concern.

This commit generates the `abiName` if one is not provided to a value
equal to the `${level}/${index}`.

Fixes web3#6965.
@AlbertFiat24
Copy link
Author

@MaxMustermann2 many thanks for the quick fixing!

avkos pushed a commit that referenced this issue Apr 22, 2024
* fix(validator): generate abi name if not provided

In the case of public mappings, the ABI generated has a blank name set.
This results in blank ids for such inputs within the JSON schema. Post
1f81ff0, the number of unique ids is
used to convert the JSON scheme into Zod, and hence the presence of
blank ids is a concern.

This commit generates the `abiName` if one is not provided to a value
equal to the `${level}/${index}`.

Fixes #6965.

* fix: add validator changes to root CHANGELOG

---------

Co-authored-by: Alex <alex.luu@mail.utoronto.ca>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants