Skip to content

Commit

Permalink
refactor(prepareTxParams): inline calculateTtl
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `calculateTtl` not exported anymore
Use a general `buildTx` method instead.
  • Loading branch information
davidyuk committed Jun 15, 2022
1 parent 0095455 commit 2f03793
Showing 1 changed file with 13 additions and 33 deletions.
46 changes: 13 additions & 33 deletions src/tx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,27 +142,6 @@ export async function getVmVersion (
return { vmVersion, abiVersion }
}

/**
* Compute the absolute ttl by adding the ttl to the current height of the chain
*
* @param ttl
* @param relative - ttl is absolute or relative(default: true(relative))
* @returns Absolute Ttl
*/
export async function calculateTtl (
{ ttl = TX_TTL, relative = true, onNode }:
{ ttl?: number, relative?: boolean, onNode: Node }
): Promise<number> {
if (ttl === 0) return 0
if (ttl < 0) throw new ArgumentError('ttl', 'greater or equal to 0', ttl)

if (relative) {
const { height } = await onNode.getCurrentKeyBlock()
return +(height) + ttl
}
return ttl
}

/**
* Calculate fee, get absolute ttl (ttl + height), get account nonce
*
Expand All @@ -174,8 +153,8 @@ export async function prepareTxParams (
txType: TX_TYPE,
{
senderId,
nonce: n,
ttl: t,
nonce,
ttl = TX_TTL,
fee: f,
gasLimit,
absoluteTtl,
Expand All @@ -187,28 +166,29 @@ export async function prepareTxParams (
senderId: EncodedData<'ak'>
vsn?: number
gasLimit?: number | string | BigNumber
absoluteTtl?: number
absoluteTtl?: boolean
strategy?: 'continuity' | 'max'
showWarning?: boolean
onNode: Node
}
): Promise<{
fee: number | string | BigNumber
ttl: number
nonce: number | string | BigNumber
nonce: number
}> {
n = n ?? (
nonce ??= (
await onNode.getAccountNextNonce(senderId, { strategy }).catch(() => ({ nextNonce: 1 }))
).nextNonce
const ttl = await calculateTtl({
ttl: t as number,
relative: absoluteTtl == null,
onNode
})

if (ttl !== 0) {
if (ttl < 0) throw new ArgumentError('ttl', 'greater or equal to 0', ttl)
ttl += absoluteTtl === true ? 0 : (await onNode.getCurrentKeyBlock()).height
}

const fee = calculateFee(
f,
txType,
{ showWarning, gasLimit, params: { ...arguments[1], nonce: n, ttl }, vsn }
{ showWarning, gasLimit, params: { ...arguments[1], nonce, ttl }, vsn }
)
return { fee, ttl, nonce: n }
return { fee, ttl, nonce }
}

0 comments on commit 2f03793

Please sign in to comment.