Skip to content

Commit

Permalink
feat: sync api interface with solana task
Browse files Browse the repository at this point in the history
  • Loading branch information
icfor committed Apr 5, 2024
1 parent bebb78c commit 65da594
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 61 deletions.
85 changes: 43 additions & 42 deletions src/screens/staking/lib/staking_sdk/context/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { walletsSupported } from "../core";
import type { Coin, CoinDenom, StakingNetworkId, WalletId } from "../core/base";
import { mainNetworkDenom } from "../core/base";
import {
filterOutTestnets,
filterUniqueAddresses,
getClaimableRewardsForAccount,
} from "../utils/accounts";
Expand Down Expand Up @@ -121,38 +120,44 @@ export const getUnbondingTokensForNetwork = (
): UnbondingTokensResult => {
const accountsForNetwork = getAccountsForNetwork(state, network);

return accountsForNetwork.reduce((acc, account) => {
if (!account.info?.unbonding?.length) {
return acc;
}

const { unbonding } = account.info;

const denom = networkToUnnormalisedDenom[account.networkId];

return unbonding.reduce((acc2, unbondingInfo) => {
const baseCoin = acc2 ? acc2.coin : getEmptyCoin(denom);
const basePeriod = acc2 ? acc2.period : "0";

const newCoin = sumCoins(baseCoin, {
amount: unbondingInfo.balance,
denom,
});

if (!unbondingInfo.completion_time) return acc2;

const itemPeriod = unbondingInfo.completion_time.seconds;

const newPeriod = new BigNumber(basePeriod).isGreaterThan(itemPeriod)
? basePeriod
: itemPeriod;

return {
coin: newCoin,
period: newPeriod,
};
}, acc);
}, null as UnbondingTokensResult);
return accountsForNetwork
.filter(filterUniqueAddresses())
.reduce((acc, account) => {
if (!account.info?.unbonding?.length) {
return acc;
}

const { unbonding } = account.info;

const denom = networkToUnnormalisedDenom[account.networkId];

return unbonding.reduce((acc2, unbondingInfo) => {
const baseCoin = acc2 ? acc2.coin : getEmptyCoin(denom);
const basePeriod = acc2 ? acc2.period : "";

const newCoin = sumCoins(baseCoin, {
amount: unbondingInfo.balance,
denom,
});

if (!unbondingInfo.completion_time)
return {
coin: newCoin,
period: basePeriod,
};

const itemPeriod = unbondingInfo.completion_time.seconds;

const newPeriod = new BigNumber(basePeriod).isGreaterThan(itemPeriod)
? basePeriod
: itemPeriod;

return {
coin: newCoin,
period: newPeriod,
};
}, acc);
}, null as UnbondingTokensResult);
};

export const getHasConnectedWallets = (state: StakingState) =>
Expand Down Expand Up @@ -230,11 +235,9 @@ export const getAllStaked = (
): number => {
const accounts = accountsProp || getAllAccounts(state);

const uniqueMainnetAccounts = accounts
.filter(filterUniqueAddresses())
.filter(filterOutTestnets);
const uniqueAccounts = accounts.filter(filterUniqueAddresses());

return uniqueMainnetAccounts.reduce((acc, account) => {
return uniqueAccounts.reduce((acc, account) => {
const delegationProp = account.info?.delegation;

const delegation = Array.isArray(delegationProp)
Expand Down Expand Up @@ -263,17 +266,15 @@ export const getAllRewards = (
) => {
const accounts = accountsProp || getAllAccounts(state);

const uniqueMainnetAccounts = accounts
.filter(filterUniqueAddresses())
.filter(filterOutTestnets);
const uniqueAccounts = accounts.filter(filterUniqueAddresses());

return uniqueMainnetAccounts.reduce((acc, account) => {
return uniqueAccounts.reduce((acc, account) => {
const { rewards } = account;

if (!rewards) return acc;

const newValue = rewards.reduce((acc2, reward) => {
const normalised = normaliseCoin(reward);
const normalised = normaliseCoin(reward.coin);

const coinPrice = state.coinsPrices[normalised.denom as CoinDenom];

Expand Down
30 changes: 21 additions & 9 deletions src/screens/staking/lib/staking_sdk/staking_client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import BigNumber from "bignumber.js";

import type { StakingNetworkId } from "./core/base";
import { cosmosStakingNetworks } from "./core/cosmos";
import type {
AccountDetailResponse,
ClaimableRewardsResponse,
StakingInfoResponse,
} from "./staking_client_types";
import { normaliseCoin } from "./utils/coins";
import { normaliseCoin, unnormalisedDenomToNetwork } from "./utils/coins";

const baseUrl =
process.env.NEXT_PUBLIC_STAKING_API || "https://staking-api.forbole.com";
Expand All @@ -28,20 +29,31 @@ const fetchJson = <A = any>(uri: string, opts?: Options): Promise<A> =>
},
}).then((res) => res.json());

const rewardsDivisor = new BigNumber(10).pow(18);
const getRewardsDivisor = (denom: string) => {
const network = unnormalisedDenomToNetwork[denom.toUpperCase()];

if (cosmosStakingNetworks.has(network as StakingNetworkId)) {
return new BigNumber(10).pow(18);
}

return 1;
};

const parseStakingRewards = async (res: ClaimableRewardsResponse) =>
Array.isArray(res)
? res
.map((coin) => {
const num = new BigNumber(coin.amount);
? res.map((reward) => {
const { coin } = reward;
const num = new BigNumber(coin.amount);
const rewardsDivisor = getRewardsDivisor(coin.denom);

return {
return {
...reward,
coin: normaliseCoin({
amount: num.dividedBy(rewardsDivisor).toString(),
denom: coin.denom,
};
})
.map(normaliseCoin)
}),
};
})
: res;

type StakeResponse = {
Expand Down
18 changes: 17 additions & 1 deletion src/screens/staking/lib/staking_sdk/staking_client_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,30 @@ type Coin = {
denom: string;
};

type StakeAccountStatus = "activating" | "active" | "deactivating" | "inactive";

export type StakeAccount = {
address: string;
amount: string;
denom: string;
status: StakeAccountStatus;
validator_address: string;
};

export type AccountDetailResponse = {
account_number: null | number;
address: string;
balances: Coin | null;
delegation: Coin | Coin[] | null;
network: string;
sequence: null | number;
stakeAccounts?: StakeAccount[];
unbonding: Unbonding[];
};

export type ClaimableRewardsResponse = Coin[] | Record<string, never>;
type Reward = {
address: string;
coin: Coin;
};

export type ClaimableRewardsResponse = Record<string, never> | Reward[];
4 changes: 2 additions & 2 deletions src/screens/staking/lib/staking_sdk/utils/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export const sortAccounts = (a: Account, b: Account) => {
export const getClaimableRewardsForAccount = (start: Coin, account: Account) =>
(Array.isArray(account.rewards) ? account.rewards : []).reduce(
(acc2, reward) => {
if (start.denom?.toUpperCase() === reward.denom?.toUpperCase()) {
return sumCoins(acc2, reward);
if (start.denom?.toUpperCase() === reward.coin.denom?.toUpperCase()) {
return sumCoins(acc2, reward.coin);
}

return acc2;
Expand Down
15 changes: 13 additions & 2 deletions src/screens/staking/lib/staking_sdk/utils/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,20 @@ export const networkToUnnormalisedDenom = {

type DenomToNormalise = (typeof networkToUnnormalisedDenom)[StakingNetworkId];

const uExp = 6;
const pExp = 12;
export const unnormalisedDenomToNetwork = Object.entries(
networkToUnnormalisedDenom,
).reduce(
(acc, [k, v]) => {
acc[v] = k as StakingNetworkId;

return acc;
},
{} as Record<string, StakingNetworkId>,
);

const aExp = 18;
const pExp = 12;
const uExp = 6;

const denomMap: Record<DenomToNormalise, [CoinDenom, number]> = {
ADYDX: [CoinDenom.DYDX, aExp],
Expand Down
10 changes: 5 additions & 5 deletions src/screens/staking/lib/staking_sdk/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { walletsSupported } from "../core";
import type { WalletId } from "../core/base";

const localStorageKey = "connectedWallets";
const walletsStorageKey = "connectedWallets";

export const getConnectedWallets = (): WalletId[] => {
const connectedWallets = window.localStorage.getItem(localStorageKey);
const connectedWallets = window.localStorage.getItem(walletsStorageKey);

if (!connectedWallets) {
return [];
Expand All @@ -25,16 +25,16 @@ export const addToConnectedWallets = (wallet: WalletId) => {
const newWalletsSet = new Set([...connectedWallets, wallet]);
const newWallets = Array.from(newWalletsSet);

window.localStorage.setItem(localStorageKey, JSON.stringify(newWallets));
window.localStorage.setItem(walletsStorageKey, JSON.stringify(newWallets));
};

export const setConnectedWallet = (walletsIds: WalletId[]) => {
if (!walletsIds.length) {
window.localStorage.removeItem(localStorageKey);
window.localStorage.removeItem(walletsStorageKey);
} else {
const newWalletsSet = new Set([...walletsIds]);
const newWallets = Array.from(newWalletsSet);

window.localStorage.setItem(localStorageKey, JSON.stringify(newWallets));
window.localStorage.setItem(walletsStorageKey, JSON.stringify(newWallets));
}
};
2 changes: 2 additions & 0 deletions src/screens/staking/lib/staking_sdk/wallet_operations/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Account } from "../core";
import type { Coin } from "../core/base";
import type { StakeAccount } from "../staking_client_types";

export type WalletOperationResult<ErrorType> =
| {
Expand All @@ -21,6 +22,7 @@ export type UnstakeAmount = {
account: Account;
amount: string;
memo: string;
stakeAccount?: StakeAccount | undefined;
};

export enum UnstakeError {
Expand Down

0 comments on commit 65da594

Please sign in to comment.