-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
355 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,44 @@ | ||
import { format } from "@greypixel_/nicenumbers"; | ||
import { twJoin } from "tailwind-merge"; | ||
import { Checkbox } from "../common/Checkbox"; | ||
import { useClaimableAmount } from "./hooks/useClaimableAmount"; | ||
|
||
export const ClaimableTokensLineItem = ({ | ||
tokenAddress, | ||
numPoints, | ||
symbol, | ||
amount, | ||
decimals, | ||
value, | ||
isSelected, | ||
onSelect, | ||
}: { | ||
tokenAddress: `0x${string}`; | ||
numPoints: number; | ||
symbol: string; | ||
decimals: number; | ||
amount: bigint; | ||
value: number; | ||
isSelected: boolean; | ||
onSelect: (amount: bigint) => void; | ||
onSelect: () => void; | ||
}) => { | ||
const { isLoading, data } = useClaimableAmount({ | ||
points: numPoints, | ||
tokenAddress, | ||
}); | ||
|
||
if (isLoading || !data.claimableAmount || !data.tokenInfo) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<Checkbox | ||
isOptionButton | ||
checked={isSelected} | ||
onCheckedChange={(state) => | ||
state === true && | ||
data.claimableAmount && | ||
onSelect(data.claimableAmount) | ||
} | ||
onCheckedChange={(state) => state === true && onSelect()} | ||
/> | ||
<span className="text-gray-400"> | ||
{format(data.claimableAmount, { | ||
tokenDecimals: data.tokenInfo.decimals, | ||
{format(amount, { | ||
tokenDecimals: decimals, | ||
})}{" "} | ||
{data.tokenInfo.symbol} | ||
{symbol} | ||
</span> | ||
<span | ||
className={twJoin( | ||
"text-white font-medium", | ||
amount && symbol ? "opacity-100" : "opacity-0", | ||
"transition-opacity", | ||
)} | ||
> | ||
${value.toFixed(2)} | ||
</span> | ||
<span className="text-white font-medium">$XXX</span> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
type MainnetClaimableToken = { | ||
address: `0x${string}`; | ||
mainnetEquivalentAddress?: undefined; | ||
}; | ||
|
||
type TestnetClaimableToken = { | ||
address: `0x${string}`; | ||
mainnetEquivalentAddress: `0x${string}`; | ||
}; | ||
type ClaimableTokensNetworkMapping = { | ||
[index: number]: TestnetClaimableToken[]; | ||
} & { | ||
mainnet: MainnetClaimableToken[]; | ||
}; | ||
|
||
export const claimableTokens: ClaimableTokensNetworkMapping = { | ||
// TODO: populate. | ||
mainnet: [], | ||
5: [ | ||
// link | ||
{ | ||
address: "0x326c977e6efc84e512bb9c30f76e30c160ed06fb", | ||
mainnetEquivalentAddress: "0x514910771AF9Ca656af840dff83E8264EcF986CA", | ||
}, | ||
// uni | ||
{ | ||
address: "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984", | ||
mainnetEquivalentAddress: "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984", | ||
}, | ||
// weth | ||
{ | ||
address: "0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6", | ||
mainnetEquivalentAddress: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useChainId, useQuery } from "wagmi"; | ||
import { multicall } from "wagmi/actions"; | ||
import { ContractTypes } from "../../../config/ContractAddresses"; | ||
import { useContractAddresses } from "../../../config/hooks/useContractAddress"; | ||
import { poolAbi } from "../../../contracts/poolAbi"; | ||
|
||
export const useClaimCalculations = ( | ||
points: number, | ||
claimableTokens: `0x${string}`[], | ||
) => { | ||
const chainId = useChainId(); | ||
const [poolContract] = useContractAddresses([ContractTypes.AirSwapPool], { | ||
useDefaultAsFallback: false, | ||
alwaysUseDefault: false, | ||
}); | ||
const _points = BigInt(points) * BigInt(10 ** 4); | ||
|
||
const fetch = async () => { | ||
if (!poolContract.address) return; | ||
const multicallResponse = await multicall({ | ||
chainId, | ||
contracts: claimableTokens.map((tokenAddress) => ({ | ||
address: poolContract.address!, | ||
abi: poolAbi, | ||
functionName: "calculate", | ||
args: [_points, tokenAddress], | ||
})), | ||
}); | ||
|
||
// return just the results | ||
return multicallResponse.map((response) => response.result || 0n); | ||
}; | ||
|
||
return useQuery(["claimCalculations", chainId, points], fetch, { | ||
enabled: Boolean( | ||
_points > 0 && poolContract.address && claimableTokens.length, | ||
), | ||
// 1 minute | ||
cacheTime: 60_000, | ||
staleTime: 30_000, | ||
refetchInterval: 30_000, | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import BigNumber from "bignumber.js"; | ||
import { useMemo } from "react"; | ||
import { useChainId } from "wagmi"; | ||
import { useMultipleTokenInfo } from "../../common/hooks/useMultipleTokenInfo"; | ||
import { claimableTokens } from "../config/claimableTokens"; | ||
import { useClaimCalculations } from "./useClaimCalculations"; | ||
import { useDefiLlamaBatchPrices } from "./useDefillamaBatchPrices"; | ||
|
||
const empty: string[] = []; | ||
|
||
export const useClaimableAmounts = (points: number) => { | ||
const chainId = useChainId(); | ||
const tokenList = | ||
claimableTokens[chainId === 1 ? "mainnet" : chainId] || empty; | ||
|
||
const { data: claimableAmounts } = useClaimCalculations( | ||
points, | ||
tokenList.map((token) => token.address), | ||
); | ||
|
||
const tokenInfoResults = useMultipleTokenInfo({ | ||
chainId, | ||
tokenAddresses: tokenList.map((token) => token.address), | ||
}); | ||
|
||
const { data: prices } = useDefiLlamaBatchPrices( | ||
tokenList.map((token) => token.mainnetEquivalentAddress || token.address), | ||
); | ||
|
||
return useMemo(() => { | ||
return tokenList | ||
.map((_, index) => { | ||
const tokenInfo = tokenInfoResults[index].data; | ||
const price = prices?.[index].price; | ||
const claimableAmount = claimableAmounts?.[index]; | ||
const claimableValue = | ||
tokenInfo?.decimals && | ||
price && | ||
claimableAmount && | ||
new BigNumber(claimableAmount.toString()) | ||
.dividedBy(10 ** tokenInfo.decimals) | ||
.multipliedBy(price) | ||
.toNumber(); | ||
|
||
return { | ||
claimableAmount, | ||
tokenInfo, | ||
price, | ||
claimableValue, | ||
}; | ||
}) | ||
.sort((a, b) => (b.claimableValue || 0) - (a.claimableValue || 0)); | ||
}, [prices, tokenInfoResults, claimableAmounts, tokenList]); | ||
}; |
Oops, something went wrong.