Skip to content

Commit

Permalink
refactor(ui): handle swimUSD as stable coin
Browse files Browse the repository at this point in the history
  • Loading branch information
swimthor committed Oct 13, 2022
1 parent 897c682 commit c19821e
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion apps/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@solana/spl-token": "^0.3.5",
"@solana/web3.js": "^1.62.0",
"@swim-io/aptos": "^0.36.0",
"@swim-io/core": "^0.36.0",
"@swim-io/core": "workspace:^",
"@swim-io/evm": "^0.36.0",
"@swim-io/evm-contracts": "^0.36.0",
"@swim-io/pool-math": "^0.36.0",
Expand Down
3 changes: 2 additions & 1 deletion apps/ui/src/components/SwapForm/SwapForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ export const SwapForm = ({ maxSlippageFraction }: Props): ReactElement => {

const isLargeSwap = useIsLargeSwap(fromToken, toToken, inputAmount);
const isSmallEthSwap =
TOKEN_PROJECTS_BY_ID[fromToken.projectId].isStablecoin &&
(TOKEN_PROJECTS_BY_ID[fromToken.projectId].isStablecoin ||
TOKEN_PROJECTS_BY_ID[fromToken.projectId].isSwimUsd) &&
[fromToken.nativeEcosystemId, toToken.nativeEcosystemId].includes(
EvmEcosystemId.Ethereum,
) &&
Expand Down
3 changes: 2 additions & 1 deletion apps/ui/src/components/SwapFormV2/SwapFormV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export const SwapFormV2 = ({ maxSlippageFraction }: Props): ReactElement => {
inputAmount,
);
const isSmallEthSwap =
TOKEN_PROJECTS_BY_ID[fromTokenConfig.projectId].isStablecoin &&
(TOKEN_PROJECTS_BY_ID[fromTokenConfig.projectId].isStablecoin ||
TOKEN_PROJECTS_BY_ID[fromTokenConfig.projectId].isSwimUsd) &&
[fromTokenOption.ecosystemId, toTokenOption.ecosystemId].includes(
EvmEcosystemId.Ethereum,
) &&
Expand Down
6 changes: 4 additions & 2 deletions apps/ui/src/hooks/swim/useIsLargeSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ export const useIsLargeSwap = (
const inputPoolUsdValue = pools[0].poolUsdValue;
const outputPoolUsdValue = pools[pools.length - 1].poolUsdValue;
const outputAmount = useSwapOutputAmountEstimate(inputAmount, toToken);
const fromTokenProject = TOKEN_PROJECTS_BY_ID[fromToken.projectId];
const toTokenProject = TOKEN_PROJECTS_BY_ID[toToken.projectId];
return (
(TOKEN_PROJECTS_BY_ID[fromToken.projectId].isStablecoin &&
((fromTokenProject.isStablecoin || fromTokenProject.isSwimUsd) &&
inputPoolUsdValue !== null &&
inputAmount
.toHuman(SOLANA_ECOSYSTEM_ID)
.gt(inputPoolUsdValue.mul(0.1))) ||
(TOKEN_PROJECTS_BY_ID[toToken.projectId].isStablecoin &&
((toTokenProject.isStablecoin || toTokenProject.isSwimUsd) &&
outputPoolUsdValue !== null &&
outputAmount !== null &&
outputAmount.toHuman(SOLANA_ECOSYSTEM_ID).gt(outputPoolUsdValue.mul(0.1)))
Expand Down
8 changes: 6 additions & 2 deletions apps/ui/src/hooks/swim/useIsLargeSwapV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ export const useIsLargeSwapV2 = (
}
const inputBalance = sum(poolBalances[0]);
const outputBalance = sum(poolBalances[poolBalances.length - 1]);
const fromTokenProject =
TOKEN_PROJECTS_BY_ID[fromTokenOption.tokenConfig.projectId];
const toTokenProject =
TOKEN_PROJECTS_BY_ID[toTokenOption.tokenConfig.projectId];
return (
(TOKEN_PROJECTS_BY_ID[fromTokenOption.tokenConfig.projectId].isStablecoin &&
((fromTokenProject.isStablecoin || fromTokenProject.isSwimUsd) &&
inputBalance !== null &&
inputAmount.gt(inputBalance.mul(0.1))) ||
(TOKEN_PROJECTS_BY_ID[toTokenOption.tokenConfig.projectId].isStablecoin &&
((toTokenProject.isStablecoin || toTokenProject.isSwimUsd) &&
outputBalance !== null &&
outputAmount !== null &&
outputAmount.gt(outputBalance.mul(0.1)))
Expand Down
9 changes: 6 additions & 3 deletions apps/ui/src/hooks/swim/usePoolUsdValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ export const usePoolUsdValues = (poolSpecs: readonly PoolSpec[]) => {
poolTokens.some(
(tokenConfig) =>
!TOKEN_PROJECTS_BY_ID[tokenConfig.projectId].isStablecoin &&
!TOKEN_PROJECTS_BY_ID[tokenConfig.projectId].isSwimUsd &&
!prices.get(tokenConfig.id),
)
) {
return new Decimal(0);
}
return poolTokens.reduce((sum, tokenConfig, i) => {
const price = TOKEN_PROJECTS_BY_ID[tokenConfig.projectId].isStablecoin
? new Decimal(1)
: prices.get(tokenConfig.id) ?? new Decimal(1);
const price =
TOKEN_PROJECTS_BY_ID[tokenConfig.projectId].isStablecoin ||
TOKEN_PROJECTS_BY_ID[tokenConfig.projectId].isSwimUsd
? new Decimal(1)
: prices.get(tokenConfig.id) ?? new Decimal(1);
return sum.add(poolBalances[i].mul(price));
}, new Decimal(0));
}
Expand Down
3 changes: 2 additions & 1 deletion apps/ui/src/models/amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ export class Amount {
i18next.resolvedLanguage,
);
const numberFormatter = new Intl.NumberFormat(language, {
...(TOKEN_PROJECTS_BY_ID[this.tokenConfig.projectId].isStablecoin
...(TOKEN_PROJECTS_BY_ID[this.tokenConfig.projectId].isStablecoin ||
TOKEN_PROJECTS_BY_ID[this.tokenConfig.projectId].isSwimUsd
? {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
Expand Down
9 changes: 6 additions & 3 deletions apps/ui/src/models/swim/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,12 @@ export const getPoolUsdValue = (
tokens: readonly TokenConfig[],
poolTokenAccounts: readonly TokenAccount[],
): Decimal | null =>
tokens.every(
(tokenConfig) => TOKEN_PROJECTS_BY_ID[tokenConfig.projectId].isStablecoin,
)
tokens.every((tokenConfig) => {
return (
TOKEN_PROJECTS_BY_ID[tokenConfig.projectId].isStablecoin ||
TOKEN_PROJECTS_BY_ID[tokenConfig.projectId].isSwimUsd
);
})
? poolTokenAccounts.reduce((acc, account) => {
const tokenConfig = tokens.find(
(spec) =>
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7304,7 +7304,7 @@ __metadata:
"@storybook/node-logger": ^6.5.10
"@storybook/react": ^6.5.10
"@swim-io/aptos": ^0.36.0
"@swim-io/core": ^0.36.0
"@swim-io/core": "workspace:^"
"@swim-io/eslint-config": "workspace:^"
"@swim-io/evm": ^0.36.0
"@swim-io/evm-contracts": ^0.36.0
Expand Down

0 comments on commit c19821e

Please sign in to comment.