generated from cheqd/.github
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Added v3 core protocol support
- Loading branch information
1 parent
d9dbe56
commit 36936de
Showing
9 changed files
with
221 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { | ||
GasPriceResponse, | ||
GasPricesResponse, | ||
ParamsResponse, | ||
QueryClientImpl, | ||
protobufPackage, | ||
} from '@cheqd/ts-proto/feemarket/feemarket/v1/index.js'; | ||
import { EncodeObject, GeneratedType } from '@cosmjs/proto-signing'; | ||
import { createProtobufRpcClient, QueryClient } from '@cosmjs/stargate'; | ||
import { AbstractCheqdSDKModule, MinimalImportableCheqdSDKModule } from './_'; | ||
import { IContext, QueryExtensionSetup } from '../types'; | ||
import { CheqdQuerier } from '../querier'; | ||
import { CheqdSigningStargateClient } from '../signer'; | ||
|
||
export const defaultFeemarketExtensionKey = 'feemarket' as const; | ||
|
||
export const protobufLiterals = { | ||
GasPriceResponse: 'GasPriceResponse', | ||
GasPricesResponse: 'GasPricesResponse', | ||
ParamsResponse: 'ParamsResponse', | ||
} as const; | ||
|
||
export const typeUrlGasPriceResponse = `/${protobufPackage}.${protobufLiterals.GasPriceResponse}`; | ||
export const typeUrlGasPricesResponse = `/${protobufPackage}.${protobufLiterals.GasPricesResponse}`; | ||
export const typeUrlParamsResponse = `/${protobufPackage}.${protobufLiterals.ParamsResponse}`; | ||
|
||
export interface GasPriceEncodeObject extends EncodeObject { | ||
readonly typeUrl: typeof typeUrlGasPriceResponse; | ||
readonly value: Partial<GasPriceResponse>; | ||
} | ||
|
||
export function isGasPriceEncodeObject(obj: EncodeObject): obj is GasPriceEncodeObject { | ||
return obj.typeUrl === typeUrlGasPriceResponse; | ||
} | ||
|
||
export interface GasPricesEncodeObject extends EncodeObject { | ||
readonly typeUrl: typeof typeUrlGasPricesResponse; | ||
readonly value: Partial<GasPricesResponse>; | ||
} | ||
|
||
export function isGasPricesEncodeObject(obj: EncodeObject): obj is GasPricesEncodeObject { | ||
return obj.typeUrl === typeUrlGasPricesResponse; | ||
} | ||
|
||
export interface ParamsEncodeObject extends EncodeObject { | ||
readonly typeUrl: typeof typeUrlParamsResponse; | ||
readonly value: Partial<ParamsResponse>; | ||
} | ||
|
||
export function isParamsEncodeObject(obj: EncodeObject): obj is ParamsEncodeObject { | ||
return obj.typeUrl === typeUrlParamsResponse; | ||
} | ||
|
||
export type MinimalImportableFeemarketModule = MinimalImportableCheqdSDKModule<FeemarketModule>; | ||
|
||
export type FeemarketExtension = { | ||
readonly [defaultFeemarketExtensionKey]: { | ||
readonly gasPrice: (denom: string) => Promise<GasPriceResponse>; | ||
readonly gasPrices: () => Promise<GasPricesResponse>; | ||
readonly params: () => Promise<ParamsResponse>; | ||
}; | ||
}; | ||
|
||
export const setupFeemarketExtension = (base: QueryClient): FeemarketExtension => { | ||
const rpc = createProtobufRpcClient(base); | ||
|
||
const queryService = new QueryClientImpl(rpc); | ||
|
||
return { | ||
[defaultFeemarketExtensionKey]: { | ||
gasPrice: async (denom: string) => { | ||
return queryService.GasPrice({ denom }); | ||
}, | ||
gasPrices: async () => { | ||
return queryService.GasPrices({}); | ||
}, | ||
params: async () => { | ||
return queryService.Params({}); | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
export class FeemarketModule extends AbstractCheqdSDKModule { | ||
// @ts-expect-error underlying type `GeneratedType` is intentionally wider | ||
static readonly registryTypes: Iterable<[string, GeneratedType]> = [ | ||
[typeUrlGasPriceResponse, GasPriceResponse], | ||
[typeUrlGasPricesResponse, GasPricesResponse], | ||
[typeUrlParamsResponse, ParamsResponse], | ||
]; | ||
|
||
static readonly querierExtensionSetup: QueryExtensionSetup<FeemarketExtension> = setupFeemarketExtension; | ||
|
||
querier: CheqdQuerier & FeemarketExtension; | ||
|
||
constructor(signer: CheqdSigningStargateClient, querier: CheqdQuerier & FeemarketExtension) { | ||
super(signer, querier); | ||
this.querier = querier; | ||
this.methods = { | ||
queryGasPrice: this.queryGasPrice.bind(this), | ||
queryGasPrices: this.queryGasPrices.bind(this), | ||
queryParams: this.queryParams.bind(this), | ||
}; | ||
} | ||
|
||
getRegistryTypes(): Iterable<[string, GeneratedType]> { | ||
return FeemarketModule.registryTypes; | ||
} | ||
|
||
getQuerierExtensionSetup(): QueryExtensionSetup<FeemarketExtension> { | ||
return FeemarketModule.querierExtensionSetup; | ||
} | ||
|
||
async queryGasPrice(denom: string, context?: IContext): Promise<GasPriceResponse> { | ||
if (!this.querier) this.querier = context!.sdk!.querier; | ||
|
||
return this.querier[defaultFeemarketExtensionKey].gasPrice(denom); | ||
} | ||
|
||
async queryGasPrices(context?: IContext): Promise<GasPricesResponse> { | ||
if (!this.querier) this.querier = context!.sdk!.querier; | ||
|
||
return this.querier[defaultFeemarketExtensionKey].gasPrices(); | ||
} | ||
|
||
async queryParams(context?: IContext): Promise<ParamsResponse> { | ||
if (!this.querier) this.querier = context!.sdk!.querier; | ||
|
||
return this.querier[defaultFeemarketExtensionKey].params(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing'; | ||
import { faucet, localnet } from '../testutils.test'; | ||
import { CheqdQuerier, CheqdSigningStargateClient, DIDModule } from '../../src'; | ||
import { FeemarketExtension, FeemarketModule, setupFeemarketExtension } from '../../src/modules/feemarket'; | ||
|
||
const defaultAsyncTxTimeout = 30000; | ||
|
||
(BigInt.prototype as any).toJSON = function () { | ||
return this.toString(); | ||
}; | ||
|
||
describe('FeemarketModule', () => { | ||
describe('constructor', () => { | ||
it('should instantiate standalone module', async () => { | ||
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic); | ||
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet); | ||
const querier = (await CheqdQuerier.connectWithExtension( | ||
localnet.rpcUrl, | ||
setupFeemarketExtension | ||
)) as CheqdQuerier & FeemarketExtension; | ||
const feemarketModule = new FeemarketModule(signer, querier); | ||
expect(feemarketModule).toBeInstanceOf(FeemarketModule); | ||
}); | ||
}); | ||
|
||
describe('queryGasPrice', () => { | ||
it('should query gas price', async () => { | ||
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic); | ||
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet); | ||
const querier = (await CheqdQuerier.connectWithExtension( | ||
localnet.rpcUrl, | ||
setupFeemarketExtension | ||
)) as CheqdQuerier & FeemarketExtension; | ||
const feemarketModule = new FeemarketModule(signer, querier); | ||
const gasPrice = await feemarketModule.queryGasPrice('ncheq'); | ||
|
||
expect(gasPrice).toBeDefined(); | ||
expect(gasPrice.price).toBeDefined(); | ||
expect(gasPrice.price!.denom).toEqual(DIDModule.baseMinimalDenom); | ||
expect(Number(gasPrice.price!.amount)).toBeGreaterThan(0); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters