Skip to content

Commit

Permalink
add: util to calc balance after sending a tx
Browse files Browse the repository at this point in the history
  • Loading branch information
pLabarta committed Jul 7, 2023
1 parent 252fcf8 commit bfb690c
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion scripts/cc-cli/src/utils/tx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ISubmittableResult } from "@polkadot/types/types";
import { ApiPromise, KeyringPair } from "creditcoin-js";
import { ApiPromise, BN, KeyringPair } from "creditcoin-js";

import { SubmittableExtrinsic } from "@polkadot/api/types";
import { getBalance } from "./balance";

export async function signSendAndWatch(
tx: SubmittableExtrinsic<"promise", ISubmittableResult>,
Expand Down Expand Up @@ -74,3 +75,44 @@ export interface TxResult {
status: TxStatus;
info: string;
}

export async function getTxFee(
tx: SubmittableExtrinsic<"promise", ISubmittableResult>,
callerAddress: string
): Promise<BN> {
const fee = await tx.paymentInfo(callerAddress);
return fee.partialFee.toBn();
}

export async function calcualteBalanceAfterTx(
tx: SubmittableExtrinsic<"promise", ISubmittableResult>,
callerAddress: string,
amount: BN,
api: ApiPromise
): Promise<BN> {
const balance = await getBalance(callerAddress, api);
const fee = await getTxFee(tx, callerAddress);
const availableBalance = balance.free.sub(balance.miscFrozen);
const balanceAfterSending = availableBalance.sub(amount).sub(fee);
return balanceAfterSending;
}

export async function requireEnoughFundsToSend(
tx: SubmittableExtrinsic<"promise", ISubmittableResult>,
address: string,
amount = new BN(0),
api: ApiPromise
) {
const balanceAfterSending = await calcualteBalanceAfterTx(
tx,
address,
amount,
api
);
if (balanceAfterSending.lt(new BN(1))) {
console.error(
`Caller ${address} has insufficient funds to send the transaction and maintain existential deposit; transaction cancelled.`
);
process.exit(1);
}
}

0 comments on commit bfb690c

Please sign in to comment.