From 0a543fb2cf3b9f09db114db0df8d2e794f45780f Mon Sep 17 00:00:00 2001 From: Geoff Lee <11830048+Vritra4@users.noreply.github.com> Date: Wed, 13 Jul 2022 13:59:42 +0900 Subject: [PATCH 1/4] rename ibc queries --- src/client/lcd/api/AuthAPI.spec.ts | 12 +++++ src/client/lcd/api/AuthAPI.ts | 33 ++++++++++++ src/client/lcd/api/BankAPI.spec.ts | 8 +++ src/client/lcd/api/BankAPI.ts | 37 ++++++++++++++ src/client/lcd/api/IbcAPI.spec.ts | 32 ++++++------ src/client/lcd/api/IbcAPI.ts | 35 +++++++------ src/client/lcd/api/UpgradeAPI.spec.ts | 31 ++++++++++++ src/client/lcd/api/UpgradeAPI.ts.ts | 73 +++++++++++++++++++++++++++ src/client/lcd/api/WasmAPI.ts | 7 ++- src/core/index.ts | 3 ++ src/core/upgrade/index.ts | 2 + 11 files changed, 240 insertions(+), 33 deletions(-) create mode 100644 src/client/lcd/api/UpgradeAPI.spec.ts create mode 100644 src/client/lcd/api/UpgradeAPI.ts.ts create mode 100644 src/core/upgrade/index.ts diff --git a/src/client/lcd/api/AuthAPI.spec.ts b/src/client/lcd/api/AuthAPI.spec.ts index 248bee03d..d4624bdc9 100644 --- a/src/client/lcd/api/AuthAPI.spec.ts +++ b/src/client/lcd/api/AuthAPI.spec.ts @@ -40,4 +40,16 @@ describe('AuthAPI', () => { ); }); }); + + describe('parameters', () => { + it('parameters', async () => { + const param = await auth.parameters(); + + expect(param.max_memo_characters).toBeGreaterThanOrEqual(0); + expect(param.tx_sig_limit).toBeGreaterThanOrEqual(0); + expect(param.tx_size_cost_per_byte).toBeGreaterThanOrEqual(0); + expect(param.sig_verify_cost_ed25519).toBeGreaterThanOrEqual(0); + expect(param.sig_verify_cost_secp256k1).toBeGreaterThanOrEqual(0); + }); + }); }); diff --git a/src/client/lcd/api/AuthAPI.ts b/src/client/lcd/api/AuthAPI.ts index 484df0f88..5462cbd98 100644 --- a/src/client/lcd/api/AuthAPI.ts +++ b/src/client/lcd/api/AuthAPI.ts @@ -11,6 +11,24 @@ import { BaseAPI } from './BaseAPI'; import { APIParams } from '../APIRequester'; import { LCDClient } from '../LCDClient'; +export interface AuthParams { + max_memo_characters: number; + tx_sig_limit: number; + tx_size_cost_per_byte: number; + sig_verify_cost_ed25519: number; + sig_verify_cost_secp256k1: number; +} + +export namespace AuthParams { + export interface Data { + max_memo_characters: string; + tx_sig_limit: string; + tx_size_cost_per_byte: string; + sig_verify_cost_ed25519: string; + sig_verify_cost_secp256k1: string; + } +} + export class AuthAPI extends BaseAPI { constructor(public lcd: LCDClient) { super(lcd.apiRequester); @@ -35,4 +53,19 @@ export class AuthAPI extends BaseAPI { }>(`/cosmos/auth/v1beta1/accounts/${address}`, params); return Account.fromData(account, this.lcd.config.isClassic); } + + public async parameters(params: APIParams = {}): Promise { + if (this.lcd.config.isClassic) { + throw new Error('Not supported for the network'); + } + return this.c + .get<{ params: AuthParams.Data }>(`/cosmos/auth/v1beta1/params`, params) + .then(({ params: d }) => ({ + max_memo_characters: Number.parseInt(d.max_memo_characters), + tx_sig_limit: Number.parseInt(d.tx_sig_limit), + tx_size_cost_per_byte: Number.parseInt(d.tx_size_cost_per_byte), + sig_verify_cost_ed25519: Number.parseInt(d.sig_verify_cost_ed25519), + sig_verify_cost_secp256k1: Number.parseInt(d.sig_verify_cost_secp256k1), + })); + } } diff --git a/src/client/lcd/api/BankAPI.spec.ts b/src/client/lcd/api/BankAPI.spec.ts index 03ff10764..3e81e3a9a 100644 --- a/src/client/lcd/api/BankAPI.spec.ts +++ b/src/client/lcd/api/BankAPI.spec.ts @@ -22,4 +22,12 @@ describe('BankAPI', () => { const totalSupply = await bank.total(); expect(totalSupply[0].toArray().length).toBeGreaterThan(0); }); + + describe('parameters', () => { + it('parameters', async () => { + const param = await bank.parameters(); + + expect(param.default_send_enabled).toBeDefined(); + }); + }); }); diff --git a/src/client/lcd/api/BankAPI.ts b/src/client/lcd/api/BankAPI.ts index 794128313..e664fb35d 100644 --- a/src/client/lcd/api/BankAPI.ts +++ b/src/client/lcd/api/BankAPI.ts @@ -3,6 +3,29 @@ import { Coins, AccAddress } from '../../../core'; import { APIParams, Pagination, PaginationOptions } from '../APIRequester'; import { LCDClient } from '../LCDClient'; +export interface SendEnabled { + denom: string; + enabled: boolean; +} + +export namespace SendEnabled { + export interface Data { + denom: string; + enabled: boolean; + } +} +export interface BankParams { + send_enabled: SendEnabled[]; + default_send_enabled: boolean; +} + +export namespace BankParams { + export interface Data { + send_enabled: SendEnabled.Data[]; + default_send_enabled: boolean; + } +} + export class BankAPI extends BaseAPI { constructor(public lcd: LCDClient) { super(lcd.apiRequester); @@ -56,4 +79,18 @@ export class BankAPI extends BaseAPI { }>(`/cosmos/bank/v1beta1/spendable_balances/${address}`, params) .then(d => [Coins.fromData(d.balances), d.pagination]); } + + public async parameters(params: APIParams = {}): Promise { + if (this.lcd.config.isClassic) { + throw new Error('Not supported for the network'); + } + return this.c + .get<{ params: BankParams.Data }>(`/cosmos/bank/v1beta1/params`, params) + .then(({ params: d }) => ({ + send_enabled: d.send_enabled, + default_send_enabled: d.default_send_enabled, + })); + } + + // TODO: TBD: implement denoms_medata? } diff --git a/src/client/lcd/api/IbcAPI.spec.ts b/src/client/lcd/api/IbcAPI.spec.ts index a0373ae37..031f7e814 100644 --- a/src/client/lcd/api/IbcAPI.spec.ts +++ b/src/client/lcd/api/IbcAPI.spec.ts @@ -15,26 +15,26 @@ describe('IbcClientAPI', () => { }); it('client_states', async () => { - const res = await ibc.client_states(); + const res = await ibc.clientStates(); expect(res).not.toBeNull(); expect(res).not.toBeUndefined(); }); - // it('client_state', async () => { - // const res = await ibc.client_state('07-tendermint-0'); - // expect(res).not.toBeNull(); - // expect(res).not.toBeUndefined(); - // }); + it('client_state', async () => { + const res = await ibc.clientState('07-tendermint-0'); + expect(res).not.toBeNull(); + expect(res).not.toBeUndefined(); + }); - // it('client_status', async () => { - // const res = await ibc.client_status('07-tendermint-0'); - // expect(res).not.toBeNull(); - // expect(res).not.toBeUndefined(); - // }); + it('client_status', async () => { + const res = await ibc.clientStatus('07-tendermint-0'); + expect(res).not.toBeNull(); + expect(res).not.toBeUndefined(); + }); - // it('consensus_states', async () => { - // const res = await ibc.consensus_states('07-tendermint-0'); - // expect(res).not.toBeNull(); - // expect(res).not.toBeUndefined(); - // }); + it('consensus_states', async () => { + const res = await ibc.consensusStates('07-tendermint-0'); + expect(res).not.toBeNull(); + expect(res).not.toBeUndefined(); + }); }); diff --git a/src/client/lcd/api/IbcAPI.ts b/src/client/lcd/api/IbcAPI.ts index 9cfc993f5..65863fca5 100644 --- a/src/client/lcd/api/IbcAPI.ts +++ b/src/client/lcd/api/IbcAPI.ts @@ -26,7 +26,6 @@ export namespace Status { } export class IbcAPI extends BaseAPI { - constructor(public lcd: LCDClient) { super(lcd.apiRequester); } @@ -42,7 +41,7 @@ export class IbcAPI extends BaseAPI { })); } - public async client_states( + public async clientStates( params: Partial = {} ): Promise<[IdentifiedClientState[], Pagination]> { return this.c @@ -56,7 +55,7 @@ export class IbcAPI extends BaseAPI { ]); } - public async client_state( + public async clientState( client_id: string, params: APIParams = {} ): Promise { @@ -67,7 +66,7 @@ export class IbcAPI extends BaseAPI { .then(); } - public async client_status( + public async clientStatus( client_id: string, params: APIParams = {} ): Promise { @@ -78,7 +77,7 @@ export class IbcAPI extends BaseAPI { .then(); } - public async consensus_states( + public async consensusStates( client_id: string, params: Partial = {} ): Promise<[ClientConsensusStates, Pagination]> { @@ -92,22 +91,28 @@ export class IbcAPI extends BaseAPI { /** * Gets paramaters for interchain account controller. */ - public async interchainAccountControllerParameters(params: APIParams = {}): Promise { + public async interchainAccountControllerParameters( + params: APIParams = {} + ): Promise { return this.c - .get<{ params: ControllerParams.Data }>(`/ibc/apps/interchain_accounts/controller/v1/params`, params) - .then(({ params: d }) => ( - ControllerParams.fromData(d) - )); + .get<{ params: ControllerParams.Data }>( + `/ibc/apps/interchain_accounts/controller/v1/params`, + params + ) + .then(({ params: d }) => ControllerParams.fromData(d)); } /** * Gets paramaters for interchain account host. */ - public async interchainAccountHostParameters(params: APIParams = {}): Promise { + public async interchainAccountHostParameters( + params: APIParams = {} + ): Promise { return this.c - .get<{ params: HostParams.Data }>(`/ibc/apps/interchain_accounts/host/v1/params`, params) - .then(({ params: d }) => ( - HostParams.fromData(d) - )); + .get<{ params: HostParams.Data }>( + `/ibc/apps/interchain_accounts/host/v1/params`, + params + ) + .then(({ params: d }) => HostParams.fromData(d)); } } diff --git a/src/client/lcd/api/UpgradeAPI.spec.ts b/src/client/lcd/api/UpgradeAPI.spec.ts new file mode 100644 index 000000000..cba08738e --- /dev/null +++ b/src/client/lcd/api/UpgradeAPI.spec.ts @@ -0,0 +1,31 @@ +import { Plan } from '../../../core'; +import { LCDClient } from '../LCDClient'; +import { UpgradeAPI } from './UpgradeAPI.ts'; + +const terra = new LCDClient({ + chainID: 'pisco-1', + URL: 'https://pisco-lcd.terra.dev', +}); +const upgrade = new UpgradeAPI(terra); + +describe('UpgradeAPI', () => { + describe('applied_plan', () => { + it('0 for invalid name', async () => { + const height = await upgrade.appliedPlan('there_is_no_plan_like_this'); + expect(height).toEqual(0); + }); + }); + + describe('current_plan', () => { + it('null plan', async () => { + const plan = await upgrade.currentPlan(); + expect(plan == null || plan instanceof Plan); + }); + }); + + describe('node_versions', () => { + it('module count', async () => { + expect(await upgrade.moduleVersions()).toHaveLength(21); + }); + }); +}); diff --git a/src/client/lcd/api/UpgradeAPI.ts.ts b/src/client/lcd/api/UpgradeAPI.ts.ts new file mode 100644 index 000000000..65bf5066a --- /dev/null +++ b/src/client/lcd/api/UpgradeAPI.ts.ts @@ -0,0 +1,73 @@ +import { BaseAPI } from './BaseAPI'; +import { Coins, Plan } from '../../../core'; +import { APIParams, Pagination, PaginationOptions } from '../APIRequester'; +import { LCDClient } from '../LCDClient'; +import { publicEncrypt } from 'crypto'; + +export interface ModuleVersion { + name: string; + version: number; +} + +export namespace ModuleVersion { + export interface Data { + name: string; + version: string; + } +} + +export class UpgradeAPI extends BaseAPI { + constructor(public lcd: LCDClient) { + super(lcd.apiRequester); + } + + /** + * queries a previously applied upgrade plan by its name. + * it returns the height of the plan. if there's no plan with given name, it returns 0. + */ + public async appliedPlan( + name: string, + params: Partial = {} + ): Promise { + return this.c + .get<{ height: string }>( + `/cosmos/upgrade/v1beta1/applied_plan/${name}`, + params + ) + .then(d => Number.parseInt(d.height)); + } + + /** + * Look up the current plan + */ + public async currentPlan(params: APIParams = {}): Promise { + if (this.lcd.config.isClassic) { + throw new Error('Not supported for the network'); + } + return this.c + .get<{ + plan: Plan.Data | null; + }>(`/cosmos/upgrade/v1beta1/current_plan`, params) + .then(d => (d.plan ? Plan.fromData(d.plan) : null)); + } + + /** + * Look up the versions of the modules + */ + public async moduleVersions( + params: APIParams = {} + ): Promise { + if (this.lcd.config.isClassic) { + throw new Error('Not supported for the network'); + } + return this.c + .get<{ + module_versions: ModuleVersion.Data[]; + }>(`/cosmos/upgrade/v1beta1/module_versions`, params) + .then(d => + d.module_versions.map(mv => { + return { name: mv.name, version: Number.parseInt(mv.version) }; + }) + ); + } +} diff --git a/src/client/lcd/api/WasmAPI.ts b/src/client/lcd/api/WasmAPI.ts index 4bb96bffe..13df97568 100644 --- a/src/client/lcd/api/WasmAPI.ts +++ b/src/client/lcd/api/WasmAPI.ts @@ -235,11 +235,14 @@ export class WasmAPI extends BaseAPI { } return this.c .get<{ result: QueryResult.Data }>( - `/cosmwasm/wasm/v1/contract/${contractAddress}/raw/${query_data}`, + `/cosmwasm/wasm/v1/contract/${contractAddress}/raw/${Buffer.from( + query_data, + 'utf-8' + ).toString('base64')}`, params ) .then(({ result: d }) => ({ - data: d.data, + data: Buffer.from(d.data, 'base64').toString(), })); } diff --git a/src/core/index.ts b/src/core/index.ts index c5a4bfe80..7f3de68b5 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -69,6 +69,9 @@ export * from './staking/Validator'; // Treasury export * from './treasury/PolicyConstraints'; +// Upgrade +export * from './upgrade'; + // WASM export * from './wasm/msgs'; diff --git a/src/core/upgrade/index.ts b/src/core/upgrade/index.ts new file mode 100644 index 000000000..9da7fad72 --- /dev/null +++ b/src/core/upgrade/index.ts @@ -0,0 +1,2 @@ +export * from './Plan'; +export * from './proposals'; From 250a9b8e6e4ef8b073cb9188c2509671ffb65d7e Mon Sep 17 00:00:00 2001 From: Geoff Lee <11830048+Vritra4@users.noreply.github.com> Date: Wed, 13 Jul 2022 14:58:14 +0900 Subject: [PATCH 2/4] add ibc channel query --- src/client/lcd/api/IbcAPI.spec.ts | 22 ++++++++++++++++++++++ src/client/lcd/api/IbcAPI.ts | 19 +++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/client/lcd/api/IbcAPI.spec.ts b/src/client/lcd/api/IbcAPI.spec.ts index 031f7e814..1398aed6f 100644 --- a/src/client/lcd/api/IbcAPI.spec.ts +++ b/src/client/lcd/api/IbcAPI.spec.ts @@ -37,4 +37,26 @@ describe('IbcClientAPI', () => { expect(res).not.toBeNull(); expect(res).not.toBeUndefined(); }); + + it('ica host paramaters', async () => { + const res = await ibc.interchainAccountHostParameters(); + expect(res).not.toBeNull(); + expect(res).not.toBeUndefined(); + }); + + /* + it('ica controller paramaters', async () => { + const res = await ibc.interchainAccountControllerParameters(); + expect(res).not.toBeNull(); + expect(res).not.toBeUndefined(); + }); + */ + + it('channels', async () => { + const [res, _] = await ibc.channels(); + console.log(res); + expect(res).not.toBeNull(); + expect(res).not.toBeUndefined(); + expect(res.length).toBeGreaterThan(0); + }); }); diff --git a/src/client/lcd/api/IbcAPI.ts b/src/client/lcd/api/IbcAPI.ts index 65863fca5..df2544cf4 100644 --- a/src/client/lcd/api/IbcAPI.ts +++ b/src/client/lcd/api/IbcAPI.ts @@ -3,8 +3,9 @@ import { APIParams, Pagination, PaginationOptions } from '../APIRequester'; import { IdentifiedClientState } from '../../../core/ibc/msgs/client/IdentifiedClient'; import { ClientConsensusStates } from '../../../core/ibc/msgs/client/ClientConsensusStates'; import { LCDClient } from '../LCDClient'; -import { Params as ControllerParams } from '../../../core/ibc/applications/interchain-account/controller/Params'; +//import { Params as ControllerParams } from '../../../core/ibc/applications/interchain-account/controller/Params'; import { Params as HostParams } from '../../../core/ibc/applications/interchain-account/host/Params'; +import { Channel } from '../../../core/ibc/msgs/channel/Channel'; export interface IbcClientParams { allowed_clients: string[]; } @@ -30,6 +31,17 @@ export class IbcAPI extends BaseAPI { super(lcd.apiRequester); } + public async channels( + params: APIParams = {} + ): Promise<[Channel[], Pagination]> { + return this.c + .get<{ + channels: Channel.Data[]; + pagination: Pagination; + }>(`/ibc/core/channel/v1/channels`, params) + .then(d => [d.channels.map(Channel.fromData), d.pagination]); + } + /** * Gets the current transfer application parameters. */ @@ -88,9 +100,11 @@ export class IbcAPI extends BaseAPI { }>(`/ibc/core/client/v1/consensus_states/${client_id}`, params) .then(); } + /** * Gets paramaters for interchain account controller. - */ + * NOTE: CURRENTLY LCD DOESN'T SERVE THE ENDPOINT + /* public async interchainAccountControllerParameters( params: APIParams = {} ): Promise { @@ -101,6 +115,7 @@ export class IbcAPI extends BaseAPI { ) .then(({ params: d }) => ControllerParams.fromData(d)); } + */ /** * Gets paramaters for interchain account host. From 08735a1d312a722a2c9b8e0f370cf4c95a7d5168 Mon Sep 17 00:00:00 2001 From: Geoff Lee <11830048+Vritra4@users.noreply.github.com> Date: Wed, 13 Jul 2022 17:37:01 +0900 Subject: [PATCH 3/4] refactor ibc and add 2 ibc queries --- src/client/lcd/api/IbcAPI.spec.ts | 8 +- src/client/lcd/api/IbcAPI.ts | 21 ++- .../ibc-transfer/msgs/MsgTransfer.spec.ts | 2 +- src/core/ibc-transfer/msgs/MsgTransfer.ts | 2 +- .../transfer/v1/msgs/MsgTransfer.spec.ts | 29 ++-- .../transfer/v1/msgs/MsgTransfer.ts | 2 +- .../ibc/{msgs => core}/channel/Channel.ts | 4 +- .../{msgs => core}/channel/Counterparty.ts | 0 src/core/ibc/{msgs => core}/channel/Packet.ts | 0 src/core/ibc/core/channel/index.ts | 2 + .../client/ClientConsensusStates.ts | 0 .../client/ConsensusStateWithHeight.ts | 0 src/core/ibc/{msgs => core}/client/Height.ts | 0 .../{msgs => core}/client/IdentifiedClient.ts | 0 src/core/ibc/core/commitment/index.ts | 2 + .../{msgs => core}/connection/Counterparty.ts | 2 +- .../core/connection/IdentifiedConnection.ts | 143 ++++++++++++++++++ .../ibc/{msgs => core}/connection/Version.ts | 0 src/core/ibc/core/connection/index.ts | 3 + src/core/ibc/core/index.ts | 3 + src/core/ibc/lightclient/index.ts | 1 + .../tendermint}/Header.ts | 4 +- .../msgs/channel/MsgChannelCloseConfirm.ts | 2 +- .../ibc/msgs/channel/MsgChannelOpenAck.ts | 2 +- .../ibc/msgs/channel/MsgChannelOpenConfirm.ts | 2 +- .../ibc/msgs/channel/MsgChannelOpenInit.ts | 2 +- .../ibc/msgs/channel/MsgChannelOpenTry.ts | 4 +- .../msgs/channel/MsgRecvAcknowledgement.ts | 4 +- src/core/ibc/msgs/channel/MsgRecvPacket.ts | 4 +- src/core/ibc/msgs/channel/MsgTimeout.ts | 4 +- src/core/ibc/msgs/channel/MsgTimeoutClose.ts | 4 +- src/core/ibc/msgs/client/MsgUpdateClient.ts | 2 +- .../msgs/connection/MsgConnectionOpenAck.ts | 4 +- .../connection/MsgConnectionOpenConfirm.ts | 2 +- .../msgs/connection/MsgConnectionOpenInit.ts | 4 +- .../msgs/connection/MsgConnectionOpenTry.ts | 6 +- 36 files changed, 227 insertions(+), 47 deletions(-) rename src/core/ibc/{msgs => core}/channel/Channel.ts (97%) rename src/core/ibc/{msgs => core}/channel/Counterparty.ts (100%) rename src/core/ibc/{msgs => core}/channel/Packet.ts (100%) create mode 100644 src/core/ibc/core/channel/index.ts rename src/core/ibc/{msgs => core}/client/ClientConsensusStates.ts (100%) rename src/core/ibc/{msgs => core}/client/ConsensusStateWithHeight.ts (100%) rename src/core/ibc/{msgs => core}/client/Height.ts (100%) rename src/core/ibc/{msgs => core}/client/IdentifiedClient.ts (100%) create mode 100644 src/core/ibc/core/commitment/index.ts rename src/core/ibc/{msgs => core}/connection/Counterparty.ts (97%) create mode 100644 src/core/ibc/core/connection/IdentifiedConnection.ts rename src/core/ibc/{msgs => core}/connection/Version.ts (100%) create mode 100644 src/core/ibc/core/connection/index.ts create mode 100644 src/core/ibc/core/index.ts create mode 100644 src/core/ibc/lightclient/index.ts rename src/core/ibc/{msgs/client => lightclient/tendermint}/Header.ts (96%) diff --git a/src/client/lcd/api/IbcAPI.spec.ts b/src/client/lcd/api/IbcAPI.spec.ts index 1398aed6f..145f0a3c1 100644 --- a/src/client/lcd/api/IbcAPI.spec.ts +++ b/src/client/lcd/api/IbcAPI.spec.ts @@ -54,7 +54,13 @@ describe('IbcClientAPI', () => { it('channels', async () => { const [res, _] = await ibc.channels(); - console.log(res); + expect(res).not.toBeNull(); + expect(res).not.toBeUndefined(); + expect(res.length).toBeGreaterThan(0); + }); + + it('connections', async () => { + const [res, _] = await ibc.connections(); expect(res).not.toBeNull(); expect(res).not.toBeUndefined(); expect(res.length).toBeGreaterThan(0); diff --git a/src/client/lcd/api/IbcAPI.ts b/src/client/lcd/api/IbcAPI.ts index df2544cf4..31e6c0100 100644 --- a/src/client/lcd/api/IbcAPI.ts +++ b/src/client/lcd/api/IbcAPI.ts @@ -1,11 +1,12 @@ import { BaseAPI } from './BaseAPI'; import { APIParams, Pagination, PaginationOptions } from '../APIRequester'; -import { IdentifiedClientState } from '../../../core/ibc/msgs/client/IdentifiedClient'; -import { ClientConsensusStates } from '../../../core/ibc/msgs/client/ClientConsensusStates'; +import { IdentifiedClientState } from '../../../core/ibc/core/client/IdentifiedClient'; +import { ClientConsensusStates } from '../../../core/ibc/core/client/ClientConsensusStates'; import { LCDClient } from '../LCDClient'; //import { Params as ControllerParams } from '../../../core/ibc/applications/interchain-account/controller/Params'; import { Params as HostParams } from '../../../core/ibc/applications/interchain-account/host/Params'; -import { Channel } from '../../../core/ibc/msgs/channel/Channel'; +import { Channel } from '../../../core/ibc/core/channel'; +import { IdentifiedConnection } from '../../../core/ibc/core/connection'; export interface IbcClientParams { allowed_clients: string[]; } @@ -42,6 +43,20 @@ export class IbcAPI extends BaseAPI { .then(d => [d.channels.map(Channel.fromData), d.pagination]); } + public async connections( + params: APIParams = {} + ): Promise<[IdentifiedConnection[], Pagination]> { + return this.c + .get<{ + connections: IdentifiedConnection.Data[]; + pagination: Pagination; + }>(`/ibc/core/connection/v1/connections`, params) + .then(d => [ + d.connections.map(IdentifiedConnection.fromData), + d.pagination, + ]); + } + /** * Gets the current transfer application parameters. */ diff --git a/src/core/ibc-transfer/msgs/MsgTransfer.spec.ts b/src/core/ibc-transfer/msgs/MsgTransfer.spec.ts index 7fae0ed0f..b710e6508 100644 --- a/src/core/ibc-transfer/msgs/MsgTransfer.spec.ts +++ b/src/core/ibc-transfer/msgs/MsgTransfer.spec.ts @@ -1,6 +1,6 @@ import { MsgTransfer } from './MsgTransfer'; import { Coin } from '../../Coin'; -import { Height } from '../../ibc/msgs/client/Height'; +import { Height } from '../../ibc/core/client/Height'; import { Numeric } from '../..'; describe('MsgTransfer', () => { diff --git a/src/core/ibc-transfer/msgs/MsgTransfer.ts b/src/core/ibc-transfer/msgs/MsgTransfer.ts index 65f3df608..c8f71a75e 100644 --- a/src/core/ibc-transfer/msgs/MsgTransfer.ts +++ b/src/core/ibc-transfer/msgs/MsgTransfer.ts @@ -4,7 +4,7 @@ import { Coin } from '../../Coin'; import * as Long from 'long'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; import { MsgTransfer as MsgTransfer_pb } from '@terra-money/terra.proto/ibc/applications/transfer/v1/tx'; -import { Height } from '../../ibc/msgs/client/Height'; +import { Height } from '../../ibc/core/client/Height'; import { Numeric } from '../../numeric'; /** * A basic message for transfer [[Coin]] via IBC. diff --git a/src/core/ibc/applications/transfer/v1/msgs/MsgTransfer.spec.ts b/src/core/ibc/applications/transfer/v1/msgs/MsgTransfer.spec.ts index 5b0c6f290..c9eff3db8 100644 --- a/src/core/ibc/applications/transfer/v1/msgs/MsgTransfer.spec.ts +++ b/src/core/ibc/applications/transfer/v1/msgs/MsgTransfer.spec.ts @@ -1,23 +1,26 @@ import { MsgTransfer } from './MsgTransfer'; import { Coin } from '../../../../../Coin'; -import { Height } from '../../../../msgs/client/Height'; +import { Height } from '../../../../core/client/Height'; import { Numeric } from '../../../../..'; describe('MsgTransfer', () => { it('deserializes correctly', () => { - const send = MsgTransfer.fromData({ - '@type': '/ibc.applications.transfer.v1.MsgTransfer', - source_port: 'sourceport-01', - source_channel: 'sourcehannel-01', - token: { denom: 'uluna', amount: '1024' }, - sender: 'terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v', - receiver: 'recvr17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp', - timeout_height: { - revision_number: '0', - revision_height: '0', + const send = MsgTransfer.fromData( + { + '@type': '/ibc.applications.transfer.v1.MsgTransfer', + source_port: 'sourceport-01', + source_channel: 'sourcehannel-01', + token: { denom: 'uluna', amount: '1024' }, + sender: 'terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v', + receiver: 'recvr17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp', + timeout_height: { + revision_number: '0', + revision_height: '0', + }, + timeout_timestamp: '1642663176848000000', }, - timeout_timestamp: '1642663176848000000', - }, true); + true + ); expect(send).toMatchObject({ source_port: 'sourceport-01', diff --git a/src/core/ibc/applications/transfer/v1/msgs/MsgTransfer.ts b/src/core/ibc/applications/transfer/v1/msgs/MsgTransfer.ts index 49512c0b1..774ae9872 100644 --- a/src/core/ibc/applications/transfer/v1/msgs/MsgTransfer.ts +++ b/src/core/ibc/applications/transfer/v1/msgs/MsgTransfer.ts @@ -4,7 +4,7 @@ import { Coin } from '../../../../../Coin'; import * as Long from 'long'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; import { MsgTransfer as MsgTransfer_pb } from '@terra-money/terra.proto/ibc/applications/transfer/v1/tx'; -import { Height } from '../../../../msgs/client/Height'; +import { Height } from '../../../../core/client/Height'; import { Numeric } from '../../../../../numeric'; /** * A basic message for transfer [[Coin]] via IBC. diff --git a/src/core/ibc/msgs/channel/Channel.ts b/src/core/ibc/core/channel/Channel.ts similarity index 97% rename from src/core/ibc/msgs/channel/Channel.ts rename to src/core/ibc/core/channel/Channel.ts index 6a9f06a54..ff76ed6be 100644 --- a/src/core/ibc/msgs/channel/Channel.ts +++ b/src/core/ibc/core/channel/Channel.ts @@ -90,7 +90,9 @@ export class Channel extends JSONSerializable< return new Channel( proto.state, proto.ordering, - proto.counterparty ? Counterparty.fromProto(proto.counterparty) : undefined, + proto.counterparty + ? Counterparty.fromProto(proto.counterparty) + : undefined, proto.connectionHops, proto.version ); diff --git a/src/core/ibc/msgs/channel/Counterparty.ts b/src/core/ibc/core/channel/Counterparty.ts similarity index 100% rename from src/core/ibc/msgs/channel/Counterparty.ts rename to src/core/ibc/core/channel/Counterparty.ts diff --git a/src/core/ibc/msgs/channel/Packet.ts b/src/core/ibc/core/channel/Packet.ts similarity index 100% rename from src/core/ibc/msgs/channel/Packet.ts rename to src/core/ibc/core/channel/Packet.ts diff --git a/src/core/ibc/core/channel/index.ts b/src/core/ibc/core/channel/index.ts new file mode 100644 index 000000000..1bad83311 --- /dev/null +++ b/src/core/ibc/core/channel/index.ts @@ -0,0 +1,2 @@ +export * from './Channel'; +export * from './PacketId'; diff --git a/src/core/ibc/msgs/client/ClientConsensusStates.ts b/src/core/ibc/core/client/ClientConsensusStates.ts similarity index 100% rename from src/core/ibc/msgs/client/ClientConsensusStates.ts rename to src/core/ibc/core/client/ClientConsensusStates.ts diff --git a/src/core/ibc/msgs/client/ConsensusStateWithHeight.ts b/src/core/ibc/core/client/ConsensusStateWithHeight.ts similarity index 100% rename from src/core/ibc/msgs/client/ConsensusStateWithHeight.ts rename to src/core/ibc/core/client/ConsensusStateWithHeight.ts diff --git a/src/core/ibc/msgs/client/Height.ts b/src/core/ibc/core/client/Height.ts similarity index 100% rename from src/core/ibc/msgs/client/Height.ts rename to src/core/ibc/core/client/Height.ts diff --git a/src/core/ibc/msgs/client/IdentifiedClient.ts b/src/core/ibc/core/client/IdentifiedClient.ts similarity index 100% rename from src/core/ibc/msgs/client/IdentifiedClient.ts rename to src/core/ibc/core/client/IdentifiedClient.ts diff --git a/src/core/ibc/core/commitment/index.ts b/src/core/ibc/core/commitment/index.ts new file mode 100644 index 000000000..03260c094 --- /dev/null +++ b/src/core/ibc/core/commitment/index.ts @@ -0,0 +1,2 @@ +export * from './MerklePrefix'; +export * from './MerkleRoot'; diff --git a/src/core/ibc/msgs/connection/Counterparty.ts b/src/core/ibc/core/connection/Counterparty.ts similarity index 97% rename from src/core/ibc/msgs/connection/Counterparty.ts rename to src/core/ibc/core/connection/Counterparty.ts index b0cd71cff..83cc3bf3b 100644 --- a/src/core/ibc/msgs/connection/Counterparty.ts +++ b/src/core/ibc/core/connection/Counterparty.ts @@ -1,6 +1,6 @@ import { Counterparty as Counterparty_pb } from '@terra-money/terra.proto/ibc/core/connection/v1/connection'; import { JSONSerializable } from '../../../../util/json'; -import { MerklePrefix } from '../../core/commitment/MerklePrefix'; +import { MerklePrefix } from '../commitment/MerklePrefix'; /** Counterparty defines a channel end counterparty */ export class Counterparty extends JSONSerializable< diff --git a/src/core/ibc/core/connection/IdentifiedConnection.ts b/src/core/ibc/core/connection/IdentifiedConnection.ts new file mode 100644 index 000000000..c5cd61e5a --- /dev/null +++ b/src/core/ibc/core/connection/IdentifiedConnection.ts @@ -0,0 +1,143 @@ +import { IdentifiedConnection as IdentifiedConnection_pb } from '@terra-money/terra.proto/ibc/core/connection/v1/connection'; +import { JSONSerializable } from '../../../../util/json'; +import { Version } from './Version'; +import { + State, + stateFromJSON, + stateToJSON, +} from '@terra-money/terra.proto/ibc/core/connection/v1/connection'; +import { Counterparty } from './Counterparty'; +import Long from 'long'; + +export { State, stateFromJSON, stateToJSON }; + +/** + * IdentifiedConnection defines a connection with additional connection identifier field + */ +export class IdentifiedConnection extends JSONSerializable< + IdentifiedConnection.Amino, + IdentifiedConnection.Data, + IdentifiedConnection.Proto +> { + /** + * @param id connection identifier + * @param client_id client associated with this connection. + * @param versions IBC version which can be utilised to determine encodings or protocols for channels or packets utilising this connection + * @param state current state of the connection end + * @param counterparty counterparty chain associated with this connection + * @param delay_period delay period associated with this connection + */ + constructor( + public id: string, + public client_id: string, + public versions: Version[], + public state: State, + public counterparty: Counterparty | undefined, + public delay_period: number + ) { + super(); + } + + public static fromAmino( + data: IdentifiedConnection.Amino + ): IdentifiedConnection { + const { id, client_id, versions, state, counterparty, delay_period } = data; + return new IdentifiedConnection( + id, + client_id, + versions.map(Version.fromAmino), + stateFromJSON(state), + counterparty ? Counterparty.fromAmino(counterparty) : undefined, + Number.parseInt(delay_period) + ); + } + + public toAmino(): IdentifiedConnection.Amino { + const { id, client_id, versions, state, counterparty, delay_period } = this; + const res: IdentifiedConnection.Amino = { + id, + client_id, + versions: versions.map(version => version.toAmino()), + state: stateToJSON(state), + counterparty: counterparty?.toAmino(), + delay_period: delay_period.toFixed(), + }; + return res; + } + + public static fromData( + data: IdentifiedConnection.Data + ): IdentifiedConnection { + const { id, client_id, versions, state, counterparty, delay_period } = data; + return new IdentifiedConnection( + id, + client_id, + versions.map(Version.fromData), + stateFromJSON(state), + counterparty ? Counterparty.fromData(counterparty) : undefined, + Number.parseInt(delay_period) + ); + } + + public toData(): IdentifiedConnection.Data { + const { id, client_id, versions, state, counterparty, delay_period } = this; + const res: IdentifiedConnection.Amino = { + id, + client_id, + versions: versions.map(version => version.toData()), + state: stateToJSON(state), + counterparty: counterparty?.toData(), + delay_period: delay_period.toFixed(), + }; + return res; + } + + public static fromProto( + proto: IdentifiedConnection.Proto + ): IdentifiedConnection { + return new IdentifiedConnection( + proto.id, + proto.clientId, + proto.versions.map(Version.fromProto), + proto.state, + proto.counterparty + ? Counterparty.fromProto(proto.counterparty) + : undefined, + proto.delayPeriod.toNumber() + ); + } + + public toProto(): IdentifiedConnection.Proto { + const { id, client_id, versions, state, counterparty, delay_period } = this; + return IdentifiedConnection_pb.fromPartial({ + id, + clientId: client_id, + versions: versions.map(v => v.toProto()), + state, + counterparty: counterparty?.toProto(), + delayPeriod: Long.fromNumber(delay_period), + }); + } +} + +export namespace IdentifiedConnection { + export interface Amino { + id: string; + client_id: string; + versions: Version.Amino[]; + state: string; + counterparty?: Counterparty.Amino; + delay_period: string; + } + + export interface Data { + id: string; + client_id: string; + versions: Version.Data[]; + state: string; + counterparty?: Counterparty.Data; + delay_period: string; + } + + export type Proto = IdentifiedConnection_pb; +} diff --git a/src/core/ibc/msgs/connection/Version.ts b/src/core/ibc/core/connection/Version.ts similarity index 100% rename from src/core/ibc/msgs/connection/Version.ts rename to src/core/ibc/core/connection/Version.ts diff --git a/src/core/ibc/core/connection/index.ts b/src/core/ibc/core/connection/index.ts new file mode 100644 index 000000000..a6cfda0ce --- /dev/null +++ b/src/core/ibc/core/connection/index.ts @@ -0,0 +1,3 @@ +export * from './Counterparty'; +export * from './Version'; +export * from './IdentifiedConnection'; diff --git a/src/core/ibc/core/index.ts b/src/core/ibc/core/index.ts new file mode 100644 index 000000000..978a24b69 --- /dev/null +++ b/src/core/ibc/core/index.ts @@ -0,0 +1,3 @@ +export * from './channel'; +export * from './commitment'; +export * from './connection'; diff --git a/src/core/ibc/lightclient/index.ts b/src/core/ibc/lightclient/index.ts new file mode 100644 index 000000000..e17981c08 --- /dev/null +++ b/src/core/ibc/lightclient/index.ts @@ -0,0 +1 @@ +export * from './tendermint/Header'; diff --git a/src/core/ibc/msgs/client/Header.ts b/src/core/ibc/lightclient/tendermint/Header.ts similarity index 96% rename from src/core/ibc/msgs/client/Header.ts rename to src/core/ibc/lightclient/tendermint/Header.ts index 42b7d1a6e..00ec79b55 100644 --- a/src/core/ibc/msgs/client/Header.ts +++ b/src/core/ibc/lightclient/tendermint/Header.ts @@ -1,7 +1,7 @@ import { JSONSerializable } from '../../../../util/json'; import { Header as Header_pb } from '@terra-money/terra.proto/ibc/lightclients/tendermint/v1/tendermint'; -import { Height } from './Height'; -import { SignedHeader, ValidatorSet } from './tendermint/types'; +import { Height } from '../../core/client/Height'; +import { SignedHeader, ValidatorSet } from '../../msgs/client/tendermint/types'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; /** diff --git a/src/core/ibc/msgs/channel/MsgChannelCloseConfirm.ts b/src/core/ibc/msgs/channel/MsgChannelCloseConfirm.ts index 2533a97ae..592ac7583 100644 --- a/src/core/ibc/msgs/channel/MsgChannelCloseConfirm.ts +++ b/src/core/ibc/msgs/channel/MsgChannelCloseConfirm.ts @@ -1,7 +1,7 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Height } from '../client/Height'; +import { Height } from '../../core/client/Height'; import { MsgChannelCloseConfirm as MsgChannelCloseConfirm_pb } from '@terra-money/terra.proto/ibc/core/channel/v1/tx'; /** diff --git a/src/core/ibc/msgs/channel/MsgChannelOpenAck.ts b/src/core/ibc/msgs/channel/MsgChannelOpenAck.ts index 52d25cf28..c3840e5d8 100644 --- a/src/core/ibc/msgs/channel/MsgChannelOpenAck.ts +++ b/src/core/ibc/msgs/channel/MsgChannelOpenAck.ts @@ -1,7 +1,7 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Height } from '../client/Height'; +import { Height } from '../../core/client/Height'; import { MsgChannelOpenAck as MsgChannelOpenAck_pb } from '@terra-money/terra.proto/ibc/core/channel/v1/tx'; /** diff --git a/src/core/ibc/msgs/channel/MsgChannelOpenConfirm.ts b/src/core/ibc/msgs/channel/MsgChannelOpenConfirm.ts index 30a244f14..0a8123e3f 100644 --- a/src/core/ibc/msgs/channel/MsgChannelOpenConfirm.ts +++ b/src/core/ibc/msgs/channel/MsgChannelOpenConfirm.ts @@ -1,7 +1,7 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Height } from '../client/Height'; +import { Height } from '../../core/client/Height'; import { MsgChannelOpenConfirm as MsgChannelOpenConfirm_pb } from '@terra-money/terra.proto/ibc/core/channel/v1/tx'; /** diff --git a/src/core/ibc/msgs/channel/MsgChannelOpenInit.ts b/src/core/ibc/msgs/channel/MsgChannelOpenInit.ts index 120bc2386..86dac9296 100644 --- a/src/core/ibc/msgs/channel/MsgChannelOpenInit.ts +++ b/src/core/ibc/msgs/channel/MsgChannelOpenInit.ts @@ -1,7 +1,7 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Channel } from './Channel'; +import { Channel } from '../../core/channel/Channel'; import { MsgChannelOpenInit as MsgChannelOpenInit_pb } from '@terra-money/terra.proto/ibc/core/channel/v1/tx'; /** diff --git a/src/core/ibc/msgs/channel/MsgChannelOpenTry.ts b/src/core/ibc/msgs/channel/MsgChannelOpenTry.ts index b13ce6d51..0a1dc65d2 100644 --- a/src/core/ibc/msgs/channel/MsgChannelOpenTry.ts +++ b/src/core/ibc/msgs/channel/MsgChannelOpenTry.ts @@ -1,8 +1,8 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Channel } from './Channel'; -import { Height } from '../client/Height'; +import { Channel } from '../../core/channel/Channel'; +import { Height } from '../../core/client/Height'; import { MsgChannelOpenTry as MsgChannelOpenTry_pb } from '@terra-money/terra.proto/ibc/core/channel/v1/tx'; /** diff --git a/src/core/ibc/msgs/channel/MsgRecvAcknowledgement.ts b/src/core/ibc/msgs/channel/MsgRecvAcknowledgement.ts index 793d9b0e1..b5fff6e02 100644 --- a/src/core/ibc/msgs/channel/MsgRecvAcknowledgement.ts +++ b/src/core/ibc/msgs/channel/MsgRecvAcknowledgement.ts @@ -1,8 +1,8 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Height } from '../client/Height'; -import { Packet } from './Packet'; +import { Height } from '../../core/client/Height'; +import { Packet } from '../../core/channel/Packet'; import { MsgAcknowledgement as MsgAcknowledgement_pb } from '@terra-money/terra.proto/ibc/core/channel/v1/tx'; /** diff --git a/src/core/ibc/msgs/channel/MsgRecvPacket.ts b/src/core/ibc/msgs/channel/MsgRecvPacket.ts index 2ce24a6fc..e37715548 100644 --- a/src/core/ibc/msgs/channel/MsgRecvPacket.ts +++ b/src/core/ibc/msgs/channel/MsgRecvPacket.ts @@ -1,8 +1,8 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Height } from '../client/Height'; -import { Packet } from './Packet'; +import { Height } from '../../core/client/Height'; +import { Packet } from '../../core/channel/Packet'; import { MsgRecvPacket as MsgRecvPacket_pb } from '@terra-money/terra.proto/ibc/core/channel/v1/tx'; /** diff --git a/src/core/ibc/msgs/channel/MsgTimeout.ts b/src/core/ibc/msgs/channel/MsgTimeout.ts index ad2191741..6f8ac5288 100644 --- a/src/core/ibc/msgs/channel/MsgTimeout.ts +++ b/src/core/ibc/msgs/channel/MsgTimeout.ts @@ -1,8 +1,8 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Height } from '../client/Height'; -import { Packet } from './Packet'; +import { Height } from '../../core/client/Height'; +import { Packet } from '../../core/channel/Packet'; import { MsgTimeout as MsgTimeout_pb } from '@terra-money/terra.proto/ibc/core/channel/v1/tx'; import Long from 'long'; diff --git a/src/core/ibc/msgs/channel/MsgTimeoutClose.ts b/src/core/ibc/msgs/channel/MsgTimeoutClose.ts index 8d109916f..39a857970 100644 --- a/src/core/ibc/msgs/channel/MsgTimeoutClose.ts +++ b/src/core/ibc/msgs/channel/MsgTimeoutClose.ts @@ -1,8 +1,8 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Height } from '../client/Height'; -import { Packet } from './Packet'; +import { Height } from '../../core/client/Height'; +import { Packet } from '../../core/channel/Packet'; import { MsgTimeoutOnClose as MsgTimeoutOnClose_pb } from '@terra-money/terra.proto/ibc/core/channel/v1/tx'; import Long from 'long'; diff --git a/src/core/ibc/msgs/client/MsgUpdateClient.ts b/src/core/ibc/msgs/client/MsgUpdateClient.ts index f3e4550c8..3935ccaec 100644 --- a/src/core/ibc/msgs/client/MsgUpdateClient.ts +++ b/src/core/ibc/msgs/client/MsgUpdateClient.ts @@ -2,7 +2,7 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; import { MsgUpdateClient as MsgUpdateClient_pb } from '@terra-money/terra.proto/ibc/core/client/v1/tx'; -import { Header } from './Header'; +import { Header } from '../../lightclient/tendermint/Header'; /** * MsgUpdateClient defines an sdk.Msg to update a IBC client state using the given header */ diff --git a/src/core/ibc/msgs/connection/MsgConnectionOpenAck.ts b/src/core/ibc/msgs/connection/MsgConnectionOpenAck.ts index 353ffeaee..065850cab 100644 --- a/src/core/ibc/msgs/connection/MsgConnectionOpenAck.ts +++ b/src/core/ibc/msgs/connection/MsgConnectionOpenAck.ts @@ -1,9 +1,9 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Version } from './Version'; +import { Version } from '../../core/connection/Version'; import { MsgConnectionOpenAck as MsgConnectionOpenAck_pb } from '@terra-money/terra.proto/ibc/core/connection/v1/tx'; -import { Height } from '../client/Height'; +import { Height } from '../../core/client/Height'; /** * MsgConnectionOpenAck defines a msg sent by a Relayer to Chain A to diff --git a/src/core/ibc/msgs/connection/MsgConnectionOpenConfirm.ts b/src/core/ibc/msgs/connection/MsgConnectionOpenConfirm.ts index f281b9bb6..3123b81e6 100644 --- a/src/core/ibc/msgs/connection/MsgConnectionOpenConfirm.ts +++ b/src/core/ibc/msgs/connection/MsgConnectionOpenConfirm.ts @@ -2,7 +2,7 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; import { MsgConnectionOpenConfirm as MsgConnectionOpenConfirm_pb } from '@terra-money/terra.proto/ibc/core/connection/v1/tx'; -import { Height } from '../client/Height'; +import { Height } from '../../core/client/Height'; /** * MsgConnectionOpenConfirm defines a msg sent by a Relayer to Chain B to diff --git a/src/core/ibc/msgs/connection/MsgConnectionOpenInit.ts b/src/core/ibc/msgs/connection/MsgConnectionOpenInit.ts index d55e1a7ad..945bb7f3b 100644 --- a/src/core/ibc/msgs/connection/MsgConnectionOpenInit.ts +++ b/src/core/ibc/msgs/connection/MsgConnectionOpenInit.ts @@ -1,8 +1,8 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Counterparty } from './Counterparty'; -import { Version } from './Version'; +import { Counterparty } from '../../core/connection/Counterparty'; +import { Version } from '../../core/connection/Version'; import { MsgConnectionOpenInit as MsgConnectionOpenInit_pb } from '@terra-money/terra.proto/ibc/core/connection/v1/tx'; import Long from 'long'; diff --git a/src/core/ibc/msgs/connection/MsgConnectionOpenTry.ts b/src/core/ibc/msgs/connection/MsgConnectionOpenTry.ts index 7a6ed3cad..18ed7f53a 100644 --- a/src/core/ibc/msgs/connection/MsgConnectionOpenTry.ts +++ b/src/core/ibc/msgs/connection/MsgConnectionOpenTry.ts @@ -1,11 +1,11 @@ import { JSONSerializable } from '../../../../util/json'; import { AccAddress } from '../../../bech32'; import { Any } from '@terra-money/terra.proto/google/protobuf/any'; -import { Counterparty } from './Counterparty'; -import { Version } from './Version'; +import { Counterparty } from '../../core/connection/Counterparty'; +import { Version } from '../../core/connection/Version'; import { MsgConnectionOpenTry as MsgConnectionOpenTry_pb } from '@terra-money/terra.proto/ibc/core/connection/v1/tx'; import Long from 'long'; -import { Height } from '../client/Height'; +import { Height } from '../../core/client/Height'; /** * MsgConnectionOpenTry defines a msg sent by a Relayer to try to open a connection on Chain B. From ef9da0863ad8641b51e5030bd3a3ae0be371a4b2 Mon Sep 17 00:00:00 2001 From: Geoff Lee <11830048+Vritra4@users.noreply.github.com> Date: Thu, 14 Jul 2022 11:55:49 +0900 Subject: [PATCH 4/4] add new query functions for IBC --- src/client/lcd/api/IbcAPI.spec.ts | 25 ++++++++ src/client/lcd/api/IbcAPI.ts | 103 +++++++++++++++++++++++++++++- 2 files changed, 126 insertions(+), 2 deletions(-) diff --git a/src/client/lcd/api/IbcAPI.spec.ts b/src/client/lcd/api/IbcAPI.spec.ts index 145f0a3c1..82bcc97de 100644 --- a/src/client/lcd/api/IbcAPI.spec.ts +++ b/src/client/lcd/api/IbcAPI.spec.ts @@ -1,3 +1,4 @@ +import { Height } from '../../../core/ibc/core/client/Height'; import { LCDClient } from '../LCDClient'; import { IbcAPI } from './IbcAPI'; @@ -59,10 +60,34 @@ describe('IbcClientAPI', () => { expect(res.length).toBeGreaterThan(0); }); + it('channels for a connection', async () => { + const [res, height, _] = await ibc.connectionChannels('connection-3'); + expect(res).not.toBeNull(); + expect(res).not.toBeUndefined(); + expect(height).not.toBeNull(); + expect(height).toBeInstanceOf(Height); + expect(res.length).toBeGreaterThan(0); + }); + + it('port', async () => { + const res = await ibc.port('channel-0', 'transfer'); + expect(res).not.toBeNull(); + expect(res).not.toBeUndefined(); + expect(res).toHaveProperty('channel'); + expect(res).toHaveProperty('proof'); + expect(res).toHaveProperty('proof_height'); + }); + it('connections', async () => { const [res, _] = await ibc.connections(); expect(res).not.toBeNull(); expect(res).not.toBeUndefined(); expect(res.length).toBeGreaterThan(0); }); + + it('a connection', async () => { + const res = await ibc.connection('connection-0'); + expect(res).not.toBeNull(); + expect(res).not.toBeUndefined(); + }); }); diff --git a/src/client/lcd/api/IbcAPI.ts b/src/client/lcd/api/IbcAPI.ts index 31e6c0100..8bb097f17 100644 --- a/src/client/lcd/api/IbcAPI.ts +++ b/src/client/lcd/api/IbcAPI.ts @@ -7,6 +7,7 @@ import { LCDClient } from '../LCDClient'; import { Params as HostParams } from '../../../core/ibc/applications/interchain-account/host/Params'; import { Channel } from '../../../core/ibc/core/channel'; import { IdentifiedConnection } from '../../../core/ibc/core/connection'; +import { Height } from '../../../core/ibc/core/client/Height'; export interface IbcClientParams { allowed_clients: string[]; } @@ -27,11 +28,27 @@ export namespace Status { } } +export interface Port { + channel: Channel; + proof: string; + proof_height: Height; +} +export namespace Port { + export interface Data { + channel: Channel.Data; + proof: string; + proof_height: Height.Data; + } +} + export class IbcAPI extends BaseAPI { constructor(public lcd: LCDClient) { super(lcd.apiRequester); } + /** + * query all the IBC channels of a chain + */ public async channels( params: APIParams = {} ): Promise<[Channel[], Pagination]> { @@ -43,6 +60,34 @@ export class IbcAPI extends BaseAPI { .then(d => [d.channels.map(Channel.fromData), d.pagination]); } + /** + * query the information of the port at given channel + * @param channel_id channel identifier + * @param port_id port name + */ + public async port( + channel_id: string, + port_id: string, + params: APIParams = {} + ): Promise { + return this.c + .get<{ + channel: Channel.Data; + proof: string; + proof_height: Height.Data; + }>(`/ibc/core/channel/v1/channels/${channel_id}/ports/${port_id}`, params) + .then(d => { + return { + channel: Channel.fromData(d.channel), + proof: d.proof, + proof_height: Height.fromData(d.proof_height), + }; + }); + } + + /** + * query all the IBC connections of a chain + */ public async connections( params: APIParams = {} ): Promise<[IdentifiedConnection[], Pagination]> { @@ -57,6 +102,42 @@ export class IbcAPI extends BaseAPI { ]); } + /** + * query an IBC connection end + * @param connection_id connection unique identifier + */ + public async connection( + connection_id: string, + params: APIParams = {} + ): Promise { + return this.c + .get<{ + connection: IdentifiedConnection.Data; + }>(`/ibc/core/connection/v1/connections/${connection_id}`, params) + .then(d => IdentifiedConnection.fromData(d.connection)); + } + + /** + * query all the channels associated with a connection end + * @param connection_id connection unique identifier + */ + public async connectionChannels( + connection_id: string, + params: APIParams = {} + ): Promise<[Channel[], Height, Pagination]> { + return this.c + .get<{ + channels: Channel.Data[]; + pagination: Pagination; + height: Height.Data; + }>(`/ibc/core/channel/v1/connections/${connection_id}/channels`, params) + .then(d => [ + d.channels.map(Channel.fromData), + Height.fromData(d.height), + d.pagination, + ]); + } + /** * Gets the current transfer application parameters. */ @@ -68,6 +149,9 @@ export class IbcAPI extends BaseAPI { })); } + /** + * query all the IBC light clients of a chain + */ public async clientStates( params: Partial = {} ): Promise<[IdentifiedClientState[], Pagination]> { @@ -82,6 +166,11 @@ export class IbcAPI extends BaseAPI { ]); } + /** + * query an IBC light client + * @param client_id client state unique identifier + * @returns + */ public async clientState( client_id: string, params: APIParams = {} @@ -90,20 +179,30 @@ export class IbcAPI extends BaseAPI { .get<{ client_state: IdentifiedClientState.Data; }>(`/ibc/core/client/v1/client_states/${client_id}`, params) - .then(); + .then(d => IdentifiedClientState.fromData(d.client_state)); } + /** + * query the status of an IBC light client + * @param client_id client state unique identifier + * @returns + */ public async clientStatus( client_id: string, params: APIParams = {} ): Promise { return this.c .get<{ - client_state: Status.Data; + status: Status.Data; }>(`/ibc/core/client/v1/client_status/${client_id}`, params) .then(); } + /** + * query all the consensus state associated with a given client + * @param client_id client identifier + * @returns + */ public async consensusStates( client_id: string, params: Partial = {}