|
| 1 | +import React, { useContext, useEffect, useState } from 'react' |
| 2 | +import useRealm from '@hooks/useRealm' |
| 3 | +import { PublicKey } from '@solana/web3.js' |
| 4 | +import { |
| 5 | + UiInstruction, |
| 6 | +} from '@utils/uiTypes/proposalCreationTypes' |
| 7 | +import { NewProposalContext } from '../../../new' |
| 8 | +import useGovernanceAssets from '@hooks/useGovernanceAssets' |
| 9 | +import { Governance, serializeInstructionToBase64 } from '@solana/spl-governance' |
| 10 | +import { ProgramAccount } from '@solana/spl-governance' |
| 11 | +import useWalletOnePointOh from '@hooks/useWalletOnePointOh' |
| 12 | +import useLegacyConnectionContext from '@hooks/useLegacyConnectionContext' |
| 13 | +import { DasNftObject, useRaydiumAssetsByOwner } from '@hooks/queries/digitalAssets' |
| 14 | +import GovernanceAccountSelect from '../../GovernanceAccountSelect' |
| 15 | +import { SplGovernance } from 'governance-idl-sdk' |
| 16 | +import Select from '@components/inputs/Select' |
| 17 | +import {Raydium} from '@raydium-io/raydium-sdk-v2' |
| 18 | +import { BN } from '@coral-xyz/anchor' |
| 19 | + |
| 20 | +interface RaydiumDasNftObject extends DasNftObject { |
| 21 | + name: string |
| 22 | + poolId: string |
| 23 | + lpAmount: BN |
| 24 | + mintARewardAmount: string |
| 25 | + mintBRewardAmount: string |
| 26 | + mintA: string |
| 27 | + mintB: string |
| 28 | +} |
| 29 | + |
| 30 | +const CollectPoolFees = ({ |
| 31 | + index, |
| 32 | + governance, |
| 33 | +}: { |
| 34 | + index: number |
| 35 | + governance: ProgramAccount<Governance> | null |
| 36 | +}) => { |
| 37 | + const connection = useLegacyConnectionContext() |
| 38 | + const wallet = useWalletOnePointOh() |
| 39 | + const { governancesArray } = useGovernanceAssets() |
| 40 | + const [selectedNft, setSelectedNft] = useState<RaydiumDasNftObject | null>(null) |
| 41 | + |
| 42 | + const [governedAccount, setGovernedAccount] = useState< |
| 43 | + ProgramAccount<Governance> | undefined |
| 44 | + >(undefined) |
| 45 | + |
| 46 | + const splGovernance = new SplGovernance(connection.current) |
| 47 | + const nativeAddress = governedAccount?.pubkey ? |
| 48 | + splGovernance.pda.nativeTreasuryAccount({governanceAccount: governedAccount.pubkey}).publicKey : |
| 49 | + undefined |
| 50 | + |
| 51 | + const {data: assets} = useRaydiumAssetsByOwner(nativeAddress) |
| 52 | + const raydiumNfts = assets?.filter((asset) => asset.name !== undefined) |
| 53 | + |
| 54 | + const { handleSetInstructions } = useContext(NewProposalContext) |
| 55 | + |
| 56 | + async function getInstruction(): Promise<UiInstruction> { |
| 57 | + if ( |
| 58 | + governedAccount?.account && |
| 59 | + wallet?.publicKey && |
| 60 | + selectedNft |
| 61 | + ) { |
| 62 | + const raydium = await Raydium.load({ |
| 63 | + connection: connection.current, |
| 64 | + owner: nativeAddress, |
| 65 | + }) |
| 66 | + |
| 67 | + if (selectedNft.lpAmount.isZero()) { |
| 68 | + throw new Error('No rewards to claim.') |
| 69 | + } |
| 70 | + |
| 71 | + const poolData = await raydium.cpmm.getRpcPoolInfo(selectedNft.poolId) |
| 72 | + const poolInfo = await raydium.cpmm.getCpmmPoolKeys(selectedNft.poolId) |
| 73 | + |
| 74 | + const harvestLpFeesIxs = await raydium.cpmm.harvestLockLp({ |
| 75 | + nftMint: new PublicKey(selectedNft.id), |
| 76 | + poolInfo: { |
| 77 | + programId: poolInfo.programId, |
| 78 | + id: poolInfo.id, |
| 79 | + mintA: poolInfo.mintA, |
| 80 | + mintB: poolInfo.mintB, |
| 81 | + rewardDefaultInfos: [], |
| 82 | + rewardDefaultPoolInfos: "Raydium", |
| 83 | + price: parseInt(poolData.poolPrice.toString()), |
| 84 | + mintAmountA: 0, |
| 85 | + mintAmountB: 0, |
| 86 | + feeRate: 0, |
| 87 | + openTime: poolInfo.openTime, |
| 88 | + tvl: 0, |
| 89 | + pooltype: [], |
| 90 | + farmUpcomingCount: 0, |
| 91 | + farmFinishedCount: 0, |
| 92 | + farmOngoingCount: 0, |
| 93 | + burnPercent: 0, |
| 94 | + day: { |
| 95 | + volume: 0, |
| 96 | + volumeQuote: 0, |
| 97 | + volumeFee: 0, |
| 98 | + apr: 0, |
| 99 | + feeApr: 0, |
| 100 | + priceMin: 0, |
| 101 | + priceMax: 0, |
| 102 | + rewardApr: [] |
| 103 | + }, |
| 104 | + week: { |
| 105 | + volume: 0, |
| 106 | + volumeQuote: 0, |
| 107 | + volumeFee: 0, |
| 108 | + apr: 0, |
| 109 | + feeApr: 0, |
| 110 | + priceMin: 0, |
| 111 | + priceMax: 0, |
| 112 | + rewardApr: [] |
| 113 | + }, |
| 114 | + month: { |
| 115 | + volume: 0, |
| 116 | + volumeQuote: 0, |
| 117 | + volumeFee: 0, |
| 118 | + apr: 0, |
| 119 | + feeApr: 0, |
| 120 | + priceMin: 0, |
| 121 | + priceMax: 0, |
| 122 | + rewardApr: [] |
| 123 | + }, |
| 124 | + lpAmount: 0, |
| 125 | + lpMint: poolInfo.mintLp, |
| 126 | + lpPrice: 0, |
| 127 | + type: 'Standard', |
| 128 | + config: poolInfo.config |
| 129 | + }, |
| 130 | + lpFeeAmount: selectedNft.lpAmount, |
| 131 | + }) |
| 132 | + |
| 133 | + const additionalSerializedInstructions = harvestLpFeesIxs.transaction.instructions.map((ix) => serializeInstructionToBase64(ix)) |
| 134 | + return { |
| 135 | + serializedInstruction : '', |
| 136 | + additionalSerializedInstructions, |
| 137 | + isValid: true, |
| 138 | + governance: governedAccount, |
| 139 | + chunkBy: 1, |
| 140 | + } |
| 141 | + } else { |
| 142 | + return { |
| 143 | + serializedInstruction: '', |
| 144 | + isValid: false, |
| 145 | + governance: governedAccount, |
| 146 | + chunkBy: 1, |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + useEffect(() => { |
| 152 | + handleSetInstructions( |
| 153 | + { governedAccount: governedAccount, getInstruction }, |
| 154 | + index, |
| 155 | + ) |
| 156 | + // eslint-disable-next-line react-hooks/exhaustive-deps -- TODO please fix, it can cause difficult bugs. You might wanna check out https://bobbyhadz.com/blog/react-hooks-exhaustive-deps for info. -@asktree |
| 157 | + }, [selectedNft, governedAccount]) |
| 158 | + |
| 159 | + return ( |
| 160 | + <> |
| 161 | + <GovernanceAccountSelect |
| 162 | + label="Governance" |
| 163 | + governanceAccounts={governancesArray} |
| 164 | + onChange={(value) => { |
| 165 | + setGovernedAccount(value) |
| 166 | + }} |
| 167 | + value={governedAccount} |
| 168 | + /> |
| 169 | + |
| 170 | + <Select |
| 171 | + onChange={(value) => setSelectedNft(value)} |
| 172 | + label="Select Pool" |
| 173 | + placeholder="Select..." |
| 174 | + value={selectedNft?.id} |
| 175 | + > |
| 176 | + {raydiumNfts?.map((asset) => ( |
| 177 | + <Select.Option key={asset.id} value={asset}> |
| 178 | + {asset.name} |
| 179 | + </Select.Option> |
| 180 | + ))} |
| 181 | + </Select> |
| 182 | + {selectedNft?.mintARewardAmount && selectedNft.mintBRewardAmount ? |
| 183 | + <p>Rewards Available to Claim: {selectedNft.mintARewardAmount} {selectedNft.mintA}, {selectedNft.mintBRewardAmount} {selectedNft.mintB} |
| 184 | + </p> : |
| 185 | + null |
| 186 | + } |
| 187 | + </> |
| 188 | + ) |
| 189 | +} |
| 190 | + |
| 191 | +export default CollectPoolFees |
0 commit comments