@jpool/bond-sdk 0.9.0-next.8 → 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 +5 -29
- package/dist/index.d.ts +5 -29
- package/dist/index.js +144 -103
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +145 -105
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Program, AnchorProvider, BN } from '@coral-xyz/anchor';
|
|
2
2
|
import { getStakePoolAccount, STAKE_POOL_PROGRAM_ID, StakePoolInstruction } from '@solana/spl-stake-pool';
|
|
3
|
-
import {
|
|
4
|
-
import { PublicKey, Transaction, SystemProgram,
|
|
3
|
+
import { TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, getAssociatedTokenAddressSync } from '@solana/spl-token';
|
|
4
|
+
import { PublicKey, Transaction, SystemProgram, LAMPORTS_PER_SOL, StakeProgram } from '@solana/web3.js';
|
|
5
5
|
import bs58 from 'bs58';
|
|
6
6
|
|
|
7
7
|
var __create = Object.create;
|
|
@@ -4582,6 +4582,39 @@ var SessionStatus = /* @__PURE__ */ ((SessionStatus2) => {
|
|
|
4582
4582
|
return SessionStatus2;
|
|
4583
4583
|
})(SessionStatus || {});
|
|
4584
4584
|
|
|
4585
|
+
// src/utils/enum-utils.ts
|
|
4586
|
+
function getVariant(v) {
|
|
4587
|
+
const key = Object.keys(v)[0];
|
|
4588
|
+
return [key, v[key]];
|
|
4589
|
+
}
|
|
4590
|
+
function variantArgs(payload) {
|
|
4591
|
+
if (Array.isArray(payload)) {
|
|
4592
|
+
return payload;
|
|
4593
|
+
}
|
|
4594
|
+
if (payload && typeof payload === "object") {
|
|
4595
|
+
const numericKeys = Object.keys(payload).filter((k) => String(+k) === k);
|
|
4596
|
+
if (numericKeys.length) {
|
|
4597
|
+
return numericKeys.sort((a, b) => +a - +b).map((k) => payload[k]);
|
|
4598
|
+
}
|
|
4599
|
+
return [payload];
|
|
4600
|
+
}
|
|
4601
|
+
return [payload];
|
|
4602
|
+
}
|
|
4603
|
+
function matchVariant(v, handlers) {
|
|
4604
|
+
const [k, data] = getVariant(v);
|
|
4605
|
+
const h = handlers[k];
|
|
4606
|
+
if (!h) {
|
|
4607
|
+
throw new Error(`Unhandled variant: ${k}`);
|
|
4608
|
+
}
|
|
4609
|
+
return h(...variantArgs(data));
|
|
4610
|
+
}
|
|
4611
|
+
function variantKey(v) {
|
|
4612
|
+
return Object.keys(v)[0];
|
|
4613
|
+
}
|
|
4614
|
+
function sameVariant(a, b) {
|
|
4615
|
+
return variantKey(a) === variantKey(b);
|
|
4616
|
+
}
|
|
4617
|
+
|
|
4585
4618
|
// src/utils/wallet.ts
|
|
4586
4619
|
function isVersionedTransaction(tx) {
|
|
4587
4620
|
return "version" in tx;
|
|
@@ -5966,17 +5999,6 @@ var jbond_default = {
|
|
|
5966
5999
|
],
|
|
5967
6000
|
type: "u64"
|
|
5968
6001
|
},
|
|
5969
|
-
{
|
|
5970
|
-
name: "status",
|
|
5971
|
-
docs: [
|
|
5972
|
-
"Current status of the bond"
|
|
5973
|
-
],
|
|
5974
|
-
type: {
|
|
5975
|
-
defined: {
|
|
5976
|
-
name: "BondStatus"
|
|
5977
|
-
}
|
|
5978
|
-
}
|
|
5979
|
-
},
|
|
5980
6002
|
{
|
|
5981
6003
|
name: "session_start_ts",
|
|
5982
6004
|
docs: [
|
|
@@ -6001,23 +6023,6 @@ var jbond_default = {
|
|
|
6001
6023
|
]
|
|
6002
6024
|
}
|
|
6003
6025
|
},
|
|
6004
|
-
{
|
|
6005
|
-
name: "BondStatus",
|
|
6006
|
-
type: {
|
|
6007
|
-
kind: "enum",
|
|
6008
|
-
variants: [
|
|
6009
|
-
{
|
|
6010
|
-
name: "Active"
|
|
6011
|
-
},
|
|
6012
|
-
{
|
|
6013
|
-
name: "Inactive"
|
|
6014
|
-
},
|
|
6015
|
-
{
|
|
6016
|
-
name: "Pending"
|
|
6017
|
-
}
|
|
6018
|
-
]
|
|
6019
|
-
}
|
|
6020
|
-
},
|
|
6021
6026
|
{
|
|
6022
6027
|
name: "BondType",
|
|
6023
6028
|
docs: [
|
|
@@ -6378,13 +6383,8 @@ var JBondClient = class _JBondClient {
|
|
|
6378
6383
|
return this;
|
|
6379
6384
|
}
|
|
6380
6385
|
getBondTypeSeed(bondType) {
|
|
6381
|
-
|
|
6382
|
-
|
|
6383
|
-
}
|
|
6384
|
-
if ("crowdfunding" in bondType) {
|
|
6385
|
-
return "crowdfunding" /* CrowdFundingBond */;
|
|
6386
|
-
}
|
|
6387
|
-
throw new Error(`Unknown bond type: ${JSON.stringify(bondType)}`);
|
|
6386
|
+
const [k] = getVariant(bondType);
|
|
6387
|
+
return bondTypeToSeed[k];
|
|
6388
6388
|
}
|
|
6389
6389
|
/**
|
|
6390
6390
|
* Program Derived Addresses (PDAs)
|
|
@@ -6539,13 +6539,16 @@ var JBondClient = class _JBondClient {
|
|
|
6539
6539
|
reserveTokenAccount: null,
|
|
6540
6540
|
mint: null
|
|
6541
6541
|
};
|
|
6542
|
-
|
|
6543
|
-
|
|
6544
|
-
|
|
6545
|
-
|
|
6546
|
-
|
|
6547
|
-
|
|
6548
|
-
|
|
6542
|
+
matchVariant(props.collateralType, {
|
|
6543
|
+
native: () => {
|
|
6544
|
+
},
|
|
6545
|
+
token: (mint) => {
|
|
6546
|
+
accounts.reserveTokenAccount = getAssociatedTokenAddressSync(mint, props.reserve);
|
|
6547
|
+
accounts.mint = mint;
|
|
6548
|
+
},
|
|
6549
|
+
stakeAccount: () => {
|
|
6550
|
+
}
|
|
6551
|
+
});
|
|
6549
6552
|
return this.program.methods.bondInitialize({
|
|
6550
6553
|
name: props.name,
|
|
6551
6554
|
bondType: props.bondType,
|
|
@@ -6574,7 +6577,7 @@ var JBondClient = class _JBondClient {
|
|
|
6574
6577
|
throw new Error("Missing creator");
|
|
6575
6578
|
}
|
|
6576
6579
|
const collateralType = (await this.getBondState(bondType, name)).collateralType;
|
|
6577
|
-
const
|
|
6580
|
+
const accounts = {
|
|
6578
6581
|
bondState,
|
|
6579
6582
|
validatorBond,
|
|
6580
6583
|
identity,
|
|
@@ -6586,12 +6589,18 @@ var JBondClient = class _JBondClient {
|
|
|
6586
6589
|
bondTokenAccount: null,
|
|
6587
6590
|
mint: null
|
|
6588
6591
|
};
|
|
6589
|
-
|
|
6590
|
-
|
|
6591
|
-
|
|
6592
|
-
|
|
6593
|
-
|
|
6594
|
-
|
|
6592
|
+
matchVariant(collateralType, {
|
|
6593
|
+
native: () => {
|
|
6594
|
+
},
|
|
6595
|
+
token: (mint) => {
|
|
6596
|
+
accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
|
|
6597
|
+
Object.assign(accounts, { mint, bondTokenAccount: accounts.bondTokenAccount });
|
|
6598
|
+
},
|
|
6599
|
+
stakeAccount: () => {
|
|
6600
|
+
throw new Error("Stake account collateral type is not currently supported");
|
|
6601
|
+
}
|
|
6602
|
+
});
|
|
6603
|
+
return this.program.methods.bondRegister(bondType, name).accountsPartial(accounts).instruction();
|
|
6595
6604
|
}
|
|
6596
6605
|
async buildTopUpIXs(props) {
|
|
6597
6606
|
const { bondType, voteAccount, collateral, name } = props;
|
|
@@ -6624,19 +6633,23 @@ var JBondClient = class _JBondClient {
|
|
|
6624
6633
|
associatedTokenProgram: null,
|
|
6625
6634
|
stakeProgram: null
|
|
6626
6635
|
};
|
|
6627
|
-
|
|
6628
|
-
|
|
6629
|
-
|
|
6630
|
-
|
|
6631
|
-
|
|
6632
|
-
|
|
6633
|
-
|
|
6634
|
-
|
|
6635
|
-
|
|
6636
|
-
|
|
6637
|
-
|
|
6638
|
-
|
|
6639
|
-
|
|
6636
|
+
matchVariant(collateralType, {
|
|
6637
|
+
native: () => {
|
|
6638
|
+
},
|
|
6639
|
+
token: (mint) => {
|
|
6640
|
+
accounts.payerTokenAccount = getAssociatedTokenAddressSync(mint, payer);
|
|
6641
|
+
accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
|
|
6642
|
+
accounts.tokenProgram = TOKEN_PROGRAM_ID;
|
|
6643
|
+
accounts.associatedTokenProgram = ASSOCIATED_TOKEN_PROGRAM_ID;
|
|
6644
|
+
},
|
|
6645
|
+
stakeAccount: () => {
|
|
6646
|
+
if (!collateral.stakeAccount) {
|
|
6647
|
+
throw new Error("Missing stakeAccount for stake collateral");
|
|
6648
|
+
}
|
|
6649
|
+
accounts.stakeAccount = collateral.stakeAccount;
|
|
6650
|
+
accounts.stakeProgram = StakeProgram.programId;
|
|
6651
|
+
}
|
|
6652
|
+
});
|
|
6640
6653
|
instructions.push(
|
|
6641
6654
|
await this.program.methods.bondTopUp(this.lamports(collateral.amount)).accountsPartial(accounts).instruction()
|
|
6642
6655
|
);
|
|
@@ -6664,18 +6677,22 @@ var JBondClient = class _JBondClient {
|
|
|
6664
6677
|
tokenProgram: null,
|
|
6665
6678
|
stakeProgram: null
|
|
6666
6679
|
};
|
|
6667
|
-
|
|
6668
|
-
|
|
6669
|
-
|
|
6670
|
-
|
|
6671
|
-
|
|
6672
|
-
|
|
6673
|
-
|
|
6674
|
-
|
|
6675
|
-
|
|
6676
|
-
|
|
6677
|
-
|
|
6678
|
-
|
|
6680
|
+
matchVariant(collateralType, {
|
|
6681
|
+
native: () => {
|
|
6682
|
+
},
|
|
6683
|
+
token: (mint) => {
|
|
6684
|
+
accounts.destinationTokenAccount = getAssociatedTokenAddressSync(mint, destination, true);
|
|
6685
|
+
accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
|
|
6686
|
+
accounts.tokenProgram = TOKEN_PROGRAM_ID;
|
|
6687
|
+
},
|
|
6688
|
+
stakeAccount: () => {
|
|
6689
|
+
if (!withdraw.stakeAccount) {
|
|
6690
|
+
throw new Error("Missing stakeAccount for stake collateral");
|
|
6691
|
+
}
|
|
6692
|
+
accounts.stakeAccount = withdraw.stakeAccount;
|
|
6693
|
+
accounts.stakeProgram = StakeProgram.programId;
|
|
6694
|
+
}
|
|
6695
|
+
});
|
|
6679
6696
|
return this.program.methods.bondWithdraw(this.lamports(withdraw.amount)).accountsPartial(accounts).instruction();
|
|
6680
6697
|
}
|
|
6681
6698
|
async buildClaimIx(props) {
|
|
@@ -6700,19 +6717,23 @@ var JBondClient = class _JBondClient {
|
|
|
6700
6717
|
tokenProgram: null,
|
|
6701
6718
|
stakeProgram: null
|
|
6702
6719
|
};
|
|
6703
|
-
|
|
6704
|
-
|
|
6705
|
-
|
|
6706
|
-
|
|
6707
|
-
|
|
6708
|
-
|
|
6709
|
-
|
|
6710
|
-
|
|
6711
|
-
|
|
6712
|
-
|
|
6713
|
-
|
|
6714
|
-
|
|
6715
|
-
|
|
6720
|
+
matchVariant(collateralType, {
|
|
6721
|
+
native: () => {
|
|
6722
|
+
},
|
|
6723
|
+
token: (mint) => {
|
|
6724
|
+
accounts.destinationTokenAccount = getAssociatedTokenAddressSync(mint, reserve, true);
|
|
6725
|
+
accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
|
|
6726
|
+
accounts.reserveTokenAccount = getAssociatedTokenAddressSync(mint, reserve, true);
|
|
6727
|
+
accounts.tokenProgram = TOKEN_PROGRAM_ID;
|
|
6728
|
+
},
|
|
6729
|
+
stakeAccount: () => {
|
|
6730
|
+
if (!claim.stakeAccount) {
|
|
6731
|
+
throw new Error("Missing stakeAccount for stake collateral");
|
|
6732
|
+
}
|
|
6733
|
+
accounts.stakeAccount = claim.stakeAccount;
|
|
6734
|
+
accounts.stakeProgram = StakeProgram.programId;
|
|
6735
|
+
}
|
|
6736
|
+
});
|
|
6716
6737
|
return this.program.methods.bondClaim(this.lamports(props.claim.amount)).accountsPartial(
|
|
6717
6738
|
accounts
|
|
6718
6739
|
).instruction();
|
|
@@ -6749,11 +6770,11 @@ var JBondClient = class _JBondClient {
|
|
|
6749
6770
|
/**
|
|
6750
6771
|
* Get all bond states with total collected collateral
|
|
6751
6772
|
*/
|
|
6752
|
-
async getAllBondStates(
|
|
6773
|
+
async getAllBondStates(bondType) {
|
|
6753
6774
|
const bondStates = [];
|
|
6754
6775
|
const bondStateAccounts = await this.program.account.bondState.all();
|
|
6755
6776
|
for (const { account: state } of bondStateAccounts) {
|
|
6756
|
-
if (!(
|
|
6777
|
+
if (!sameVariant(state.bondType, bondType)) {
|
|
6757
6778
|
continue;
|
|
6758
6779
|
}
|
|
6759
6780
|
const validatorBonds = await this.getValidatorBondsByState(state.bondType, state.name);
|
|
@@ -6770,6 +6791,9 @@ var JBondClient = class _JBondClient {
|
|
|
6770
6791
|
}
|
|
6771
6792
|
return bondStates;
|
|
6772
6793
|
}
|
|
6794
|
+
/**
|
|
6795
|
+
* Get session status for bond state
|
|
6796
|
+
*/
|
|
6773
6797
|
async getBondStateSessionStatus(bondState) {
|
|
6774
6798
|
const now = Math.floor(Date.now() / 1e3);
|
|
6775
6799
|
if (bondState.sessionFinishTs.toNumber() > now) {
|
|
@@ -6778,6 +6802,7 @@ var JBondClient = class _JBondClient {
|
|
|
6778
6802
|
return 1 /* Finished */;
|
|
6779
6803
|
}
|
|
6780
6804
|
}
|
|
6805
|
+
/** Get all validator bonds for a given bond state */
|
|
6781
6806
|
async getValidatorBondsByState(bondType, bondName) {
|
|
6782
6807
|
const validatorBonds = [];
|
|
6783
6808
|
const bondStatePda = this.pda.bondState(bondType, bondName)[0];
|
|
@@ -6826,20 +6851,35 @@ var JBondClient = class _JBondClient {
|
|
|
6826
6851
|
const [address] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
|
|
6827
6852
|
const bondStateData = await this.getBondState(bondType, bondName);
|
|
6828
6853
|
const bondStateCollateralType = bondStateData.collateralType;
|
|
6829
|
-
|
|
6830
|
-
|
|
6831
|
-
|
|
6832
|
-
|
|
6833
|
-
|
|
6834
|
-
|
|
6835
|
-
|
|
6836
|
-
|
|
6837
|
-
|
|
6838
|
-
|
|
6839
|
-
|
|
6840
|
-
|
|
6841
|
-
|
|
6842
|
-
|
|
6854
|
+
return await matchVariant(bondStateCollateralType, {
|
|
6855
|
+
native: async () => {
|
|
6856
|
+
const accountInfo = await this.connection.getAccountInfo(address).catch(() => null);
|
|
6857
|
+
if (!accountInfo) {
|
|
6858
|
+
return 0;
|
|
6859
|
+
}
|
|
6860
|
+
const { data, lamports } = accountInfo;
|
|
6861
|
+
const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
|
|
6862
|
+
return Math.max(0, lamports - rentExempt);
|
|
6863
|
+
},
|
|
6864
|
+
token: async (mint) => {
|
|
6865
|
+
const tokenAccount = getAssociatedTokenAddressSync(mint, address, true);
|
|
6866
|
+
try {
|
|
6867
|
+
const balance = await this.connection.getTokenAccountBalance(tokenAccount);
|
|
6868
|
+
return Number(balance.value.uiAmount ?? 0);
|
|
6869
|
+
} catch {
|
|
6870
|
+
return 0;
|
|
6871
|
+
}
|
|
6872
|
+
},
|
|
6873
|
+
stakeAccount: async () => {
|
|
6874
|
+
const accountInfo = await this.connection.getAccountInfo(address).catch(() => null);
|
|
6875
|
+
if (!accountInfo) {
|
|
6876
|
+
return 0;
|
|
6877
|
+
}
|
|
6878
|
+
const { data, lamports } = accountInfo;
|
|
6879
|
+
const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
|
|
6880
|
+
return Math.max(0, lamports - rentExempt);
|
|
6881
|
+
}
|
|
6882
|
+
});
|
|
6843
6883
|
}
|
|
6844
6884
|
/**
|
|
6845
6885
|
* Load stake pool account and cache it if not already cached
|