Skip to content

Use latest cheqd image tag and gas estimation #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/cheqd-api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
env:
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"
CHEQD_IMAGE_TAG: 3.1.5
CHEQD_IMAGE_TAG: 4.0.2
CHEQD_NETWORK: "testnet"
steps:
- uses: actions/checkout@v2
Expand All @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
env:
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"
CHEQD_IMAGE_TAG: 3.1.5
CHEQD_IMAGE_TAG: 4.0.2
CHEQD_NETWORK: "mainnet"
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/cheqd-modules-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
env:
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"
CHEQD_IMAGE_TAG: 3.1.5
CHEQD_IMAGE_TAG: 4.0.2
CHEQD_NETWORK: "testnet"
steps:
- uses: actions/checkout@v2
Expand All @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
env:
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"
CHEQD_IMAGE_TAG: 3.1.5
CHEQD_IMAGE_TAG: 4.0.2
CHEQD_NETWORK: "mainnet"
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
env:
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"
CHEQD_IMAGE_TAG: 3.1.5
CHEQD_IMAGE_TAG: 4.0.2
CHEQD_NETWORK: "testnet"
steps:
- uses: actions/checkout@v2
Expand All @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
env:
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"
CHEQD_IMAGE_TAG: 3.1.5
CHEQD_IMAGE_TAG: 4.0.2
CHEQD_NETWORK: "mainnet"
steps:
- uses: actions/checkout@v2
Expand Down
125 changes: 0 additions & 125 deletions packages/cheqd-blockchain-api/src/api/gas.js

This file was deleted.

97 changes: 27 additions & 70 deletions packages/cheqd-blockchain-api/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
retry,
minBigInt,
} from '@docknetwork/credential-sdk/utils';
import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx.js';
import {
DIDModule,
ResourceModule,
Expand Down Expand Up @@ -55,13 +54,6 @@ import {
import { TypedEnum } from '@docknetwork/credential-sdk/types/generic';
import pLimit from 'p-limit';
import { buildTypeUrlObject, fullTypeUrl, fullTypeUrls } from './type-url';
import {
createOrUpdateDIDDocGas,
createResourceGas,
deactivateDIDDocGas,
gasAmountForBatch,
} from './gas';
import { signedTxHash } from '../utils/tx';

export class CheqdAPI extends AbstractApiProvider {
#sdk;
Expand Down Expand Up @@ -90,13 +82,6 @@ export class CheqdAPI extends AbstractApiProvider {
ResourceModule.fees.DefaultCreateResourceDefaultFee,
);

static BaseGasAmounts = buildTypeUrlObject(
createOrUpdateDIDDocGas,
createOrUpdateDIDDocGas,
deactivateDIDDocGas,
createResourceGas,
);

static PayloadWrappers = buildTypeUrlObject(
CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures,
CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures,
Expand Down Expand Up @@ -233,6 +218,7 @@ export class CheqdAPI extends AbstractApiProvider {
const options = {
modules: [DIDModule, ResourceModule, FeemarketModule],
rpcUrl: url,
endpoint: url,
wallet,
network,
};
Expand All @@ -241,6 +227,7 @@ export class CheqdAPI extends AbstractApiProvider {
this.ensureNotInitialized();
this.#sdk = sdk;
this.#spawn = pLimit(1);
this.#sdk.signer.endpoint = options.endpoint; // HACK: cheqd SDK doesnt pass this with createCheqdSDK yet

return this;
}
Expand Down Expand Up @@ -270,18 +257,10 @@ export class CheqdAPI extends AbstractApiProvider {
* @param {object|Array<object>} txOrTxs
* @returns {Promise<BigInt>}
*/
async estimateGas(txOrTxs) {
const { BaseGasAmounts, BlockLimits } = this.constructor;

async estimateGas(txOrTxs, from) {
const txs = this.constructor.txToJSON(txOrTxs);
const limit = BlockLimits[this.network()];

const estimated = txs.reduce(
(total, { typeUrl, value }) => minBigInt(total + BigInt(BaseGasAmounts[typeUrl](value)), limit),
0n,
);

return String(gasAmountForBatch(estimated, txs.length));
const simulatedGas = await this.sdk.signer.simulate(from, txs);
return String(simulatedGas);
}

/**
Expand Down Expand Up @@ -320,15 +299,13 @@ export class CheqdAPI extends AbstractApiProvider {
*
* @param {string} sender
* @param {Array<object>} txJSON
* @param {object} payment
* @param {DidStdFee | 'auto' | number} payment
* @param {?string} memo
* @returns {Promise<object>}
*/
async signAndBroadcast(sender, txJSON, { ...payment }, memo) {
async signAndBroadcast(sender, txJSON, payment, memo) {
const { BlockLimits } = this.constructor;

let signedTx;
let connectionClosed = false;
const onError = async (err, continueSym) => {
const strErr = String(err);

Expand All @@ -337,45 +314,29 @@ export class CheqdAPI extends AbstractApiProvider {
|| strErr.includes('Bad status')
|| strErr.includes('other side closed')
) {
connectionClosed = true;
console.error(err);
await this.reconnect();

return continueSym;
} else if (
strErr.includes('exists')
&& connectionClosed
&& signedTx != null
) {
const hash = signedTxHash(signedTx);

const res = await this.txResult(hash);
if (res == null) {
throw new Error(
`Transaction with hash ${hash} not found, but entity already exists: ${JSON.stringify(
txJSON,
)}`,
);
}

return res;
} else if (strErr.includes('out of gas in location')) {
const gasAmount = BigInt(payment.gas);
const limit = BlockLimits[this.network()];
if (gasAmount >= limit) {
throw new Error(
`Can't process transaction because it exceeds block gas limit: ${JSON.stringify(
txJSON,
)}`,
);
if (payment.gas) {
const gasAmount = BigInt(payment.gas);
const limit = BlockLimits[this.network()];
if (gasAmount >= limit) {
throw new Error(
`Can't process transaction because it exceeds block gas limit: ${JSON.stringify(
txJSON,
)}`,
);
}

// eslint-disable-next-line no-param-reassign
payment.gas = String(minBigInt(gasAmount * 2n, limit));
} else {
throw err;
}

// eslint-disable-next-line no-param-reassign
payment.gas = String(minBigInt(gasAmount * 2n, limit));
signedTx = void 0;
return continueSym;
} else if (strErr.includes('account sequence mismatch')) {
signedTx = void 0;
return continueSym;
}

Expand All @@ -384,11 +345,7 @@ export class CheqdAPI extends AbstractApiProvider {

return await this.#spawn(() => retry(
async () => {
signedTx ??= TxRaw.encode(
await this.sdk.signer.sign(sender, txJSON, payment, memo ?? ''),
).finish();
const res = await this.sdk.signer.broadcastTx(signedTx);

const res = await this.sdk.signer.signAndBroadcast(sender, txJSON, payment, memo ?? '');
if (res.code) {
console.error(res);

Expand Down Expand Up @@ -417,18 +374,18 @@ export class CheqdAPI extends AbstractApiProvider {
* @returns {Promise<*>}
*/
async signAndSend(tx, {
from, fee, memo, gas,
from, fee, memo, gas, payment,
} = {}) {
const sender = from ?? (await this.address());
const txJSON = this.constructor.txToJSON(tx);

const payment = {
const paymentObj = payment || {
amount: [].concat(fee ?? this.calculateFee(tx)),
gas: gas ?? (await this.estimateGas(tx)),
gas: gas ?? (await this.estimateGas(tx, sender)),
payer: sender,
};

return await this.signAndBroadcast(sender, txJSON, payment, memo);
return await this.signAndBroadcast(sender, txJSON, paymentObj, memo);
}

async reconnect() {
Expand Down