-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add faucet for Swanky node (#60)
Co-authored-by: Igor Papandinas <igor.papandinas@posteo.net>
- Loading branch information
1 parent
cd93607
commit 0914aed
Showing
11 changed files
with
199 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Args } from "@oclif/core"; | ||
import { ApiPromise } from "@polkadot/api"; | ||
import type { AccountInfo, Balance as BalanceType } from "@polkadot/types/interfaces"; | ||
import { ChainApi, resolveNetworkUrl } from "../../lib/index.js"; | ||
import { SwankyCommand } from "../../lib/swankyCommand.js"; | ||
import { InputError } from "../../lib/errors.js"; | ||
import { formatBalance } from "@polkadot/util"; | ||
|
||
export class Balance extends SwankyCommand<typeof Balance> { | ||
static description = "Balance of an account"; | ||
|
||
static args = { | ||
alias: Args.string({ | ||
name: "alias", | ||
description: "Alias of account to be used", | ||
}), | ||
}; | ||
async run(): Promise<void> { | ||
const { args } = await this.parse(Balance); | ||
|
||
if (!args.alias) { | ||
throw new InputError( | ||
"Missing argument! Please provide an alias account to get the balance from. Example usage: `swanky account balance <YourAliasAccount>`" | ||
); | ||
} | ||
|
||
const accountData = this.findAccountByAlias(args.alias); | ||
const networkUrl = resolveNetworkUrl(this.swankyConfig, ""); | ||
|
||
const api = (await this.spinner.runCommand(async () => { | ||
const api = await ChainApi.create(networkUrl); | ||
await api.start(); | ||
return api.apiInst; | ||
}, "Connecting to node")) as ApiPromise; | ||
|
||
const decimals = api.registry.chainDecimals[0]; | ||
formatBalance.setDefaults({ unit: "UNIT", decimals }); | ||
|
||
const { nonce, data: balance } = await api.query.system.account<AccountInfo>( | ||
accountData.address | ||
); | ||
const { free, reserved, miscFrozen, feeFrozen } = balance; | ||
|
||
let frozen: BalanceType; | ||
if (feeFrozen.gt(miscFrozen)) { | ||
frozen = feeFrozen; | ||
} else { | ||
frozen = miscFrozen; | ||
} | ||
|
||
const transferrableBalance = free.sub(frozen); | ||
const totalBalance = free.add(reserved); | ||
|
||
console.log("Transferrable Balance:", formatBalance(transferrableBalance)); | ||
if (!transferrableBalance.eq(totalBalance)) { | ||
console.log("Total Balance:", formatBalance(totalBalance)); | ||
console.log("Raw Balances:", balance.toHuman()); | ||
} | ||
console.log("Account Nonce:", nonce.toHuman()); | ||
|
||
await api.disconnect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Args } from "@oclif/core"; | ||
import { SwankyAccountCommand } from "./swankyAccountCommands.js"; | ||
|
||
export class Faucet extends SwankyAccountCommand<typeof Faucet> { | ||
static description = "Transfer some tokens from faucet to an account"; | ||
|
||
static aliases = [`account:faucet`]; | ||
|
||
static args = { | ||
alias: Args.string({ | ||
name: "alias", | ||
required: true, | ||
description: "Alias of account to be used", | ||
}), | ||
}; | ||
|
||
async run(): Promise<void> { | ||
const { args } = await this.parse(Faucet); | ||
|
||
const accountData = this.findAccountByAlias(args.alias); | ||
await this.performFaucetTransfer(accountData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Command } from "@oclif/core"; | ||
import chalk from "chalk"; | ||
import { AccountData, ChainApi, resolveNetworkUrl } from "../../index.js"; | ||
import { LOCAL_FAUCET_AMOUNT } from "../../lib/consts.js"; | ||
import { SwankyCommand } from "../../lib/swankyCommand.js"; | ||
import { ApiError } from "../../lib/errors.js"; | ||
|
||
export abstract class SwankyAccountCommand<T extends typeof Command> extends SwankyCommand<T> { | ||
async performFaucetTransfer(accountData: AccountData, canBeSkipped = false) { | ||
let api: ChainApi | null = null; | ||
try { | ||
api = (await this.spinner.runCommand(async () => { | ||
const networkUrl = resolveNetworkUrl(this.swankyConfig, ""); | ||
const api = await ChainApi.create(networkUrl); | ||
await api.start(); | ||
return api; | ||
}, "Connecting to node")) as ChainApi; | ||
|
||
if (api) | ||
await this.spinner.runCommand( | ||
async () => { | ||
if (api) await api.faucet(accountData); | ||
}, | ||
`Transferring ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`, | ||
`Transferred ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`, | ||
`Failed to transfer ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`, | ||
true | ||
); | ||
} catch (cause) { | ||
if (cause instanceof Error) { | ||
if (cause.message.includes('ECONNREFUSED') && canBeSkipped) { | ||
this.warn(`Unable to connect to the node. Skipping faucet transfer for ${chalk.yellowBright(accountData.alias)}.`); | ||
} else { | ||
throw new ApiError("Error transferring tokens from faucet account", { cause }); | ||
} | ||
} else { | ||
throw new ApiError("An unknown error occurred during faucet transfer", { cause: new Error(String(cause)) }); | ||
} | ||
} finally { | ||
if (api) { | ||
await api.disconnect(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters