Sui GraphQL 使用

  • sycute
  • 更新于 2024-03-18 15:07
  • 阅读 277

SuiRPC已经从JSON-RPC升级到GraphQL,本篇文章介绍下如何使用此接口

Sui RPC 已经从 JSON-RPC 升级到 GraphQL,本篇文章介绍下如何使用此接口。

1. 什么是 GraphQL

GraphQL 官网对此介绍是:GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。

更多 GraphQL 资料请访问官方网站

2. 类型对应关系

SuiJSON 是对 JSON 格式的二次封装。为了在使用上更贴近调用合约的感觉,去掉了不需要的类型

GraphQL 使用 JSON 作为输入输出的格式,而 Sui 使用 Move 语言开发智能合约,那么这两种语言格式存在一些差异,有以下类型对应关系:

Move 类型 JSON
u8, u32 Number
u64, u128, u256 String
Bool Boolean
Vector<u8>, Address, ObjectID, TypeTag, Identifier, Unsigned integer (256 bit max) String

3. 如何使用 Sui-GraphQL

GraphQL的语法详见官网,这里不赘述。https://spec.graphql.cn

这里以测试网接口为例,Sui Testnet GraphQL

Sui Mainnet GraphQL

3.1 查询交易

{
  transactionBlock(digest: "GFUw4fvPR7jji8i3pHpwB2bjzQmWP3BBdGc9y4PEfX49") {
    gasInput {
      gasSponsor {
        address
      }
      gasPrice
      gasBudget
    }
    effects {
      status
      timestamp
      checkpoint {
        sequenceNumber
      }
      epoch {
        epochId
        referenceGasPrice
      }
      events {
        nodes {
          sender {
            address
          }
          timestamp
          type {
            repr
          }
          json
        }
      }
    }
  }
}
{
  "data": {
    "transactionBlock": {
      "gasInput": {
        "gasSponsor": {
          "address": "0x75856288b7609c28a9864711ff30134706050c522ec1d8ba9e40d1833aa16045"
        },
        "gasPrice": "1000",
        "gasBudget": "4454800"
      },
      "effects": {
        "status": "SUCCESS",
        "timestamp": "2024-03-09T03:20:53.055Z",
        "checkpoint": {
          "sequenceNumber": 26051103
        },
        "epoch": {
          "epochId": 303,
          "referenceGasPrice": "1000"
        },
        "events": {
          "nodes": [
            {
              "sender": {
                "address": "0x75856288b7609c28a9864711ff30134706050c522ec1d8ba9e40d1833aa16045"
              },
              "timestamp": "2024-03-09T03:20:53.055Z",
              "type": {
                "repr": "0x17722610942c2c232781f6f233f6de0cc9332ccae174be368f6b250ee09d5157::mynft::MintNFT"
              },
              "json": {
                "object_id": "0x8651a86be0724cf6fbd85ceca11f325bbc01f4b64d3c1cd52a753e1c6cb9e9a3",
                "creator": "0x75856288b7609c28a9864711ff30134706050c522ec1d8ba9e40d1833aa16045",
                "name": "inscriptiontek"
              }
            }
          ]
        }
      }
    }
  }
}

对应浏览器中的数据 query_tx

其中事件部分 query_tx_event

3.2 查询余额

包括自行发行的其他币种

{
  address(
    address: "0x75856288b7609c28a9864711ff30134706050c522ec1d8ba9e40d1833aa16045"
  ) {
    balance(
      type: "0xc060006111016b8a020ad5b33834984a437aaa7d3c74c18e09a95d48aceab08c::coin::COIN"
    ) {
      coinObjectCount
      totalBalance
    }
    balances {
      nodes {
        coinType {
          repr
        }
        coinObjectCount
        totalBalance
      }
      pageInfo {
        endCursor
      }
    }
  }
}
{
  "data": {
    "address": {
      "balance": null,
      "balances": {
        "nodes": [
          {
            "coinType": {
              "repr": "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI"
            },
            "coinObjectCount": 1,
            "totalBalance": "104952877284"
          },
          {
            "coinType": {
              "repr": "0xd1a5b6d0dc963c61e1f0115865fc71f5a54d7ca0ab256c2500263a0ecdad8b60::coinA::COINA"
            },
            "coinObjectCount": 3,
            "totalBalance": "100000000999999"
          }
        ],
        "pageInfo": {
          "endCursor": "eyJ0IjoiMHhkMWE1YjZkMGRjOTYzYzYxZTFmMDExNTg2NWZjNzFmNWE1NGQ3Y2EwYWIyNTZjMjUwMDI2M2EwZWNkYWQ4YjYwOjpjb2luQTo6Q09JTkEiLCJjIjoyNjgyOTQ3Mn0"
        }
      }
    }
  }
}

对应浏览器中的数据query_coin

3.3 查询动态字段

query ($id: SuiAddress!) {
  owner(address: $id) {
    dynamicFields {
      nodes{
        name{
          ...Value
        }
      }
    }
  }
}

fragment Value on MoveValue {
  type {
    repr
  }
  json
}

Variables填入

{
  "id": "0xed399a480963dca70935f71f98216a75518ba90f81d75d1ffae44751fbc72171"
}
{
  "data": {
    "owner": {
      "dynamicFields": {
        "nodes": [
          {
            "name": {
              "type": {
                "repr": "address"
              },
              "json": "0x017eba911b6e5a4faf9afa96983640a1eadbff4d395bf4a7a943bfef42f82299"
            }
          },
          {
            "name": {
              "type": {
                "repr": "address"
              },
              "json": "0xd86fd0045fcd02fb3dbd936fe902d5372c81cbb39c84d0c9f7ca8f4cb7cb471a"
            }
          }
        ]
      }
    }
  }
}

对比区块浏览器中的数据

动态字段查询

4. 参考

更多使用样例参考官方代码仓库


StarTrek 星航计划 https://github.com/movefuns/SuiStartrek

点赞 1
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
sycute
sycute
0x80d6...FdC9
电报联系 @luosanzhang