From fa64e725c021c25456976b07ef15ec8ac13a6c98 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 9ed43c9..0efd22f 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); @@ -209,7 +208,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); @@ -225,7 +224,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); @@ -242,7 +241,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); @@ -259,7 +258,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); @@ -274,7 +273,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); @@ -290,7 +289,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); @@ -305,7 +304,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); @@ -320,7 +319,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); @@ -336,7 +335,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); @@ -352,7 +351,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); @@ -368,7 +367,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); @@ -384,7 +383,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); @@ -420,7 +419,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); @@ -443,7 +442,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); @@ -454,20 +453,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); @@ -502,9 +491,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);