Skip to content

Commit

Permalink
feat: fetch dreps
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirvolek committed Dec 11, 2024
1 parent 034c264 commit 71aece6
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 16 deletions.
49 changes: 38 additions & 11 deletions packages/suite/src/actions/wallet/cardanoStakingActions.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import { getUnixTime } from 'date-fns';

import { BlockchainBlock } from '@trezor/connect';
import { CARDANO_STAKE_POOL_PREVIEW_URL, CARDANO_STAKE_POOL_MAINNET_URL } from '@trezor/urls';
import {
CARDANO_STAKE_POOL_PREVIEW_URL,
CARDANO_STAKE_POOL_MAINNET_URL,
CARDANO_MAINNET_DREPS,
CARDANO_PREVIEW_DREPS,
} from '@trezor/urls';
import { isPending, getAccountTransactions } from '@suite-common/wallet-utils';
import { CARDANO_DEFAULT_TTL_OFFSET } from '@suite-common/wallet-constants';
import { transactionsActions } from '@suite-common/wallet-core';
import { getNetworkOptional } from '@suite-common/wallet-config';

import { CARDANO_STAKING } from 'src/actions/wallet/constants';
import { PendingStakeTx, PoolsResponse, CardanoNetwork } from 'src/types/wallet/cardanoStaking';
import {
PendingStakeTx,
PoolsResponse,
CardanoNetwork,
DrepsResponse,
} from 'src/types/wallet/cardanoStaking';
import { Account, WalletAccountTransaction } from 'src/types/wallet';
import { Dispatch, GetState } from 'src/types/suite';

export type CardanoStakingAction =
| { type: typeof CARDANO_STAKING.ADD_PENDING_STAKE_TX; pendingStakeTx: PendingStakeTx }
| { type: typeof CARDANO_STAKING.REMOVE_PENDING_STAKE_TX; accountKey: string }
| {
type: typeof CARDANO_STAKING.SET_TREZOR_POOLS;
type: typeof CARDANO_STAKING.SET_TREZOR_DATA;
trezorPools: PoolsResponse;
network: CardanoNetwork;
}
Expand Down Expand Up @@ -102,7 +112,7 @@ export const validatePendingStakeTxOnTx =
}
};

export const fetchTrezorPools = (network: 'ADA' | 'tADA') => async (dispatch: Dispatch) => {
export const fetchTrezorData = (network: 'ADA' | 'tADA') => async (dispatch: Dispatch) => {
const cardanoNetwork = network === 'ADA' ? 'mainnet' : 'preview';

dispatch({
Expand All @@ -112,24 +122,41 @@ export const fetchTrezorPools = (network: 'ADA' | 'tADA') => async (dispatch: Di
});

// Fetch ID of Trezor stake pool that will be used in delegation transaction
const url =
const url_pools =
cardanoNetwork === 'mainnet'
? CARDANO_STAKE_POOL_MAINNET_URL
: CARDANO_STAKE_POOL_PREVIEW_URL;

try {
const response = await fetch(url, { credentials: 'same-origin' });
const responseJson = await response.json();
// Fetch dreps for transaction withdrawal
const url_dreps = cardanoNetwork === 'mainnet' ? CARDANO_MAINNET_DREPS : CARDANO_PREVIEW_DREPS;

if (!responseJson || !('next' in responseJson) || !('pools' in responseJson)) {
try {
const responsePools = await fetch(url_pools, { credentials: 'same-origin' });
const responsePoolsJson = await responsePools.json();

if (
!responsePoolsJson ||
!('next' in responsePoolsJson) ||
!('pools' in responsePoolsJson)
) {
// todo: even if this happens, error will be overridden by this bug
// https://github.com/trezor/trezor-suite/issues/5485
throw new Error('Cardano: fetchTrezorPools: Invalid data format');
}

const responseDreps = await fetch(url_dreps, { credentials: 'same-origin' });
const responseDrepsJson = await responseDreps.json();

if (!responseDrepsJson || !('drep' in responseDrepsJson)) {
// todo: even if this happens, error will be overridden by this bug
// https://github.com/trezor/trezor-suite/issues/5485
throw new Error('Cardano: fetchTrezorDreps: Invalid data format');
}

dispatch({
type: CARDANO_STAKING.SET_TREZOR_POOLS,
trezorPools: responseJson as PoolsResponse,
type: CARDANO_STAKING.SET_TREZOR_DATA,
trezorPools: responsePoolsJson as PoolsResponse,
trezorDreps: responseDrepsJson as DrepsResponse,
network: cardanoNetwork,
});
} catch {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const ADD_PENDING_STAKE_TX = '@cardano-staking/set-pending-stake-tx';
export const REMOVE_PENDING_STAKE_TX = '@cardano-staking/remove-pending-stake-tx';
export const IS_LOADING = '@cardano-staking/is-loading';
export const SET_TREZOR_POOLS = '@cardano-staking/set-trezor-pools';
export const SET_TREZOR_DATA = '@cardano-staking/set-trezor-data';
export const SET_FETCH_LOADING = '@cardano-staking/set-fetch-loading';
export const SET_FETCH_ERROR = '@cardano-staking/set-fetch-error';
2 changes: 1 addition & 1 deletion packages/suite/src/support/extraDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const connectInitSettings = {
export const extraDependencies: ExtraDependencies = {
thunks: {
cardanoValidatePendingTxOnBlock: cardanoStakingActions.validatePendingTxOnBlock,
cardanoFetchTrezorPools: cardanoStakingActions.fetchTrezorPools,
cardanoFetchTrezorData: cardanoStakingActions.fetchTrezorData,
initMetadata: metadataLabelingActions.init,
fetchAndSaveMetadata: metadataLabelingActions.fetchAndSaveMetadata,
addAccountMetadata: metadataLabelingActions.addAccountMetadata,
Expand Down
1 change: 1 addition & 0 deletions packages/suite/src/types/wallet/cardanoStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export type {
StakePool,
PoolsResponse,
ActionAvailability,
DrepsResponse,
CardanoStaking,
} from '@suite-common/wallet-types';
4 changes: 4 additions & 0 deletions packages/urls/src/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ export const CARDANO_STAKE_POOL_MAINNET_URL: Url =
'https://trezor-cardano-mainnet.blockfrost.io/api/v0/pools/';
export const CARDANO_STAKE_POOL_PREVIEW_URL: Url =
'https://trezor-cardano-preview.blockfrost.io/api/v0/pools/';
export const CARDANO_MAINNET_DREPS: Url =
'https://trezor-cardano-mainnet.blockfrost.io/api/v0/dreps/';
export const CARDANO_PREVIEW_DREPS: Url =
'https://trezor-cardano-preview.blockfrost.io/api/v0/dreps/';

export const CHROME_URL: Url = 'https://www.google.com/chrome/';
export const CHROME_UPDATE_URL: Url = 'https://support.google.com/chrome/answer/95414';
Expand Down
2 changes: 1 addition & 1 deletion suite-common/redux-utils/src/extraDependenciesType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type ExtraDependencies = {
block: BlockchainBlock;
timestamp: number;
}>;
cardanoFetchTrezorPools: SuiteCompatibleThunk<'tADA' | 'ADA'>;
cardanoFetchTrezorData: SuiteCompatibleThunk<'tADA' | 'ADA'>;
initMetadata: SuiteCompatibleThunk<boolean>;
fetchAndSaveMetadata: SuiteCompatibleThunk<StaticSessionId>;
addAccountMetadata: SuiteCompatibleThunk<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const prepareBlockchainMiddleware = createMiddlewareWithExtraDeps(
// propagate action to reducers
next(action);

const { cardanoValidatePendingTxOnBlock, cardanoFetchTrezorPools } = extra.thunks;
const { cardanoValidatePendingTxOnBlock, cardanoFetchTrezorData } = extra.thunks;

switch (action.type) {
case TREZOR_CONNECT_BLOCKCHAIN_ACTIONS.CONNECT:
Expand All @@ -26,7 +26,7 @@ export const prepareBlockchainMiddleware = createMiddlewareWithExtraDeps(
// for cardano staking if applicable
if (['ADA', 'tADA'].includes(action.payload.coin.shortcut)) {
dispatch(
cardanoFetchTrezorPools(action.payload.coin.shortcut as 'ADA' | 'tADA'),
cardanoFetchTrezorData(action.payload.coin.shortcut as 'ADA' | 'tADA'),
);
}
break;
Expand Down
9 changes: 9 additions & 0 deletions suite-common/wallet-types/src/cardanoStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ export type PoolsResponse = {
pools: StakePool[];
};

interface Drep {
hex: string;
bech32: string;
}

export interface DrepsResponse {
[key: string]: Drep;
}

export type ActionAvailability =
| { status: true; reason?: undefined }
| { status: false; reason: 'POOL_ID_FETCH_FAIL' | 'TX_NOT_FINAL' | 'UTXO_BALANCE_INSUFFICIENT' }
Expand Down

0 comments on commit 71aece6

Please sign in to comment.