graph-tooling icon indicating copy to clipboard operation
graph-tooling copied to clipboard

There is a problem with the generated code.

Open amandafanny opened this issue 2 years ago • 6 comments
trafficstars

Which packages are impacted by your issue?

@graphprotocol/graph-cli

Describe the issue

run npm run codegen run npm run test

the error happens

economic-module-hub
--------------------------------------------------
  Describe entity assertions:
    √ Activate created and stored - 0.000ms

pass
--------------------------------------------------
  Describe entity assertions:
thread 'main' panicked at '🆘 Unexpected error upon calling hook: Mapping aborted at ~lib/@graphprotocol/graph-ts/chain/ethereum.ts, line 63, column 7, with message: Ethereum value is not bytes.
wasm backtrace:
    0: 0x2829 - <unknown>!generated/Pass/Pass/Mint__Params#get:name
    1: 0x3035 - <unknown>!src/pass/handleMint
    2: 0x34fb - <unknown>!start:tests/pass.test~anonymous|3~anonymous|0
', src/test_suite.rs:51:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Reproduction

no

Steps to Reproduce the Bug or Issue

I have written.

Expected behavior

the code generated like this

export class Mint__Params {
  _event: Mint;

  constructor(event: Mint) {
    this._event = event;
  }

  get to(): Address {
    return this._event.parameters[0].value.toAddress();
  }

  get tokenId(): BigInt {
    return this._event.parameters[1].value.toBigInt();
  }

  get name(): String {
    return this._event.parameters[2].value.toString();
  }
}

Screenshots or Videos

No response

Platform

  • OS: [macOS]
  • NodeJS: [18.4.0]
  • @graphprotocol/graph-cli version(s): [0.55.0]

Subgraph Manifest

specVersion: 0.0.5
description: Wrap for Ethereum
schema:
  file: ./schema.graphql
features:
  - fullTextSearch
  - nonFatalErrors
  - grafting
  - ipfsOnEthereumContracts
dataSources:
  - kind: ethereum
    name: Pass
    network: goerli
    source:
      address: "0x58bD112D901c6cAFB130F7d6A0D7BF7798B1E362"
      abi: Pass
      startBlock: 9289681
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.7
      language: wasm/assemblyscript
      entities:
        - Pass
      abis:
        - name: Pass
          file: ./abis/Pass.json
      # mapping—./src/mapping.ts
      eventHandlers:
        - event: Mint(indexed address,indexed uint256,indexed string)
          handler: handleMint
        - event: Transfer(indexed address,indexed address,indexed uint256)
          handler: handleTransfer
      # callHandlers:
      # - function: mint(string)
      #   handler: handleMint
      file: ./src/pass.ts
  - kind: ethereum
    name: EconomicModuleHub
    network: goerli
    source:
      address: "0xb47986fcEdE16c93d41F946B82Fa78180821DC2d"
      abi: EconomicModuleHub
      startBlock: 9289685
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.7
      language: wasm/assemblyscript
      entities:
        - EconomicModule
      abis:
        - name: EconomicModuleHub
          file: ./abis/EconomicModuleHub.json
      eventHandlers:
        - event: Activate(indexed uint256,indexed address)
          handler: handleActivate
        - event: Mint(indexed address,indexed uint256,indexed address)
          handler: handleMint
        - event: Transfer(indexed address,indexed address,indexed uint256)
          handler: handleTransfer
      file: ./src/economic-module-hub.ts

Subgraph GraphQL Schema

type Pass @entity {
  id: ID! # 通常为主键, 可能是 tokenId 或其他唯一标识
  tokenId: BigInt!
  name: String!
  holder: Holder!
  tokenURIData: String
  metaIdentityAddress: Bytes
}

type Holder @entity {
  id: ID!
  passTokens: [Pass!] @derivedFrom(field: "holder")
}

Additional context

import { log } from "matchstick-as";
import {
  Approval as ApprovalEvent,
  ApprovalForAll as ApprovalForAllEvent,
  CallExecuted as CallExecutedEvent,
  Mint as MintEvent,
  Paused as PausedEvent,
  RoleAdminChanged as RoleAdminChangedEvent,
  RoleGranted as RoleGrantedEvent,
  RoleRevoked as RoleRevokedEvent,
  Transfer as TransferEvent,
  Unpaused as UnpausedEvent,
} from "../generated/Pass/Pass";
import { Pass } from "../generated/schema";

// tokenId: BigInt!;
// name: String!;
// holder: Holder!;
// tokenURIData: String;
// metaIdentityAddress: Bytes;

export function handleMint(event: MintEvent): void {
  log.info("{}", [event.params.name.toString()]);
  let entity = new Pass(event.params.tokenId.toString());
  entity.holder = event.params.to.toHexString();
  entity.tokenId = event.params.tokenId;
  entity.name = event.params.name.toString();
  entity.save();
}

amandafanny avatar Aug 23 '23 11:08 amandafanny

here is the demo https://github.com/amandafanny/graph-bug-demo

amandafanny avatar Aug 23 '23 12:08 amandafanny

I think this is a bug in graph test handling things cc @dimitrovmaksim. I just deployed the subgraph https://ipfs.io/ipfs/QmSViNCkXNFbCNxASvWQ7TA694i5bufi4ncf2QxSVNA1Ga and it seems to work CleanShot 2023-08-28 at 15 47 38

saihaj avatar Aug 28 '23 19:08 saihaj

The issue comes from the auto-generated helpers that create the mocked event here https://github.com/amandafanny/graph-bug-demo/blob/master/tests/pass-utils.ts#L104. Because the name event param is an indexed string, it should be ethereum.Value.fromBytes(..) instead. Indexed string params in solidity are hashed with keccak256 because of their dynamic size. Changing it to

  mintEvent.parameters.push(
    new ethereum.EventParam("name", ethereum.Value.fromBytes(Bytes.fromUTF8("some_name")))
  )

should fix the issue

dimitrovmaksim avatar Aug 28 '23 20:08 dimitrovmaksim

I change "Pass" name To "Deployer". image name garbled

Is there some problem in wasm?

amandafanny avatar Aug 31 '23 03:08 amandafanny

probably some type issue 🤔 ideas @azf20 @incrypto32

saihaj avatar Aug 31 '23 04:08 saihaj

When you use indexed params, they become log topics that you can use to filter the logs with eth_getLogs. Dynamic values like strings, arrays, etc get keccak256 hashed and the hash is used as the topic. For example the string someName will be hashed to 0586970d131147e56722dbb5f2920490c0a09633e9fdf5c00b0218f164abdeea. Those values are not recoverable back to string and they are represented as Bytes, so using toString() will return a jibberish value, instead to obtain the hash, you have to use toHex() or toHexString(), but you can't get the original value someName

dimitrovmaksim avatar Aug 31 '23 06:08 dimitrovmaksim