-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(tx-construction): throw when deregistering reward accounts with … #1547
Merged
szymonmaslowski
merged 1 commit into
master
from
fix/lw-11898-unstake-account-with-rewards-but-no-vote-delegation
Jan 2, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 14 additions & 0 deletions
14
packages/tx-construction/src/tx-builder/ensureNoDeRegistrationsWithRewardsLocked.ts
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,14 @@ | ||
import { DeRegistrationsWithRewardsLocked } from './types'; | ||
import { hasCorrectVoteDelegation } from './hasCorrectVoteDelegation'; | ||
import type { RewardAccountWithPoolId } from '../types'; | ||
|
||
export const ensureNoDeRegistrationsWithRewardsLocked = (rewardAccountsToBeDeRegistered: RewardAccountWithPoolId[]) => { | ||
const rewardAccountsWithLockedRewards = rewardAccountsToBeDeRegistered.filter( | ||
(rewardAccountWithPoolId) => | ||
rewardAccountWithPoolId.rewardBalance > 0n && !hasCorrectVoteDelegation(rewardAccountWithPoolId) | ||
); | ||
|
||
if (rewardAccountsWithLockedRewards.length > 0) { | ||
throw new DeRegistrationsWithRewardsLocked(rewardAccountsWithLockedRewards); | ||
} | ||
}; |
9 changes: 9 additions & 0 deletions
9
packages/tx-construction/src/tx-builder/hasCorrectVoteDelegation.ts
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,9 @@ | ||
import { Cardano } from '@cardano-sdk/core'; | ||
import { RewardAccountWithPoolId } from '../types'; | ||
|
||
export const hasCorrectVoteDelegation = ({ | ||
dRepDelegatee | ||
}: Pick<RewardAccountWithPoolId, 'dRepDelegatee'>): boolean => { | ||
const drep = dRepDelegatee?.delegateRepresentative; | ||
return !!drep && (!Cardano.isDrepInfo(drep) || drep.active); | ||
}; |
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
/* eslint-disable sonarjs/no-duplicate-string */ | ||
import * as Crypto from '@cardano-sdk/crypto'; | ||
import { AddressType, Bip32Account, GroupedAddress, InMemoryKeyAgent, util } from '@cardano-sdk/key-management'; | ||
import { Cardano, setInConwayEra } from '@cardano-sdk/core'; | ||
import { Cardano, DRepInfo, setInConwayEra } from '@cardano-sdk/core'; | ||
import { | ||
DeRegistrationsWithRewardsLocked, | ||
GenericTxBuilder, | ||
OutOfSyncRewardAccounts, | ||
OutputValidation, | ||
|
@@ -40,12 +41,14 @@ const inputResolver: Cardano.InputResolver = { | |
|
||
/** Utility factory for tests to create a GenericTxBuilder with mocked dependencies */ | ||
const createTxBuilder = async ({ | ||
adjustRewardAccount = (r) => r, | ||
stakeDelegations, | ||
numAddresses = stakeDelegations.length, | ||
useMultiplePaymentKeys = false, | ||
rewardAccounts, | ||
keyAgent | ||
}: { | ||
adjustRewardAccount?: (rewardAccountWithPoolId: RewardAccountWithPoolId, index: number) => RewardAccountWithPoolId; | ||
stakeDelegations: { | ||
credentialStatus: Cardano.StakeCredentialStatus; | ||
poolId?: Cardano.PoolId; | ||
|
@@ -88,13 +91,21 @@ const createTxBuilder = async ({ | |
// This would normally be done by the wallet.delegation.rewardAccounts | ||
.map<RewardAccountWithPoolId>(({ rewardAccount: address }, index) => { | ||
const { credentialStatus, poolId, deposit } = stakeDelegations[index] ?? {}; | ||
return { | ||
address, | ||
credentialStatus: credentialStatus ?? Cardano.StakeCredentialStatus.Unregistered, | ||
rewardBalance: mocks.rewardAccountBalance, | ||
...(poolId ? { delegatee: { nextNextEpoch: { id: poolId } } } : undefined), | ||
...(deposit && { deposit }) | ||
}; | ||
return adjustRewardAccount( | ||
{ | ||
address, | ||
credentialStatus: credentialStatus ?? Cardano.StakeCredentialStatus.Unregistered, | ||
dRepDelegatee: { | ||
delegateRepresentative: { | ||
__typename: 'AlwaysAbstain' | ||
} | ||
}, | ||
rewardBalance: mocks.rewardAccountBalance, | ||
...(poolId ? { delegatee: { nextNextEpoch: { id: poolId } } } : undefined), | ||
...(deposit && { deposit }) | ||
}, | ||
index | ||
); | ||
}) | ||
) | ||
), | ||
|
@@ -853,4 +864,131 @@ describe('TxBuilder/delegatePortfolio', () => { | |
expect(roundRobinRandomImprove).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
// eslint-disable-next-line sonarjs/cognitive-complexity | ||
describe('DRep delegation', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very well written tests. Great coverage and easy to ready ❤️ 👏 |
||
const dRepInfo: DRepInfo = { | ||
active: true, | ||
amount: 9586n, | ||
hasScript: false, | ||
id: Cardano.DRepID('drep15cfxz9exyn5rx0807zvxfrvslrjqfchrd4d47kv9e0f46uedqtc') | ||
}; | ||
|
||
const dRepDelegateeOptions = { | ||
abstain: { __typename: 'AlwaysAbstain' }, | ||
activeDrep: dRepInfo, | ||
inactiveDrep: { ...dRepInfo, active: false }, | ||
noConfidence: { __typename: 'AlwaysNoConfidence' }, | ||
none: undefined | ||
} as const; | ||
|
||
const createTxBuilderWithRewardBalance = | ||
(rewardBalance: bigint) => | ||
(...delegateRepresentatives: (Cardano.DRepDelegatee['delegateRepresentative'] | undefined)[]) => | ||
createTxBuilder({ | ||
adjustRewardAccount: (rewardAccountWithPoolId, index) => { | ||
const delegateRepresentative = delegateRepresentatives[index]; | ||
return { | ||
...rewardAccountWithPoolId, | ||
dRepDelegatee: delegateRepresentative ? { delegateRepresentative } : undefined, | ||
rewardBalance: rewardBalance ?? rewardAccountWithPoolId.rewardBalance | ||
}; | ||
}, | ||
keyAgent, | ||
numAddresses: delegateRepresentatives.length, | ||
stakeDelegations: [] | ||
}); | ||
|
||
describe('when reward accounts have no balance', () => { | ||
it('can deregister all reward accounts no matter the drep delegation', async () => { | ||
const drepValues = Object.values(dRepDelegateeOptions); | ||
const createTxBuilderWithDreps = createTxBuilderWithRewardBalance(0n); | ||
const txBuilderFactory = await createTxBuilderWithDreps(...drepValues); | ||
|
||
await expect(txBuilderFactory.txBuilder.delegatePortfolio(null).build().inspect()).resolves.not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('when deregistered reward account has balance', () => { | ||
const createTxBuilderWithDreps = createTxBuilderWithRewardBalance(7365n); | ||
|
||
it('deregisters the key when it delegates to abstain', async () => { | ||
const txBuilderFactory = await createTxBuilderWithDreps(dRepDelegateeOptions.abstain); | ||
await expect(txBuilderFactory.txBuilder.delegatePortfolio(null).build().inspect()).resolves.not.toThrow(); | ||
}); | ||
|
||
it('deregisters the key when it delegates to no confidence', async () => { | ||
const txBuilderFactory = await createTxBuilderWithDreps(dRepDelegateeOptions.noConfidence); | ||
await expect(txBuilderFactory.txBuilder.delegatePortfolio(null).build().inspect()).resolves.not.toThrow(); | ||
}); | ||
|
||
it('deregisters the key when it delegates to active drep', async () => { | ||
const txBuilderFactory = await createTxBuilderWithDreps(dRepDelegateeOptions.activeDrep); | ||
await expect(txBuilderFactory.txBuilder.delegatePortfolio(null).build().inspect()).resolves.not.toThrow(); | ||
}); | ||
|
||
it('throws and DeRegistrationsWithRewardsLocked when it delegates to inactive drep', async () => { | ||
const txBuilderFactory = await createTxBuilderWithDreps(dRepDelegateeOptions.inactiveDrep); | ||
const [{ address: lockedRewardAccount }] = await txBuilderFactory.txBuilderProviders.rewardAccounts(); | ||
|
||
try { | ||
await txBuilderFactory.txBuilder.delegatePortfolio(null).build().inspect(); | ||
throw new Error('TxBuilder supposed to throw an DeRegistrationsWithRewardsLocked error'); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(DeRegistrationsWithRewardsLocked); | ||
expect((error as DeRegistrationsWithRewardsLocked).keysWithLockedRewards[0]).toEqual({ | ||
cbor: Cardano.Address.fromString(lockedRewardAccount)?.toBytes(), | ||
key: lockedRewardAccount | ||
}); | ||
} | ||
}); | ||
|
||
it('throws an DeRegistrationsWithRewardsLocked when it has no drep delegation', async () => { | ||
const txBuilderFactory = await createTxBuilderWithDreps(dRepDelegateeOptions.none); | ||
const [{ address: lockedRewardAccount }] = await txBuilderFactory.txBuilderProviders.rewardAccounts(); | ||
|
||
try { | ||
await txBuilderFactory.txBuilder.delegatePortfolio(null).build().inspect(); | ||
throw new Error('TxBuilder supposed to throw an DeRegistrationsWithRewardsLocked error'); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(DeRegistrationsWithRewardsLocked); | ||
expect((error as DeRegistrationsWithRewardsLocked).keysWithLockedRewards[0]).toEqual({ | ||
cbor: Cardano.Address.fromString(lockedRewardAccount)?.toBytes(), | ||
key: lockedRewardAccount | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
it('attaches data of all locked reward accounts to the DeRegistrationsWithRewardsLocked error', async () => { | ||
const createTxBuilderWithDreps = createTxBuilderWithRewardBalance(7365n); | ||
const txBuilderFactory = await createTxBuilderWithDreps( | ||
dRepDelegateeOptions.none, | ||
dRepDelegateeOptions.activeDrep, | ||
dRepDelegateeOptions.inactiveDrep | ||
); | ||
const allrewardAccountsData = await txBuilderFactory.txBuilderProviders.rewardAccounts(); | ||
const lockedRewardAccountsData = allrewardAccountsData | ||
// TXBuilder reverses them so doing the same here so it is more convenient here to test | ||
.reverse() | ||
// At the index 1 there is an activeDrep allowing to withdraw during de-registering | ||
.filter((_, index) => index !== 1) | ||
.map((r) => ({ | ||
cbor: Cardano.Address.fromString(r.address)?.toBytes(), | ||
key: r.address | ||
})); | ||
|
||
try { | ||
await txBuilderFactory.txBuilder.delegatePortfolio(null).build().inspect(); | ||
throw new Error('TxBuilder supposed to throw an DeRegistrationsWithRewardsLocked error'); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(DeRegistrationsWithRewardsLocked); | ||
for (const [index, keyWithLockedRewards] of ( | ||
error as DeRegistrationsWithRewardsLocked | ||
).keysWithLockedRewards.entries()) { | ||
expect(keyWithLockedRewards).toEqual(lockedRewardAccountsData[index]); | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the nitpicking. Could you reduce this type too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not reduce it here. This function uses
hasCorrectVoteDelegation
function which has that type already reduced so reducing it here would create an implicit relation between them as theensureNoDeRegistrationsWithRewardsLocked
would need to pickdRepDelegatee
despite not using it