diff --git a/package-lock.json b/package-lock.json index 76f7e95..cee82ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,16 @@ { "name": "@terra-money/feather.js", - "version": "3.0.0-beta.0", + "version": "3.0.0-beta.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@terra-money/feather.js", - "version": "3.0.0-beta.0", + "version": "3.0.0-beta.1", "license": "MIT", "dependencies": { "@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7", - "@terra-money/terra.proto": "5.2.0", + "@terra-money/terra.proto": "5.3.0-beta.0", "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.2.0", - "resolved": "https://registry.npmjs.org/@terra-money/terra.proto/-/terra.proto-5.2.0.tgz", - "integrity": "sha512-fD/jqDryw97ogPyNXUSQGIUfHGgJ7n1QgS5cBthwzS1gmq3Dc0dt2q94/AM4bWp6PjN0F84QJu/T3fwHlbeDsQ==", + "version": "5.3.0-beta.0", + "resolved": "https://registry.npmjs.org/@terra-money/terra.proto/-/terra.proto-5.3.0-beta.0.tgz", + "integrity": "sha512-pRy8RScTbhHuSZsuevQBGdVAcFaMTriQeCrT1TOJ2/cMJLGnBJ/43wQN/0KS+4tg2UyYpudZzBhARp+0nvbs1Q==", "dependencies": { "@improbable-eng/grpc-web": "^0.14.1", "browser-headers": "^0.4.1", diff --git a/package.json b/package.json index 44ce776..b8c71a9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@terra-money/feather.js", - "version": "3.0.0-beta.0", + "version": "3.0.0-beta.1", "description": "The JavaScript SDK for Terra and Feather chains", "license": "MIT", "author": "Terraform Labs, PTE.", @@ -86,7 +86,7 @@ }, "dependencies": { "@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7", - "@terra-money/terra.proto": "5.2.0", + "@terra-money/terra.proto": "5.3.0-beta.0", "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..3f61a51 100644 --- a/src/client/lcd/LCDClient.ts +++ b/src/client/lcd/LCDClient.ts @@ -15,6 +15,8 @@ import { IbcTransferAPI, IbcAPI, TokenFactory, + FeemarketAPI, + SmartaccountAPI, } from './api'; import { LCDUtils } from './LCDUtils'; import { Wallet } from './Wallet'; @@ -27,7 +29,6 @@ import { FeeshareAPI } from './api/FeeshareAPI'; import { GovV1API } from './api/GovV1API'; import { ICAv1API } from './api/ICAv1API'; import { ICQv1API } from './api/ICQv1API'; -import { FeemarketAPI } from './api/FeemarketAPI'; 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/AllianceAPI.ts b/src/client/lcd/api/AllianceAPI.ts index 9b5563d..ea42f32 100644 --- a/src/client/lcd/api/AllianceAPI.ts +++ b/src/client/lcd/api/AllianceAPI.ts @@ -134,7 +134,7 @@ export class AllianceAPI extends BaseAPI { } // If all parameters are provided, the response will be a single delegation. - // In order to fit the return type, an array of delegations is returned. + // In order to fit the return type, an array of delegations is returned. if (delAddr && valAddr && denom) { const res = await this.getReqFromChainID(chainID).get<{ delegation: AllianceDelegation.Data; @@ -232,9 +232,9 @@ export class AllianceAPI extends BaseAPI { * that valAddr depend on the denom. When all values are provided the query will be faster, * Any denom specified in this query will be URL encoded to allow querying for alliance assets * with "/" or other special characters in their denom. - * + * * - When **delAddr** is provided, this query returns the unbondings for the provided address. - * - When **denom** and **delAddr** are provided, this query returns the unbondings for the + * - When **denom** and **delAddr** are provided, this query returns the unbondings for the * specified address and denom. * - When **delAddr**, **valAddr** and **denom** are provided, this query returns the unbondings * for the specified address, validator and denom. @@ -274,8 +274,8 @@ export class AllianceAPI extends BaseAPI { } /** - * Query all validators that have at least one user delegation. You can optionally provide valAddr - * to query a single validator. Providing the validatorAddr will deliver a faster response. + * Query all validators that have at least one user delegation. You can optionally provide valAddr + * to query a single validator. Providing the validatorAddr will deliver a faster response. * This query returns data about the delegations shares, validator shares, * and total staked tokens. * 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..bb5958f --- /dev/null +++ b/src/client/lcd/api/SmartaccountAPI.spec.ts @@ -0,0 +1,41 @@ +import { Setting } from '../../../core/smartaccount/v1/models/Setting'; +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({}); + }); + + // test with wallet15 terra1tck9vx8vwu6l83zy76ssdkhnhw8dfcrt80hc6x + it('assert the account setting', async () => { + const res = await smartaccount.setting( + 'terra1tck9vx8vwu6l83zy76ssdkhnhw8dfcrt80hc6x' + ); + expect(res.toData()).toEqual({ + owner: 'terra1tck9vx8vwu6l83zy76ssdkhnhw8dfcrt80hc6x', + authorization: [], + post_transaction: [], + pre_transaction: [], + fallback: true, + }); + }); +}); diff --git a/src/client/lcd/api/SmartaccountAPI.ts b/src/client/lcd/api/SmartaccountAPI.ts new file mode 100644 index 0000000..77f6730 --- /dev/null +++ b/src/client/lcd/api/SmartaccountAPI.ts @@ -0,0 +1,41 @@ +import { Setting } from '../../../core/smartaccount'; +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/client/lcd/api/index.ts b/src/client/lcd/api/index.ts index 9743a1f..3d62a75 100644 --- a/src/client/lcd/api/index.ts +++ b/src/client/lcd/api/index.ts @@ -13,3 +13,5 @@ export * from './MintAPI'; export * from './TokenFactoryAPI'; export * from './IbcAPI'; export * from './IbcTransferAPI'; +export * from './FeemarketAPI'; +export * from './SmartaccountAPI'; diff --git a/src/core/Msg.ts b/src/core/Msg.ts index c059ac1..0685554 100644 --- a/src/core/Msg.ts +++ b/src/core/Msg.ts @@ -121,6 +121,13 @@ import { } from './ica/controller/v1/msgs'; import { MsgForceTransfer } from './tokenfactory/MsgForceTransfer'; import { MsgSetDenomMetadata } from './tokenfactory/MsgSetDenomMetadata'; +import { + MsgCreateSmartAccount, + MsgDisableSmartAccount, + MsgUpdateAuthorization, + MsgUpdateTransactionHooks, + SmartAccountMsg, +} from './smartaccount'; export type Msg = | BankMsg @@ -143,7 +150,8 @@ export type Msg = | CrisisMsg | MsgAuctionBid | FeeshareMsg - | TokenFactoryMsg; + | TokenFactoryMsg + | SmartAccountMsg; export namespace Msg { export type Amino = @@ -163,7 +171,8 @@ export namespace Msg { | CrisisMsg.Amino | MsgAuctionBid.Amino | FeeshareMsg.Amino - | TokenFactoryMsg.Amino; + | TokenFactoryMsg.Amino + | SmartAccountMsg.Amino; export type Data = | BankMsg.Data @@ -186,7 +195,8 @@ export namespace Msg { | CrisisMsg.Data | MsgAuctionBid.Data | FeeshareMsg.Data - | TokenFactoryMsg.Data; + | TokenFactoryMsg.Data + | SmartAccountMsg.Data; export type Proto = | BankMsg.Proto @@ -208,7 +218,8 @@ export namespace Msg { | CrisisMsg.Proto | MsgAuctionBid.Proto | FeeshareMsg.Proto - | TokenFactoryMsg.Proto; + | TokenFactoryMsg.Proto + | SmartAccountMsg.Proto; export function fromAmino(data: Msg.Amino, isClassic?: boolean): Msg { switch (data.type) { @@ -501,6 +512,24 @@ export namespace Msg { isClassic ); + // SmartAccount module + case 'terra/MsgCreateSmartAccount': + return MsgCreateSmartAccount.fromAmino( + data as MsgCreateSmartAccount.Amino + ); + case 'terra/MsgDisableSmartAccount': + return MsgDisableSmartAccount.fromAmino( + data as MsgDisableSmartAccount.Amino + ); + case 'terra/MsgUpdateAuthorization': + return MsgUpdateAuthorization.fromAmino( + data as MsgUpdateAuthorization.Amino + ); + case 'terra/MsgUpdateTransactionHooks': + return MsgUpdateTransactionHooks.fromAmino( + data as MsgUpdateTransactionHooks.Amino + ); + // custom default: return MsgAminoCustom.fromAmino(data as any, isClassic); @@ -703,6 +732,16 @@ export namespace Msg { case '/juno.feeshare.v1.MsgCancelFeeShare': return MsgCancelFeeShare.fromData(data, isClassic); + // SmartAccount + case '/terra.smartaccount.v1.MsgCreateSmartAccount': + return MsgCreateSmartAccount.fromData(data); + case '/terra.smartaccount.v1.MsgDisableSmartAccount': + return MsgDisableSmartAccount.fromData(data); + case '/terra.smartaccount.v1.MsgUpdateAuthorization': + return MsgUpdateAuthorization.fromData(data); + case '/terra.smartaccount.v1.MsgUpdateTransactionHooks': + return MsgUpdateTransactionHooks.fromData(data); + // custom default: return MsgAminoCustom.fromData(data as any, isClassic); @@ -908,6 +947,16 @@ export namespace Msg { case '/juno.feeshare.v1.MsgCancelFeeShare': return MsgCancelFeeShare.unpackAny(proto, isClassic); + // SmartAccount + case '/terra.smartaccount.v1.MsgCreateSmartAccount': + return MsgCreateSmartAccount.unpackAny(proto); + case '/terra.smartaccount.v1.MsgDisableSmartAccount': + return MsgDisableSmartAccount.unpackAny(proto); + case '/terra.smartaccount.v1.MsgUpdateAuthorization': + return MsgUpdateAuthorization.unpackAny(proto); + case '/terra.smartaccount.v1.MsgUpdateTransactionHooks': + return MsgUpdateTransactionHooks.unpackAny(proto); + default: throw Error(`not supported msg ${proto.typeUrl}`); } diff --git a/src/core/smartaccount/index.ts b/src/core/smartaccount/index.ts new file mode 100644 index 0000000..c0b4112 --- /dev/null +++ b/src/core/smartaccount/index.ts @@ -0,0 +1,5 @@ +export * from './v1/msgs'; +export * from './v1/models/Initialization'; +export * from './v1/models/AuthorizationMsg'; +export * from './v1/models/Setting'; +export * from './v1/models/SmartaccountParams'; diff --git a/src/core/smartaccount/v1/models/AuthorizationMsg.ts b/src/core/smartaccount/v1/models/AuthorizationMsg.ts new file mode 100644 index 0000000..3000a5c --- /dev/null +++ b/src/core/smartaccount/v1/models/AuthorizationMsg.ts @@ -0,0 +1,100 @@ +import { Any } from '@terra-money/terra.proto/google/protobuf/any'; +import { AuthorizationMsg as AuthorizationMsg_pb } from '@terra-money/terra.proto/terra/smartaccount/v1/setting'; +import { JSONSerializable } from '../../../../util/json'; +import { Initialization } from './Initialization'; + +/** + * AuthorizationMsg holds the contract address and initial message + * to be passed to the contract for custom authorization + */ +export class AuthorizationMsg extends JSONSerializable< + AuthorizationMsg.Amino, + AuthorizationMsg.Data, + AuthorizationMsg.Proto +> { + /** + * + * @param contractAddress contract address of authorization logic + * @param initMsg initial message to be passed to the contract + */ + constructor(public contractAddress: string, public initMsg: Initialization) { + super(); + } + + public static fromAmino(data: AuthorizationMsg.Amino): AuthorizationMsg { + const { + value: { contractAddress, initMsg }, + } = data; + return new AuthorizationMsg( + contractAddress, + Initialization.fromAmino(initMsg) + ); + } + + public toAmino(): AuthorizationMsg.Amino { + const { contractAddress, initMsg } = this; + return { + value: { + contractAddress, + initMsg: initMsg.toAmino(), + }, + }; + } + + public static fromData(data: AuthorizationMsg.Data): AuthorizationMsg { + const { contract_address, init_msg } = data; + return new AuthorizationMsg( + contract_address, + Initialization.fromData(init_msg) + ); + } + + public toData(): AuthorizationMsg.Data { + const { contractAddress, initMsg } = this; + return { + contract_address: contractAddress, + init_msg: initMsg.toData(), + }; + } + + public static fromProto(proto: AuthorizationMsg.Proto): AuthorizationMsg { + return new AuthorizationMsg( + proto.contractAddress, + proto.initMsg as Initialization + ); + } + + public toProto(): AuthorizationMsg.Proto { + const { contractAddress, initMsg } = this; + return AuthorizationMsg_pb.fromPartial({ + contractAddress, + initMsg, + }); + } + + public packAny(): Any { + return Any.fromPartial({ + value: AuthorizationMsg_pb.encode(this.toProto()).finish(), + }); + } + + public static unpackAny(msgAny: Any): AuthorizationMsg { + return AuthorizationMsg.fromProto(AuthorizationMsg_pb.decode(msgAny.value)); + } +} + +export namespace AuthorizationMsg { + export interface Amino { + value: { + contractAddress: string; + initMsg: Initialization.Amino; + }; + } + + export interface Data { + contract_address: string; + init_msg: Initialization.Data; + } + + export type Proto = AuthorizationMsg_pb; +} diff --git a/src/core/smartaccount/v1/models/Initialization.ts b/src/core/smartaccount/v1/models/Initialization.ts new file mode 100644 index 0000000..f19f68f --- /dev/null +++ b/src/core/smartaccount/v1/models/Initialization.ts @@ -0,0 +1,96 @@ +import { Any } from '@terra-money/terra.proto/google/protobuf/any'; +import { Initialization as Initialization_pb } from '@terra-money/terra.proto/terra/smartaccount/v1/wasm'; +import { JSONSerializable } from '../../../../util/json'; + +/** + * Initialization holds the contract address and initial message + * to be passed to the contract for custom authorization + */ +export class Initialization extends JSONSerializable< + Initialization.Amino, + Initialization.Data, + Initialization.Proto +> { + /** + * + * @param contractAddress contract address of authorization logic + * @param initMsg initial message to be passed to the contract + */ + constructor(public account: string, public msg: Uint8Array) { + super(); + } + + public static fromAmino(data: Initialization.Amino): Initialization { + const { + value: { account, msg }, + } = data; + const buf = Buffer.from(msg, 'ascii'); + const msgBs = new Uint8Array(buf); + return new Initialization(account, msgBs); + } + + public toAmino(): Initialization.Amino { + const { account, msg } = this; + const base64Str = Buffer.from(msg).toString('ascii'); + return { + value: { + account, + msg: base64Str, + }, + }; + } + + public static fromData(data: Initialization.Data): Initialization { + const { account, msg } = data; + const buf = Buffer.from(msg, 'ascii'); + const msgBs = new Uint8Array(buf); + return new Initialization(account, msgBs); + } + + public toData(): Initialization.Data { + const { account, msg } = this; + const base64Str = Buffer.from(msg).toString('ascii'); + return { + account, + msg: base64Str, + }; + } + + public static fromProto(proto: Initialization.Proto): Initialization { + return new Initialization(proto.account, proto.msg); + } + + public toProto(): Initialization.Proto { + const { account, msg } = this; + return Initialization_pb.fromPartial({ + account, + msg, + }); + } + + public packAny(): Any { + return Any.fromPartial({ + value: Initialization_pb.encode(this.toProto()).finish(), + }); + } + + public static unpackAny(msgAny: Any): Initialization { + return Initialization.fromProto(Initialization_pb.decode(msgAny.value)); + } +} + +export namespace Initialization { + export interface Amino { + value: { + account: string; + msg: string; + }; + } + + export interface Data { + account: string; + msg: string; + } + + export type Proto = Initialization_pb; +} 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; +} diff --git a/src/core/smartaccount/v1/msgs/MsgCreateSmartAccount.ts b/src/core/smartaccount/v1/msgs/MsgCreateSmartAccount.ts new file mode 100644 index 0000000..a6a93fd --- /dev/null +++ b/src/core/smartaccount/v1/msgs/MsgCreateSmartAccount.ts @@ -0,0 +1,97 @@ +import { Any } from '@terra-money/terra.proto/google/protobuf/any'; +import { MsgCreateSmartAccount as MsgCreateSmartAccount_pb } from '@terra-money/terra.proto/terra/smartaccount/v1/tx'; +import { JSONSerializable } from '../../../../util/json'; +import { AccAddress } from '../../../bech32'; + +/** + * MsgCreateSmartAccount creates a new smart account + */ +export class MsgCreateSmartAccount extends JSONSerializable< + MsgCreateSmartAccount.Amino, + MsgCreateSmartAccount.Data, + MsgCreateSmartAccount.Proto +> { + /** + * + * @param account sender's account address + */ + constructor(public account: AccAddress) { + super(); + } + + public static fromAmino( + data: MsgCreateSmartAccount.Amino + ): MsgCreateSmartAccount { + const { + value: { account }, + } = data; + return new MsgCreateSmartAccount(account); + } + + public toAmino(): MsgCreateSmartAccount.Amino { + const { account } = this; + return { + type: 'terra/MsgCreateSmartAccount', + value: { + account, + }, + }; + } + + public static fromData( + data: MsgCreateSmartAccount.Data + ): MsgCreateSmartAccount { + const { account } = data; + return new MsgCreateSmartAccount(account); + } + + public toData(): MsgCreateSmartAccount.Data { + const { account } = this; + return { + '@type': '/terra.smartaccount.v1.MsgCreateSmartAccount', + account, + }; + } + + public static fromProto( + proto: MsgCreateSmartAccount.Proto + ): MsgCreateSmartAccount { + return new MsgCreateSmartAccount(proto.account); + } + + public toProto(): MsgCreateSmartAccount.Proto { + const { account } = this; + return MsgCreateSmartAccount_pb.fromPartial({ + account, + }); + } + + public packAny(): Any { + return Any.fromPartial({ + typeUrl: '/terra.smartaccount.v1.MsgCreateSmartAccount', + value: MsgCreateSmartAccount_pb.encode(this.toProto()).finish(), + }); + } + + public static unpackAny(msgAny: Any): MsgCreateSmartAccount { + return MsgCreateSmartAccount.fromProto( + MsgCreateSmartAccount_pb.decode(msgAny.value) + ); + } +} + +export namespace MsgCreateSmartAccount { + export interface Amino { + type: 'terra/MsgCreateSmartAccount'; + value: { + account: AccAddress; + }; + } + + export interface Data { + '@type': '/terra.smartaccount.v1.MsgCreateSmartAccount'; + account: AccAddress; + } + + export type Proto = MsgCreateSmartAccount_pb; +} diff --git a/src/core/smartaccount/v1/msgs/MsgDisableSmartAccount.ts b/src/core/smartaccount/v1/msgs/MsgDisableSmartAccount.ts new file mode 100644 index 0000000..bcca3cd --- /dev/null +++ b/src/core/smartaccount/v1/msgs/MsgDisableSmartAccount.ts @@ -0,0 +1,97 @@ +import { Any } from '@terra-money/terra.proto/google/protobuf/any'; +import { MsgDisableSmartAccount as MsgDisableSmartAccount_pb } from '@terra-money/terra.proto/terra/smartaccount/v1/tx'; +import { JSONSerializable } from '../../../../util/json'; +import { AccAddress } from '../../../bech32'; + +/** + * MsgDisableSmartAccount disables a smart account + */ +export class MsgDisableSmartAccount extends JSONSerializable< + MsgDisableSmartAccount.Amino, + MsgDisableSmartAccount.Data, + MsgDisableSmartAccount.Proto +> { + /** + * + * @param account sender's account address + */ + constructor(public account: AccAddress) { + super(); + } + + public static fromAmino( + data: MsgDisableSmartAccount.Amino + ): MsgDisableSmartAccount { + const { + value: { account }, + } = data; + return new MsgDisableSmartAccount(account); + } + + public toAmino(): MsgDisableSmartAccount.Amino { + const { account } = this; + return { + type: 'terra/MsgDisableSmartAccount', + value: { + account, + }, + }; + } + + public static fromData( + data: MsgDisableSmartAccount.Data + ): MsgDisableSmartAccount { + const { account } = data; + return new MsgDisableSmartAccount(account); + } + + public toData(): MsgDisableSmartAccount.Data { + const { account } = this; + return { + '@type': '/terra.smartaccount.v1.MsgDisableSmartAccount', + account, + }; + } + + public static fromProto( + proto: MsgDisableSmartAccount.Proto + ): MsgDisableSmartAccount { + return new MsgDisableSmartAccount(proto.account); + } + + public toProto(): MsgDisableSmartAccount.Proto { + const { account } = this; + return MsgDisableSmartAccount_pb.fromPartial({ + account, + }); + } + + public packAny(): Any { + return Any.fromPartial({ + typeUrl: '/terra.smartaccount.v1.MsgDisableSmartAccount', + value: MsgDisableSmartAccount_pb.encode(this.toProto()).finish(), + }); + } + + public static unpackAny(msgAny: Any): MsgDisableSmartAccount { + return MsgDisableSmartAccount.fromProto( + MsgDisableSmartAccount_pb.decode(msgAny.value) + ); + } +} + +export namespace MsgDisableSmartAccount { + export interface Amino { + type: 'terra/MsgDisableSmartAccount'; + value: { + account: AccAddress; + }; + } + + export interface Data { + '@type': '/terra.smartaccount.v1.MsgDisableSmartAccount'; + account: AccAddress; + } + + export type Proto = MsgDisableSmartAccount_pb; +} diff --git a/src/core/smartaccount/v1/msgs/MsgUpdateAuthorization.ts b/src/core/smartaccount/v1/msgs/MsgUpdateAuthorization.ts new file mode 100644 index 0000000..fc209de --- /dev/null +++ b/src/core/smartaccount/v1/msgs/MsgUpdateAuthorization.ts @@ -0,0 +1,126 @@ +import { Any } from '@terra-money/terra.proto/google/protobuf/any'; +import { MsgUpdateAuthorization as MsgUpdateAuthorization_pb } from '@terra-money/terra.proto/terra/smartaccount/v1/tx'; +import { JSONSerializable } from '../../../../util/json'; +import { AccAddress } from '../../../bech32'; +import { AuthorizationMsg } from '../models/AuthorizationMsg'; + +/** + * MsgUpdateAuthorization updates the list of custom authorization + * for a smart account + */ +export class MsgUpdateAuthorization extends JSONSerializable< + MsgUpdateAuthorization.Amino, + MsgUpdateAuthorization.Data, + MsgUpdateAuthorization.Proto +> { + /** + * + * @param account sender's account address + * @param fallback fallback if no custom authorization matches + * @param authorizationMsgs list of custom authorization messages + */ + constructor( + public account: AccAddress, + public fallback: boolean, + public authorizationMsgs: AuthorizationMsg[] + ) { + super(); + } + + public static fromAmino( + data: MsgUpdateAuthorization.Amino + ): MsgUpdateAuthorization { + const { + value: { account, fallback, authorizationMsgs }, + } = data; + return new MsgUpdateAuthorization( + account, + fallback, + authorizationMsgs.map(msg => AuthorizationMsg.fromAmino(msg)) + ); + } + + public toAmino(): MsgUpdateAuthorization.Amino { + const { account, fallback, authorizationMsgs } = this; + return { + type: 'terra/MsgUpdateAuthorization', + value: { + account, + fallback, + authorizationMsgs: authorizationMsgs.map(msg => msg.toAmino()), + }, + }; + } + + public static fromData( + data: MsgUpdateAuthorization.Data + ): MsgUpdateAuthorization { + const { account, fallback, authorizationMsgs } = data; + const authMsgs = authorizationMsgs.map(msg => + AuthorizationMsg.fromData(msg) + ); + return new MsgUpdateAuthorization(account, fallback, authMsgs); + } + + public toData(): MsgUpdateAuthorization.Data { + const { account, fallback, authorizationMsgs } = this; + return { + '@type': '/terra.smartaccount.v1.MsgUpdateAuthorization', + account, + fallback, + authorizationMsgs: authorizationMsgs.map(msg => msg.toData()), + }; + } + + public static fromProto( + proto: MsgUpdateAuthorization.Proto + ): MsgUpdateAuthorization { + return new MsgUpdateAuthorization( + proto.account, + proto.fallback, + proto.authorizationMsgs.map(AuthorizationMsg.fromProto) + ); + } + + public toProto(): MsgUpdateAuthorization.Proto { + const { account, fallback, authorizationMsgs } = this; + return MsgUpdateAuthorization_pb.fromPartial({ + account, + fallback, + authorizationMsgs, + }); + } + + public packAny(): Any { + return Any.fromPartial({ + typeUrl: '/terra.smartaccount.v1.MsgUpdateAuthorization', + value: MsgUpdateAuthorization_pb.encode(this.toProto()).finish(), + }); + } + + public static unpackAny(msgAny: Any): MsgUpdateAuthorization { + return MsgUpdateAuthorization.fromProto( + MsgUpdateAuthorization_pb.decode(msgAny.value) + ); + } +} + +export namespace MsgUpdateAuthorization { + export interface Amino { + type: 'terra/MsgUpdateAuthorization'; + value: { + account: AccAddress; + fallback: boolean; + authorizationMsgs: AuthorizationMsg.Amino[]; + }; + } + + export interface Data { + '@type': '/terra.smartaccount.v1.MsgUpdateAuthorization'; + account: AccAddress; + fallback: boolean; + authorizationMsgs: AuthorizationMsg.Data[]; + } + + export type Proto = MsgUpdateAuthorization_pb; +} diff --git a/src/core/smartaccount/v1/msgs/MsgUpdateTransactionHooks.ts b/src/core/smartaccount/v1/msgs/MsgUpdateTransactionHooks.ts new file mode 100644 index 0000000..080e801 --- /dev/null +++ b/src/core/smartaccount/v1/msgs/MsgUpdateTransactionHooks.ts @@ -0,0 +1,124 @@ +import { Any } from '@terra-money/terra.proto/google/protobuf/any'; +import { MsgUpdateTransactionHooks as MsgUpdateTransactionHooks_pb } from '@terra-money/terra.proto/terra/smartaccount/v1/tx'; +import { JSONSerializable } from '../../../../util/json'; +import { AccAddress } from '../../../bech32'; + +/** + * MsgUpdateTransactionHooks adds permission for Grantee to spend up to Allowance + * of fees from the account of Granter. + */ +export class MsgUpdateTransactionHooks extends JSONSerializable< + MsgUpdateTransactionHooks.Amino, + MsgUpdateTransactionHooks.Data, + MsgUpdateTransactionHooks.Proto +> { + /** + * + * @param account sender's account address + */ + constructor( + public account: AccAddress, + public preTransactionHooks: string[], + public postTransactionHooks: string[] + ) { + super(); + } + + public static fromAmino( + data: MsgUpdateTransactionHooks.Amino + ): MsgUpdateTransactionHooks { + const { + value: { account, preTransactionHooks, postTransactionHooks }, + } = data; + return new MsgUpdateTransactionHooks( + account, + preTransactionHooks, + postTransactionHooks + ); + } + + public toAmino(): MsgUpdateTransactionHooks.Amino { + const { account, preTransactionHooks, postTransactionHooks } = this; + return { + type: 'terra/MsgUpdateTransactionHooks', + value: { + account, + preTransactionHooks, + postTransactionHooks, + }, + }; + } + + public static fromData( + data: MsgUpdateTransactionHooks.Data + ): MsgUpdateTransactionHooks { + const { account, preTransactionHooks, postTransactionHooks } = data; + return new MsgUpdateTransactionHooks( + account, + preTransactionHooks, + postTransactionHooks + ); + } + + public toData(): MsgUpdateTransactionHooks.Data { + const { account, preTransactionHooks, postTransactionHooks } = this; + return { + '@type': '/terra.smartaccount.v1.MsgUpdateTransactionHooks', + account, + preTransactionHooks, + postTransactionHooks, + }; + } + + public static fromProto( + proto: MsgUpdateTransactionHooks.Proto + ): MsgUpdateTransactionHooks { + return new MsgUpdateTransactionHooks( + proto.account, + proto.preTransactionHooks, + proto.postTransactionHooks + ); + } + + public toProto(): MsgUpdateTransactionHooks.Proto { + const { account, preTransactionHooks, postTransactionHooks } = this; + return MsgUpdateTransactionHooks_pb.fromPartial({ + account, + preTransactionHooks, + postTransactionHooks, + }); + } + + public packAny(): Any { + return Any.fromPartial({ + typeUrl: '/terra.smartaccount.v1.MsgUpdateTransactionHooks', + value: MsgUpdateTransactionHooks_pb.encode(this.toProto()).finish(), + }); + } + + public static unpackAny(msgAny: Any): MsgUpdateTransactionHooks { + return MsgUpdateTransactionHooks.fromProto( + MsgUpdateTransactionHooks_pb.decode(msgAny.value) + ); + } +} + +export namespace MsgUpdateTransactionHooks { + export interface Amino { + type: 'terra/MsgUpdateTransactionHooks'; + value: { + account: AccAddress; + preTransactionHooks: string[]; + postTransactionHooks: string[]; + }; + } + + export interface Data { + '@type': '/terra.smartaccount.v1.MsgUpdateTransactionHooks'; + account: AccAddress; + preTransactionHooks: string[]; + postTransactionHooks: string[]; + } + + export type Proto = MsgUpdateTransactionHooks_pb; +} diff --git a/src/core/smartaccount/v1/msgs/index.ts b/src/core/smartaccount/v1/msgs/index.ts new file mode 100644 index 0000000..2c66490 --- /dev/null +++ b/src/core/smartaccount/v1/msgs/index.ts @@ -0,0 +1,33 @@ +import { MsgCreateSmartAccount } from './MsgCreateSmartAccount'; +import { MsgDisableSmartAccount } from './MsgDisableSmartAccount'; +import { MsgUpdateAuthorization } from './MsgUpdateAuthorization'; +import { MsgUpdateTransactionHooks } from './MsgUpdateTransactionHooks'; + +export * from './MsgCreateSmartAccount'; +export * from './MsgDisableSmartAccount'; +export * from './MsgUpdateAuthorization'; +export * from './MsgUpdateTransactionHooks'; + +export type SmartAccountMsg = + | MsgCreateSmartAccount + | MsgDisableSmartAccount + | MsgUpdateAuthorization + | MsgUpdateTransactionHooks; + +export namespace SmartAccountMsg { + export type Amino = + | MsgCreateSmartAccount.Amino + | MsgDisableSmartAccount.Amino + | MsgUpdateAuthorization.Amino + | MsgUpdateTransactionHooks.Amino; + export type Data = + | MsgCreateSmartAccount.Data + | MsgDisableSmartAccount.Data + | MsgUpdateAuthorization.Data + | MsgUpdateTransactionHooks.Data; + export type Proto = + | MsgCreateSmartAccount.Proto + | MsgDisableSmartAccount.Proto + | MsgUpdateAuthorization.Proto + | MsgUpdateTransactionHooks.Proto; +}