@kamino-finance/klend-sdk 5.10.28-beta.1 → 5.10.28
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/classes/index.d.ts +0 -3
- package/dist/classes/index.d.ts.map +1 -1
- package/dist/classes/index.js +0 -3
- package/dist/classes/index.js.map +1 -1
- package/dist/classes/manager.d.ts +6 -0
- package/dist/classes/manager.d.ts.map +1 -1
- package/dist/classes/manager.js +8 -0
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/types.d.ts +1 -13
- package/dist/classes/types.d.ts.map +1 -1
- package/dist/classes/vault.d.ts +7 -0
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +18 -0
- package/dist/classes/vault.js.map +1 -1
- package/package.json +1 -4
- package/src/classes/index.ts +0 -3
- package/src/classes/manager.ts +9 -0
- package/src/classes/types.ts +1 -17
- package/src/classes/vault.ts +21 -1
- package/dist/classes/stakePool.d.ts +0 -8
- package/dist/classes/stakePool.d.ts.map +0 -1
- package/dist/classes/stakePool.js +0 -18
- package/dist/classes/stakePool.js.map +0 -1
- package/dist/classes/standardStakePool.d.ts +0 -174
- package/dist/classes/standardStakePool.d.ts.map +0 -1
- package/dist/classes/standardStakePool.js +0 -275
- package/dist/classes/standardStakePool.js.map +0 -1
- package/dist/classes/unstakingPool.d.ts +0 -117
- package/dist/classes/unstakingPool.d.ts.map +0 -1
- package/dist/classes/unstakingPool.js +0 -448
- package/dist/classes/unstakingPool.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/accounts/PoolState.d.ts +0 -50
- package/dist/idl_codegen_unstaking_pool/accounts/PoolState.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/accounts/PoolState.js +0 -156
- package/dist/idl_codegen_unstaking_pool/accounts/PoolState.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/accounts/index.d.ts +0 -3
- package/dist/idl_codegen_unstaking_pool/accounts/index.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/accounts/index.js +0 -6
- package/dist/idl_codegen_unstaking_pool/accounts/index.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/errors/anchor.d.ts +0 -435
- package/dist/idl_codegen_unstaking_pool/errors/anchor.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/errors/anchor.js +0 -767
- package/dist/idl_codegen_unstaking_pool/errors/anchor.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/errors/custom.d.ts +0 -163
- package/dist/idl_codegen_unstaking_pool/errors/custom.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/errors/custom.js +0 -290
- package/dist/idl_codegen_unstaking_pool/errors/custom.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/errors/index.d.ts +0 -6
- package/dist/idl_codegen_unstaking_pool/errors/index.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/errors/index.js +0 -75
- package/dist/idl_codegen_unstaking_pool/errors/index.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/burn.d.ts +0 -21
- package/dist/idl_codegen_unstaking_pool/instructions/burn.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/burn.js +0 -73
- package/dist/idl_codegen_unstaking_pool/instructions/burn.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/collect.d.ts +0 -16
- package/dist/idl_codegen_unstaking_pool/instructions/collect.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/collect.js +0 -29
- package/dist/idl_codegen_unstaking_pool/instructions/collect.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/index.d.ts +0 -11
- package/dist/idl_codegen_unstaking_pool/instructions/index.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/index.js +0 -14
- package/dist/idl_codegen_unstaking_pool/instructions/index.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/initializePool.d.ts +0 -15
- package/dist/idl_codegen_unstaking_pool/instructions/initializePool.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/initializePool.js +0 -28
- package/dist/idl_codegen_unstaking_pool/instructions/initializePool.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/mint.d.ts +0 -21
- package/dist/idl_codegen_unstaking_pool/instructions/mint.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/mint.js +0 -77
- package/dist/idl_codegen_unstaking_pool/instructions/mint.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/updatePoolConfig.d.ts +0 -13
- package/dist/idl_codegen_unstaking_pool/instructions/updatePoolConfig.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/instructions/updatePoolConfig.js +0 -58
- package/dist/idl_codegen_unstaking_pool/instructions/updatePoolConfig.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/programId.d.ts +0 -4
- package/dist/idl_codegen_unstaking_pool/programId.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/programId.js +0 -9
- package/dist/idl_codegen_unstaking_pool/programId.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/types/PoolConfigField.d.ts +0 -32
- package/dist/idl_codegen_unstaking_pool/types/PoolConfigField.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/types/PoolConfigField.js +0 -105
- package/dist/idl_codegen_unstaking_pool/types/PoolConfigField.js.map +0 -1
- package/dist/idl_codegen_unstaking_pool/types/index.d.ts +0 -5
- package/dist/idl_codegen_unstaking_pool/types/index.d.ts.map +0 -1
- package/dist/idl_codegen_unstaking_pool/types/index.js +0 -39
- package/dist/idl_codegen_unstaking_pool/types/index.js.map +0 -1
- package/src/classes/stakePool.ts +0 -21
- package/src/classes/standardStakePool.ts +0 -358
- package/src/classes/unstakingPool.ts +0 -577
- package/src/idl_codegen_unstaking_pool/accounts/PoolState.ts +0 -163
- package/src/idl_codegen_unstaking_pool/accounts/index.ts +0 -2
- package/src/idl_codegen_unstaking_pool/errors/anchor.ts +0 -764
- package/src/idl_codegen_unstaking_pool/errors/custom.ts +0 -288
- package/src/idl_codegen_unstaking_pool/errors/index.ts +0 -49
- package/src/idl_codegen_unstaking_pool/instructions/burn.ts +0 -60
- package/src/idl_codegen_unstaking_pool/instructions/collect.ts +0 -43
- package/src/idl_codegen_unstaking_pool/instructions/index.ts +0 -10
- package/src/idl_codegen_unstaking_pool/instructions/initializePool.ts +0 -41
- package/src/idl_codegen_unstaking_pool/instructions/mint.ts +0 -64
- package/src/idl_codegen_unstaking_pool/instructions/updatePoolConfig.ts +0 -40
- package/src/idl_codegen_unstaking_pool/programId.ts +0 -7
- package/src/idl_codegen_unstaking_pool/types/PoolConfigField.ts +0 -85
- package/src/idl_codegen_unstaking_pool/types/index.ts +0 -6
- package/src/idl_unstaking_pool.json +0 -456
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.PoolConfigField = void 0;
|
|
37
|
-
const PoolConfigField = __importStar(require("./PoolConfigField"));
|
|
38
|
-
exports.PoolConfigField = PoolConfigField;
|
|
39
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/idl_codegen_unstaking_pool/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mEAAqD;AAE5C,0CAAe"}
|
package/src/classes/stakePool.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Connection, PublicKey } from '@solana/web3.js/lib';
|
|
2
|
-
import { maybeGetStakedPoolByMint } from './standardStakePool';
|
|
3
|
-
import { StakePool as StandardStakePool } from '@solana/spl-stake-pool/dist/layouts';
|
|
4
|
-
|
|
5
|
-
// Expand this type to represent all supported stake pool implementations
|
|
6
|
-
export type GenericStakePool = StandardStakePool;
|
|
7
|
-
export enum StakePoolType {
|
|
8
|
-
Standard,
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function mapStakedSolMintToPool(
|
|
12
|
-
connection: Connection,
|
|
13
|
-
mint: PublicKey
|
|
14
|
-
): Promise<[GenericStakePool, PublicKey, StakePoolType]> {
|
|
15
|
-
// We cannot know which pool the mint corresponds to, so we fetch them program by program
|
|
16
|
-
const maybeStandardPoolAndKey = await maybeGetStakedPoolByMint(connection, mint);
|
|
17
|
-
if (maybeStandardPoolAndKey) {
|
|
18
|
-
return [...maybeStandardPoolAndKey, StakePoolType.Standard];
|
|
19
|
-
}
|
|
20
|
-
throw new Error(`Cannot map mint ${mint} to staked sol pool`);
|
|
21
|
-
}
|
|
@@ -1,358 +0,0 @@
|
|
|
1
|
-
import { STAKE_POOL_PROGRAM_ID, ValidatorList, ValidatorListLayout } from '@solana/spl-stake-pool';
|
|
2
|
-
import { StakePool, StakePoolLayout } from '@solana/spl-stake-pool';
|
|
3
|
-
import { AccountMeta, Connection, Keypair, PublicKey } from '@solana/web3.js';
|
|
4
|
-
import { CLOCK_PROGRAM_ID, STAKE_PROGRAM_ID } from './unstakingPool';
|
|
5
|
-
import BN from 'bn.js';
|
|
6
|
-
import assert from 'assert';
|
|
7
|
-
import { enums, Infer, nullable, number, type } from 'superstruct';
|
|
8
|
-
|
|
9
|
-
export const TRANSIENT_STAKE_SEED_PREFIX = Buffer.from('transient');
|
|
10
|
-
export const STAKE_ACCOUNT_RENT_EXEMPTION = 2_282_880;
|
|
11
|
-
const MINIMUM_ACTIVE_STAKE = 1_000_000;
|
|
12
|
-
// This represents the minimum each validator stake account must have and cannot be withdrawn
|
|
13
|
-
const TRANSIENT_STAKE_ACCOUNT_RENT_EXEMPTION = STAKE_ACCOUNT_RENT_EXEMPTION + MINIMUM_ACTIVE_STAKE;
|
|
14
|
-
|
|
15
|
-
export async function getStandardPoolState(address: PublicKey, connection: Connection): Promise<StakePool> {
|
|
16
|
-
const accountInfo = await connection.getAccountInfo(address);
|
|
17
|
-
if (!accountInfo) {
|
|
18
|
-
throw new Error(`Cannot fetch standard stake pool account ${address.toString()}`);
|
|
19
|
-
}
|
|
20
|
-
return StakePoolLayout.decode(accountInfo.data);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export async function getValidatorList(address: PublicKey, connection: Connection): Promise<ValidatorList> {
|
|
24
|
-
const accountInfo = await connection.getAccountInfo(address);
|
|
25
|
-
if (!accountInfo) {
|
|
26
|
-
throw new Error(`Cannot fetch standard stake pool account ${address.toString()}`);
|
|
27
|
-
}
|
|
28
|
-
return ValidatorListLayout.decode(accountInfo.data);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export async function maybeGetStakedPoolByMint(
|
|
32
|
-
connection: Connection,
|
|
33
|
-
mint: PublicKey
|
|
34
|
-
): Promise<[StakePool, PublicKey] | undefined> {
|
|
35
|
-
const results = await connection.getProgramAccounts(STAKE_POOL_PROGRAM_ID, {
|
|
36
|
-
filters: [{ memcmp: { offset: 162, bytes: mint.toBase58() } }],
|
|
37
|
-
});
|
|
38
|
-
// There should be only 1 stake pool for mint
|
|
39
|
-
if (results.length === 0) {
|
|
40
|
-
return undefined;
|
|
41
|
-
}
|
|
42
|
-
if (results.length === 1) {
|
|
43
|
-
return [StakePoolLayout.decode(results[0].account.data), results[0].pubkey];
|
|
44
|
-
}
|
|
45
|
-
throw new Error(`Got ${results.length} stake pools for mint ${mint.toString()} and not sure which one is correct.`);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export async function getStandardPoolMintRemainingAccounts(
|
|
49
|
-
connection: Connection,
|
|
50
|
-
stakedSolPool: StakePool,
|
|
51
|
-
stakedSolPoolPk: PublicKey,
|
|
52
|
-
stakedSolToDeposit: BN
|
|
53
|
-
): Promise<[Array<AccountMeta>, Array<Keypair>]> {
|
|
54
|
-
const withdrawAuthority = await findWithdrawAuthorityProgramAddress(STAKE_POOL_PROGRAM_ID, stakedSolPoolPk);
|
|
55
|
-
const remainingAccounts = [
|
|
56
|
-
{ pubkey: stakedSolPoolPk, isSigner: false, isWritable: true },
|
|
57
|
-
{ pubkey: stakedSolPool.validatorList, isSigner: false, isWritable: true },
|
|
58
|
-
{ pubkey: withdrawAuthority, isSigner: false, isWritable: true },
|
|
59
|
-
{ pubkey: stakedSolPool.managerFeeAccount, isSigner: false, isWritable: true },
|
|
60
|
-
{ pubkey: CLOCK_PROGRAM_ID, isSigner: false, isWritable: false },
|
|
61
|
-
{ pubkey: STAKE_PROGRAM_ID, isSigner: false, isWritable: false },
|
|
62
|
-
{ pubkey: STAKE_POOL_PROGRAM_ID, isSigner: false, isWritable: false },
|
|
63
|
-
];
|
|
64
|
-
const withdrawCandidates = await getWithdrawCandidates(
|
|
65
|
-
connection,
|
|
66
|
-
stakedSolPool,
|
|
67
|
-
stakedSolPoolPk,
|
|
68
|
-
stakedSolToDeposit
|
|
69
|
-
);
|
|
70
|
-
// Each withdraw candidate should also create a new keypair for the stake account
|
|
71
|
-
const withdrawCandidatesTo: Keypair[] = [];
|
|
72
|
-
for (const withdrawCandidateFrom of withdrawCandidates) {
|
|
73
|
-
remainingAccounts.push({ pubkey: withdrawCandidateFrom, isSigner: false, isWritable: true });
|
|
74
|
-
const withdrawCandidateTo = Keypair.generate();
|
|
75
|
-
remainingAccounts.push({ pubkey: withdrawCandidateTo.publicKey, isSigner: true, isWritable: true });
|
|
76
|
-
withdrawCandidatesTo.push(withdrawCandidateTo);
|
|
77
|
-
}
|
|
78
|
-
return [remainingAccounts, withdrawCandidatesTo];
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
async function getAllWithdrawCandidatesSorted(
|
|
82
|
-
connection: Connection,
|
|
83
|
-
stakedSolPool: StakePool,
|
|
84
|
-
stakedSolPoolPk: PublicKey
|
|
85
|
-
): Promise<Array<{ isPreferred: boolean; balance: number; pk: PublicKey }>> {
|
|
86
|
-
const activeValidators: { isPreferred: boolean; balance: number; pk: PublicKey }[] = [];
|
|
87
|
-
const transientValidators: { isPreferred: boolean; balance: number; pk: PublicKey }[] = [];
|
|
88
|
-
const validatorList = await getValidatorList(stakedSolPool.validatorList, connection);
|
|
89
|
-
const accountsToFetch: PublicKey[] = [];
|
|
90
|
-
// Add all accounts to be fetched to an array so that we can use getMultipleAccounts
|
|
91
|
-
for (const validator of validatorList.validators) {
|
|
92
|
-
const stakeAccount = await findStakeProgramAddress(
|
|
93
|
-
STAKE_POOL_PROGRAM_ID,
|
|
94
|
-
validator.voteAccountAddress,
|
|
95
|
-
stakedSolPoolPk,
|
|
96
|
-
validator.transientSeedSuffixStart.toNumber()
|
|
97
|
-
);
|
|
98
|
-
const transientAccount = await findTransientStakeProgramAddress(
|
|
99
|
-
STAKE_POOL_PROGRAM_ID,
|
|
100
|
-
validator.voteAccountAddress,
|
|
101
|
-
stakedSolPoolPk,
|
|
102
|
-
validator.transientSeedSuffixEnd
|
|
103
|
-
);
|
|
104
|
-
accountsToFetch.push(stakeAccount);
|
|
105
|
-
accountsToFetch.push(transientAccount);
|
|
106
|
-
}
|
|
107
|
-
let accountsBalances: Array<number> = [];
|
|
108
|
-
// TODO: if this is still too slow we can also start all getMultipleAccounts in parallel and do Promise.all
|
|
109
|
-
for (let i = 0; i < accountsToFetch.length; i += 100) {
|
|
110
|
-
const accountInfos = await connection.getMultipleAccountsInfo(accountsToFetch.slice(i, i + 100));
|
|
111
|
-
accountsBalances = accountsBalances.concat(
|
|
112
|
-
accountInfos.map((accountInfo) => (accountInfo ? accountInfo.lamports : 0))
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
assert(accountsBalances.length === accountsToFetch.length);
|
|
116
|
-
let i = 0;
|
|
117
|
-
for (const validator of validatorList.validators) {
|
|
118
|
-
const isPreferred = stakedSolPool.preferredWithdrawValidatorVoteAddress
|
|
119
|
-
? validator.voteAccountAddress.equals(stakedSolPool.preferredWithdrawValidatorVoteAddress)
|
|
120
|
-
: false;
|
|
121
|
-
const stakeAccount = accountsToFetch[i];
|
|
122
|
-
const stakeAccountBalance = accountsBalances[i] - TRANSIENT_STAKE_ACCOUNT_RENT_EXEMPTION;
|
|
123
|
-
if (stakeAccountBalance > 0) {
|
|
124
|
-
activeValidators.push({ isPreferred, balance: stakeAccountBalance, pk: stakeAccount });
|
|
125
|
-
}
|
|
126
|
-
const transientAccount = accountsToFetch[i + 1];
|
|
127
|
-
const transientAccountBalance = accountsBalances[i + 1] - TRANSIENT_STAKE_ACCOUNT_RENT_EXEMPTION;
|
|
128
|
-
if (transientAccountBalance > 0) {
|
|
129
|
-
transientValidators.push({ isPreferred, balance: transientAccountBalance, pk: transientAccount });
|
|
130
|
-
}
|
|
131
|
-
i += 2;
|
|
132
|
-
}
|
|
133
|
-
// Sorting descending based on balance, but preferred validators should always be used first
|
|
134
|
-
activeValidators.sort((a, b) => {
|
|
135
|
-
if (a.isPreferred) {
|
|
136
|
-
return -1;
|
|
137
|
-
}
|
|
138
|
-
if (b.isPreferred) {
|
|
139
|
-
return 1;
|
|
140
|
-
}
|
|
141
|
-
return b.balance - a.balance;
|
|
142
|
-
});
|
|
143
|
-
transientValidators.sort((a, b) => {
|
|
144
|
-
if (a.isPreferred) {
|
|
145
|
-
return -1;
|
|
146
|
-
}
|
|
147
|
-
if (b.isPreferred) {
|
|
148
|
-
return 1;
|
|
149
|
-
}
|
|
150
|
-
return b.balance - a.balance;
|
|
151
|
-
});
|
|
152
|
-
const allCandidates = activeValidators.concat(transientValidators);
|
|
153
|
-
|
|
154
|
-
// Add reserve stake account at the end as that should be used only if no validators have enough stake
|
|
155
|
-
const reserveStakeBalance = (await connection.getBalance(stakedSolPool.reserveStake)) - STAKE_ACCOUNT_RENT_EXEMPTION;
|
|
156
|
-
if (reserveStakeBalance > 0) {
|
|
157
|
-
allCandidates.push({
|
|
158
|
-
isPreferred: false,
|
|
159
|
-
balance: reserveStakeBalance,
|
|
160
|
-
pk: stakedSolPool.reserveStake,
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
return allCandidates;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export async function getWithdrawCandidates(
|
|
167
|
-
connection: Connection,
|
|
168
|
-
stakedSolPool: StakePool,
|
|
169
|
-
stakedSolPoolPk: PublicKey,
|
|
170
|
-
stakedSolToDeposit: BN
|
|
171
|
-
): Promise<Array<PublicKey>> {
|
|
172
|
-
const allCandidates = await getAllWithdrawCandidatesSorted(connection, stakedSolPool, stakedSolPoolPk);
|
|
173
|
-
|
|
174
|
-
let stakedSolRemaining = stakedSolToDeposit;
|
|
175
|
-
let solToWithdraw = new BN(0);
|
|
176
|
-
const withdrawCandidates: Array<PublicKey> = [];
|
|
177
|
-
|
|
178
|
-
// Try to withdraw all of the SOL from validators' active/transient accounts
|
|
179
|
-
for (const candidate of allCandidates) {
|
|
180
|
-
if (stakedSolRemaining.isZero()) {
|
|
181
|
-
break;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// See how much the validator balance is worth in staked sol
|
|
185
|
-
// but limit to amount of needed stake sol
|
|
186
|
-
let stakedSolAmountToWithdraw = BN.min(
|
|
187
|
-
stakedSolRemaining,
|
|
188
|
-
solToStakePoolTokensWithInverseFee(stakedSolPool, new BN(candidate.balance))
|
|
189
|
-
);
|
|
190
|
-
// Convert it back to staked sol so we get the real amount
|
|
191
|
-
let actualSolAmount = calcLamportsWithdrawAmount(stakedSolPool, stakedSolAmountToWithdraw);
|
|
192
|
-
|
|
193
|
-
const remainingSolAmount = calcLamportsWithdrawAmount(
|
|
194
|
-
stakedSolPool,
|
|
195
|
-
stakedSolRemaining.sub(stakedSolAmountToWithdraw)
|
|
196
|
-
);
|
|
197
|
-
|
|
198
|
-
// If the current validator uses up all of the remaining staked sol except some minimum that we need
|
|
199
|
-
// in order to split_stake, then leave at least the minimum required to be consumed by another validator
|
|
200
|
-
if (!remainingSolAmount.isZero() && remainingSolAmount < new BN(MINIMUM_ACTIVE_STAKE)) {
|
|
201
|
-
stakedSolAmountToWithdraw = stakedSolAmountToWithdraw.sub(
|
|
202
|
-
solToStakePoolTokensWithInverseFee(stakedSolPool, new BN(MINIMUM_ACTIVE_STAKE))
|
|
203
|
-
);
|
|
204
|
-
actualSolAmount = calcLamportsWithdrawAmount(stakedSolPool, stakedSolAmountToWithdraw);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (actualSolAmount < new BN(MINIMUM_ACTIVE_STAKE) && !candidate.pk.equals(stakedSolPool.reserveStake)) {
|
|
208
|
-
// Skip if the amount to withdraw is less than the minimum required for a valid stake
|
|
209
|
-
continue;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// Update stake_pool so simulation stays true to what happens on chain
|
|
213
|
-
stakedSolRemaining = stakedSolRemaining.sub(stakedSolAmountToWithdraw);
|
|
214
|
-
solToWithdraw = solToWithdraw.add(actualSolAmount);
|
|
215
|
-
stakedSolPool.totalLamports = stakedSolPool.totalLamports.sub(actualSolAmount);
|
|
216
|
-
stakedSolPool.poolTokenSupply = stakedSolPool.poolTokenSupply.sub(
|
|
217
|
-
stakePoolTokensMinusFee(stakedSolPool, stakedSolAmountToWithdraw)
|
|
218
|
-
);
|
|
219
|
-
withdrawCandidates.push(candidate.pk);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
return withdrawCandidates;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
function calcPoolTokensStakeWithdrawalFee(stakedSolPool: StakePool, stakedSolAmountToWithdraw: BN): BN {
|
|
226
|
-
const denominator = stakedSolPool.stakeWithdrawalFee.denominator;
|
|
227
|
-
if (denominator.isZero()) {
|
|
228
|
-
return new BN(0);
|
|
229
|
-
}
|
|
230
|
-
const numerator = stakedSolAmountToWithdraw.mul(stakedSolPool.stakeWithdrawalFee.numerator);
|
|
231
|
-
const poolTokens = numerator.add(denominator).sub(new BN(1)).div(denominator);
|
|
232
|
-
return poolTokens;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
function stakePoolTokensMinusFee(stakedSolPool: StakePool, stakedSolAmountToWithdraw: BN): BN {
|
|
236
|
-
const stakedSolFee = calcPoolTokensStakeWithdrawalFee(stakedSolPool, stakedSolAmountToWithdraw);
|
|
237
|
-
return stakedSolAmountToWithdraw.sub(stakedSolFee);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
function solToStakePoolTokensWithInverseFee(stakedSolPool: StakePool, sol: BN): BN {
|
|
241
|
-
let poolTokens = calcPoolTokensForDeposit(stakedSolPool, sol);
|
|
242
|
-
if (!stakedSolPool.stakeWithdrawalFee.numerator.isZero()) {
|
|
243
|
-
const numerator = poolTokens.mul(stakedSolPool.stakeWithdrawalFee.denominator);
|
|
244
|
-
const denominator = stakedSolPool.stakeWithdrawalFee.denominator.sub(stakedSolPool.stakeWithdrawalFee.numerator);
|
|
245
|
-
if (denominator.isZero()) {
|
|
246
|
-
// If the pool has 100% fee for some reason just fail it, we cannot compute the inverse
|
|
247
|
-
throw new Error('Pool fee cannot be 100%');
|
|
248
|
-
}
|
|
249
|
-
poolTokens = numerator.div(denominator);
|
|
250
|
-
}
|
|
251
|
-
return poolTokens;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// Below functions/types are not exported from spl-stake-pool
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Generates the withdraw authority program address for the stake pool
|
|
258
|
-
*/
|
|
259
|
-
export async function findWithdrawAuthorityProgramAddress(programId: PublicKey, stakePoolAddress: PublicKey) {
|
|
260
|
-
const [publicKey] = await PublicKey.findProgramAddress(
|
|
261
|
-
[stakePoolAddress.toBuffer(), Buffer.from('withdraw')],
|
|
262
|
-
programId
|
|
263
|
-
);
|
|
264
|
-
return publicKey;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export async function findStakeProgramAddress(
|
|
268
|
-
programId: PublicKey,
|
|
269
|
-
voteAccountAddress: PublicKey,
|
|
270
|
-
stakedSolPoolPk: PublicKey,
|
|
271
|
-
seed: number
|
|
272
|
-
): Promise<PublicKey> {
|
|
273
|
-
const [publicKey] = await PublicKey.findProgramAddress(
|
|
274
|
-
[
|
|
275
|
-
voteAccountAddress.toBuffer(),
|
|
276
|
-
stakedSolPoolPk.toBuffer(),
|
|
277
|
-
seed ? new BN(seed).toArrayLike(Buffer, 'le', 4) : Buffer.alloc(0),
|
|
278
|
-
],
|
|
279
|
-
programId
|
|
280
|
-
);
|
|
281
|
-
return publicKey;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
export async function findTransientStakeProgramAddress(
|
|
285
|
-
programId: PublicKey,
|
|
286
|
-
voteAccountAddress: PublicKey,
|
|
287
|
-
stakePoolAddress: PublicKey,
|
|
288
|
-
seed: BN
|
|
289
|
-
) {
|
|
290
|
-
const [publicKey] = await PublicKey.findProgramAddress(
|
|
291
|
-
[
|
|
292
|
-
TRANSIENT_STAKE_SEED_PREFIX,
|
|
293
|
-
voteAccountAddress.toBuffer(),
|
|
294
|
-
stakePoolAddress.toBuffer(),
|
|
295
|
-
seed.toArrayLike(Buffer, 'le', 8),
|
|
296
|
-
],
|
|
297
|
-
programId
|
|
298
|
-
);
|
|
299
|
-
return publicKey;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
function calcPoolTokensForDeposit(stakePool: StakePool, stakeLamports: BN): BN {
|
|
303
|
-
if (stakePool.poolTokenSupply.isZero() || stakePool.totalLamports.isZero()) {
|
|
304
|
-
return stakeLamports;
|
|
305
|
-
}
|
|
306
|
-
const numerator = stakeLamports.mul(stakePool.poolTokenSupply);
|
|
307
|
-
return numerator.div(stakePool.totalLamports);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
export function calcLamportsWithdrawAmount(stakePool: StakePool, poolTokens: BN): BN {
|
|
311
|
-
const numerator = poolTokens.mul(stakePool.totalLamports);
|
|
312
|
-
const denominator = stakePool.poolTokenSupply;
|
|
313
|
-
if (numerator.lt(denominator)) {
|
|
314
|
-
return new BN(0);
|
|
315
|
-
}
|
|
316
|
-
return numerator.div(denominator);
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
export declare const BigNumFromString: import('superstruct').Struct<BN, null>;
|
|
320
|
-
export declare const PublicKeyFromString: import('superstruct').Struct<PublicKey, null>;
|
|
321
|
-
export const StakeAccountType = enums(['uninitialized', 'initialized', 'delegated', 'rewardsPool']);
|
|
322
|
-
|
|
323
|
-
export type StakeMeta = Infer<typeof StakeMeta>;
|
|
324
|
-
export const StakeMeta = type({
|
|
325
|
-
rentExemptReserve: BigNumFromString,
|
|
326
|
-
authorized: type({
|
|
327
|
-
staker: PublicKeyFromString,
|
|
328
|
-
withdrawer: PublicKeyFromString,
|
|
329
|
-
}),
|
|
330
|
-
lockup: type({
|
|
331
|
-
unixTimestamp: number(),
|
|
332
|
-
epoch: number(),
|
|
333
|
-
custodian: PublicKeyFromString,
|
|
334
|
-
}),
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
type StakeAccountInfo = Infer<typeof StakeAccountInfo>;
|
|
338
|
-
const StakeAccountInfo = type({
|
|
339
|
-
meta: StakeMeta,
|
|
340
|
-
stake: nullable(
|
|
341
|
-
type({
|
|
342
|
-
delegation: type({
|
|
343
|
-
voter: PublicKeyFromString,
|
|
344
|
-
stake: BigNumFromString,
|
|
345
|
-
activationEpoch: BigNumFromString,
|
|
346
|
-
deactivationEpoch: BigNumFromString,
|
|
347
|
-
warmupCooldownRate: number(),
|
|
348
|
-
}),
|
|
349
|
-
creditsObserved: number(),
|
|
350
|
-
})
|
|
351
|
-
),
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
export type StakeAccount = Infer<typeof StakeAccount>;
|
|
355
|
-
export const StakeAccount = type({
|
|
356
|
-
type: StakeAccountType,
|
|
357
|
-
info: StakeAccountInfo,
|
|
358
|
-
});
|