Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Tokens state #2823

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion apps/wallet-mobile/src/features/Swap/SwapNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,26 @@ export const SwapTabNavigator = () => {

// state data
const wallet = useSelectedWallet()
const {aggregatorTokenId, lpTokenHeldChanged, frontendFeeTiers, frontendFeeTiersChanged} = useSwap()
const {
aggregatorTokenId,
lpTokenHeldChanged,
frontendFeeTiers,
frontendFeeTiersChanged,
sellTokenInfoChanged,
primaryTokenInfoChanged,
} = useSwap()
const lpTokenHeld = useBalance({wallet, tokenId: aggregatorTokenId})

// initialize sell with / and primary token
React.useEffect(() => {
const ptInfo = {
decimals: wallet.primaryTokenInfo.decimals ?? 0,
id: wallet.primaryTokenInfo.id,
}
sellTokenInfoChanged(ptInfo)
primaryTokenInfoChanged(ptInfo)
}, [primaryTokenInfoChanged, sellTokenInfoChanged, wallet.primaryTokenInfo.decimals, wallet.primaryTokenInfo.id])

// update the fee tiers
React.useEffect(() => {
frontendFeeTiersChanged(frontendFeeTiers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ type SelectableTokenProps = {
const SelectableToken = ({wallet, token, walletTokenIds}: SelectableTokenProps) => {
const balanceAvailable = useBalance({wallet, tokenId: token.info.id})
const {closeSearch} = useSearch()
const {buyTokenIdChanged, orderData} = useSwap()
const {buyTokenInfoChanged, orderData} = useSwap()
const {
sellQuantity: {isTouched: isSellTouched},
buyTouched,
Expand All @@ -150,7 +150,10 @@ const SelectableToken = ({wallet, token, walletTokenIds}: SelectableTokenProps)
track.swapAssetToChanged({
to_asset: [{asset_name: token.info.name, asset_ticker: token.info.ticker, policy_id: token.info.group}],
})
buyTokenIdChanged(token.info.id)
buyTokenInfoChanged({
decimals: token.info.decimals ?? 0,
id: token.info.id,
})
buyTouched()
navigateTo.startSwap()
closeSearch()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const TokenList = () => {
type SelectableTokenProps = {disabled?: boolean; tokenInfo: Balance.TokenInfo; wallet: YoroiWallet}
const SelectableToken = ({tokenInfo, wallet}: SelectableTokenProps) => {
const {closeSearch} = useSearch()
const {sellTokenIdChanged, orderData} = useSwap()
const {sellTokenInfoChanged, orderData} = useSwap()
const {
buyQuantity: {isTouched: isBuyTouched},
sellTouched,
Expand All @@ -104,7 +104,10 @@ const SelectableToken = ({tokenInfo, wallet}: SelectableTokenProps) => {
from_asset: [{asset_name: tokenInfo.name, asset_ticker: tokenInfo.ticker, policy_id: tokenInfo.group}],
})
sellTouched()
sellTokenIdChanged(tokenInfo.id)
sellTokenInfoChanged({
id: tokenInfo.id,
decimals: tokenInfo.decimals ?? 0,
})
navigateTo.startSwap()
closeSearch()
}
Expand Down
42 changes: 14 additions & 28 deletions packages/swap/src/helpers/orders/costs/getFrontendFee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ describe('getFrontendFee', () => {
// act
const fee = getFrontendFee({
lpTokenHeld: {tokenId: 'lp.token', quantity: '999999999999999999'},
sellInPrimaryTokenValue: sell,
primaryTokenId,
ptAmount: sell,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand All @@ -43,9 +42,8 @@ describe('getFrontendFee', () => {
}
// act
const fee = getFrontendFee({
sellInPrimaryTokenValue: sellPrimaryAmountOver99,
ptAmount: sellPrimaryAmountOver99,
lpTokenHeld: {tokenId: 'lp.token', quantity: Quantities.zero},
primaryTokenId,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand All @@ -67,8 +65,7 @@ describe('getFrontendFee', () => {
// act
const fee = getFrontendFee({
lpTokenHeld: {tokenId: 'lp.token', quantity: '499'},
sellInPrimaryTokenValue: sellPrimaryAmountOver99,
primaryTokenId,
ptAmount: sellPrimaryAmountOver99,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand All @@ -90,8 +87,7 @@ describe('getFrontendFee', () => {
// act
const fee = getFrontendFee({
lpTokenHeld: {tokenId: 'lp.token', quantity: '500'},
sellInPrimaryTokenValue: sellPrimaryAmountOver99,
primaryTokenId,
ptAmount: sellPrimaryAmountOver99,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand All @@ -114,9 +110,8 @@ describe('getFrontendFee', () => {
}
// act
const fee = getFrontendFee({
sellInPrimaryTokenValue: buyPrimaryTokenAmount,
ptAmount: buyPrimaryTokenAmount,
lpTokenHeld: {tokenId: 'lp.token', quantity: '999999999999999999'},
primaryTokenId,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand All @@ -137,9 +132,8 @@ describe('getFrontendFee', () => {
}
// act
const fee = getFrontendFee({
sellInPrimaryTokenValue: buyPrimaryAmountOver99,
ptAmount: buyPrimaryAmountOver99,
lpTokenHeld: {tokenId: 'lp.token', quantity: Quantities.zero},
primaryTokenId,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand All @@ -161,8 +155,7 @@ describe('getFrontendFee', () => {
// act
const fee = getFrontendFee({
lpTokenHeld: {tokenId: 'lp.token', quantity: '499'},
sellInPrimaryTokenValue: buyPrimaryAmountOver99,
primaryTokenId,
ptAmount: buyPrimaryAmountOver99,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand All @@ -184,8 +177,7 @@ describe('getFrontendFee', () => {
// act
const fee = getFrontendFee({
lpTokenHeld: {tokenId: 'lp.token', quantity: '500'},
sellInPrimaryTokenValue: buyPrimaryAmountOver99,
primaryTokenId,
ptAmount: buyPrimaryAmountOver99,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand All @@ -207,8 +199,7 @@ describe('getFrontendFee', () => {
// act
const fee = getFrontendFee({
lpTokenHeld: {tokenId: 'lp.token', quantity: '999999999999999'},
sellInPrimaryTokenValue: buyPrimaryAmountOver99,
primaryTokenId,
ptAmount: buyPrimaryAmountOver99,
feeTiers: [],
})
// assert
Expand All @@ -230,8 +221,7 @@ describe('getFrontendFee', () => {
// act
const fee = getFrontendFee({
lpTokenHeld: {tokenId: 'lp.token', quantity: '999999999999999'},
sellInPrimaryTokenValue: buyPrimaryAmountOver99,
primaryTokenId,
ptAmount: buyPrimaryAmountOver99,
feeTiers: [
{
fixedFee: asQuantity(3_000_000),
Expand Down Expand Up @@ -265,9 +255,8 @@ describe('getFrontendFee', () => {
}
// act
const fee = getFrontendFee({
sellInPrimaryTokenValue: sellValueInPrimaryToken,
ptAmount: sellValueInPrimaryToken,
lpTokenHeld: {tokenId: 'lp.token', quantity: '999999999999999999'},
primaryTokenId,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand All @@ -287,9 +276,8 @@ describe('getFrontendFee', () => {
}
// act
const fee = getFrontendFee({
sellInPrimaryTokenValue: sellValueInPrimaryToken,
ptAmount: sellValueInPrimaryToken,
lpTokenHeld: {tokenId: 'lp.token', quantity: Quantities.zero},
primaryTokenId,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand All @@ -311,8 +299,7 @@ describe('getFrontendFee', () => {
// act
const fee = getFrontendFee({
lpTokenHeld: {tokenId: 'lp.token', quantity: '499'},
sellInPrimaryTokenValue: sellValueInPrimaryToken,
primaryTokenId,
ptAmount: sellValueInPrimaryToken,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand All @@ -334,8 +321,7 @@ describe('getFrontendFee', () => {
// act
const fee = getFrontendFee({
lpTokenHeld: {tokenId: 'lp.token', quantity: '500'},
sellInPrimaryTokenValue: sellValueInPrimaryToken,
primaryTokenId,
ptAmount: sellValueInPrimaryToken,
feeTiers: milkHoldersDiscountTiers,
})
// assert
Expand Down
12 changes: 5 additions & 7 deletions packages/swap/src/helpers/orders/costs/getFrontendFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ import {asQuantity} from '../../../utils/asQuantity'
*/
export const getFrontendFee = ({
lpTokenHeld,
primaryTokenId,
sellInPrimaryTokenValue,
ptAmount,
feeTiers,
}: {
primaryTokenId: Balance.TokenInfo['id']
lpTokenHeld?: Balance.Amount
feeTiers: ReadonlyArray<App.FrontendFeeTier>
sellInPrimaryTokenValue: Balance.Amount
ptAmount: Balance.Amount
}): Readonly<{
fee: Balance.Amount
discountTier: App.FrontendFeeTier | undefined
Expand All @@ -36,21 +34,21 @@ export const getFrontendFee = ({
tier.secondaryTokenBalanceThreshold,
) &&
Quantities.isGreaterThanOrEqualTo(
sellInPrimaryTokenValue.quantity,
ptAmount.quantity,
tier.primaryTokenValueThreshold,
),
)

// calculate the fee
const fee = asQuantity(
new BigNumber(sellInPrimaryTokenValue.quantity)
new BigNumber(ptAmount.quantity)
.times(discountTier?.variableFeeMultiplier ?? 0)
.integerValue(BigNumber.ROUND_CEIL)
.plus(discountTier?.fixedFee ?? 0),
)

return {
fee: {tokenId: primaryTokenId, quantity: fee},
fee: {tokenId: ptAmount.tokenId, quantity: fee},
discountTier,
} as const
}
Loading