@@ -135,11 +135,35 @@ const useOptions = (options: Options): Network => {
135135
136136type Expiration = { at_height : { height : number } } | { at_time : { time : number } } | { never : { } }
137137
138- interface AllowanceResponse {
138+ interface CanSendResponse {
139+ readonly canSend : boolean ;
140+ }
141+
142+ interface Permissions {
143+ readonly delegate : boolean
144+ readonly undelegate : boolean
145+ readonly redelegate : boolean
146+ readonly withdraw : boolean
147+ }
148+
149+ interface PermissionsInfo {
150+ readonly spender : string ;
151+ readonly permissions : Permissions ;
152+ }
153+
154+ interface AllPermissionsResponse {
155+ readonly permissions : readonly PermissionsInfo [ ] ;
156+ }
157+
158+ interface AllowanceInfo {
139159 readonly balance : readonly Coin [ ] ,
140160 readonly expires : Expiration ,
141161}
142162
163+ interface AllAllowancesResponse {
164+ readonly allowances : readonly AllowanceInfo [ ] ;
165+ }
166+
143167interface AdminListResponse {
144168 readonly admins : readonly string [ ] ,
145169 readonly mutable : boolean ,
@@ -150,8 +174,7 @@ interface InitMsg {
150174 readonly mutable : boolean ,
151175}
152176
153- // TODO: define more of these
154- type CosmosMsg = SendMsg ;
177+ type CosmosMsg = SendMsg | DelegateMsg | UndelegateMsg | RedelegateMsg | WithdrawMsg
155178
156179interface SendMsg {
157180 readonly bank : {
@@ -163,19 +186,62 @@ interface SendMsg {
163186 }
164187}
165188
189+ interface DelegateMsg {
190+ readonly staking : {
191+ readonly delegate : {
192+ readonly validator : string ,
193+ readonly amount : Coin ,
194+ }
195+ }
196+ }
197+
198+ interface UndelegateMsg {
199+ readonly staking : {
200+ readonly undelegate : {
201+ readonly validator : string ,
202+ readonly amount : Coin ,
203+ }
204+ }
205+ }
206+
207+ interface RedelegateMsg {
208+ readonly staking : {
209+ readonly redelegate : {
210+ readonly src_validator : string ,
211+ readonly dst_validator : string ,
212+ readonly amount : Coin ,
213+ }
214+ }
215+ }
216+
217+ interface WithdrawMsg {
218+ readonly staking : {
219+ readonly withdraw : {
220+ readonly validator : string ,
221+ readonly recipient ?: string ,
222+ }
223+ }
224+ }
225+
166226interface CW1Instance {
167227 readonly contractAddress : string
168228
169229 // queries
170230 admins : ( ) => Promise < AdminListResponse >
171- allowance : ( address ?: string ) => Promise < AllowanceResponse >
231+ allowance : ( address ?: string ) => Promise < AllowanceInfo >
232+ allAllowances : ( startAfter ?: string , limit ?: number ) => Promise < AllAllowancesResponse >
233+
234+ permissions : ( address ?: string ) => Promise < PermissionsInfo >
235+ allPermissions : ( startAfter ?: string , limit ?: number ) => Promise < AllPermissionsResponse >
236+ canSend : ( sender : string , msg : CosmosMsg ) => Promise < CanSendResponse >
172237
173238 // actions
174239 execute : ( msgs : readonly CosmosMsg [ ] ) => Promise < string >
175240 freeze : ( ) => Promise < string >
176241 updateAdmins : ( admins : readonly string [ ] ) => Promise < string >
177242 increaseAllowance : ( recipient : string , amount : Coin , expires ?: Expiration ) => Promise < string >
178243 decreaseAllowance : ( recipient : string , amount : Coin , expires ?: Expiration ) => Promise < string >
244+ setPermissions : ( recipient : string , permissions : Permissions ) => Promise < string >
179245}
180246
181247interface CW1Contract {
@@ -194,10 +260,26 @@ interface CW1Contract {
194260
195261const CW1 = ( client : SigningCosmWasmClient ) : CW1Contract => {
196262 const use = ( contractAddress : string ) : CW1Instance => {
197- const allowance = async ( address ?: string ) : Promise < AllowanceResponse > => {
263+ const allowance = async ( address ?: string ) : Promise < AllowanceInfo > => {
198264 const spender = address || client . senderAddress ;
199- const result = await client . queryContractSmart ( contractAddress , { allowance : { spender } } ) ;
200- return result ;
265+ return await client . queryContractSmart ( contractAddress , { allowance : { spender} } ) ;
266+ } ;
267+
268+ const allAllowances = async ( startAfter ?: string , limit ?: number ) : Promise < AllAllowancesResponse > => {
269+ return client . queryContractSmart ( contractAddress , { all_allowances : { start_after : startAfter , limit : limit } } ) ;
270+ } ;
271+
272+ const permissions = async ( address ?: string ) : Promise < PermissionsInfo > => {
273+ const spender = address || client . senderAddress ;
274+ return await client . queryContractSmart ( contractAddress , { permissions : { spender} } ) ;
275+ } ;
276+
277+ const allPermissions = async ( startAfter ?: string , limit ?: number ) : Promise < AllPermissionsResponse > => {
278+ return client . queryContractSmart ( contractAddress , { all_permissions : { start_after : startAfter , limit : limit } } ) ;
279+ } ;
280+
281+ const canSend = async ( sender : string , msg : CosmosMsg ) : Promise < CanSendResponse > => {
282+ return client . queryContractSmart ( contractAddress , { can_send : { sender : sender , msg : msg } } ) ;
201283 } ;
202284
203285 const admins = async ( ) : Promise < AdminListResponse > => {
@@ -231,16 +313,26 @@ const CW1 = (client: SigningCosmWasmClient): CW1Contract => {
231313 const result = await client . execute ( contractAddress , { decrease_allowance : { spender, amount, expires} } ) ;
232314 return result . transactionHash ;
233315 }
234-
316+
317+ const setPermissions = async ( spender : string , permissions : Permissions ) : Promise < string > => {
318+ const result = await client . execute ( contractAddress , { set_permissions : { spender, permissions} } ) ;
319+ return result . transactionHash ;
320+ }
321+
235322 return {
236323 contractAddress,
237324 admins,
238325 allowance,
326+ allAllowances,
327+ permissions,
328+ allPermissions,
329+ canSend,
239330 execute,
240331 freeze,
241332 updateAdmins,
242333 increaseAllowance,
243334 decreaseAllowance,
335+ setPermissions
244336 } ;
245337 }
246338
@@ -273,7 +365,7 @@ const CW1 = (client: SigningCosmWasmClient): CW1Contract => {
273365
274366// Demo:
275367// const client = await useOptions(coralnetOptions).setup(PASSWORD);
276- // const { address} = await client.getAccount()
368+ // const { address } = await client.getAccount()
277369// const factory = CW1(client)
278370//
279371// const codeId = await factory.upload();
@@ -314,3 +406,13 @@ const CW1 = (client: SigningCosmWasmClient): CW1Contract => {
314406// contract.execute([{bank: {send: {from_address: contractAddress, to_address: address, amount: [{denom: "ushell", amount: "440000"}]}}}])
315407// client.getAccount(contractAddress)
316408// client.getAccount()
409+
410+ // let permissions: Permissions = { delegate: true, undelegate: true, redelegate: true, withdraw: true }
411+ // contract.setStakingPermissions(randomAddress, permissions)
412+
413+ // test delegating and undelegating from another account
414+ // let dmsg: DelegateMsg = {staking: {delegate: {validator:"coralvaloper1hf50trj7plz2sd8cmcvn7c8ruh3tjhc2uch4gp", amount:{denom:"ureef",amount:"999"}}}}
415+ // contract.execute([dmsg])
416+ //
417+ // let unmsg: UndelegateMsg = {staking: {undelegate: {validator:"coralvaloper1hf50trj7plz2sd8cmcvn7c8ruh3tjhc2uch4gp", amount:{denom:"ureef",amount:"999"}}}}
418+ // contract.execute([unmsg])
0 commit comments