• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bmresearch / Solnet / 15456731198

05 Jun 2025 02:04AM UTC coverage: 66.052% (-4.7%) from 70.792%
15456731198

Pull #497

github

web-flow
Merge e0afe0e16 into 632c6bef2
Pull Request #497: Stakepool program

1161 of 2022 branches covered (57.42%)

Branch coverage included in aggregate %.

704 of 1521 new or added lines in 12 files covered. (46.29%)

5822 of 8550 relevant lines covered (68.09%)

987890.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.49
/src/Solnet.Programs/StakePool/StakePoolProgramData.cs
1
using Solnet.Programs.StakePool.Models;
2
using Solnet.Programs.Utilities;
3
using Solnet.Wallet;
4
using System;
5
using System.Collections.Generic;
6
using System.Text;
7

8
namespace Solnet.Programs.StakePool
9
{
10
    internal static class StakePoolProgramData
11
    {
12
        /// <summary>
13
        /// The offset at which the value which defines the program method begins. 
14
        /// </summary>
15
        internal const int MethodOffset = 0;
16

17

18
        /// <summary>
19
        /// Encodes the 'Initialize' instruction data.
20
        /// </summary>
21
        /// <param name="fee"></param>
22
        /// <param name="withdrawalFee"></param>
23
        /// <param name="depositFee"></param>
24
        /// <param name="referralFee"></param>
25
        /// <param name="maxValidators"></param>
26
        /// <returns></returns>
27
        internal static byte[] EncodeInitializeData(Fee fee, Fee withdrawalFee, Fee depositFee, Fee referralFee, uint? maxValidators)
28
        {
29
            byte[] data = new byte[72];
1✔
30
            data.WriteU32((uint)StakePoolProgramInstructions.Values.Initialize, MethodOffset);
1✔
31
            data.WriteU64(fee.Numerator, MethodOffset + 4); // Serialize Fee as Numerator and Denominator
1✔
32
            data.WriteU64(fee.Denominator, MethodOffset + 12);
1✔
33
            data.WriteU64(withdrawalFee.Numerator, MethodOffset + 20);
1✔
34
            data.WriteU64(withdrawalFee.Denominator, MethodOffset + 28);
1✔
35
            data.WriteU64(depositFee.Numerator, MethodOffset + 36);
1✔
36
            data.WriteU64(depositFee.Denominator, MethodOffset + 44);
1✔
37
            data.WriteU64(referralFee.Numerator, MethodOffset + 52);
1✔
38
            data.WriteU64(referralFee.Denominator, MethodOffset + 60);
1✔
39
            data.WriteU32(maxValidators ?? 0, MethodOffset + 68);
1✔
40
            return data;
1✔
41
        }
42

43

44
        /// <summary>
45
        /// Encodes the 'AddValidatorToPool' instruction data.
46
        /// </summary>
47
        /// <param name="seed"></param>
48
        /// <returns></returns>
49
        internal static byte[] EncodeAddValidatorToPoolData(uint? seed)
50
        {
51
            byte[] data = new byte[8];
1✔
52
            var seedValue = seed ?? 0;
1✔
53
            data.WriteU32((uint)StakePoolProgramInstructions.Values.AddValidatorToPool, MethodOffset);
1✔
54
            data.WriteU32(seedValue, MethodOffset + 4);
1✔
55
            // Assuming the enum or data structure you're sending in the instruction is properly serialized here
56
            return data; // Example encoding, adjust based on actual data
1✔
57
        }
58

59
        /// <summary>
60
        /// Encodes the 'AddValidatorToPoolWithVote' instruction data.
61
        /// </summary>
62
        /// <returns></returns>
63
        internal static byte[] EncodeRemoveValidatorFromPoolData()
64
        {
65
            byte[] data = new byte[4];
1✔
66
            data.WriteU32((uint)StakePoolProgramInstructions.Values.RemoveValidatorFromPool, MethodOffset);
1✔
67
            // Here you would implement the serialization of removeData (e.g., using Borsh or another method)
68
            return data; // Example: return the serialized byte array
1✔
69
        }
70

71
        /// <summary>
72
        /// Encodes the 'DecreaseValidatorStake' instruction data.
73
        /// </summary>
74
        internal static byte[] EncodeDecreaseValidatorStakeData(ulong lamports, ulong transientStakeSeed)
75
        {
NEW
76
            byte[] data = new byte[20];
×
NEW
77
            data.WriteU32((uint)StakePoolProgramInstructions.Values.DecreaseValidatorStake, MethodOffset);
×
NEW
78
            data.WriteU64(lamports, MethodOffset + 4);
×
NEW
79
            data.WriteU64(transientStakeSeed, MethodOffset + 12);
×
80
            // Here you would implement the serialization of decreaseData (e.g., using Borsh or another method)
NEW
81
            return data; // Example: return the serialized byte array
×
82
        }
83

84

85
        /// <summary>
86
        /// Encodes the 'DecreaseAdditionalValidatorStake' instruction data.
87
        /// </summary>
88
        internal static byte[] EncodeDecreaseAdditionalValidatorStakeData(ulong lamports, ulong transientStakeSeed, ulong ephemeralStakeSeed)
89
        {
90
            byte[] data = new byte[28];
1✔
91
            data.WriteU32((uint)StakePoolProgramInstructions.Values.DecreaseAdditionalValidatorStake, MethodOffset);
1✔
92
            data.WriteU64(lamports, MethodOffset + 4);
1✔
93
            data.WriteU64(transientStakeSeed, MethodOffset + 12);
1✔
94
            data.WriteU64(ephemeralStakeSeed, MethodOffset + 20);
1✔
95
            // Here you would implement the serialization of decreaseData (e.g., using Borsh or another method)
96
            return data; // Example: return the serialized byte array
1✔
97
        }
98

99
        /// <summary>
100
        /// Encodes the 'DecreaseValidatorStakeWithReserve' instruction data.
101
        /// </summary>
102
        internal static byte[] EncodeDecreaseValidatorStakeWithReserveData(ulong lamports, ulong transientStakeSeed)
103
        {
104
            byte[] data = new byte[20];
2✔
105
            data.WriteU32((uint)StakePoolProgramInstructions.Values.DecreaseValidatorStakeWithReserve, MethodOffset);
2✔
106
            data.WriteU64(lamports, MethodOffset + 4);
2✔
107
            data.WriteU64(transientStakeSeed, MethodOffset + 12);
2✔
108
            // Implement the serialization of decreaseData (e.g., using Borsh or another method)
109
            return data; // Example: return the serialized byte array
2✔
110
        }
111

112
        /// <summary>
113
        /// Encodes the 'IncreaseValidatorStake' instruction data.
114
        /// </summary>
115
        /// <returns></returns>
116
        internal static byte[] EncodeIncreaseValidatorStakeData(ulong lamports, ulong transientStakeSeed)
117
        {
118
            byte[] data = new byte[28];
1✔
119
            data.WriteU32((uint)StakePoolProgramInstructions.Values.IncreaseValidatorStake, MethodOffset);
1✔
120
            data.WriteU64(lamports, MethodOffset + 4);
1✔
121
            data.WriteU64(transientStakeSeed, MethodOffset + 12);
1✔
122
            // Implement the serialization of increaseData (e.g., using Borsh or another method)
123
            return data; // Example: return the serialized byte array
1✔
124
        }
125

126
        /// <summary>
127
        /// Encodes the 'IncreaseAdditionalValidatorStake' instruction data.
128
        /// </summary>
129
        internal static byte[] EncodeIncreaseAdditionalValidatorStakeData(ulong lamports, ulong transientStakeSeed, ulong ephemeralStakeSeed)
130
        {
131
            byte[] data = new byte[28];
1✔
132
            data.WriteU32((uint)StakePoolProgramInstructions.Values.IncreaseValidatorStake, MethodOffset);
1✔
133
            data.WriteU64(lamports, MethodOffset + 4);
1✔
134
            data.WriteU64(transientStakeSeed, MethodOffset + 12);
1✔
135
            data.WriteU64(ephemeralStakeSeed, MethodOffset + 20);
1✔
136
            // Implement the serialization of increaseData (e.g., using Borsh or another method)
137
            return data; // Example: return the serialized byte array
1✔
138
        }
139

140
#nullable enable
141
        /// <summary>
142
        /// Encodes the 'SetPreferredDepositValidator' instruction data.
143
        /// </summary>
144
        internal static byte[] EncodeSetPreferredValidatorData(PreferredValidatorType validatorType)
145
        {
146
            // Allocate 8 bytes: 4 bytes for the discriminator and 4 bytes for the validator type.
147
            byte[] data = new byte[8];
1✔
148
            data.WriteU32((uint)StakePoolProgramInstructions.Values.SetPreferredValidator, MethodOffset);
1✔
149
            data.WriteU32((uint)validatorType, MethodOffset + 4);
1✔
150
            return data;
1✔
151
        }
152

153
        internal static byte[] EncodeSetFeeData(FeeType feeType)
154
        {
155
            // 4 bytes for method, 1 for discriminant, up to 16 for Fee, or 1 for byte
NEW
156
            var buffer = new List<byte>();
×
NEW
157
            buffer.AddRange(BitConverter.GetBytes((uint)StakePoolProgramInstructions.Values.SetFee));
×
158

159
            // Discriminant and value
160
            switch (feeType)
161
            {
162
                case FeeType.SolReferral solReferral:
NEW
163
                    buffer.Add(0); // Discriminant for SolReferral
×
NEW
164
                    buffer.Add(solReferral.Percentage);
×
NEW
165
                    break;
×
166
                case FeeType.StakeReferral stakeReferral:
NEW
167
                    buffer.Add(1);
×
NEW
168
                    buffer.Add(stakeReferral.Percentage);
×
NEW
169
                    break;
×
170
                case FeeType.Epoch epoch:
NEW
171
                    buffer.Add(2);
×
NEW
172
                    buffer.AddRange(BitConverter.GetBytes(epoch.Fee.Numerator));
×
NEW
173
                    buffer.AddRange(BitConverter.GetBytes(epoch.Fee.Denominator));
×
NEW
174
                    break;
×
175
                case FeeType.StakeWithdrawal stakeWithdrawal:
NEW
176
                    buffer.Add(3);
×
NEW
177
                    buffer.AddRange(BitConverter.GetBytes(stakeWithdrawal.Fee.Numerator));
×
NEW
178
                    buffer.AddRange(BitConverter.GetBytes(stakeWithdrawal.Fee.Denominator));
×
NEW
179
                    break;
×
180
                case FeeType.SolWithdrawal solWithdrawal:
NEW
181
                    buffer.Add(4);
×
NEW
182
                    buffer.AddRange(BitConverter.GetBytes(solWithdrawal.Fee.Numerator));
×
NEW
183
                    buffer.AddRange(BitConverter.GetBytes(solWithdrawal.Fee.Denominator));
×
NEW
184
                    break;
×
185
                case FeeType.SolDeposit solDeposit:
NEW
186
                    buffer.Add(5);
×
NEW
187
                    buffer.AddRange(BitConverter.GetBytes(solDeposit.Fee.Numerator));
×
NEW
188
                    buffer.AddRange(BitConverter.GetBytes(solDeposit.Fee.Denominator));
×
NEW
189
                    break;
×
190
                case FeeType.StakeDeposit stakeDeposit:
NEW
191
                    buffer.Add(6);
×
NEW
192
                    buffer.AddRange(BitConverter.GetBytes(stakeDeposit.Fee.Numerator));
×
NEW
193
                    buffer.AddRange(BitConverter.GetBytes(stakeDeposit.Fee.Denominator));
×
NEW
194
                    break;
×
195
                default:
NEW
196
                    throw new ArgumentException("Unknown FeeType variant");
×
197
            }
198

NEW
199
            return buffer.ToArray();
×
200
        }
201

202
        /// <summary>
203
        /// Encodes the 'SetFee' instruction data using a Fee object.
204
        /// </summary>
205
        /// <param name="fee">The fee to set.</param>
206
        /// <returns>A byte array containing the encoded instruction data.</returns>
207
        internal static byte[] EncodeSetFee(Fee fee)
208
        {
209
            // Allocate 20 bytes: 4 bytes for the discriminator and 16 bytes for the fee (8 for numerator and 8 for denominator).
210
            byte[] data = new byte[20];
1✔
211
            data.WriteU32((uint)StakePoolProgramInstructions.Values.SetFee, MethodOffset);
1✔
212
            data.WriteU64(fee.Numerator, MethodOffset + 4);
1✔
213
            data.WriteU64(fee.Denominator, MethodOffset + 12);
1✔
214
            return data;
1✔
215
        }
216

217
        /// <summary>
218
        /// Encodes the 'Redelegate' instruction data.
219
        /// </summary>
220
        internal static byte[] EncodeRedelegateData(ulong lamports, ulong sourceTransientStakeSeed, ulong ephemeralStakeSeed, ulong destinationTransientStakeSeed)
221
        {
222
            // 4 bytes for discriminator + 8 bytes each for 4 fields = 36 bytes total.
NEW
223
            byte[] data = new byte[36];
×
NEW
224
            data.WriteU32((uint)StakePoolProgramInstructions.Values.Redelegate, MethodOffset);
×
NEW
225
            data.WriteU64(lamports, MethodOffset + 4);
×
NEW
226
            data.WriteU64(sourceTransientStakeSeed, MethodOffset + 12);
×
NEW
227
            data.WriteU64(ephemeralStakeSeed, MethodOffset + 20);
×
NEW
228
            data.WriteU64(destinationTransientStakeSeed, MethodOffset + 28);
×
NEW
229
            return data;
×
230
        }
231

232
        /// <summary>
233
        /// Encodes the 'UpdateStakePoolBalance' instruction data.
234
        /// </summary>
235
        internal static byte[] EncodeUpdateStakePoolBalance()
236
        {
237
            byte[] data = new byte[4];
2✔
238
            data.WriteU32((uint)StakePoolProgramInstructions.Values.UpdateStakePoolBalance, MethodOffset);
2✔
239
            return data;
2✔
240
        }
241

242
        /// <summary>
243
        /// Encodes the 'UpdateValidatorListBalance' instruction data.
244
        /// </summary>
245
        /// <param name="startIndex">The starting index in the validator list.</param>
246
        /// <param name="noMerge">If true, merging is disabled.</param>
247
        /// <returns>A byte array containing the encoded instruction data.</returns>
248
        internal static byte[] EncodeUpdateValidatorListBalance(uint startIndex, bool noMerge)
249
        {
250
            // Allocate 9 bytes: 4 for the method discriminator,
251
            // 4 for the start index, and 1 for the no-merge flag.
252
            byte[] data = new byte[9];
2✔
253
            data.WriteU32((uint)StakePoolProgramInstructions.Values.UpdateValidatorListBalance, MethodOffset);
2✔
254
            data.WriteU32(startIndex, MethodOffset + 4);
2✔
255
            data[MethodOffset + 8] = noMerge ? (byte)1 : (byte)0;
2✔
256
            return data;
2✔
257
        }
258

259
        /// <summary>
260
        /// Encodes the 'CleanupRemovedValidatorEntries' instruction data.
261
        /// </summary>
262
        /// <returns>A byte array containing the encoded instruction data.</returns>
263
        internal static byte[] EncodeCleanupRemovedValidatorEntries()
264
        {
265
            byte[] data = new byte[4];
2✔
266
            data.WriteU32((uint)StakePoolProgramInstructions.Values.CleanupRemovedValidatorEntries, MethodOffset);
2✔
267
            return data;
2✔
268
        }
269

270
        /// <summary>
271
        /// Encodes the 'DepositStakeWithSlippage' instruction data.
272
        /// </summary>
273
        /// <param name="minimumPoolTokensOut">The minimum pool tokens expected on deposit (slippage constraint).</param>
274
        /// <returns>A byte array containing the encoded instruction data.</returns>
275
        internal static byte[] EncodeDepositStakeWithSlippage(ulong minimumPoolTokensOut)
276
        {
277
            // Allocate 12 bytes:
278
            // 4 bytes for the instruction discriminator and 8 bytes for the minimum pool tokens out value.
279
            byte[] data = new byte[12];
1✔
280
            data.WriteU32((uint)StakePoolProgramInstructions.Values.DepositStakeWithSlippage, MethodOffset);
1✔
281
            data.WriteU64(minimumPoolTokensOut, MethodOffset + 4);
1✔
282
            return data;
1✔
283
        }
284

285
        /// <summary>
286
        /// Encodes the 'DepositStake' instruction data.
287
        /// </summary>
288
        /// <returns>A byte array containing the encoded instruction data for DepositStake.</returns>
289
        internal static byte[] EncodeDepositStake()
290
        {
291
            // Allocate 4 bytes for the instruction discriminator.
292
            byte[] data = new byte[4];
1✔
293
            data.WriteU32((uint)StakePoolProgramInstructions.Values.DepositStake, MethodOffset);
1✔
294
            return data;
1✔
295
        }
296

297
        /// <summary>
298
        /// Encodes the 'DepositSol' instruction data (without slippage).
299
        /// </summary>
300
        /// <param name="lamportsIn">The amount of SOL lamports being deposited.</param>
301
        /// <returns>A byte array containing the encoded instruction data.</returns>
302
        internal static byte[] EncodeDepositSol(ulong lamportsIn)
303
        {
304
            // Allocate 12 bytes: 4 for the discriminator and 8 for lamportsIn.
NEW
305
            byte[] data = new byte[12];
×
NEW
306
            data.WriteU32((uint)StakePoolProgramInstructions.Values.DepositSol, MethodOffset);
×
NEW
307
            data.WriteU64(lamportsIn, MethodOffset + 4);
×
NEW
308
            return data;
×
309
        }
310

311
        /// <summary>
312
        /// Encodes the 'DepositSolWithSlippage' instruction data.
313
        /// </summary>
314
        /// <param name="lamportsIn">The amount of SOL lamports being deposited.</param>
315
        /// <param name="minimumPoolTokensOut">The minimum pool tokens expected on deposit.</param>
316
        /// <returns>A byte array containing the encoded instruction data.</returns>
317
        internal static byte[] EncodeDepositSolWithSlippage(ulong lamportsIn, ulong minimumPoolTokensOut)
318
        {
319
            // Allocate 20 bytes: 4 bytes for the discriminator, 8 for lamportsIn, 8 for minimumPoolTokensOut.
NEW
320
            byte[] data = new byte[20];
×
NEW
321
            data.WriteU32((uint)StakePoolProgramInstructions.Values.DepositSolWithSlippage, MethodOffset);
×
NEW
322
            data.WriteU64(lamportsIn, MethodOffset + 4);
×
NEW
323
            data.WriteU64(minimumPoolTokensOut, MethodOffset + 12);
×
NEW
324
            return data;
×
325
        }
326

327
        /// <summary>
328
        /// Encodes the 'CreateTokenMetadata' instruction data.
329
        /// </summary>
330
        /// <param name="name">The name of the token.</param>
331
        /// <param name="symbol">The token symbol.</param>
332
        /// <param name="uri">The URI for the token metadata.</param>
333
        /// <returns>A byte array containing the encoded instruction data.</returns>
334
        internal static byte[] EncodeCreateTokenMetadata(string name, string symbol, string uri)
335
        {
336
            // Encode string fields as UTF8 byte arrays.
NEW
337
            byte[] nameBytes = Encoding.UTF8.GetBytes(name);
×
NEW
338
            byte[] symbolBytes = Encoding.UTF8.GetBytes(symbol);
×
NEW
339
            byte[] uriBytes = Encoding.UTF8.GetBytes(uri);
×
340

341
            // Total length:
342
            // 4 bytes for discriminator +
343
            // 4 bytes (name length) + name bytes +
344
            // 4 bytes (symbol length) + symbol bytes +
345
            // 4 bytes (uri length) + uri bytes.
NEW
346
            int totalLength = 4 + (4 + nameBytes.Length) + (4 + symbolBytes.Length) + (4 + uriBytes.Length);
×
NEW
347
            byte[] data = new byte[totalLength];
×
348

NEW
349
            int offset = 0;
×
350
            // Write the discriminator.
NEW
351
            data.WriteU32((uint)StakePoolProgramInstructions.Values.CreateTokenMetadata, offset);
×
NEW
352
            offset += 4;
×
353

354
            // Write the name.
NEW
355
            data.WriteU32((uint)nameBytes.Length, offset);
×
NEW
356
            offset += 4;
×
NEW
357
            nameBytes.CopyTo(data, offset);
×
NEW
358
            offset += nameBytes.Length;
×
359

360
            // Write the symbol.
NEW
361
            data.WriteU32((uint)symbolBytes.Length, offset);
×
NEW
362
            offset += 4;
×
NEW
363
            symbolBytes.CopyTo(data, offset);
×
NEW
364
            offset += symbolBytes.Length;
×
365

366
            // Write the URI.
NEW
367
            data.WriteU32((uint)uriBytes.Length, offset);
×
NEW
368
            offset += 4;
×
NEW
369
            uriBytes.CopyTo(data, offset);
×
370

NEW
371
            return data;
×
372
        }
373

374
        /// <summary>
375
        /// Encodes the 'UpdateTokenMetadata' instruction data.
376
        /// </summary>
377
        /// <param name="name">The new name for the pool token.</param>
378
        /// <param name="symbol">The new symbol for the pool token.</param>
379
        /// <param name="uri">The new URI for the pool token metadata.</param>
380
        /// <returns>A byte array containing the encoded instruction data.</returns>
381
        internal static byte[] EncodeUpdateTokenMetadata(string name, string symbol, string uri)
382
        {
383
            // Convert the string fields to UTF8 byte arrays.
NEW
384
            byte[] nameBytes = Encoding.UTF8.GetBytes(name);
×
NEW
385
            byte[] symbolBytes = Encoding.UTF8.GetBytes(symbol);
×
NEW
386
            byte[] uriBytes = Encoding.UTF8.GetBytes(uri);
×
387

388
            // Compute total length:
389
            // 4 bytes for discriminator +
390
            // 4 bytes (name length) + name bytes +
391
            // 4 bytes (symbol length) + symbol bytes +
392
            // 4 bytes (uri length) + uri bytes.
NEW
393
            int totalLength = 4 + (4 + nameBytes.Length) + (4 + symbolBytes.Length) + (4 + uriBytes.Length);
×
NEW
394
            byte[] data = new byte[totalLength];
×
395

NEW
396
            int offset = 0;
×
397
            // Write the discriminator. Ensure that your StakePoolProgramInstructions enum has a value for UpdateTokenMetadata.
NEW
398
            data.WriteU32((uint)StakePoolProgramInstructions.Values.UpdateTokenMetadata, offset);
×
NEW
399
            offset += 4;
×
400

401
            // Write 'name'.
NEW
402
            data.WriteU32((uint)nameBytes.Length, offset);
×
NEW
403
            offset += 4;
×
NEW
404
            nameBytes.CopyTo(data, offset);
×
NEW
405
            offset += nameBytes.Length;
×
406

407
            // Write 'symbol'.
NEW
408
            data.WriteU32((uint)symbolBytes.Length, offset);
×
NEW
409
            offset += 4;
×
NEW
410
            symbolBytes.CopyTo(data, offset);
×
NEW
411
            offset += symbolBytes.Length;
×
412

413
            // Write 'uri'.
NEW
414
            data.WriteU32((uint)uriBytes.Length, offset);
×
NEW
415
            offset += 4;
×
NEW
416
            uriBytes.CopyTo(data, offset);
×
417

NEW
418
            return data;
×
419
        }
420

421
        /// <summary>
422
        /// Encodes the 'SetFundingAuthority' instruction data.
423
        /// </summary>
424
        /// <param name="fundingType">The funding type to be set.</param>
425
        /// <param name="newAuthority">The new authority public key.</param>
426
        /// <returns>A byte array containing the encoded instruction data.</returns>
427
        internal static byte[] EncodeSetFundingAuthority(FundingType fundingType, PublicKey newAuthority)
428
        {
429
            // Allocate 37 bytes:
430
            // 4 bytes for the discriminator,
431
            // 1 byte for the funding type (as a byte),
432
            // 32 bytes for the new authority public key.
NEW
433
            byte[] data = new byte[37];
×
NEW
434
            data.WriteU32((uint)StakePoolProgramInstructions.Values.SetFundingAuthority, MethodOffset);
×
435

436
            // Write funding type enum value as a byte.
NEW
437
            data[MethodOffset + 4] = (byte)fundingType;
×
438

439
            // Write the new authority public key.
NEW
440
            newAuthority.KeyBytes.CopyTo(data, MethodOffset + 5);
×
441

NEW
442
            return data;
×
443
        }
444

445
        /// <summary>
446
        /// Encodes the 'SetFundingAuthority' instruction data.
447
        /// </summary>
448
        /// <param name="fundingType">The funding type.</param>
449
        /// <returns>A byte array containing the encoded instruction data.</returns>
450
        internal static byte[] EncodeSetFundingAuthority(FundingType fundingType)
451
        {
452
            // Allocate 5 bytes: 4 bytes for the method discriminator and 1 byte for the funding type.
453
            byte[] data = new byte[5];
1✔
454
            data.WriteU32((uint)StakePoolProgramInstructions.Values.SetFundingAuthority, MethodOffset);
1✔
455
            data[MethodOffset + 4] = (byte)fundingType;
1✔
456
            return data;
1✔
457
        }
458

459
        /// <summary>
460
        /// Encodes the 'SetStaker' instruction data.
461
        /// </summary>
462
        /// <returns>A byte array containing the encoded instruction data.</returns>
463
        internal static byte[] EncodeSetStaker()
464
        {
465
            // Allocate 4 bytes for the discriminator only.
466
            byte[] data = new byte[4];
1✔
467
            data.WriteU32((uint)StakePoolProgramInstructions.Values.SetStaker, MethodOffset);
1✔
468
            return data;
1✔
469
        }
470

471
        /// <summary>
472
        /// Encodes the 'SetManager' instruction data.
473
        /// </summary>
474
        /// <returns>A byte array containing the encoded instruction data.</returns>
475
        internal static byte[] EncodeSetManager()
476
        {
477
            // Allocate 4 bytes for the instruction discriminator.
478
            byte[] data = new byte[4];
1✔
479
            data.WriteU32((uint)StakePoolProgramInstructions.Values.SetManager, MethodOffset);
1✔
480
            return data;
1✔
481
        }
482

483
        /// <summary>
484
        /// Encodes the 'WithdrawSolWithSlippage' instruction data.
485
        /// </summary>
486
        /// <param name="poolTokensIn">The amount of pool tokens being withdrawn.</param>
487
        /// <param name="minimumLamportsOut">The minimum lamports expected on withdrawal.</param>
488
        /// <returns>A byte array containing the encoded instruction data.</returns>
489
        internal static byte[] EncodeWithdrawSolWithSlippage(ulong poolTokensIn, ulong minimumLamportsOut)
490
        {
491
            // Allocate 20 bytes:
492
            // 4 bytes for the instruction discriminator,
493
            // 8 bytes for the poolTokensIn amount,
494
            // 8 bytes for the minimum lamports out value.
495
            byte[] data = new byte[20];
2✔
496
            data.WriteU32((uint)StakePoolProgramInstructions.Values.WithdrawSolWithSlippage, MethodOffset);
2✔
497
            data.WriteU64(poolTokensIn, MethodOffset + 4);
2✔
498
            data.WriteU64(minimumLamportsOut, MethodOffset + 12);
2✔
499
            return data;
2✔
500
        }
501

502
        /// <summary>
503
        /// Encodes the 'WithdrawSol' instruction data (without slippage).
504
        /// </summary>
505
        /// <param name="poolTokensIn">The amount of pool tokens to be redeemed.</param>
506
        /// <returns>a byte array containing the encoded instruction data.</returns>
507
        internal static byte[] EncodeWithdrawSol(ulong poolTokensIn)
508
        {
509
            // Allocate 12 bytes: 4 bytes for the instruction discriminator and 8 for poolTokensIn.
510
            byte[] data = new byte[12];
2✔
511
            data.WriteU32((uint)StakePoolProgramInstructions.Values.WithdrawSol, MethodOffset);
2✔
512
            data.WriteU64(poolTokensIn, MethodOffset + 4);
2✔
513
            return data;
2✔
514
        }
515

516
        /// <summary>
517
        /// Encodes the 'WithdrawStake' instruction data (without slippage).
518
        /// </summary>
519
        /// <param name="poolTokensIn">The amount of pool tokens to redeem.</param>
520
        /// <returns>A byte array containing the encoded instruction data.</returns>
521
        internal static byte[] EncodeWithdrawStake(ulong poolTokensIn)
522
        {
523
            // Allocate 12 bytes: 4 bytes for the instruction discriminator and 8 for poolTokensIn.
524
            byte[] data = new byte[12];
1✔
525
            data.WriteU32((uint)StakePoolProgramInstructions.Values.WithdrawStake, MethodOffset);
1✔
526
            data.WriteU64(poolTokensIn, MethodOffset + 4);
1✔
527
            return data;
1✔
528
        }
529

530
        /// <summary>
531
        /// Encodes the 'WithdrawStakeWithSlippage' instruction data.
532
        /// </summary>
533
        /// <param name="poolTokensIn">The amount of pool tokens to redeem.</param>
534
        /// <param name="minimumLamportsOut">The minimum lamports expected on withdrawal.</param>
535
        /// <returns>A byte array containing the encoded instruction data.</returns>
536
        internal static byte[] EncodeWithdrawStakeWithSlippage(ulong poolTokensIn, ulong minimumLamportsOut)
537
        {
538
            // Allocate 20 bytes:
539
            // 4 bytes for the instruction discriminator,
540
            // 8 bytes for poolTokensIn,
541
            // 8 bytes for minimumLamportsOut.
542
            byte[] data = new byte[20];
1✔
543
            data.WriteU32((uint)StakePoolProgramInstructions.Values.WithdrawStakeWithSlippage, MethodOffset);
1✔
544
            data.WriteU64(poolTokensIn, MethodOffset + 4);
1✔
545
            data.WriteU64(minimumLamportsOut, MethodOffset + 12);
1✔
546
            return data;
1✔
547
        }
548

549
        /// <summary>
550
        /// Decodes the 'initialize' instruction data.
551
        /// </summary>
552
        internal static void DecodeInitializeData(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
553
        {
NEW
554
            decodedInstruction.Values.Add("Fee assessed as percentage of perceived rewards", data.GetU64(4));
×
NEW
555
            decodedInstruction.Values.Add("Fee charged per withdrawal as percentage of withdrawal", data.GetU64(12));
×
NEW
556
            decodedInstruction.Values.Add("Fee charged per deposit as percentage of deposit", data.GetU64(20));
×
NEW
557
            decodedInstruction.Values.Add("Percentage [0-100] of deposit_fee that goes to referrer", data.GetU64(28));
×
NEW
558
            decodedInstruction.Values.Add("Maximum expected number of validators", data.GetU32(36));
×
559

NEW
560
        }
×
561

562
        /// <summary>
563
        /// Decodes the 'AddValidatorToPool' instruction data.
564
        /// </summary>
565
        internal static void DecodeAddValidatorToPoolData(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
566
        {
567
            // The seed is at offset 4 (after the 4-byte method discriminator)
NEW
568
            decodedInstruction.Values.Add("Seed", data.GetU32(4));
×
NEW
569
        }
×
570

571
        /// <summary>
572
        /// Decodes the 'DecreaseValidatorStake' instruction data.
573
        /// </summary>
574
        internal static void DecodeDecreaseValidatorStakeData(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
575
        {
NEW
576
            decodedInstruction.Values.Add("Lamports", data.GetU64(4));
×
NEW
577
            decodedInstruction.Values.Add("Transient Stake Seed", data.GetU64(12));
×
NEW
578
        }
×
579

580
        /// <summary>
581
        /// Decodes the 'DecreaseAdditionalValidatorStake' instruction data.
582
        /// </summary>
583
        internal static void DecodeDecreaseAdditionalValidatorStakeData(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
584
        {
NEW
585
            decodedInstruction.Values.Add("Lamports", data.GetU64(4));
×
NEW
586
            decodedInstruction.Values.Add("Transient Stake Seed", data.GetU64(12));
×
NEW
587
            decodedInstruction.Values.Add("Ephemeral Stake Seed", data.GetU64(20));
×
NEW
588
        }
×
589

590
        /// <summary>
591
        /// Decodes the 'DecreaseValidatorStakeWithReserve' instruction data.
592
        /// </summary>
593
        internal static void DecodeDecreaseValidatorStakeWithReserveData(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
594
        {
NEW
595
            decodedInstruction.Values.Add("Lamports", data.GetU64(4));
×
NEW
596
            decodedInstruction.Values.Add("Transient Stake Seed", data.GetU64(12));
×
NEW
597
        }
×
598

599
        /// <summary>
600
        /// Decodes the 'IncreaseValidatorStake' instruction data.
601
        /// </summary>
602
        internal static void DecodeIncreaseValidatorStakeData(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
603
        {
NEW
604
            decodedInstruction.Values.Add("Lamports", data.GetU64(4));
×
NEW
605
            decodedInstruction.Values.Add("Transient Stake Seed", data.GetU64(12));
×
NEW
606
        }
×
607

608
        /// <summary>
609
        /// Decodes the 'IncreaseAdditionalValidatorStake' instruction data.
610
        /// </summary>
611
        internal static void DecodeIncreaseAdditionalValidatorStakeData(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
612
        {
NEW
613
            decodedInstruction.Values.Add("Lamports", data.GetU64(4));
×
NEW
614
            decodedInstruction.Values.Add("Transient Stake Seed", data.GetU64(12));
×
NEW
615
            decodedInstruction.Values.Add("Ephemeral Stake Seed", data.GetU64(20));
×
NEW
616
        }
×
617

618
        /// <summary>
619
        /// Decodes the 'SetPreferredDepositValidator' instruction data.
620
        /// </summary>
621
        internal static void DecodeSetPreferredValidatorData(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
622
        {
NEW
623
            decodedInstruction.Values.Add("Validator Type", (PreferredValidatorType)data.GetU32(4));
×
NEW
624
            if (data.Length >= 8 + PublicKey.PublicKeyLength)
×
625
            {
NEW
626
                var keyBytes = data.Slice(8, PublicKey.PublicKeyLength).ToArray();
×
NEW
627
                decodedInstruction.Values.Add("Validator Vote Address", new PublicKey(keyBytes));
×
628
            }
NEW
629
        }
×
630

631
        /// <summary>
632
        /// Decodes the 'UpdateValidatorListBalance' instruction data.
633
        /// </summary>
634
        /// <param name="decodedInstruction">The <see cref="DecodedInstruction"/> to populate.</param>
635
        /// <param name="data">The instruction data as a read-only span.</param>
636
        /// <param name="keys">The list of account public keys associated with the instruction.</param>
637
        /// <param name="keyIndices">Indices of the keys related to the instruction.</param>
638
        internal static void DecodeUpdateValidatorListBalance(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
639
        {
NEW
640
            uint startIndex = data.GetU32(MethodOffset + 4);
×
NEW
641
            bool noMerge = data[MethodOffset + 8] != 0;
×
NEW
642
            decodedInstruction.Values.Add("Start Index", startIndex);
×
NEW
643
            decodedInstruction.Values.Add("No Merge", noMerge);
×
NEW
644
        }
×
645

646
        /// <summary>
647
        /// Decodes the 'DepositStakeWithSlippage' instruction data.
648
        /// </summary>
649
        /// <param name="decodedInstruction">The <see cref="DecodedInstruction"/> to populate.</param>
650
        /// <param name="data">The instruction data as a read-only span.</param>
651
        /// <param name="keys">The list of account public keys associated with the instruction.</param>
652
        /// <param name="keyIndices">Indices of the keys related to the instruction.</param>
653
        internal static void DecodeDepositStakeWithSlippage(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
654
        {
655
            // The minimum pool tokens out is stored 4 bytes after the discriminator.
NEW
656
            ulong minimumPoolTokensOut = data.GetU64(MethodOffset + 4);
×
NEW
657
            decodedInstruction.Values.Add("Minimum Pool Tokens Out", minimumPoolTokensOut);
×
NEW
658
        }
×
659

660
        /// <summary>
661
        /// Decodes the 'SetFundingAuthority' instruction data.
662
        /// </summary>
663
        /// <param name="decodedInstruction">The <see cref="DecodedInstruction"/> to populate.</param>
664
        /// <param name="data">The instruction data as a read-only span.</param>
665
        /// <param name="keys">The list of account public keys associated with the instruction.</param>
666
        /// <param name="keyIndices">Indices of the keys related to the instruction.</param>
667
        internal static void DecodeSetFundingAuthority(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
668
        {
669
            // The funding type is stored at offset 4 (after the 4-byte discriminator)
NEW
670
            decodedInstruction.Values.Add("Funding Type", (FundingType)data[MethodOffset + 4]);
×
NEW
671
        }
×
672

673
        /// <summary>
674
        /// Decodes the 'SetStaker' instruction data.
675
        /// </summary>
676
        /// <param name="decodedInstruction">The <see cref="DecodedInstruction"/> to populate.</param>
677
        /// <param name="data">The instruction data as a read-only span.</param>
678
        /// <param name="keys">The list of account public keys associated with the instruction.</param>
679
        /// <param name="keyIndices">Indices of the keys related to the instruction.</param>
680
        internal static void DecodeSetStaker(DecodedInstruction decodedInstruction, ReadOnlySpan<byte> data, IList<PublicKey> keys, byte[] keyIndices)
681
        {
682
            // Extract the new staker public key from offset 4.
NEW
683
            var keyBytes = data.Slice(MethodOffset + 4, PublicKey.PublicKeyLength).ToArray();
×
NEW
684
            decodedInstruction.Values.Add("New Staker", new PublicKey(keyBytes));
×
NEW
685
        }
×
686
    }
687
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc