Skip to content

Commit

Permalink
fixup! fixup! test(e2e): delegation portfolio e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceahasegan committed Jun 2, 2023
1 parent d4e79ed commit 7535de3
Showing 1 changed file with 28 additions and 24 deletions.
@@ -1,8 +1,8 @@
import { Cardano } from '@cardano-sdk/core';
import { DelegatedStake, PersonalWallet, createUtxoBalanceByAddressTracker } from '@cardano-sdk/wallet';
import { MINUTE, getWallet } from '../../../src';
import { Observable, filter, firstValueFrom, map, tap } from 'rxjs';
import { Percent } from '@cardano-sdk/util';
import { PersonalWallet, StakeByPool, createUtxoBalanceByAddressTracker } from '@cardano-sdk/wallet';
import { createLogger } from '@cardano-sdk/util-dev';
import { firstValueFromTimed, submitAndConfirm, walletReady } from '../../util';
import { getEnv, walletVariables } from '../../../src/environment';
Expand Down Expand Up @@ -125,7 +125,7 @@ const delegateToMultiplePools = async (wallet: PersonalWallet) => {
return poolIds;
};

describe('PersonalWallet/stakeDistribution', () => {
describe('PersonalWallet/delegationDistribution', () => {
let wallet: PersonalWallet;

beforeAll(async () => {
Expand All @@ -138,14 +138,14 @@ describe('PersonalWallet/stakeDistribution', () => {
wallet.shutdown();
});

it('reports observable wallet multi delegation as stakeDistribution by pool', async () => {
it('reports observable wallet multi delegation as delegationDistribution by pool', async () => {
await walletReady(wallet);
const walletAddresses = await firstValueFromTimed(wallet.addresses$);
const rewardAccounts = await firstValueFrom(wallet.delegation.rewardAccounts$);

// No stake distribution initially
const stakeDistribution = await firstValueFrom(wallet.delegation.stakeDistribution$);
expect(stakeDistribution).toEqual([]);
const delegationDistribution = await firstValueFrom(wallet.delegation.distribution$);
expect(delegationDistribution).toEqual(new Map());

const poolIds = await delegateToMultiplePools(wallet);

Expand All @@ -167,16 +167,16 @@ describe('PersonalWallet/stakeDistribution', () => {
})
);

// Check delegation.stakeDistribution$ has the delegation information
const expectedStakeDistribution: StakeByPool[] = rewardAccounts.map(({ address }, index) => ({
// Check delegation.delegationDistribution$ has the delegation information
const expectedDelegationDistribution: DelegatedStake[] = rewardAccounts.map(({ address }, index) => ({
percentage: Percent(Number(perAddrBalance[index]) / Number(totalBalance.coins)),
pool: expect.objectContaining({ id: poolIds[index].id }),
rewardAccounts: [address],
value: perAddrBalance[index]
stake: perAddrBalance[index]
}));
const actualStakeDistribution = await firstValueFrom(wallet.delegation.stakeDistribution$);
const actualDelegationDistribution = await firstValueFrom(wallet.delegation.distribution$);

expect(actualStakeDistribution).toEqual(expectedStakeDistribution);
expect([...actualDelegationDistribution.values()]).toEqual(expectedDelegationDistribution);

// Send all coins to the last address. Check that stake distribution is 100 for that address and 0 for the rest
const { coins: totalCoins } = await firstValueFrom(wallet.balance.utxo.total$);
Expand All @@ -193,14 +193,18 @@ describe('PersonalWallet/stakeDistribution', () => {
.sign();
await submitAndConfirm(wallet, txMoveFunds);

let simplifiedStakeDistribution: Partial<StakeByPool>[] = await firstValueFrom(
wallet.delegation.stakeDistribution$.pipe(
map((stakeByPool) =>
stakeByPool.map(({ pool, percentage }) => ({ id: pool.id, name: pool.metadata?.name, percentage }))
let simplifiedDelegationDistribution: Partial<DelegatedStake>[] = await firstValueFrom(
wallet.delegation.distribution$.pipe(
map((delegatedStake) =>
[...delegatedStake.values()].map(({ pool, percentage }) => ({
id: pool.id,
name: pool.metadata?.name,
percentage
}))
)
)
);
expect(simplifiedStakeDistribution).toEqual(
expect(simplifiedDelegationDistribution).toEqual(
rewardAccounts.map((_, index) => ({
id: poolIds[index].id,
name: poolIds[index].metadata?.name,
Expand All @@ -210,23 +214,23 @@ describe('PersonalWallet/stakeDistribution', () => {
}))
);

// Delegate all reward accounts to the same pool. stakeDistribution$ should have 1 entry with 100% distribution
// Delegate all reward accounts to the same pool. delegationDistribution$ should have 1 entry with 100% distribution
txBuilder = wallet.createTxBuilder();
const { tx: txDelegateTo1Pool } = await txBuilder.delegate(poolIds[0].id).build().sign();
await submitAndConfirm(wallet, txDelegateTo1Pool);
simplifiedStakeDistribution = await firstValueFrom(
wallet.delegation.stakeDistribution$.pipe(
simplifiedDelegationDistribution = await firstValueFrom(
wallet.delegation.distribution$.pipe(
map((distribution) =>
distribution.map((stakeByPool) => ({
id: stakeByPool.pool.id,
name: stakeByPool.pool.metadata?.name,
percentage: stakeByPool.percentage,
rewardAccounts: stakeByPool.rewardAccounts
[...distribution.values()].map((delegatedStake) => ({
id: delegatedStake.pool.id,
name: delegatedStake.pool.metadata?.name,
percentage: delegatedStake.percentage,
rewardAccounts: delegatedStake.rewardAccounts
}))
)
)
);
expect(simplifiedStakeDistribution).toEqual([
expect(simplifiedDelegationDistribution).toEqual([
{
id: poolIds[0].id,
name: poolIds[0].metadata?.name,
Expand Down

0 comments on commit 7535de3

Please sign in to comment.