Skip to content

Commit

Permalink
fix: claim rewards in ampluna (#586)
Browse files Browse the repository at this point in the history
* fix: claim rewards in ampluna
  • Loading branch information
nick134-bit authored Sep 3, 2024
1 parent 2675587 commit 6a48b3e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 35 deletions.
61 changes: 40 additions & 21 deletions components/Pages/Trade/Liquidity/hooks/useClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { createGasFee } from 'services/treasuryService'
import { chainState } from 'state/chainState'
import { createExecuteMessage } from 'util/messages/index'

import { useFetchLiquidityAlliances } from './useLiquidityAlliancePositions'
import { useAllianceRewards, useFetchLiquidityAlliances } from './useLiquidityAlliancePositions'

export const useClaim = (pool: PoolEntityType) => {
console.log('Pool: ', pool)
const { data: allianceRewards } = useAllianceRewards()
const { walletChainName } = useRecoilValue(chainState)
const { address } = useChain(walletChainName)
const { signingClient, injectiveSigningClient } = useClients(walletChainName)
Expand All @@ -29,46 +29,65 @@ export const useClaim = (pool: PoolEntityType) => {
if (isLoading || !position?.length) {
return null
}
return position[0]?.bribeMarket
const reward_asset = position[0]?.config.reward_info.native
return {
reward_asset,
reward_address: reward_asset?.split('/')[1],
config: position[0]?.config,
bribeMarket: position[0]?.bribeMarket
}
}, [position, isLoading])

let msg = null
let msg = []
if (!bribeMarket) {
msg =
createExecuteMessage({
message: {
claim: {},
},
senderAddress: address,
contractAddress: pool.staking_address,
funds: [],
})
msg.push(
createExecuteMessage({
message: {
claim: {},
},
senderAddress: address,
contractAddress: pool.staking_address,
funds: [],
}))
} else {
msg = createExecuteMessage({
msg.push(createExecuteMessage({
message: {
claim_reward: {
native: pool.lp_token,
},
},
senderAddress: address,
contractAddress: bribeMarket,
contractAddress: bribeMarket.bribeMarket,
funds: [],
})
}))
}

const { mutate: submit, ...state } = useMutation({
mutationFn: async () => {
if (bribeMarket) {
const reward = allianceRewards?.find((reward) => reward.staked_asset_share.info.native === pool.lp_token)
const balance = await signingClient.getBalance(address, bribeMarket.reward_asset)
const amount = { amount: String(Number(balance.amount) + Number(reward?.reward_asset.amount)), denom: balance.denom}
msg.push(createExecuteMessage({
message: {
withdraw: {},
},
senderAddress: address,
contractAddress: bribeMarket.reward_address,
funds: [amount],
}))
}
if (injectiveSigningClient && await signingClient.getChainId() === ChainId.injective) {
const injectiveTxData = await injectiveSigningClient.sign(
address, [msg], await createGasFee(
injectiveSigningClient, address, [msg],
address, msg, await createGasFee(
injectiveSigningClient, address, msg,
), ADV_MEMO,
)
return await signingClient.broadcastTx(TxRaw.encode(injectiveTxData).finish())
}
return await signingClient.signAndBroadcast(
address, [msg], await createGasFee(
signingClient, address, [msg],
address, msg, await createGasFee(
signingClient, address, msg,
), ADV_MEMO,
)
},
Expand All @@ -81,5 +100,5 @@ export const useClaim = (pool: PoolEntityType) => {
...state,
...tx,
}),
[tx, state, submit])
[tx, state, submit])
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ const fetchWhitelistedAllianceTokens = async (
const assets = await cosmWasmClient?.queryContractSmart(bribeMarket, {
whitelisted_assets: {},
})
const config = await cosmWasmClient?.queryContractSmart(bribeMarket, {
config: {},
})
return assets.map((asset) => ({
reward_asset: config.reward_info.native,
reward_contract: config.reward_info.native.split('/')[1],
token: asset?.cw20 || asset?.native,
bribeMarket,
config,
}))
}))

Expand All @@ -110,8 +116,10 @@ export const useFetchLiquidityAlliances = (searchForToken?: string) => {
cosmWasmClient, chainId, searchForToken,
),
{
refetchInterval: 9000,
refetchInterval: 1 * 60 * 1000,
enabled: chainId === 'phoenix-1',
cacheTime: 12 * 60 * 60 * 1000,
staleTime: 6 * 60 * 1000,
},
)
}
Expand All @@ -128,10 +136,13 @@ export const fetchAllAllianceRewards = async (
all_pending_rewards: { address },
})
for (const asset of assets) {
if (asset.reward_asset.info.native == 'factory/terra16l43xt2uq09yvz4axg73n8rtm0qte9lremdwm6ph0e35r2jnm43qnl8h53/zluna') {
asset.reward_asset.info.denom = 'uluna'
}
asset.reward_asset.bribe_market = bribeMarket
if (asset.reward_asset.info.native.endsWith('zluna')) {
const claimAddress = asset.reward_asset.info.native.split('/')[1]
//ampLuna
asset.reward_asset.info.contract_addr = 'terra1ecgazyd0waaj3g7l9cmy5gulhxkps2gmxu9ghducvuypjq68mq2s5lvsct'
asset.reward_asset.info.claim_addr = claimAddress
}
}
return assets
}))
Expand All @@ -149,7 +160,7 @@ export const useAllianceRewards = () => {
cosmWasmClient, address, chainId,
),
{
refetchInterval: 9000,
refetchInterval: 10000,
enabled: chainId === 'phoenix-1' && Boolean(address),
},
)
Expand Down
8 changes: 3 additions & 5 deletions components/Pages/Trade/Liquidity/hooks/useRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ const useRewards = (pool: PoolEntityType): RewardsResult => {
return useMemo(() => {
const rewardsWithToken: RewardData[] = []
aggregatedRewards?.forEach((reward) => {
console.log('Reward: ', reward)
// Cw20 token
if (reward.info?.token) {
const t = tokenList?.tokens.find((token) => token.denom === reward.info.token.contract_addr)
Expand Down Expand Up @@ -142,12 +141,12 @@ const useRewards = (pool: PoolEntityType): RewardsResult => {
dollarValue,
})
}
if (reward.info?.native && reward.bribe_market) {
const t = tokenList?.tokens.find((token) => token.denom === reward.info.denom)
if (reward.info?.contract_addr && reward.bribe_market) {
const t = tokenList?.tokens.find((token) => token.denom === reward.info.contract_addr)
const amount = fromChainAmount(reward.amount, t?.decimals)
const dollarValue = num(amount).
times(prices?.[t?.symbol] || 0).
dp(4).
dp(2).
toNumber()
rewardsWithToken.push({
...t,
Expand All @@ -157,7 +156,6 @@ const useRewards = (pool: PoolEntityType): RewardsResult => {
})
}
})
console.log('Rewards with token: ', rewardsWithToken)
return {
rewards: rewardsWithToken,
totalValue: rewardsWithToken?.
Expand Down
2 changes: 0 additions & 2 deletions components/Pages/Trade/Liquidity/hooks/useStakePosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ const useStake = ({ amount, contract }: StakeProps) => {
return useStakeTransaction({
enabled: Boolean(msgs.length && bribeMarket),
msgs,
encodedMsgs: [],
amount,
senderAddress: address,
signingClient,
})
Expand Down
6 changes: 4 additions & 2 deletions constants/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ export const B_WHALE_TOKEN_SYMBOL = 'bWHALE'
export const AMP_WHALE_TOKEN_SYMBOL = 'ampWHALE'
export const WHALE_TOKEN_SYMBOL = 'WHALE'

export const TERRA2_BRIBE_MARKETS = { project: 'terra1awq6t7jfakg9wfjn40fk3wzwmd57mvrqtt3a39z9rmet7wdjj3ysgw3lpa',
export const TERRA2_BRIBE_MARKETS = {
project: 'terra1awq6t7jfakg9wfjn40fk3wzwmd57mvrqtt3a39z9rmet7wdjj3ysgw3lpa',
bluechip: 'terra14mmvqn0kthw6sre75vku263lafn5655mkjdejqjedjga4cw0qx2qlf4arv',
stable: 'terra1v399cx9drllm70wxfsgvfe694tdsd9x96p9ha36w7muffe4znlusqswspq' }
stable: 'terra1v399cx9drllm70wxfsgvfe694tdsd9x96p9ha36w7muffe4znlusqswspq'
}
export const POOL_TOKENS_DECIMALS = 6

0 comments on commit 6a48b3e

Please sign in to comment.