Skip to content

Commit dcfbb65

Browse files
committed
added query for smartaccounts
1 parent 07977b2 commit dcfbb65

File tree

9 files changed

+288
-5
lines changed

9 files changed

+288
-5
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
},
8787
"dependencies": {
8888
"@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7",
89-
"@terra-money/terra.proto": "5.1.0-beta.0",
89+
"@terra-money/terra.proto": "5.1.0-beta.1",
9090
"assert": "^2.0.0",
9191
"axios": "^0.27.2",
9292
"bech32": "^2.0.0",

src/client/lcd/LCDClient.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { GovV1API } from './api/GovV1API';
2828
import { ICAv1API } from './api/ICAv1API';
2929
import { ICQv1API } from './api/ICQv1API';
3030
import { FeemarketAPI } from './api/FeemarketAPI';
31+
import { SmartaccountAPI } from './api/SmartAccountAPI';
3132

3233
export type AxiosConfig = {
3334
/**
@@ -138,6 +139,7 @@ export class LCDClient {
138139
public feeshare: FeeshareAPI;
139140
public feemarket: FeemarketAPI;
140141
public utils: LCDUtils;
142+
public smartaccount: SmartaccountAPI;
141143

142144
/**
143145
* Creates a new LCD client with the specified configuration.
@@ -189,6 +191,7 @@ export class LCDClient {
189191
this.feeshare = new FeeshareAPI(this);
190192
this.feemarket = new FeemarketAPI(this);
191193
this.utils = new LCDUtils(this);
194+
this.smartaccount = new SmartaccountAPI(this);
192195
}
193196

194197
public static fromDefaultConfig(network: 'mainnet' | 'testnet') {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { SmartaccountParams } from '../../../core/smartaccount/v1/models/SmartaccountParams';
2+
import { LCDClient } from '../LCDClient';
3+
import { SmartaccountAPI } from './SmartaccountAPI';
4+
5+
const lcd = new LCDClient({
6+
'pisco-1': {
7+
chainID: 'pisco-1',
8+
gasAdjustment: 1.5,
9+
gasPrices: {
10+
uluna: 0.02,
11+
},
12+
lcd: 'http://localhost:1317/',
13+
prefix: 'terra',
14+
},
15+
});
16+
const smartaccount = new SmartaccountAPI(lcd);
17+
18+
describe('SmartaccountAPI', () => {
19+
it('assert the module params', async () => {
20+
const res = await smartaccount.params('pisco-1');
21+
22+
expect(res).toStrictEqual(new SmartaccountParams());
23+
24+
expect(res.toData()).toEqual({});
25+
});
26+
27+
// TODO: there must be a smart account created to test this
28+
// it('assert the account setting', async () => {
29+
// const res = await smartaccount.setting('a');
30+
// // TODO: call
31+
// const setting = new Setting('a', [], [], [], true);
32+
// expect(res).toStrictEqual(setting);
33+
// expect(res.toData()).toEqual(setting);
34+
// });
35+
});

src/client/lcd/api/SmartaccountAPI.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Setting } from 'core/smartaccount/v1/models/Setting';
2+
import { AccAddress } from '../../../core';
3+
import { LCDClient } from '../LCDClient';
4+
import { BaseAPI } from './BaseAPI';
5+
import { SmartaccountParams } from 'core/smartaccount';
6+
7+
export class SmartaccountAPI extends BaseAPI {
8+
constructor(public lcd: LCDClient) {
9+
super(lcd.apiRequesters, lcd.config);
10+
}
11+
12+
/**
13+
* Query the feemarket module params.
14+
*
15+
* @tags Query
16+
* @name params
17+
* @request GET:/terra/smartaccount/v1/params
18+
*/
19+
public async params(chainId: string): Promise<SmartaccountParams> {
20+
const res = await this.getReqFromChainID(chainId).get<{
21+
params: SmartaccountParams.Data;
22+
}>(`/terra/smartaccount/v1/params`);
23+
24+
return SmartaccountParams.fromData(res.params);
25+
}
26+
27+
/**
28+
* Query the feemarket module setting for account.
29+
*
30+
* @tags Query
31+
* @name setting
32+
* @request GET:/terra/smartaccount/v1/setting/{account}
33+
*/
34+
public async setting(account: AccAddress): Promise<Setting> {
35+
const res = await this.getReqFromAddress(account).get<{
36+
setting: Setting.Data;
37+
}>(`/terra/smartaccount/v1/setting/${account}`);
38+
39+
return Setting.fromData(res.setting);
40+
}
41+
}

src/core/smartaccount/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from './v1/msgs';
22
export * from './v1/models/AuthorizationMsg';
3+
export * from './v1/models/Setting';
4+
export * from './v1/models/SmartaccountParams';
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import { Any } from '@terra-money/terra.proto/google/protobuf/any';
2+
import { Setting as Setting_pb } from '@terra-money/terra.proto/terra/smartaccount/v1/setting';
3+
import { JSONSerializable } from '../../../../util/json';
4+
import { AuthorizationMsg } from './AuthorizationMsg';
5+
6+
/**
7+
* Setting holds the contract address and initial message
8+
* to be passed to the contract for custom authorization
9+
*/
10+
export class Setting extends JSONSerializable<
11+
Setting.Amino,
12+
Setting.Data,
13+
Setting.Proto
14+
> {
15+
/**
16+
*
17+
* @param contractAddress contract address of authorization logic
18+
* @param initMsg initial message to be passed to the contract
19+
*/
20+
constructor(
21+
public owner: string,
22+
public authorization: AuthorizationMsg[],
23+
public preTransaction: string[],
24+
public postTransaction: string[],
25+
public fallback: boolean
26+
) {
27+
super();
28+
}
29+
30+
public static fromAmino(data: Setting.Amino): Setting {
31+
const {
32+
value: {
33+
owner,
34+
authorization,
35+
preTransaction,
36+
postTransaction,
37+
fallback,
38+
},
39+
} = data;
40+
return new Setting(
41+
owner,
42+
authorization,
43+
preTransaction,
44+
postTransaction,
45+
fallback
46+
);
47+
}
48+
49+
public toAmino(): Setting.Amino {
50+
const { owner, authorization, preTransaction, postTransaction, fallback } =
51+
this;
52+
return {
53+
value: {
54+
owner,
55+
authorization,
56+
preTransaction,
57+
postTransaction,
58+
fallback,
59+
},
60+
};
61+
}
62+
63+
public static fromData(data: Setting.Data): Setting {
64+
const {
65+
owner,
66+
authorization,
67+
pre_transaction,
68+
post_transaction,
69+
fallback,
70+
} = data;
71+
return new Setting(
72+
owner,
73+
authorization,
74+
pre_transaction,
75+
post_transaction,
76+
fallback
77+
);
78+
}
79+
80+
public toData(): Setting.Data {
81+
const { owner, authorization, preTransaction, postTransaction, fallback } =
82+
this;
83+
return {
84+
owner,
85+
authorization,
86+
pre_transaction: preTransaction,
87+
post_transaction: postTransaction,
88+
fallback,
89+
};
90+
}
91+
92+
public static fromProto(proto: Setting.Proto): Setting {
93+
return new Setting(
94+
proto.owner,
95+
proto.authorization.map(AuthorizationMsg.fromProto),
96+
proto.preTransaction,
97+
proto.postTransaction,
98+
proto.fallback
99+
);
100+
}
101+
102+
public toProto(): Setting.Proto {
103+
const { owner, authorization, preTransaction, postTransaction, fallback } =
104+
this;
105+
return Setting_pb.fromPartial({
106+
owner,
107+
authorization,
108+
preTransaction,
109+
postTransaction,
110+
fallback,
111+
});
112+
}
113+
114+
public packAny(): Any {
115+
return Any.fromPartial({
116+
value: Setting_pb.encode(this.toProto()).finish(),
117+
});
118+
}
119+
120+
public static unpackAny(msgAny: Any): Setting {
121+
return Setting.fromProto(Setting_pb.decode(msgAny.value));
122+
}
123+
}
124+
125+
export namespace Setting {
126+
export interface Amino {
127+
value: {
128+
owner: string;
129+
authorization: AuthorizationMsg[];
130+
preTransaction: string[];
131+
postTransaction: string[];
132+
fallback: boolean;
133+
};
134+
}
135+
136+
export interface Data {
137+
owner: string;
138+
authorization: AuthorizationMsg[];
139+
pre_transaction: string[];
140+
post_transaction: string[];
141+
fallback: boolean;
142+
}
143+
144+
export type Proto = Setting_pb;
145+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Params as Params_pb } from '@terra-money/terra.proto/terra/smartaccount/v1/params';
2+
import { JSONSerializable } from '../../../../util/json';
3+
4+
export class SmartaccountParams extends JSONSerializable<
5+
SmartaccountParams.Amino,
6+
SmartaccountParams.Data,
7+
SmartaccountParams.Proto
8+
> {
9+
constructor() {
10+
super();
11+
}
12+
13+
public static fromAmino(_: SmartaccountParams.Amino): SmartaccountParams {
14+
_;
15+
return new SmartaccountParams();
16+
}
17+
18+
public toAmino(): SmartaccountParams.Amino {
19+
return {
20+
value: {},
21+
};
22+
}
23+
24+
public static fromData(
25+
proto: SmartaccountParams.Data,
26+
_?: boolean
27+
): SmartaccountParams {
28+
proto;
29+
_;
30+
return new SmartaccountParams();
31+
}
32+
33+
public toData(_?: boolean): SmartaccountParams.Data {
34+
_;
35+
return {};
36+
}
37+
38+
public static fromProto(proto: SmartaccountParams.Proto): SmartaccountParams {
39+
proto;
40+
return new SmartaccountParams();
41+
}
42+
43+
public toProto(): SmartaccountParams.Proto {
44+
return Params_pb.fromPartial({});
45+
}
46+
}
47+
48+
export namespace SmartaccountParams {
49+
export interface Amino {
50+
value: {};
51+
}
52+
53+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
54+
export interface Data {}
55+
56+
export type Proto = Params_pb;
57+
}

0 commit comments

Comments
 (0)