第3节:语法学习

小白入门:https://github.com/dukedaily/solidity-expert ,欢迎star转发,文末加V入群。

职场进阶: https://dukeweb3.com

请求格式

{
  useFlashloans(first: 5) {
    id
    user
    token
    tokenName
  }
  stakePoolClaimeds(first: 5) {
    id
    user
    amount
    token
  }
}

查询blocks

{
  simpleBlocks(first: 5) {
    id
  }
}

语法:

  1. 在查询的时候,如果是查询数组,则会默认加上复数s,用于查询

  2. 如果查询单个,则首字母变成小写。

  3. shift + 空格:查看有哪些schema可以使用。

  4. 请求语法:https://thegraph.com/docs/developer/graphql-api

  5. 同步块的时候有两种状态:syncing和pending,勿忘切换查看。

  6. stone这个老版本的graph-ts,apiVersion一定要是:0.0.4,新版本的必须是0.0.5以上

  7. event的address字段就是当前的合约地址

  8. require的字段在创建的时候,都需要显示初始化,例如BigInt的要初始化为BigInt.zero()

  9. 查询条件:

    _not
    _gt
    _lt
    _gte
    _lte
    _in
    _not_in
    _contains
    _not_contains
    _starts_with
    _ends_with
    _not_starts_with
    _not_ends_with
    

    举例:

    {
       //其中:deposit_gt 是deposit和 
      applications(where: { deposit_gt: "10000000000" }) {  _gt的组合
        id
        whitelisted
        deposit
      }
    }
    

    测试:

    query sb {
      simpleBlocks(where: { id_gt: "13712600"}){
        id
      }
    }
    

    查询闪电贷:

    {flahloanBalances{id,fee,time,balance}}
    
  10. 打印log使用技巧,必须有{}

    log.info('***MARKET*** : underlyingAddress {}', [market.underlyingAddress.toHexString()])
    

    image-20230624130010204

其他

  1. https://www.apollographql.com/blog/graphql/filtering/how-to-search-and-filter-results-with-graphql/
  2. https://www.apollographql.com/docs/studio/explorer/explorer/
  3. https://thegraph.com/docs/en/querying/querying-the-graph/

best practice

  1. https://thegraph.com/docs/en/querying/querying-best-practices/
  2. ==https://thegraph.com/docs/en/querying/graphql-api/==(实用)

specification

  1. https://spec.graphql.org/October2021/
  2. https://www.bilibili.com/video/BV1RS4y1F7zw?spm_id_from=333.337.search-card.all.click&vd_source=42fe91bf6d16ec8841b22ea520184d76

schema.graphql

type UseFlashloan @entity {
        id: ID!
        user: String!
    token: String!
    tokenName: String!
    amount: BigInt!
    fee: BigInt!
    block: BigInt!
    time: BigInt!
}

type StakePoolClaimed @entity {
        id: ID!
        user: String!
        token: String!
        block: BigInt!
        time: BigInt!
}

type FlashTotalClaimed @entity {
        id: ID!
        amount: BigInt!
        lashHash: String!
        lastTime: BigInt!
}

type MerkleDistributor @entity {
        id: ID!
        index: BigInt!
        amount: BigInt!
        settleBlocNumber: BigInt!
}

type SimpleBlock @entity {
        id: ID!
        height: BigInt!
        time: BigInt!
}

type NoHandlerUseFlashloan @entity {
        id: ID!
        list: [UseFlashloan!]!
}

type UserFlashloan @entity {
        id: ID!
        user: String!
        reward: BigInt!
        flashloan: BigInt!
        claimed: Boolean!
}

type FlashloanReward @eneity {
        id: ID!
        index: BigInt!
        amount: BigInt!
        settleBlocNumber: BigInt!
        totalAmount: BigInt!
        flashloanTotalAmount: BigInt!
        list: [UserFlashloan!]!
}

type FlashloanBalance @entity {
        id: ID!
        tokenName: String
        fee: BigInt
        balance: BigInt
        time: BigInt
}