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-api/src/api/index.js b/packages/cheqd-blockchain-api/src/api/index.js index ae9bce072..4f2068b6b 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,9 @@ import { FeemarketModule, // eslint-disable-next-line no-unused-vars CheqdSDK, + calculateDidFee, + CheqdQuerier, + setupFeemarketExtension, } from '@cheqd/sdk'; import { DidRef, @@ -53,7 +57,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 +88,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, @@ -247,9 +244,29 @@ export class CheqdAPI extends AbstractApiProvider { }; this.#sdk.signer.endpoint = options.endpoint; // HACK: cheqd SDK doesnt pass this with createCheqdSDK yet + 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; } + /** + * Sets the gas price for fee estimation + */ + setGasPrice(gasPrice) { + this.gasPrice = gasPrice ?? GasPrice.fromString('5000ncheq'); + } + /** * @returns {Promise} */ @@ -287,18 +304,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 +404,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, }; 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"