-
Notifications
You must be signed in to change notification settings - Fork 50
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: token list api source #2860
Changes from all commits
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,5 +1,5 @@ | ||
import {createMaterialTopTabNavigator} from '@react-navigation/material-top-tabs' | ||
import {useSwap, useSwapTokensByPairToken} from '@yoroi/swap' | ||
import {useSwap, useSwapTokensOnlyVerified} from '@yoroi/swap' | ||
import React from 'react' | ||
import {StyleSheet} from 'react-native' | ||
import {SafeAreaView} from 'react-native-safe-area-context' | ||
|
@@ -57,7 +57,7 @@ export const SwapTabNavigator = () => { | |
}, [aggregatorTokenId, lpTokenHeld, lpTokenHeldChanged]) | ||
|
||
// pre load swap tokens | ||
const {refetch} = useSwapTokensByPairToken('', {suspense: false, enabled: false}) | ||
const {refetch} = useSwapTokensOnlyVerified({suspense: false, enabled: false}) | ||
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. pre-fetch updated, until we migrate router for tan stack router |
||
React.useEffect(() => { | ||
refetch() | ||
}, [refetch]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,26 +73,22 @@ function containsOnlyValidChars(str?: string): boolean { | |
return typeof str === 'string' && validCharsRegex.test(str) | ||
} | ||
|
||
export const sortTokensByName = (a: Balance.Token, b: Balance.Token, wallet: YoroiWallet) => { | ||
const isValidNameA = containsOnlyValidChars(a.info.name) | ||
const isValidNameB = containsOnlyValidChars(b.info.name) | ||
const isValidTickerA = containsOnlyValidChars(a.info.ticker) | ||
const isValidTickerB = containsOnlyValidChars(b.info.ticker) | ||
export const sortTokensByName = (a: Balance.TokenInfo, b: Balance.TokenInfo, wallet: YoroiWallet) => { | ||
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. new endpoint returns TokenInfo not Token, adjusted |
||
const isValidNameA = containsOnlyValidChars(a.name) | ||
const isValidNameB = containsOnlyValidChars(b.name) | ||
const isValidTickerA = containsOnlyValidChars(a.ticker) | ||
const isValidTickerB = containsOnlyValidChars(b.ticker) | ||
|
||
const nameA = | ||
a.info.ticker?.toLocaleLowerCase() && isValidTickerA | ||
? a.info.ticker?.toLocaleLowerCase() | ||
: a.info.name.toLocaleLowerCase() | ||
a.ticker?.toLocaleLowerCase() && isValidTickerA ? a.ticker?.toLocaleLowerCase() : a.name.toLocaleLowerCase() | ||
|
||
const nameB = | ||
b.info.ticker?.toLocaleLowerCase() && isValidTickerB | ||
? b.info.ticker?.toLocaleLowerCase() | ||
: b.info.name.toLocaleLowerCase() | ||
b.ticker?.toLocaleLowerCase() && isValidTickerB ? b.ticker?.toLocaleLowerCase() : b.name.toLocaleLowerCase() | ||
|
||
const isBPrimary = b.info.ticker === wallet.primaryTokenInfo.ticker | ||
const isBPrimary = b.ticker === wallet.primaryTokenInfo.ticker | ||
if (isBPrimary) return 1 | ||
|
||
const isAPrimary = a.info.ticker === wallet.primaryTokenInfo.ticker | ||
const isAPrimary = a.ticker === wallet.primaryTokenInfo.ticker | ||
if (isAPrimary) return -1 | ||
|
||
if (!isValidNameA && isValidNameB) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
{ | ||
"npmClient": "yarn", | ||
"version": "independent" | ||
"version": "independent", | ||
"command": { | ||
"run": { | ||
"ignore": ["e2e/*"] | ||
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. ignoring e2e on lerna (it was rebuilding it) |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import { | |
getCompletedOrders, | ||
getOrders, // returns all orders for a given stake key hash. | ||
} from './orders' | ||
import {getTokenPairs} from './token-pairs' | ||
import {getTokens} from './tokens' | ||
import { | ||
CancelOrderRequest, | ||
|
@@ -95,13 +96,17 @@ export class OpenSwapApi { | |
) | ||
} | ||
|
||
public async getTokens({policyId = '', assetName = ''} = {}) { | ||
const tokens = await getTokens( | ||
public async getTokenPairs({policyId = '', assetName = ''} = {}) { | ||
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. getTokens is actually getTokenPairs |
||
const tokenPairs = await getTokenPairs( | ||
{network: this.network, client: this.client}, | ||
{policyId, assetName}, | ||
) | ||
|
||
return tokens | ||
return tokenPairs | ||
} | ||
|
||
public async getTokens() { | ||
return getTokens({network: this.network, client: this.client}) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {expect, describe, it, vi, Mocked} from 'vitest' | ||
import {getTokenPairs} from './token-pairs' | ||
import {axiosClient} from './config' | ||
|
||
vi.mock('./config.ts') | ||
|
||
describe('SwapTokenPairsApi', () => { | ||
it('should get all tokens based pairs', async () => { | ||
const mockAxios = axiosClient as Mocked<typeof axiosClient> | ||
mockAxios.get.mockImplementationOnce(() => | ||
Promise.resolve({status: 200, data: mockedGetTokenPairsResponse}), | ||
) | ||
|
||
const result = await getTokenPairs({network: 'mainnet', client: mockAxios}) | ||
|
||
expect(result).to.be.lengthOf(1) | ||
}) | ||
|
||
it('should return empty list on preprod network', async () => { | ||
const mockAxios = axiosClient as Mocked<typeof axiosClient> | ||
|
||
const result = await getTokenPairs({network: 'preprod', client: mockAxios}) | ||
|
||
expect(result).to.be.empty | ||
}) | ||
|
||
it('should throw error for invalid response', async () => { | ||
const mockAxios = axiosClient as Mocked<typeof axiosClient> | ||
mockAxios.get.mockImplementationOnce(() => Promise.resolve({status: 500})) | ||
expect(() => | ||
getTokenPairs({network: 'mainnet', client: mockAxios}), | ||
).rejects.toThrow('Failed to fetch token pairs') | ||
}) | ||
}) | ||
|
||
const mockedGetTokenPairsResponse = [ | ||
{ | ||
info: { | ||
supply: {total: '1000000000000', circulating: null}, | ||
status: 'unverified', | ||
image: 'ipfs://QmPzaykTy4yfutCtwv7nRUmgbQbA7euiThyy2i9fiFuDHX', | ||
imageIpfsHash: 'QmPzaykTy4yfutCtwv7nRUmgbQbA7euiThyy2i9fiFuDHX', | ||
symbol: 'ARGENT', | ||
minting: { | ||
type: 'time-lock-policy', | ||
blockchain: 'cardano', | ||
mintedBeforeSlotNumber: 91850718, | ||
}, | ||
mediatype: 'image/png', | ||
tokentype: 'token', | ||
description: 'ARGENT Token', | ||
totalsupply: 1000000000000, | ||
address: { | ||
policyId: 'c04f4200502a998e9eebafac0291a1f38008de3fe146d136946d8f4b', | ||
name: '415247454e54', | ||
}, | ||
decimalPlaces: 0, | ||
categories: [], | ||
}, | ||
price: { | ||
volume: {base: 0, quote: 0}, | ||
volumeChange: {base: 0, quote: 0}, | ||
volumeTotal: {base: 0, quote: 0}, | ||
volumeAggregator: {}, | ||
price: 0, | ||
askPrice: 0, | ||
bidPrice: 0, | ||
priceChange: {'24h': 0, '7d': 0}, | ||
quoteDecimalPlaces: 0, | ||
baseDecimalPlaces: 6, | ||
quoteAddress: { | ||
policyId: 'c04f4200502a998e9eebafac0291a1f38008de3fe146d136946d8f4b', | ||
name: '415247454e54', | ||
}, | ||
baseAddress: {policyId: '', name: ''}, | ||
price10d: [], | ||
}, | ||
}, | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {SWAP_API_ENDPOINTS} from './config' | ||
import type {ApiDeps, TokenPairsResponse} from './types' | ||
|
||
export async function getTokenPairs( | ||
deps: ApiDeps, | ||
{policyId = '', assetName = ''} = {}, | ||
): Promise<TokenPairsResponse> { | ||
const {network, client} = deps | ||
if (network === 'preprod') return [] | ||
|
||
const apiUrl = SWAP_API_ENDPOINTS[network].getTokenPairs | ||
const response = await client.get<TokenPairsResponse>('', { | ||
baseURL: apiUrl, | ||
params: { | ||
'base-policy-id': policyId, | ||
'base-tokenname': assetName, | ||
}, | ||
}) | ||
|
||
if (response.status !== 200) { | ||
throw new Error('Failed to fetch token pairs', {cause: response.data}) | ||
} | ||
|
||
return response.data | ||
} |
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.
changed the variant
swap
to don't list supply / pair