From 1a1d4901aa453757ff2c43137d2a779a6addca71 Mon Sep 17 00:00:00 2001 From: Valtteri Karesto Date: Tue, 24 May 2022 15:26:38 +0300 Subject: [PATCH] Add estimate fee function --- utils/estimateFee.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 utils/estimateFee.ts diff --git a/utils/estimateFee.ts b/utils/estimateFee.ts new file mode 100644 index 0000000..8d66646 --- /dev/null +++ b/utils/estimateFee.ts @@ -0,0 +1,20 @@ +import { sign, XRPL_Account } from "xrpl-accountlib" +import state from "../state" + +// Mutate tx object with network estimated fee value +const estimateFee = async (tx: Record, keypair: XRPL_Account): Promise => { + const copyTx = JSON.parse(JSON.stringify(tx)) + delete copyTx['SigningPubKey'] + const { signedTransaction } = sign(copyTx, keypair); + try { + const res = await state.client?.send({ command: 'fee', tx_blob: signedTransaction }) + if (res && res.drops) { + return tx['Fee'] = res.drops.base_fee; + } + return null + } catch (err) { + throw Error('Cannot estimate fee') + } +} + +export default estimateFee \ No newline at end of file