UNPKG

@cheqd/sdk

Version:

A TypeScript SDK built with CosmJS to interact with the cheqd network ledger

462 lines 21.1 kB
import { protobufPackage, MsgAddHostZone, MsgAddHostZoneResponse, MsgFundFeeAbsModuleAccount, MsgFundFeeAbsModuleAccountResponse, MsgRemoveHostZone, MsgRemoveHostZoneResponse, MsgSendQueryIbcDenomTWAP, MsgSendQueryIbcDenomTWAPResponse, MsgSwapCrossChain, MsgSwapCrossChainResponse, MsgUpdateHostZone, MsgUpdateHostZoneResponse, MsgUpdateParams, MsgUpdateParamsResponse, QueryClientImpl, } from '@cheqd/ts-proto/feeabstraction/feeabs/v1beta1/index.js'; import { AbstractCheqdSDKModule } from './_.js'; import { createProtobufRpcClient } from '@cosmjs/stargate'; import { MsgSubmitProposal } from 'cosmjs-types/cosmos/gov/v1/tx.js'; /** Default extension key for fee abstraction-related query operations */ export const defaultFeeabstractionExtensionKey = 'feeabs'; /** * Protobuf message type literals for fee abstraction operations. * Used for consistent message type identification across the module. */ export const protobufLiterals = { /** Add host zone message type */ MsgAddHostZone: 'MsgAddHostZone', /** Add host zone response message type */ MsgAddHostZoneResponse: 'MsgAddHostZoneResponse', /** Fund fee abstraction module account message type */ MsgFundFeeAbsModuleAccount: 'MsgFundFeeAbsModuleAccount', /** Fund fee abstraction module account response message type */ MsgFundFeeAbsModuleAccountResponse: 'MsgFundFeeAbsModuleAccountResponse', /** Remove host zone message type */ MsgRemoveHostZone: 'MsgRemoveHostZone', /** Remove host zone response message type */ MsgRemoveHostZoneResponse: 'MsgRemoveHostZoneResponse', /** Send query IBC denomination TWAP message type */ MsgSendQueryIbcDenomTWAP: 'MsgSendQueryIbcDenomTWAP', /** Send query IBC denomination TWAP response message type */ MsgSendQueryIbcDenomTWAPResponse: 'MsgSendQueryIbcDenomTWAPResponse', /** Swap cross-chain message type */ MsgSwapCrossChain: 'MsgSwapCrossChain', /** Swap cross-chain response message type */ MsgSwapCrossChainResponse: 'MsgSwapCrossChainResponse', /** Update host zone message type */ MsgUpdateHostZone: 'MsgUpdateHostZone', /** Update host zone response message type */ MsgUpdateHostZoneResponse: 'MsgUpdateHostZoneResponse', /** Update parameters message type */ MsgUpdateParams: 'MsgUpdateParams', /** Update parameters response message type */ MsgUpdateParamsResponse: 'MsgUpdateParamsResponse', }; /** Type URL for MsgAddHostZone messages */ export const typeUrlMsgAddHostZone = `/${protobufPackage}.${protobufLiterals.MsgAddHostZone}`; /** Type URL for MsgAddHostZoneResponse messages */ export const typeUrlMsgAddHostZoneResponse = `/${protobufPackage}.${protobufLiterals.MsgAddHostZoneResponse}`; /** Type URL for MsgFundFeeAbsModuleAccount messages */ export const typeUrlMsgFundFeeAbsModuleAccount = `/${protobufPackage}.${protobufLiterals.MsgFundFeeAbsModuleAccount}`; /** Type URL for MsgFundFeeAbsModuleAccountResponse messages */ export const typeUrlMsgFundFeeAbsModuleAccountResponse = `/${protobufPackage}.${protobufLiterals.MsgFundFeeAbsModuleAccountResponse}`; /** Type URL for MsgRemoveHostZone messages */ export const typeUrlMsgRemoveHostZone = `/${protobufPackage}.${protobufLiterals.MsgRemoveHostZone}`; /** Type URL for MsgRemoveHostZoneResponse messages */ export const typeUrlMsgRemoveHostZoneResponse = `/${protobufPackage}.${protobufLiterals.MsgRemoveHostZoneResponse}`; /** Type URL for MsgSendQueryIbcDenomTWAP messages */ export const typeUrlMsgSendQueryIbcDenomTWAP = `/${protobufPackage}.${protobufLiterals.MsgSendQueryIbcDenomTWAP}`; /** Type URL for MsgSendQueryIbcDenomTWAPResponse messages */ export const typeUrlMsgSendQueryIbcDenomTWAPResponse = `/${protobufPackage}.${protobufLiterals.MsgSendQueryIbcDenomTWAPResponse}`; /** Type URL for MsgSwapCrossChain messages */ export const typeUrlMsgSwapCrossChain = `/${protobufPackage}.${protobufLiterals.MsgSwapCrossChain}`; /** Type URL for MsgSwapCrossChainResponse messages */ export const typeUrlMsgSwapCrossChainResponse = `/${protobufPackage}.${protobufLiterals.MsgSwapCrossChainResponse}`; /** Type URL for MsgUpdateHostZone messages */ export const typeUrlMsgUpdateHostZone = `/${protobufPackage}.${protobufLiterals.MsgUpdateHostZone}`; /** Type URL for MsgUpdateHostZoneResponse messages */ export const typeUrlMsgUpdateHostZoneResponse = `/${protobufPackage}.${protobufLiterals.MsgUpdateHostZoneResponse}`; /** Type URL for MsgUpdateParams messages */ export const typeUrlMsgUpdateParams = `/${protobufPackage}.${protobufLiterals.MsgUpdateParams}`; /** Type URL for MsgUpdateParamsResponse messages */ export const typeUrlMsgUpdateParamsResponse = `/${protobufPackage}.${protobufLiterals.MsgUpdateParamsResponse}`; /** * Type guard function to check if an object is a MsgAddHostZoneEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgAddHostZoneEncodeObject */ export function isMsgAddHostZoneEncodeObject(obj) { return obj.typeUrl === typeUrlMsgAddHostZone; } export function isMsgAddHostZoneResponseEncodeObject(obj) { return obj.typeUrl === typeUrlMsgAddHostZoneResponse; } export function isMsgFundFeeAbsModuleAccountEncodeObject(obj) { return obj.typeUrl === typeUrlMsgFundFeeAbsModuleAccount; } /** * Type guard function to check if an object is a MsgFundFeeAbsModuleAccountResponseEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgFundFeeAbsModuleAccountResponseEncodeObject */ export function isMsgFundFeeAbsModuleAccountResponseEncodeObject(obj) { return obj.typeUrl === typeUrlMsgFundFeeAbsModuleAccountResponse; } /** * Type guard function to check if an object is a MsgRemoveHostZoneEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgRemoveHostZoneEncodeObject */ export function isMsgRemoveHostZoneEncodeObject(obj) { return obj.typeUrl === typeUrlMsgRemoveHostZone; } /** * Type guard function to check if an object is a MsgRemoveHostZoneResponseEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgRemoveHostZoneResponseEncodeObject */ export function isMsgRemoveHostZoneResponseEncodeObject(obj) { return obj.typeUrl === typeUrlMsgRemoveHostZoneResponse; } /** * Type guard function to check if an object is a MsgSendQueryIbcDenomTWAPEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgSendQueryIbcDenomTWAPEncodeObject */ export function isMsgSendQueryIbcDenomTWAPEncodeObject(obj) { return obj.typeUrl === typeUrlMsgSendQueryIbcDenomTWAP; } /** * Type guard function to check if an object is a MsgSendQueryIbcDenomTWAPResponseEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgSendQueryIbcDenomTWAPResponseEncodeObject */ export function isMsgSendQueryIbcDenomTWAPResponseEncodeObject(obj) { return obj.typeUrl === typeUrlMsgSendQueryIbcDenomTWAPResponse; } /** * Type guard function to check if an object is a MsgSwapCrossChainEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgSwapCrossChainEncodeObject */ export function isMsgSwapCrossChainEncodeObject(obj) { return obj.typeUrl === typeUrlMsgSwapCrossChain; } /** * Type guard function to check if an object is a MsgSwapCrossChainResponseEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgSwapCrossChainResponseEncodeObject */ export function isMsgSwapCrossChainResponseEncodeObject(obj) { return obj.typeUrl === typeUrlMsgSwapCrossChainResponse; } /** * Type guard function to check if an object is a MsgUpdateHostZoneEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgUpdateHostZoneEncodeObject */ export function isMsgUpdateHostZoneEncodeObject(obj) { return obj.typeUrl === typeUrlMsgUpdateHostZone; } /** * Type guard function to check if an object is a MsgUpdateHostZoneResponseEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgUpdateHostZoneResponseEncodeObject */ export function isMsgUpdateHostZoneResponseEncodeObject(obj) { return obj.typeUrl === typeUrlMsgUpdateHostZoneResponse; } /** * Type guard function to check if an object is a MsgUpdateParamsEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgUpdateParamsEncodeObject */ export function isMsgUpdateParamsEncodeObject(obj) { return obj.typeUrl === typeUrlMsgUpdateParams; } /** * Type guard function to check if an object is a MsgUpdateParamsResponseEncodeObject. * * @param obj - EncodeObject to check * @returns True if the object is a MsgUpdateParamsResponseEncodeObject */ export function isMsgUpdateParamsResponseEncodeObject(obj) { return obj.typeUrl === typeUrlMsgUpdateParamsResponse; } /** * Sets up the fee abstraction extension for the querier client. * Creates and configures the fee abstraction-specific query methods. * * @param base - Base QueryClient to extend * @returns Configured fee abstraction extension with query methods */ export const setupFeeabstractionExtension = (base) => { const rpc = createProtobufRpcClient(base); const queryService = new QueryClientImpl(rpc); return { [defaultFeeabstractionExtensionKey]: { hostChainConfig: async (request) => { return await queryService.HostChainConfig(request); }, osmosisArithmeticTwap: async (request) => { return await queryService.OsmosisArithmeticTwap(request); }, feeabsModuleBalances: async (request) => { return await queryService.FeeabsModuleBalances(request); }, }, }; }; /** * Fee Abstraction Module class providing comprehensive fee abstraction functionality. * Handles host zone management, cross-chain swaps, and fee abstraction operations on the Cheqd blockchain. */ export class FeeabstractionModule extends AbstractCheqdSDKModule { // @ts-expect-error underlying type `GeneratedType` is intentionally wider static registryTypes = [ [typeUrlMsgAddHostZone, MsgAddHostZone], [typeUrlMsgAddHostZoneResponse, MsgAddHostZoneResponse], [typeUrlMsgFundFeeAbsModuleAccount, MsgFundFeeAbsModuleAccount], [typeUrlMsgFundFeeAbsModuleAccountResponse, MsgFundFeeAbsModuleAccountResponse], [typeUrlMsgRemoveHostZone, MsgRemoveHostZone], [typeUrlMsgRemoveHostZoneResponse, MsgRemoveHostZoneResponse], [typeUrlMsgSendQueryIbcDenomTWAP, MsgSendQueryIbcDenomTWAP], [typeUrlMsgSendQueryIbcDenomTWAPResponse, MsgSendQueryIbcDenomTWAPResponse], [typeUrlMsgSwapCrossChain, MsgSwapCrossChain], [typeUrlMsgSwapCrossChainResponse, MsgSwapCrossChainResponse], [typeUrlMsgUpdateHostZone, MsgUpdateHostZone], [typeUrlMsgUpdateHostZoneResponse, MsgUpdateHostZoneResponse], [typeUrlMsgUpdateParams, MsgUpdateParams], [typeUrlMsgUpdateParamsResponse, MsgUpdateParamsResponse], ]; /** Querier extension setup function for fee abstraction operations */ static querierExtensionSetup = setupFeeabstractionExtension; /** Querier instance with fee abstraction extension capabilities */ querier; /** * Constructs a new fee abstraction module instance. * * @param signer - Signing client for blockchain transactions * @param querier - Querier client with fee abstraction extension for data retrieval */ constructor(signer, querier) { super(signer, querier); this.querier = querier; this.methods = { addHostZoneProposal: this.addHostZoneProposal.bind(this), fundFeeAbsModuleAccount: this.fundFeeAbsModuleAccount.bind(this), removeHostZoneProposal: this.removeHostZoneProposal.bind(this), sendQueryIbcDenomTWAP: this.sendQueryIbcDenomTWAP.bind(this), swapCrossChain: this.swapCrossChain.bind(this), updateHostZoneProposal: this.updateHostZoneProposal.bind(this), updateParamsProposal: this.updateParamsProposal.bind(this), }; } /** * Gets the registry types for fee abstraction message encoding/decoding. * * @returns Iterable of [typeUrl, GeneratedType] pairs for the registry */ getRegistryTypes() { return FeeabstractionModule.registryTypes; } /** * Gets the querier extension setup for fee abstraction operations. * * @returns Query extension setup function for fee abstraction functionality */ getQuerierExtensionSetup() { return FeeabstractionModule.querierExtensionSetup; } /** * Creates a governance proposal to add a new host zone for fee abstraction. * Submits the proposal through the governance module for community voting. * * @param data - Host zone data to add * @param title - Proposal title * @param deposit - Initial deposit for the proposal * @param proposer - Address of the proposal submitter * @param fee - Transaction fee configuration * @param memo - Optional transaction memo * @param context - Optional SDK context for accessing clients * @returns Promise resolving to the transaction response */ async addHostZoneProposal(data, title, deposit, proposer, fee, memo, context) { if (!this._signer) this._signer = context.sdk.signer; const proposalEncodeObject = { typeUrl: '/cosmos.gov.v1.MsgSubmitProposal', value: MsgSubmitProposal.fromPartial({ messages: [ { typeUrl: typeUrlMsgAddHostZone, value: Uint8Array.from(MsgAddHostZone.encode(data).finish()), }, ], title, initialDeposit: deposit, proposer, }), }; return await this._signer.signAndBroadcast(proposer, [proposalEncodeObject], fee, memo); } /** * Funds the fee abstraction module account with tokens. * Allows users to provide liquidity for fee abstraction functionality. * * @param sender - Address of the account sending funds * @param amount - Array of coins to send to the module account * @param fee - Transaction fee configuration * @param memo - Optional transaction memo * @param context - Optional SDK context for accessing clients * @returns Promise resolving to the transaction response */ async fundFeeAbsModuleAccount(sender, amount, fee, memo, context) { if (!this._signer) this._signer = context.sdk.signer; const fundFeeAbsModuleAccountMsg = { typeUrl: typeUrlMsgFundFeeAbsModuleAccount, value: { sender, amount, }, }; return await this._signer.signAndBroadcast(sender, [fundFeeAbsModuleAccountMsg], fee, memo); } /** * Creates a governance proposal to remove an existing host zone from fee abstraction. * Submits the proposal through the governance module for community voting. * * @param data - Host zone data to remove * @param title - Proposal title * @param deposit - Initial deposit for the proposal * @param proposer - Address of the proposal submitter * @param fee - Transaction fee configuration * @param memo - Optional transaction memo * @param context - Optional SDK context for accessing clients * @returns Promise resolving to the transaction response */ async removeHostZoneProposal(data, title, deposit, proposer, fee, memo, context) { if (!this._signer) this._signer = context.sdk.signer; const proposalEncodeObject = { typeUrl: '/cosmos.gov.v1.MsgSubmitProposal', value: MsgSubmitProposal.fromPartial({ messages: [ { typeUrl: typeUrlMsgRemoveHostZone, value: Uint8Array.from(MsgRemoveHostZone.encode(data).finish()), }, ], title, initialDeposit: deposit, proposer, }), }; return await this._signer.signAndBroadcast(proposer, [proposalEncodeObject], fee, memo); } /** * Sends a query for IBC denomination Time-Weighted Average Price (TWAP) data. * Initiates a cross-chain query to retrieve pricing information for fee calculation. * * @param sender - Address of the account sending the query * @param data - TWAP query data containing denomination and parameters * @param fee - Transaction fee configuration * @param memo - Optional transaction memo * @param context - Optional SDK context for accessing clients * @returns Promise resolving to the TWAP query response */ async sendQueryIbcDenomTWAP(sender, data, fee, memo, context) { if (!this._signer) this._signer = context.sdk.signer; const sendQueryIbcDenomTwapMsg = { typeUrl: typeUrlMsgSendQueryIbcDenomTWAP, value: data, }; return await this._signer.signAndBroadcast(sender, [sendQueryIbcDenomTwapMsg], fee, memo); } /** * Performs a cross-chain token swap using fee abstraction. * Enables users to swap IBC tokens across different chains. * * @param sender - Address of the account initiating the swap * @param ibcDenom - IBC denomination to swap * @param fee - Transaction fee configuration * @param memo - Optional transaction memo * @param context - Optional SDK context for accessing clients * @returns Promise resolving to the swap response */ async swapCrossChain(sender, ibcDenom, fee, memo, context) { if (!this._signer) this._signer = context.sdk.signer; const swapCrossChainMsg = { typeUrl: typeUrlMsgSwapCrossChain, value: { ibcDenom, }, }; return await this._signer.signAndBroadcast(sender, [swapCrossChainMsg], fee, memo); } /** * Creates a governance proposal to update an existing host zone configuration. * Submits the proposal through the governance module for community voting. * * @param data - Updated host zone configuration data * @param title - Proposal title * @param deposit - Initial deposit for the proposal * @param proposer - Address of the proposal submitter * @param fee - Transaction fee configuration * @param memo - Optional transaction memo * @param context - Optional SDK context for accessing clients * @returns Promise resolving to the transaction response */ async updateHostZoneProposal(data, title, deposit, proposer, fee, memo, context) { if (!this._signer) this._signer = context.sdk.signer; const proposalEncodeObject = { typeUrl: '/cosmos.gov.v1.MsgSubmitProposal', value: MsgSubmitProposal.fromPartial({ messages: [ { typeUrl: typeUrlMsgUpdateHostZone, value: Uint8Array.from(MsgUpdateHostZone.encode(data).finish()), }, ], title, initialDeposit: deposit, proposer, }), }; return await this._signer.signAndBroadcast(proposer, [proposalEncodeObject], fee, memo); } /** * Creates a governance proposal to update fee abstraction module parameters. * Submits the proposal through the governance module for community voting. * * @param data - Updated module parameters * @param title - Proposal title * @param deposit - Initial deposit for the proposal * @param proposer - Address of the proposal submitter * @param fee - Transaction fee configuration * @param memo - Optional transaction memo * @param context - Optional SDK context for accessing clients * @returns Promise resolving to the transaction response */ async updateParamsProposal(data, title, deposit, proposer, fee, memo, context) { if (!this._signer) this._signer = context.sdk.signer; const proposalEncodeObject = { typeUrl: '/cosmos.gov.v1.MsgSubmitProposal', value: MsgSubmitProposal.fromPartial({ messages: [ { typeUrl: typeUrlMsgUpdateParams, value: Uint8Array.from(MsgUpdateParams.encode(data).finish()), }, ], title, initialDeposit: deposit, proposer, }), }; return await this._signer.signAndBroadcast(proposer, [proposalEncodeObject], fee, memo); } } //# sourceMappingURL=feeabstraction.js.map