@jpool/bond-sdk 0.9.0-next.7 → 0.9.0-next.9

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.d.mts CHANGED
@@ -1227,17 +1227,6 @@ type Jbond = {
1227
1227
  ];
1228
1228
  "type": "u64";
1229
1229
  },
1230
- {
1231
- "name": "status";
1232
- "docs": [
1233
- "Current status of the bond"
1234
- ];
1235
- "type": {
1236
- "defined": {
1237
- "name": "bondStatus";
1238
- };
1239
- };
1240
- },
1241
1230
  {
1242
1231
  "name": "sessionStartTs";
1243
1232
  "docs": [
@@ -1262,23 +1251,6 @@ type Jbond = {
1262
1251
  ];
1263
1252
  };
1264
1253
  },
1265
- {
1266
- "name": "bondStatus";
1267
- "type": {
1268
- "kind": "enum";
1269
- "variants": [
1270
- {
1271
- "name": "active";
1272
- },
1273
- {
1274
- "name": "inactive";
1275
- },
1276
- {
1277
- "name": "pending";
1278
- }
1279
- ];
1280
- };
1281
- },
1282
1254
  {
1283
1255
  "name": "bondType";
1284
1256
  "docs": [
@@ -1534,7 +1506,7 @@ type BondType = IdlTypes<Jbond>['bondType'];
1534
1506
  type CollateralType = IdlTypes<Jbond>['collateralType'];
1535
1507
  type BondStateStats = {
1536
1508
  totalCollected: number;
1537
- totalMembers: number;
1509
+ status: SessionStatus;
1538
1510
  };
1539
1511
  declare const bondTypeToSeed: {
1540
1512
  performance: Seeds;
@@ -1823,7 +1795,12 @@ declare class JBondClient {
1823
1795
  /**
1824
1796
  * Get all bond states with total collected collateral
1825
1797
  */
1826
- getAllBondStates(_bondType: BondType): Promise<Array<[BondState, number]>>;
1798
+ getAllBondStates(bondType: BondType): Promise<Array<[BondState, BondStateStats]>>;
1799
+ /**
1800
+ * Get session status for bond state
1801
+ */
1802
+ private getBondStateSessionStatus;
1803
+ /** Get all validator bonds for a given bond state */
1827
1804
  private getValidatorBondsByState;
1828
1805
  /**
1829
1806
  * Fetch validator bond data or null if not found
package/dist/index.d.ts CHANGED
@@ -1227,17 +1227,6 @@ type Jbond = {
1227
1227
  ];
1228
1228
  "type": "u64";
1229
1229
  },
1230
- {
1231
- "name": "status";
1232
- "docs": [
1233
- "Current status of the bond"
1234
- ];
1235
- "type": {
1236
- "defined": {
1237
- "name": "bondStatus";
1238
- };
1239
- };
1240
- },
1241
1230
  {
1242
1231
  "name": "sessionStartTs";
1243
1232
  "docs": [
@@ -1262,23 +1251,6 @@ type Jbond = {
1262
1251
  ];
1263
1252
  };
1264
1253
  },
1265
- {
1266
- "name": "bondStatus";
1267
- "type": {
1268
- "kind": "enum";
1269
- "variants": [
1270
- {
1271
- "name": "active";
1272
- },
1273
- {
1274
- "name": "inactive";
1275
- },
1276
- {
1277
- "name": "pending";
1278
- }
1279
- ];
1280
- };
1281
- },
1282
1254
  {
1283
1255
  "name": "bondType";
1284
1256
  "docs": [
@@ -1534,7 +1506,7 @@ type BondType = IdlTypes<Jbond>['bondType'];
1534
1506
  type CollateralType = IdlTypes<Jbond>['collateralType'];
1535
1507
  type BondStateStats = {
1536
1508
  totalCollected: number;
1537
- totalMembers: number;
1509
+ status: SessionStatus;
1538
1510
  };
1539
1511
  declare const bondTypeToSeed: {
1540
1512
  performance: Seeds;
@@ -1823,7 +1795,12 @@ declare class JBondClient {
1823
1795
  /**
1824
1796
  * Get all bond states with total collected collateral
1825
1797
  */
1826
- getAllBondStates(_bondType: BondType): Promise<Array<[BondState, number]>>;
1798
+ getAllBondStates(bondType: BondType): Promise<Array<[BondState, BondStateStats]>>;
1799
+ /**
1800
+ * Get session status for bond state
1801
+ */
1802
+ private getBondStateSessionStatus;
1803
+ /** Get all validator bonds for a given bond state */
1827
1804
  private getValidatorBondsByState;
1828
1805
  /**
1829
1806
  * Fetch validator bond data or null if not found
package/dist/index.js CHANGED
@@ -4615,6 +4615,40 @@ var SessionStatus = /* @__PURE__ */ ((SessionStatus2) => {
4615
4615
  // src/utils/index.ts
4616
4616
  init_cjs_shims();
4617
4617
 
4618
+ // src/utils/enum-utils.ts
4619
+ init_cjs_shims();
4620
+ function getVariant(v) {
4621
+ const key = Object.keys(v)[0];
4622
+ return [key, v[key]];
4623
+ }
4624
+ function variantArgs(payload) {
4625
+ if (Array.isArray(payload)) {
4626
+ return payload;
4627
+ }
4628
+ if (payload && typeof payload === "object") {
4629
+ const numericKeys = Object.keys(payload).filter((k) => String(+k) === k);
4630
+ if (numericKeys.length) {
4631
+ return numericKeys.sort((a, b) => +a - +b).map((k) => payload[k]);
4632
+ }
4633
+ return [payload];
4634
+ }
4635
+ return [payload];
4636
+ }
4637
+ function matchVariant(v, handlers) {
4638
+ const [k, data] = getVariant(v);
4639
+ const h = handlers[k];
4640
+ if (!h) {
4641
+ throw new Error(`Unhandled variant: ${k}`);
4642
+ }
4643
+ return h(...variantArgs(data));
4644
+ }
4645
+ function variantKey(v) {
4646
+ return Object.keys(v)[0];
4647
+ }
4648
+ function sameVariant(a, b) {
4649
+ return variantKey(a) === variantKey(b);
4650
+ }
4651
+
4618
4652
  // src/utils/wallet.ts
4619
4653
  init_cjs_shims();
4620
4654
  function isVersionedTransaction(tx) {
@@ -6000,17 +6034,6 @@ var jbond_default = {
6000
6034
  ],
6001
6035
  type: "u64"
6002
6036
  },
6003
- {
6004
- name: "status",
6005
- docs: [
6006
- "Current status of the bond"
6007
- ],
6008
- type: {
6009
- defined: {
6010
- name: "BondStatus"
6011
- }
6012
- }
6013
- },
6014
6037
  {
6015
6038
  name: "session_start_ts",
6016
6039
  docs: [
@@ -6035,23 +6058,6 @@ var jbond_default = {
6035
6058
  ]
6036
6059
  }
6037
6060
  },
6038
- {
6039
- name: "BondStatus",
6040
- type: {
6041
- kind: "enum",
6042
- variants: [
6043
- {
6044
- name: "Active"
6045
- },
6046
- {
6047
- name: "Inactive"
6048
- },
6049
- {
6050
- name: "Pending"
6051
- }
6052
- ]
6053
- }
6054
- },
6055
6061
  {
6056
6062
  name: "BondType",
6057
6063
  docs: [
@@ -6413,13 +6419,8 @@ var JBondClient = class _JBondClient {
6413
6419
  return this;
6414
6420
  }
6415
6421
  getBondTypeSeed(bondType) {
6416
- if ("performance" in bondType) {
6417
- return "performance" /* PerformanceBond */;
6418
- }
6419
- if ("crowdfunding" in bondType) {
6420
- return "crowdfunding" /* CrowdFundingBond */;
6421
- }
6422
- throw new Error(`Unknown bond type: ${JSON.stringify(bondType)}`);
6422
+ const [k] = getVariant(bondType);
6423
+ return bondTypeToSeed[k];
6423
6424
  }
6424
6425
  /**
6425
6426
  * Program Derived Addresses (PDAs)
@@ -6574,13 +6575,16 @@ var JBondClient = class _JBondClient {
6574
6575
  reserveTokenAccount: null,
6575
6576
  mint: null
6576
6577
  };
6577
- if ("token" in props.collateralType) {
6578
- accounts.reserveTokenAccount = splToken.getAssociatedTokenAddressSync(
6579
- props.collateralType.token[0],
6580
- props.reserve
6581
- );
6582
- accounts.mint = props.collateralType.token[0];
6583
- }
6578
+ matchVariant(props.collateralType, {
6579
+ native: () => {
6580
+ },
6581
+ token: (mint) => {
6582
+ accounts.reserveTokenAccount = splToken.getAssociatedTokenAddressSync(mint, props.reserve);
6583
+ accounts.mint = mint;
6584
+ },
6585
+ stakeAccount: () => {
6586
+ }
6587
+ });
6584
6588
  return this.program.methods.bondInitialize({
6585
6589
  name: props.name,
6586
6590
  bondType: props.bondType,
@@ -6609,7 +6613,7 @@ var JBondClient = class _JBondClient {
6609
6613
  throw new Error("Missing creator");
6610
6614
  }
6611
6615
  const collateralType = (await this.getBondState(bondType, name)).collateralType;
6612
- const acct = {
6616
+ const accounts = {
6613
6617
  bondState,
6614
6618
  validatorBond,
6615
6619
  identity,
@@ -6621,12 +6625,18 @@ var JBondClient = class _JBondClient {
6621
6625
  bondTokenAccount: null,
6622
6626
  mint: null
6623
6627
  };
6624
- if ("token" in collateralType) {
6625
- const mint = collateralType.token[0];
6626
- const bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6627
- Object.assign(acct, { mint, bondTokenAccount });
6628
- }
6629
- return this.program.methods.bondRegister(bondType, name).accountsPartial(acct).instruction();
6628
+ matchVariant(collateralType, {
6629
+ native: () => {
6630
+ },
6631
+ token: (mint) => {
6632
+ accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6633
+ Object.assign(accounts, { mint, bondTokenAccount: accounts.bondTokenAccount });
6634
+ },
6635
+ stakeAccount: () => {
6636
+ throw new Error("Stake account collateral type is not currently supported");
6637
+ }
6638
+ });
6639
+ return this.program.methods.bondRegister(bondType, name).accountsPartial(accounts).instruction();
6630
6640
  }
6631
6641
  async buildTopUpIXs(props) {
6632
6642
  const { bondType, voteAccount, collateral, name } = props;
@@ -6659,19 +6669,23 @@ var JBondClient = class _JBondClient {
6659
6669
  associatedTokenProgram: null,
6660
6670
  stakeProgram: null
6661
6671
  };
6662
- if ("token" in collateralType) {
6663
- const mint = collateralType.token[0];
6664
- accounts.payerTokenAccount = splToken.getAssociatedTokenAddressSync(mint, payer);
6665
- accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6666
- accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
6667
- accounts.associatedTokenProgram = splToken.ASSOCIATED_TOKEN_PROGRAM_ID;
6668
- } else if ("stakeAccount" in collateralType) {
6669
- if (!collateral.stakeAccount) {
6670
- throw new Error("Missing stakeAccount for stake collateral");
6671
- }
6672
- accounts.stakeAccount = collateral.stakeAccount;
6673
- accounts.stakeProgram = web3_js.StakeProgram.programId;
6674
- }
6672
+ matchVariant(collateralType, {
6673
+ native: () => {
6674
+ },
6675
+ token: (mint) => {
6676
+ accounts.payerTokenAccount = splToken.getAssociatedTokenAddressSync(mint, payer);
6677
+ accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6678
+ accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
6679
+ accounts.associatedTokenProgram = splToken.ASSOCIATED_TOKEN_PROGRAM_ID;
6680
+ },
6681
+ stakeAccount: () => {
6682
+ if (!collateral.stakeAccount) {
6683
+ throw new Error("Missing stakeAccount for stake collateral");
6684
+ }
6685
+ accounts.stakeAccount = collateral.stakeAccount;
6686
+ accounts.stakeProgram = web3_js.StakeProgram.programId;
6687
+ }
6688
+ });
6675
6689
  instructions.push(
6676
6690
  await this.program.methods.bondTopUp(this.lamports(collateral.amount)).accountsPartial(accounts).instruction()
6677
6691
  );
@@ -6699,18 +6713,22 @@ var JBondClient = class _JBondClient {
6699
6713
  tokenProgram: null,
6700
6714
  stakeProgram: null
6701
6715
  };
6702
- if ("token" in collateralType) {
6703
- const mint = collateralType.token[0];
6704
- accounts.destinationTokenAccount = splToken.getAssociatedTokenAddressSync(mint, destination, true);
6705
- accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6706
- accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
6707
- } else if ("stakeAccount" in collateralType) {
6708
- if (!withdraw.stakeAccount) {
6709
- throw new Error("Missing stakeAccount for stake collateral");
6710
- }
6711
- accounts.stakeAccount = withdraw.stakeAccount;
6712
- accounts.stakeProgram = web3_js.StakeProgram.programId;
6713
- }
6716
+ matchVariant(collateralType, {
6717
+ native: () => {
6718
+ },
6719
+ token: (mint) => {
6720
+ accounts.destinationTokenAccount = splToken.getAssociatedTokenAddressSync(mint, destination, true);
6721
+ accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6722
+ accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
6723
+ },
6724
+ stakeAccount: () => {
6725
+ if (!withdraw.stakeAccount) {
6726
+ throw new Error("Missing stakeAccount for stake collateral");
6727
+ }
6728
+ accounts.stakeAccount = withdraw.stakeAccount;
6729
+ accounts.stakeProgram = web3_js.StakeProgram.programId;
6730
+ }
6731
+ });
6714
6732
  return this.program.methods.bondWithdraw(this.lamports(withdraw.amount)).accountsPartial(accounts).instruction();
6715
6733
  }
6716
6734
  async buildClaimIx(props) {
@@ -6735,19 +6753,23 @@ var JBondClient = class _JBondClient {
6735
6753
  tokenProgram: null,
6736
6754
  stakeProgram: null
6737
6755
  };
6738
- if ("token" in collateralType) {
6739
- const mint = collateralType.token[0];
6740
- accounts.destinationTokenAccount = splToken.getAssociatedTokenAddressSync(mint, reserve, true);
6741
- accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6742
- accounts.reserveTokenAccount = splToken.getAssociatedTokenAddressSync(mint, reserve, true);
6743
- accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
6744
- } else if ("stakeAccount" in collateralType) {
6745
- if (!claim.stakeAccount) {
6746
- throw new Error("Missing stakeAccount for stake collateral");
6747
- }
6748
- accounts.stakeAccount = claim.stakeAccount;
6749
- accounts.stakeProgram = web3_js.StakeProgram.programId;
6750
- }
6756
+ matchVariant(collateralType, {
6757
+ native: () => {
6758
+ },
6759
+ token: (mint) => {
6760
+ accounts.destinationTokenAccount = splToken.getAssociatedTokenAddressSync(mint, reserve, true);
6761
+ accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6762
+ accounts.reserveTokenAccount = splToken.getAssociatedTokenAddressSync(mint, reserve, true);
6763
+ accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
6764
+ },
6765
+ stakeAccount: () => {
6766
+ if (!claim.stakeAccount) {
6767
+ throw new Error("Missing stakeAccount for stake collateral");
6768
+ }
6769
+ accounts.stakeAccount = claim.stakeAccount;
6770
+ accounts.stakeProgram = web3_js.StakeProgram.programId;
6771
+ }
6772
+ });
6751
6773
  return this.program.methods.bondClaim(this.lamports(props.claim.amount)).accountsPartial(
6752
6774
  accounts
6753
6775
  ).instruction();
@@ -6784,11 +6806,11 @@ var JBondClient = class _JBondClient {
6784
6806
  /**
6785
6807
  * Get all bond states with total collected collateral
6786
6808
  */
6787
- async getAllBondStates(_bondType) {
6809
+ async getAllBondStates(bondType) {
6788
6810
  const bondStates = [];
6789
6811
  const bondStateAccounts = await this.program.account.bondState.all();
6790
6812
  for (const { account: state } of bondStateAccounts) {
6791
- if (!("crowdfunding" in state.bondType)) {
6813
+ if (!sameVariant(state.bondType, bondType)) {
6792
6814
  continue;
6793
6815
  }
6794
6816
  const validatorBonds = await this.getValidatorBondsByState(state.bondType, state.name);
@@ -6797,11 +6819,26 @@ var JBondClient = class _JBondClient {
6797
6819
  (vb) => this.getValidatorBondBalance(state.bondType, state.name, vb.voteAccount).then((v) => v ?? 0)
6798
6820
  )
6799
6821
  );
6800
- const totalCollected = balances.reduce((sum, v) => sum + v, 0);
6801
- bondStates.push([state, totalCollected]);
6822
+ const bondStateStats = {
6823
+ totalCollected: balances.reduce((sum, v) => sum + v, 0),
6824
+ status: await this.getBondStateSessionStatus(state)
6825
+ };
6826
+ bondStates.push([state, bondStateStats]);
6802
6827
  }
6803
6828
  return bondStates;
6804
6829
  }
6830
+ /**
6831
+ * Get session status for bond state
6832
+ */
6833
+ async getBondStateSessionStatus(bondState) {
6834
+ const now = Math.floor(Date.now() / 1e3);
6835
+ if (bondState.sessionFinishTs.toNumber() > now) {
6836
+ return 0 /* Live */;
6837
+ } else {
6838
+ return 1 /* Finished */;
6839
+ }
6840
+ }
6841
+ /** Get all validator bonds for a given bond state */
6805
6842
  async getValidatorBondsByState(bondType, bondName) {
6806
6843
  const validatorBonds = [];
6807
6844
  const bondStatePda = this.pda.bondState(bondType, bondName)[0];
@@ -6850,20 +6887,35 @@ var JBondClient = class _JBondClient {
6850
6887
  const [address] = this.pda.validatorBond(bondType, bondName, new web3_js.PublicKey(vote));
6851
6888
  const bondStateData = await this.getBondState(bondType, bondName);
6852
6889
  const bondStateCollateralType = bondStateData.collateralType;
6853
- if ("token" in bondStateCollateralType) {
6854
- const mint = bondStateCollateralType.token[0];
6855
- const tokenAccount = splToken.getAssociatedTokenAddressSync(mint, address, true);
6856
- const balance = await this.connection.getTokenAccountBalance(tokenAccount);
6857
- return Number(balance.value.uiAmount ?? 0);
6858
- }
6859
- const accountInfo = await this.connection.getAccountInfo(address).catch(() => null);
6860
- if (!accountInfo) {
6861
- return 0;
6862
- }
6863
- const { data, lamports } = accountInfo;
6864
- const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
6865
- const availableBalance = Math.max(0, lamports - rentExempt);
6866
- return availableBalance;
6890
+ return await matchVariant(bondStateCollateralType, {
6891
+ native: async () => {
6892
+ const accountInfo = await this.connection.getAccountInfo(address).catch(() => null);
6893
+ if (!accountInfo) {
6894
+ return 0;
6895
+ }
6896
+ const { data, lamports } = accountInfo;
6897
+ const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
6898
+ return Math.max(0, lamports - rentExempt);
6899
+ },
6900
+ token: async (mint) => {
6901
+ const tokenAccount = splToken.getAssociatedTokenAddressSync(mint, address, true);
6902
+ try {
6903
+ const balance = await this.connection.getTokenAccountBalance(tokenAccount);
6904
+ return Number(balance.value.uiAmount ?? 0);
6905
+ } catch {
6906
+ return 0;
6907
+ }
6908
+ },
6909
+ stakeAccount: async () => {
6910
+ const accountInfo = await this.connection.getAccountInfo(address).catch(() => null);
6911
+ if (!accountInfo) {
6912
+ return 0;
6913
+ }
6914
+ const { data, lamports } = accountInfo;
6915
+ const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
6916
+ return Math.max(0, lamports - rentExempt);
6917
+ }
6918
+ });
6867
6919
  }
6868
6920
  /**
6869
6921
  * Load stake pool account and cache it if not already cached