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

Commit

Permalink
Pr2160/follow up (#2176)
Browse files Browse the repository at this point in the history
* Refactored claim type EnhancedUserClaimData

* Refactored useUserEnhancedClaimData into smaller pieces

Co-authored-by: Leandro <[email protected]>
  • Loading branch information
alfetopito and Leandro authored Jan 18, 2022
1 parent 325efc1 commit b7f6eee
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
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 @@ -755,36 +755,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()
}

0 comments on commit b7f6eee

Please sign in to comment.