Skip to content

Commit

Permalink
fix(wallet): delegation changes reflect at end of cuurrent epoch + 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mkazlauskas committed Nov 22, 2021
1 parent cf2e5aa commit 1c0d275
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
Expand Up @@ -124,7 +124,7 @@ type TransactionsCertificates = ObservableType<ReturnType<typeof accountCertific

export const getStakePoolIdAtEpoch = (transactions: TransactionsCertificates) => (atEpoch: Cardano.Epoch) => {
const certificatesUpToEpoch = transactions
.filter(({ epoch }) => epoch <= atEpoch - 2)
.filter(({ epoch }) => epoch < atEpoch - 2)
.map(({ certificates }) => certificates);
if (!isLastStakeKeyCertOfType(certificatesUpToEpoch, Cardano.CertificateType.StakeKeyRegistration)) return;
const delegationTxCertificates = findLast(certificatesUpToEpoch, (certs) =>
Expand All @@ -141,7 +141,7 @@ export const createDelegateeTracker = (
): Observable<Delegatee | undefined> =>
combineLatest([certificates$, epoch$]).pipe(
switchMap(([transactions, lastEpoch]) => {
const stakePoolIds = [lastEpoch, lastEpoch + 1, lastEpoch + 2].map(getStakePoolIdAtEpoch(transactions));
const stakePoolIds = [lastEpoch + 1, lastEpoch + 2, lastEpoch + 3].map(getStakePoolIdAtEpoch(transactions));
return stakePoolSearchProvider(uniq(stakePoolIds.filter(util.isNotNil))).pipe(
map((stakePools) => stakePoolIds.map((poolId) => stakePools.find((pool) => pool.id === poolId) || undefined)),
map(([currentEpoch, nextEpoch, nextNextEpoch]) => {
Expand Down
Expand Up @@ -38,7 +38,7 @@ const firstDelegationEpoch$ = (transactions$: Observable<TxWithEpoch[]>) =>
transactions.filter(({ tx }) => transactionHasAnyCertificate(tx, [Cardano.CertificateType.StakeDelegation]))
)
),
map((tx) => (util.isNotNil(tx) ? tx.epoch + 2 : null)),
map((tx) => (util.isNotNil(tx) ? tx.epoch + 3 : null)),
distinctUntilChanged()
);

Expand Down
4 changes: 4 additions & 0 deletions packages/wallet/src/services/types.ts
Expand Up @@ -67,6 +67,10 @@ export interface RewardsHistory {
}

export interface Delegatee {
/**
* Rewards at the end of current epoch will
* be from this stake pool
*/
currentEpoch?: Cardano.StakePool;
nextEpoch?: Cardano.StakePool;
nextNextEpoch: Cardano.StakePool;
Expand Down
5 changes: 5 additions & 0 deletions packages/wallet/test/e2e/SingleAddressWallet.test.ts
Expand Up @@ -92,6 +92,7 @@ describe('SingleAddressWallet', () => {
const { poolId, certificates, isStakeKeyRegistered } = await createDelegationCertificates(wallet);
const initialDeposit = isStakeKeyRegistered ? stakeKeyDeposit : 0n;
expect(initialAvailableBalance.deposit).toBe(initialDeposit);
const [initialDelegatee] = await firstValueFrom(wallet.delegation.rewardAccounts$);

// Make a 1st tx with key registration (if not already registered) and stake delegation
// Also send some coin to faucet
Expand Down Expand Up @@ -128,6 +129,10 @@ describe('SingleAddressWallet', () => {
// Test it updates wallet.delegation after delegating to stake pool
(async () => {
expect(await waitForNewStakePoolIdAfterTx(wallet)).toBe(poolId);
const [newDelegatee] = await firstValueFrom(wallet.delegation.rewardAccounts$);
// nothing changes for 2 epochs
expect(newDelegatee.delegatee?.nextEpoch).toEqual(initialDelegatee?.delegatee?.nextEpoch);
expect(newDelegatee.delegatee?.currentEpoch).toEqual(initialDelegatee?.delegatee?.currentEpoch);
})(),
// Test confirmed$
async () => {
Expand Down
Expand Up @@ -48,9 +48,10 @@ describe('RewardAccounts', () => {
}
];
expect(getStakePoolIdAtEpoch(transactions)(102)).toBeUndefined();
expect(getStakePoolIdAtEpoch(transactions)(103)).toBe('pool1');
expect(getStakePoolIdAtEpoch(transactions)(104)).toBeUndefined();
expect(getStakePoolIdAtEpoch(transactions)(103)).toBeUndefined();
expect(getStakePoolIdAtEpoch(transactions)(104)).toBe('pool1');
expect(getStakePoolIdAtEpoch(transactions)(105)).toBeUndefined();
expect(getStakePoolIdAtEpoch(transactions)(106)).toBeUndefined();
});

test('addressKeyStatuses ', () => {
Expand Down Expand Up @@ -161,12 +162,7 @@ describe('RewardAccounts', () => {
a: [
{
certificates: [
{ __typename: Cardano.CertificateType.StakeKeyRegistration } as Cardano.StakeAddressCertificate
],
epoch: epoch - 2
},
{
certificates: [
{ __typename: Cardano.CertificateType.StakeKeyRegistration } as Cardano.StakeAddressCertificate,
{
__typename: Cardano.CertificateType.StakeDelegation,
poolId: 'pool1'
Expand Down
Expand Up @@ -18,7 +18,7 @@ describe('RewardsHistory', () => {
});

describe('createRewardsHistoryTracker', () => {
it('queries and maps reward history starting from first delgation epoch+2', () => {
it('queries and maps reward history starting from first delgation epoch+3', () => {
createTestScheduler().run(({ cold, expectObservable, flush }) => {
const epoch = rewardsHistory[0].epoch;
const getRewardsHistory = jest.fn().mockReturnValue(cold('-a', { a: rewardsHistory }));
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('RewardsHistory', () => {
});
flush();
expect(getRewardsHistory).toBeCalledTimes(1);
expect(getRewardsHistory).toBeCalledWith(epoch + 2);
expect(getRewardsHistory).toBeCalledWith(epoch + 3);
});
});
});
Expand Down

0 comments on commit 1c0d275

Please sign in to comment.