Skip to content

Commit 009e028

Browse files
committed
Use cheqd SDK gas estimation
1 parent 1579036 commit 009e028

File tree

5 files changed

+33
-181
lines changed

5 files changed

+33
-181
lines changed

.github/workflows/cheqd-api-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
env:
88
CHEQD_MNEMONIC: "steak come surprise obvious remain black trouble measure design volume retreat float coach amused match album moment radio stuff crack orphan ranch dose endorse"
9-
CHEQD_IMAGE_TAG: 3.1.5
9+
CHEQD_IMAGE_TAG: 4.0.2
1010
CHEQD_NETWORK: "testnet"
1111
steps:
1212
- uses: actions/checkout@v2
@@ -22,7 +22,7 @@ jobs:
2222
runs-on: ubuntu-latest
2323
env:
2424
CHEQD_MNEMONIC: "steak come surprise obvious remain black trouble measure design volume retreat float coach amused match album moment radio stuff crack orphan ranch dose endorse"
25-
CHEQD_IMAGE_TAG: 3.1.5
25+
CHEQD_IMAGE_TAG: 4.0.2
2626
CHEQD_NETWORK: "mainnet"
2727
steps:
2828
- uses: actions/checkout@v2

.github/workflows/cheqd-modules-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
env:
88
CHEQD_MNEMONIC: "steak come surprise obvious remain black trouble measure design volume retreat float coach amused match album moment radio stuff crack orphan ranch dose endorse"
9-
CHEQD_IMAGE_TAG: 3.1.5
9+
CHEQD_IMAGE_TAG: 4.0.2
1010
CHEQD_NETWORK: "testnet"
1111
steps:
1212
- uses: actions/checkout@v2
@@ -22,7 +22,7 @@ jobs:
2222
runs-on: ubuntu-latest
2323
env:
2424
CHEQD_MNEMONIC: "steak come surprise obvious remain black trouble measure design volume retreat float coach amused match album moment radio stuff crack orphan ranch dose endorse"
25-
CHEQD_IMAGE_TAG: 3.1.5
25+
CHEQD_IMAGE_TAG: 4.0.2
2626
CHEQD_NETWORK: "mainnet"
2727
steps:
2828
- uses: actions/checkout@v2

.github/workflows/examples.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
env:
88
CHEQD_MNEMONIC: "steak come surprise obvious remain black trouble measure design volume retreat float coach amused match album moment radio stuff crack orphan ranch dose endorse"
9-
CHEQD_IMAGE_TAG: 3.1.5
9+
CHEQD_IMAGE_TAG: 4.0.2
1010
CHEQD_NETWORK: "testnet"
1111
steps:
1212
- uses: actions/checkout@v2
@@ -22,7 +22,7 @@ jobs:
2222
runs-on: ubuntu-latest
2323
env:
2424
CHEQD_MNEMONIC: "steak come surprise obvious remain black trouble measure design volume retreat float coach amused match album moment radio stuff crack orphan ranch dose endorse"
25-
CHEQD_IMAGE_TAG: 3.1.5
25+
CHEQD_IMAGE_TAG: 4.0.2
2626
CHEQD_NETWORK: "mainnet"
2727
steps:
2828
- uses: actions/checkout@v2

packages/cheqd-blockchain-api/src/api/gas.js

Lines changed: 0 additions & 125 deletions
This file was deleted.

packages/cheqd-blockchain-api/src/api/index.js

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
retry,
88
minBigInt,
99
} from '@docknetwork/credential-sdk/utils';
10-
import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx.js';
1110
import {
1211
DIDModule,
1312
ResourceModule,
@@ -55,13 +54,6 @@ import {
5554
import { TypedEnum } from '@docknetwork/credential-sdk/types/generic';
5655
import pLimit from 'p-limit';
5756
import { buildTypeUrlObject, fullTypeUrl, fullTypeUrls } from './type-url';
58-
import {
59-
createOrUpdateDIDDocGas,
60-
createResourceGas,
61-
deactivateDIDDocGas,
62-
gasAmountForBatch,
63-
} from './gas';
64-
import { signedTxHash } from '../utils/tx';
6557

6658
export class CheqdAPI extends AbstractApiProvider {
6759
#sdk;
@@ -90,13 +82,6 @@ export class CheqdAPI extends AbstractApiProvider {
9082
ResourceModule.fees.DefaultCreateResourceDefaultFee,
9183
);
9284

93-
static BaseGasAmounts = buildTypeUrlObject(
94-
createOrUpdateDIDDocGas,
95-
createOrUpdateDIDDocGas,
96-
deactivateDIDDocGas,
97-
createResourceGas,
98-
);
99-
10085
static PayloadWrappers = buildTypeUrlObject(
10186
CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures,
10287
CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures,
@@ -233,6 +218,7 @@ export class CheqdAPI extends AbstractApiProvider {
233218
const options = {
234219
modules: [DIDModule, ResourceModule, FeemarketModule],
235220
rpcUrl: url,
221+
endpoint: url,
236222
wallet,
237223
network,
238224
};
@@ -241,6 +227,7 @@ export class CheqdAPI extends AbstractApiProvider {
241227
this.ensureNotInitialized();
242228
this.#sdk = sdk;
243229
this.#spawn = pLimit(1);
230+
this.#sdk.signer.endpoint = options.endpoint; // HACK: cheqd SDK doesnt pass this with createCheqdSDK yet
244231

245232
return this;
246233
}
@@ -270,18 +257,10 @@ export class CheqdAPI extends AbstractApiProvider {
270257
* @param {object|Array<object>} txOrTxs
271258
* @returns {Promise<BigInt>}
272259
*/
273-
async estimateGas(txOrTxs) {
274-
const { BaseGasAmounts, BlockLimits } = this.constructor;
275-
260+
async estimateGas(txOrTxs, from) {
276261
const txs = this.constructor.txToJSON(txOrTxs);
277-
const limit = BlockLimits[this.network()];
278-
279-
const estimated = txs.reduce(
280-
(total, { typeUrl, value }) => minBigInt(total + BigInt(BaseGasAmounts[typeUrl](value)), limit),
281-
0n,
282-
);
283-
284-
return String(gasAmountForBatch(estimated, txs.length));
262+
const simulatedGas = await this.sdk.signer.simulate(from, txs);
263+
return String(simulatedGas);
285264
}
286265

287266
/**
@@ -320,11 +299,11 @@ export class CheqdAPI extends AbstractApiProvider {
320299
*
321300
* @param {string} sender
322301
* @param {Array<object>} txJSON
323-
* @param {object} payment
302+
* @param {DidStdFee | 'auto' | number} payment
324303
* @param {?string} memo
325304
* @returns {Promise<object>}
326305
*/
327-
async signAndBroadcast(sender, txJSON, { ...payment }, memo) {
306+
async signAndBroadcast(sender, txJSON, payment, memo) {
328307
const { BlockLimits } = this.constructor;
329308

330309
let signedTx;
@@ -360,22 +339,24 @@ export class CheqdAPI extends AbstractApiProvider {
360339

361340
return res;
362341
} else if (strErr.includes('out of gas in location')) {
363-
const gasAmount = BigInt(payment.gas);
364-
const limit = BlockLimits[this.network()];
365-
if (gasAmount >= limit) {
366-
throw new Error(
367-
`Can't process transaction because it exceeds block gas limit: ${JSON.stringify(
368-
txJSON,
369-
)}`,
370-
);
342+
if (payment.gas) {
343+
const gasAmount = BigInt(payment.gas);
344+
const limit = BlockLimits[this.network()];
345+
if (gasAmount >= limit) {
346+
throw new Error(
347+
`Can't process transaction because it exceeds block gas limit: ${JSON.stringify(
348+
txJSON,
349+
)}`,
350+
);
351+
}
352+
353+
// eslint-disable-next-line no-param-reassign
354+
payment.gas = String(minBigInt(gasAmount * 2n, limit));
355+
} else {
356+
throw err;
371357
}
372-
373-
// eslint-disable-next-line no-param-reassign
374-
payment.gas = String(minBigInt(gasAmount * 2n, limit));
375-
signedTx = void 0;
376358
return continueSym;
377359
} else if (strErr.includes('account sequence mismatch')) {
378-
signedTx = void 0;
379360
return continueSym;
380361
}
381362

@@ -384,11 +365,7 @@ export class CheqdAPI extends AbstractApiProvider {
384365

385366
return await this.#spawn(() => retry(
386367
async () => {
387-
signedTx ??= TxRaw.encode(
388-
await this.sdk.signer.sign(sender, txJSON, payment, memo ?? ''),
389-
).finish();
390-
const res = await this.sdk.signer.broadcastTx(signedTx);
391-
368+
const res = await this.sdk.signer.signAndBroadcast(sender, txJSON, payment, memo ?? '');
392369
if (res.code) {
393370
console.error(res);
394371

@@ -417,18 +394,18 @@ export class CheqdAPI extends AbstractApiProvider {
417394
* @returns {Promise<*>}
418395
*/
419396
async signAndSend(tx, {
420-
from, fee, memo, gas,
397+
from, fee, memo, gas, payment,
421398
} = {}) {
422399
const sender = from ?? (await this.address());
423400
const txJSON = this.constructor.txToJSON(tx);
424401

425-
const payment = {
402+
const paymentObj = payment || {
426403
amount: [].concat(fee ?? this.calculateFee(tx)),
427-
gas: gas ?? (await this.estimateGas(tx)),
404+
gas: gas ?? (await this.estimateGas(tx, sender)),
428405
payer: sender,
429406
};
430407

431-
return await this.signAndBroadcast(sender, txJSON, payment, memo);
408+
return await this.signAndBroadcast(sender, txJSON, paymentObj, memo);
432409
}
433410

434411
async reconnect() {

0 commit comments

Comments
 (0)