-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1134 from gluwa/update-testnet-2.222.1
Update testnet branch
- Loading branch information
Showing
27 changed files
with
692 additions
and
92 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Command, OptionValues } from "commander"; | ||
import { newApi } from "../api"; | ||
import { getSeedFromOptions, initKeyringPair } from "../utils/account"; | ||
import { promptContinue } from "../utils/promptContinue"; | ||
import { Balance, getBalance, toMicrounits } from "../utils/balance"; | ||
import { bondExtra } from "../utils/bondExtra"; | ||
import { BN } from "creditcoin-js"; | ||
|
||
export function makeBondExtraCommand() { | ||
const cmd = new Command("bond-extra"); | ||
cmd.description("Add CTC to an existing bond from a Stash account"); | ||
cmd.option("-a, --amount [amount]", "Amount to bond"); | ||
cmd.option("-s, --seed [seed phrase]", "Specify seed phrase to bond from"); | ||
cmd.option( | ||
"-f, --file [file-name]", | ||
"Specify file with seed phrase to bond from" | ||
); | ||
cmd.action(bondExtraAction); | ||
return cmd; | ||
} | ||
|
||
async function bondExtraAction(options: OptionValues) { | ||
// If no amount error and exit | ||
if (!options.amount || !parseInt(options.amount, 10)) { | ||
console.log("Must specify amount to bond"); | ||
process.exit(1); | ||
} | ||
|
||
const { api } = await newApi(options.url); | ||
|
||
const stashSeed = getSeedFromOptions(options); | ||
|
||
// Check balance | ||
const stashKeyring = initKeyringPair(stashSeed); | ||
const stashAddress = stashKeyring.address; | ||
const balance = await getBalance(stashAddress, api); | ||
const amount = parseInt(options.amount, 10); | ||
checkBalanceAgainstBondAmount(balance, amount); | ||
|
||
const alreadyBonded = checkIfAlreadyBonded(stashAddress, balance); | ||
if (!alreadyBonded) { | ||
console.log("Must already be bonded to use bond-extra, use bond instead"); | ||
process.exit(1); | ||
} | ||
|
||
console.log("Creating bond extra transaction..."); | ||
console.log("Amount:", parseInt(options.amount, 10)); | ||
|
||
await promptContinue(); | ||
|
||
const bondTxHash = await bondExtra(stashSeed, options.amount, api); | ||
|
||
console.log("Bond transaction sent with hash:", bondTxHash); | ||
process.exit(0); | ||
} | ||
|
||
function checkBalanceAgainstBondAmount(balance: Balance, amount: number) { | ||
if (balance.free.sub(balance.miscFrozen).lt(toMicrounits(amount))) { | ||
throw new Error( | ||
`Insufficient funds to bond ${toMicrounits(amount).toString()}` | ||
); | ||
} | ||
} | ||
|
||
function checkIfAlreadyBonded(address: string, balance: Balance) { | ||
if (balance.miscFrozen.gt(new BN(0))) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} |
Oops, something went wrong.