Skip to content

Commit

Permalink
added query for smartaccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
freeelancer committed Mar 5, 2024
1 parent 07977b2 commit dcfbb65
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 5 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/client/lcd/LCDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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') {
Expand Down
File renamed without changes.
35 changes: 35 additions & 0 deletions src/client/lcd/api/SmartaccountAPI.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
// });
});
41 changes: 41 additions & 0 deletions src/client/lcd/api/SmartaccountAPI.ts
Original file line number Diff line number Diff line change
@@ -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<SmartaccountParams> {
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<Setting> {
const res = await this.getReqFromAddress(account).get<{
setting: Setting.Data;
}>(`/terra/smartaccount/v1/setting/${account}`);

return Setting.fromData(res.setting);
}
}
2 changes: 2 additions & 0 deletions src/core/smartaccount/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './v1/msgs';
export * from './v1/models/AuthorizationMsg';
export * from './v1/models/Setting';
export * from './v1/models/SmartaccountParams';
145 changes: 145 additions & 0 deletions src/core/smartaccount/v1/models/Setting.ts
Original file line number Diff line number Diff line change
@@ -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;
}
57 changes: 57 additions & 0 deletions src/core/smartaccount/v1/models/SmartaccountParams.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit dcfbb65

Please sign in to comment.