diff --git a/package-lock.json b/package-lock.json index cf12fe1..b863c3b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7", - "@terra-money/terra.proto": "5.1.0-beta.0", + "@terra-money/terra.proto": "5.1.0-beta.1", "assert": "^2.0.0", "axios": "^0.27.2", "bech32": "^2.0.0", @@ -1291,9 +1291,9 @@ "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==" }, "node_modules/@terra-money/terra.proto": { - "version": "5.1.0-beta.0", - "resolved": "https://registry.npmjs.org/@terra-money/terra.proto/-/terra.proto-5.1.0-beta.0.tgz", - "integrity": "sha512-EV6Snq7J7h7plFpzDDBxuJ16Xpm83MNe7Qywqbljd/QPcaeP2fJjIkmAfzjOaT0muPEjbU2VL3kG7V/78F9Jgw==", + "version": "5.1.0-beta.1", + "resolved": "https://registry.npmjs.org/@terra-money/terra.proto/-/terra.proto-5.1.0-beta.1.tgz", + "integrity": "sha512-M0UwlbLYsbSFMZZ4YS1lQaLXqnGJaVk+obeHEsgZvJL+0uWaxxFHbLfCXbXKr0SIz6fnPPHdgHAo6trxLu1GXw==", "dependencies": { "@improbable-eng/grpc-web": "^0.14.1", "browser-headers": "^0.4.1", diff --git a/package.json b/package.json index a91e90e..3264efb 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ }, "dependencies": { "@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7", - "@terra-money/terra.proto": "5.1.0-beta.0", + "@terra-money/terra.proto": "5.1.0-beta.1", "assert": "^2.0.0", "axios": "^0.27.2", "bech32": "^2.0.0", diff --git a/src/client/lcd/LCDClient.ts b/src/client/lcd/LCDClient.ts index 7bc7f73..216fd66 100644 --- a/src/client/lcd/LCDClient.ts +++ b/src/client/lcd/LCDClient.ts @@ -28,6 +28,7 @@ import { GovV1API } from './api/GovV1API'; import { ICAv1API } from './api/ICAv1API'; import { ICQv1API } from './api/ICQv1API'; import { FeemarketAPI } from './api/FeemarketAPI'; +import { SmartaccountAPI } from './api/SmartAccountAPI'; export type AxiosConfig = { /** @@ -138,6 +139,7 @@ export class LCDClient { public feeshare: FeeshareAPI; public feemarket: FeemarketAPI; public utils: LCDUtils; + public smartaccount: SmartaccountAPI; /** * Creates a new LCD client with the specified configuration. @@ -189,6 +191,7 @@ export class LCDClient { this.feeshare = new FeeshareAPI(this); this.feemarket = new FeemarketAPI(this); this.utils = new LCDUtils(this); + this.smartaccount = new SmartaccountAPI(this); } public static fromDefaultConfig(network: 'mainnet' | 'testnet') { diff --git a/src/client/lcd/api/Feemarket.API.spec.ts b/src/client/lcd/api/FeemarketAPI.spec.ts similarity index 100% rename from src/client/lcd/api/Feemarket.API.spec.ts rename to src/client/lcd/api/FeemarketAPI.spec.ts diff --git a/src/client/lcd/api/SmartaccountAPI.spec.ts b/src/client/lcd/api/SmartaccountAPI.spec.ts new file mode 100644 index 0000000..04ffbc2 --- /dev/null +++ b/src/client/lcd/api/SmartaccountAPI.spec.ts @@ -0,0 +1,35 @@ +import { SmartaccountParams } from '../../../core/smartaccount/v1/models/SmartaccountParams'; +import { LCDClient } from '../LCDClient'; +import { SmartaccountAPI } from './SmartaccountAPI'; + +const lcd = new LCDClient({ + 'pisco-1': { + chainID: 'pisco-1', + gasAdjustment: 1.5, + gasPrices: { + uluna: 0.02, + }, + lcd: 'http://localhost:1317/', + prefix: 'terra', + }, +}); +const smartaccount = new SmartaccountAPI(lcd); + +describe('SmartaccountAPI', () => { + it('assert the module params', async () => { + const res = await smartaccount.params('pisco-1'); + + expect(res).toStrictEqual(new SmartaccountParams()); + + expect(res.toData()).toEqual({}); + }); + + // TODO: there must be a smart account created to test this + // it('assert the account setting', async () => { + // const res = await smartaccount.setting('a'); + // // TODO: call + // const setting = new Setting('a', [], [], [], true); + // expect(res).toStrictEqual(setting); + // expect(res.toData()).toEqual(setting); + // }); +}); diff --git a/src/client/lcd/api/SmartaccountAPI.ts b/src/client/lcd/api/SmartaccountAPI.ts new file mode 100644 index 0000000..522df10 --- /dev/null +++ b/src/client/lcd/api/SmartaccountAPI.ts @@ -0,0 +1,41 @@ +import { Setting } from 'core/smartaccount/v1/models/Setting'; +import { AccAddress } from '../../../core'; +import { LCDClient } from '../LCDClient'; +import { BaseAPI } from './BaseAPI'; +import { SmartaccountParams } from 'core/smartaccount'; + +export class SmartaccountAPI extends BaseAPI { + constructor(public lcd: LCDClient) { + super(lcd.apiRequesters, lcd.config); + } + + /** + * Query the feemarket module params. + * + * @tags Query + * @name params + * @request GET:/terra/smartaccount/v1/params + */ + public async params(chainId: string): Promise { + const res = await this.getReqFromChainID(chainId).get<{ + params: SmartaccountParams.Data; + }>(`/terra/smartaccount/v1/params`); + + return SmartaccountParams.fromData(res.params); + } + + /** + * Query the feemarket module setting for account. + * + * @tags Query + * @name setting + * @request GET:/terra/smartaccount/v1/setting/{account} + */ + public async setting(account: AccAddress): Promise { + const res = await this.getReqFromAddress(account).get<{ + setting: Setting.Data; + }>(`/terra/smartaccount/v1/setting/${account}`); + + return Setting.fromData(res.setting); + } +} diff --git a/src/core/smartaccount/index.ts b/src/core/smartaccount/index.ts index 58474ca..d977022 100644 --- a/src/core/smartaccount/index.ts +++ b/src/core/smartaccount/index.ts @@ -1,2 +1,4 @@ export * from './v1/msgs'; export * from './v1/models/AuthorizationMsg'; +export * from './v1/models/Setting'; +export * from './v1/models/SmartaccountParams'; diff --git a/src/core/smartaccount/v1/models/Setting.ts b/src/core/smartaccount/v1/models/Setting.ts new file mode 100644 index 0000000..00d7d3c --- /dev/null +++ b/src/core/smartaccount/v1/models/Setting.ts @@ -0,0 +1,145 @@ +import { Any } from '@terra-money/terra.proto/google/protobuf/any'; +import { Setting as Setting_pb } from '@terra-money/terra.proto/terra/smartaccount/v1/setting'; +import { JSONSerializable } from '../../../../util/json'; +import { AuthorizationMsg } from './AuthorizationMsg'; + +/** + * Setting holds the contract address and initial message + * to be passed to the contract for custom authorization + */ +export class Setting extends JSONSerializable< + Setting.Amino, + Setting.Data, + Setting.Proto +> { + /** + * + * @param contractAddress contract address of authorization logic + * @param initMsg initial message to be passed to the contract + */ + constructor( + public owner: string, + public authorization: AuthorizationMsg[], + public preTransaction: string[], + public postTransaction: string[], + public fallback: boolean + ) { + super(); + } + + public static fromAmino(data: Setting.Amino): Setting { + const { + value: { + owner, + authorization, + preTransaction, + postTransaction, + fallback, + }, + } = data; + return new Setting( + owner, + authorization, + preTransaction, + postTransaction, + fallback + ); + } + + public toAmino(): Setting.Amino { + const { owner, authorization, preTransaction, postTransaction, fallback } = + this; + return { + value: { + owner, + authorization, + preTransaction, + postTransaction, + fallback, + }, + }; + } + + public static fromData(data: Setting.Data): Setting { + const { + owner, + authorization, + pre_transaction, + post_transaction, + fallback, + } = data; + return new Setting( + owner, + authorization, + pre_transaction, + post_transaction, + fallback + ); + } + + public toData(): Setting.Data { + const { owner, authorization, preTransaction, postTransaction, fallback } = + this; + return { + owner, + authorization, + pre_transaction: preTransaction, + post_transaction: postTransaction, + fallback, + }; + } + + public static fromProto(proto: Setting.Proto): Setting { + return new Setting( + proto.owner, + proto.authorization.map(AuthorizationMsg.fromProto), + proto.preTransaction, + proto.postTransaction, + proto.fallback + ); + } + + public toProto(): Setting.Proto { + const { owner, authorization, preTransaction, postTransaction, fallback } = + this; + return Setting_pb.fromPartial({ + owner, + authorization, + preTransaction, + postTransaction, + fallback, + }); + } + + public packAny(): Any { + return Any.fromPartial({ + value: Setting_pb.encode(this.toProto()).finish(), + }); + } + + public static unpackAny(msgAny: Any): Setting { + return Setting.fromProto(Setting_pb.decode(msgAny.value)); + } +} + +export namespace Setting { + export interface Amino { + value: { + owner: string; + authorization: AuthorizationMsg[]; + preTransaction: string[]; + postTransaction: string[]; + fallback: boolean; + }; + } + + export interface Data { + owner: string; + authorization: AuthorizationMsg[]; + pre_transaction: string[]; + post_transaction: string[]; + fallback: boolean; + } + + export type Proto = Setting_pb; +} diff --git a/src/core/smartaccount/v1/models/SmartaccountParams.ts b/src/core/smartaccount/v1/models/SmartaccountParams.ts new file mode 100644 index 0000000..d4b90f7 --- /dev/null +++ b/src/core/smartaccount/v1/models/SmartaccountParams.ts @@ -0,0 +1,57 @@ +import { Params as Params_pb } from '@terra-money/terra.proto/terra/smartaccount/v1/params'; +import { JSONSerializable } from '../../../../util/json'; + +export class SmartaccountParams extends JSONSerializable< + SmartaccountParams.Amino, + SmartaccountParams.Data, + SmartaccountParams.Proto +> { + constructor() { + super(); + } + + public static fromAmino(_: SmartaccountParams.Amino): SmartaccountParams { + _; + return new SmartaccountParams(); + } + + public toAmino(): SmartaccountParams.Amino { + return { + value: {}, + }; + } + + public static fromData( + proto: SmartaccountParams.Data, + _?: boolean + ): SmartaccountParams { + proto; + _; + return new SmartaccountParams(); + } + + public toData(_?: boolean): SmartaccountParams.Data { + _; + return {}; + } + + public static fromProto(proto: SmartaccountParams.Proto): SmartaccountParams { + proto; + return new SmartaccountParams(); + } + + public toProto(): SmartaccountParams.Proto { + return Params_pb.fromPartial({}); + } +} + +export namespace SmartaccountParams { + export interface Amino { + value: {}; + } + + // eslint-disable-next-line @typescript-eslint/no-empty-interface + export interface Data {} + + export type Proto = Params_pb; +}