Skip to content

Commit

Permalink
wip: clean ups - get pt price
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed Oct 9, 2023
1 parent c41fea9 commit b3fee39
Show file tree
Hide file tree
Showing 37 changed files with 272 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ storiesOf('Swap List Pool', module)
ptPriceTokenB: '0',
fee: '0.3', // 0.3%
provider: 'minswap',
price: 2,
batcherFee: {quantity: '1', tokenId: ''},
deposit: {quantity: '1', tokenId: ''},
poolId: '0',
Expand All @@ -38,7 +37,6 @@ storiesOf('Swap List Pool', module)
ptPriceTokenB: '0',
fee: '0.3', // 0.3%
provider: 'sundaeswap',
price: 6,
batcherFee: {quantity: '122', tokenId: ''},
deposit: {quantity: '432', tokenId: ''},
poolId: '23455',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import {useSwap} from '@yoroi/swap'
import {Swap} from '@yoroi/types'
import {Balance, Swap} from '@yoroi/types'
import BigNumber from 'bignumber.js'
import React, {useState} from 'react'
import {StyleSheet, Text, TouchableOpacity, View} from 'react-native'
import LinearGradient from 'react-native-linear-gradient'

import {Spacer} from '../../../../../components'
import {Boundary, Spacer} from '../../../../../components'
import {useMetrics} from '../../../../../metrics/metricsManager'
import {useSelectedWallet} from '../../../../../SelectedWallet'
import {COLORS} from '../../../../../theme'
import {YoroiWallet} from '../../../../../yoroi-wallets/cardano/types'
import {useTokenInfo} from '../../../../../yoroi-wallets/hooks'
import {Quantities} from '../../../../../yoroi-wallets/utils'
import {useNavigateTo} from '../../navigation'
import {PoolIcon} from '../../PoolIcon/PoolIcon'
import {useStrings} from '../../strings'
Expand All @@ -33,74 +37,109 @@ export const SelectPoolFromList = ({pools = []}: Props) => {
navigate.startSwap()
}

const decimals = wallet.primaryTokenInfo.decimals ?? 0
const ticker = wallet.primaryTokenInfo.ticker

const protocolCapitalize = (protocol: string): string => protocol[0].toUpperCase() + protocol.substring(1)

return (
<View style={styles.container}>
{pools.map((pool) => (
<View key={pool.poolId}>
<Spacer height={16} />

<View style={[styles.shadowProp]}>
<LinearGradient
colors={pool.poolId === selectedCardIndex ? ['#E4E8F7', '#C6F7F7'] : [COLORS.WHITE, COLORS.WHITE]}
style={styles.linearGradient}
>
<TouchableOpacity key={pool.poolId} onPress={() => handleCardSelect(pool)} style={[styles.card]}>
<View style={styles.cardHeader}>
<View style={styles.icon}>
<PoolIcon size={40} providerId={pool.provider} />
</View>

<Text style={styles.label}>{protocolCapitalize(pool.provider)}</Text>
</View>
{pools.map((pool) => {
const formattedDepositInPt = `${Quantities.format(pool.deposit.quantity, decimals, decimals)} ${ticker}`
const formattedBatcherFeeInPt = `${Quantities.format(pool.batcherFee.quantity, decimals, decimals)} ${ticker}`

return (
<View key={pool.poolId}>
<Spacer height={16} />

<View style={[styles.shadowProp]}>
<LinearGradient
colors={pool.poolId === selectedCardIndex ? ['#E4E8F7', '#C6F7F7'] : [COLORS.WHITE, COLORS.WHITE]}
style={styles.linearGradient}
>
<TouchableOpacity key={pool.poolId} onPress={() => handleCardSelect(pool)} style={[styles.card]}>
<View style={styles.cardHeader}>
<View style={styles.icon}>
<PoolIcon size={40} providerId={pool.provider} />
</View>

<View style={styles.infoContainer}>
<View>
<Spacer height={8} />
<Text style={styles.label}>{protocolCapitalize(pool.provider)}</Text>
</View>

<View style={styles.info}>
<Text style={styles.infoLabel}>{strings.price}</Text>
<View style={styles.infoContainer}>
<View>
<Spacer height={8} />

<Text style={styles.infoValue}>{`${pool.price} ${wallet.primaryTokenInfo.ticker}`}</Text>
<Boundary>
<PriceInAda pool={pool} wallet={wallet} sell={orderData.amounts.sell} />
</Boundary>
</View>
</View>

<View>
<Spacer height={8} />
<View>
<Spacer height={8} />

<View style={styles.info}>
<Text style={styles.infoLabel}>{`${strings.tvl}, ${wallet.primaryTokenInfo.ticker}`}</Text>
<View style={styles.info}>
<Text style={styles.infoLabel}>{`${strings.tvl}, ${wallet.primaryTokenInfo.ticker}`}</Text>

<Text style={styles.infoValue}>{pool.deposit.quantity}</Text>
<Text style={styles.infoValue}>{formattedDepositInPt}</Text>
</View>
</View>
</View>

<View>
<Spacer height={8} />
<View>
<Spacer height={8} />

<View style={styles.info}>
<Text style={styles.infoLabel}>{strings.poolFee}, %</Text>
<View style={styles.info}>
<Text style={styles.infoLabel}>{strings.poolFee}, %</Text>

<Text style={styles.infoValue}>{pool.fee}%</Text>
<Text style={styles.infoValue}>{pool.fee}%</Text>
</View>
</View>
</View>

<View>
<Spacer height={8} />
<View>
<Spacer height={8} />

<View style={styles.info}>
<Text style={styles.infoLabel}>{`${strings.batcherFee}, ${wallet.primaryTokenInfo.ticker}`}</Text>
<View style={styles.info}>
<Text
style={styles.infoLabel}
>{`${strings.batcherFee}, ${wallet.primaryTokenInfo.ticker}`}</Text>

<Text style={styles.infoValue}>{pool.batcherFee.quantity}</Text>
<Text style={styles.infoValue}>{formattedBatcherFeeInPt}</Text>
</View>
</View>
</View>
</View>
</TouchableOpacity>
</LinearGradient>
</TouchableOpacity>
</LinearGradient>
</View>
</View>
</View>
))}
)
})}
</View>
)
}

type PriceInAdaProps = {pool: Swap.Pool; wallet: YoroiWallet; sell: Balance.Amount}
const PriceInAda = ({pool, wallet, sell}: PriceInAdaProps) => {
const strings = useStrings()

const {decimals: decimalsA = 0}= useTokenInfo({wallet, tokenId: pool.tokenA.tokenId})
const {decimals: decimalsB = 0}= useTokenInfo({wallet, tokenId: pool.tokenB.tokenId})

const scaleA = new BigNumber(10).pow(decimalsA)
const scaleB = new BigNumber(10).pow(decimalsB)

const isSellTokenA = pool.tokenA.tokenId === sell.tokenId
const [dividend, divisor] = isSellTokenA
? [new BigNumber(pool.ptPriceTokenB).multipliedBy(scaleB), new BigNumber(pool.ptPriceTokenA).multipliedBy(scaleA)]
: [new BigNumber(pool.ptPriceTokenA).multipliedBy(scaleA), new BigNumber(pool.ptPriceTokenB).multipliedBy(scaleB)]
// limit decimals
const ptPrice = divisor.isZero() ? '0' : dividend.dividedBy(divisor).toFixed(Math.max(decimalsA, decimalsB), BigNumber.ROUND_DOWN)
// const price = parsedPrice != null ? asQuantity(parsedPrice) : Quantities.zero
// const formattedPriceInPt = `${Quantities.format(price, 0, decimals)} ${ticker}`
return (
<View style={styles.info}>
<Text style={styles.infoLabel}>{strings.price}</Text>

<Text style={styles.infoValue}>{ptPrice}</Text>
</View>
)
}
Expand Down
1 change: 0 additions & 1 deletion apps/wallet-mobile/src/features/Swap/common/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export const mocks = {
tokenId: '0029cb7c88c7567b63d1a512c0ed626aa169688ec980730c0473b913.6c702083',
},
poolId: '0029cb7c88c7567b63d1a512c0ed626aa169688ec980730c0473b913.702083',
price: 0.0890390378168252,
provider: 'sundaeswap',
tokenA: {quantity: asQuantity(20630071), tokenId: ''},
tokenB: {
Expand Down
2 changes: 1 addition & 1 deletion apps/wallet-mobile/src/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
"swap.swapScreen.enterSlippage": "Enter a value from 0% to 100%. You can also enter up to 1 decimal",
"swap.swapScreen.poolVerification": "{pool} verification",
"swap.swapScreen.volume": "Volume, 24h",
"swap.swapScreen.tvl": " TVL",
"swap.swapScreen.tvl": "TVL",
"swap.listOrders.completed": "completed orders",
"swap.listOrders.open": "open orders",
"swap.listOrders.sheet.title": "Confirm order cancelation",
Expand Down
4 changes: 0 additions & 4 deletions packages/swap/src/adapters/openswap-api/api.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const createOrderData: Swap.CreateOrderData = {
tokenB: {tokenId: '', quantity: '1000000000'},
ptPriceTokenA: '0',
ptPriceTokenB: '0',
price: 0,
batcherFee: {tokenId: '', quantity: '0'},
deposit: {tokenId: '', quantity: '2000000'},
poolId:
Expand Down Expand Up @@ -78,7 +77,6 @@ const getPools: Swap.Pool[] = [
},
batcherFee: {quantity: '2000000', tokenId: ''},
fee: '0.3',
price: 0,
poolId:
'0be55d262b29f564998ff81efe21bdc0022621c12f15af08d0f2ddb1.7339a8bcda85e2c997d9f16beddbeb3ad755f5202f5cfd9cb08db346a1292c01',
provider: 'minswap',
Expand All @@ -100,7 +98,6 @@ const getPools: Swap.Pool[] = [
},
batcherFee: {quantity: '2000000', tokenId: ''},
fee: '0.3',
price: 0,
poolId:
'0be55d262b29f564998ff81efe21bdc0022621c12f15af08d0f2ddb1.7339a8bcda85e2c997d9f16beddbeb3ad755f5202f5cfd9cb08db346a1292c01',
provider: 'sundaeswap',
Expand All @@ -122,7 +119,6 @@ const getPools: Swap.Pool[] = [
},
batcherFee: {quantity: '2000000', tokenId: ''},
fee: '0.3',
price: 0,
poolId:
'0be55d262b29f564998ff81efe21bdc0022621c12f15af08d0f2ddb1.7339a8bcda85e2c997d9f16beddbeb3ad755f5202f5cfd9cb08db346a1292c01',
provider: 'sundaeswap',
Expand Down
Loading

0 comments on commit b3fee39

Please sign in to comment.