-
Notifications
You must be signed in to change notification settings - Fork 49
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
chore: impr ffee #2738
Merged
Merged
chore: impr ffee #2738
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
387 changes: 387 additions & 0 deletions
387
packages/swap/src/helpers/orders/getFrontendFee.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,387 @@ | ||
import {Balance} from '@yoroi/types' | ||
|
||
import {getFrontendFee} from './getFrontendFee' | ||
import {milkHoldersDiscountTiers} from '../../translators/constants' | ||
import {Quantities} from '../../utils/quantities' | ||
import {asQuantity} from '../../utils/asQuantity' | ||
|
||
describe('getFrontendFee', () => { | ||
const primaryTokenInfo: Balance.TokenInfo = { | ||
id: '', | ||
decimals: 6, | ||
description: 'primary', | ||
fingerprint: '', | ||
image: '', | ||
group: '', | ||
icon: '', | ||
kind: 'ft', | ||
name: 'ttADA', | ||
symbol: 'ttADA', | ||
ticker: 'ttADA', | ||
metadatas: {}, | ||
} | ||
|
||
const notPrimaryTokenAmount: Balance.Amount = { | ||
tokenId: 'not.primary.token', | ||
quantity: '99', | ||
} | ||
|
||
describe('selling side is primary token', () => { | ||
it('< 100 and whatever milk in balance', () => { | ||
// arrange | ||
const sellAmount: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(99_999_999), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: sellAmount, | ||
buyAmount: notPrimaryTokenAmount, | ||
sellInPrimaryTokenValue: sellAmount, | ||
milkBalance: '999999999999999999', | ||
buyInPrimaryTokenValue: sellAmount, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: Quantities.zero, | ||
}) | ||
}) | ||
|
||
it('>= 100 and milk in balance = 0', () => { | ||
// arrange | ||
const sellPrimaryAmountOver99: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: sellPrimaryAmountOver99, | ||
buyAmount: notPrimaryTokenAmount, | ||
sellInPrimaryTokenValue: sellPrimaryAmountOver99, | ||
milkBalance: Quantities.zero, | ||
buyInPrimaryTokenValue: sellPrimaryAmountOver99, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(1_050_000), // no milk, 100 ADA * 0.05% + 1 = 1.05 ADA | ||
}) | ||
}) | ||
|
||
it('>= 100 and milk in balance >= 100', () => { | ||
// arrange | ||
const sellPrimaryAmountOver99: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: sellPrimaryAmountOver99, | ||
buyAmount: notPrimaryTokenAmount, | ||
milkBalance: '499', | ||
sellInPrimaryTokenValue: sellPrimaryAmountOver99, | ||
buyInPrimaryTokenValue: sellPrimaryAmountOver99, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(1_025_000), // hold 100-499 milk, 100 ADA * 0.025% + 1 = 1.025 ADA | ||
}) | ||
}) | ||
|
||
it('>= 100 and milk in balance >= 500', () => { | ||
// arrange | ||
const sellPrimaryAmountOver99: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: sellPrimaryAmountOver99, | ||
buyAmount: notPrimaryTokenAmount, | ||
milkBalance: '500', | ||
sellInPrimaryTokenValue: sellPrimaryAmountOver99, | ||
buyInPrimaryTokenValue: sellPrimaryAmountOver99, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(1_020_000), // hold 500+ milk, 100 ADA * 0.020% + 1 = 1.02 ADA | ||
}) | ||
}) | ||
}) | ||
|
||
describe('buying side is primary token', () => { | ||
it('< 100 and whatever milk in balance', () => { | ||
// arrange | ||
const buyPrimaryTokenAmount: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(99_999_999), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: notPrimaryTokenAmount, | ||
buyAmount: buyPrimaryTokenAmount, | ||
sellInPrimaryTokenValue: buyPrimaryTokenAmount, | ||
milkBalance: '999999999999999999', | ||
buyInPrimaryTokenValue: buyPrimaryTokenAmount, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: Quantities.zero, | ||
}) | ||
}) | ||
|
||
it('>= 100 and milk in balance = 0', () => { | ||
// arrange | ||
const buyPrimaryAmountOver99: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: notPrimaryTokenAmount, | ||
buyAmount: buyPrimaryAmountOver99, | ||
sellInPrimaryTokenValue: buyPrimaryAmountOver99, | ||
milkBalance: Quantities.zero, | ||
buyInPrimaryTokenValue: buyPrimaryAmountOver99, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(1_050_000), // no milk, 100 ADA * 0.05% + 1 = 1.05 ADA | ||
}) | ||
}) | ||
|
||
it('>= 100 and milk in balance >= 100', () => { | ||
// arrange | ||
const buyPrimaryAmountOver99: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: notPrimaryTokenAmount, | ||
buyAmount: buyPrimaryAmountOver99, | ||
milkBalance: '499', | ||
sellInPrimaryTokenValue: buyPrimaryAmountOver99, | ||
buyInPrimaryTokenValue: buyPrimaryAmountOver99, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(1_025_000), // hold 100-499 milk, 100 ADA * 0.025% + 1 = 1.025 ADA | ||
}) | ||
}) | ||
|
||
it('>= 100 and milk in balance >= 500', () => { | ||
// arrange | ||
const buyPrimaryAmountOver99: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: notPrimaryTokenAmount, | ||
buyAmount: buyPrimaryAmountOver99, | ||
milkBalance: '500', | ||
sellInPrimaryTokenValue: buyPrimaryAmountOver99, | ||
buyInPrimaryTokenValue: buyPrimaryAmountOver99, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(1_020_000), // hold 500+ milk, 100 ADA * 0.020% + 1= 1.02 ADA | ||
}) | ||
}) | ||
}) | ||
it('should calc 0 fee if no tier', () => { | ||
// arrange | ||
const buyPrimaryAmountOver99: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: notPrimaryTokenAmount, | ||
buyAmount: buyPrimaryAmountOver99, | ||
milkBalance: '999999999999999', | ||
sellInPrimaryTokenValue: buyPrimaryAmountOver99, | ||
buyInPrimaryTokenValue: buyPrimaryAmountOver99, | ||
primaryTokenInfo, | ||
discountTiers: [], | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: Quantities.zero, | ||
}) | ||
}) | ||
|
||
it('should fallback - coverage only', () => { | ||
// arrange | ||
const buyPrimaryAmountOver99: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(1_000_000), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: notPrimaryTokenAmount, | ||
buyAmount: buyPrimaryAmountOver99, | ||
milkBalance: '999999999999999', | ||
sellInPrimaryTokenValue: buyPrimaryAmountOver99, | ||
buyInPrimaryTokenValue: buyPrimaryAmountOver99, | ||
primaryTokenInfo, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: Quantities.zero, | ||
}) | ||
}) | ||
|
||
// TODO: check with openswap | ||
describe('neither sell nor buy are primary token, it should use the value in ADA (paired)', () => { | ||
it('< 100 and whatever milk in balance', () => { | ||
// arrange | ||
const sellNotPrimaryAmount: Balance.Amount = { | ||
tokenId: 'not.primary.token', | ||
quantity: asQuantity(99_999_999), | ||
} | ||
const sellValueInPrimaryToken: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(99_999_999), | ||
} | ||
const buyValueInPrimaryToken: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(99_999_998), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: sellNotPrimaryAmount, | ||
buyAmount: notPrimaryTokenAmount, | ||
sellInPrimaryTokenValue: sellValueInPrimaryToken, | ||
milkBalance: '999999999999999999', | ||
buyInPrimaryTokenValue: buyValueInPrimaryToken, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: Quantities.zero, | ||
}) | ||
}) | ||
|
||
it('>= 100 and milk in balance = 0', () => { | ||
// arrange | ||
const sellNotPrimaryAmountOver99: Balance.Amount = { | ||
tokenId: 'not.primary.token', | ||
quantity: asQuantity(100_000_000), | ||
} | ||
const sellValueInPrimaryToken: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
const buyValueInPrimaryToken: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(99_999_998), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: sellNotPrimaryAmountOver99, | ||
buyAmount: notPrimaryTokenAmount, | ||
sellInPrimaryTokenValue: sellValueInPrimaryToken, | ||
milkBalance: Quantities.zero, | ||
buyInPrimaryTokenValue: buyValueInPrimaryToken, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(1_050_000), // no milk, 100 ADA * 0.05% + 1 = 1.05 ADA | ||
}) | ||
}) | ||
|
||
it('>= 100 and milk in balance >= 100 (buy side higher)', () => { | ||
// arrange | ||
const sellNotPrimaryAmountOver99: Balance.Amount = { | ||
tokenId: 'not.primary.token', | ||
quantity: asQuantity(100_000_000), | ||
} | ||
const sellValueInPrimaryToken: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(99_000_000), | ||
} | ||
const buyValueInPrimaryToken: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: sellNotPrimaryAmountOver99, | ||
buyAmount: notPrimaryTokenAmount, | ||
milkBalance: '499', | ||
sellInPrimaryTokenValue: sellValueInPrimaryToken, | ||
buyInPrimaryTokenValue: buyValueInPrimaryToken, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(1_025_000), // hold 100-499 milk, 100 ADA * 0.025% + 1= 1.025 ADA | ||
}) | ||
}) | ||
|
||
it('>= 100 and milk in balance >= 500 (50/50)', () => { | ||
// arrange | ||
const sellNotPrimaryAmountOver99: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
const sellValueInPrimaryToken: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
const buyValueInPrimaryToken: Balance.Amount = { | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(100_000_000), | ||
} | ||
// act | ||
const fee = getFrontendFee({ | ||
sellAmount: sellNotPrimaryAmountOver99, | ||
buyAmount: notPrimaryTokenAmount, | ||
milkBalance: '500', | ||
sellInPrimaryTokenValue: sellValueInPrimaryToken, | ||
buyInPrimaryTokenValue: buyValueInPrimaryToken, | ||
primaryTokenInfo, | ||
discountTiers: milkHoldersDiscountTiers, | ||
}) | ||
// assert | ||
expect(fee).toEqual({ | ||
tokenId: primaryTokenInfo.id, | ||
quantity: asQuantity(1_020_000), // hold 500+ milk, 100 ADA * 0.020% + 1 = 1.02 ADA | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From their docs,
new is optional
But we can add them if you like them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TS complains after BN 9.1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh, ok, I hadn't noticed anything but probably didn't do any install yet since 9.1 4 days ago