evmosjs icon indicating copy to clipboard operation
evmosjs copied to clipboard

feat(rest): Implement evmos and cometbft all rest interfaces for us to query information about the chain

Open luchenqun opened this issue 2 years ago • 2 comments
trafficstars

Overview

evmos and cometbft provide many interfaces for us to query information about the chain. However, unfortunately, evmosjs does not implement these interfaces in the provider package. In actual business scenarios, when these interfaces are needed, they are individually encapsulated. To solve the above problem, the packages/provider/src/rest/cometbft.ts file in evmosjs implements the related request interfaces of cometbft, and the packages/provider/src/rest/app.ts file implements the related request interfaces of evmos.

To be able to use both in Node.js and browsers, axios is used as the underlying network library.

Usage and Examples

Below is a simple code example that demonstrates how to use the provider package in evmosjs to query information about the evmos chain.

import { App, Cometbft } from '@evmos/provider'

const main = async () => {
  let data

  // Find node urls for either mainnet or testnet here:
  // https://docs.evmos.org/develop/api/networks.
  const cbft = new Cometbft({ baseURL: 'http://127.0.0.1:26657' })
  const app = new App({ baseURL: 'http://127.0.0.1:1317' })
  const address = 'evmos1qqqqhe5pnaq5qq39wqkn957aydnrm45sdn8583'

  {
    // query block info
    data = await cbft.block({ height: 1 })
    console.log('block = ', data)
  }

  {
    // query accounts
    data = await app.auth.accounts({
      'pagination.count_total': true,
      'pagination.limit': '3',
    })
    console.log('accounts = ', data)
  }

  {
    // query account balances
    data = await app.bank.allBalances(address, {
      'pagination.limit': '10',
    })
    console.log(`${address} all balances = `, data)
  }

  try {
    // a invalid query tendminter block for error
    data = await cbft.block({ height: -1 })
  } catch (error) {
    console.log('cbft.block error = ', error)
  }

  try {
    // a invalid query account balances for error
    data = await app.bank.allBalances(address + 'ERROR', {
      'pagination.limit': '10',
    })
  } catch (error) {
    console.log('app.bank.allBalances error = ', error)
  }

  try {
    const errBaseUrlApp = new App({ baseURL: 'http://127.0.0.1:12345' })
    await errBaseUrlApp.bank.allBalances(address, {
      'pagination.limit': '10',
    })
  } catch (error) {
    console.log('err base url app error = ', error)
  }

  try {
    const errBaseUrlCbft = new Cometbft({ baseURL: 'http://127.0.0.1:23456' })
    await errBaseUrlCbft.block({ height: 1 })
  } catch (error) {
    console.log('err base url cometbft error = ', error)
  }
}

main()
  .then()
  .catch((err) => console.error('main error = ', err))

If the returned data is correct, you can directly retrieve the values from the returned Object. For example, if the data returned by bank.allBalances is as follows:

{
  balances: [
    { denom: 'aevmos', amount: '1000000000000000000000' }
  ],
  pagination: { next_key: null, total: '0' }
}

If the returned data is an error, the evmos declares the error data as follows:

export interface GrpcGatewayRuntimeError {
  error?: string
  /** @format int32 */
  code?: number
  message?: string
  details?: GoogleProtobufAny[]
}

Cometbft declares the error data as follows:

/** Error Response RPC */
export type ErrorResponseRPC = {
  /** @example "Description of failure" */
  code?: number
  message?: string
  data?: string
}

luchenqun avatar Sep 10 '23 03:09 luchenqun

@GAtom22 Please do a review. If you think the implementation is reasonable, I will continue to improve the function

luchenqun avatar Sep 10 '23 03:09 luchenqun

Hey @luchenqun thanks for the contribution! Unfortunately, the team decided not to maintain this repo anymore

GAtom22 avatar Sep 13 '23 16:09 GAtom22