@jpool/bond-sdk 0.9.0-next.12 → 0.9.0-next.14
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 +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +79 -45
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +81 -47
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1911,7 +1911,7 @@ declare class JBondClient {
|
|
|
1911
1911
|
/**
|
|
1912
1912
|
* Get all bond states with total collected collateral
|
|
1913
1913
|
*/
|
|
1914
|
-
getAllBondStates(bondType: BondType): Promise<Array<[BondState, BondStateStats]>>;
|
|
1914
|
+
getAllBondStates(bondType: BondType, session_status?: SessionStatus): Promise<Array<[BondState, BondStateStats]>>;
|
|
1915
1915
|
/**
|
|
1916
1916
|
* Get session status for bond state
|
|
1917
1917
|
*/
|
|
@@ -1937,6 +1937,8 @@ declare class JBondClient {
|
|
|
1937
1937
|
* @return Balance in lamports
|
|
1938
1938
|
*/
|
|
1939
1939
|
getValidatorBondBalance(bondType: BondType, bondName: string, vote: PublicKeyInitData): Promise<number>;
|
|
1940
|
+
getBondStateTotalCollected(bondType: BondType, bondName: string, votes: PublicKeyInitData[]): Promise<number>;
|
|
1941
|
+
private getBondStateStats;
|
|
1940
1942
|
/**
|
|
1941
1943
|
* Load stake pool account and cache it if not already cached
|
|
1942
1944
|
* @private
|
package/dist/index.d.ts
CHANGED
|
@@ -1911,7 +1911,7 @@ declare class JBondClient {
|
|
|
1911
1911
|
/**
|
|
1912
1912
|
* Get all bond states with total collected collateral
|
|
1913
1913
|
*/
|
|
1914
|
-
getAllBondStates(bondType: BondType): Promise<Array<[BondState, BondStateStats]>>;
|
|
1914
|
+
getAllBondStates(bondType: BondType, session_status?: SessionStatus): Promise<Array<[BondState, BondStateStats]>>;
|
|
1915
1915
|
/**
|
|
1916
1916
|
* Get session status for bond state
|
|
1917
1917
|
*/
|
|
@@ -1937,6 +1937,8 @@ declare class JBondClient {
|
|
|
1937
1937
|
* @return Balance in lamports
|
|
1938
1938
|
*/
|
|
1939
1939
|
getValidatorBondBalance(bondType: BondType, bondName: string, vote: PublicKeyInitData): Promise<number>;
|
|
1940
|
+
getBondStateTotalCollected(bondType: BondType, bondName: string, votes: PublicKeyInitData[]): Promise<number>;
|
|
1941
|
+
private getBondStateStats;
|
|
1940
1942
|
/**
|
|
1941
1943
|
* Load stake pool account and cache it if not already cached
|
|
1942
1944
|
* @private
|
package/dist/index.js
CHANGED
|
@@ -6689,8 +6689,6 @@ var JBondClient = class _JBondClient {
|
|
|
6689
6689
|
token: (mint) => {
|
|
6690
6690
|
accounts.reserveTokenAccount = splToken.getAssociatedTokenAddressSync(mint, props.reserve);
|
|
6691
6691
|
accounts.mint = mint;
|
|
6692
|
-
},
|
|
6693
|
-
stakeAccount: () => {
|
|
6694
6692
|
}
|
|
6695
6693
|
});
|
|
6696
6694
|
return this.program.methods.bondInitialize({
|
|
@@ -6739,9 +6737,6 @@ var JBondClient = class _JBondClient {
|
|
|
6739
6737
|
token: (mint) => {
|
|
6740
6738
|
accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
|
|
6741
6739
|
Object.assign(accounts, { mint, bondTokenAccount: accounts.bondTokenAccount });
|
|
6742
|
-
},
|
|
6743
|
-
stakeAccount: () => {
|
|
6744
|
-
throw new Error("Stake account collateral type is not currently supported");
|
|
6745
6740
|
}
|
|
6746
6741
|
});
|
|
6747
6742
|
return this.program.methods.bondRegister(bondType, name).accountsPartial(accounts).instruction();
|
|
@@ -6787,13 +6782,6 @@ var JBondClient = class _JBondClient {
|
|
|
6787
6782
|
accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
|
|
6788
6783
|
accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
|
|
6789
6784
|
accounts.associatedTokenProgram = splToken.ASSOCIATED_TOKEN_PROGRAM_ID;
|
|
6790
|
-
},
|
|
6791
|
-
stakeAccount: () => {
|
|
6792
|
-
if (!collateral.stakeAccount) {
|
|
6793
|
-
throw new Error("Missing stakeAccount for stake collateral");
|
|
6794
|
-
}
|
|
6795
|
-
accounts.stakeAccount = collateral.stakeAccount;
|
|
6796
|
-
accounts.stakeProgram = web3_js.StakeProgram.programId;
|
|
6797
6785
|
}
|
|
6798
6786
|
});
|
|
6799
6787
|
instructions.push(
|
|
@@ -6830,19 +6818,12 @@ var JBondClient = class _JBondClient {
|
|
|
6830
6818
|
accounts.destinationTokenAccount = splToken.getAssociatedTokenAddressSync(mint, destination, true);
|
|
6831
6819
|
accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
|
|
6832
6820
|
accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
|
|
6833
|
-
},
|
|
6834
|
-
stakeAccount: () => {
|
|
6835
|
-
if (!withdraw.stakeAccount) {
|
|
6836
|
-
throw new Error("Missing stakeAccount for stake collateral");
|
|
6837
|
-
}
|
|
6838
|
-
accounts.stakeAccount = withdraw.stakeAccount;
|
|
6839
|
-
accounts.stakeProgram = web3_js.StakeProgram.programId;
|
|
6840
6821
|
}
|
|
6841
6822
|
});
|
|
6842
6823
|
return this.program.methods.bondWithdraw(this.lamports(withdraw.amount)).accountsPartial(accounts).instruction();
|
|
6843
6824
|
}
|
|
6844
6825
|
async buildClaimIx(props) {
|
|
6845
|
-
const {
|
|
6826
|
+
const { bondType, name } = props;
|
|
6846
6827
|
const [bondState] = this.pda.bondState(bondType, name);
|
|
6847
6828
|
const [validatorBond] = this.pda.validatorBond(bondType, name, props.voteAccount);
|
|
6848
6829
|
const reserve = props.reserve ?? (await this.getBondState(bondType, name)).reserve;
|
|
@@ -6871,13 +6852,6 @@ var JBondClient = class _JBondClient {
|
|
|
6871
6852
|
accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
|
|
6872
6853
|
accounts.reserveTokenAccount = splToken.getAssociatedTokenAddressSync(mint, reserve, true);
|
|
6873
6854
|
accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
|
|
6874
|
-
},
|
|
6875
|
-
stakeAccount: () => {
|
|
6876
|
-
if (!claim.stakeAccount) {
|
|
6877
|
-
throw new Error("Missing stakeAccount for stake collateral");
|
|
6878
|
-
}
|
|
6879
|
-
accounts.stakeAccount = claim.stakeAccount;
|
|
6880
|
-
accounts.stakeProgram = web3_js.StakeProgram.programId;
|
|
6881
6855
|
}
|
|
6882
6856
|
});
|
|
6883
6857
|
return this.program.methods.bondClaim(this.lamports(props.claim.amount)).accountsPartial(
|
|
@@ -6934,23 +6908,17 @@ var JBondClient = class _JBondClient {
|
|
|
6934
6908
|
/**
|
|
6935
6909
|
* Get all bond states with total collected collateral
|
|
6936
6910
|
*/
|
|
6937
|
-
async getAllBondStates(bondType) {
|
|
6911
|
+
async getAllBondStates(bondType, session_status) {
|
|
6938
6912
|
const bondStates = [];
|
|
6939
6913
|
const bondStateAccounts = await this.program.account.bondState.all();
|
|
6940
6914
|
for (const { account: state } of bondStateAccounts) {
|
|
6941
6915
|
if (!sameVariant(state.bondType, bondType)) {
|
|
6942
6916
|
continue;
|
|
6943
6917
|
}
|
|
6944
|
-
|
|
6945
|
-
|
|
6946
|
-
|
|
6947
|
-
|
|
6948
|
-
)
|
|
6949
|
-
);
|
|
6950
|
-
const bondStateStats = {
|
|
6951
|
-
totalCollected: balances.reduce((sum, v) => sum + v, 0),
|
|
6952
|
-
status: await this.getBondStateSessionStatus(state)
|
|
6953
|
-
};
|
|
6918
|
+
if (session_status !== void 0 && this.getBondStateSessionStatus(state) !== session_status) {
|
|
6919
|
+
continue;
|
|
6920
|
+
}
|
|
6921
|
+
const bondStateStats = await this.getBondStateStats(state);
|
|
6954
6922
|
bondStates.push([state, bondStateStats]);
|
|
6955
6923
|
}
|
|
6956
6924
|
return bondStates;
|
|
@@ -6958,7 +6926,7 @@ var JBondClient = class _JBondClient {
|
|
|
6958
6926
|
/**
|
|
6959
6927
|
* Get session status for bond state
|
|
6960
6928
|
*/
|
|
6961
|
-
|
|
6929
|
+
getBondStateSessionStatus(bondState) {
|
|
6962
6930
|
const now = Math.floor(Date.now() / 1e3);
|
|
6963
6931
|
if (bondState.sessionFinishTs.toNumber() > now) {
|
|
6964
6932
|
return 0 /* Live */;
|
|
@@ -7033,18 +7001,84 @@ var JBondClient = class _JBondClient {
|
|
|
7033
7001
|
} catch {
|
|
7034
7002
|
return 0;
|
|
7035
7003
|
}
|
|
7004
|
+
}
|
|
7005
|
+
});
|
|
7006
|
+
}
|
|
7007
|
+
// Get total collected collateral for a bond state
|
|
7008
|
+
async getBondStateTotalCollected(bondType, bondName, votes) {
|
|
7009
|
+
const bondStateData = await this.getBondState(bondType, bondName);
|
|
7010
|
+
const bondStateCollateralType = bondStateData.collateralType;
|
|
7011
|
+
const chunk = (arr, n = 100) => {
|
|
7012
|
+
const res = [];
|
|
7013
|
+
for (let i = 0; i < arr.length; i += n) {
|
|
7014
|
+
res.push(arr.slice(i, i + n));
|
|
7015
|
+
}
|
|
7016
|
+
return res;
|
|
7017
|
+
};
|
|
7018
|
+
return await matchVariant(bondStateCollateralType, {
|
|
7019
|
+
native: async () => {
|
|
7020
|
+
const addresses = votes.map((vote) => {
|
|
7021
|
+
const [address] = this.pda.validatorBond(bondType, bondName, new web3_js.PublicKey(vote));
|
|
7022
|
+
return address;
|
|
7023
|
+
});
|
|
7024
|
+
const accountInfos = await this.connection.getMultipleAccountsInfo(addresses);
|
|
7025
|
+
const balances = await Promise.all(
|
|
7026
|
+
accountInfos.map(async (accountInfo) => {
|
|
7027
|
+
if (!accountInfo) {
|
|
7028
|
+
return 0;
|
|
7029
|
+
}
|
|
7030
|
+
const { lamports } = accountInfo;
|
|
7031
|
+
return lamports;
|
|
7032
|
+
})
|
|
7033
|
+
);
|
|
7034
|
+
return balances.reduce((sum, v) => sum + v, 0);
|
|
7036
7035
|
},
|
|
7037
|
-
|
|
7038
|
-
|
|
7039
|
-
|
|
7036
|
+
// TODO
|
|
7037
|
+
token: async (mint) => {
|
|
7038
|
+
const tokenAccounts = votes.map((vote) => {
|
|
7039
|
+
const [validatorBondAddress] = this.pda.validatorBond(bondType, bondName, new web3_js.PublicKey(vote));
|
|
7040
|
+
return splToken.getAssociatedTokenAddressSync(mint, validatorBondAddress, true);
|
|
7041
|
+
});
|
|
7042
|
+
if (tokenAccounts.length === 0) {
|
|
7040
7043
|
return 0;
|
|
7041
7044
|
}
|
|
7042
|
-
|
|
7043
|
-
const
|
|
7044
|
-
|
|
7045
|
+
let total = 0n;
|
|
7046
|
+
for (const part of chunk(tokenAccounts, 100)) {
|
|
7047
|
+
const infos = await this.connection.getMultipleAccountsInfo(part).catch(() => []);
|
|
7048
|
+
for (let i = 0; i < infos.length; i++) {
|
|
7049
|
+
const info = infos[i];
|
|
7050
|
+
if (!info) {
|
|
7051
|
+
continue;
|
|
7052
|
+
}
|
|
7053
|
+
if (info.data.length !== splToken.ACCOUNT_SIZE) {
|
|
7054
|
+
continue;
|
|
7055
|
+
}
|
|
7056
|
+
try {
|
|
7057
|
+
const acc = splToken.AccountLayout.decode(info.data);
|
|
7058
|
+
const raw = BigInt(acc.amount.toString());
|
|
7059
|
+
total += raw;
|
|
7060
|
+
} catch {
|
|
7061
|
+
}
|
|
7062
|
+
}
|
|
7063
|
+
}
|
|
7064
|
+
const asNumber = Number(total);
|
|
7065
|
+
return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
|
|
7045
7066
|
}
|
|
7046
7067
|
});
|
|
7047
7068
|
}
|
|
7069
|
+
async getBondStateStats(state) {
|
|
7070
|
+
const validatorBonds = await this.getValidatorBondsByState(state.bondType, state.name);
|
|
7071
|
+
const totalCollected = await this.getBondStateTotalCollected(
|
|
7072
|
+
state.bondType,
|
|
7073
|
+
state.name,
|
|
7074
|
+
validatorBonds.map((vb) => vb.voteAccount)
|
|
7075
|
+
);
|
|
7076
|
+
const bondStateStats = {
|
|
7077
|
+
totalCollected,
|
|
7078
|
+
status: await this.getBondStateSessionStatus(state)
|
|
7079
|
+
};
|
|
7080
|
+
return bondStateStats;
|
|
7081
|
+
}
|
|
7048
7082
|
/**
|
|
7049
7083
|
* Load stake pool account and cache it if not already cached
|
|
7050
7084
|
* @private
|