Skip to content

Commit

Permalink
feat: arbitrum multidistributor
Browse files Browse the repository at this point in the history
  • Loading branch information
DillonLin authored Feb 13, 2023
1 parent 01a47ec commit 5df8c3e
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 43 deletions.
29 changes: 29 additions & 0 deletions app/src/constants/tokenlists/arbitrum.tokenlist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "Arbitrum",
"logoURI": "https://arbitrum.io/wp-content/uploads/2022/12/One-Logo-280x280-1.png",
"keywords": ["scaling", "layer2", "infrastructure"],
"timestamp": "2023-02-10T22:16:04.897Z",
"tokens": [
{
"chainId": 42161,
"address": "0x079504b86d38119F859c4194765029F692b7B7aa",
"name": "Lyra",
"symbol": "LYRA",
"decimals": 18,
"logoURI": "https://raw.githubusercontent.com/lyra-finance/interface/master/app/public/images/lyra-token.png"
},
{
"chainId": 42161,
"address": "0x5B237ab26CeD47Fb8ED104671819C801Aa5bA45E",
"name": "stkLyra",
"symbol": "stkLYRA",
"decimals": 18,
"logoURI": "https://raw.githubusercontent.com/lyra-finance/interface/master/app/public/images/staked-lyra-token.png"
}
],
"version": {
"major": 4,
"minor": 3,
"patch": 0
}
}
59 changes: 31 additions & 28 deletions app/src/containers/rewards/ClaimModal/ClaimButton.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import ButtonShimmer from '@lyra/ui/components/Shimmer/ButtonShimmer'
import { Network } from '@lyrafinance/lyra-js'
import React from 'react'

import { ZERO_BN } from '@/app/constants/bn'
import { TransactionType } from '@/app/constants/screen'
import useNetwork from '@/app/hooks/account/useNetwork'
import useTransaction from '@/app/hooks/account/useTransaction'
import useWalletAccount from '@/app/hooks/account/useWalletAccount'
import useOptimismToken from '@/app/hooks/data/useOptimismToken'
import useNetworkToken from '@/app/hooks/data/useNetworkToken'
import withSuspense from '@/app/hooks/data/withSuspense'
import useAccount from '@/app/hooks/market/useAccount'
import useAccountWethLyraStakingL2, {
useMutateAccountWethLyraStakingL2,
} from '@/app/hooks/rewards/useAccountWethLyraStakingL2'
import useClaimableBalances, { useMutateClaimableBalances } from '@/app/hooks/rewards/useClaimableBalance'
import filterNulls from '@/app/utils/filterNulls'
import { lyraOptimism } from '@/app/utils/lyra'

import TransactionButton from '../../common/TransactionButton'

Expand All @@ -27,11 +26,11 @@ type Props = {

const ClaimButton = withSuspense(
({ isOpChecked, isOldStkLyraChecked, isNewStkLyraChecked, isWethLyraChecked, onClaim }: Props) => {
const owner = useWalletAccount()
const account = lyraOptimism.account(owner ?? '')
const op = useOptimismToken('op')
const stkLyra = useOptimismToken('stkLyra')
const execute = useTransaction(Network.Optimism)
const network = useNetwork()
const account = useAccount(network)
const stkLyra = useNetworkToken(network, 'stkLyra')
const op = useNetworkToken(network, 'op')
const execute = useTransaction(network)
const mutateClaimableBalance = useMutateClaimableBalances()
const claimableBalances = useClaimableBalances()
const wethLyraAccount = useAccountWethLyraStakingL2()
Expand All @@ -43,15 +42,17 @@ const ClaimButton = withSuspense(
.isZero()

const handleWethLyraClaim = async () => {
const tx = await account.claimWethLyraRewardsL2()
await execute(tx, {
onComplete: () => {
mutateWethLyraAccount()
if (onClaim) {
onClaim()
}
},
})
const tx = await account?.claimWethLyraRewardsL2()
if (tx) {
await execute(tx, {
onComplete: () => {
mutateWethLyraAccount()
if (onClaim) {
onClaim()
}
},
})
}
}

const handleDistributorClaim = async () => {
Expand All @@ -60,20 +61,22 @@ const ClaimButton = withSuspense(
isNewStkLyraChecked ? stkLyra?.address : null,
isOldStkLyraChecked ? '0xdE48b1B5853cc63B1D05e507414D3E02831722F8' : null,
])
const tx = await account.claim(tokens)
await execute(tx, {
onComplete: () => {
mutateClaimableBalance()
if (onClaim) {
onClaim()
}
},
})
const tx = await account?.claim(tokens)
if (tx) {
await execute(tx, {
onComplete: () => {
mutateClaimableBalance()
if (onClaim) {
onClaim()
}
},
})
}
}

return (
<TransactionButton
network={Network.Optimism}
network={network}
transactionType={TransactionType.ClaimRewards}
width="100%"
label={
Expand Down
9 changes: 6 additions & 3 deletions app/src/containers/rewards/ClaimModal/ClaimModalContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import Flex from '@lyra/ui/components/Flex'
import Checkbox from '@lyra/ui/components/Input/Checkbox'
import Spinner from '@lyra/ui/components/Spinner'
import Text from '@lyra/ui/components/Text'
import { Network } from '@lyrafinance/lyra-js'
import React from 'react'

import TokenAmountText from '@/app/components/common/TokenAmountText'
import useNetwork from '@/app/hooks/account/useNetwork'
import withSuspense from '@/app/hooks/data/withSuspense'
import useAccountWethLyraStakingL2 from '@/app/hooks/rewards/useAccountWethLyraStakingL2'
import useClaimableBalances from '@/app/hooks/rewards/useClaimableBalance'
Expand All @@ -34,13 +36,14 @@ const ClaimModalContent = withSuspense(
onClickOldStkLyra,
onClickWethLyra,
}: Props) => {
const network = useNetwork()
const claimableBalances = useClaimableBalances()
const wethLyraAccount = useAccountWethLyraStakingL2()
const isDistributorRewardsDisabled = isWethLyraChecked
const isWethLyraDisabled = isOpChecked || isNewStkLyraChecked || isOldStkLyraChecked
return (
<Box>
{claimableBalances.oldStkLyra.gt(0) ? (
{network === Network.Optimism && claimableBalances.oldStkLyra.gt(0) ? (
<Card
variant="nested"
mb={6}
Expand Down Expand Up @@ -98,7 +101,7 @@ const ClaimModalContent = withSuspense(
</CardBody>
</Card>
) : null}
{claimableBalances.op.gt(0) ? (
{network === Network.Optimism && claimableBalances.op.gt(0) ? (
<Card
variant="nested"
mb={6}
Expand All @@ -123,7 +126,7 @@ const ClaimModalContent = withSuspense(
</CardBody>
</Card>
) : null}
{wethLyraAccount?.rewards.gt(0) ? (
{network === Network.Optimism && wethLyraAccount?.rewards.gt(0) ? (
<Card
variant="nested"
mb={6}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const ShortCollateralRewardsCardGrid = withSuspense(
const lyraRewardsCap = findLyraRewardEpochToken(globalRewardEpoch?.tradingRewardsCap ?? [])
const opRewardsCap = findOpRewardEpochToken(globalRewardEpoch?.tradingRewardsCap ?? [])
const lyraRewards = findLyraRewardEpochToken(accountRewardEpoch?.shortCollateralRewards ?? [])
const opRewards = findLyraRewardEpochToken(accountRewardEpoch?.shortCollateralRewards ?? [])
const opRewards = findOpRewardEpochToken(accountRewardEpoch?.shortCollateralRewards ?? [])

const shortPositions = useMemo(() => {
if (!globalRewardEpoch) {
Expand Down
21 changes: 21 additions & 0 deletions app/src/hooks/data/useArbitrumToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { TokenInfo } from '@uniswap/token-lists'

import getArbitrumChainId from '@/app/utils/getArbitrumChainId'

import useArbitrumTokenList from './useArbitrumTokenList'

export default function useArbitrumToken(tokenNameOrAddress: string): TokenInfo | null {
const arbitrumTokenList = useArbitrumTokenList()
if (!arbitrumTokenList) {
return null
}
const chainId = getArbitrumChainId()
const token = arbitrumTokenList.find(
token =>
(token.address === tokenNameOrAddress ||
token.symbol.toLowerCase() === tokenNameOrAddress.toLowerCase() ||
token.name.toLowerCase() === tokenNameOrAddress.toLowerCase()) &&
token.chainId === chainId
)
return token ?? null
}
18 changes: 18 additions & 0 deletions app/src/hooks/data/useArbitrumTokenList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TokenInfo } from '@uniswap/token-lists'
import { useMemo } from 'react'

import getUniqueBy from '@/app/utils/getUniqueBy'

import ARBITRUM_TOKEN_LIST from '../../constants/tokenlists/arbitrum.tokenlist.json'

export default function useArbitrumTokenList(): TokenInfo[] {
return useMemo(
() =>
getUniqueBy(
// Insert custom list last for overrides
[...ARBITRUM_TOKEN_LIST.tokens],
token => `${token.address}-${token.chainId}`
),
[]
)
}
16 changes: 16 additions & 0 deletions app/src/hooks/data/useNetworkToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Network } from '@lyrafinance/lyra-js'
import { TokenInfo } from '@uniswap/token-lists'

import useArbitrumToken from './useArbitrumToken'
import useOptimismToken from './useOptimismToken'

export default function useNetworkToken(network: Network, tokenNameOrAddress: string): TokenInfo | null {
const arbitrumToken = useArbitrumToken(tokenNameOrAddress)
const optimismToken = useOptimismToken(tokenNameOrAddress)
switch (network) {
case Network.Arbitrum:
return arbitrumToken
case Network.Optimism:
return optimismToken
}
}
25 changes: 19 additions & 6 deletions app/src/hooks/rewards/useClaimableBalance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ClaimableBalanceL2 } from '@lyrafinance/lyra-js'
import { ClaimableBalanceL2, Network } from '@lyrafinance/lyra-js'
import { useCallback } from 'react'

import { ZERO_BN } from '@/app/constants/bn'
import { FetchId } from '@/app/constants/fetch'
import { lyraOptimism } from '@/app/utils/lyra'
import { lyraArbitrum, lyraOptimism } from '@/app/utils/lyra'

import useNetwork from '../account/useNetwork'
import useWalletAccount from '../account/useWalletAccount'
import useFetch, { useMutate } from '../data/useFetch'

Expand All @@ -14,17 +15,29 @@ const EMPTY: ClaimableBalanceL2 = {
op: ZERO_BN,
}

const fetchClaimableBalance = async (account: string): Promise<ClaimableBalanceL2> =>
await lyraOptimism.account(account).claimableRewardsL2()
const fetchClaimableBalance = async (network: Network, account: string): Promise<ClaimableBalanceL2> => {
switch (network) {
case Network.Arbitrum:
return await lyraArbitrum.account(account).claimableRewardsL2()
case Network.Optimism:
return await lyraOptimism.account(account).claimableRewardsL2()
}
}

export default function useClaimableBalances(): ClaimableBalanceL2 {
const network = useNetwork()
const account = useWalletAccount()
const [claimableBalance] = useFetch(FetchId.ClaimableBalanceL2, account ? [account] : null, fetchClaimableBalance)
const [claimableBalance] = useFetch(
FetchId.ClaimableBalanceL2,
network && account ? [network, account] : null,
fetchClaimableBalance
)
return claimableBalance ?? EMPTY
}

export const useMutateClaimableBalances = (): (() => Promise<ClaimableBalanceL2 | null>) => {
const mutate = useMutate(FetchId.ClaimableBalanceL2, fetchClaimableBalance)
const network = useNetwork()
const account = useWalletAccount()
return useCallback(async () => (account ? await mutate(account) : null), [mutate, account])
return useCallback(async () => (account ? await mutate(network, account) : null), [mutate, account, network])
}
7 changes: 6 additions & 1 deletion sdk/src/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
LyraContractId,
LyraGlobalContractId,
LyraMarketContractId,
NEW_STAKED_LYRA_ARBITRUM_ADDRESS,
NEW_STAKED_LYRA_OPTIMISM_ADDRESS,
OLD_STAKED_LYRA_OPTIMISM_ADDRESS,
OP_OPTIMISM_MAINNET_ADDRESS,
} from '../constants/contracts'
import { Network } from '../constants/network'
import { LiquidityDeposit } from '../liquidity_deposit'
import { LiquidityWithdrawal } from '../liquidity_withdrawal'
import Lyra from '../lyra'
Expand Down Expand Up @@ -233,7 +235,10 @@ export class Account {

async claimableRewardsL2(): Promise<ClaimableBalanceL2> {
const distributorContract = getGlobalContract(this.lyra, LyraGlobalContractId.MultiDistributor)
const newStkLyraAddress = getAddress(NEW_STAKED_LYRA_OPTIMISM_ADDRESS)
const newStkLyraAddress =
this.lyra.network === Network.Arbitrum
? getAddress(NEW_STAKED_LYRA_ARBITRUM_ADDRESS)
: getAddress(NEW_STAKED_LYRA_OPTIMISM_ADDRESS)
const oldStkLyraAddress = getAddress(OLD_STAKED_LYRA_OPTIMISM_ADDRESS)
const opAddress =
this.lyra.deployment === Deployment.Mainnet ? OP_OPTIMISM_MAINNET_ADDRESS : LYRA_OPTIMISM_KOVAN_ADDRESS
Expand Down
11 changes: 8 additions & 3 deletions sdk/src/account_reward_epoch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@ export class AccountRewardEpoch {
})
this.totalVaultRewards = Object.values(marketVaultRewardsMap)
this.tradingFeeRebate = this.globalEpoch.tradingFeeRebate(this.stakedLyraBalance)
this.tradingFees = this.accountEpoch.tradingRewards.fees
const integratorTradingFees = this.accountEpoch.integratorTradingRewards?.fees ?? 0
this.tradingFees = integratorTradingFees > 0 ? integratorTradingFees : this.accountEpoch.tradingRewards.fees
this.tradingRewards = this.globalEpoch.tradingRewards(this.tradingFees, this.stakedLyraBalance)
const integratorShortCollateralRewardDollars =
this.accountEpoch.integratorTradingRewards?.shortCollateralRewardDollars ?? 0
this.shortCollateralRewards = this.globalEpoch.shortCollateralRewards(
this.accountEpoch.tradingRewards.shortCollateralRewardDollars
integratorShortCollateralRewardDollars > 0
? integratorShortCollateralRewardDollars
: this.accountEpoch.tradingRewards.shortCollateralRewardDollars
)
const claimAddedTags = parseClaimAddedTags(claimAddedEvents)

// TODO @dillon - refactor this later
// TODO @dillon: refactor this later
const lyraTradingRewards =
this.tradingRewards.find(token => ['lyra', 'stklyra'].includes(token.symbol.toLowerCase()))?.amount ?? 0
const lyraShortCollateralRewards =
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/constants/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ export const POSITION_UPDATED_TYPES = [
]

export const LYRA_OPTIMISM_MAINNET_ADDRESS = '0x50c5725949a6f0c72e6c4a641f24049a917db0cb'
export const LYRA_ARBITRUM_MAINNET_ADDRESS = '0x079504b86d38119F859c4194765029F692b7B7aa'
export const LYRA_ETHEREUM_MAINNET_ADDRESS = '0x01BA67AAC7f75f647D94220Cc98FB30FCc5105Bf'
export const OP_OPTIMISM_MAINNET_ADDRESS = '0x4200000000000000000000000000000000000042'
export const LYRA_OPTIMISM_KOVAN_ADDRESS = '0xC9801013F0c45F836Ad07Dded1df9C475d2844FC'
export const LYRA_ETHEREUM_KOVAN_ADDRESS = '0xC9801013F0c45F836Ad07Dded1df9C475d2844FC'
export const OLD_STAKED_LYRA_OPTIMISM_ADDRESS = '0xde48b1b5853cc63b1d05e507414d3e02831722f8'
export const NEW_STAKED_LYRA_OPTIMISM_ADDRESS = '0x0F5d45a7023612e9e244fe84FAc5fCf3740d1492'
export const NEW_STAKED_LYRA_ARBITRUM_ADDRESS = '0x5B237ab26CeD47Fb8ED104671819C801Aa5bA45E'
export const OLD_STAKED_LYRA_OPTIMISM_KOVAN_ADDRESS = '0xd16d254c42a03ad72d77b4226d086afcc0b2e43e'
export const STAKED_LYRA_ETHEREUM_ADDRESS = '0xb9619D73C08076bC5d4f0470593e98b9eb19a219'
export const ONE_INCH_ORACLE_OPTIMISM_MAINNET_ADDRESS = '0x11DEE30E710B8d4a8630392781Cc3c0046365d4c'
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/contracts/common/addresses/arbitrum.addresses.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"Multicall3": "0xcA11bde05977b3631167028862bE2a173976CA11",
"ArrakisPoolL1": "0xE6f375A29cDd3B40fa7aA0932fF510D304D95FA6",
"WethLyraStakingRewardsL1": "0x1A364a7e66B21ed3045b13D3465627F9e9613f07"
"WethLyraStakingRewardsL1": "0x1A364a7e66B21ed3045b13D3465627F9e9613f07",
"MultiDistributor": "0xecB73D4621Cabbf199e778CAEBc74bE27f2EcEe1"
}
1 change: 1 addition & 0 deletions sdk/src/utils/fetchAccountRewardEpochData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type AccountRewardEpochData = {
mmvRewards: AccountMMVRewards
tradingRewards: AccountTradingRewards
arrakisRewards?: AccountArrakisRewards
integratorTradingRewards?: AccountTradingRewards
}

export type AccountStakingRewards = {
Expand Down

0 comments on commit 5df8c3e

Please sign in to comment.