Skip to content

Commit

Permalink
[COW-SDK] Add legacy types (#567)
Browse files Browse the repository at this point in the history
* add cow-sdk package

* replace SupportedChainId with sdk chain and change xdai > gnosis_chain

* update to latest sdk

* leave commented out mod

* legacy quote information types

* add legacy types to price utils

* use legacy types in price actions/reducer

* add Legacy types

* fix merge issues
  • Loading branch information
W3stside authored Jun 3, 2022
1 parent 516847b commit 77795fd
Show file tree
Hide file tree
Showing 18 changed files with 239 additions and 238 deletions.
4 changes: 2 additions & 2 deletions src/custom/api/coingecko/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SWR_OPTIONS } from 'constants/index'
import { PriceInformation } from '@cowprotocol/cow-sdk'
import { SupportedChainId as ChainId } from 'constants/chains'
import { SWR_OPTIONS } from 'constants/index'
import useSWR from 'swr'
import { PriceInformation } from 'utils/price'

function getApiUrl(): string {
// it's all the same base url
Expand Down
3 changes: 2 additions & 1 deletion src/custom/api/gnosisProtocol/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import QuoteError, {
mapOperatorErrorToQuoteError,
} from 'api/gnosisProtocol/errors/QuoteError'
import { toErc20Address, toNativeBuyAddress } from 'utils/tokens'
import { FeeQuoteParams, PriceInformation, PriceQuoteParams, SimpleGetQuoteResponse } from 'utils/price'
import { LegacyFeeQuoteParams as FeeQuoteParams, LegacyPriceQuoteParams as PriceQuoteParams } from './legacy/types'

import { DEFAULT_NETWORK_FOR_LISTS } from 'constants/lists'
import * as Sentry from '@sentry/browser'
Expand All @@ -32,6 +32,7 @@ import { ZERO_ADDRESS } from 'constants/misc'
import { getAppDataHash } from 'constants/appDataHash'
import { GpPriceStrategy } from 'hooks/useGetGpPriceStrategy'
import { Context } from '@sentry/types'
import { PriceInformation, SimpleGetQuoteResponse } from '@cowprotocol/cow-sdk'

function getGnosisProtocolUrl(): Partial<Record<ChainId, string>> {
if (isLocal || isDev || isPr || isBarn) {
Expand Down
41 changes: 41 additions & 0 deletions src/custom/api/gnosisProtocol/legacy/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
FeeInformation,
FeeQuoteParams,
PriceInformation,
PriceQuoteParams,
SupportedChainId as ChainId,
} from '@cowprotocol/cow-sdk'
import { GpPriceStrategy } from 'hooks/useGetGpPriceStrategy'

export interface LegacyQuoteParams {
quoteParams: LegacyFeeQuoteParams
strategy: GpPriceStrategy
fetchFee: boolean
previousFee?: FeeInformation
isPriceRefresh: boolean
}

export class LegacyPriceQuoteError extends Error {
params: LegacyPriceQuoteParams
results: PromiseSettledResult<any>[]

constructor(message: string, params: LegacyPriceQuoteParams, results: PromiseSettledResult<any>[]) {
super(message)
this.params = params
this.results = results
}
}

export type LegacyFeeQuoteParams = FeeQuoteParams & {
fromDecimals: number
toDecimals: number
chainId: ChainId
priceQuality?: string
isBestQuote?: boolean
}

export type LegacyPriceQuoteParams = Omit<LegacyFeeQuoteParams, 'sellToken' | 'buyToken'> & PriceQuoteParams

export type LegacyPriceSource = 'gnosis-protocol' | 'paraswap' | 'matcha-0x'
export type LegacyPriceInformationWithSource = PriceInformation & { source: LegacyPriceSource; data?: any }
export type LegacyPromiseRejectedResultWithSource = PromiseRejectedResult & { source: LegacyPriceSource }
6 changes: 4 additions & 2 deletions src/custom/api/matcha-0x/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { OrderKind } from '@cowprotocol/contracts'
import { NetworkID } from 'paraswap'
import { SupportedChainId as ChainId } from 'constants/chains'
import { getTokensFromMarket } from 'utils/misc'
import { getValidParams, PriceInformation, PriceQuoteParams } from 'utils/price'
import { getValidParams } from 'utils/price'
import { LegacyPriceQuoteParams } from 'api/gnosisProtocol/legacy/types'
import { PriceInformation } from '@cowprotocol/cow-sdk'

// copy/pasting as the library types correspond to the internal types, not API response
// e.g "price: BigNumber" when we want the API response type: "price: string"
Expand Down Expand Up @@ -104,7 +106,7 @@ function _get(chainId: ChainId, url: string): Promise<Response> {
return _fetch(chainId, url, 'GET')
}

export async function getPriceQuote(params: PriceQuoteParams): Promise<MatchaPriceQuote | null> {
export async function getPriceQuote(params: LegacyPriceQuoteParams): Promise<MatchaPriceQuote | null> {
const { baseToken, quoteToken, amount, kind, chainId } = getValidParams(params)

const networkId = getMatchaChainId(chainId)
Expand Down
6 changes: 4 additions & 2 deletions src/custom/api/paraswap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { OptimalRate } from 'paraswap-core'
import { APIError, RateOptions } from 'paraswap/build/types'
import { SupportedChainId as ChainId } from 'constants/chains'
import { getTokensFromMarket } from 'utils/misc'
import { getValidParams, PriceInformation, PriceQuoteParams } from 'utils/price'
import { getValidParams } from 'utils/price'
import { SOLVER_ADDRESS as defaultUserAddress } from 'constants/index'
import { PriceInformation } from '@cowprotocol/cow-sdk'
import { LegacyPriceQuoteParams } from 'api/gnosisProtocol/legacy/types'

type ParaSwapPriceQuote = OptimalRate

Expand Down Expand Up @@ -67,7 +69,7 @@ function getPriceQuoteFromError(error: APIError): ParaSwapPriceQuote | null {
}
}

export async function getPriceQuote(params: PriceQuoteParams): Promise<ParaSwapPriceQuote | null> {
export async function getPriceQuote(params: LegacyPriceQuoteParams): Promise<ParaSwapPriceQuote | null> {
const { baseToken, quoteToken, fromDecimals, toDecimals, amount, kind, chainId, userAddress } = getValidParams(params)

let paraSwap = paraSwapLibs.get(chainId)
Expand Down
7 changes: 5 additions & 2 deletions src/custom/hooks/usePriceImpact/useFallbackPriceImpact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import { useSwapState } from 'state/swap/hooks'

import useExactInSwap, { useCalculateQuote } from './useQuoteAndSwap'
import { FallbackPriceImpactParams } from './types'
import { calculateFallbackPriceImpact, FeeQuoteParams } from 'utils/price'
import { calculateFallbackPriceImpact } from 'utils/price'
import TradeGp from 'state/swap/TradeGp'
import { QuoteInformationObject } from 'state/price/reducer'
import { QuoteError } from 'state/price/actions'
import { useQuote } from 'state/price/hooks'
import { useActiveWeb3React } from 'hooks/web3'
import { LegacyFeeQuoteParams } from 'api/gnosisProtocol/legacy/types'

type SwapParams = { abTrade?: TradeGp; sellToken?: string | null; buyToken?: string | null }

function _isQuoteValid(quote: QuoteInformationObject | FeeQuoteParams | undefined): quote is QuoteInformationObject {
function _isQuoteValid(
quote: QuoteInformationObject | LegacyFeeQuoteParams | undefined
): quote is QuoteInformationObject {
return Boolean(quote && 'lastCheck' in quote)
}

Expand Down
7 changes: 4 additions & 3 deletions src/custom/hooks/usePriceImpact/useQuoteAndSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useActiveWeb3React } from 'hooks/web3'

import { getPromiseFulfilledValue, isPromiseFulfilled } from 'utils/misc'
import { supportedChainId } from 'utils/supportedChainId'
import { FeeQuoteParams, getBestQuote, QuoteResult } from 'utils/price'
import { getBestQuote, QuoteResult } from 'utils/price'

import { ZERO_ADDRESS } from 'constants/misc'
import { SupportedChainId } from 'constants/chains'
Expand All @@ -19,6 +19,7 @@ import { QuoteError } from 'state/price/actions'
import { isWrappingTrade } from 'state/swap/utils'
import useGetGpPriceStrategy from 'hooks/useGetGpPriceStrategy'
import { onlyResolvesLast } from 'utils/async'
import { LegacyFeeQuoteParams } from 'api/gnosisProtocol/legacy/types'

type WithLoading = { loading: boolean; setLoading: (state: boolean) => void }

Expand All @@ -37,7 +38,7 @@ type GetQuoteParams = {
validTo?: number
} & WithLoading

type FeeQuoteParamsWithError = FeeQuoteParams & { error?: QuoteError }
type FeeQuoteParamsWithError = LegacyFeeQuoteParams & { error?: QuoteError }

const getBestQuoteResolveOnlyLastCall = onlyResolvesLast<QuoteResult>(getBestQuote)

Expand Down Expand Up @@ -78,7 +79,7 @@ export function useCalculateQuote(params: GetQuoteParams) {
chainId: chainId || SupportedChainId.MAINNET,
validTo,
}
let quoteData: QuoteInformationObject | FeeQuoteParams = quoteParams
let quoteData: QuoteInformationObject | LegacyFeeQuoteParams = quoteParams
getBestQuoteResolveOnlyLastCall({
strategy,
quoteParams,
Expand Down
9 changes: 5 additions & 4 deletions src/custom/hooks/useRefetchPriceCallback.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react'

import { FeeQuoteParams, getBestQuote, getFastQuote, QuoteParams, QuoteResult } from 'utils/price'
import { getBestQuote, getFastQuote, QuoteResult } from 'utils/price'
import { isValidOperatorError, ApiErrorCodes } from 'api/gnosisProtocol/errors/OperatorError'
import GpQuoteError, {
GpQuoteErrorCodes,
Expand All @@ -23,14 +23,15 @@ import { CancelableResult, onlyResolvesLast } from 'utils/async'
import useGetGpPriceStrategy from 'hooks/useGetGpPriceStrategy'
import { calculateValidTo } from 'hooks/useSwapCallback'
import { useUserTransactionTTL } from 'state/user/hooks'
import { LegacyFeeQuoteParams, LegacyQuoteParams } from 'api/gnosisProtocol/legacy/types'

interface HandleQuoteErrorParams {
quoteData: QuoteInformationObject | FeeQuoteParams
quoteData: QuoteInformationObject | LegacyFeeQuoteParams
error: unknown
addUnsupportedToken: (params: AddGpUnsupportedTokenParams) => void
}

type QuoteParamsForFetching = Omit<QuoteParams, 'strategy'>
type QuoteParamsForFetching = Omit<LegacyQuoteParams, 'strategy'>

export function handleQuoteError({ quoteData, error, addUnsupportedToken }: HandleQuoteErrorParams): QuoteError {
if (isValidOperatorError(error)) {
Expand Down Expand Up @@ -143,7 +144,7 @@ export function useRefetchQuoteCallback() {
// set the validTo time here
quoteParams.validTo = calculateValidTo(deadline)

let quoteData: FeeQuoteParams | QuoteInformationObject = quoteParams
let quoteData: LegacyFeeQuoteParams | QuoteInformationObject = quoteParams

// price can be null if fee > price
const handleResponse = (response: CancelableResult<QuoteResult>, isBestQuote: boolean) => {
Expand Down
2 changes: 1 addition & 1 deletion src/custom/state/lists/reducer/reducerMod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const EMPTY_STORE = {
activeListUrls: undefined,
gpUnsupportedTokens: {},
},
[ChainId.XDAI]: {
[ChainId.GNOSIS_CHAIN]: {
byUrl: {},
activeListUrls: undefined,
gpUnsupportedTokens: {},
Expand Down
3 changes: 2 additions & 1 deletion src/custom/state/orders/updaters/UnfillableOrdersUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { PENDING_ORDERS_PRICE_CHECK_POLL_INTERVAL } from 'state/orders/consts'

import { SupportedChainId as ChainId } from 'constants/chains'

import { getBestQuote, PriceInformation } from 'utils/price'
import { getBestQuote } from 'utils/price'
import { isOrderUnfillable } from 'state/orders/utils'
import useGetGpPriceStrategy, { GpPriceStrategy } from 'hooks/useGetGpPriceStrategy'
import { getPromiseFulfilledValue } from 'utils/misc'
import { PriceInformation } from '@cowprotocol/cow-sdk'

/**
* Thin wrapper around `getBestPrice` that builds the params and returns null on failure
Expand Down
2 changes: 1 addition & 1 deletion src/custom/state/orders/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @jest-environment ./custom-test-env.js
*/

import { PriceInformation } from 'utils/price'
import { PriceInformation } from '@cowprotocol/cow-sdk'
import { OrderKind } from 'state/orders/actions'

import { USDC_MAINNET as USDC, USDT } from 'constants/tokens'
Expand Down
2 changes: 1 addition & 1 deletion src/custom/state/orders/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { ONE_HUNDRED_PERCENT } from 'constants/misc'
import { PENDING_ORDERS_BUFFER } from 'constants/index'
import { OrderMetaData } from 'api/gnosisProtocol'
import { Order } from 'state/orders/actions'
import { PriceInformation } from 'utils/price'
import { OUT_OF_MARKET_PRICE_DELTA_PERCENTAGE } from 'state/orders/consts'
import { calculatePrice, invertPrice, ZERO_BIG_NUMBER } from '@cowprotocol/cow-js'
import { BigNumber } from 'bignumber.js'
import { PriceInformation } from '@cowprotocol/cow-sdk'

export type OrderTransitionStatus =
| 'unknown'
Expand Down
4 changes: 2 additions & 2 deletions src/custom/state/price/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createAction } from '@reduxjs/toolkit'
import { FeeQuoteParams } from 'utils/price'
import { LegacyFeeQuoteParams } from 'api/gnosisProtocol/legacy/types'
import { SupportedChainId as ChainId } from 'constants/chains'
import { QuoteInformationObject } from './reducer'

Expand All @@ -10,7 +10,7 @@ export interface ClearQuoteParams {
chainId: ChainId
}

export type GetQuoteParams = FeeQuoteParams
export type GetQuoteParams = LegacyFeeQuoteParams
export type RefreshQuoteParams = Pick<GetQuoteParams, 'sellToken' | 'chainId'>

export type QuoteError =
Expand Down
5 changes: 3 additions & 2 deletions src/custom/state/price/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { OrderKind } from '@cowprotocol/contracts'
import { updateQuote, setQuoteError, getNewQuote, refreshQuote, QuoteError } from './actions'
import { Writable } from 'custom/types'
import { PrefillStateRequired } from '../orders/reducer'
import { FeeInformation, FeeQuoteParams, PriceInformation } from 'utils/price'
import { LegacyFeeQuoteParams } from 'api/gnosisProtocol/legacy/types'
import { FeeInformation, PriceInformation } from '@cowprotocol/cow-sdk'

// API Doc: https://protocol-rinkeby.dev.gnosisdev.com/api

Expand All @@ -13,7 +14,7 @@ export const EMPTY_FEE = {
amount: '0',
}

export interface QuoteInformationObject extends FeeQuoteParams {
export interface QuoteInformationObject extends LegacyFeeQuoteParams {
fee?: FeeInformation
price?: PriceInformation
error?: QuoteError
Expand Down
4 changes: 2 additions & 2 deletions src/custom/state/price/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useState } from 'react'
import { DEFAULT_DECIMALS } from 'custom/constants'

import { UnsupportedToken } from 'api/gnosisProtocol'
import { FeeQuoteParams as FeeQuoteParamsFull } from 'utils/price'
import { LegacyFeeQuoteParams as LegacyFeeQuoteParamsFull } from 'api/gnosisProtocol/legacy/types'
import { OrderKind } from '@cowprotocol/contracts'

import { useSwapState } from 'state/swap/hooks'
Expand All @@ -29,7 +29,7 @@ const RENEW_FEE_QUOTES_BEFORE_EXPIRATION_TIME = 30000 // Will renew the quote if
const WAITING_TIME_BETWEEN_EQUAL_REQUESTS = 5000 // Prevents from sending the same request to often (max, every 5s)
const UNSUPPORTED_TOKEN_REFETCH_CHECK_INTERVAL = 10 * 60 * 1000 // if unsupported token was added > 10min ago, re-try

type FeeQuoteParams = Omit<FeeQuoteParamsFull, 'validTo'>
type FeeQuoteParams = Omit<LegacyFeeQuoteParamsFull, 'validTo'>

/**
* Returns if the quote has been recently checked
Expand Down
2 changes: 1 addition & 1 deletion src/custom/state/swap/TradeGp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CanonicalMarketParams, getCanonicalMarket } from 'utils/misc'
import { FeeInformation, PriceInformation } from 'utils/price'
import { CurrencyAmount, Currency, TradeType, Price, Percent, Fraction } from '@uniswap/sdk-core'
import { Trade } from '@uniswap/v2-sdk'
import { FeeInformation, PriceInformation } from '@cowprotocol/cow-sdk'

export type FeeForTrade = { feeAsCurrency: CurrencyAmount<Currency> } & Pick<FeeInformation, 'amount'>

Expand Down
Loading

0 comments on commit 77795fd

Please sign in to comment.