Skip to content

Commit

Permalink
fix: add timeout height option to pk broadcaster
Browse files Browse the repository at this point in the history
  • Loading branch information
billyjacoby committed Jan 8, 2025
1 parent 9935349 commit 43bd4c5
Showing 1 changed file with 28 additions and 4 deletions.
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()
}

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

0 comments on commit 43bd4c5

Please sign in to comment.