@cosmwasm/ts-codegen
Version:
@cosmwasm/ts-codegen converts your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.
123 lines (105 loc) • 3.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.contractContextBase = void 0;
exports.contractContextBase = `
import {
ICosmWasmClient,
ISigningCosmWasmClient,
getCosmWasmClient,
getSigningCosmWasmClient,
} from './baseClient';
export interface IContractConstructor {
address: string | undefined;
cosmWasmClient: ICosmWasmClient | undefined;
signingCosmWasmClient: ISigningCosmWasmClient | undefined;
}
export const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';
export const NO_COSMWASW_CLIENT_ERROR_MESSAGE = 'cosmWasmClient not connected';
export const NO_ADDRESS_ERROR_MESSAGE = "address doesn't exist";
export const NO_SIGNING_CLIENT_ERROR_MESSAGE =
'Signing client is not generated. Please check ts-codegen config';
export const NO_QUERY_CLIENT_ERROR_MESSAGE =
'Query client is not generated. Please check ts-codegen config';
export const NO_MESSAGE_COMPOSER_ERROR_MESSAGE =
'Message composer client is not generated. Please check ts-codegen config';
/**
* a placeholder for non-generated classes
*/
export interface IEmptyClient {}
export interface ISigningClientProvider<T> {
getSigningClient(contractAddr: string): T;
}
export interface IQueryClientProvider<T> {
getQueryClient(contractAddr: string): T;
}
export interface IMessageComposerProvider<T> {
getMessageComposer(contractAddr: string): T;
}
export class ContractBase<
TSign = IEmptyClient,
TQuery = IEmptyClient,
TMsgComposer = IEmptyClient
> {
address: string | undefined;
cosmWasmClient: ICosmWasmClient | undefined;
signingCosmWasmClient: ISigningCosmWasmClient | undefined;
TSign?: new (
client: ISigningCosmWasmClient,
sender: string,
contractAddress: string
) => TSign;
TQuery?: new (
client: ICosmWasmClient,
contractAddress: string
) => TQuery;
TMsgComposer?: new (
sender: string,
contractAddress: string
) => TMsgComposer;
constructor(
address: string | undefined,
cosmWasmClient: ICosmWasmClient | undefined,
signingCosmWasmClient: ISigningCosmWasmClient | undefined,
TSign?: new (
client: ISigningCosmWasmClient,
sender: string,
contractAddress: string
) => TSign,
TQuery?: new (
client: ICosmWasmClient,
contractAddress: string
) => TQuery,
TMsgComposer?: new (
sender: string,
contractAddress: string
) => TMsgComposer
) {
this.address = address;
this.cosmWasmClient = cosmWasmClient;
this.signingCosmWasmClient = signingCosmWasmClient;
this.TSign = TSign;
this.TQuery = TQuery;
this.TMsgComposer = TMsgComposer;
}
public getSigningClient(contractAddr: string): TSign {
if (!this.signingCosmWasmClient) throw new Error(NO_SINGING_ERROR_MESSAGE);
if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);
if (!this.TSign) throw new Error(NO_SIGNING_CLIENT_ERROR_MESSAGE);
return new this.TSign(
this.signingCosmWasmClient,
this.address,
contractAddr
);
}
public getQueryClient(contractAddr: string): TQuery {
if (!this.cosmWasmClient) throw new Error(NO_COSMWASW_CLIENT_ERROR_MESSAGE);
if (!this.TQuery) throw new Error(NO_QUERY_CLIENT_ERROR_MESSAGE);
return new this.TQuery(this.cosmWasmClient, contractAddr);
}
public getMessageComposer(contractAddr: string): TMsgComposer {
if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);
if (!this.TMsgComposer) throw new Error(NO_MESSAGE_COMPOSER_ERROR_MESSAGE);
return new this.TMsgComposer(this.address, contractAddr);
}
}
`;