Skip to content

On-chain query aggregator/batcher in CosmWasm.

License

Notifications You must be signed in to change notification settings

AbstractSDK/multiquery

 
 

Repository files navigation

Multiquery

On-chain query aggregator/batcher in Terra.

Artboard_12x


Deployments can be found in state.json. To use this in a Typescript project, you can use @abstract-money/cosmwasm-utils package with the MultiQueryCosmWasmClient class (source). This will take all smart-queries sent through the client and batch them to lessen the load on the RPC endpoints.

Example Usage

Aggregate

Aggregate

Example Query: Link

const multicallRes: any = await terra.wasm.contractQuery(multiquery, {
  aggregate: {
    queries: [
      {
        address: "terra15dwd5mj8v59wpj0wvt23mf5efdff808c5tkal",
        data: toBase64({ config: {} }),
      },
      {
        address: "terra15dwd5mj8v59wpj0wvt233mf5efdff808c5tkal",
        data: toBase64({ epoch_state: {} }),
      },
    ],
  },
})

console.log(multicallRes)

// ---
{
  return_data: [
    {
      success: true,
      data: "eyJvd25lcl9hZGRyIjoidGVycmExNmNrZXV1N2M2Z2d1NTJhOHNlMD....",
    },
    {
      success: true,
      data: "eyJleGNoYWnZV9yYXRlIjoiMS4yMzE0NTYyNzU4MjA1MDYwMDQiLC.....",
    },
  ]
}
// ---

const decoded = multicallRes.return_data.map((e) => {
  return JSON.parse(Buffer.from(e.data, "base64").toString())
})


console.log(decoded)

// ---

[
  ({
    owner_addr: "terra16ckeuu7c6ggu52a8se005mg5c0kd2kmuun63cu",
    aterra_contract: "terra1ajt556dpzvjwl0kl5tzku3fc3p3knkg9mkv8jl",
    interest_model: "terra1m25aqupscdw2kw4tnq5ql6hexgr34mr76azh5x",
    distribution_model: "terra1u64cezah94sq3ye8y0ung28x3pxc37tv8fth7h",
    overseer_contract: "terra1qljxd0y3j3gk97025qvl3lgq8ygup4gsksvaxv",
    collector_contract: "terra1hlctcrrhcl2azxzcsns467le876cfuzam6jty4",
    distributor_contract: "terra1z7nxemcnm8kp7fs33cs7ge4wfuld307v80gypj",
    stable_denom: "uusd",
    max_borrow_factor: "0.95",
  },
  {
    exchange_rate: "1.231456275820506004",
    aterra_supply: "146558727243845",
  })
]

Try Aggregate

Aggregate with error suppression variant. If include_cause is true, data of the query will be error message in String if that query is return error. Else will return empty string.

Example Query: Link

const multicallRes: any = await terra.wasm.contractQuery(multiquery, {
  try_aggregate: {
    require_success: false, // defualt to false
    include_cause: true, // default to false
    queries: [
      {
        address: "terra15dwd5mj8v59wpj0wvt233mf5efdff808c5tkal",
        data: toBase64({ config: {} }),
      },
      {
        address: "terra15dwd5mj8v59wpj0wvt233mf5efdff808c5tkal",
        data: toBase64({ epoch_state: {} }),
      },
    ],
  },
})

const decoded = multicallRes.return_data.map((e) => {
  return e.length == 0
    ? null
    : JSON.parse(Buffer.from(e.data, "base64").toString())
})

Try Aggregate With Optional Require Success

Aggregate with specific error suppression variant. Same as try_aggregate but with element-specific error handling.

Example Query: Link

const multicallRes: any = await terra.wasm.contractQuery(multiquery, {
  try_aggregate_optional: {
    include_cause: true, // default to false
    queries: []]]]
      {
        require_success: true,
        address: "terra15dwd5mj8v59wpj0wvt233mf5efdff808c5tkal",
        data: toBase64({ config: {} })
      },
      {
        require_success: false,
        address: "terra15dwd5mj8v59wpj0wvt233mf5efdff808c5tkal",
        data: toBase64({ epoch_state: {} })
      },
    ]
  }
})

const decoded = multicallRes.return_data.map((e) => {
  return e.length == 0 ? null : JSON.parse(Buffer.from(e.data, 'base64').toString())
})

Aggregate With Block

Include block_ as prefix for query message to include block height as a result.

Example Query: Link

const multicallRes: any = await terra.wasm.contractQuery(multiquery, {
  block_aggregate: {
    queries: [
      {
        address: "terra15dwd5mj8v59wpj0wvt233mf5efdff808c5tkal",
        data: toBase64({ config: {} })
      },
      {
        address: "terra15dwd5mj8v59wpj0wvt233mf5efdff808c5tkal",
        data: toBase64({ epoch_state: {} })
      },
    ]
  }
})

console.log(multicallRes)

// --

{
  block: 8259453,
  return_data: [
    {
      success: true,
      data: 'eyJvd25lcl9hZGRyIjoidGVycmExNmNrZXV1N2M2Z2d1NTJhOHNlMDA1bWc1YzBrZDJrbXV1bjYzY3UiLCJhdGVycmFfY29udHJhY3QiOiJ0ZXJyYTFhanQ1NTZkcHp2andsMGtsNXR6a3UzZmMzcDNrbmtnOW1rdjhqbCIsImludGVyZXN0X21vZGVsIjoidGVycmExbTI1YXF1cHNjZHcya3c0dG5xNXFsNmhleGdyMzRtcjc2YXpoNXgiLCJkaXN0cmlidXRpb25fbW9kZWwiOiJ0ZXJyYTF1NjRjZXphaDk0c3EzeWU4eTB1bmcyOHgzcHhjMzd0djhmdGg3aCIsIm92ZXJzZWVyX2NvbnRyYWN0IjoidGVycmExcWxqeGQweTNqM2drOTcwMjVxdmwzbGdxOHlndXA0Z3Nrc3ZheHYiLCJjb2xsZWN0b3JfY29udHJhY3QiOiJ0ZXJyYTFobGN0Y3JyaGNsMmF6eHpjc25zNDY3bGU4NzZjZnV6YW02anR5NCIsImRpc3RyaWJ1dG9yX2NvbnRyYWN0IjoidGVycmExejdueGVtY25tOGtwN2ZzMzNjczdnZTR3ZnVsZDMwN3Y4MGd5cGoiLCJzdGFibGVfZGVub20iOiJ1dXNkIiwibWF4X2JvcnJvd19mYWN0b3IiOiIwLjk1In0='
    },
    {
      success: true,
      data: 'eyJleGNoYW5nZV9yYXRlIjoiMS4yMzIxNzc1ODQ0NTQzOTY2OTQiLCJhdGVycmFfc3VwcGx5IjoiMTQxNjE3NTE5MTk2NTY2In0='
    }
  ]
}

About

On-chain query aggregator/batcher in CosmWasm.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 93.8%
  • Just 6.2%