Skip to content

Commit 7667198

Browse files
authored
Add deltafi claim rewards (#138)
1 parent 7654460 commit 7667198

File tree

6 files changed

+185
-20
lines changed

6 files changed

+185
-20
lines changed

hooks/useGovernanceAssets.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -581,121 +581,107 @@ export default function useGovernanceAssets() {
581581
name: 'Transfer Tokens',
582582
isVisible: canUseAnyInstruction,
583583
packageId: PackageEnum.Native,
584-
tag: 'beta',
585584
},
586585
[InstructionEnum.DeltafiCreateLiquidityProvider]: {
587586
name: 'Create Liquidity Provider',
588587
isVisible: canUseAnyInstruction,
589588
packageId: PackageEnum.Deltafi,
590-
tag: 'beta',
591589
},
592590
[InstructionEnum.DeltafiPoolDeposit]: {
593591
name: 'Deposit Tokens to Pool',
594592
isVisible: canUseAnyInstruction,
595593
packageId: PackageEnum.Deltafi,
596-
tag: 'beta',
597594
},
598595
[InstructionEnum.DeltafiPoolWithdraw]: {
599596
name: 'Withdraw Tokens from Pool',
600597
isVisible: canUseAnyInstruction,
601598
packageId: PackageEnum.Deltafi,
602-
tag: 'beta',
603599
},
604600
[InstructionEnum.DeltafiCreateFarmUser]: {
605601
name: 'Create Farm User',
606602
isVisible: canUseAnyInstruction,
607603
packageId: PackageEnum.Deltafi,
608-
tag: 'beta',
609604
},
610605
[InstructionEnum.DeltafiFarmDeposit]: {
611606
name: 'Deposit Tokens to Farm',
612607
isVisible: canUseAnyInstruction,
613608
packageId: PackageEnum.Deltafi,
614-
tag: 'beta',
615609
},
616610
[InstructionEnum.DeltafiFarmWithdraw]: {
617611
name: 'Withdraw Tokens from Farm',
618612
isVisible: canUseAnyInstruction,
619613
packageId: PackageEnum.Deltafi,
614+
},
615+
[InstructionEnum.DeltafiClaimFarmRewards]: {
616+
name: 'Claim Farm Rewards',
617+
isVisible: canUseAnyInstruction,
618+
packageId: PackageEnum.Deltafi,
620619
tag: 'beta',
621620
},
622621
[InstructionEnum.NativeBurnSplTokens]: {
623622
name: 'Burn SPL Tokens',
624623
isVisible: canUseAnyInstruction,
625624
packageId: PackageEnum.Native,
626-
tag: 'beta',
627625
},
628626
[InstructionEnum.OrcaWhirlpoolOpenPosition]: {
629627
name: 'Orca Whirlpool Open Position',
630628
isVisible: canUseAnyInstruction,
631629
packageId: PackageEnum.Orca,
632-
tag: 'beta',
633630
},
634631
[InstructionEnum.OrcaWhirlpoolIncreaseLiquidity]: {
635632
name: 'Orca Whirlpool Increase Liquidity',
636633
isVisible: canUseAnyInstruction,
637634
packageId: PackageEnum.Orca,
638-
tag: 'beta',
639635
},
640636
[InstructionEnum.OrcaWhirlpoolUpdateFeesAndRewards]: {
641637
name: 'Orca Whirlpool Update Fees and Rewards',
642638
isVisible: canUseAnyInstruction,
643639
packageId: PackageEnum.Orca,
644-
tag: 'beta',
645640
},
646641
[InstructionEnum.OrcaWhirlpoolCollectFees]: {
647642
name: 'Orca Whirlpool Collect Fees',
648643
isVisible: canUseAnyInstruction,
649644
packageId: PackageEnum.Orca,
650-
tag: 'beta',
651645
},
652646
[InstructionEnum.OrcaWhirlpoolDecreaseLiquidity]: {
653647
name: 'Orca Whirlpool Decrease Liquidity',
654648
isVisible: canUseAnyInstruction,
655649
packageId: PackageEnum.Orca,
656-
tag: 'beta',
657650
},
658651
[InstructionEnum.OrcaWhirlpoolClosePosition]: {
659652
name: 'Orca Whirlpool Close Position',
660653
isVisible: canUseAnyInstruction,
661654
packageId: PackageEnum.Orca,
662-
tag: 'beta',
663655
},
664656
[InstructionEnum.OrcaWhirlpoolSwap]: {
665657
name: 'Orca Whirlpool Swap',
666658
isVisible: canUseAnyInstruction,
667659
packageId: PackageEnum.Orca,
668-
tag: 'beta',
669660
},
670661
[InstructionEnum.MercurialPoolDeposit]: {
671662
name: 'Mercurial Pool Deposit',
672663
isVisible: canUseAnyInstruction,
673664
packageId: PackageEnum.Mercurial,
674-
tag: 'beta',
675665
},
676666
[InstructionEnum.MercurialPoolWithdraw]: {
677667
name: 'Mercurial Pool Withdraw',
678668
isVisible: canUseAnyInstruction,
679669
packageId: PackageEnum.Mercurial,
680-
tag: 'beta',
681670
},
682671
[InstructionEnum.NativeIncreaseComputingBudget]: {
683672
name: 'Increase Computing Budget',
684673
isVisible: canUseAnyInstruction,
685674
packageId: PackageEnum.Native,
686-
tag: 'beta',
687675
},
688676
[InstructionEnum.CredixDeposit]: {
689677
name: 'Deposit',
690678
isVisible: canUseAnyInstruction,
691679
packageId: PackageEnum.Credix,
692-
tag: 'beta',
693680
},
694681
[InstructionEnum.CredixWithdraw]: {
695682
name: 'Withdraw',
696683
isVisible: canUseAnyInstruction,
697684
packageId: PackageEnum.Credix,
698-
tag: 'beta',
699685
},
700686
};
701687

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import * as yup from 'yup';
2+
import useInstructionFormBuilder from '@hooks/useInstructionFormBuilder';
3+
import { DeltafiDexV2, PoolInfo } from '@tools/sdk/deltafi/configuration';
4+
import { GovernedMultiTypeAccount } from '@utils/tokens';
5+
import { DeltafiClaimFarmRewardsForm } from '@utils/uiTypes/proposalCreationTypes';
6+
import SelectDeltafiPool, { PoolName } from '@components/SelectDeltafiPool';
7+
import { useState } from 'react';
8+
import useDeltafiProgram from '@hooks/useDeltafiProgram';
9+
import claimFarmRewards from '@tools/sdk/deltafi/instructions/claimFarmRewards';
10+
11+
const schema = yup.object().shape({
12+
governedAccount: yup
13+
.object()
14+
.nullable()
15+
.required('Governed account is required'),
16+
poolName: yup.string().required('Pool name is required'),
17+
});
18+
19+
const DeltafiClaimFarmRewards = ({
20+
index,
21+
governedAccount,
22+
}: {
23+
index: number;
24+
governedAccount?: GovernedMultiTypeAccount;
25+
}) => {
26+
const { poolInfoList } = DeltafiDexV2.configuration;
27+
28+
const deltafiProgram = useDeltafiProgram();
29+
30+
const [poolInfo, setPoolInfo] = useState<PoolInfo | null>(null);
31+
32+
const {
33+
form,
34+
handleSetForm,
35+
} = useInstructionFormBuilder<DeltafiClaimFarmRewardsForm>({
36+
index,
37+
initialFormValues: {
38+
governedAccount,
39+
},
40+
schema,
41+
buildInstruction: async function ({ cluster, governedAccountPubkey }) {
42+
if (cluster !== 'mainnet') {
43+
throw new Error('Other cluster than mainnet are not supported yet.');
44+
}
45+
46+
if (!deltafiProgram) {
47+
throw new Error('Deltafi program not loaded yet');
48+
}
49+
50+
if (!poolInfo) {
51+
throw new Error('Pool info is required');
52+
}
53+
54+
if (!poolInfo.farmInfo) {
55+
throw new Error('Farm info is required');
56+
}
57+
58+
return claimFarmRewards({
59+
deltafiProgram,
60+
authority: governedAccountPubkey,
61+
poolInfo,
62+
farmInfo: poolInfo.farmInfo,
63+
});
64+
},
65+
});
66+
67+
return (
68+
<>
69+
<SelectDeltafiPool
70+
title="Pool"
71+
poolInfoList={poolInfoList}
72+
selectedValue={form.poolName}
73+
onSelect={(poolName: PoolName) => {
74+
const poolInfo = poolInfoList.find(({ name }) => name === poolName);
75+
76+
setPoolInfo(poolInfo ?? null);
77+
78+
handleSetForm({
79+
value: poolName,
80+
propertyName: 'poolName',
81+
});
82+
}}
83+
/>
84+
85+
{poolInfo && !poolInfo.farmInfo ? (
86+
<div className="mt-2 text-sm">This pool does not contains a farm</div>
87+
) : null}
88+
</>
89+
);
90+
};
91+
92+
export default DeltafiClaimFarmRewards;

pages/dao/[symbol]/proposal/components/instructions/SelectedInstruction.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import DeltafiPoolWithdraw from './Deltafi/Withdraw';
7171
import DeltafiCreateFarmUser from './Deltafi/CreateFarmUserV2';
7272
import DeltafiDepositToFarm from './Deltafi/DepositToFarm';
7373
import DeltafiFarmWithdraw from './Deltafi/WithdrawFromFarm';
74+
import DeltafiClaimFarmRewards from './Deltafi/ClaimFarmRewards';
7475
import NativeBurnSplTokens from './Native/BurnSplTokens';
7576
import OrcaWhirlpoolOpenPosition from './Orca/WhirlpoolOpenPosition';
7677
import OrcaWhirlpoolIncreaseLiquidity from './Orca/WhirlpoolIncreaseLiquidity';
@@ -498,6 +499,13 @@ const SelectedInstruction = ({
498499
return (
499500
<DeltafiFarmWithdraw index={index} governedAccount={governedAccount} />
500501
);
502+
case InstructionEnum.DeltafiClaimFarmRewards:
503+
return (
504+
<DeltafiClaimFarmRewards
505+
index={index}
506+
governedAccount={governedAccount}
507+
/>
508+
);
501509
case InstructionEnum.NativeBurnSplTokens:
502510
return (
503511
<NativeBurnSplTokens index={index} governedAccount={governedAccount} />

tools/sdk/deltafi/configuration.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Connection, PublicKey } from '@solana/web3.js';
22
import { deltafiDexV2 } from './idl/deltafi';
3-
43
import { newProgram } from '@saberhq/anchor-contrib';
54
import { SolanaAugmentedProvider } from '@saberhq/solana-contrib';
65
import { DeltafiProgram } from './program/deltafi';
@@ -67,6 +66,14 @@ export class DeltafiDexV2 {
6766
'GNExJhNUhc9LN2DauuQAUJnXoy6DJ6zey3t9kT9A2PF3',
6867
);
6968

69+
public static readonly deltafiMint = new PublicKey(
70+
'de1QJkP1qDCk5JYCCXCeq27bQQUdCaiv7xVKFrhPSzF',
71+
);
72+
73+
public static readonly deltafiToken = new PublicKey(
74+
'7Xay86NUjUp8Lzv7kAYp8TqVG8eoWf8qzwbYt6tYoPCP',
75+
);
76+
7077
public static readonly instructionsCode = {
7178
CreateLiquidityProviderV2: 173,
7279
DepositToStableSwap: 54,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { PublicKey } from '@solana/web3.js';
2+
import { DeltafiProgram } from '../program/deltafi';
3+
import deltafiConfiguration, {
4+
FarmInfo,
5+
PoolInfo,
6+
DeltafiDexV2,
7+
} from '../configuration';
8+
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
9+
import { findATAAddrSync } from '@utils/ataTools';
10+
11+
export default async function claimFarmRewards({
12+
deltafiProgram,
13+
authority,
14+
poolInfo,
15+
farmInfo,
16+
}: {
17+
deltafiProgram: DeltafiProgram;
18+
authority: PublicKey;
19+
poolInfo: PoolInfo;
20+
farmInfo: FarmInfo;
21+
}) {
22+
const [{ configKey }, [farmUser]] = await Promise.all([
23+
deltafiProgram.account.swapInfo.fetch(poolInfo.swapInfo),
24+
25+
deltafiConfiguration.findFarmUserAddress({
26+
farmAddress: farmInfo.address,
27+
authority,
28+
}),
29+
]);
30+
31+
const farmUserInfo = await deltafiProgram.account.farmUser.fetchNullable(
32+
farmUser,
33+
);
34+
const farmInfoAccount = await deltafiProgram.account.farmInfo.fetch(
35+
farmInfo.address,
36+
);
37+
const swapInfo = await deltafiProgram.account.swapInfo.fetch(
38+
poolInfo.swapInfo,
39+
);
40+
41+
console.log('farmUserInfo', farmUserInfo);
42+
console.log('farmInfo', farmInfoAccount);
43+
console.log('swapInfo', swapInfo);
44+
45+
if (!farmUserInfo) {
46+
throw new Error('Farm user is required. Please create it beforehand.');
47+
}
48+
49+
const [userDeltafiToken] = findATAAddrSync(
50+
authority,
51+
DeltafiDexV2.deltafiMint,
52+
);
53+
54+
return deltafiProgram.instruction.claimFarmRewards({
55+
accounts: {
56+
marketConfig: configKey.toBase58(),
57+
swapInfo: poolInfo.swapInfo,
58+
farmInfo: farmInfo.address,
59+
farmUser,
60+
userDeltafiToken,
61+
swapDeltafiToken: DeltafiDexV2.deltafiToken,
62+
owner: authority,
63+
tokenProgram: TOKEN_PROGRAM_ID,
64+
},
65+
});
66+
}

utils/uiTypes/proposalCreationTypes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,11 @@ export interface DeltafiCreateFarmUserForm {
533533
poolName?: DeltafiPoolName;
534534
}
535535

536+
export interface DeltafiClaimFarmRewardsForm {
537+
governedAccount?: GovernedMultiTypeAccount;
538+
poolName?: DeltafiPoolName;
539+
}
540+
536541
export interface NativeBurnSplTokensForm {
537542
governedAccount?: GovernedMultiTypeAccount;
538543
uiAmount?: number;
@@ -692,6 +697,7 @@ export enum InstructionEnum {
692697
DeltafiCreateFarmUser,
693698
DeltafiFarmDeposit,
694699
DeltafiFarmWithdraw,
700+
DeltafiClaimFarmRewards,
695701
NativeBurnSplTokens,
696702
OrcaWhirlpoolOpenPosition,
697703
OrcaWhirlpoolIncreaseLiquidity,

0 commit comments

Comments
 (0)