Skip to content

Commit

Permalink
add: check if account has enough funds to send tx
Browse files Browse the repository at this point in the history
  • Loading branch information
pLabarta committed Jul 7, 2023
1 parent bfb690c commit 60b998c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 28 deletions.
4 changes: 3 additions & 1 deletion scripts/cc-cli/src/commands/distributeRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getCallerSeedFromEnvOrPrompt,
initKeyringPair,
} from "../utils/account";
import { signSendAndWatch } from "../utils/tx";
import { requireEnoughFundsToSend, signSendAndWatch } from "../utils/tx";
import {
parseAddresOrExit,
parseIntegerOrExit,
Expand Down Expand Up @@ -32,6 +32,8 @@ async function distributeRewardsAction(options: OptionValues) {
const callerSeed = await getCallerSeedFromEnvOrPrompt();
const distributeTx = api.tx.staking.payoutStakers(validator, era);

await requireEnoughFundsToSend(distributeTx, callerSeed, api);

const result = await signSendAndWatch(
distributeTx,
api,
Expand Down
22 changes: 3 additions & 19 deletions scripts/cc-cli/src/commands/send.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Command, OptionValues } from "commander";
import { ApiPromise, BN } from "creditcoin-js";
import { newApi } from "../api";
import {
getCallerSeedFromEnvOrPrompt,
initECDSAKeyringPairFromPK,
initKeyringPair,
} from "../utils/account";
import { getBalance } from "../utils/balance";
import { signSendAndWatch } from "../utils/tx";
import { requireEnoughFundsToSend, signSendAndWatch } from "../utils/tx";
import {
parseAddresOrExit,
parseAmountOrExit,
Expand Down Expand Up @@ -38,10 +36,10 @@ async function sendAction(options: OptionValues) {
? initECDSAKeyringPairFromPK(seed)
: initKeyringPair(seed);

await checkEnoughFundsToSend(caller.address, amount, api);

const tx = api.tx.balances.transfer(recipient, amount.toString());

await requireEnoughFundsToSend(tx, caller.address, api, amount);

const result = await signSendAndWatch(tx, api, caller);
console.log(result.info);

Expand All @@ -61,17 +59,3 @@ function parseOptions(options: OptionValues) {

return { amount, recipient, useEcdsa };
}

async function checkEnoughFundsToSend(
address: string,
amount: BN,
api: ApiPromise
) {
const balance = await getBalance(address, api);
if (balance.free.sub(balance.miscFrozen).lt(amount)) {
console.log(
`Caller ${address} has insufficient funds to send ${amount.toString()}`
);
process.exit(1);
}
}
5 changes: 4 additions & 1 deletion scripts/cc-cli/src/commands/setKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getControllerSeedFromEnvOrPrompt,
initKeyringPair,
} from "../utils/account";
import { signSendAndWatch } from "../utils/tx";
import { requireEnoughFundsToSend, signSendAndWatch } from "../utils/tx";
import { parseHexStringOrExit } from "../utils/parsing";

export function makeSetKeysCommand() {
Expand Down Expand Up @@ -42,6 +42,9 @@ async function setKeysAction(options: OptionValues) {
}

const tx = api.tx.session.setKeys(keys, "");

await requireEnoughFundsToSend(tx, controllerSeed, api);

const result = await signSendAndWatch(tx, api, controller);

console.log(result.info);
Expand Down
3 changes: 2 additions & 1 deletion scripts/cc-cli/src/commands/unbond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "../utils/account";
import { getBalance } from "../utils/balance";
import { getStatus, requireStatus } from "../utils/status";
import { signSendAndWatch } from "../utils/tx";
import { requireEnoughFundsToSend, signSendAndWatch } from "../utils/tx";
import { ApiPromise, BN } from "creditcoin-js";
import { promptContinue } from "../utils/promptContinue";
import { parseAmountOrExit, requiredInput } from "../utils/parsing";
Expand Down Expand Up @@ -44,6 +44,7 @@ async function unbondAction(options: OptionValues) {

// Unbond transaction
const tx = api.tx.staking.unbond(amount.toString());
await requireEnoughFundsToSend(tx, controllerAddress, api);

const result = await signSendAndWatch(tx, api, controllerKeyring);

Expand Down
5 changes: 4 additions & 1 deletion scripts/cc-cli/src/commands/withdrawUnbonded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getControllerSeedFromEnvOrPrompt,
initKeyringPair,
} from "../utils/account";
import { signSendAndWatch } from "../utils/tx";
import { requireEnoughFundsToSend, signSendAndWatch } from "../utils/tx";

export function makeWithdrawUnbondedCommand() {
const cmd = new Command("withdraw-unbonded");
Expand Down Expand Up @@ -43,6 +43,9 @@ async function withdrawUnbondedAction(options: OptionValues) {
? slashingSpans.unwrap().lastNonzeroSlash
: 0;
const withdrawUnbondTx = api.tx.staking.withdrawUnbonded(slashingSpansCount);

await requireEnoughFundsToSend(withdrawUnbondTx, controller.address, api);

const result = await signSendAndWatch(withdrawUnbondTx, api, controller);

console.log(result.info);
Expand Down
7 changes: 6 additions & 1 deletion scripts/cc-cli/src/commands/wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import { bond, checkRewardDestination } from "../utils/bond";
import { percentFromPerbill } from "../utils/perbill";
import { promptContinue, promptContinueOrSkip } from "../utils/promptContinue";
import { StakingPalletValidatorPrefs } from "../utils/validate";
import { TxStatus, signSendAndWatch } from "../utils/tx";
import {
TxStatus,
requireEnoughFundsToSend,
signSendAndWatch,
} from "../utils/tx";
import {
inputOrDefault,
parseAmountOrExit,
Expand Down Expand Up @@ -161,6 +165,7 @@ export function makeWizardCommand() {
const txs = [setKeysTx, validateTx];

const batchTx = api.tx.utility.batchAll(txs);
await requireEnoughFundsToSend(batchTx, controllerAddress, api);

const batchResult = await signSendAndWatch(batchTx, api, controllerKeyring);

Expand Down
4 changes: 3 additions & 1 deletion scripts/cc-cli/src/utils/bond.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiPromise, BN } from "creditcoin-js";
import { initKeyringPair } from "./account";
import { MICROUNITS_PER_CTC } from "./balance";
import { signSendAndWatch } from "./tx";
import { requireEnoughFundsToSend, signSendAndWatch } from "./tx";

export async function bond(
stashSeed: string,
Expand Down Expand Up @@ -31,6 +31,8 @@ export async function bond(

const stashKeyring = initKeyringPair(stashSeed);

await requireEnoughFundsToSend(bondTx, stashKeyring.address, api, amount);

const result = await signSendAndWatch(bondTx, api, stashKeyring);

return result;
Expand Down
4 changes: 2 additions & 2 deletions scripts/cc-cli/src/utils/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export async function calcualteBalanceAfterTx(
export async function requireEnoughFundsToSend(
tx: SubmittableExtrinsic<"promise", ISubmittableResult>,
address: string,
amount = new BN(0),
api: ApiPromise
api: ApiPromise,
amount = new BN(0)
) {
const balanceAfterSending = await calcualteBalanceAfterTx(
tx,
Expand Down
6 changes: 5 additions & 1 deletion scripts/cc-cli/src/utils/validate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiPromise } from "creditcoin-js";
import { initKeyringPair } from "./account";
import { signSendAndWatch } from "./tx";
import { requireEnoughFundsToSend, signSendAndWatch } from "./tx";

export interface StakingPalletValidatorPrefs {
// The validator's commission.
Expand All @@ -27,6 +27,8 @@ export async function validate(

const validateTx = api.tx.staking.validate(preferences);

await requireEnoughFundsToSend(validateTx, controller.address, api);

const result = await signSendAndWatch(validateTx, api, controller);

return result;
Expand All @@ -37,6 +39,8 @@ export async function chill(controllerSeed: string, api: ApiPromise) {

const chillTx = api.tx.staking.chill();

await requireEnoughFundsToSend(chillTx, account.address, api);

const result = await signSendAndWatch(chillTx, api, account);

return result;
Expand Down

0 comments on commit 60b998c

Please sign in to comment.