@ignitionfi/fogo-stake-pool 1.0.3 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -459,6 +459,139 @@ const USER_STAKE_SEED_PREFIX = node_buffer.Buffer.from('user_stake');
459
459
  // for merges without a mismatch on credits observed
460
460
  const MINIMUM_ACTIVE_STAKE = 1000000;
461
461
 
462
+ /**
463
+ * BN-based layout for data decoding using buffer-layout.
464
+ * Used for decoding on-chain account data (StakePool, ValidatorList, etc.)
465
+ */
466
+ class BNDataLayout extends bufferLayout.Layout {
467
+ constructor(span, signed, property) {
468
+ super(span, property);
469
+ this.blobLayout = bufferLayout.blob(span);
470
+ this.signed = signed;
471
+ }
472
+ decode(b, offset = 0) {
473
+ const num = new BN(this.blobLayout.decode(b, offset), 10, 'le');
474
+ if (this.signed) {
475
+ return num.fromTwos(this.span * 8).clone();
476
+ }
477
+ return num;
478
+ }
479
+ encode(src, b, offset = 0) {
480
+ if (this.signed) {
481
+ src = src.toTwos(this.span * 8);
482
+ }
483
+ return this.blobLayout.encode(src.toArrayLike(Buffer, 'le', this.span), b, offset);
484
+ }
485
+ }
486
+ /**
487
+ * Creates a u64 layout for data decoding (account layouts).
488
+ * Used in StakePoolLayout, ValidatorListLayout, etc.
489
+ */
490
+ function u64(property) {
491
+ return new BNDataLayout(8, false, property);
492
+ }
493
+ /**
494
+ * BN-based layout for 64-bit unsigned integers using @solana/buffer-layout.
495
+ * Used for encoding instruction data with support for values > MAX_SAFE_INTEGER.
496
+ */
497
+ class BNInstructionLayout extends BufferLayout__namespace.Layout {
498
+ constructor(span, signed, property) {
499
+ super(span, property);
500
+ this.blobLayout = BufferLayout__namespace.blob(span);
501
+ this.signed = signed;
502
+ }
503
+ decode(b, offset = 0) {
504
+ const num = new BN(this.blobLayout.decode(b, offset), 10, 'le');
505
+ if (this.signed) {
506
+ return num.fromTwos(this.span * 8).clone();
507
+ }
508
+ return num;
509
+ }
510
+ encode(src, b, offset = 0) {
511
+ if (this.signed) {
512
+ src = src.toTwos(this.span * 8);
513
+ }
514
+ return this.blobLayout.encode(src.toArrayLike(Buffer, 'le', this.span), b, offset);
515
+ }
516
+ getSpan(_b, _offset) {
517
+ return this.span;
518
+ }
519
+ }
520
+ /**
521
+ * Creates a u64 layout for instruction encoding.
522
+ * Properly handles BN values larger than Number.MAX_SAFE_INTEGER.
523
+ * Compatible with @solana/buffer-layout.struct().
524
+ */
525
+ // eslint-disable-next-line ts/no-explicit-any
526
+ function u64Instruction(property) {
527
+ return new BNInstructionLayout(8, false, property);
528
+ }
529
+ class WrappedLayout extends bufferLayout.Layout {
530
+ constructor(layout, decoder, encoder, property) {
531
+ super(layout.span, property);
532
+ this.layout = layout;
533
+ this.decoder = decoder;
534
+ this.encoder = encoder;
535
+ }
536
+ decode(b, offset) {
537
+ return this.decoder(this.layout.decode(b, offset));
538
+ }
539
+ encode(src, b, offset) {
540
+ return this.layout.encode(this.encoder(src), b, offset);
541
+ }
542
+ getSpan(b, offset) {
543
+ return this.layout.getSpan(b, offset);
544
+ }
545
+ }
546
+ function publicKey(property) {
547
+ return new WrappedLayout(bufferLayout.blob(32), (b) => new web3_js.PublicKey(b), (key) => key.toBuffer(), property);
548
+ }
549
+ class OptionLayout extends bufferLayout.Layout {
550
+ constructor(layout, property) {
551
+ super(-1, property);
552
+ this.layout = layout;
553
+ this.discriminator = bufferLayout.u8();
554
+ }
555
+ encode(src, b, offset = 0) {
556
+ if (src === null || src === undefined) {
557
+ return this.discriminator.encode(0, b, offset);
558
+ }
559
+ this.discriminator.encode(1, b, offset);
560
+ return this.layout.encode(src, b, offset + 1) + 1;
561
+ }
562
+ decode(b, offset = 0) {
563
+ const discriminator = this.discriminator.decode(b, offset);
564
+ if (discriminator === 0) {
565
+ return null;
566
+ }
567
+ else if (discriminator === 1) {
568
+ return this.layout.decode(b, offset + 1);
569
+ }
570
+ throw new Error(`Invalid option ${this.property}`);
571
+ }
572
+ getSpan(b, offset = 0) {
573
+ const discriminator = this.discriminator.decode(b, offset);
574
+ if (discriminator === 0) {
575
+ return 1;
576
+ }
577
+ else if (discriminator === 1) {
578
+ return this.layout.getSpan(b, offset + 1) + 1;
579
+ }
580
+ throw new Error(`Invalid option ${this.property}`);
581
+ }
582
+ }
583
+ function option(layout, property) {
584
+ return new OptionLayout(layout, property);
585
+ }
586
+ function vec(elementLayout, property) {
587
+ const length = bufferLayout.u32('length');
588
+ const layout = bufferLayout.struct([
589
+ length,
590
+ bufferLayout.seq(elementLayout, bufferLayout.offset(length, -length.span), 'values'),
591
+ ]);
592
+ return new WrappedLayout(layout, ({ values }) => values, values => ({ values }), property);
593
+ }
594
+
462
595
  /**
463
596
  * Populate a buffer of instruction data using an InstructionType
464
597
  * @internal
@@ -577,95 +710,6 @@ function findUserStakeProgramAddress(programId, userWallet, seed) {
577
710
  return publicKey;
578
711
  }
579
712
 
580
- class BNLayout extends bufferLayout.Layout {
581
- constructor(span, signed, property) {
582
- super(span, property);
583
- this.blob = bufferLayout.blob(span);
584
- this.signed = signed;
585
- }
586
- decode(b, offset = 0) {
587
- const num = new BN(this.blob.decode(b, offset), 10, 'le');
588
- if (this.signed) {
589
- return num.fromTwos(this.span * 8).clone();
590
- }
591
- return num;
592
- }
593
- encode(src, b, offset = 0) {
594
- if (this.signed) {
595
- src = src.toTwos(this.span * 8);
596
- }
597
- return this.blob.encode(src.toArrayLike(Buffer, 'le', this.span), b, offset);
598
- }
599
- }
600
- function u64(property) {
601
- return new BNLayout(8, false, property);
602
- }
603
- class WrappedLayout extends bufferLayout.Layout {
604
- constructor(layout, decoder, encoder, property) {
605
- super(layout.span, property);
606
- this.layout = layout;
607
- this.decoder = decoder;
608
- this.encoder = encoder;
609
- }
610
- decode(b, offset) {
611
- return this.decoder(this.layout.decode(b, offset));
612
- }
613
- encode(src, b, offset) {
614
- return this.layout.encode(this.encoder(src), b, offset);
615
- }
616
- getSpan(b, offset) {
617
- return this.layout.getSpan(b, offset);
618
- }
619
- }
620
- function publicKey(property) {
621
- return new WrappedLayout(bufferLayout.blob(32), (b) => new web3_js.PublicKey(b), (key) => key.toBuffer(), property);
622
- }
623
- class OptionLayout extends bufferLayout.Layout {
624
- constructor(layout, property) {
625
- super(-1, property);
626
- this.layout = layout;
627
- this.discriminator = bufferLayout.u8();
628
- }
629
- encode(src, b, offset = 0) {
630
- if (src === null || src === undefined) {
631
- return this.discriminator.encode(0, b, offset);
632
- }
633
- this.discriminator.encode(1, b, offset);
634
- return this.layout.encode(src, b, offset + 1) + 1;
635
- }
636
- decode(b, offset = 0) {
637
- const discriminator = this.discriminator.decode(b, offset);
638
- if (discriminator === 0) {
639
- return null;
640
- }
641
- else if (discriminator === 1) {
642
- return this.layout.decode(b, offset + 1);
643
- }
644
- throw new Error(`Invalid option ${this.property}`);
645
- }
646
- getSpan(b, offset = 0) {
647
- const discriminator = this.discriminator.decode(b, offset);
648
- if (discriminator === 0) {
649
- return 1;
650
- }
651
- else if (discriminator === 1) {
652
- return this.layout.getSpan(b, offset + 1) + 1;
653
- }
654
- throw new Error(`Invalid option ${this.property}`);
655
- }
656
- }
657
- function option(layout, property) {
658
- return new OptionLayout(layout, property);
659
- }
660
- function vec(elementLayout, property) {
661
- const length = bufferLayout.u32('length');
662
- const layout = bufferLayout.struct([
663
- length,
664
- bufferLayout.seq(elementLayout, bufferLayout.offset(length, -length.span), 'values'),
665
- ]);
666
- return new WrappedLayout(layout, ({ values }) => values, values => ({ values }), property);
667
- }
668
-
669
713
  const feeFields = [u64('denominator'), u64('numerator')];
670
714
  var AccountType;
671
715
  (function (AccountType) {
@@ -1008,11 +1052,50 @@ function arrayChunk(array, size) {
1008
1052
  return result;
1009
1053
  }
1010
1054
 
1055
+ /**
1056
+ * Converts various numeric types to BN for safe large number handling.
1057
+ * @internal
1058
+ */
1059
+ function toBN(value) {
1060
+ if (BN.isBN(value)) {
1061
+ return value;
1062
+ }
1063
+ if (typeof value === 'bigint') {
1064
+ return new BN(value.toString());
1065
+ }
1066
+ if (typeof value === 'string') {
1067
+ // Validate string is a valid non-negative integer
1068
+ const trimmed = value.trim();
1069
+ if (!/^\d+$/.test(trimmed)) {
1070
+ throw new Error(`Invalid amount string: "${value}". Must be a non-negative integer.`);
1071
+ }
1072
+ return new BN(trimmed);
1073
+ }
1074
+ if (typeof value === 'number') {
1075
+ if (!Number.isFinite(value)) {
1076
+ throw new Error('Invalid amount: must be a finite number');
1077
+ }
1078
+ if (value < 0) {
1079
+ throw new Error('Invalid amount: must be non-negative');
1080
+ }
1081
+ if (!Number.isInteger(value)) {
1082
+ throw new Error('Invalid amount: must be an integer (lamports)');
1083
+ }
1084
+ // CRITICAL: Numbers > MAX_SAFE_INTEGER have already lost precision
1085
+ // We throw an error instead of silently corrupting data
1086
+ if (value > Number.MAX_SAFE_INTEGER) {
1087
+ throw new Error(`Amount ${value} exceeds Number.MAX_SAFE_INTEGER (9,007,199,254,740,991). ` +
1088
+ `Use BigInt or BN for large values to avoid precision loss.`);
1089
+ }
1090
+ return new BN(value);
1091
+ }
1092
+ throw new Error(`Invalid amount type: ${typeof value}`);
1093
+ }
1011
1094
  // 'UpdateTokenMetadata' and 'CreateTokenMetadata' have dynamic layouts
1012
1095
  const MOVE_STAKE_LAYOUT = BufferLayout__namespace.struct([
1013
1096
  BufferLayout__namespace.u8('instruction'),
1014
- BufferLayout__namespace.ns64('lamports'),
1015
- BufferLayout__namespace.ns64('transientStakeSeed'),
1097
+ u64Instruction('lamports'),
1098
+ u64Instruction('transientStakeSeed'),
1016
1099
  ]);
1017
1100
  const UPDATE_VALIDATOR_LIST_BALANCE_LAYOUT = BufferLayout__namespace.struct([
1018
1101
  BufferLayout__namespace.u8('instruction'),
@@ -1087,7 +1170,7 @@ const STAKE_POOL_INSTRUCTION_LAYOUTS = Object.freeze({
1087
1170
  index: 10,
1088
1171
  layout: BufferLayout__namespace.struct([
1089
1172
  BufferLayout__namespace.u8('instruction'),
1090
- BufferLayout__namespace.ns64('poolTokens'),
1173
+ u64Instruction('poolTokens'),
1091
1174
  ]),
1092
1175
  },
1093
1176
  /// Deposit SOL directly into the pool's reserve account. The output is a "pool" token
@@ -1096,7 +1179,7 @@ const STAKE_POOL_INSTRUCTION_LAYOUTS = Object.freeze({
1096
1179
  index: 14,
1097
1180
  layout: BufferLayout__namespace.struct([
1098
1181
  BufferLayout__namespace.u8('instruction'),
1099
- BufferLayout__namespace.ns64('lamports'),
1182
+ u64Instruction('lamports'),
1100
1183
  ]),
1101
1184
  },
1102
1185
  /// Withdraw SOL directly from the pool's reserve account. Fails if the
@@ -1105,25 +1188,25 @@ const STAKE_POOL_INSTRUCTION_LAYOUTS = Object.freeze({
1105
1188
  index: 16,
1106
1189
  layout: BufferLayout__namespace.struct([
1107
1190
  BufferLayout__namespace.u8('instruction'),
1108
- BufferLayout__namespace.ns64('poolTokens'),
1191
+ u64Instruction('poolTokens'),
1109
1192
  ]),
1110
1193
  },
1111
1194
  IncreaseAdditionalValidatorStake: {
1112
1195
  index: 19,
1113
1196
  layout: BufferLayout__namespace.struct([
1114
1197
  BufferLayout__namespace.u8('instruction'),
1115
- BufferLayout__namespace.ns64('lamports'),
1116
- BufferLayout__namespace.ns64('transientStakeSeed'),
1117
- BufferLayout__namespace.ns64('ephemeralStakeSeed'),
1198
+ u64Instruction('lamports'),
1199
+ u64Instruction('transientStakeSeed'),
1200
+ u64Instruction('ephemeralStakeSeed'),
1118
1201
  ]),
1119
1202
  },
1120
1203
  DecreaseAdditionalValidatorStake: {
1121
1204
  index: 20,
1122
1205
  layout: BufferLayout__namespace.struct([
1123
1206
  BufferLayout__namespace.u8('instruction'),
1124
- BufferLayout__namespace.ns64('lamports'),
1125
- BufferLayout__namespace.ns64('transientStakeSeed'),
1126
- BufferLayout__namespace.ns64('ephemeralStakeSeed'),
1207
+ u64Instruction('lamports'),
1208
+ u64Instruction('transientStakeSeed'),
1209
+ u64Instruction('ephemeralStakeSeed'),
1127
1210
  ]),
1128
1211
  },
1129
1212
  DecreaseValidatorStakeWithReserve: {
@@ -1138,62 +1221,62 @@ const STAKE_POOL_INSTRUCTION_LAYOUTS = Object.freeze({
1138
1221
  index: 23,
1139
1222
  layout: BufferLayout__namespace.struct([
1140
1223
  BufferLayout__namespace.u8('instruction'),
1141
- BufferLayout__namespace.ns64('lamports'),
1224
+ u64Instruction('lamports'),
1142
1225
  ]),
1143
1226
  },
1144
1227
  WithdrawStakeWithSlippage: {
1145
1228
  index: 24,
1146
1229
  layout: BufferLayout__namespace.struct([
1147
1230
  BufferLayout__namespace.u8('instruction'),
1148
- BufferLayout__namespace.ns64('poolTokensIn'),
1149
- BufferLayout__namespace.ns64('minimumLamportsOut'),
1231
+ u64Instruction('poolTokensIn'),
1232
+ u64Instruction('minimumLamportsOut'),
1150
1233
  ]),
1151
1234
  },
1152
1235
  DepositSolWithSlippage: {
1153
1236
  index: 25,
1154
1237
  layout: BufferLayout__namespace.struct([
1155
1238
  BufferLayout__namespace.u8('instruction'),
1156
- BufferLayout__namespace.ns64('lamports'),
1239
+ u64Instruction('lamports'),
1157
1240
  ]),
1158
1241
  },
1159
1242
  WithdrawSolWithSlippage: {
1160
1243
  index: 26,
1161
1244
  layout: BufferLayout__namespace.struct([
1162
1245
  BufferLayout__namespace.u8('instruction'),
1163
- BufferLayout__namespace.ns64('lamports'),
1246
+ u64Instruction('lamports'),
1164
1247
  ]),
1165
1248
  },
1166
1249
  DepositWsolWithSession: {
1167
1250
  index: 27,
1168
1251
  layout: BufferLayout__namespace.struct([
1169
1252
  BufferLayout__namespace.u8('instruction'),
1170
- BufferLayout__namespace.ns64('lamportsIn'),
1171
- BufferLayout__namespace.ns64('minimumPoolTokensOut'),
1253
+ u64Instruction('lamportsIn'),
1254
+ u64Instruction('minimumPoolTokensOut'),
1172
1255
  ]),
1173
1256
  },
1174
1257
  WithdrawWsolWithSession: {
1175
1258
  index: 28,
1176
1259
  layout: BufferLayout__namespace.struct([
1177
1260
  BufferLayout__namespace.u8('instruction'),
1178
- BufferLayout__namespace.ns64('poolTokensIn'),
1179
- BufferLayout__namespace.ns64('minimumLamportsOut'),
1261
+ u64Instruction('poolTokensIn'),
1262
+ u64Instruction('minimumLamportsOut'),
1180
1263
  ]),
1181
1264
  },
1182
1265
  WithdrawStakeWithSession: {
1183
1266
  index: 29,
1184
1267
  layout: BufferLayout__namespace.struct([
1185
1268
  BufferLayout__namespace.u8('instruction'),
1186
- BufferLayout__namespace.ns64('poolTokensIn'),
1187
- BufferLayout__namespace.ns64('minimumLamportsOut'),
1188
- BufferLayout__namespace.ns64('userStakeSeed'),
1269
+ u64Instruction('poolTokensIn'),
1270
+ u64Instruction('minimumLamportsOut'),
1271
+ u64Instruction('userStakeSeed'),
1189
1272
  ]),
1190
1273
  },
1191
1274
  WithdrawFromStakeAccountWithSession: {
1192
1275
  index: 30,
1193
1276
  layout: BufferLayout__namespace.struct([
1194
1277
  BufferLayout__namespace.u8('instruction'),
1195
- BufferLayout__namespace.ns64('lamports'),
1196
- BufferLayout__namespace.ns64('userStakeSeed'),
1278
+ u64Instruction('lamports'),
1279
+ u64Instruction('userStakeSeed'),
1197
1280
  ]),
1198
1281
  },
1199
1282
  });
@@ -1325,7 +1408,10 @@ class StakePoolInstruction {
1325
1408
  static increaseValidatorStake(params) {
1326
1409
  const { programId, stakePool, staker, withdrawAuthority, validatorList, reserveStake, transientStake, validatorStake, validatorVote, lamports, transientStakeSeed, } = params;
1327
1410
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.IncreaseValidatorStake;
1328
- const data = encodeData(type, { lamports, transientStakeSeed });
1411
+ const data = encodeData(type, {
1412
+ lamports: toBN(lamports),
1413
+ transientStakeSeed: toBN(transientStakeSeed),
1414
+ });
1329
1415
  const keys = [
1330
1416
  { pubkey: stakePool, isSigner: false, isWritable: false },
1331
1417
  { pubkey: staker, isSigner: true, isWritable: false },
@@ -1355,7 +1441,11 @@ class StakePoolInstruction {
1355
1441
  static increaseAdditionalValidatorStake(params) {
1356
1442
  const { programId, stakePool, staker, withdrawAuthority, validatorList, reserveStake, transientStake, validatorStake, validatorVote, lamports, transientStakeSeed, ephemeralStake, ephemeralStakeSeed, } = params;
1357
1443
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.IncreaseAdditionalValidatorStake;
1358
- const data = encodeData(type, { lamports, transientStakeSeed, ephemeralStakeSeed });
1444
+ const data = encodeData(type, {
1445
+ lamports: toBN(lamports),
1446
+ transientStakeSeed: toBN(transientStakeSeed),
1447
+ ephemeralStakeSeed: toBN(ephemeralStakeSeed),
1448
+ });
1359
1449
  const keys = [
1360
1450
  { pubkey: stakePool, isSigner: false, isWritable: false },
1361
1451
  { pubkey: staker, isSigner: true, isWritable: false },
@@ -1385,7 +1475,10 @@ class StakePoolInstruction {
1385
1475
  static decreaseValidatorStake(params) {
1386
1476
  const { programId, stakePool, staker, withdrawAuthority, validatorList, validatorStake, transientStake, lamports, transientStakeSeed, } = params;
1387
1477
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.DecreaseValidatorStake;
1388
- const data = encodeData(type, { lamports, transientStakeSeed });
1478
+ const data = encodeData(type, {
1479
+ lamports: toBN(lamports),
1480
+ transientStakeSeed: toBN(transientStakeSeed),
1481
+ });
1389
1482
  const keys = [
1390
1483
  { pubkey: stakePool, isSigner: false, isWritable: false },
1391
1484
  { pubkey: staker, isSigner: true, isWritable: false },
@@ -1411,7 +1504,10 @@ class StakePoolInstruction {
1411
1504
  static decreaseValidatorStakeWithReserve(params) {
1412
1505
  const { programId, stakePool, staker, withdrawAuthority, validatorList, reserveStake, validatorStake, transientStake, lamports, transientStakeSeed, } = params;
1413
1506
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.DecreaseValidatorStakeWithReserve;
1414
- const data = encodeData(type, { lamports, transientStakeSeed });
1507
+ const data = encodeData(type, {
1508
+ lamports: toBN(lamports),
1509
+ transientStakeSeed: toBN(transientStakeSeed),
1510
+ });
1415
1511
  const keys = [
1416
1512
  { pubkey: stakePool, isSigner: false, isWritable: false },
1417
1513
  { pubkey: staker, isSigner: true, isWritable: false },
@@ -1438,7 +1534,11 @@ class StakePoolInstruction {
1438
1534
  static decreaseAdditionalValidatorStake(params) {
1439
1535
  const { programId, stakePool, staker, withdrawAuthority, validatorList, reserveStake, validatorStake, transientStake, lamports, transientStakeSeed, ephemeralStakeSeed, ephemeralStake, } = params;
1440
1536
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.DecreaseAdditionalValidatorStake;
1441
- const data = encodeData(type, { lamports, transientStakeSeed, ephemeralStakeSeed });
1537
+ const data = encodeData(type, {
1538
+ lamports: toBN(lamports),
1539
+ transientStakeSeed: toBN(transientStakeSeed),
1540
+ ephemeralStakeSeed: toBN(ephemeralStakeSeed),
1541
+ });
1442
1542
  const keys = [
1443
1543
  { pubkey: stakePool, isSigner: false, isWritable: false },
1444
1544
  { pubkey: staker, isSigner: true, isWritable: false },
@@ -1495,7 +1595,7 @@ class StakePoolInstruction {
1495
1595
  static depositSol(params) {
1496
1596
  const { programId, stakePool, withdrawAuthority, depositAuthority, reserveStake, fundingAccount, destinationPoolAccount, managerFeeAccount, referralPoolAccount, poolMint, lamports, } = params;
1497
1597
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.DepositSol;
1498
- const data = encodeData(type, { lamports });
1598
+ const data = encodeData(type, { lamports: toBN(lamports) });
1499
1599
  const keys = [
1500
1600
  { pubkey: stakePool, isSigner: false, isWritable: true },
1501
1601
  { pubkey: withdrawAuthority, isSigner: false, isWritable: false },
@@ -1528,8 +1628,8 @@ class StakePoolInstruction {
1528
1628
  var _a;
1529
1629
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.DepositWsolWithSession;
1530
1630
  const data = encodeData(type, {
1531
- lamportsIn: params.lamportsIn,
1532
- minimumPoolTokensOut: params.minimumPoolTokensOut,
1631
+ lamportsIn: toBN(params.lamportsIn),
1632
+ minimumPoolTokensOut: toBN(params.minimumPoolTokensOut),
1533
1633
  });
1534
1634
  const keys = [
1535
1635
  { pubkey: params.stakePool, isSigner: false, isWritable: true },
@@ -1571,7 +1671,7 @@ class StakePoolInstruction {
1571
1671
  static withdrawStake(params) {
1572
1672
  const { programId, stakePool, validatorList, withdrawAuthority, validatorStake, destinationStake, destinationStakeAuthority, sourceTransferAuthority, sourcePoolAccount, managerFeeAccount, poolMint, poolTokens, } = params;
1573
1673
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.WithdrawStake;
1574
- const data = encodeData(type, { poolTokens });
1674
+ const data = encodeData(type, { poolTokens: toBN(poolTokens) });
1575
1675
  const keys = [
1576
1676
  { pubkey: stakePool, isSigner: false, isWritable: true },
1577
1677
  { pubkey: validatorList, isSigner: false, isWritable: true },
@@ -1599,7 +1699,7 @@ class StakePoolInstruction {
1599
1699
  static withdrawSol(params) {
1600
1700
  const { programId, stakePool, withdrawAuthority, sourceTransferAuthority, sourcePoolAccount, reserveStake, destinationSystemAccount, managerFeeAccount, solWithdrawAuthority, poolMint, poolTokens, } = params;
1601
1701
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.WithdrawSol;
1602
- const data = encodeData(type, { poolTokens });
1702
+ const data = encodeData(type, { poolTokens: toBN(poolTokens) });
1603
1703
  const keys = [
1604
1704
  { pubkey: stakePool, isSigner: false, isWritable: true },
1605
1705
  { pubkey: withdrawAuthority, isSigner: false, isWritable: false },
@@ -1634,8 +1734,8 @@ class StakePoolInstruction {
1634
1734
  static withdrawWsolWithSession(params) {
1635
1735
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.WithdrawWsolWithSession;
1636
1736
  const data = encodeData(type, {
1637
- poolTokensIn: params.poolTokensIn,
1638
- minimumLamportsOut: params.minimumLamportsOut,
1737
+ poolTokensIn: toBN(params.poolTokensIn),
1738
+ minimumLamportsOut: toBN(params.minimumLamportsOut),
1639
1739
  });
1640
1740
  const keys = [
1641
1741
  { pubkey: params.stakePool, isSigner: false, isWritable: true },
@@ -1677,9 +1777,9 @@ class StakePoolInstruction {
1677
1777
  static withdrawStakeWithSession(params) {
1678
1778
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.WithdrawStakeWithSession;
1679
1779
  const data = encodeData(type, {
1680
- poolTokensIn: params.poolTokensIn,
1681
- minimumLamportsOut: params.minimumLamportsOut,
1682
- userStakeSeed: params.userStakeSeed,
1780
+ poolTokensIn: toBN(params.poolTokensIn),
1781
+ minimumLamportsOut: toBN(params.minimumLamportsOut),
1782
+ userStakeSeed: toBN(params.userStakeSeed),
1683
1783
  });
1684
1784
  const keys = [
1685
1785
  { pubkey: params.stakePool, isSigner: false, isWritable: true },
@@ -1714,8 +1814,8 @@ class StakePoolInstruction {
1714
1814
  static withdrawFromStakeAccountWithSession(params) {
1715
1815
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.WithdrawFromStakeAccountWithSession;
1716
1816
  const data = encodeData(type, {
1717
- lamports: params.lamports,
1718
- userStakeSeed: params.userStakeSeed,
1817
+ lamports: toBN(params.lamports),
1818
+ userStakeSeed: toBN(params.userStakeSeed),
1719
1819
  });
1720
1820
  const keys = [
1721
1821
  { pubkey: params.userStakeAccount, isSigner: false, isWritable: true },
@@ -2004,10 +2104,18 @@ skipBalanceCheck = false) {
2004
2104
  if (!skipBalanceCheck) {
2005
2105
  const tokenAccountInfo = await connection.getTokenAccountBalance(wsolTokenAccount, 'confirmed');
2006
2106
  const wsolBalance = tokenAccountInfo
2007
- ? parseInt(tokenAccountInfo.value.amount)
2008
- : 0;
2009
- if (wsolBalance < lamports) {
2010
- throw new Error(`Not enough WSOL to deposit into pool. Maximum deposit amount is ${lamportsToSol(wsolBalance)} WSOL.`);
2107
+ ? BigInt(tokenAccountInfo.value.amount)
2108
+ : BigInt(0);
2109
+ // Convert lamports to BigInt for comparison
2110
+ const lamportsBigInt = typeof lamports === 'bigint'
2111
+ ? lamports
2112
+ : typeof lamports === 'string'
2113
+ ? BigInt(lamports)
2114
+ : BN.isBN(lamports)
2115
+ ? BigInt(lamports.toString())
2116
+ : BigInt(lamports);
2117
+ if (wsolBalance < lamportsBigInt) {
2118
+ throw new Error(`Not enough WSOL to deposit into pool. Maximum deposit amount is ${lamportsToSol(Number(wsolBalance))} WSOL.`);
2011
2119
  }
2012
2120
  }
2013
2121
  const stakePoolAccount = await getStakePoolAccount(connection, stakePoolAddress);
@@ -2053,7 +2161,15 @@ skipBalanceCheck = false) {
2053
2161
  */
2054
2162
  async function depositSol(connection, stakePoolAddress, from, lamports, destinationTokenAccount, referrerTokenAccount, depositAuthority) {
2055
2163
  const fromBalance = await connection.getBalance(from, 'confirmed');
2056
- if (fromBalance < lamports) {
2164
+ // Convert lamports to BigInt for comparison
2165
+ const lamportsBigInt = typeof lamports === 'bigint'
2166
+ ? lamports
2167
+ : typeof lamports === 'string'
2168
+ ? BigInt(lamports)
2169
+ : BN.isBN(lamports)
2170
+ ? BigInt(lamports.toString())
2171
+ : BigInt(lamports);
2172
+ if (BigInt(fromBalance) < lamportsBigInt) {
2057
2173
  throw new Error(`Not enough SOL to deposit into pool. Maximum deposit amount is ${lamportsToSol(fromBalance)} SOL.`);
2058
2174
  }
2059
2175
  const stakePoolAccount = await getStakePoolAccount(connection, stakePoolAddress);
@@ -2067,7 +2183,7 @@ async function depositSol(connection, stakePoolAddress, from, lamports, destinat
2067
2183
  instructions.push(web3_js.SystemProgram.transfer({
2068
2184
  fromPubkey: from,
2069
2185
  toPubkey: userSolTransfer.publicKey,
2070
- lamports,
2186
+ lamports: lamportsBigInt,
2071
2187
  }));
2072
2188
  // Create token account if not specified
2073
2189
  if (!destinationTokenAccount) {