@cheqd/sdk
Version:
A TypeScript SDK built with CosmJS to interact with the cheqd network ledger
75 lines • 2.73 kB
JavaScript
/**
* Abstract base class for all Cheqd SDK modules.
* Provides common functionality and enforces implementation of required methods.
*/
export class AbstractCheqdSDKModule {
/** Signing client for blockchain transactions */
_signer;
/** Module methods registry */
methods = {};
/** Querier client for data retrieval */
querier;
/** List of methods that should not be exposed externally */
_protectedMethods = ['constructor', 'getRegistryTypes', 'getQuerierExtensionSetup'];
/** Static registry of protobuf message types */
static registryTypes = [];
/** Static querier extension setup function */
static querierExtensionSetup = (base) => ({});
/**
* Creates a new SDK module instance.
*
* @param signer - Signing client for blockchain transactions
* @param querier - Querier client for data retrieval
* @throws Error if signer or querier is not provided
*/
constructor(signer, querier) {
if (!signer) {
throw new Error('signer is required');
}
if (!querier) {
throw new Error('querier is required');
}
this._signer = signer;
this.querier = querier;
}
}
/**
* Creates a new instance of a Cheqd SDK module.
* Generic factory function for instantiating any module type.
*
* @param module - Module constructor class
* @param args - Constructor arguments for the module
* @returns New instance of the specified module
*/
export function instantiateCheqdSDKModule(module, ...args) {
return new module(...args);
}
/**
* Extracts registry types from a module instance.
* Safely retrieves protobuf message types with fallback to empty array.
*
* @param module - Module instance to extract registry types from
* @returns Iterable of [typeUrl, GeneratedType] pairs for protobuf messages
*/
export function instantiateCheqdSDKModuleRegistryTypes(module) {
return module.registryTypes ?? [];
}
export function instantiateCheqdSDKModuleQuerierExtensionSetup(module) {
return module.querierExtensionSetup ?? {};
}
export function applyMixins(derivedCtor, constructors) {
let methods = {};
constructors.forEach((baseCtor) => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
const property = baseCtor.prototype[name];
if (typeof property !== 'function' ||
derivedCtor.hasOwnProperty(name) ||
derivedCtor?.protectedMethods.includes(name) ||
baseCtor.prototype?._protectedMethods?.includes(name))
return;
methods = { ...methods, [name]: property };
});
});
return methods;
}
//# sourceMappingURL=_.js.map