Skip to content

Commit 817c917

Browse files
committed
refactor(sdk-coin-sol): change marinade and jito flags to enum
Ticket: SC-2620
1 parent 0f20764 commit 817c917

File tree

10 files changed

+197
-191
lines changed

10 files changed

+197
-191
lines changed

examples/ts/sol/stake-jito.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ async function main() {
5959
.sender(account.publicKey.toBase58())
6060
.stakingAddress(JITO_STAKE_POOL_ADDRESS)
6161
.validator(JITO_STAKE_POOL_ADDRESS)
62-
.isJito(true)
63-
.jitoParams({
62+
.stakingTypeParams({
63+
type: 'JITO',
6464
stakePoolData: {
6565
managerFeeAccount: stakePoolAccount.account.data.managerFeeAccount.toString(),
6666
poolMint: stakePoolAccount.account.data.poolMint.toString(),

modules/sdk-coin-sol/src/lib/iface.ts

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,32 @@ export interface Approve {
120120
};
121121
}
122122

123+
export interface JitoStakingActivateParams {
124+
type: 'JITO';
125+
stakePoolData: DepositSolStakePoolData;
126+
}
127+
128+
export interface MarinadeStakingActivateParams {
129+
type: 'MARINADE';
130+
}
131+
132+
export interface NativeStakingActivateParams {
133+
type: 'NATIVE';
134+
}
135+
136+
export type StakingActivateStakingTypeParams =
137+
| JitoStakingActivateParams
138+
| MarinadeStakingActivateParams
139+
| NativeStakingActivateParams;
140+
123141
export interface StakingActivate {
124142
type: InstructionBuilderTypes.StakingActivate;
125143
params: {
126144
fromAddress: string;
127145
stakingAddress: string;
128146
amount: string;
129147
validator: string;
130-
isMarinade?: boolean;
131-
isJito?: boolean;
132-
jitoParams?: {
133-
stakePoolData: DepositSolStakePoolData;
134-
};
148+
stakingTypeParams: StakingActivateStakingTypeParams;
135149
};
136150
}
137151

@@ -140,20 +154,34 @@ export interface StakingDelegate {
140154
params: { stakingAddress: string; fromAddress: string; validator: string };
141155
}
142156

157+
export interface JitoStakingDeactivateParams {
158+
type: 'JITO';
159+
stakePoolData: WithdrawStakeStakePoolData;
160+
validatorAddress: string;
161+
transferAuthorityAddress: string;
162+
}
163+
164+
export interface MarinadeStakingDeactivateParams {
165+
type: 'MARINADE';
166+
}
167+
168+
export interface NativeStakingDeactivateParams {
169+
type: 'NATIVE';
170+
}
171+
172+
export type StakingDeactivateStakingTypeParams =
173+
| JitoStakingDeactivateParams
174+
| MarinadeStakingDeactivateParams
175+
| NativeStakingDeactivateParams;
176+
143177
export interface StakingDeactivate {
144178
type: InstructionBuilderTypes.StakingDeactivate;
145179
params: {
146180
fromAddress: string;
147181
stakingAddress: string;
148182
amount?: string;
149183
unstakingAddress?: string;
150-
isMarinade?: boolean;
151-
isJito?: boolean;
152-
jitoParams?: {
153-
stakePoolData: WithdrawStakeStakePoolData;
154-
validatorAddress: string;
155-
transferAuthorityAddress: string;
156-
};
184+
stakingTypeParams: StakingDeactivateStakingTypeParams;
157185
recipients?: Recipient[];
158186
};
159187
}

modules/sdk-coin-sol/src/lib/instructionParamsFactory.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -421,19 +421,22 @@ function parseStakingActivateInstructions(
421421
stakingInstructions.initialize?.authorized.staker.toString() ||
422422
stakingInstructions.depositSol?.stakePool.toString() ||
423423
'',
424-
isMarinade: stakingInstructionsIsMarinade(stakingInstructions),
425-
isJito: stakingInstructionsIsJito(stakingInstructions),
426-
...(stakingInstructions.depositSol && stakingInstructionsIsJito(stakingInstructions)
424+
stakingTypeParams: stakingInstructionsIsMarinade(stakingInstructions)
427425
? {
428-
jitoParams: {
429-
stakePoolData: {
430-
managerFeeAccount: stakingInstructions.depositSol.managerFeeAccount.toString(),
431-
poolMint: stakingInstructions.depositSol.poolMint.toString(),
432-
reserveStake: stakingInstructions.depositSol.reserveStake.toString(),
433-
},
426+
type: 'MARINADE',
427+
}
428+
: stakingInstructions.depositSol && stakingInstructionsIsJito(stakingInstructions)
429+
? {
430+
type: 'JITO',
431+
stakePoolData: {
432+
managerFeeAccount: stakingInstructions.depositSol.managerFeeAccount.toString(),
433+
poolMint: stakingInstructions.depositSol.poolMint.toString(),
434+
reserveStake: stakingInstructions.depositSol.reserveStake.toString(),
434435
},
435436
}
436-
: {}),
437+
: {
438+
type: 'NATIVE',
439+
},
437440
},
438441
};
439442
instructionData.push(stakingActivate);
@@ -653,7 +656,24 @@ function parseStakingDeactivateInstructions(
653656
unstakingAddress:
654657
unstakingInstruction.split?.splitStakePubkey.toString() ||
655658
unstakingInstruction.withdrawStake?.destinationStake.toString(),
656-
isMarinade: isMarinade,
659+
stakingTypeParams: isMarinade
660+
? {
661+
type: 'MARINADE',
662+
}
663+
: unstakingInstruction.withdrawStake !== undefined
664+
? {
665+
type: 'JITO',
666+
stakePoolData: {
667+
managerFeeAccount: unstakingInstruction.withdrawStake.managerFeeAccount.toString(),
668+
poolMint: unstakingInstruction.withdrawStake.poolMint.toString(),
669+
validatorList: unstakingInstruction.withdrawStake.validatorList.toString(),
670+
},
671+
validatorAddress: unstakingInstruction.withdrawStake.validatorStake.toString(),
672+
transferAuthorityAddress: unstakingInstruction.withdrawStake.sourceTransferAuthority.toString(),
673+
}
674+
: {
675+
type: 'NATIVE',
676+
},
657677
recipients: isMarinade
658678
? [
659679
{
@@ -662,20 +682,6 @@ function parseStakingDeactivateInstructions(
662682
},
663683
]
664684
: undefined,
665-
...(unstakingInstruction.withdrawStake !== undefined
666-
? {
667-
isJito: unstakingInstruction.withdrawStake !== undefined,
668-
jitoParams: unstakingInstruction.withdrawStake && {
669-
stakePoolData: {
670-
managerFeeAccount: unstakingInstruction.withdrawStake.managerFeeAccount.toString(),
671-
poolMint: unstakingInstruction.withdrawStake.poolMint.toString(),
672-
validatorList: unstakingInstruction.withdrawStake.validatorList.toString(),
673-
},
674-
validatorAddress: unstakingInstruction.withdrawStake.validatorStake.toString(),
675-
transferAuthorityAddress: unstakingInstruction.withdrawStake.sourceTransferAuthority.toString(),
676-
},
677-
}
678-
: {}),
679685
},
680686
};
681687
instructionData.push(stakingDeactivate);

modules/sdk-coin-sol/src/lib/solInstructionFactory.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -274,34 +274,30 @@ function createNonceAccountInstruction(data: WalletInit): TransactionInstruction
274274
*/
275275
function stakingInitializeInstruction(data: StakingActivate): TransactionInstruction[] {
276276
const {
277-
params: { fromAddress, stakingAddress, amount, validator, isMarinade, isJito, jitoParams },
277+
params: { fromAddress, stakingAddress, amount, validator, stakingTypeParams },
278278
} = data;
279279
assert(fromAddress, 'Missing fromAddress param');
280280
assert(stakingAddress, 'Missing stakingAddress param');
281281
assert(amount, 'Missing amount param');
282282
assert(validator, 'Missing validator param');
283-
assert(isMarinade !== undefined, 'Missing isMarinade param');
284-
assert(isJito !== undefined, 'Missing isJito param');
285-
assert([isMarinade, isJito].filter((x) => x).length <= 1, 'At most one of isMarinade and isJito can be true');
283+
assert(stakingTypeParams !== undefined, 'Missing stakingTypeParams param');
286284

287285
const fromPubkey = new PublicKey(fromAddress);
288286
const stakePubkey = new PublicKey(stakingAddress);
289287
const validatorPubkey = new PublicKey(validator);
290288
const tx = new Transaction();
291289

292-
if (isJito) {
293-
assert(jitoParams, 'missing jitoParams param');
294-
290+
if (stakingTypeParams.type === 'JITO') {
295291
const instructions = depositSolInstructions(
296292
{
297293
stakePoolAddress: stakePubkey,
298294
from: fromPubkey,
299295
lamports: BigInt(amount),
300296
},
301-
jitoParams.stakePoolData
297+
stakingTypeParams.stakePoolData
302298
);
303299
tx.add(...instructions);
304-
} else if (isMarinade) {
300+
} else if (stakingTypeParams.type === 'MARINADE') {
305301
const walletInitStaking = StakeProgram.createAccount({
306302
fromPubkey,
307303
stakePubkey,
@@ -339,18 +335,16 @@ function stakingInitializeInstruction(data: StakingActivate): TransactionInstruc
339335
*/
340336
function stakingDeactivateInstruction(data: StakingDeactivate): TransactionInstruction[] {
341337
const {
342-
params: { fromAddress, stakingAddress, amount, unstakingAddress, isMarinade, isJito, recipients, jitoParams },
338+
params: { fromAddress, stakingAddress, amount, unstakingAddress, recipients, stakingTypeParams },
343339
} = data;
344340
assert(fromAddress, 'Missing fromAddress param');
345-
if (!isMarinade) {
341+
if (stakingTypeParams.type !== 'MARINADE') {
346342
assert(stakingAddress, 'Missing stakingAddress param');
347343
}
348-
assert([isMarinade, isJito].filter((x) => x).length <= 1, 'At most one of isMarinade and isJito can be true');
349344

350-
if (isJito) {
345+
if (stakingTypeParams.type === 'JITO') {
351346
assert(unstakingAddress, 'Missing unstakingAddress param');
352347
assert(amount, 'Missing amount param');
353-
assert(jitoParams, 'Missing jitoParams param');
354348

355349
const tx = new Transaction();
356350
tx.add(
@@ -359,15 +353,15 @@ function stakingDeactivateInstruction(data: StakingDeactivate): TransactionInstr
359353
stakePoolAddress: new PublicKey(stakingAddress),
360354
tokenOwner: new PublicKey(fromAddress),
361355
destinationStakeAccount: new PublicKey(unstakingAddress),
362-
validatorAddress: new PublicKey(jitoParams.validatorAddress),
363-
transferAuthority: new PublicKey(jitoParams.transferAuthorityAddress),
356+
validatorAddress: new PublicKey(stakingTypeParams.validatorAddress),
357+
transferAuthority: new PublicKey(stakingTypeParams.transferAuthorityAddress),
364358
poolAmount: amount,
365359
},
366-
jitoParams.stakePoolData
360+
stakingTypeParams.stakePoolData
367361
)
368362
);
369363
return tx.instructions;
370-
} else if (isMarinade) {
364+
} else if (stakingTypeParams.type === 'MARINADE') {
371365
assert(recipients, 'Missing recipients param');
372366

373367
const tx = new Transaction();

modules/sdk-coin-sol/src/lib/stakingActivateBuilder.ts

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ import { TransactionBuilder } from './transactionBuilder';
55
import { InstructionBuilderTypes } from './constants';
66

77
import assert from 'assert';
8-
import { StakingActivate } from './iface';
8+
import { StakingActivate, StakingActivateStakingTypeParams } from './iface';
99
import { isValidStakingAmount, validateAddress } from './utils';
1010

1111
export class StakingActivateBuilder extends TransactionBuilder {
1212
protected _amount: string;
1313
protected _stakingAddress: string;
1414
protected _validator: string;
15-
protected _isMarinade = false;
16-
protected _isJito = false;
17-
protected _jitoParams?: StakingActivate['params']['jitoParams'];
15+
protected _stakingTypeParams: StakingActivateStakingTypeParams = { type: 'NATIVE' };
1816

1917
constructor(_coinConfig: Readonly<CoinConfig>) {
2018
super(_coinConfig);
@@ -34,9 +32,7 @@ export class StakingActivateBuilder extends TransactionBuilder {
3432
this.stakingAddress(activateInstruction.params.stakingAddress);
3533
this.amount(activateInstruction.params.amount);
3634
this.validator(activateInstruction.params.validator);
37-
this.isMarinade(activateInstruction.params.isMarinade ?? false);
38-
this.isJito(activateInstruction.params.isJito ?? false);
39-
this.jitoParams(activateInstruction.params.jitoParams);
35+
this.stakingTypeParams(activateInstruction.params.stakingTypeParams);
4036
}
4137
}
4238
}
@@ -84,34 +80,13 @@ export class StakingActivateBuilder extends TransactionBuilder {
8480
}
8581

8682
/**
87-
* Set isMarinade flag
88-
* @param {boolean} flag - true if the transaction is for Marinade, false by default if not set
89-
* @returns {StakingActivateBuilder} This staking builder
90-
*/
91-
isMarinade(flag: boolean): this {
92-
this._isMarinade = flag;
93-
return this;
94-
}
95-
96-
/**
97-
* Set isJito flag
98-
* @param {boolean} flag - true if the transaction is for Jito, false by default if not set
99-
* @returns {StakingActivateBuilder} This staking builder
100-
*/
101-
isJito(flag: boolean): this {
102-
this._isJito = flag;
103-
// this._coinConfig.network.type === NetworkType.TESTNET
104-
return this;
105-
}
106-
107-
/**
108-
* Set parameters specific to Jito staking.
83+
* Set parameters specific to a staking type.
10984
*
110-
* @param {string} jitoParams parameters specific to Jito staking.
85+
* @param {StakingActivateStakingTypeParams} stakingTypeParams parameters specific to a staking type.
11186
* @returns {StakingActivateBuilder} This staking builder.
11287
*/
113-
jitoParams(jitoParams: StakingActivate['params']['jitoParams']): this {
114-
this._jitoParams = jitoParams;
88+
stakingTypeParams(stakingTypeParams: StakingActivateStakingTypeParams): this {
89+
this._stakingTypeParams = stakingTypeParams;
11590
return this;
11691
}
11792

@@ -121,12 +96,7 @@ export class StakingActivateBuilder extends TransactionBuilder {
12196
assert(this._stakingAddress, 'Staking Address must be set before building the transaction');
12297
assert(this._validator, 'Validator must be set before building the transaction');
12398
assert(this._amount, 'Amount must be set before building the transaction');
124-
assert(this._isMarinade !== undefined, 'isMarinade must be set before building the transaction');
125-
assert(this._isJito !== undefined, 'isJito must be set before building the transaction');
126-
assert(
127-
[this._isMarinade, this._isJito].filter((x) => x).length <= 1,
128-
'At most one of isMarinade and isJito can be true'
129-
);
99+
assert(this._stakingTypeParams, 'Staking type params must be set before building the transaction');
130100

131101
if (this._sender === this._stakingAddress) {
132102
throw new BuildTransactionError('Sender address cannot be the same as the Staking address');
@@ -139,9 +109,7 @@ export class StakingActivateBuilder extends TransactionBuilder {
139109
stakingAddress: this._stakingAddress,
140110
amount: this._amount,
141111
validator: this._validator,
142-
isMarinade: this._isMarinade,
143-
isJito: this._isJito,
144-
jitoParams: this._jitoParams,
112+
stakingTypeParams: this._stakingTypeParams,
145113
},
146114
};
147115
this._instructionsData = [stakingAccountData];

0 commit comments

Comments
 (0)