From 40b29bbcdcb45e779875a9df32b09a1f84edec2e Mon Sep 17 00:00:00 2001 From: joon9823 Date: Tue, 16 Apr 2024 14:08:00 +0900 Subject: [PATCH] Add forwarding module --- package-lock.json | 12 +- package.json | 4 +- src/client/lcd/LCDClient.ts | 3 + src/client/lcd/api/ForwardingAPI.ts | 48 ++++++++ src/client/lcd/api/index.ts | 1 + src/core/Msg.ts | 27 +++++ src/core/forwarding/index.ts | 1 + .../msgs/MsgClearForwardingAccount.ts | 99 +++++++++++++++ .../msgs/MsgRegisterForwardingAccount.ts | 113 ++++++++++++++++++ src/core/forwarding/msgs/index.ts | 23 ++++ src/core/index.ts | 1 + 11 files changed, 324 insertions(+), 8 deletions(-) create mode 100644 src/client/lcd/api/ForwardingAPI.ts create mode 100644 src/core/forwarding/index.ts create mode 100644 src/core/forwarding/msgs/MsgClearForwardingAccount.ts create mode 100644 src/core/forwarding/msgs/MsgRegisterForwardingAccount.ts create mode 100644 src/core/forwarding/msgs/index.ts diff --git a/package-lock.json b/package-lock.json index a724aad..a29fd86 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@initia/initia.js", - "version": "0.1.43", + "version": "0.1.44", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@initia/initia.js", - "version": "0.1.43", + "version": "0.1.44", "license": "MIT", "dependencies": { - "@initia/initia.proto": "^0.1.32", + "@initia/initia.proto": "^0.1.33", "@initia/opinit.proto": "^0.0.6", "@ledgerhq/hw-transport": "^6.27.12", "@ledgerhq/hw-transport-webhid": "^6.27.12", @@ -883,9 +883,9 @@ } }, "node_modules/@initia/initia.proto": { - "version": "0.1.32", - "resolved": "https://registry.npmjs.org/@initia/initia.proto/-/initia.proto-0.1.32.tgz", - "integrity": "sha512-Ue0Kzrpdg6p5p0EREiYEcBScAr3Dz/QGyrPiJEjrC5SROTHK5vLljPjMUUqkaF5L8NR98wW/8sDgKj3+DmrkuA==", + "version": "0.1.33", + "resolved": "https://registry.npmjs.org/@initia/initia.proto/-/initia.proto-0.1.33.tgz", + "integrity": "sha512-cPtpyesVJh3527BLYdWF0fXFYuGaacvEWvD1JRyBYuugrBVtk+uBGgCdFeiOVSFz+/gpKiRrGPhc9KlkBv5SQQ==", "dependencies": { "@improbable-eng/grpc-web": "^0.15.0", "google-protobuf": "^3.21.0", diff --git a/package.json b/package.json index 691869a..2ea36ed 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@initia/initia.js", - "version": "0.1.43", + "version": "0.1.44", "description": "The JavaScript SDK for Initia", "license": "MIT", "author": "InitiaLabs", @@ -86,7 +86,7 @@ "webpack-cli": "^4.10.0" }, "dependencies": { - "@initia/initia.proto": "^0.1.32", + "@initia/initia.proto": "^0.1.33", "@initia/opinit.proto": "^0.0.6", "@ledgerhq/hw-transport": "^6.27.12", "@ledgerhq/hw-transport-webhid": "^6.27.12", diff --git a/src/client/lcd/LCDClient.ts b/src/client/lcd/LCDClient.ts index e055ce4..84d3e1b 100644 --- a/src/client/lcd/LCDClient.ts +++ b/src/client/lcd/LCDClient.ts @@ -8,6 +8,7 @@ import { EvidenceAPI, EvmAPI, FeeGrantAPI, + ForwardingAPI, GovAPI, GroupAPI, IbcAPI, @@ -90,6 +91,7 @@ export class LCDClient { public evidence: EvidenceAPI; public evm: EvmAPI; public feeGrant: FeeGrantAPI; + public forwarding: ForwardingAPI; public gov: GovAPI; public group: GroupAPI; public ibc: IbcAPI; @@ -141,6 +143,7 @@ export class LCDClient { this.evidence = new EvidenceAPI(this.apiRequester); this.evm = new EvmAPI(this.apiRequester); this.feeGrant = new FeeGrantAPI(this.apiRequester); + this.forwarding = new ForwardingAPI(this.apiRequester); this.gov = new GovAPI(this.apiRequester); this.group = new GroupAPI(this.apiRequester); this.ibc = new IbcAPI(this.apiRequester); diff --git a/src/client/lcd/api/ForwardingAPI.ts b/src/client/lcd/api/ForwardingAPI.ts new file mode 100644 index 0000000..20f5712 --- /dev/null +++ b/src/client/lcd/api/ForwardingAPI.ts @@ -0,0 +1,48 @@ +import { AccAddress, Coin } from '../../../core'; +import { APIParams } from '../APIRequester'; +import { BaseAPI } from './BaseAPI'; + +export interface ForwardingStats { + num_of_accounts: number; + num_of_forwards: number; + total_forwarded: Coin; +} + +export namespace ForwardingStats { + export interface Data { + num_of_accounts: string; + num_of_forwards: string; + total_forwarded: Coin.Data; + } +} + +export class ForwardingAPI extends BaseAPI { + public async address( + channel: string, + recipient: string, + params: APIParams = {} + ): Promise { + return this.c + .get<{ address: AccAddress }>( + `/noble/forwarding/v1/address/${channel}/${recipient}`, + params + ) + .then(d => d.address); + } + + public async stats( + channel: string, + params: APIParams = {} + ): Promise { + return this.c + .get( + `/noble/forwarding/v1/stats/${channel}`, + params + ) + .then(d => ({ + num_of_accounts: parseInt(d.num_of_accounts), + num_of_forwards: parseInt(d.num_of_forwards), + total_forwarded: Coin.fromData(d.total_forwarded), + })); + } +} diff --git a/src/client/lcd/api/index.ts b/src/client/lcd/api/index.ts index 7cc5812..38386ec 100644 --- a/src/client/lcd/api/index.ts +++ b/src/client/lcd/api/index.ts @@ -6,6 +6,7 @@ export * from './DistributionAPI'; export * from './EvidenceAPI'; export * from './EvmAPI'; export * from './FeeGrantAPI'; +export * from './ForwardingAPI'; export * from './GovAPI'; export * from './GroupAPI'; export * from './IbcAPI'; diff --git a/src/core/Msg.ts b/src/core/Msg.ts index 293c415..3c3434a 100644 --- a/src/core/Msg.ts +++ b/src/core/Msg.ts @@ -29,6 +29,11 @@ import { import { EvidenceMsg, MsgSubmitEvidence } from './evidence'; import { EvmMsg, MsgCreate, MsgCall, MsgUpdateEvmParams } from './evm'; import { FeeGrantMsg, MsgGrantAllowance, MsgRevokeAllowance } from './feegrant'; +import { + ForwardingMsg, + MsgRegisterForwardingAccount, + MsgClearForwardingAccount, +} from './forwarding'; import { GovMsg, MsgCancelProposal, @@ -217,6 +222,7 @@ export type Msg = | EvidenceMsg | EvmMsg | FeeGrantMsg + | ForwardingMsg | GovMsg | GroupMsg | IbcFeeMsg @@ -251,6 +257,7 @@ export namespace Msg { | EvidenceMsg.Amino | EvmMsg.Amino | FeeGrantMsg.Amino + | ForwardingMsg.Amino | GovMsg.Amino | GroupMsg.Amino | IbcFetchpriceMsg.Amino @@ -281,6 +288,7 @@ export namespace Msg { | EvidenceMsg.Data | EvmMsg.Data | FeeGrantMsg.Data + | ForwardingMsg.Data | GovMsg.Data | GroupMsg.Data | IbcFeeMsg.Data @@ -315,6 +323,7 @@ export namespace Msg { | EvidenceMsg.Proto | EvmMsg.Proto | FeeGrantMsg.Proto + | ForwardingMsg.Proto | GovMsg.Proto | GroupMsg.Proto | IbcFeeMsg.Proto @@ -410,6 +419,12 @@ export namespace Msg { case 'cosmos-sdk/MsgRevokeAllowance': return MsgRevokeAllowance.fromAmino(data); + // forwarding + case 'noble/forwarding/RegisterAccount': + return MsgRegisterForwardingAccount.fromAmino(data); + case 'noble/forwarding/ClearAccount': + return MsgClearForwardingAccount.fromAmino(data); + // gov case 'cosmos-sdk/v1/MsgCancelProposal': return MsgCancelProposal.fromAmino(data); @@ -730,6 +745,12 @@ export namespace Msg { case '/cosmos.feegrant.v1beta1.MsgRevokeAllowance': return MsgRevokeAllowance.fromData(data); + // forwarding + case '/noble.forwarding.v1.MsgRegisterAccount': + return MsgRegisterForwardingAccount.fromData(data); + case '/noble.forwarding.v1.MsgClearAccount': + return MsgClearForwardingAccount.fromData(data); + // gov case '/cosmos.gov.v1.MsgCancelProposal': return MsgCancelProposal.fromData(data); @@ -1117,6 +1138,12 @@ export namespace Msg { case '/cosmos.feegrant.v1beta1.MsgRevokeAllowance': return MsgRevokeAllowance.unpackAny(proto); + // forwarding + case '/noble.forwarding.v1.MsgRegisterAccount': + return MsgRegisterForwardingAccount.unpackAny(proto); + case '/noble.forwarding.v1.MsgClearAccount': + return MsgClearForwardingAccount.unpackAny(proto); + // gov case '/cosmos.gov.v1.MsgCancelProposal': return MsgCancelProposal.unpackAny(proto); diff --git a/src/core/forwarding/index.ts b/src/core/forwarding/index.ts new file mode 100644 index 0000000..231cb79 --- /dev/null +++ b/src/core/forwarding/index.ts @@ -0,0 +1 @@ +export * from './msgs'; diff --git a/src/core/forwarding/msgs/MsgClearForwardingAccount.ts b/src/core/forwarding/msgs/MsgClearForwardingAccount.ts new file mode 100644 index 0000000..5cef1c2 --- /dev/null +++ b/src/core/forwarding/msgs/MsgClearForwardingAccount.ts @@ -0,0 +1,99 @@ +import { JSONSerializable } from '../../../util/json'; +import { AccAddress } from '../../bech32'; +import { Any } from '@initia/initia.proto/google/protobuf/any'; +import { MsgClearAccount as MsgClearAccount_pb } from '@initia/initia.proto/noble/forwarding/v1/tx'; + +export class MsgClearForwardingAccount extends JSONSerializable< + MsgClearForwardingAccount.Amino, + MsgClearForwardingAccount.Data, + MsgClearForwardingAccount.Proto +> { + /** + * @param signer + * @param address + */ + constructor(public signer: AccAddress, public address: string) { + super(); + } + + public static fromAmino( + data: MsgClearForwardingAccount.Amino + ): MsgClearForwardingAccount { + const { + value: { signer, address }, + } = data; + return new MsgClearForwardingAccount(signer, address); + } + + public toAmino(): MsgClearForwardingAccount.Amino { + const { signer, address } = this; + return { + type: 'noble/forwarding/ClearAccount', + value: { + signer, + address, + }, + }; + } + + public static fromData( + data: MsgClearForwardingAccount.Data + ): MsgClearForwardingAccount { + const { signer, address } = data; + return new MsgClearForwardingAccount(signer, address); + } + + public toData(): MsgClearForwardingAccount.Data { + const { signer, address } = this; + return { + '@type': '/noble.forwarding.v1.MsgClearAccount', + signer, + address, + }; + } + + public static fromProto( + data: MsgClearForwardingAccount.Proto + ): MsgClearForwardingAccount { + return new MsgClearForwardingAccount(data.signer, data.address); + } + + public toProto(): MsgClearForwardingAccount.Proto { + const { signer, address } = this; + return MsgClearAccount_pb.fromPartial({ + signer, + address, + }); + } + + public packAny(): Any { + return Any.fromPartial({ + typeUrl: '/noble.forwarding.v1.MsgClearAccount', + value: MsgClearAccount_pb.encode(this.toProto()).finish(), + }); + } + + public static unpackAny(msgAny: Any): MsgClearForwardingAccount { + return MsgClearForwardingAccount.fromProto( + MsgClearAccount_pb.decode(msgAny.value) + ); + } +} + +export namespace MsgClearForwardingAccount { + export interface Amino { + type: 'noble/forwarding/ClearAccount'; + value: { + signer: AccAddress; + address: string; + }; + } + + export interface Data { + '@type': '/noble.forwarding.v1.MsgClearAccount'; + signer: AccAddress; + address: string; + } + + export type Proto = MsgClearAccount_pb; +} diff --git a/src/core/forwarding/msgs/MsgRegisterForwardingAccount.ts b/src/core/forwarding/msgs/MsgRegisterForwardingAccount.ts new file mode 100644 index 0000000..c9cddaf --- /dev/null +++ b/src/core/forwarding/msgs/MsgRegisterForwardingAccount.ts @@ -0,0 +1,113 @@ +import { JSONSerializable } from '../../../util/json'; +import { AccAddress } from '../../bech32'; +import { Any } from '@initia/initia.proto/google/protobuf/any'; +import { MsgRegisterAccount as MsgRegisterAccount_pb } from '@initia/initia.proto/noble/forwarding/v1/tx'; + +export class MsgRegisterForwardingAccount extends JSONSerializable< + MsgRegisterForwardingAccount.Amino, + MsgRegisterForwardingAccount.Data, + MsgRegisterForwardingAccount.Proto +> { + /** + * @param signer + * @param recipient + * @param channel + */ + constructor( + public signer: AccAddress, + public recipient: string, + public channel: string + ) { + super(); + } + + public static fromAmino( + data: MsgRegisterForwardingAccount.Amino + ): MsgRegisterForwardingAccount { + const { + value: { signer, recipient, channel }, + } = data; + return new MsgRegisterForwardingAccount(signer, recipient, channel); + } + + public toAmino(): MsgRegisterForwardingAccount.Amino { + const { signer, recipient, channel } = this; + return { + type: 'noble/forwarding/RegisterAccount', + value: { + signer, + recipient, + channel, + }, + }; + } + + public static fromData( + data: MsgRegisterForwardingAccount.Data + ): MsgRegisterForwardingAccount { + const { signer, recipient, channel } = data; + return new MsgRegisterForwardingAccount(signer, recipient, channel); + } + + public toData(): MsgRegisterForwardingAccount.Data { + const { signer, recipient, channel } = this; + return { + '@type': '/noble.forwarding.v1.MsgRegisterAccount', + signer, + recipient, + channel, + }; + } + + public static fromProto( + data: MsgRegisterForwardingAccount.Proto + ): MsgRegisterForwardingAccount { + return new MsgRegisterForwardingAccount( + data.signer, + data.recipient, + data.channel + ); + } + + public toProto(): MsgRegisterForwardingAccount.Proto { + const { signer, recipient, channel } = this; + return MsgRegisterAccount_pb.fromPartial({ + signer, + recipient, + channel, + }); + } + + public packAny(): Any { + return Any.fromPartial({ + typeUrl: '/noble.forwarding.v1.MsgRegisterAccount', + value: MsgRegisterAccount_pb.encode(this.toProto()).finish(), + }); + } + + public static unpackAny(msgAny: Any): MsgRegisterForwardingAccount { + return MsgRegisterForwardingAccount.fromProto( + MsgRegisterAccount_pb.decode(msgAny.value) + ); + } +} + +export namespace MsgRegisterForwardingAccount { + export interface Amino { + type: 'noble/forwarding/RegisterAccount'; + value: { + signer: AccAddress; + recipient: string; + channel: string; + }; + } + + export interface Data { + '@type': '/noble.forwarding.v1.MsgRegisterAccount'; + signer: AccAddress; + recipient: string; + channel: string; + } + + export type Proto = MsgRegisterAccount_pb; +} diff --git a/src/core/forwarding/msgs/index.ts b/src/core/forwarding/msgs/index.ts new file mode 100644 index 0000000..0e329d4 --- /dev/null +++ b/src/core/forwarding/msgs/index.ts @@ -0,0 +1,23 @@ +import { MsgRegisterForwardingAccount } from './MsgRegisterForwardingAccount'; +import { MsgClearForwardingAccount } from './MsgClearForwardingAccount'; + +export * from './MsgRegisterForwardingAccount'; +export * from './MsgClearForwardingAccount'; + +export type ForwardingMsg = + | MsgRegisterForwardingAccount + | MsgClearForwardingAccount; + +export namespace ForwardingMsg { + export type Amino = + | MsgRegisterForwardingAccount.Amino + | MsgClearForwardingAccount.Amino; + + export type Data = + | MsgRegisterForwardingAccount.Data + | MsgClearForwardingAccount.Data; + + export type Proto = + | MsgRegisterForwardingAccount.Proto + | MsgClearForwardingAccount.Proto; +} diff --git a/src/core/index.ts b/src/core/index.ts index 95498d5..903df26 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -19,6 +19,7 @@ export * from './distribution'; export * from './evidence'; export * from './evm'; export * from './feegrant'; +export * from './forwarding'; export * from './gov'; export * from './group'; export * from './ibc';