Skip to content
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

fix: add timeout height option to pk broadcaster #534

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
32 changes: 28 additions & 4 deletions packages/sdk-ts/src/core/tx/broadcaster/MsgBroadcasterWithPk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ interface MsgBroadcasterWithPkOptions {
useRest?: boolean
txTimeout?: number // blocks to wait for tx to be included in a block
gasBufferCoefficient?: number
txTimeoutOnFeeDelegation?: boolean
}

/**
Expand All @@ -83,6 +84,8 @@ export class MsgBroadcasterWithPk {

public simulateTx: boolean = false

public txTimeoutOnFeeDelegation: boolean = false

public useRest: boolean = false

public gasBufferCoefficient: number = 1.1
Expand All @@ -106,6 +109,8 @@ export class MsgBroadcasterWithPk {
options.privateKey instanceof PrivateKey
? options.privateKey
: PrivateKey.fromHex(options.privateKey)
this.txTimeoutOnFeeDelegation =
options.txTimeoutOnFeeDelegation || this.txTimeoutOnFeeDelegation
}

/**
Expand Down Expand Up @@ -141,7 +146,14 @@ export class MsgBroadcasterWithPk {
* @returns {string} transaction hash
*/
async broadcastWithFeeDelegation(transaction: MsgBroadcasterTxOptions) {
const { simulateTx, privateKey, ethereumChainId, endpoints } = this
const {
simulateTx,
privateKey,
ethereumChainId,
endpoints,
txTimeoutOnFeeDelegation,
txTimeout,
} = this

const ethereumWallet = this.privateKey.toHex()

Expand All @@ -167,6 +179,19 @@ export class MsgBroadcasterWithPk {
throw new GeneralException(new Error('Please provide ethereumChainId'))
}

let timeoutHeight = undefined

if (txTimeoutOnFeeDelegation) {
const latestBlock = await new ChainGrpcTendermintApi(
endpoints.grpc,
).fetchLatestBlock()
const latestHeight = latestBlock!.header!.height

timeoutHeight = new BigNumberInBase(latestHeight)
.plus(txTimeout)
.toNumber()
}
Comment on lines +184 to +193
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add null checks for latestBlock response.

The code assumes latestBlock and header are non-null through the use of the non-null assertion operator (!). Consider adding explicit null checks to handle potential API failures gracefully.

-      const latestBlock = await new ChainGrpcTendermintApi(
-        endpoints.grpc,
-      ).fetchLatestBlock()
-      const latestHeight = latestBlock!.header!.height
+      const latestBlock = await new ChainGrpcTendermintApi(
+        endpoints.grpc,
+      ).fetchLatestBlock()
+      if (!latestBlock?.header?.height) {
+        throw new GeneralException(
+          new Error('Failed to fetch latest block height'),
+        )
+      }
+      const latestHeight = latestBlock.header.height
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (txTimeoutOnFeeDelegation) {
const latestBlock = await new ChainGrpcTendermintApi(
endpoints.grpc,
).fetchLatestBlock()
const latestHeight = latestBlock!.header!.height
timeoutHeight = new BigNumberInBase(latestHeight)
.plus(txTimeout)
.toNumber()
}
if (txTimeoutOnFeeDelegation) {
const latestBlock = await new ChainGrpcTendermintApi(
endpoints.grpc,
).fetchLatestBlock()
if (!latestBlock?.header?.height) {
throw new GeneralException(
new Error('Failed to fetch latest block height'),
)
}
const latestHeight = latestBlock.header.height
timeoutHeight = new BigNumberInBase(latestHeight)
.plus(txTimeout)
.toNumber()
}


const transactionApi = new IndexerGrpcWeb3GwApi(endpoints.indexer)
const txResponse = await transactionApi.prepareTxRequest({
memo: tx.memo,
Expand All @@ -175,6 +200,7 @@ export class MsgBroadcasterWithPk {
chainId: ethereumChainId,
gasLimit: getGasPriceBasedOnMessage(msgs),
estimateGas: simulateTx || false,
timeoutHeight,
})

const signature = await privateKey.signTypedData(
Expand Down Expand Up @@ -404,9 +430,7 @@ export class MsgBroadcasterWithPk {
).fetchLatestBlock()
const latestHeight = latestBlock!.header!.height

return new BigNumberInBase(latestHeight).plus(
txTimeout,
)
return new BigNumberInBase(latestHeight).plus(txTimeout)
}

const latestBlock = await new ChainGrpcTendermintApi(
Expand Down