From 95bfaba2620b7c1f88e9b09128ddda64a5a0fd9c Mon Sep 17 00:00:00 2001 From: emidev98 Date: Mon, 4 Mar 2024 09:34:39 +0200 Subject: [PATCH 01/11] feat: smart accs --- package-lock.json | 12 ++++++------ package.json | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 17391e0..cf12fe1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,16 @@ { "name": "@terra-money/feather.js", - "version": "2.1.0-beta.1", + "version": "2.1.0-beta.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@terra-money/feather.js", - "version": "2.1.0-beta.1", + "version": "2.1.0-beta.3", "license": "MIT", "dependencies": { "@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7", - "@terra-money/terra.proto": "^4.0.10", + "@terra-money/terra.proto": "5.1.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": "4.0.10", - "resolved": "https://registry.npmjs.org/@terra-money/terra.proto/-/terra.proto-4.0.10.tgz", - "integrity": "sha512-cSTGri/X7r+RjTHKQ40lUDM7+lwWIiodLmBvuCUWMH8svji0D45StZTVGfaQ5wCnPr7KcDbZTERzyLKiSwsBqg==", + "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==", "dependencies": { "@improbable-eng/grpc-web": "^0.14.1", "browser-headers": "^0.4.1", diff --git a/package.json b/package.json index 169e0db..a91e90e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@terra-money/feather.js", - "version": "2.1.0-beta.1", - "description": "The JavaScript SDK for Terra and Feather chains", + "version": "2.1.0-beta.3", + "description": "TypeScript SDK for Terra and Cosmos chains", "license": "MIT", "author": "Terraform Labs, PTE.", "keywords": [ @@ -86,7 +86,7 @@ }, "dependencies": { "@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7", - "@terra-money/terra.proto": "^4.0.10", + "@terra-money/terra.proto": "5.1.0-beta.0", "assert": "^2.0.0", "axios": "^0.27.2", "bech32": "^2.0.0", From 07977b2840c92bca8eb3326a4a93f6f1313bf19e Mon Sep 17 00:00:00 2001 From: freeelancer Date: Tue, 5 Mar 2024 05:18:16 +0800 Subject: [PATCH 02/11] created smartaccount msgs --- src/core/smartaccount/index.ts | 2 + .../v1/models/AuthorizationMsg.ts | 90 +++++++++++++ .../v1/msgs/MsgCreateSmartAccount.ts | 97 ++++++++++++++ .../v1/msgs/MsgDisableSmartAccount.ts | 97 ++++++++++++++ .../v1/msgs/MsgUpdateAuthorization.ts | 119 +++++++++++++++++ .../v1/msgs/MsgUpdateTransactionHooks.ts | 124 ++++++++++++++++++ src/core/smartaccount/v1/msgs/index.ts | 33 +++++ 7 files changed, 562 insertions(+) create mode 100644 src/core/smartaccount/index.ts create mode 100644 src/core/smartaccount/v1/models/AuthorizationMsg.ts create mode 100644 src/core/smartaccount/v1/msgs/MsgCreateSmartAccount.ts create mode 100644 src/core/smartaccount/v1/msgs/MsgDisableSmartAccount.ts create mode 100644 src/core/smartaccount/v1/msgs/MsgUpdateAuthorization.ts create mode 100644 src/core/smartaccount/v1/msgs/MsgUpdateTransactionHooks.ts create mode 100644 src/core/smartaccount/v1/msgs/index.ts diff --git a/src/core/smartaccount/index.ts b/src/core/smartaccount/index.ts new file mode 100644 index 0000000..58474ca --- /dev/null +++ b/src/core/smartaccount/index.ts @@ -0,0 +1,2 @@ +export * from './v1/msgs'; +export * from './v1/models/AuthorizationMsg'; diff --git a/src/core/smartaccount/v1/models/AuthorizationMsg.ts b/src/core/smartaccount/v1/models/AuthorizationMsg.ts new file mode 100644 index 0000000..8d60fd6 --- /dev/null +++ b/src/core/smartaccount/v1/models/AuthorizationMsg.ts @@ -0,0 +1,90 @@ +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'; + +/** + * 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: string) { + super(); + } + + public static fromAmino(data: AuthorizationMsg.Amino): AuthorizationMsg { + const { + value: { contractAddress, initMsg }, + } = data; + return new AuthorizationMsg(contractAddress, initMsg); + } + + public toAmino(): AuthorizationMsg.Amino { + const { contractAddress, initMsg } = this; + return { + value: { + contractAddress, + initMsg, + }, + }; + } + + public static fromData(data: AuthorizationMsg.Data): AuthorizationMsg { + const { contractAddress, initMsg } = data; + return new AuthorizationMsg(contractAddress, initMsg); + } + + public toData(): AuthorizationMsg.Data { + const { contractAddress, initMsg } = this; + return { + contractAddress, + initMsg, + }; + } + + public static fromProto(proto: AuthorizationMsg.Proto): AuthorizationMsg { + return new AuthorizationMsg(proto.contractAddress, proto.initMsg); + } + + 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: string; + }; + } + + export interface Data { + contractAddress: string; + initMsg: string; + } + + export type Proto = AuthorizationMsg_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..0551217 --- /dev/null +++ b/src/core/smartaccount/v1/msgs/MsgUpdateAuthorization.ts @@ -0,0 +1,119 @@ +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); + } + + public toAmino(): MsgUpdateAuthorization.Amino { + const { account, fallback, authorizationMsgs } = this; + return { + type: 'terra/MsgUpdateAuthorization', + value: { + account, + fallback, + authorizationMsgs, + }, + }; + } + + public static fromData( + data: MsgUpdateAuthorization.Data + ): MsgUpdateAuthorization { + const { account, fallback, authorizationMsgs } = data; + return new MsgUpdateAuthorization(account, fallback, authorizationMsgs); + } + + public toData(): MsgUpdateAuthorization.Data { + const { account, fallback, authorizationMsgs } = this; + return { + '@type': '/terra.smartaccount.v1.MsgUpdateAuthorization', + account, + fallback, + authorizationMsgs, + }; + } + + 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[]; + }; + } + + export interface Data { + '@type': '/terra.smartaccount.v1.MsgUpdateAuthorization'; + account: AccAddress; + fallback: boolean; + authorizationMsgs: AuthorizationMsg[]; + } + + 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; +} From dcfbb650e1e7603cacb9b920bc77dce506e8fa5f Mon Sep 17 00:00:00 2001 From: freeelancer Date: Tue, 5 Mar 2024 15:55:03 +0800 Subject: [PATCH 03/11] added query for smartaccounts --- package-lock.json | 8 +- package.json | 2 +- src/client/lcd/LCDClient.ts | 3 + ...arket.API.spec.ts => FeemarketAPI.spec.ts} | 0 src/client/lcd/api/SmartaccountAPI.spec.ts | 35 +++++ src/client/lcd/api/SmartaccountAPI.ts | 41 +++++ src/core/smartaccount/index.ts | 2 + src/core/smartaccount/v1/models/Setting.ts | 145 ++++++++++++++++++ .../v1/models/SmartaccountParams.ts | 57 +++++++ 9 files changed, 288 insertions(+), 5 deletions(-) rename src/client/lcd/api/{Feemarket.API.spec.ts => FeemarketAPI.spec.ts} (100%) create mode 100644 src/client/lcd/api/SmartaccountAPI.spec.ts create mode 100644 src/client/lcd/api/SmartaccountAPI.ts create mode 100644 src/core/smartaccount/v1/models/Setting.ts create mode 100644 src/core/smartaccount/v1/models/SmartaccountParams.ts 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; +} From b250a3780b91d20e69c804aea55001e79717c29c Mon Sep 17 00:00:00 2001 From: freeelancer Date: Tue, 5 Mar 2024 15:57:22 +0800 Subject: [PATCH 04/11] fix casing --- src/client/lcd/LCDClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/lcd/LCDClient.ts b/src/client/lcd/LCDClient.ts index 216fd66..1b17a12 100644 --- a/src/client/lcd/LCDClient.ts +++ b/src/client/lcd/LCDClient.ts @@ -28,7 +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'; +import { SmartaccountAPI } from './api/SmartaccountAPI'; export type AxiosConfig = { /** From 9196190f8d2f03bd114ddfa26055dbca55eefddc Mon Sep 17 00:00:00 2001 From: freeelancer Date: Wed, 6 Mar 2024 18:16:50 +0800 Subject: [PATCH 05/11] pass smartaccountAPI tests --- src/client/lcd/LCDClient.ts | 4 ++-- src/client/lcd/api/SmartaccountAPI.spec.ts | 22 ++++++++++++++-------- src/client/lcd/api/SmartaccountAPI.ts | 4 ++-- src/client/lcd/api/index.ts | 2 ++ 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/client/lcd/LCDClient.ts b/src/client/lcd/LCDClient.ts index 1b17a12..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,8 +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'; -import { SmartaccountAPI } from './api/SmartaccountAPI'; export type AxiosConfig = { /** diff --git a/src/client/lcd/api/SmartaccountAPI.spec.ts b/src/client/lcd/api/SmartaccountAPI.spec.ts index 04ffbc2..bb5958f 100644 --- a/src/client/lcd/api/SmartaccountAPI.spec.ts +++ b/src/client/lcd/api/SmartaccountAPI.spec.ts @@ -1,3 +1,4 @@ +import { Setting } from '../../../core/smartaccount/v1/models/Setting'; import { SmartaccountParams } from '../../../core/smartaccount/v1/models/SmartaccountParams'; import { LCDClient } from '../LCDClient'; import { SmartaccountAPI } from './SmartaccountAPI'; @@ -24,12 +25,17 @@ describe('SmartaccountAPI', () => { 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); - // }); + // 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 index 522df10..77f6730 100644 --- a/src/client/lcd/api/SmartaccountAPI.ts +++ b/src/client/lcd/api/SmartaccountAPI.ts @@ -1,8 +1,8 @@ -import { Setting } from 'core/smartaccount/v1/models/Setting'; +import { Setting } from '../../../core/smartaccount'; import { AccAddress } from '../../../core'; import { LCDClient } from '../LCDClient'; import { BaseAPI } from './BaseAPI'; -import { SmartaccountParams } from 'core/smartaccount'; +import { SmartaccountParams } from '../../../core/smartaccount'; export class SmartaccountAPI extends BaseAPI { constructor(public lcd: LCDClient) { 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'; From 85aac4281275f02ba5419ca67f8fddc823ba9fb9 Mon Sep 17 00:00:00 2001 From: freeelancer Date: Thu, 7 Mar 2024 09:14:25 +0800 Subject: [PATCH 06/11] add smartaccount to msg --- src/core/Msg.ts | 57 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) 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}`); } From db0a17b50e43b662311b0cd1e2fdb960f1547123 Mon Sep 17 00:00:00 2001 From: freeelancer Date: Thu, 7 Mar 2024 21:08:43 +0800 Subject: [PATCH 07/11] changed authorizationMsg --- package-lock.json | 8 +- package.json | 4 +- src/core/smartaccount/index.ts | 1 + .../v1/models/AuthorizationMsg.ts | 12 ++- .../smartaccount/v1/models/Initialization.ts | 99 +++++++++++++++++++ 5 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 src/core/smartaccount/v1/models/Initialization.ts diff --git a/package-lock.json b/package-lock.json index b863c3b..9d92277 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.1", + "@terra-money/terra.proto": "5.1.0-beta.2", "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.1", - "resolved": "https://registry.npmjs.org/@terra-money/terra.proto/-/terra.proto-5.1.0-beta.1.tgz", - "integrity": "sha512-M0UwlbLYsbSFMZZ4YS1lQaLXqnGJaVk+obeHEsgZvJL+0uWaxxFHbLfCXbXKr0SIz6fnPPHdgHAo6trxLu1GXw==", + "version": "5.1.0-beta.2", + "resolved": "https://registry.npmjs.org/@terra-money/terra.proto/-/terra.proto-5.1.0-beta.2.tgz", + "integrity": "sha512-2PxA+0ImHOS+0y8JvGzQXE2AwG5CpWpgq7PL8orE2m6dTWJYfEFxjZGmo1ID9/6aOa1T09dvkHNvTHPg2q06/Q==", "dependencies": { "@improbable-eng/grpc-web": "^0.14.1", "browser-headers": "^0.4.1", diff --git a/package.json b/package.json index 3264efb..c87a4ce 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.1", + "@terra-money/terra.proto": "5.1.0-beta.2", "assert": "^2.0.0", "axios": "^0.27.2", "bech32": "^2.0.0", @@ -104,4 +104,4 @@ "utf-8-validate": "^5.0.5", "ws": "^7.5.9" } -} +} \ No newline at end of file diff --git a/src/core/smartaccount/index.ts b/src/core/smartaccount/index.ts index d977022..c0b4112 100644 --- a/src/core/smartaccount/index.ts +++ b/src/core/smartaccount/index.ts @@ -1,4 +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 index 8d60fd6..6b6611f 100644 --- a/src/core/smartaccount/v1/models/AuthorizationMsg.ts +++ b/src/core/smartaccount/v1/models/AuthorizationMsg.ts @@ -1,6 +1,7 @@ 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 @@ -16,7 +17,7 @@ export class AuthorizationMsg extends JSONSerializable< * @param contractAddress contract address of authorization logic * @param initMsg initial message to be passed to the contract */ - constructor(public contractAddress: string, public initMsg: string) { + constructor(public contractAddress: string, public initMsg: Initialization) { super(); } @@ -51,7 +52,10 @@ export class AuthorizationMsg extends JSONSerializable< } public static fromProto(proto: AuthorizationMsg.Proto): AuthorizationMsg { - return new AuthorizationMsg(proto.contractAddress, proto.initMsg); + return new AuthorizationMsg( + proto.contractAddress, + proto.initMsg as Initialization + ); } public toProto(): AuthorizationMsg.Proto { @@ -77,13 +81,13 @@ export namespace AuthorizationMsg { export interface Amino { value: { contractAddress: string; - initMsg: string; + initMsg: Initialization; }; } export interface Data { contractAddress: string; - initMsg: string; + initMsg: Initialization; } 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..8011b64 --- /dev/null +++ b/src/core/smartaccount/v1/models/Initialization.ts @@ -0,0 +1,99 @@ +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 senders: string[], + public account: string, + public msg: Uint8Array + ) { + super(); + } + + public static fromAmino(data: Initialization.Amino): Initialization { + const { + value: { senders, account, msg }, + } = data; + return new Initialization(senders, account, msg); + } + + public toAmino(): Initialization.Amino { + const { senders, account, msg } = this; + return { + value: { + senders, + account, + msg, + }, + }; + } + + public static fromData(data: Initialization.Data): Initialization { + const { senders, account, msg } = data; + return new Initialization(senders, account, msg); + } + + public toData(): Initialization.Data { + const { senders, account, msg } = this; + return { + senders, + account, + msg, + }; + } + + public static fromProto(proto: Initialization.Proto): Initialization { + return new Initialization(proto.senders, proto.account, proto.msg); + } + + public toProto(): Initialization.Proto { + const { senders, account, msg } = this; + return Initialization_pb.fromPartial({ + senders, + 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: { + senders: string[]; + account: string; + msg: Uint8Array; + }; + } + + export interface Data { + senders: string[]; + account: string; + msg: Uint8Array; + } + + export type Proto = Initialization_pb; +} From c9a2c92b0b7b626beecd3313def35639ede36b2f Mon Sep 17 00:00:00 2001 From: freeelancer Date: Fri, 8 Mar 2024 16:01:43 +0800 Subject: [PATCH 08/11] changed initialization --- .../v1/models/AuthorizationMsg.ts | 24 ++++++++++++------- .../smartaccount/v1/models/Initialization.ts | 18 +++++++++----- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/core/smartaccount/v1/models/AuthorizationMsg.ts b/src/core/smartaccount/v1/models/AuthorizationMsg.ts index 6b6611f..3000a5c 100644 --- a/src/core/smartaccount/v1/models/AuthorizationMsg.ts +++ b/src/core/smartaccount/v1/models/AuthorizationMsg.ts @@ -25,7 +25,10 @@ export class AuthorizationMsg extends JSONSerializable< const { value: { contractAddress, initMsg }, } = data; - return new AuthorizationMsg(contractAddress, initMsg); + return new AuthorizationMsg( + contractAddress, + Initialization.fromAmino(initMsg) + ); } public toAmino(): AuthorizationMsg.Amino { @@ -33,21 +36,24 @@ export class AuthorizationMsg extends JSONSerializable< return { value: { contractAddress, - initMsg, + initMsg: initMsg.toAmino(), }, }; } public static fromData(data: AuthorizationMsg.Data): AuthorizationMsg { - const { contractAddress, initMsg } = data; - return new AuthorizationMsg(contractAddress, initMsg); + const { contract_address, init_msg } = data; + return new AuthorizationMsg( + contract_address, + Initialization.fromData(init_msg) + ); } public toData(): AuthorizationMsg.Data { const { contractAddress, initMsg } = this; return { - contractAddress, - initMsg, + contract_address: contractAddress, + init_msg: initMsg.toData(), }; } @@ -81,13 +87,13 @@ export namespace AuthorizationMsg { export interface Amino { value: { contractAddress: string; - initMsg: Initialization; + initMsg: Initialization.Amino; }; } export interface Data { - contractAddress: string; - initMsg: Initialization; + 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 index 8011b64..b510940 100644 --- a/src/core/smartaccount/v1/models/Initialization.ts +++ b/src/core/smartaccount/v1/models/Initialization.ts @@ -28,31 +28,37 @@ export class Initialization extends JSONSerializable< const { value: { senders, account, msg }, } = data; - return new Initialization(senders, account, msg); + const buf = Buffer.from(msg, 'ascii'); + const msgBs = new Uint8Array(buf); + return new Initialization(senders, account, msgBs); } public toAmino(): Initialization.Amino { const { senders, account, msg } = this; + const base64Str = Buffer.from(msg).toString('ascii'); return { value: { senders, account, - msg, + msg: base64Str, }, }; } public static fromData(data: Initialization.Data): Initialization { const { senders, account, msg } = data; - return new Initialization(senders, account, msg); + const buf = Buffer.from(msg, 'ascii'); + const msgBs = new Uint8Array(buf); + return new Initialization(senders, account, msgBs); } public toData(): Initialization.Data { const { senders, account, msg } = this; + const base64Str = Buffer.from(msg).toString('ascii'); return { senders, account, - msg, + msg: base64Str, }; } @@ -85,14 +91,14 @@ export namespace Initialization { value: { senders: string[]; account: string; - msg: Uint8Array; + msg: string; }; } export interface Data { senders: string[]; account: string; - msg: Uint8Array; + msg: string; } export type Proto = Initialization_pb; From 708ce635e5e57e04418eb5d9b2e8fe5481c3c5f5 Mon Sep 17 00:00:00 2001 From: emidev98 Date: Tue, 12 Mar 2024 10:46:22 +0200 Subject: [PATCH 09/11] chore: adjust Initialization model --- package-lock.json | 8 +++--- package.json | 4 +-- .../smartaccount/v1/models/Initialization.ts | 27 +++++++------------ 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9d92277..5ea5b03 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.2", + "@terra-money/terra.proto": "^5.1.0-beta.3", "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.2", - "resolved": "https://registry.npmjs.org/@terra-money/terra.proto/-/terra.proto-5.1.0-beta.2.tgz", - "integrity": "sha512-2PxA+0ImHOS+0y8JvGzQXE2AwG5CpWpgq7PL8orE2m6dTWJYfEFxjZGmo1ID9/6aOa1T09dvkHNvTHPg2q06/Q==", + "version": "5.1.0-beta.3", + "resolved": "https://registry.npmjs.org/@terra-money/terra.proto/-/terra.proto-5.1.0-beta.3.tgz", + "integrity": "sha512-y69nixzKxV5AK/4Kgza/eF+0Ez++xpbnse5LrYI7ar43OMWP59gxeASeTZPLsbx8bG5CsqCtcRT/klFtwsDzXA==", "dependencies": { "@improbable-eng/grpc-web": "^0.14.1", "browser-headers": "^0.4.1", diff --git a/package.json b/package.json index c87a4ce..0c64ee7 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.2", + "@terra-money/terra.proto": "^5.1.0-beta.3", "assert": "^2.0.0", "axios": "^0.27.2", "bech32": "^2.0.0", @@ -104,4 +104,4 @@ "utf-8-validate": "^5.0.5", "ws": "^7.5.9" } -} \ No newline at end of file +} diff --git a/src/core/smartaccount/v1/models/Initialization.ts b/src/core/smartaccount/v1/models/Initialization.ts index b510940..f19f68f 100644 --- a/src/core/smartaccount/v1/models/Initialization.ts +++ b/src/core/smartaccount/v1/models/Initialization.ts @@ -16,29 +16,24 @@ export class Initialization extends JSONSerializable< * @param contractAddress contract address of authorization logic * @param initMsg initial message to be passed to the contract */ - constructor( - public senders: string[], - public account: string, - public msg: Uint8Array - ) { + constructor(public account: string, public msg: Uint8Array) { super(); } public static fromAmino(data: Initialization.Amino): Initialization { const { - value: { senders, account, msg }, + value: { account, msg }, } = data; const buf = Buffer.from(msg, 'ascii'); const msgBs = new Uint8Array(buf); - return new Initialization(senders, account, msgBs); + return new Initialization(account, msgBs); } public toAmino(): Initialization.Amino { - const { senders, account, msg } = this; + const { account, msg } = this; const base64Str = Buffer.from(msg).toString('ascii'); return { value: { - senders, account, msg: base64Str, }, @@ -46,30 +41,28 @@ export class Initialization extends JSONSerializable< } public static fromData(data: Initialization.Data): Initialization { - const { senders, account, msg } = data; + const { account, msg } = data; const buf = Buffer.from(msg, 'ascii'); const msgBs = new Uint8Array(buf); - return new Initialization(senders, account, msgBs); + return new Initialization(account, msgBs); } public toData(): Initialization.Data { - const { senders, account, msg } = this; + const { account, msg } = this; const base64Str = Buffer.from(msg).toString('ascii'); return { - senders, account, msg: base64Str, }; } public static fromProto(proto: Initialization.Proto): Initialization { - return new Initialization(proto.senders, proto.account, proto.msg); + return new Initialization(proto.account, proto.msg); } public toProto(): Initialization.Proto { - const { senders, account, msg } = this; + const { account, msg } = this; return Initialization_pb.fromPartial({ - senders, account, msg, }); @@ -89,14 +82,12 @@ export class Initialization extends JSONSerializable< export namespace Initialization { export interface Amino { value: { - senders: string[]; account: string; msg: string; }; } export interface Data { - senders: string[]; account: string; msg: string; } From 0718ca50aa778ab9e1938f75f64910fd364c5972 Mon Sep 17 00:00:00 2001 From: freeelancer Date: Tue, 12 Mar 2024 17:20:16 +0800 Subject: [PATCH 10/11] fix msgupdateauthorization --- .../v1/msgs/MsgUpdateAuthorization.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/core/smartaccount/v1/msgs/MsgUpdateAuthorization.ts b/src/core/smartaccount/v1/msgs/MsgUpdateAuthorization.ts index 0551217..fc209de 100644 --- a/src/core/smartaccount/v1/msgs/MsgUpdateAuthorization.ts +++ b/src/core/smartaccount/v1/msgs/MsgUpdateAuthorization.ts @@ -33,7 +33,11 @@ export class MsgUpdateAuthorization extends JSONSerializable< const { value: { account, fallback, authorizationMsgs }, } = data; - return new MsgUpdateAuthorization(account, fallback, authorizationMsgs); + return new MsgUpdateAuthorization( + account, + fallback, + authorizationMsgs.map(msg => AuthorizationMsg.fromAmino(msg)) + ); } public toAmino(): MsgUpdateAuthorization.Amino { @@ -43,7 +47,7 @@ export class MsgUpdateAuthorization extends JSONSerializable< value: { account, fallback, - authorizationMsgs, + authorizationMsgs: authorizationMsgs.map(msg => msg.toAmino()), }, }; } @@ -52,7 +56,10 @@ export class MsgUpdateAuthorization extends JSONSerializable< data: MsgUpdateAuthorization.Data ): MsgUpdateAuthorization { const { account, fallback, authorizationMsgs } = data; - return new MsgUpdateAuthorization(account, fallback, authorizationMsgs); + const authMsgs = authorizationMsgs.map(msg => + AuthorizationMsg.fromData(msg) + ); + return new MsgUpdateAuthorization(account, fallback, authMsgs); } public toData(): MsgUpdateAuthorization.Data { @@ -61,7 +68,7 @@ export class MsgUpdateAuthorization extends JSONSerializable< '@type': '/terra.smartaccount.v1.MsgUpdateAuthorization', account, fallback, - authorizationMsgs, + authorizationMsgs: authorizationMsgs.map(msg => msg.toData()), }; } @@ -104,7 +111,7 @@ export namespace MsgUpdateAuthorization { value: { account: AccAddress; fallback: boolean; - authorizationMsgs: AuthorizationMsg[]; + authorizationMsgs: AuthorizationMsg.Amino[]; }; } @@ -112,7 +119,7 @@ export namespace MsgUpdateAuthorization { '@type': '/terra.smartaccount.v1.MsgUpdateAuthorization'; account: AccAddress; fallback: boolean; - authorizationMsgs: AuthorizationMsg[]; + authorizationMsgs: AuthorizationMsg.Data[]; } export type Proto = MsgUpdateAuthorization_pb; From 08ee2c8360770ca867de24018f5302621886cbf1 Mon Sep 17 00:00:00 2001 From: emidev98 Date: Tue, 12 Mar 2024 11:32:40 +0200 Subject: [PATCH 11/11] chore: increase version 2.1.0-beta.4 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5ea5b03..6c46661 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@terra-money/feather.js", - "version": "2.1.0-beta.3", + "version": "2.1.0-beta.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@terra-money/feather.js", - "version": "2.1.0-beta.3", + "version": "2.1.0-beta.4", "license": "MIT", "dependencies": { "@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7", diff --git a/package.json b/package.json index 0c64ee7..e7da93e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@terra-money/feather.js", - "version": "2.1.0-beta.3", + "version": "2.1.0-beta.4", "description": "TypeScript SDK for Terra and Cosmos chains", "license": "MIT", "author": "Terraform Labs, PTE.",