Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
feat(mean-finance): Improve Mean Finance label to show position info (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
FiboApe authored Jun 28, 2022
1 parent f395856 commit e042010
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 36 deletions.
40 changes: 32 additions & 8 deletions src/apps/mean-finance/helpers/intervals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,36 @@ const ONE_DAY = FOUR_HOURS * 6;
const ONE_WEEK = ONE_DAY * 7;

export const STRING_SWAP_INTERVALS = {
[ONE_MINUTE]: (left: number) => `${toReadable(left, ONE_MINUTE)} (${left} swaps)`,
[FIVE_MINUTES]: (left: number) => `${toReadable(left, FIVE_MINUTES)} (${left} swaps)`,
[FIFTEEN_MINUTES]: (left: number) => `${toReadable(left, FIFTEEN_MINUTES)} (${left} swaps)`,
[THIRTY_MINUTES]: (left: number) => `${toReadable(left, THIRTY_MINUTES)} (${left} swaps)`,
[ONE_HOUR]: (left: number) => `${toReadable(left, ONE_HOUR)} (${left} swaps)`,
[FOUR_HOURS]: (left: number) => `${toReadable(left, FOUR_HOURS)} (${left} swaps)`,
[ONE_DAY]: (left: number) => `${toReadable(left, ONE_DAY)} (${left} swaps)`,
[ONE_WEEK]: (left: number) => `${toReadable(left, ONE_WEEK)} (${left} swaps)`,
[ONE_MINUTE]: {
plural: (left: number) => `${toReadable(left, ONE_MINUTE)} (${left} swaps)`,
adverb: 'every 1 minute',
},
[FIVE_MINUTES]: {
plural: (left: number) => `${toReadable(left, FIVE_MINUTES)} (${left} swaps)`,
adverb: 'every 5 minutes',
},
[FIFTEEN_MINUTES]: {
plural: (left: number) => `${toReadable(left, FIFTEEN_MINUTES)} (${left} swaps)`,
adverb: 'every 15 minutes',
},
[THIRTY_MINUTES]: {
plural: (left: number) => `${toReadable(left, THIRTY_MINUTES)} (${left} swaps)`,
adverb: 'every 30 minutes',
},
[ONE_HOUR]: {
plural: (left: number) => `${toReadable(left, ONE_HOUR)} (${left} swaps)`,
adverb: 'every hour',
},
[FOUR_HOURS]: {
plural: (left: number) => `${toReadable(left, FOUR_HOURS)} (${left} swaps)`,
adverb: 'every 4 hours',
},
[ONE_DAY]: {
plural: (left: number) => `${toReadable(left, ONE_DAY)} (${left} swaps)`,
adverb: 'every day',
},
[ONE_WEEK]: {
plural: (left: number) => `${toReadable(left, ONE_WEEK)} (${left} swaps)`,
adverb: 'every week',
},
};
25 changes: 20 additions & 5 deletions src/apps/mean-finance/optimism/mean-finance.balance-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { BalanceFetcher } from '~balance/balance-fetcher.interface';
import { ContractType } from '~position/contract.interface';
import { WithMetaType } from '~position/display.interface';
import { BaseTokenBalance, ContractPositionBalance } from '~position/position-balance.interface';
import { claimable } from '~position/position.utils';
import { BaseToken } from '~position/token.interface';
import { Network } from '~types/network.interface';

import { getUserPositions } from '../helpers/graph';
Expand All @@ -36,8 +38,15 @@ export class OptimismMeanFinanceBalanceFetcher implements BalanceFetcher {
const contractPositionBalances: ContractPositionBalance[] = positions.map(dcaPosition => {
const toWithdraw = dcaPosition.current.idleSwapped;
const remainingLiquidity = dcaPosition.current.remainingLiquidity;
const remainingSwaps = dcaPosition.current.remainingSwaps;
const swapInterval = dcaPosition.swapInterval.interval;
const remainingSwaps = Number(dcaPosition.current.remainingSwaps);
const swapInterval = Number(dcaPosition.swapInterval.interval) as keyof typeof STRING_SWAP_INTERVALS;
const rawRate = dcaPosition.current.rate;
const rate = Number(rawRate) / 10 ** Number(dcaPosition.from.decimals);
let formattedRate = rate.toFixed(3);

if (rate < 0.001) {
formattedRate = '<0.001'
}

const from = baseTokens.find(v => v.address === dcaPosition.from.address);
const to = baseTokens.find(v => v.address === dcaPosition.to.address);
Expand All @@ -54,17 +63,23 @@ export class OptimismMeanFinanceBalanceFetcher implements BalanceFetcher {
}
if (to) {
to.network = network;
tokens.push(drillBalance(to, toWithdraw));
tokens.push(drillBalance(claimable(to), toWithdraw));
images = [
...images,
...getImagesFromToken(to),
];
}

const balanceUSD = sumBy(tokens, t => t.balanceUSD);
const swapIntervalAdverb = STRING_SWAP_INTERVALS[swapInterval].adverb;
let label = '';

const label = `Swapping ${from?.symbol} to ${to?.symbol}`;
const secondaryLabel = parseInt(remainingSwaps, 10) && STRING_SWAP_INTERVALS[swapInterval] ? `${STRING_SWAP_INTERVALS[swapInterval](remainingSwaps)} left` : 'Position finished';
if (remainingSwaps > 0) {
label = `Swapping ~${formattedRate} ${from?.symbol || dcaPosition.from.symbol} ${swapIntervalAdverb} to ${to?.symbol || dcaPosition.from.symbol}`;
} else {
label = `Swapping ${from?.symbol || dcaPosition.from.symbol} to ${to?.symbol || dcaPosition.from.symbol}`;
}
const secondaryLabel = remainingSwaps && STRING_SWAP_INTERVALS[swapInterval] ? `${STRING_SWAP_INTERVALS[swapInterval].plural(remainingSwaps)} left` : 'Position finished';

return {
type: ContractType.POSITION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { WithMetaType } from '~position/display.interface';
import { BaseTokenBalance } from '~position/position-balance.interface';
import { PositionFetcher } from '~position/position-fetcher.interface';
import { ContractPosition } from '~position/position.interface';
import { claimable } from '~position/position.utils';
import { BaseToken } from '~position/token.interface';
import { Network } from '~types/network.interface';

import { MeanFinanceContractFactory } from '../contracts';
Expand Down Expand Up @@ -37,8 +39,15 @@ export class OptimismMeanFinanceDcaPositionContractPositionFetcher implements Po
const contractPositions: ContractPosition[] = positions.map(dcaPosition => {
const toWithdraw = dcaPosition.current.idleSwapped;
const remainingLiquidity = dcaPosition.current.remainingLiquidity;
const remainingSwaps = dcaPosition.current.remainingSwaps;
const swapInterval = dcaPosition.swapInterval.interval;
const remainingSwaps = Number(dcaPosition.current.remainingSwaps);
const swapInterval = Number(dcaPosition.swapInterval.interval) as keyof typeof STRING_SWAP_INTERVALS;
const rawRate = dcaPosition.current.rate;
const rate = Number(rawRate) / 10 ** Number(dcaPosition.from.decimals);
let formattedRate = rate.toFixed(3);

if (rate < 0.001) {
formattedRate = '<0.001'
}

const from = baseTokens.find(v => v.address === dcaPosition.from.address);
const to = baseTokens.find(v => v.address === dcaPosition.to.address);
Expand All @@ -55,18 +64,24 @@ export class OptimismMeanFinanceDcaPositionContractPositionFetcher implements Po
}
if (to) {
to.network = network;
tokens.push(drillBalance(to, toWithdraw));
tokens.push(drillBalance(claimable(to), toWithdraw));
images = [
...images,
...getImagesFromToken(to),
];
}

const balanceUSD = sumBy(tokens, t => t.balanceUSD);
const swapIntervalAdverb = STRING_SWAP_INTERVALS[swapInterval].adverb;
let label = '';

const label = `Swapping ${from?.symbol} to ${to?.symbol}`;
const secondaryLabel = parseInt(remainingSwaps, 10) && STRING_SWAP_INTERVALS[swapInterval] ? `${STRING_SWAP_INTERVALS[swapInterval](remainingSwaps)} left` : 'Position finished';
if (remainingSwaps > 0) {
label = `Swapping ~${formattedRate} ${from?.symbol || dcaPosition.from.symbol} ${swapIntervalAdverb} to ${to?.symbol || dcaPosition.from.symbol}`;
} else {
label = `Swapping ${from?.symbol || dcaPosition.from.symbol} to ${to?.symbol || dcaPosition.from.symbol}`;
}

const secondaryLabel = remainingSwaps && STRING_SWAP_INTERVALS[swapInterval] ? `${STRING_SWAP_INTERVALS[swapInterval].plural(remainingSwaps)} left` : 'Position finished';
return {
type: ContractType.POSITION,
address: dcaPosition.id,
Expand Down
4 changes: 3 additions & 1 deletion src/apps/mean-finance/optimism/mean-finance.tvl-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Network } from '~types/network.interface';
import { getPositions } from '../helpers/graph';

import { MEAN_FINANCE_DEFINITION } from '../mean-finance.definition';
import { claimable } from '~position/position.utils';
import { BaseToken } from '~position/token.interface';

const appId = MEAN_FINANCE_DEFINITION.id;
const network = Network.OPTIMISM_MAINNET;
Expand Down Expand Up @@ -39,7 +41,7 @@ export class OptimismMeanFinanceTvlFetcher implements TvlFetcher {
}
if (to) {
to.network = network;
tokens.push(drillBalance(to, toWithdraw));
tokens.push(drillBalance(claimable(to), toWithdraw));
}

const balanceUSD = sumBy(tokens, t => t.balanceUSD);
Expand Down
27 changes: 22 additions & 5 deletions src/apps/mean-finance/polygon/mean-finance.balance-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { BalanceFetcher } from '~balance/balance-fetcher.interface';
import { ContractType } from '~position/contract.interface';
import { WithMetaType } from '~position/display.interface';
import { BaseTokenBalance, ContractPositionBalance } from '~position/position-balance.interface';
import { claimable } from '~position/position.utils';
import { BaseToken } from '~position/token.interface';
import { Network } from '~types/network.interface';

import { getUserPositions } from '../helpers/graph';
Expand All @@ -36,8 +38,15 @@ export class PolygonMeanFinanceBalanceFetcher implements BalanceFetcher {
const contractPositionBalances: ContractPositionBalance[] = positions.map(dcaPosition => {
const toWithdraw = dcaPosition.current.idleSwapped;
const remainingLiquidity = dcaPosition.current.remainingLiquidity;
const remainingSwaps = dcaPosition.current.remainingSwaps;
const swapInterval = dcaPosition.swapInterval.interval;
const remainingSwaps = Number(dcaPosition.current.remainingSwaps);
const swapInterval = Number(dcaPosition.swapInterval.interval) as keyof typeof STRING_SWAP_INTERVALS;
const rawRate = dcaPosition.current.rate;
const rate = Number(rawRate) / 10 ** Number(dcaPosition.from.decimals);
let formattedRate = rate.toFixed(3);

if (rate < 0.001) {
formattedRate = '<0.001'
}

const from = baseTokens.find(v => v.address === dcaPosition.from.address);
const to = baseTokens.find(v => v.address === dcaPosition.to.address);
Expand All @@ -54,7 +63,7 @@ export class PolygonMeanFinanceBalanceFetcher implements BalanceFetcher {
}
if (to) {
to.network = network;
tokens.push(drillBalance(to, toWithdraw));
tokens.push(drillBalance(claimable(to), toWithdraw));
images = [
...images,
...getImagesFromToken(to),
Expand All @@ -63,8 +72,16 @@ export class PolygonMeanFinanceBalanceFetcher implements BalanceFetcher {

const balanceUSD = sumBy(tokens, t => t.balanceUSD);

const label = `Swapping ${from?.symbol} to ${to?.symbol}`;
const secondaryLabel = parseInt(remainingSwaps, 10) && STRING_SWAP_INTERVALS[swapInterval] ? `${STRING_SWAP_INTERVALS[swapInterval](remainingSwaps)} left` : 'Position finished';
const swapIntervalAdverb = STRING_SWAP_INTERVALS[swapInterval].adverb;
let label = '';

if (remainingSwaps > 0) {
label = `Swapping ~${formattedRate} ${from?.symbol || dcaPosition.from.symbol} ${swapIntervalAdverb} to ${to?.symbol || dcaPosition.from.symbol}`;
} else {
label = `Swapping ${from?.symbol || dcaPosition.from.symbol} to ${to?.symbol || dcaPosition.from.symbol}`;
}

const secondaryLabel = remainingSwaps && STRING_SWAP_INTERVALS[swapInterval] ? `${STRING_SWAP_INTERVALS[swapInterval].plural(remainingSwaps)} left` : 'Position finished';

return {
type: ContractType.POSITION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { WithMetaType } from '~position/display.interface';
import { BaseTokenBalance } from '~position/position-balance.interface';
import { PositionFetcher } from '~position/position-fetcher.interface';
import { ContractPosition } from '~position/position.interface';
import { claimable } from '~position/position.utils';
import { BaseToken } from '~position/token.interface';
import { Network } from '~types/network.interface';

import { MeanFinanceContractFactory } from '../contracts';
Expand Down Expand Up @@ -36,8 +38,15 @@ export class PolygonMeanFinanceDcaPositionContractPositionFetcher implements Pos
const contractPositions: ContractPosition[] = positions.map(dcaPosition => {
const toWithdraw = dcaPosition.current.idleSwapped;
const remainingLiquidity = dcaPosition.current.remainingLiquidity;
const remainingSwaps = dcaPosition.current.remainingSwaps;
const swapInterval = dcaPosition.swapInterval.interval;
const remainingSwaps = Number(dcaPosition.current.remainingSwaps);
const swapInterval = Number(dcaPosition.swapInterval.interval) as keyof typeof STRING_SWAP_INTERVALS;
const rawRate = dcaPosition.current.rate;
const rate = Number(rawRate) / 10 ** Number(dcaPosition.from.decimals);
let formattedRate = rate.toFixed(3);

if (rate < 0.001) {
formattedRate = '<0.001'
}

const from = baseTokens.find(v => v.address === dcaPosition.from.address);
const to = baseTokens.find(v => v.address === dcaPosition.to.address);
Expand All @@ -54,7 +63,7 @@ export class PolygonMeanFinanceDcaPositionContractPositionFetcher implements Pos
}
if (to) {
to.network = network;
tokens.push(drillBalance(to, toWithdraw));
tokens.push(drillBalance(claimable(to), toWithdraw));
images = [
...images,
...getImagesFromToken(to),
Expand All @@ -63,8 +72,15 @@ export class PolygonMeanFinanceDcaPositionContractPositionFetcher implements Pos

const balanceUSD = sumBy(tokens, t => t.balanceUSD);

const label = `Swapping ${from?.symbol} to ${to?.symbol}`;
const secondaryLabel = parseInt(remainingSwaps, 10) && STRING_SWAP_INTERVALS[swapInterval] ? `${STRING_SWAP_INTERVALS[swapInterval](remainingSwaps)} left` : 'Position finished';
const swapIntervalAdverb = STRING_SWAP_INTERVALS[swapInterval].adverb;
let label = '';

if (remainingSwaps > 0) {
label = `Swapping ~${formattedRate} ${from?.symbol || dcaPosition.from.symbol} ${swapIntervalAdverb} to ${to?.symbol || dcaPosition.from.symbol}`;
} else {
label = `Swapping ${from?.symbol || dcaPosition.from.symbol} to ${to?.symbol || dcaPosition.from.symbol}`;
}
const secondaryLabel = remainingSwaps && STRING_SWAP_INTERVALS[swapInterval] ? `${STRING_SWAP_INTERVALS[swapInterval].plural(remainingSwaps)} left` : 'Position finished';

return {
type: ContractType.POSITION,
Expand Down
4 changes: 3 additions & 1 deletion src/apps/mean-finance/polygon/mean-finance.tvl-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Network } from '~types/network.interface';
import { getPositions } from '../helpers/graph';

import { MEAN_FINANCE_DEFINITION } from '../mean-finance.definition';
import { claimable } from '~position/position.utils';
import { BaseToken } from '~position/token.interface';

const appId = MEAN_FINANCE_DEFINITION.id;
const network = Network.POLYGON_MAINNET;
Expand Down Expand Up @@ -39,7 +41,7 @@ export class PolygonMeanFinanceTvlFetcher implements TvlFetcher {
}
if (to) {
to.network = network;
tokens.push(drillBalance(to, toWithdraw));
tokens.push(drillBalance(claimable(to), toWithdraw));
}

const balanceUSD = sumBy(tokens, t => t.balanceUSD);
Expand Down
12 changes: 6 additions & 6 deletions src/position/position.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const isClaimable = (token: WithMetaType<Token>) => token.metaType === Me
export const isVesting = (token: WithMetaType<Token>) => token.metaType === MetaType.VESTING;
export const isLocked = (token: WithMetaType<Token>) => token.metaType === MetaType.LOCKED;

export const wallet = <T>(token: T): WithMetaType<T> => ({ metaType: MetaType.WALLET, ...token });
export const supplied = <T>(token: T): WithMetaType<T> => ({ metaType: MetaType.SUPPLIED, ...token });
export const borrowed = <T>(token: T): WithMetaType<T> => ({ metaType: MetaType.BORROWED, ...token });
export const claimable = <T>(token: T): WithMetaType<T> => ({ metaType: MetaType.CLAIMABLE, ...token });
export const vesting = <T>(token: T): WithMetaType<T> => ({ metaType: MetaType.VESTING, ...token });
export const locked = <T>(token: T): WithMetaType<T> => ({ metaType: MetaType.LOCKED, ...token });
export const wallet = <T extends Token>(token: T): WithMetaType<T> => ({ metaType: MetaType.WALLET, ...token });
export const supplied = <T extends Token>(token: T): WithMetaType<T> => ({ metaType: MetaType.SUPPLIED, ...token });
export const borrowed = <T extends Token>(token: T): WithMetaType<T> => ({ metaType: MetaType.BORROWED, ...token });
export const claimable = <T extends Token>(token: T): WithMetaType<T> => ({ metaType: MetaType.CLAIMABLE, ...token });
export const vesting = <T extends Token>(token: T): WithMetaType<T> => ({ metaType: MetaType.VESTING, ...token });
export const locked = <T extends Token>(token: T): WithMetaType<T> => ({ metaType: MetaType.LOCKED, ...token });

0 comments on commit e042010

Please sign in to comment.