-
Notifications
You must be signed in to change notification settings - Fork 36
/
feegrant.ts
214 lines (198 loc) · 7.22 KB
/
feegrant.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { MsgParams } from ".";
import {
AllowedMsgAllowance as AllowedMsgAllowanceProto,
BasicAllowance,
PeriodicAllowance,
} from "../protobuf/cosmos/feegrant/v1beta1/feegrant";
import {
MsgGrantAllowance as MsgGrantAllowanceProto,
MsgRevokeAllowance as MsgRevokeAllowanceProto,
} from "../protobuf/cosmos/feegrant/v1beta1/tx";
import { Any } from "../protobuf/google/protobuf/any";
import { AminoMsg, Msg, ProtoMsg } from "./types";
export interface MsgGrantAllowanceParams extends MsgParams {
/** granter is the address of the user granting an allowance of their funds. */
granter: string;
/** grantee is the address of the user being granted an allowance of another user's funds. */
grantee: string;
/** allowance can be any of basic and filtered fee allowance. */
allowance: BasicAllowance | PeriodicAllowance | AllowedMsgAllowance;
}
export interface AllowedMsgAllowance {
/** allowance can be any of basic and filtered fee allowance. */
allowance: BasicAllowance | PeriodicAllowance;
/** allowed_messages are the messages for which the grantee has the access. */
allowed_messages: string[];
}
function isBasicAllowance(obj: any): obj is BasicAllowance {
return "spend_limit" in obj;
}
function isPeriodicAllowance(obj: any): obj is PeriodicAllowance {
return "period_spend_limit" in obj;
}
function isAllowedMsgAllowance(obj: any): obj is AllowedMsgAllowanceProto {
return "allowed_messages" in obj;
}
/**
* MsgGrantAllowance adds permission for Grantee to spend up to Allowance
* of fees from the account of Granter.
*/
export class MsgGrantAllowance implements Msg {
constructor(public params: MsgGrantAllowanceParams) {}
async toProto(): Promise<ProtoMsg> {
let allowance: Any;
if (isBasicAllowance(this.params.allowance)) {
allowance = {
type_url: "/cosmos.feegrant.v1beta1.BasicAllowance",
value: BasicAllowance.encode(this.params.allowance).finish(),
};
} else if (isPeriodicAllowance(this.params.allowance)) {
allowance = {
type_url: "/cosmos.feegrant.v1beta1.PeriodicAllowance",
value: PeriodicAllowance.encode(this.params.allowance).finish(),
};
} else if (isAllowedMsgAllowance(this.params.allowance)) {
let internalAllowance: Any;
if (isBasicAllowance(this.params.allowance.allowance)) {
internalAllowance = {
type_url: "/cosmos.feegrant.v1beta1.BasicAllowance",
value: BasicAllowance.encode(
this.params.allowance.allowance,
).finish(),
};
} else if (isPeriodicAllowance(this.params.allowance.allowance)) {
internalAllowance = {
type_url: "/cosmos.feegrant.v1beta1.PeriodicAllowance",
value: PeriodicAllowance.encode(
this.params.allowance.allowance,
).finish(),
};
} else {
throw new Error(
`PeriodicAllowance: Cannot cast allowance into 'BasicAllowance' or 'PeriodicAllowance': ${JSON.stringify(
this.params.allowance.allowance,
)}`,
);
}
allowance = {
type_url: "/cosmos.feegrant.v1beta1.AllowedMsgAllowance",
value: AllowedMsgAllowanceProto.encode({
allowed_messages: this.params.allowance.allowed_messages,
allowance: internalAllowance,
}).finish(),
};
} else {
throw new Error(
`Cannot cast allowance into 'BasicAllowance', 'PeriodicAllowance' or 'AllowedMsgAllowance': ${JSON.stringify(
this.params.allowance,
)}`,
);
}
return {
type_url: "/cosmos.feegrant.v1beta1.MsgGrantAllowance",
value: this.params,
encode: async () =>
MsgGrantAllowanceProto.encode({
grantee: this.params.grantee,
granter: this.params.granter,
allowance,
}).finish(),
};
}
async toAmino(): Promise<AminoMsg> {
let allowance: { type: string; value: object };
if (isBasicAllowance(this.params.allowance)) {
allowance = {
type: "cosmos-sdk/BasicAllowance",
value: {
spend_limit: this.params.allowance.spend_limit,
expiration: this.params.allowance.expiration,
},
};
} else if (isPeriodicAllowance(this.params.allowance)) {
allowance = {
type: "cosmos-sdk/PeriodicAllowance",
value: {
basic: this.params.allowance.basic,
period: this.params.allowance.period,
period_spend_limit: this.params.allowance.period_spend_limit,
period_can_spend: this.params.allowance.period_can_spend,
period_reset: this.params.allowance.period_reset,
},
};
} else if (isAllowedMsgAllowance(this.params.allowance)) {
let internalAllowance: { type: string; value: object };
if (isBasicAllowance(this.params.allowance.allowance)) {
internalAllowance = {
type: "cosmos-sdk/BasicAllowance",
value: {
spend_limit: this.params.allowance.allowance.spend_limit,
expiration: this.params.allowance.allowance.expiration,
},
};
} else if (isPeriodicAllowance(this.params.allowance.allowance)) {
internalAllowance = {
type: "cosmos-sdk/PeriodicAllowance",
value: {
basic: this.params.allowance.allowance.basic,
period: this.params.allowance.allowance.period,
period_spend_limit:
this.params.allowance.allowance.period_spend_limit,
period_can_spend: this.params.allowance.allowance.period_can_spend,
period_reset: this.params.allowance.allowance.period_reset,
},
};
} else {
throw new Error(
`PeriodicAllowance: Cannot cast allowance into 'BasicAllowance' or 'PeriodicAllowance': ${JSON.stringify(
this.params.allowance.allowance,
)}`,
);
}
allowance = {
type: "cosmos-sdk/AllowedMsgAllowance",
value: {
allowed_messages: this.params.allowance.allowed_messages,
allowance: internalAllowance,
},
};
} else {
throw new Error(
`Cannot cast allowance into 'BasicAllowance', 'PeriodicAllowance' or 'AllowedMsgAllowance': ${JSON.stringify(
this.params.allowance,
)}`,
);
}
return {
type: "cosmos-sdk/MsgGrantAllowance",
value: {
granter: this.params.granter,
grantee: this.params.grantee,
allowance: allowance,
},
};
}
}
export interface MsgRevokeAllowanceParams extends MsgParams {
/** granter is the address of the user granting an allowance of their funds. */
granter: string;
/** grantee is the address of the user being granted an allowance of another user's funds. */
grantee: string;
}
/** MsgRevokeAllowance removes any existing Allowance from Granter to Grantee. */
export class MsgRevokeAllowance implements Msg {
constructor(public params: MsgRevokeAllowanceParams) {}
async toProto(): Promise<ProtoMsg> {
return {
type_url: "/cosmos.feegrant.v1beta1.MsgRevokeAllowance",
value: this.params,
encode: async () => MsgRevokeAllowanceProto.encode(this.params).finish(),
};
}
async toAmino(): Promise<AminoMsg> {
return {
type: "cosmos-sdk/MsgRevokeAllowance",
value: this.params,
};
}
}