Skip to content

Commit

Permalink
[cli] fixing show-bond to not showing negative mod
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed Jul 9, 2024
1 parent d7c6558 commit d09efa4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
48 changes: 37 additions & 11 deletions packages/validator-bonds-cli/__tests__/test-validator/show.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getVoteAccountFromData,
signerWithPubkey,
transaction,
waitForNextEpoch,
} from '@marinade.finance/web3js-common'
import {
Connection,
Expand All @@ -34,6 +35,7 @@ import {
import { AnchorExtendedProvider } from '@marinade.finance/anchor-common'
import { VoteAccountShow } from '../../src/commands/show'
import BN from 'bn.js'
import { claimWithdrawRequestInstruction } from '@marinade.finance/validator-bonds-sdk/src/instructions/claimWithdrawRequest'

beforeAll(() => {
shellMatchers()
Expand Down Expand Up @@ -508,9 +510,10 @@ describe('Show command using CLI', () => {
const stakeAccountLamports: number[] = [3, 10, 23].map(
l => l * LAMPORTS_PER_SOL
)
let lastStakeAccount: PublicKey
const sumLamports = stakeAccountLamports.reduce((a, b) => a + b, 0)
for (const lamports of stakeAccountLamports) {
await createBondsFundedStakeAccount({
lastStakeAccount = await createBondsFundedStakeAccount({
program,
provider,
configAccount,
Expand Down Expand Up @@ -681,14 +684,33 @@ describe('Show command using CLI', () => {
const { div: requestedDiv, mod: requestedMod } = new BN(U64_MAX).divmod(
bnLamportsPerSol
)
await executeInitWithdrawRequestInstruction({
program,
provider,
configAccount,
bondAccount,
validatorIdentity,
amount: U64_MAX,
})
const withdrawingAmount =
stakeAccountLamports[stakeAccountLamports.length - 1]
const { div: withdrawingDiv, mod: withdrawingMod } = new BN(
withdrawingAmount
).divmod(bnLamportsPerSol)
const { div: toWithdrawDiv, mod: toWithdrawMod } = new BN(U64_MAX)
.sub(new BN(withdrawingAmount))
.divmod(bnLamportsPerSol)
const { withdrawRequestAccount: toWithdrawRequestAcc } =
await executeInitWithdrawRequestInstruction({
program,
provider,
configAccount,
bondAccount,
validatorIdentity,
amount: U64_MAX,
})
await waitForNextEpoch(provider.connection, 15)
const { instruction, splitStakeAccount } =
await claimWithdrawRequestInstruction({
program,
authority: bondAuthority,
withdrawRequestAccount: toWithdrawRequestAcc,
bondAccount,
stakeAccount: lastStakeAccount!,
})
provider.sendIx([bondAuthority, splitStakeAccount], instruction)
await (
expect([
'pnpm',
Expand All @@ -714,11 +736,13 @@ describe('Show command using CLI', () => {
stdout: YAML.stringify({
...expectedData,
amountActive: `${activeDiv.toString()}.${activeMod
.muln(-1)
.toString()
.padStart(9, '0')} SOLs`,
amountToWithdraw: `${requestedDiv.toString()}.${requestedMod
amountToWithdraw: `${toWithdrawDiv.toString()}.${toWithdrawMod
.toString()
.padStart(9, '0')} SOLs`,
numberActiveStakeAccounts: stakeAccountLamports.length - 1,
withdrawRequest: {
publicKey: withdrawRequestAccount.toBase58(),
account: {
Expand All @@ -728,7 +752,9 @@ describe('Show command using CLI', () => {
requestedAmount: `${requestedDiv.toString()}.${requestedMod
.toString()
.padStart(9, '0')} SOLs`,
withdrawnAmount: '0.000000000 SOL',
withdrawnAmount: `${withdrawingDiv.toString()}.${withdrawingMod
.toString()
.padStart(9, '0')} SOLs`,
},
},
}),
Expand Down
1 change: 1 addition & 0 deletions packages/validator-bonds-cli/src/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ function formatLamportsToSol(value: BN | number | BigInt): string {
value = new BN(value.toString())
const { div, mod } = new BN(value).divmod(new BN(LAMPORTS_PER_SOL))
return `${div.toString()}.${mod
.abs()
.toString()
.padStart(Math.log10(LAMPORTS_PER_SOL), '0')}`
}
Expand Down

0 comments on commit d09efa4

Please sign in to comment.