From 4adb5cebaac1142734747673bb10b0cd05a8057b Mon Sep 17 00:00:00 2001 From: Sam Hellawell Date: Thu, 15 Jan 2026 16:48:06 +0000 Subject: [PATCH 1/3] Proper cheqd fee estimation Signed-off-by: Sam Hellawell --- .../cheqd-blockchain-api/src/api/index.js | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/packages/cheqd-blockchain-api/src/api/index.js b/packages/cheqd-blockchain-api/src/api/index.js index ae9bce072..3f3be2f81 100644 --- a/packages/cheqd-blockchain-api/src/api/index.js +++ b/packages/cheqd-blockchain-api/src/api/index.js @@ -1,4 +1,5 @@ import { AbstractApiProvider } from '@docknetwork/credential-sdk/modules/abstract/common'; +import { GasPrice } from '@cosmjs/stargate'; import { maybeToJSONString, fmtIterable, @@ -15,6 +16,7 @@ import { FeemarketModule, // eslint-disable-next-line no-unused-vars CheqdSDK, + calculateDidFee, } from '@cheqd/sdk'; import { DidRef, @@ -53,7 +55,7 @@ import { } from '@docknetwork/credential-sdk/types'; import { TypedEnum } from '@docknetwork/credential-sdk/types/generic'; import pLimit from 'p-limit'; -import { buildTypeUrlObject, fullTypeUrl, fullTypeUrls } from './type-url'; +import { buildTypeUrlObject, fullTypeUrl } from './type-url'; export class CheqdAPI extends AbstractApiProvider { #sdk; @@ -84,13 +86,6 @@ export class CheqdAPI extends AbstractApiProvider { return this.ensureInitialized().#sdk; } - static Fees = buildTypeUrlObject( - DIDModule.fees.DefaultCreateDidDocFee, - DIDModule.fees.DefaultUpdateDidDocFee, - DIDModule.fees.DefaultDeactivateDidDocFee, - ResourceModule.fees.DefaultCreateResourceDefaultFee, - ); - static PayloadWrappers = buildTypeUrlObject( CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures, CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures, @@ -246,10 +241,18 @@ export class CheqdAPI extends AbstractApiProvider { network, }; this.#sdk.signer.endpoint = options.endpoint; // HACK: cheqd SDK doesnt pass this with createCheqdSDK yet + this.setGasPrice(options.gasPrice); return this; } + /** + * Sets the gas price for fee estimation + */ + setGasPrice(gasPrice) { + this.gasPrice = gasPrice ?? GasPrice.fromString('5000ncheq'); + } + /** * @returns {Promise} */ @@ -287,18 +290,10 @@ export class CheqdAPI extends AbstractApiProvider { * @param {object|Array} txOrTxs * @returns {BigInt} */ - calculateFee(txOrTxs) { - const { Fees } = this.constructor; - - const amount = fullTypeUrls(txOrTxs).reduce( - (total, typeUrl) => total + BigInt(Fees[typeUrl].amount), - 0n, - ); - - return { - amount: String(amount), - denom: 'ncheq', - }; + async calculateFee(txOrTxs, signerAddress, estimatedGas = null, multiplier = 1.3, memo = '') { + const gasEstimation = estimatedGas ?? (await this.simulate(signerAddress, Array.isArray(txOrTxs) ? txOrTxs : [txOrTxs], memo)); + const usedFee = calculateDidFee(Math.round(gasEstimation * multiplier), this.gasPrice); + return usedFee.amount[0]; } /** @@ -395,9 +390,10 @@ export class CheqdAPI extends AbstractApiProvider { const sender = from ?? (await this.address()); const txJSON = this.constructor.txToJSON(tx); + const estimatedGas = gas ?? (await this.estimateGas(tx, sender)); const paymentObj = payment || { - amount: [].concat(fee ?? this.calculateFee(tx)), - gas: gas ?? (await this.estimateGas(tx, sender)), + amount: [].concat(fee ?? (await this.calculateFee(tx, sender, estimatedGas))), + gas: estimatedGas, payer: sender, }; From f3bef5594d4e5f03ca8e8bf4fe86623742c6c333 Mon Sep 17 00:00:00 2001 From: Sam Hellawell Date: Thu, 15 Jan 2026 17:28:28 +0000 Subject: [PATCH 2/3] Use fee market for auto gas price at init --- packages/cheqd-blockchain-api/src/api/index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/cheqd-blockchain-api/src/api/index.js b/packages/cheqd-blockchain-api/src/api/index.js index 3f3be2f81..4f2068b6b 100644 --- a/packages/cheqd-blockchain-api/src/api/index.js +++ b/packages/cheqd-blockchain-api/src/api/index.js @@ -17,6 +17,8 @@ import { // eslint-disable-next-line no-unused-vars CheqdSDK, calculateDidFee, + CheqdQuerier, + setupFeemarketExtension, } from '@cheqd/sdk'; import { DidRef, @@ -241,7 +243,19 @@ export class CheqdAPI extends AbstractApiProvider { network, }; this.#sdk.signer.endpoint = options.endpoint; // HACK: cheqd SDK doesnt pass this with createCheqdSDK yet - this.setGasPrice(options.gasPrice); + + if (options.gasPrice) { + this.setGasPrice(options.gasPrice); + } else { + const querier = (await CheqdQuerier.connectWithExtension( + rpcUrls[0], + setupFeemarketExtension, + )); + + const feemarketModule = new FeemarketModule(this.#sdk.signer, querier); + const gasPrice = await feemarketModule.queryGasPrice('ncheq'); + this.setGasPrice(gasPrice?.price ? GasPrice.fromString(`${gasPrice.price.amount}${gasPrice.price.denom}`) : undefined); + } return this; } From 55a554c51602c32bb095108f638d1df87420c631 Mon Sep 17 00:00:00 2001 From: Sam Hellawell Date: Thu, 15 Jan 2026 17:29:42 +0000 Subject: [PATCH 3/3] Changesets --- examples/CHANGELOG.md | 7 +++++++ examples/package.json | 4 ++-- packages/cheqd-blockchain-api/CHANGELOG.md | 6 ++++++ packages/cheqd-blockchain-api/package.json | 2 +- packages/cheqd-blockchain-modules/package.json | 2 +- scripts/bench/CHANGELOG.md | 7 +++++++ scripts/bench/package.json | 4 ++-- 7 files changed, 26 insertions(+), 6 deletions(-) diff --git a/examples/CHANGELOG.md b/examples/CHANGELOG.md index 4da49537b..78f09ff99 100644 --- a/examples/CHANGELOG.md +++ b/examples/CHANGELOG.md @@ -1,5 +1,12 @@ # @docknetwork/sdk-examples +## 0.21.18 + +### Patch Changes + +- Updated dependencies + - @docknetwork/cheqd-blockchain-api@4.1.1 + ## 0.21.17 ### Patch Changes diff --git a/examples/package.json b/examples/package.json index 1453c31ed..f83ba5a3e 100644 --- a/examples/package.json +++ b/examples/package.json @@ -2,7 +2,7 @@ "name": "@docknetwork/sdk-examples", "private": true, "type": "module", - "version": "0.21.17", + "version": "0.21.18", "scripts": { "bbs-dock-example": "babel-node ./src/bbs-dock.js", "claim-deduction-example": "babel-node ./src/claim-deduction.js", @@ -19,7 +19,7 @@ }, "dependencies": { "@docknetwork/credential-sdk": "0.54.16", - "@docknetwork/cheqd-blockchain-api": "4.1.0", + "@docknetwork/cheqd-blockchain-api": "4.1.1", "@docknetwork/cheqd-blockchain-modules": "4.0.8" }, "devDependencies": { diff --git a/packages/cheqd-blockchain-api/CHANGELOG.md b/packages/cheqd-blockchain-api/CHANGELOG.md index 9a4b14d14..e21f47004 100644 --- a/packages/cheqd-blockchain-api/CHANGELOG.md +++ b/packages/cheqd-blockchain-api/CHANGELOG.md @@ -1,5 +1,11 @@ # @docknetwork/cheqd-blockchain-api +## 4.1.1 + +### Patch Changes + +- Proper cheqd fee estimation + ## 4.1.0 ### Minor Changes diff --git a/packages/cheqd-blockchain-api/package.json b/packages/cheqd-blockchain-api/package.json index e992a0ae3..64f10fe39 100644 --- a/packages/cheqd-blockchain-api/package.json +++ b/packages/cheqd-blockchain-api/package.json @@ -1,6 +1,6 @@ { "name": "@docknetwork/cheqd-blockchain-api", - "version": "4.1.0", + "version": "4.1.1", "license": "MIT", "main": "./dist/esm/index.js", "type": "module", diff --git a/packages/cheqd-blockchain-modules/package.json b/packages/cheqd-blockchain-modules/package.json index 9ff3bfbad..b67d49a4d 100644 --- a/packages/cheqd-blockchain-modules/package.json +++ b/packages/cheqd-blockchain-modules/package.json @@ -41,7 +41,7 @@ "@babel/plugin-syntax-import-attributes": "^7.25.6", "@babel/plugin-transform-modules-commonjs": "^7.24.1", "@babel/preset-env": "^7.24.3", - "@docknetwork/cheqd-blockchain-api": "4.1.0", + "@docknetwork/cheqd-blockchain-api": "4.1.1", "@docknetwork/credential-sdk": "^0.54.11", "@rollup/plugin-alias": "^4.0.2", "@rollup/plugin-babel": "^6.0.4", diff --git a/scripts/bench/CHANGELOG.md b/scripts/bench/CHANGELOG.md index 6f2dd2fec..b759fd78a 100644 --- a/scripts/bench/CHANGELOG.md +++ b/scripts/bench/CHANGELOG.md @@ -1,5 +1,12 @@ # @docknetwork/benchmarks +## 0.4.18 + +### Patch Changes + +- Updated dependencies + - @docknetwork/cheqd-blockchain-api@4.1.1 + ## 0.4.17 ### Patch Changes diff --git a/scripts/bench/package.json b/scripts/bench/package.json index e95cd169b..68d345982 100644 --- a/scripts/bench/package.json +++ b/scripts/bench/package.json @@ -2,12 +2,12 @@ "name": "@docknetwork/benchmarks", "private": true, "type": "module", - "version": "0.4.17", + "version": "0.4.18", "scripts": { "bench": "babel-node src/main.js" }, "dependencies": { - "@docknetwork/cheqd-blockchain-api": "4.1.0", + "@docknetwork/cheqd-blockchain-api": "4.1.1", "@docknetwork/cheqd-blockchain-modules": "4.0.8", "@docknetwork/credential-sdk": "0.54.16", "@docknetwork/crypto-wasm-ts": "^0.63.0"