// Generated by Ignite ignite.com/cli import { StdFee } from "@cosmjs/launchpad"; import { SigningStargateClient, DeliverTxResponse } from "@cosmjs/stargate"; import { EncodeObject, GeneratedType, OfflineSigner, Registry } from "@cosmjs/proto-signing"; import { msgTypes } from './registry'; import { IgniteClient } from "../client" import { MissingWalletError } from "../helpers" import { Api } from "./rest"; import { MsgRevoke } from "./types/cosmos/authz/v1beta1/tx"; import { MsgGrant } from "./types/cosmos/authz/v1beta1/tx"; import { MsgExec } from "./types/cosmos/authz/v1beta1/tx"; export { MsgRevoke, MsgGrant, MsgExec }; type sendMsgRevokeParams = { value: MsgRevoke, fee?: StdFee, memo?: string }; type sendMsgGrantParams = { value: MsgGrant, fee?: StdFee, memo?: string }; type sendMsgExecParams = { value: MsgExec, fee?: StdFee, memo?: string }; type msgRevokeParams = { value: MsgRevoke, }; type msgGrantParams = { value: MsgGrant, }; type msgExecParams = { value: MsgExec, }; export const registry = new Registry(msgTypes); const defaultFee = { amount: [], gas: "200000", }; interface TxClientOptions { addr: string prefix: string signer?: OfflineSigner } export const txClient = ({ signer, prefix, addr }: TxClientOptions = { addr: "http://localhost:26657", prefix: "cosmos" }) => { return { async sendMsgRevoke({ value, fee, memo }: sendMsgRevokeParams): Promise { if (!signer) { throw new Error('TxClient:sendMsgRevoke: Unable to sign Tx. Signer is not present.') } try { const { address } = (await signer.getAccounts())[0]; const signingClient = await SigningStargateClient.connectWithSigner(addr,signer,{registry, prefix}); let msg = this.msgRevoke({ value: MsgRevoke.fromPartial(value) }) return await signingClient.signAndBroadcast(address, [msg], fee ? fee : defaultFee, memo) } catch (e: any) { throw new Error('TxClient:sendMsgRevoke: Could not broadcast Tx: '+ e.message) } }, async sendMsgGrant({ value, fee, memo }: sendMsgGrantParams): Promise { if (!signer) { throw new Error('TxClient:sendMsgGrant: Unable to sign Tx. Signer is not present.') } try { const { address } = (await signer.getAccounts())[0]; const signingClient = await SigningStargateClient.connectWithSigner(addr,signer,{registry, prefix}); let msg = this.msgGrant({ value: MsgGrant.fromPartial(value) }) return await signingClient.signAndBroadcast(address, [msg], fee ? fee : defaultFee, memo) } catch (e: any) { throw new Error('TxClient:sendMsgGrant: Could not broadcast Tx: '+ e.message) } }, async sendMsgExec({ value, fee, memo }: sendMsgExecParams): Promise { if (!signer) { throw new Error('TxClient:sendMsgExec: Unable to sign Tx. Signer is not present.') } try { const { address } = (await signer.getAccounts())[0]; const signingClient = await SigningStargateClient.connectWithSigner(addr,signer,{registry, prefix}); let msg = this.msgExec({ value: MsgExec.fromPartial(value) }) return await signingClient.signAndBroadcast(address, [msg], fee ? fee : defaultFee, memo) } catch (e: any) { throw new Error('TxClient:sendMsgExec: Could not broadcast Tx: '+ e.message) } }, msgRevoke({ value }: msgRevokeParams): EncodeObject { try { return { typeUrl: "/cosmos.authz.v1beta1.MsgRevoke", value: MsgRevoke.fromPartial( value ) } } catch (e: any) { throw new Error('TxClient:MsgRevoke: Could not create message: ' + e.message) } }, msgGrant({ value }: msgGrantParams): EncodeObject { try { return { typeUrl: "/cosmos.authz.v1beta1.MsgGrant", value: MsgGrant.fromPartial( value ) } } catch (e: any) { throw new Error('TxClient:MsgGrant: Could not create message: ' + e.message) } }, msgExec({ value }: msgExecParams): EncodeObject { try { return { typeUrl: "/cosmos.authz.v1beta1.MsgExec", value: MsgExec.fromPartial( value ) } } catch (e: any) { throw new Error('TxClient:MsgExec: Could not create message: ' + e.message) } }, } }; interface QueryClientOptions { addr: string } export const queryClient = ({ addr: addr }: QueryClientOptions = { addr: "http://localhost:1317" }) => { return new Api({ baseURL: addr }); }; class SDKModule { public query: ReturnType; public tx: ReturnType; public registry: Array<[string, GeneratedType]> = []; constructor(client: IgniteClient) { this.query = queryClient({ addr: client.env.apiURL }); this.updateTX(client); client.on('signer-changed',(signer) => { this.updateTX(client); }) } updateTX(client: IgniteClient) { const methods = txClient({ signer: client.signer, addr: client.env.rpcURL, prefix: client.env.prefix ?? "cosmos", }) this.tx = methods; for (let m in methods) { this.tx[m] = methods[m].bind(this.tx); } } }; const Module = (test: IgniteClient) => { return { module: { CosmosAuthzV1Beta1: new SDKModule(test) }, registry: msgTypes } } export default Module;