@everstake/eversol-ts-sdk
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -1,6 +0,6 @@ | ||
import { PublicKey, Transaction, Keypair, SystemProgram, StakeProgram, } from '@solana/web3.js'; | ||
import { PublicKey, Transaction, Keypair, TransactionInstruction, SystemProgram, StakeProgram, } from '@solana/web3.js'; | ||
import { ESolConfig } from './config'; | ||
import { StakePoolProgram } from './service/stakepool-program'; | ||
import { lamportsToSol, solToLamports } from './utils'; | ||
import { getStakePoolAccount, addAssociatedTokenAccount, findWithdrawAuthorityProgramAddress, getTokenAccount, prepareWithdrawAccounts, calcLamportsWithdrawAmount, newStakeAccount, } from './service/service'; | ||
import { getStakePoolAccount, addAssociatedTokenAccount, findWithdrawAuthorityProgramAddress, getTokenAccount, prepareWithdrawAccounts, newStakeAccount, } from './service/service'; | ||
import checkDaoStakingAccounts from './service/checkDaoStakingAccounts'; | ||
@@ -252,3 +252,2 @@ import checkCommunityTokenStakingAccount from './service/checkCommunityTokenStakingAccount'; | ||
async createWithdrawSolTransaction(userAddress, eSolAmount, stakeReceiver, poolTokenAccount) { | ||
var _a, _b; | ||
const { connection } = this.config; | ||
@@ -342,49 +341,53 @@ const stakePoolAddress = this.config.eSOLStakePoolAddress; | ||
instructions.push(Token.createApproveInstruction(TOKEN_PROGRAM_ID, poolTokenAccount, userTransferAuthority.publicKey, userAddress, [], lamportsToWithdraw)); | ||
const withdrawAccount = await prepareWithdrawAccounts(connection, stakePool.account.data, stakePoolAddress, lamportsToWithdraw); | ||
if (!withdrawAccount) { | ||
throw Error(`Not available at the moment. Please try again later. Sorry for the inconvenience.`); | ||
const withdrawAccounts = await prepareWithdrawAccounts(connection, stakePool.account.data, stakePoolAddress, lamportsToWithdraw); | ||
if (withdrawAccounts.length === 0) { | ||
throw Error('Not available at the moment. Please try again later. Sorry for the inconvenience.'); | ||
} | ||
const availableSol = lamportsToSol(withdrawAccount.poolAmount); | ||
if (withdrawAccount.poolAmount < lamportsToWithdraw) { | ||
throw Error(`Currently, you can undelegate only ${availableSol} SOL within one transaction due to delayed unstake limitations. Please unstake the desired amount in few transactions. Note that you will be able to track your unstaked SOL in the “Wallet” tab as a summary of transactions!.`); | ||
let availableSol = 0; | ||
withdrawAccounts.forEach((account) => { | ||
availableSol += account.poolAmount; | ||
}); | ||
if (availableSol < lamportsToWithdraw) { | ||
throw Error(`Currently, you can undelegate only ${lamportsToSol(availableSol)} SOL within one transaction due to delayed unstake limitations. Please unstake the desired amount in few transactions. | ||
Note that you will be able to track your unstaked SOL in the “Wallet” tab as a summary of transactions!.`); | ||
} | ||
const solWithdrawAmount = Math.ceil(calcLamportsWithdrawAmount(stakePool.account.data, withdrawAccount.poolAmount)); | ||
let infoMsg = `Withdrawing ◎${solWithdrawAmount}, | ||
from stake account ${(_a = withdrawAccount.stakeAddress) === null || _a === void 0 ? void 0 : _a.toBase58()}`; | ||
if (withdrawAccount.voteAddress) { | ||
infoMsg = `${infoMsg}, delegated to ${(_b = withdrawAccount.voteAddress) === null || _b === void 0 ? void 0 : _b.toBase58()}`; | ||
} | ||
let stakeToReceive; | ||
let numberOfStakeAccounts = 1; | ||
let totalRentFreeBalances = 0; | ||
function incrementStakeAccount() { | ||
numberOfStakeAccounts++; | ||
numberOfStakeAccounts += 1; | ||
} | ||
const stakeReceiverAccountBalance = await connection.getMinimumBalanceForRentExemption(StakeProgram.space); | ||
const stakeAccountPubkey = await newStakeAccount(connection, userAddress, instructions, stakeReceiverAccountBalance, numberOfStakeAccounts, incrementStakeAccount); | ||
totalRentFreeBalances += stakeReceiverAccountBalance; | ||
stakeToReceive = stakeAccountPubkey; | ||
stakeReceiver = stakeAccountPubkey; | ||
instructions.push(StakePoolProgram.withdrawStakeWithDao({ | ||
daoCommunityTokenReceiverAccount, | ||
communityTokenStakingRewards: communityTokenStakingRewardsPubkey, | ||
ownerWallet: userAddress, | ||
communityTokenPubkey, | ||
stakePool: stakePoolAddress, | ||
validatorList: stakePool.account.data.validatorList, | ||
validatorStake: withdrawAccount.stakeAddress, | ||
destinationStake: stakeToReceive, | ||
destinationStakeAuthority: userAddress, | ||
sourceTransferAuthority: userTransferAuthority.publicKey, | ||
sourcePoolAccount: poolTokenAccount, | ||
managerFeeAccount: stakePool.account.data.managerFeeAccount, | ||
poolMint: stakePool.account.data.poolMint, | ||
poolTokens: withdrawAccount.poolAmount, | ||
withdrawAuthority, | ||
})); | ||
const deactivateTransaction = StakeProgram.deactivate({ | ||
stakePubkey: stakeToReceive, | ||
authorizedPubkey: userAddress, | ||
}); | ||
instructions.push(...deactivateTransaction.instructions); | ||
// Go through prepared accounts and withdraw/claim them | ||
// eslint-disable-next-line no-restricted-syntax | ||
for await (const withdrawAccount of withdrawAccounts) { | ||
if (poolTokenAccount) { | ||
incrementStakeAccount(); | ||
const stakeReceiverAccountBalance = await connection.getMinimumBalanceForRentExemption(StakeProgram.space); | ||
const stakeToReceive = await newStakeAccount(connection, userAddress, instructions, stakeReceiverAccountBalance, numberOfStakeAccounts, incrementStakeAccount); | ||
stakeReceiver = stakeToReceive; | ||
instructions.push(StakePoolProgram.withdrawStakeWithDao({ | ||
daoCommunityTokenReceiverAccount, | ||
communityTokenStakingRewards: communityTokenStakingRewardsPubkey, | ||
ownerWallet: userAddress, | ||
communityTokenPubkey, | ||
stakePool: stakePoolAddress, | ||
validatorList: stakePool.account.data.validatorList, | ||
validatorStake: withdrawAccount.stakeAddress, | ||
destinationStake: stakeToReceive, | ||
destinationStakeAuthority: userAddress, | ||
sourceTransferAuthority: userTransferAuthority.publicKey, | ||
sourcePoolAccount: poolTokenAccount, | ||
managerFeeAccount: stakePool.account.data.managerFeeAccount, | ||
poolMint: stakePool.account.data.poolMint, | ||
poolTokens: withdrawAccount.poolAmount, | ||
withdrawAuthority, | ||
})); | ||
const deactivateTransaction = StakeProgram.deactivate({ | ||
stakePubkey: stakeToReceive, | ||
authorizedPubkey: userAddress, | ||
}); | ||
for (const deactivateInstruction of deactivateTransaction.instructions) { | ||
const instruction = new TransactionInstruction(deactivateInstruction); | ||
instructions.push(instruction); | ||
} | ||
} | ||
} | ||
const transaction = new Transaction(); | ||
@@ -391,0 +394,0 @@ instructions.forEach((instruction) => transaction.add(instruction)); |
@@ -5,1 +5,3 @@ /// <reference types="node" /> | ||
export declare const RENT_EXEMPTION_FEE = 0.00203928; | ||
export declare const MIN_AMOUNT_TO_LEAVE_ON_VALIDATOR: number; | ||
export declare const MIN_AMOUNT_TO_WITHDRAW_FROM_VALIDATOR = 1.2; |
@@ -0,3 +1,6 @@ | ||
import { solToLamports } from '../utils'; | ||
export const TRANSIENT_STAKE_SEED_PREFIX = Buffer.from('transient'); | ||
export const TRANSACTION_FEE = 0.000005; | ||
export const RENT_EXEMPTION_FEE = 0.00203928; | ||
export const MIN_AMOUNT_TO_LEAVE_ON_VALIDATOR = solToLamports(0.2); | ||
export const MIN_AMOUNT_TO_WITHDRAW_FROM_VALIDATOR = 1.2; |
import { PublicKey, Connection, AccountInfo, TransactionInstruction } from '@solana/web3.js'; | ||
import { StakePool } from "./layouts"; | ||
import { StakePool } from './layouts'; | ||
export interface StakePoolAccount { | ||
@@ -4,0 +4,0 @@ pubkey: PublicKey; |
@@ -7,2 +7,4 @@ import { PublicKey, SystemProgram, StakeProgram, } from '@solana/web3.js'; | ||
import BN from 'bn.js'; | ||
import { lamportsToSol } from '../utils'; | ||
import { MIN_AMOUNT_TO_LEAVE_ON_VALIDATOR, MIN_AMOUNT_TO_WITHDRAW_FROM_VALIDATOR } from './constants'; | ||
const FAILED_TO_FIND_ACCOUNT = 'Failed to find account'; | ||
@@ -160,4 +162,6 @@ const INVALID_ACCOUNT_OWNER = 'Invalid account owner'; | ||
// const withdrawFrom: WithdrawAccount[] = []; | ||
let withdrawFrom; | ||
const remainingAmount = amount; | ||
const withdrawFrom = []; | ||
let remainingAmount = amount; | ||
const maxWithdrawAccounts = 2; | ||
let i = 1; | ||
// for (const type of ["preferred", "active", "transient", "reserve"]) { | ||
@@ -167,2 +171,5 @@ for (const type of ['active']) { | ||
for (const { stakeAddress, voteAddress, lamports } of filteredAccounts) { | ||
if (i > maxWithdrawAccounts) { | ||
break; | ||
} | ||
let availableForWithdrawal = Math.floor(calcPoolTokensForDeposit(stakePool, lamports)); | ||
@@ -172,2 +179,8 @@ if (!stakePool.stakeWithdrawalFee.denominator.isZero()) { | ||
} | ||
if (lamportsToSol(availableForWithdrawal) <= MIN_AMOUNT_TO_WITHDRAW_FROM_VALIDATOR) { | ||
// don't withdraw if validator has less than 1.2 SOL | ||
continue; | ||
} | ||
// leave SOL on validator for other actions | ||
availableForWithdrawal -= MIN_AMOUNT_TO_LEAVE_ON_VALIDATOR; | ||
const poolAmount = Math.min(availableForWithdrawal, remainingAmount); | ||
@@ -178,9 +191,8 @@ if (poolAmount <= 0) { | ||
// Those accounts will be withdrawn completely with `claim` instruction | ||
withdrawFrom = { stakeAddress, voteAddress, poolAmount }; | ||
// new | ||
break; | ||
// remainingAmount -= poolAmount; | ||
// if (remainingAmount == 0) { | ||
// break; | ||
// } | ||
withdrawFrom.push({ stakeAddress, voteAddress, poolAmount }); | ||
remainingAmount -= poolAmount; | ||
i += 1; | ||
if (remainingAmount === 0) { | ||
break; | ||
} | ||
} | ||
@@ -187,0 +199,0 @@ if (remainingAmount === 0) { |
{ | ||
"name": "@everstake/eversol-ts-sdk", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "Eversol ts sdk", | ||
@@ -5,0 +5,0 @@ "main": "eversol-ts-sdk/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107735
2148