-
Notifications
You must be signed in to change notification settings - Fork 89
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
test(fiatAmount): test UsdPricesUpdater #3091
Changes from all commits
0436ad0
76925b3
3ff2993
36931aa
315d15a
32affd6
007afd6
cc1298a
3409abb
639f220
6e6eba5
e8ab945
245409d
7f353e3
4eb1f6d
1b339f5
d0019d6
3795e7d
7274ad1
960c22b
6e3b193
da2b879
22ed725
40ac98c
3601d0f
e97a8e6
1e18bbd
e5b0a4e
dc2b958
1554ecd
b4e7224
c95e7de
9fc2797
0ca263b
fa035e8
4955667
04908d6
ff84560
355671f
45b0615
7dea62f
10cc765
d5ef158
f24b689
ed61077
1263764
3f4db54
dddc86b
2bc7e50
04a1791
6ac4d6f
3a56435
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { SupportedChainId } from '@cowprotocol/cow-sdk' | ||
import { Currency, Percent, TradeType } from '@uniswap/sdk-core' | ||
import { Currency } from '@uniswap/sdk-core' | ||
|
||
import { WRAPPED_NATIVE_CURRENCY } from 'legacy/constants/tokens' | ||
import { ParsedQs } from 'qs' | ||
|
||
import TradeGp from './TradeGp' | ||
import { TOKEN_SHORTHANDS, WRAPPED_NATIVE_CURRENCY } from 'legacy/constants/tokens' | ||
import { isAddress } from 'legacy/utils' | ||
|
||
import { Field } from './actions' | ||
import { SwapState } from './reducer' | ||
|
||
export function isWrappingTrade( | ||
sellCurrency: Currency | null | undefined, | ||
|
@@ -20,44 +24,69 @@ export function isWrappingTrade( | |
) | ||
} | ||
|
||
export function logTradeDetails(trade: TradeGp | undefined, allowedSlippage: Percent) { | ||
// don't do anything outside of dev env | ||
if (!trade || process.env.NODE_ENV !== 'development') return | ||
|
||
const exactIn = trade.tradeType === TradeType.EXACT_INPUT | ||
|
||
// Log Exact In Trade info | ||
if (exactIn) { | ||
console.debug( | ||
`[SwapMod::[SELL] Trade Constructed]`, | ||
` | ||
Type: SELL | ||
========== | ||
Input Amount: ${trade.inputAmount.toExact()} | ||
Output Amount: ${trade.outputAmount.toExact()} | ||
========== | ||
Fee Amount [as SELL]: ${trade.fee?.feeAsCurrency?.toExact()} ${trade.inputAmount.currency.symbol} | ||
Fee Amount [as BUY]: ${ | ||
trade.outputAmountWithoutFee && trade.outputAmountWithoutFee.subtract(trade.outputAmount).toExact() | ||
} ${trade.outputAmount.currency.symbol} | ||
========== | ||
Minimum Received: ${trade.minimumAmountOut(allowedSlippage).toExact()} | ||
` | ||
) | ||
} else { | ||
// Log Exact Out Trade info | ||
console.debug( | ||
`[SwapMod::[BUY] Trade Constructed]`, | ||
` | ||
Type: BUY | ||
========= | ||
Input Amount [w/FEE]: ${trade.inputAmountWithFee.toExact()} | ||
Output Amount: ${trade.outputAmount.toExact()} | ||
========= | ||
Fee Amount [as SELL]: ${trade.fee?.feeAsCurrency?.toExact()} ${trade.inputAmount.currency.symbol} | ||
========= | ||
Maximum Sold: ${trade.fee?.feeAsCurrency && trade.maximumAmountIn(allowedSlippage).toExact()} | ||
` | ||
) | ||
function parseIndependentFieldURLParameter(urlParam: any): Field { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just moved functions from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To fix circular dependency problem There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return typeof urlParam === 'string' && urlParam.toLowerCase() === 'output' ? Field.OUTPUT : Field.INPUT | ||
} | ||
|
||
export function parseCurrencyFromURLParameter(urlParam: ParsedQs[string]): string { | ||
if (typeof urlParam === 'string') { | ||
const valid = isAddress(urlParam) | ||
if (valid) return valid | ||
const upper = urlParam.toUpperCase() | ||
if (upper === 'ETH') return 'ETH' | ||
if (upper in TOKEN_SHORTHANDS) return upper | ||
} | ||
return '' | ||
} | ||
|
||
export function parseTokenAmountURLParameter(urlParam: any): string { | ||
return typeof urlParam === 'string' && !isNaN(parseFloat(urlParam)) ? urlParam : '' | ||
} | ||
|
||
const ENS_NAME_REGEX = /^[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)?$/ | ||
const ADDRESS_REGEX = /^0x[a-fA-F0-9]{40}$/ | ||
|
||
export function validatedRecipient(recipient: any): string | null { | ||
if (typeof recipient !== 'string') return null | ||
const address = isAddress(recipient) | ||
if (address) return address | ||
if (ENS_NAME_REGEX.test(recipient)) return recipient | ||
if (ADDRESS_REGEX.test(recipient)) return recipient | ||
return null | ||
} | ||
|
||
export function queryParametersToSwapState( | ||
parsedQs: ParsedQs, | ||
defaultInputCurrency = '', | ||
chainId: number | null | ||
): SwapState { | ||
let inputCurrency = parseCurrencyFromURLParameter(parsedQs.inputCurrency) | ||
let outputCurrency = parseCurrencyFromURLParameter(parsedQs.outputCurrency) | ||
const typedValue = parseTokenAmountURLParameter(parsedQs.exactAmount) | ||
const independentField = parseIndependentFieldURLParameter(parsedQs.exactField) | ||
|
||
if (inputCurrency === '' && outputCurrency === '' && typedValue === '' && independentField === Field.INPUT) { | ||
// Defaults to having the wrapped native currency selected | ||
inputCurrency = defaultInputCurrency // 'ETH' // mod | ||
} else if (inputCurrency === outputCurrency) { | ||
// clear output if identical | ||
outputCurrency = '' | ||
} | ||
|
||
const recipient = validatedRecipient(parsedQs.recipient) | ||
const recipientAddress = validatedRecipient(parsedQs.recipientAddress) | ||
|
||
return { | ||
chainId: chainId || null, | ||
[Field.INPUT]: { | ||
currencyId: inputCurrency === '' ? null : inputCurrency ?? null, | ||
}, | ||
[Field.OUTPUT]: { | ||
currencyId: outputCurrency === '' ? null : outputCurrency ?? null, | ||
}, | ||
typedValue, | ||
independentField, | ||
recipient, | ||
recipientAddress, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { createStore } from 'jotai/vanilla' | ||
import { ReactNode } from 'react' | ||
|
||
import { CurrencyAmount } from '@uniswap/sdk-core' | ||
|
||
import { renderHook } from '@testing-library/react-hooks' | ||
import { JotaiTestProvider } from 'test-utils' | ||
|
||
import { WETH_GNOSIS_CHAIN } from 'legacy/utils/gnosis_chain/constants' | ||
|
||
import { useUsdAmount } from './useUsdAmount' | ||
|
||
import { usdRawPricesAtom, UsdRawPriceState } from '../state/usdRawPricesAtom' | ||
|
||
const WETH_RAW_PRICE_STATE: UsdRawPriceState = { | ||
updatedAt: Date.now(), | ||
price: 1650, | ||
currency: WETH_GNOSIS_CHAIN, | ||
isLoading: false, | ||
} | ||
|
||
function getWrapper() { | ||
const store = createStore() | ||
const initialValues = [[usdRawPricesAtom, { [WETH_GNOSIS_CHAIN.address.toLowerCase()]: WETH_RAW_PRICE_STATE }]] | ||
|
||
return { | ||
store, | ||
TestComponent: function ({ children }: { children: ReactNode }) { | ||
return ( | ||
<JotaiTestProvider store={store} initialValues={initialValues}> | ||
{children} | ||
</JotaiTestProvider> | ||
) | ||
}, | ||
} | ||
} | ||
|
||
describe('useUsdAmount', () => { | ||
const ONE_WETH = CurrencyAmount.fromRawAmount(WETH_GNOSIS_CHAIN, 1 * 10 ** WETH_GNOSIS_CHAIN.decimals) | ||
|
||
it('USD amount for 1 WETH should be 1650', async () => { | ||
const { TestComponent } = getWrapper() | ||
const { result } = renderHook( | ||
() => { | ||
return useUsdAmount(ONE_WETH) | ||
}, | ||
{ wrapper: TestComponent } | ||
) | ||
|
||
expect(result.current.value?.toExact()).toBe(WETH_RAW_PRICE_STATE.price?.toString()) | ||
}) | ||
}) |
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.
Here I broke Circular dependency.
reducer
andhooks
were in a circle