From 19163869293268011052e5f62f7d81a47f84f130 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 5 Sep 2024 09:06:20 +0000 Subject: [PATCH] Use pagination in query to get records (#27) Part of [Create a public laconicd testnet](https://www.notion.so/Create-a-public-laconicd-testnet-896a11bdd8094eff8f1b49c0be0ca3b8) Handles cerc-io/laconic-console#59 Requires https://git.vdb.to/cerc-io/laconicd/pulls/58 Co-authored-by: IshaVenikar Reviewed-on: https://git.vdb.to/cerc-io/registry-sdk/pulls/27 Co-authored-by: Prathamesh Musale Co-committed-by: Prathamesh Musale --- package.json | 2 +- src/bond.test.ts | 17 +++++++---- src/index.ts | 17 +++++++---- src/laconic-client.ts | 4 +-- src/onboarding.test.ts | 5 ++-- src/registry-client.ts | 43 +++++++++++++++++++++------- src/types/cerc/onboarding/message.ts | 2 ++ 7 files changed, 63 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 569d050..09ee73a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/registry-sdk", - "version": "0.2.7", + "version": "0.2.8", "main": "dist/index.js", "types": "dist/index.d.ts", "repository": "git@github.com:cerc-io/registry-sdk.git", diff --git a/src/bond.test.ts b/src/bond.test.ts index ce4524c..6bbeb7f 100644 --- a/src/bond.test.ts +++ b/src/bond.test.ts @@ -54,15 +54,20 @@ const bondTests = () => { test('Query bonds.', async () => { const bonds = await registry.queryBonds(); expect(bonds).toBeDefined(); - const bond = bonds.filter((bond: any) => bond.id === bond1.id); - expect(bond).toBeDefined(); + + const filteredBonds = bonds.filter((bond: any) => bond.id === bond1.id); + expect(filteredBonds).toHaveLength(1); + expect(filteredBonds[0]).toMatchObject({ id: bond1.id, owner: bond1.owner }); }); test('Query bonds by owner.', async () => { - const bonds = await registry.queryBonds({ owner: bond1.owner }); - expect(bonds).toBeDefined(); - const bond = bonds.filter((bond: any) => bond.id === bond1.id); - expect(bond).toBeDefined(); + const [result] = await registry.queryBondsByOwner([bond1.owner]); + expect(result).toBeDefined(); + expect(result.bonds).toBeDefined(); + + const filteredBonds = result.bonds.filter((bond: any) => bond.id === bond1.id); + expect(filteredBonds).toHaveLength(1); + expect(filteredBonds[0]).toMatchObject({ id: bond1.id, owner: bond1.owner }); }); test('Refill bond.', async () => { diff --git a/src/index.ts b/src/index.ts index 17047ff..09c5e1a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -107,8 +107,8 @@ export class Registry { /** * Get records by attributes. */ - async queryRecords (attributes: { [key: string]: any }, all = false, refs = false) { - return this._client.queryRecords(attributes, all, refs); + async queryRecords (attributes: { [key: string]: any }, all = false, refs = false, limit?: number, offset?: number) { + return this._client.queryRecords(attributes, all, refs, limit, offset); } /** @@ -186,10 +186,17 @@ export class Registry { } /** - * Query bonds by attributes. + * Query bonds. */ - async queryBonds (attributes = {}) { - return this._client.queryBonds(attributes); + async queryBonds () { + return this._client.queryBonds(); + } + + /** + * Query bonds by owner(s). + */ + async queryBondsByOwner (owners: string[]) { + return this._client.queryBondsByOwner(owners); } /** diff --git a/src/laconic-client.ts b/src/laconic-client.ts index 551393d..ae8f90c 100644 --- a/src/laconic-client.ts +++ b/src/laconic-client.ts @@ -13,7 +13,7 @@ import { MsgCancelBondEncodeObject, MsgCreateBondEncodeObject, MsgRefillBondEnco import { Coin } from './proto/cosmos/base/v1beta1/coin'; import { MsgAssociateBondEncodeObject, MsgDeleteNameEncodeObject, MsgDissociateBondEncodeObject, MsgDissociateRecordsEncodeObject, MsgReassociateRecordsEncodeObject, MsgReserveAuthorityEncodeObject, MsgSetAuthorityBondEncodeObject, MsgSetNameEncodeObject, MsgSetRecordEncodeObject, registryTypes, typeUrlMsgAssociateBond, typeUrlMsgDeleteName, typeUrlMsgDissociateBond, typeUrlMsgDissociateRecords, typeUrlMsgReassociateRecords, typeUrlMsgReserveAuthority, typeUrlMsgSetAuthorityBond, typeUrlMsgSetName, typeUrlMsgSetRecord, NAMESERVICE_ERRORS } from './types/cerc/registry/message'; import { MsgCommitBidEncodeObject, MsgRevealBidEncodeObject, auctionTypes, typeUrlMsgCommitBid, typeUrlMsgRevealBid } from './types/cerc/auction/message'; -import { MsgOnboardParticipantEncodeObject, onboardingTypes, typeUrlMsgOnboardParticipant } from './types/cerc/onboarding/message'; +import { MsgOnboardParticipantEncodeObject, ONBOARDING_DISABLED_ERROR, onboardingTypes, typeUrlMsgOnboardParticipant } from './types/cerc/onboarding/message'; import { MsgAssociateBondResponse, MsgDeleteNameResponse, MsgDissociateBondResponse, MsgDissociateRecordsResponse, MsgReassociateRecordsResponse, MsgReserveAuthorityResponse, MsgSetAuthorityBondResponse, MsgSetNameResponse, MsgSetRecordResponse, Payload } from './proto/cerc/registry/v1/tx'; import { Record, Signature } from './proto/cerc/registry/v1/registry'; import { Account } from './account'; @@ -381,7 +381,7 @@ export class LaconicClient extends SigningStargateClient { } processWriteError (error: string) { - const errorMessage = NAMESERVICE_ERRORS.find(message => error.includes(message)); + const errorMessage = [...NAMESERVICE_ERRORS, ONBOARDING_DISABLED_ERROR].find(message => error.includes(message)); if (!errorMessage) { console.error(error); diff --git a/src/onboarding.test.ts b/src/onboarding.test.ts index 25e6f51..ff51ca6 100644 --- a/src/onboarding.test.ts +++ b/src/onboarding.test.ts @@ -2,7 +2,7 @@ import { Wallet } from 'ethers'; import { DirectSecp256k1Wallet, AccountData as CosmosAccount } from '@cosmjs/proto-signing'; -import { Registry, Account } from './index'; +import { Registry, Account, ONBOARDING_DISABLED_ERROR } from './index'; import { getConfig } from './testing/helper'; import { Participant } from './proto/cerc/onboarding/v1/onboarding'; @@ -80,7 +80,6 @@ const onboardingDisabledTests = () => { }); test('Error on onboarding attempt.', async () => { - const errorMsg = 'Validator onboarding is disabled: invalid request'; const mnemonic = Account.generateMnemonic(); ethWallet = Wallet.fromMnemonic(mnemonic); @@ -100,7 +99,7 @@ const onboardingDisabledTests = () => { kycId: DUMMY_KYC_ID }, privateKey, fee); } catch (error: any) { - expect(error.toString()).toContain(errorMsg); + expect(error.toString()).toContain(ONBOARDING_DISABLED_ERROR); } }); diff --git a/src/registry-client.ts b/src/registry-client.ts index 23c7776..08ebefa 100644 --- a/src/registry-client.ts +++ b/src/registry-client.ts @@ -241,13 +241,13 @@ export class RegistryClient { /** * Get records by attributes. */ - async queryRecords (attributes: { [key: string]: any }, all = false, refs = false) { + async queryRecords (attributes: { [key: string]: any }, all = false, refs = false, limit?: number, offset?: number) { if (!attributes) { attributes = {}; } - const query = `query ($attributes: [KeyValueInput!], $all: Boolean) { - queryRecords(attributes: $attributes, all: $all) { + const query = `query ($attributes: [KeyValueInput!], $all: Boolean, $limit: Int, $offset: Int) { + queryRecords(attributes: $attributes, all: $all, limit: $limit, offset: $offset) { id names owners @@ -261,7 +261,9 @@ export class RegistryClient { const variables = { attributes: Util.toGQLAttributes(attributes), - all + all, + limit, + offset }; let result = (await this._graph(query)(variables)).queryRecords; @@ -425,11 +427,11 @@ export class RegistryClient { } /** - * Get bonds by attributes. + * Get bonds. */ - async queryBonds (attributes = {}) { - const query = `query ($attributes: [KeyValueInput!]) { - queryBonds(attributes: $attributes) { + async queryBonds () { + const query = `query { + queryBonds { id owner balance { @@ -439,11 +441,32 @@ export class RegistryClient { } }`; + return RegistryClient.getResult(this._graph(query)({}), 'queryBonds'); + } + + /** + * Get bonds by owner(s). + */ + async queryBondsByOwner (ownerAddresses: string[]) { + const query = `query ($ownerAddresses: [String!]) { + queryBondsByOwner(ownerAddresses: $ownerAddresses) { + owner + bonds { + id + owner + balance { + type + quantity + } + } + } + }`; + const variables = { - attributes: Util.toGQLAttributes(attributes) + ownerAddresses }; - return RegistryClient.getResult(this._graph(query)(variables), 'queryBonds'); + return RegistryClient.getResult(this._graph(query)(variables), 'queryBondsByOwner'); } /** diff --git a/src/types/cerc/onboarding/message.ts b/src/types/cerc/onboarding/message.ts index 750fca2..1056704 100644 --- a/src/types/cerc/onboarding/message.ts +++ b/src/types/cerc/onboarding/message.ts @@ -15,6 +15,8 @@ export interface MsgOnboardParticipantEncodeObject extends EncodeObject { readonly value: MsgOnboardParticipant; } +export const ONBOARDING_DISABLED_ERROR = 'Onboarding is disabled'; + interface ethPayload { address: string msg: string