Skip to content

Commit

Permalink
Add forwarding module
Browse files Browse the repository at this point in the history
  • Loading branch information
joon9823 committed Apr 16, 2024
1 parent ecfc4d1 commit 40b29bb
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 8 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/client/lcd/LCDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EvidenceAPI,
EvmAPI,
FeeGrantAPI,
ForwardingAPI,
GovAPI,
GroupAPI,
IbcAPI,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
48 changes: 48 additions & 0 deletions src/client/lcd/api/ForwardingAPI.ts
Original file line number Diff line number Diff line change
@@ -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<AccAddress> {
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<ForwardingStats> {
return this.c
.get<ForwardingStats.Data>(
`/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),
}));
}
}
1 change: 1 addition & 0 deletions src/client/lcd/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
27 changes: 27 additions & 0 deletions src/core/Msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -217,6 +222,7 @@ export type Msg =
| EvidenceMsg
| EvmMsg
| FeeGrantMsg
| ForwardingMsg
| GovMsg
| GroupMsg
| IbcFeeMsg
Expand Down Expand Up @@ -251,6 +257,7 @@ export namespace Msg {
| EvidenceMsg.Amino
| EvmMsg.Amino
| FeeGrantMsg.Amino
| ForwardingMsg.Amino
| GovMsg.Amino
| GroupMsg.Amino
| IbcFetchpriceMsg.Amino
Expand Down Expand Up @@ -281,6 +288,7 @@ export namespace Msg {
| EvidenceMsg.Data
| EvmMsg.Data
| FeeGrantMsg.Data
| ForwardingMsg.Data
| GovMsg.Data
| GroupMsg.Data
| IbcFeeMsg.Data
Expand Down Expand Up @@ -315,6 +323,7 @@ export namespace Msg {
| EvidenceMsg.Proto
| EvmMsg.Proto
| FeeGrantMsg.Proto
| ForwardingMsg.Proto
| GovMsg.Proto
| GroupMsg.Proto
| IbcFeeMsg.Proto
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/core/forwarding/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './msgs';
99 changes: 99 additions & 0 deletions src/core/forwarding/msgs/MsgClearForwardingAccount.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit 40b29bb

Please sign in to comment.