图表:我怎样才能在我的回复中包含这个“价格”?不同 GraphQL 类型中存在的值?

发布于 01-18 11:06 字数 3616 浏览 6 评论 0原文

我正在与 thegraph.com 合作,并创建了一个子图来索引来自 NFT 市场智能合约的区块链数据我从头开始构建,只是为了我自己的教育目的。同时,我第一次使用 GraphQL 并在这里构建查询,所以我什至不确定这是否是正确的问题,但我将通过下面的屏幕截图来说明我的最终目标是什么。 这是我正在使用的子图端点,以防有人想要检查我在那里的模式并使用它: https://api.thegraph.com/subgraphs/name/parenthesislab/cubansea-mumbai-v9/graphql

我想要的结果:接收此价格值存在于我在这个单一API响应中获得的相应ERC721tokens对象内的MarketTokenMinted类型中。

查询响应示例屏幕截图

我当前的 GraphQL 查询(根据上面的屏幕截图生成响应 ^):

query FetchMarketTokens {
  account(id: "0xae198b77c760c8d547f796f57c469c0294592ab8") {
    id
    ERC721tokens(orderBy: identifier, orderDirection: desc, first: 10) {
      id
      identifier
      uri
    }
  }
  marketTokenMinteds(orderBy: tokenId, orderDirection: desc) {
    nftContract
    price
    tokenId
  }
}

我当前的 GraphQL 架构部署到此 图表中的子图

# schema.graphql

type MarketTokenMinted @entity {
  id: ID!
  itemId: BigInt! # uint256
  nftContract: Bytes! # address
  tokenId: BigInt! # uint256
  seller: Bytes! # address
  owner: Bytes! # address
  price: BigInt! # uint256
  sold: Boolean! # bool
}

type MarketTokenSold @entity {
  id: ID!
  itemId: BigInt! # uint256
  nftContract: Bytes! # address
  tokenId: BigInt! # uint256
  seller: Bytes! # address
  owner: Bytes! # address
  price: BigInt! # uint256
  sold: Boolean! # bool
}

type Account @entity {
    id: ID!
    asERC721: ERC721Contract
    ERC721tokens: [ERC721Token!]! @derivedFrom(field: "owner")
    ERC721operatorOwner: [ERC721Operator!]! @derivedFrom(field: "owner")
    ERC721operatorOperator: [ERC721Operator!]! @derivedFrom(field: "operator")
    ERC721transferFromEvent: [ERC721Transfer!]! @derivedFrom(field: "from")
    ERC721transferToEvent: [ERC721Transfer!]! @derivedFrom(field: "to")
    events: [Event!]! @derivedFrom(field: "emitter")
}
type ERC721Contract @entity {
    id: ID!
    asAccount: Account!
    supportsMetadata: Boolean
    name: String
    symbol: String
    tokens: [ERC721Token!]! @derivedFrom(field: "contract")
    operators: [ERC721Operator!]! @derivedFrom(field: "contract")
    transfers: [ERC721Transfer!]! @derivedFrom(field: "contract")
}
type ERC721Token @entity {
    id: ID!
    contract: ERC721Contract!
    identifier: BigInt!
    owner: Account!
    approval: Account!
    uri: String
    transfers: [ERC721Transfer!]! @derivedFrom(field: "token")
}
type ERC721Operator @entity {
    id: ID!
    contract: ERC721Contract!
    owner: Account!
    operator: Account!
    approved: Boolean!
}
type ERC721Transfer implements Event @entity {
    id: ID!
    emitter: Account!
    transaction: Transaction!
    timestamp: BigInt!
    contract: ERC721Contract!
    token: ERC721Token!
    from: Account!
    to: Account!
}
interface Event {
    id: ID!
    transaction: Transaction!
    emitter: Account!
    timestamp: BigInt!
}
type Transaction @entity {
    id: ID!
    timestamp: BigInt!
    blockNumber: BigInt!
    events: [Event!]! @derivedFrom(field: "transaction")
}

经过无数小时试图弄清楚这一点,我不确定我错过了什么,也不知道如何将其组合在一起以接收此 price 值以及每个上获得的其余数据收到的ERC721tokens对象。任何帮助我走向正确方向的帮助都将受到高度赞赏。

I'm working with thegraph.com and created a subgraph to index blockchain data from a NFT marketplace smart contract I'm building from scratch, just for my own educational purposes. At the same time I'm using using GraphQL and building queries here for the first time, so I'm not even sure if this is the correct question, but I'll get to the point of what's my ultimate goal with the screenshot below.
Here is the subgraph endpoint I'm working with in case someone wants to inspect the schema I have there and play with it: https://api.thegraph.com/subgraphs/name/parenthesislab/cubansea-mumbai-v9/graphql

My desired outcome: Receive this price value that exists in MarketTokenMinted type inside the corresponding ERC721tokens objects i'm getting on this single API response.

query response example screenshot

My current GraphQL query (produces the response from the screenshot above ^):

query FetchMarketTokens {
  account(id: "0xae198b77c760c8d547f796f57c469c0294592ab8") {
    id
    ERC721tokens(orderBy: identifier, orderDirection: desc, first: 10) {
      id
      identifier
      uri
    }
  }
  marketTokenMinteds(orderBy: tokenId, orderDirection: desc) {
    nftContract
    price
    tokenId
  }
}

My current GraphQL schema deployed to this subgraph in The Graph:

# schema.graphql

type MarketTokenMinted @entity {
  id: ID!
  itemId: BigInt! # uint256
  nftContract: Bytes! # address
  tokenId: BigInt! # uint256
  seller: Bytes! # address
  owner: Bytes! # address
  price: BigInt! # uint256
  sold: Boolean! # bool
}

type MarketTokenSold @entity {
  id: ID!
  itemId: BigInt! # uint256
  nftContract: Bytes! # address
  tokenId: BigInt! # uint256
  seller: Bytes! # address
  owner: Bytes! # address
  price: BigInt! # uint256
  sold: Boolean! # bool
}

type Account @entity {
    id: ID!
    asERC721: ERC721Contract
    ERC721tokens: [ERC721Token!]! @derivedFrom(field: "owner")
    ERC721operatorOwner: [ERC721Operator!]! @derivedFrom(field: "owner")
    ERC721operatorOperator: [ERC721Operator!]! @derivedFrom(field: "operator")
    ERC721transferFromEvent: [ERC721Transfer!]! @derivedFrom(field: "from")
    ERC721transferToEvent: [ERC721Transfer!]! @derivedFrom(field: "to")
    events: [Event!]! @derivedFrom(field: "emitter")
}
type ERC721Contract @entity {
    id: ID!
    asAccount: Account!
    supportsMetadata: Boolean
    name: String
    symbol: String
    tokens: [ERC721Token!]! @derivedFrom(field: "contract")
    operators: [ERC721Operator!]! @derivedFrom(field: "contract")
    transfers: [ERC721Transfer!]! @derivedFrom(field: "contract")
}
type ERC721Token @entity {
    id: ID!
    contract: ERC721Contract!
    identifier: BigInt!
    owner: Account!
    approval: Account!
    uri: String
    transfers: [ERC721Transfer!]! @derivedFrom(field: "token")
}
type ERC721Operator @entity {
    id: ID!
    contract: ERC721Contract!
    owner: Account!
    operator: Account!
    approved: Boolean!
}
type ERC721Transfer implements Event @entity {
    id: ID!
    emitter: Account!
    transaction: Transaction!
    timestamp: BigInt!
    contract: ERC721Contract!
    token: ERC721Token!
    from: Account!
    to: Account!
}
interface Event {
    id: ID!
    transaction: Transaction!
    emitter: Account!
    timestamp: BigInt!
}
type Transaction @entity {
    id: ID!
    timestamp: BigInt!
    blockNumber: BigInt!
    events: [Event!]! @derivedFrom(field: "transaction")
}

After countless hours trying to figure this out I'm not sure anymore what I'm missing or how to put this together to receive this price value along with the rest of the obtained data on each ERC721tokens objects received. Any help that sets me in the right direction is highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一桥轻雨一伞开2025-01-25 11:06:28

最终找到了如何在此处回答我自己的问题以供将来参考。

这可以通过将Price字段添加到erc721token我的架构的实体来完成。 。然后,我能够将映射逻辑添加到事件返回价格中,该价格将加载erc721token实体并保存那里的价格信息。此事件是MarketTokenMinted

将最终解决方案组合在一起如下:

erc721token键入可选的价格字段:

type ERC721Token @entity {
  id: ID!
  contract: ERC721Contract!
  identifier: BigInt!
  owner: Account!
  approval: Account!
  uri: String
  transfers: [ERC721Transfer!]! @derivedFrom(field: "token")
  price: BigInt # uint256
}

mapping.ts带有添加的映射逻辑的文件,以通过获取令牌tokenid参数(来自MarketTokenMintedEvent),添加.price属性>属性和值,并使用token.save()

import {
  MarketTokenMinted as MarketTokenMintedEvent,
  MarketTokenSold as MarketTokenSoldEvent,
} from "../generated/CSMarket/CSMarket";
import { MarketTokenMinted, MarketTokenSold } from "../generated/schema";
import { fetchERC721, fetchERC721Token } from "./fetch/erc721";

export function handleMarketTokenMinted(event: MarketTokenMintedEvent): void {
  let entity = new MarketTokenMinted(
    event.transaction.hash.toHex() + "-" + event.logIndex.toString()
  );
  entity.itemId = event.params.itemId;
  entity.nftContract = event.params.nftContract;
  entity.tokenId = event.params.tokenId;
  entity.seller = event.params.seller;
  entity.owner = event.params.owner;
  entity.price = event.params.price;
  entity.sold = event.params.sold;

  // Add token price value to new .price field in the ERC721Token type.
  let contract = fetchERC721(event.params.nftContract);
  if (contract != null) {
    let token = fetchERC721Token(contract, event.params.tokenId);

    token.price = event.params.price;

    contract.save();
    token.save();
  }

  entity.save();
}

fetchmarkettokensbyowner添加价格字段:

query FetchMarketTokensByOwner {
  erc721Tokens(
    where: {owner: "0xae198b77c760c8d547f796f57c469c0294592ab8"}
    orderBy: identifier
    orderDirection: desc
    first: 10
  ) {
    identifier
    uri
    owner {
      id
    }
    price
  }
}

我现在在每个令牌对象中包含Price值的结果:

{
  "data": {
    "erc721Tokens": [
      {
        "identifier": "4",
        "uri": "https://ipfs.infura.io/ipfs/QmfZXdugdU6BwtxWDNxpnETviB36qBX9qrLDrxeDSoFept",
        "owner": {
          "id": "0xae198b77c760c8d547f796f57c469c0294592ab8"
        },
        "price": "324324520000000"
      },
      {
        "identifier": "3",
        "uri": "https://ipfs.infura.io/ipfs/QmW21btPRbB8zgXiLs4oegpGXiLLSX9jnuSip4axwkFdaz",
        "owner": {
          "id": "0xae198b77c760c8d547f796f57c469c0294592ab8"
        },
        "price": "343235000000000"
      },
      {
        "identifier": "1",
        "uri": "https://ipfs.infura.io/ipfs/QmRKGJMnnLMBeT72bXU6yjG2g2MpHSvrq8pRGbZykmjSgE",
        "owner": {
          "id": "0xae198b77c760c8d547f796f57c469c0294592ab8"
        },
        "price": "323424523400000"
      }
    ]
  }
}

Finally found how, answering my own question here for future reference.

This could be done by adding a price field into the ERC721Token entity of my schema. Then I was able to add mapping logic into the event returning price which would load the ERC721Token entity and save the price information there. This event was MarketTokenMinted.

The final solution was put together as follows:

The ERC721Token type with an optional price field added:

type ERC721Token @entity {
  id: ID!
  contract: ERC721Contract!
  identifier: BigInt!
  owner: Account!
  approval: Account!
  uri: String
  transfers: [ERC721Transfer!]! @derivedFrom(field: "token")
  price: BigInt # uint256
}

The mapping.ts file with added mapping logic to fetch the token by tokenId parameter (coming from the MarketTokenMintedEvent), adds the .price property and value and save with token.save():

import {
  MarketTokenMinted as MarketTokenMintedEvent,
  MarketTokenSold as MarketTokenSoldEvent,
} from "../generated/CSMarket/CSMarket";
import { MarketTokenMinted, MarketTokenSold } from "../generated/schema";
import { fetchERC721, fetchERC721Token } from "./fetch/erc721";

export function handleMarketTokenMinted(event: MarketTokenMintedEvent): void {
  let entity = new MarketTokenMinted(
    event.transaction.hash.toHex() + "-" + event.logIndex.toString()
  );
  entity.itemId = event.params.itemId;
  entity.nftContract = event.params.nftContract;
  entity.tokenId = event.params.tokenId;
  entity.seller = event.params.seller;
  entity.owner = event.params.owner;
  entity.price = event.params.price;
  entity.sold = event.params.sold;

  // Add token price value to new .price field in the ERC721Token type.
  let contract = fetchERC721(event.params.nftContract);
  if (contract != null) {
    let token = fetchERC721Token(contract, event.params.tokenId);

    token.price = event.params.price;

    contract.save();
    token.save();
  }

  entity.save();
}

The FetchMarketTokensByOwner adding the price field:

query FetchMarketTokensByOwner {
  erc721Tokens(
    where: {owner: "0xae198b77c760c8d547f796f57c469c0294592ab8"}
    orderBy: identifier
    orderDirection: desc
    first: 10
  ) {
    identifier
    uri
    owner {
      id
    }
    price
  }
}

And the results I'm getting now with the price value included inside each token object:

{
  "data": {
    "erc721Tokens": [
      {
        "identifier": "4",
        "uri": "https://ipfs.infura.io/ipfs/QmfZXdugdU6BwtxWDNxpnETviB36qBX9qrLDrxeDSoFept",
        "owner": {
          "id": "0xae198b77c760c8d547f796f57c469c0294592ab8"
        },
        "price": "324324520000000"
      },
      {
        "identifier": "3",
        "uri": "https://ipfs.infura.io/ipfs/QmW21btPRbB8zgXiLs4oegpGXiLLSX9jnuSip4axwkFdaz",
        "owner": {
          "id": "0xae198b77c760c8d547f796f57c469c0294592ab8"
        },
        "price": "343235000000000"
      },
      {
        "identifier": "1",
        "uri": "https://ipfs.infura.io/ipfs/QmRKGJMnnLMBeT72bXU6yjG2g2MpHSvrq8pRGbZykmjSgE",
        "owner": {
          "id": "0xae198b77c760c8d547f796f57c469c0294592ab8"
        },
        "price": "323424523400000"
      }
    ]
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文