From 74d56317530bc5b22cd2d5f7212ee65886fdcbab Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Fri, 23 Aug 2024 12:31:40 +0530 Subject: [PATCH] Accept fees optionally as gas estimation multiplier --- src/constants.ts | 3 +++ src/index.ts | 56 +++++++++++++++++++++---------------------- src/testing/helper.ts | 3 ++- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 7371e32..2206b01 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1 +1,4 @@ export const DENOM = 'alnt'; +export const DEFAULT_CHAIN_ID = 'laconic_9000-1'; + +export const DEFAULT_GAS_ESTIMATION_MULTIPLIER = 1.5; diff --git a/src/index.ts b/src/index.ts index 7a3b776..f82cc49 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,8 +31,7 @@ import { Coin } from './proto/cosmos/base/v1beta1/coin'; import { MsgCancelBondResponse, MsgCreateBondResponse, MsgRefillBondResponse, MsgWithdrawBondResponse } from './proto/cerc/bond/v1/tx'; import { MsgOnboardParticipantResponse } from './proto/cerc/onboarding/v1/tx'; import { MsgSendResponse } from './proto/cosmos/bank/v1beta1/tx'; - -export const DEFAULT_CHAIN_ID = 'laconic_9000-1'; +import { DEFAULT_CHAIN_ID, DEFAULT_GAS_ESTIMATION_MULTIPLIER } from './constants'; /** * Create an auction bid. @@ -132,7 +131,7 @@ export class Registry { async setRecord ( { privateKey, record, bondId }: { privateKey: string, record: any, bondId: string }, transactionPrivateKey: string, - fee: StdFee + fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER ) { const account = new Account(Buffer.from(transactionPrivateKey, 'hex')); await account.init(); @@ -147,7 +146,7 @@ export class Registry { /** * Send coins. */ - async sendCoins ({ amount, denom, destinationAddress }: MessageMsgSendCoins, privateKey: string, fee: StdFee) { + async sendCoins ({ amount, denom, destinationAddress }: MessageMsgSendCoins, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER) { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -202,7 +201,7 @@ export class Registry { /** * Create bond. */ - async createBond ({ denom, amount }: MessageMsgCreateBond, privateKey: string, fee: StdFee): Promise { + async createBond ({ denom, amount }: MessageMsgCreateBond, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER): Promise { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -218,7 +217,7 @@ export class Registry { /** * Refill bond. */ - async refillBond ({ denom, amount, id }: MessageMsgRefillBond, privateKey: string, fee: StdFee): Promise { + async refillBond ({ denom, amount, id }: MessageMsgRefillBond, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER): Promise { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -235,7 +234,7 @@ export class Registry { /** * Withdraw (from) bond. */ - async withdrawBond ({ denom, amount, id }: MessageMsgWithdrawBond, privateKey: string, fee: StdFee): Promise { + async withdrawBond ({ denom, amount, id }: MessageMsgWithdrawBond, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER): Promise { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -252,7 +251,7 @@ export class Registry { /** * Cancel bond. */ - async cancelBond ({ id }: MessageMsgCancelBond, privateKey: string, fee: StdFee): Promise { + async cancelBond ({ id }: MessageMsgCancelBond, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER): Promise { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -267,7 +266,7 @@ export class Registry { /** * Associate record with bond. */ - async associateBond ({ bondId, recordId }: MessageMsgAssociateBond, privateKey: string, fee: StdFee) { + async associateBond ({ bondId, recordId }: MessageMsgAssociateBond, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER) { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -283,7 +282,7 @@ export class Registry { /** * Dissociate record from bond. */ - async dissociateBond ({ recordId }: MessageMsgDissociateBond, privateKey: string, fee: StdFee) { + async dissociateBond ({ recordId }: MessageMsgDissociateBond, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER) { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -298,7 +297,7 @@ export class Registry { /** * Dissociate all records from bond. */ - async dissociateRecords ({ bondId }: MessageMsgDissociateRecords, privateKey: string, fee: StdFee) { + async dissociateRecords ({ bondId }: MessageMsgDissociateRecords, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER) { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -313,7 +312,7 @@ export class Registry { /** * Reassociate records (switch bond). */ - async reassociateRecords ({ newBondId, oldBondId }: MessageMsgReAssociateRecords, privateKey: string, fee: StdFee) { + async reassociateRecords ({ newBondId, oldBondId }: MessageMsgReAssociateRecords, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER) { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -329,7 +328,7 @@ export class Registry { /** * Reserve authority. */ - async reserveAuthority ({ name, owner }: { name: string, owner?: string }, privateKey: string, fee: StdFee) { + async reserveAuthority ({ name, owner }: { name: string, owner?: string }, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER) { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -345,7 +344,7 @@ export class Registry { /** * Set authority bond. */ - async setAuthorityBond ({ bondId, name }: MessageMsgSetAuthorityBond, privateKey: string, fee: StdFee) { + async setAuthorityBond ({ bondId, name }: MessageMsgSetAuthorityBond, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER) { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -361,7 +360,7 @@ export class Registry { /** * Commit auction bid. */ - async commitBid ({ auctionId, commitHash }: MessageMsgCommitBid, privateKey: string, fee: StdFee) { + async commitBid ({ auctionId, commitHash }: MessageMsgCommitBid, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER) { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -377,7 +376,7 @@ export class Registry { /** * Reveal auction bid. */ - async revealBid ({ auctionId, reveal }: MessageMsgRevealBid, privateKey: string, fee: StdFee) { + async revealBid ({ auctionId, reveal }: MessageMsgRevealBid, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER) { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -413,7 +412,7 @@ export class Registry { /** * Set name (LRN) to record ID (CID). */ - async setName ({ cid, lrn }: MessageMsgSetName, privateKey: string, fee: StdFee) { + async setName ({ cid, lrn }: MessageMsgSetName, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER) { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -436,7 +435,7 @@ export class Registry { /** * Delete name (LRN) mapping. */ - async deleteName ({ lrn }: MessageMsgDeleteName, privateKey: string, fee: StdFee) { + async deleteName ({ lrn }: MessageMsgDeleteName, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER) { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -447,20 +446,10 @@ export class Registry { ); } - async getLaconicClient (account: Account) { - return LaconicClient.connectWithSigner( - this._endpoints.rpc, - account.wallet, - { - gasPrice: this._gasPrice - } - ); - } - /** * Onboard participant. */ - async onboardParticipant ({ ethPayload, ethSignature, role, kycId }: MessageMsgOnboardParticipant, privateKey: string, fee: StdFee): Promise { + async onboardParticipant ({ ethPayload, ethSignature, role, kycId }: MessageMsgOnboardParticipant, privateKey: string, fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER): Promise { const account = new Account(Buffer.from(privateKey, 'hex')); await account.init(); const laconicClient = await this.getLaconicClient(account); @@ -495,9 +484,18 @@ export class Registry { async getParticipantByNitroAddress (nitroAddress: string) { return this._client.getParticipantByNitroAddress(nitroAddress); } + + async getLaconicClient (account: Account) { + return LaconicClient.connectWithSigner( + this._endpoints.rpc, + account.wallet, + { gasPrice: this._gasPrice } + ); + } } export { Account }; export { LaconicClient }; +export * from './constants'; export * from './types/cerc/bond/message'; export * from './types/cerc/onboarding/message'; diff --git a/src/testing/helper.ts b/src/testing/helper.ts index 8c85652..ed8a1e5 100644 --- a/src/testing/helper.ts +++ b/src/testing/helper.ts @@ -2,7 +2,8 @@ import assert from 'assert'; import yaml from 'node-yaml'; import semver from 'semver'; -import { Account, DEFAULT_CHAIN_ID } from '../index'; +import { Account } from '../index'; +import { DEFAULT_CHAIN_ID } from '../constants'; export const ensureUpdatedConfig = async (path: string) => { const conf = await yaml.read(path);