Skip to content

Commit 2b2f110

Browse files
authored
Merge pull request #77 from macalinao/igm/replica-miner-init
various improvements to quarry/react-quarry
2 parents 6e3a376 + 965dd4f commit 2b2f110

33 files changed

+684
-449
lines changed

.changeset/blue-glasses-matter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@macalinao/quarry": patch
3+
---
4+
5+
Fix deposit/withdraw bugs

.changeset/giant-islands-stand.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@macalinao/react-quarry": minor
3+
"@macalinao/quarry": minor
4+
"@macalinao/grill": minor
5+
---
6+
7+
Fix bugs around quarry merge miner initialization, more pda hooks

bun.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
"version": "0.1.0",
149149
"dependencies": {
150150
"@macalinao/clients-quarry": "^0.4.2",
151+
"@macalinao/gill-extra": "workspace:*",
151152
"@macalinao/token-utils": "workspace:*",
152153
"@solana-program/token": "^0.6.0",
153154
"nonempty-array": "^0.1.4",

packages/grill/src/hooks/create-pda-hook.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { PdaFn } from "@macalinao/gill-extra";
2+
import type { Address } from "@solana/kit";
23
import { useQuery } from "@tanstack/react-query";
34
import { createPdaQuery } from "./pda-query-utils.js";
45

56
/**
67
* Hook for computing a PDA from some arguments.
78
*/
8-
export type PdaHook<TArgs, TResult> = (
9+
export type PdaHook<TArgs, TResult = Address> = (
910
args: TArgs | null | undefined,
1011
) => TResult | null | undefined;
1112

packages/grill/src/hooks/create-pdas-hook.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import type { PdaFn } from "@macalinao/gill-extra";
2+
import type { Address } from "@solana/kit";
23
import { useQueries } from "@tanstack/react-query";
34
import { createPdaQuery } from "./pda-query-utils.js";
45

5-
export type PdasHook<TArgs, TResult> = <T extends readonly TArgs[]>(
6+
/**
7+
* Hook for computing multiple PDAs from some arguments.
8+
*/
9+
export type PdasHook<TArgs, TResult = Address> = <T extends readonly TArgs[]>(
610
args: T | null | undefined,
711
) => { [K in keyof T]: TResult } | null | undefined;
812

packages/quarry/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
},
6060
"dependencies": {
6161
"@macalinao/clients-quarry": "^0.4.2",
62+
"@macalinao/gill-extra": "workspace:*",
6263
"@macalinao/token-utils": "workspace:*",
6364
"@solana-program/token": "^0.6.0",
6465
"nonempty-array": "^0.1.4"

packages/quarry/src/ixs/merge-miner/claim-rewards.ts

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import type { MergePool, Rewarder } from "@macalinao/clients-quarry";
2+
import type { AccountInfo } from "@macalinao/gill-extra";
23
import type { Address, Instruction, TransactionSigner } from "@solana/kit";
34
import {
45
findMergeMinerPda,
56
findMinerPda,
6-
findMinterPda,
77
findQuarryPda,
8-
getClaimRewardsMMInstruction,
9-
getWithdrawTokensMMInstruction,
10-
QUARRY_MINT_WRAPPER_PROGRAM_ADDRESS,
8+
getClaimRewardsMMInstructionAsync,
9+
getWithdrawTokensMMInstructionAsync,
1110
} from "@macalinao/clients-quarry";
1211
import {
1312
findAssociatedTokenPda,
@@ -24,10 +23,12 @@ export interface QuarryStakeAccounts {
2423
minerVault: Address;
2524
}
2625

27-
export interface MergePoolAccount {
28-
address: Address;
29-
data: Pick<MergePool, "primaryMint" | "replicaMint">;
30-
}
26+
/**
27+
* A merge pool account with primary/replica mint exposed.
28+
*/
29+
export type MergePoolAccount = AccountInfo<
30+
Pick<MergePool, "primaryMint" | "replicaMint">
31+
>;
3132

3233
export interface ClaimRewardsCommonArgs {
3334
quarryMint: Address;
@@ -57,12 +58,6 @@ export async function claimRewardsCommon({
5758
}: ClaimRewardsCommonArgs): Promise<Instruction[]> {
5859
const instructions: Instruction[] = [];
5960

60-
// Derive the minter PDA
61-
const [minter] = await findMinterPda({
62-
wrapper: rewarder.data.mintWrapper,
63-
authority: stake.rewarder,
64-
});
65-
6661
// Get or create associated token accounts for the merge miner
6762
const [mmQuarryTokenAccount] = await findAssociatedTokenPda({
6863
mint: quarryMint,
@@ -119,30 +114,23 @@ export async function claimRewardsCommon({
119114

120115
// Claim rewards instruction
121116
instructions.push(
122-
getClaimRewardsMMInstruction({
117+
await getClaimRewardsMMInstructionAsync({
123118
mintWrapper: rewarder.data.mintWrapper,
124-
mintWrapperProgram: QUARRY_MINT_WRAPPER_PROGRAM_ADDRESS,
125-
minter,
126119
rewardsTokenMint: rewarder.data.rewardsTokenMint,
127-
rewardsTokenAccount: mmRewardsTokenAccount,
128-
claimFeeTokenAccount: rewarder.data.claimFeeTokenAccount,
129120
stakeTokenAccount: mmQuarryTokenAccount,
130121
pool: stake.pool,
131122
mm: stake.mm,
132123
rewarder: stake.rewarder,
133124
quarry: stake.quarry,
134-
miner: stake.miner,
135125
minerVault: stake.minerVault,
136126
}),
137127
);
138128

139129
// Withdraw rewards tokens from merge miner to owner
140130
instructions.push(
141-
getWithdrawTokensMMInstruction({
131+
await getWithdrawTokensMMInstructionAsync({
142132
owner: mmOwner,
143133
pool: stake.pool,
144-
mm: stake.mm,
145-
mmTokenAccount: mmRewardsTokenAccount,
146134
withdrawMint: rewarder.data.rewardsTokenMint,
147135
tokenDestination: ownerRewardsTokenAccount,
148136
}),
@@ -161,13 +149,9 @@ export async function claimPrimaryRewards({
161149
}: {
162150
mergePool: MergePoolAccount;
163151
mmOwner: TransactionSigner;
164-
rewarder: {
165-
address: Address;
166-
data: Pick<
167-
Rewarder,
168-
"mintWrapper" | "rewardsTokenMint" | "claimFeeTokenAccount"
169-
>;
170-
};
152+
rewarder: AccountInfo<
153+
Pick<Rewarder, "mintWrapper" | "rewardsTokenMint" | "claimFeeTokenAccount">
154+
>;
171155
}): Promise<Instruction[]> {
172156
const stake = await getPrimaryStakeAccounts({
173157
rewarder: rewarder.address,

packages/quarry/src/ixs/merge-miner/deposit.ts renamed to packages/quarry/src/ixs/merge-miner/create-deposit-merge-miner-ixs.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
getTransferInstruction,
88
TOKEN_PROGRAM_ADDRESS,
99
} from "@solana-program/token";
10-
import { getCreateInitMergeMinerIxs } from "./init.js";
10+
import { getCreateInitMergeMinerIxs } from "./init/get-create-init-merge-miner-ixs.js";
1111
import {
1212
createStakePrimaryMinerIx,
1313
createStakeReplicaMinerIx,
@@ -19,6 +19,7 @@ import {
1919
export interface DepositMergeMinerArgs extends MergeMinerAmountArgs {
2020
/** The merge miner owner (defaults to payer) */
2121
mmOwner?: TransactionSigner;
22+
initMergeMiner?: boolean;
2223
}
2324

2425
/**
@@ -43,6 +44,7 @@ export async function createDepositMergeMinerIxs({
4344
payer,
4445
replicaRewarders = [],
4546
mmOwner = payer,
47+
initMergeMiner,
4648
}: DepositMergeMinerArgs): Promise<{
4749
ixs: Instruction[];
4850
}> {
@@ -55,14 +57,17 @@ export async function createDepositMergeMinerIxs({
5557
tokenProgram: TOKEN_PROGRAM_ADDRESS,
5658
});
5759

58-
// Initialize merge miner and primary miner if needed
59-
const { ixs: initIxs } = await getCreateInitMergeMinerIxs({
60-
rewarder,
61-
mergePool,
62-
owner: mmOwner.address,
63-
payer,
64-
});
65-
instructions.push(...initIxs);
60+
if (initMergeMiner) {
61+
// Initialize merge miner and primary miner if needed
62+
const { ixs: initIxs } = await getCreateInitMergeMinerIxs({
63+
rewarder,
64+
replicaRewarders,
65+
mergePool,
66+
owner: mmOwner.address,
67+
payer,
68+
});
69+
instructions.push(...initIxs);
70+
}
6671

6772
// Get merge miner address
6873
const [mmAddress] = await findMergeMinerPda({

packages/quarry/src/ixs/merge-miner/withdraw.ts renamed to packages/quarry/src/ixs/merge-miner/create-withdraw-merge-miner-ixs.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Address, Instruction, TransactionSigner } from "@solana/kit";
22
import type { MergeMinerAmountArgs } from "./types.js";
33
import {
44
findMergeMinerPda,
5-
getWithdrawTokensMMInstruction,
5+
getWithdrawTokensMMInstructionAsync,
66
} from "@macalinao/clients-quarry";
77
import {
88
findAssociatedTokenPda,
@@ -21,7 +21,11 @@ import {
2121
export interface WithdrawMergeMinerArgs extends MergeMinerAmountArgs {
2222
/** Owner of the merge miner */
2323
owner: TransactionSigner;
24-
/** Destination of the tokens, defaults to the owner's primary ATA */
24+
/**
25+
* Destination of the tokens, defaults to the owner's primary ATA
26+
*
27+
* Note: this must be a token account.
28+
*/
2529
tokenDestination?: Address;
2630
}
2731

@@ -37,12 +41,6 @@ export async function createWithdrawMergeMinerIxs({
3741
owner,
3842
payer,
3943
}: WithdrawMergeMinerArgs): Promise<Instruction[]> {
40-
const [mmTokenAccount] = await findAssociatedTokenPda({
41-
mint: mergePool.data.primaryMint,
42-
owner: owner.address,
43-
tokenProgram: TOKEN_PROGRAM_ADDRESS,
44-
});
45-
4644
// Get merge miner address
4745
const [mmAddress] = await findMergeMinerPda({
4846
pool: mergePool.address,
@@ -71,19 +69,6 @@ export async function createWithdrawMergeMinerIxs({
7169
primaryMint: mergePool.data.primaryMint,
7270
amount,
7371
}),
74-
// Stake replica tokens back into the merge miner
75-
...(await Promise.all(
76-
replicaRewarders.map(
77-
async (replicaRewarder) =>
78-
await createStakeReplicaMinerIx({
79-
mmOwner: owner,
80-
pool: mergePool.address,
81-
mm: mmAddress,
82-
rewarder: replicaRewarder,
83-
replicaMint: mergePool.data.replicaMint,
84-
}),
85-
),
86-
)),
8772
];
8873

8974
if (!tokenDestination) {
@@ -104,15 +89,29 @@ export async function createWithdrawMergeMinerIxs({
10489
tokenDestination = destinationATA;
10590
}
10691

107-
const withdrawIx = getWithdrawTokensMMInstruction({
92+
const withdrawIx = await getWithdrawTokensMMInstructionAsync({
10893
owner,
10994
pool: mergePool.address,
110-
mm: mmAddress,
111-
mmTokenAccount,
11295
withdrawMint: mergePool.data.primaryMint,
11396
tokenDestination,
11497
});
11598
ixs.push(withdrawIx);
11699

100+
ixs.push(
101+
// Stake replica tokens back into the merge miner
102+
...(await Promise.all(
103+
replicaRewarders.map(
104+
async (replicaRewarder) =>
105+
await createStakeReplicaMinerIx({
106+
mmOwner: owner,
107+
pool: mergePool.address,
108+
mm: mmAddress,
109+
rewarder: replicaRewarder,
110+
replicaMint: mergePool.data.replicaMint,
111+
}),
112+
),
113+
)),
114+
);
115+
117116
return ixs;
118117
}

packages/quarry/src/ixs/merge-miner/helpers.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Address } from "@solana/kit";
22
import type { MinerAddresses } from "./types.js";
33
import {
4-
findMergeMinerPda,
54
findMergePoolPda,
65
findMinerPda,
76
findQuarryPda,
@@ -34,23 +33,6 @@ export async function getMergePoolAddresses({
3433
return { pool, replicaMint };
3534
}
3635

37-
/**
38-
* Helper to get the merge miner PDA
39-
*/
40-
export async function getMergeMinerAddress({
41-
pool,
42-
owner,
43-
}: {
44-
pool: Address;
45-
owner: Address;
46-
}): Promise<Address> {
47-
const [mm] = await findMergeMinerPda({
48-
pool,
49-
owner,
50-
});
51-
return mm;
52-
}
53-
5436
/**
5537
* Helper to derive miner addresses from a mint and authority
5638
*/

0 commit comments

Comments
 (0)