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

322/store quote info on redux #555

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/custom/api/coingecko/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ export function toPriceInformation(priceRaw: CoinGeckoUsdQuote | null): PriceInf
}

const { usd } = priceRaw[token]
return { amount: usd.toString(), token }
return { amount: usd.toString(), token, oppositeAmount: '1' }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i find oppositeAmount missliading.

If we can really not add from/to, would it be better amountInputted or something?

}
7 changes: 6 additions & 1 deletion src/custom/api/gnosisProtocol/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,12 @@ async function _handleQuoteResponse<T = any, P extends FeeQuoteParams = FeeQuote
},
})
} else {
return response.json()
const rawResponse = await response.json()
// The legacy quote endpoint does takes as part of the URL path the amount in
// and returns as response the amount out
// Since now we'll need to keep track of both, here we add to the response the amount in as oppositeAmount
const oppositeAmount = params.amount
return { ...rawResponse, oppositeAmount }
}
} catch (error) {
throw _handleError(error, response, params, 'QUOTE')
Expand Down
4 changes: 2 additions & 2 deletions src/custom/api/matcha-0x/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ export function toPriceInformation(priceRaw: MatchaPriceQuote | null, kind: Orde
const { sellAmount, buyAmount, sellTokenAddress, buyTokenAddress } = priceRaw

if (kind === OrderKind.BUY) {
return { amount: sellAmount, token: sellTokenAddress }
return { amount: sellAmount, oppositeAmount: buyAmount, token: sellTokenAddress }
} else {
return { amount: buyAmount, token: buyTokenAddress }
return { amount: buyAmount, oppositeAmount: sellAmount, token: buyTokenAddress }
}
}
2 changes: 2 additions & 0 deletions src/custom/api/paraswap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ export function toPriceInformation(priceRaw: ParaSwapPriceQuote | null): PriceIn
if (side === SwapSide.SELL) {
return {
amount: destAmount,
oppositeAmount: srcAmount,
token: destToken,
}
} else {
return {
amount: srcAmount,
oppositeAmount: destAmount,
token: srcToken,
}
}
Expand Down
1 change: 1 addition & 0 deletions src/custom/state/price/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function initializeState(
function getResetPrice(sellToken: string, buyToken: string, kind: OrderKind) {
return {
amount: null,
oppositeAmount: null,
// When we buy, the price estimation is given in sell tokens (if we sell, we give it in sell tokens)
// The price estimation is given in:
// - sell tokens (for buy orders)
Expand Down
27 changes: 21 additions & 6 deletions src/custom/utils/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export interface FeeInformation {
export interface PriceInformation {
token: string
amount: string | null
oppositeAmount: string | null
// quoteId?: string // TODO fill in this info if we have it
}

// GetQuoteResponse from @cowprotocol/contracts types Timestamp and BigNumberish
Expand All @@ -54,6 +56,7 @@ export type SimpleGetQuoteResponse = Pick<GetQuoteResponse, 'from'> & {
buyAmount: string
validTo: string
feeAmount: string
// quoteId?: string // TODO: not yet coming from the API, tracked on https://github.com/cowprotocol/services/issues/170
}
expiration: string
}
Expand Down Expand Up @@ -110,13 +113,22 @@ function _filterWinningPrice(params: FilterWinningPriceParams) {
const amount = BigNumberJs[aggregationFunction](...params.amounts).toString(10)
const token = params.priceQuotes[0].token

const winningPrices = params.priceQuotes
.filter((quote) => quote.amount === amount)
.map((p) => p.source)
.join(', ')
console.debug('[util::filterWinningPrice] Winning price: ' + winningPrices + ' for token ' + token + ' @', amount)
const winningPrices = params.priceQuotes.filter((quote) => quote.amount === amount)
const oppositeAmount = winningPrices[0]?.oppositeAmount

console.debug(
'[util::filterWinningPrice] Winning price: ' +
winningPrices.map((p) => p.source).join(', ') +
' for token ' +
token +
' @ ' +
amount +
' & oppositeAmount ' +
oppositeAmount,
winningPrices
)

return { token, amount }
return { token, amount, oppositeAmount }
}

export type QuoteResult = [PromiseSettledResult<PriceInformation>, PromiseSettledResult<FeeInformation>]
Expand Down Expand Up @@ -260,10 +272,13 @@ export async function getBestPrice(params: PriceQuoteParams, options?: GetBestPr
export async function getFullQuote({ quoteParams }: { quoteParams: FeeQuoteParams }): Promise<QuoteResult> {
const { kind } = quoteParams
const { quote, expiration: expirationDate } = await getQuote(quoteParams)
// TODO: store/pass down as response the buyAmount, sellAmount and quoteId from quote obj

const price = {
amount: kind === OrderKind.SELL ? quote.buyAmount : quote.sellAmount,
oppositeAmount: kind === OrderKind.SELL ? quote.sellAmount : quote.buyAmount,
token: kind === OrderKind.SELL ? quote.buyToken : quote.sellToken,
// quoteId: quote.quoteId,
}
const fee = {
amount: quote.feeAmount,
Expand Down