Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Pr2160/follow up #2176

Merged
merged 2 commits into from
Jan 18, 2022
Merged
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
4 changes: 2 additions & 2 deletions src/custom/pages/Claim/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export type ClaimCommonTypes = {

// Enhanced UserClaimData with useful additional properties
export type EnhancedUserClaimData = UserClaimData & {
claimAmount: CurrencyAmount<Token>
isFree: boolean
currencyAmount?: CurrencyAmount<Token | GpEther> | undefined
claimAmount?: CurrencyAmount<Token> | undefined
price?: Price<Currency, Currency> | undefined
cost?: CurrencyAmount<Currency> | undefined
isFree: boolean
}
58 changes: 32 additions & 26 deletions src/custom/state/claim/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useActiveWeb3React } from 'hooks/web3'
import { useSingleContractMultipleData } from 'state/multicall/hooks'
import { useTransactionAdder } from 'state/enhancedTransactions/hooks'

import { V_COW } from 'constants/tokens'
import { GpEther, V_COW } from 'constants/tokens'

import { formatSmart } from 'utils/format'
import { calculateGasMargin } from 'utils/calculateGasMargin'
Expand Down Expand Up @@ -749,36 +749,42 @@ export function useUserEnhancedClaimData(account: Account): EnhancedUserClaimDat
const chainId = supportedChainId(preCheckChainId)
if (!chainId) return []

return sorted.map<EnhancedUserClaimData>((claim) => {
const claimAmount = CurrencyAmount.fromRawAmount(ONE_VCOW.currency, claim.amount)
return sorted.map((claim) => _enhanceClaimData(claim, chainId))
}, [preCheckChainId, sorted])
}

const tokenAndAmount = claimTypeToTokenAmount(claim.type, chainId)
function _sortTypes(a: UserClaimData, b: UserClaimData): number {
return Number(isFreeClaim(b.type)) - Number(isFreeClaim(a.type))
}

const data: EnhancedUserClaimData = {
...claim,
isFree: isFreeClaim(claim.type),
claimAmount,
}
function _enhanceClaimData(claim: UserClaimData, chainId: SupportedChainId): EnhancedUserClaimData {
const claimAmount = CurrencyAmount.fromRawAmount(ONE_VCOW.currency, claim.amount)

if (!tokenAndAmount) {
return data
} else {
data.price = new Price({
baseAmount: ONE_VCOW,
quoteAmount: CurrencyAmount.fromRawAmount(tokenAndAmount.token, tokenAndAmount.amount),
}).invert()
// get the currency amount using the price base currency (remember price was inverted) and claim amount
data.currencyAmount = CurrencyAmount.fromRawAmount(data.price.baseCurrency, claim.amount)
const data: EnhancedUserClaimData = {
...claim,
isFree: isFreeClaim(claim.type),
claimAmount,
}

// e.g 1000 vCow / 20 GNO = 50 GNO cost
data.cost = data.currencyAmount.divide(data.price)
const tokenAndAmount = claimTypeToTokenAmount(claim.type, chainId)

return data
}
})
}, [preCheckChainId, sorted])
// Free claims will have tokenAndAmount === undefined
// If it's not a free claim, store the price and calculate cost in investment token
if (tokenAndAmount) {
data.price = _getPrice(tokenAndAmount)
// get the currency amount using the price base currency (remember price was inverted)
data.currencyAmount = CurrencyAmount.fromRawAmount(data.price.baseCurrency, claim.amount)

// e.g 1000 vCow / 20 GNO = 50 GNO cost
data.cost = data.currencyAmount.divide(data.price)
}

return data
}

function _sortTypes(a: UserClaimData, b: UserClaimData): number {
return Number(isFreeClaim(b.type)) - Number(isFreeClaim(a.type))
function _getPrice({ token, amount }: { amount: string; token: Token | GpEther }) {
return new Price({
baseAmount: ONE_VCOW,
quoteAmount: CurrencyAmount.fromRawAmount(token, amount),
}).invert()
}