Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
6 changes: 6 additions & 0 deletions packages/cheqd-blockchain-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @docknetwork/cheqd-blockchain-api

## 4.1.1

### Patch Changes

- Proper cheqd fee estimation

## 4.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cheqd-blockchain-api/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
54 changes: 32 additions & 22 deletions packages/cheqd-blockchain-api/src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AbstractApiProvider } from '@docknetwork/credential-sdk/modules/abstract/common';
import { GasPrice } from '@cosmjs/stargate';
import {
maybeToJSONString,
fmtIterable,
Expand All @@ -15,6 +16,9 @@ import {
FeemarketModule,
// eslint-disable-next-line no-unused-vars
CheqdSDK,
calculateDidFee,
CheqdQuerier,
setupFeemarketExtension,
} from '@cheqd/sdk';
import {
DidRef,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<this>}
*/
Expand Down Expand Up @@ -287,18 +304,10 @@ export class CheqdAPI extends AbstractApiProvider {
* @param {object|Array<object>} 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];
}

/**
Expand Down Expand Up @@ -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,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/cheqd-blockchain-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions scripts/bench/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions scripts/bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading