@voltr/vault-sdk 1.0.2 → 1.0.3
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/client.d.ts +6 -2
- package/dist/client.js +44 -25
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -179,7 +179,7 @@ export declare class VoltrClient extends AccountUtils {
|
|
|
179
179
|
* ```
|
|
180
180
|
*/
|
|
181
181
|
createInitializeVaultIx(vaultParams: VaultParams, { vault, vaultAssetMint, admin, manager, payer, }: {
|
|
182
|
-
vault:
|
|
182
|
+
vault: PublicKey;
|
|
183
183
|
vaultAssetMint: PublicKey;
|
|
184
184
|
admin: PublicKey;
|
|
185
185
|
manager: PublicKey;
|
|
@@ -817,10 +817,14 @@ export declare class VoltrClient extends AccountUtils {
|
|
|
817
817
|
* ```
|
|
818
818
|
*/
|
|
819
819
|
getAllPendingWithdrawalsForVault(vault: PublicKey): Promise<{
|
|
820
|
-
|
|
820
|
+
amountAssetToWithdrawEffective: number;
|
|
821
|
+
amountAssetToWithdrawAtRequest: number;
|
|
822
|
+
amountAssetToWithdrawAtPresent: number;
|
|
823
|
+
amountLpEscrowed: number;
|
|
821
824
|
withdrawableFromTs: number;
|
|
822
825
|
}[]>;
|
|
823
826
|
calculateLockedProfit(lastUpdatedLockedProfit: BN, lockedProfitDegradationDuration: BN, currentTime: BN): BN;
|
|
827
|
+
calculateAssetsForWithdrawHelper(vaultTotalValue: BN, vaultLastUpdatedLockedProfit: BN, vaultLockedProfitDegradationDuration: BN, vaultAccumulatedLpAdminFees: BN, vaultAccumulatedLpManagerFees: BN, vaultAccumulatedLpProtocolFees: BN, vaultRedemptionFee: number, lpSupply: BN, lpAmount: BN): BN;
|
|
824
828
|
/**
|
|
825
829
|
* Calculates the amount of assets that would be received for a given LP token amount
|
|
826
830
|
*
|
package/dist/client.js
CHANGED
|
@@ -303,18 +303,23 @@ class VoltrClient extends AccountUtils {
|
|
|
303
303
|
* ```
|
|
304
304
|
*/
|
|
305
305
|
async createInitializeVaultIx(vaultParams, { vault, vaultAssetMint, admin, manager, payer, }) {
|
|
306
|
-
const addresses = this.findVaultAddresses(vault
|
|
306
|
+
const addresses = this.findVaultAddresses(vault);
|
|
307
307
|
const vaultAssetIdleAta = (0, spl_token_1.getAssociatedTokenAddressSync)(vaultAssetMint, addresses.vaultAssetIdleAuth, true);
|
|
308
|
+
const vaultAssetMintAccount = await this.provider.connection.getAccountInfo(vaultAssetMint);
|
|
309
|
+
const assetTokenProgram = vaultAssetMintAccount?.owner;
|
|
310
|
+
if (!assetTokenProgram) {
|
|
311
|
+
throw new Error("Vault asset mint not found");
|
|
312
|
+
}
|
|
308
313
|
return await this.vaultProgram.methods
|
|
309
314
|
.initializeVault(vaultParams.config, vaultParams.name, vaultParams.description)
|
|
310
315
|
.accounts({
|
|
311
316
|
payer,
|
|
312
317
|
admin,
|
|
313
318
|
manager,
|
|
314
|
-
vault
|
|
319
|
+
vault,
|
|
315
320
|
vaultAssetMint,
|
|
316
321
|
vaultAssetIdleAta,
|
|
317
|
-
assetTokenProgram
|
|
322
|
+
assetTokenProgram,
|
|
318
323
|
})
|
|
319
324
|
.instruction();
|
|
320
325
|
}
|
|
@@ -926,11 +931,24 @@ class VoltrClient extends AccountUtils {
|
|
|
926
931
|
*/
|
|
927
932
|
async getAllPendingWithdrawalsForVault(vault) {
|
|
928
933
|
const requestWithdrawVaultReceipts = await this.fetchAllRequestWithdrawVaultReceiptsOfVault(vault);
|
|
934
|
+
const vaultAccount = await this.fetchVaultAccount(vault);
|
|
935
|
+
const lpMint = this.findVaultLpMint(vault);
|
|
936
|
+
const lp = await (0, spl_token_1.getMint)(this.conn, lpMint);
|
|
929
937
|
return requestWithdrawVaultReceipts.map((receipt) => {
|
|
930
938
|
const amountAssetToWithdrawDecimal = (0, decimals_1.convertDecimalBitsToDecimal)(receipt.account.amountAssetToWithdrawDecimalBits);
|
|
931
|
-
const
|
|
939
|
+
const amountAssetToWithdrawAtRequest = amountAssetToWithdrawDecimal.toNumber();
|
|
940
|
+
const amountLpEscrowed = receipt.account.amountLpEscrowed;
|
|
941
|
+
const amountAssetToWithdrawAtPresent = this.calculateAssetsForWithdrawHelper(vaultAccount.asset.totalValue, vaultAccount.lockedProfitState.lastUpdatedLockedProfit, vaultAccount.vaultConfiguration.lockedProfitDegradationDuration, vaultAccount.feeState.accumulatedLpAdminFees, vaultAccount.feeState.accumulatedLpManagerFees, vaultAccount.feeState.accumulatedLpProtocolFees, vaultAccount.feeConfiguration.redemptionFee, new bn_js_1.default(lp.supply.toString()), amountLpEscrowed).toNumber();
|
|
942
|
+
// Get the capped amount of asset to withdraw
|
|
943
|
+
// If the latest amount of asset to withdraw is greater than the initial amount of asset to withdraw,
|
|
944
|
+
// then return the initial amount of asset to withdraw
|
|
945
|
+
// Otherwise, return the latest amount of asset to withdraw
|
|
946
|
+
const amountAssetToWithdrawEffective = Math.min(amountAssetToWithdrawAtPresent, amountAssetToWithdrawAtRequest);
|
|
932
947
|
return {
|
|
933
|
-
|
|
948
|
+
amountAssetToWithdrawEffective,
|
|
949
|
+
amountAssetToWithdrawAtRequest,
|
|
950
|
+
amountAssetToWithdrawAtPresent,
|
|
951
|
+
amountLpEscrowed: amountLpEscrowed.toNumber(),
|
|
934
952
|
withdrawableFromTs: receipt.account.withdrawableFromTs.toNumber(),
|
|
935
953
|
};
|
|
936
954
|
});
|
|
@@ -947,6 +965,26 @@ class VoltrClient extends AccountUtils {
|
|
|
947
965
|
else
|
|
948
966
|
return lockedProfit;
|
|
949
967
|
}
|
|
968
|
+
calculateAssetsForWithdrawHelper(vaultTotalValue, vaultLastUpdatedLockedProfit, vaultLockedProfitDegradationDuration, vaultAccumulatedLpAdminFees, vaultAccumulatedLpManagerFees, vaultAccumulatedLpProtocolFees, vaultRedemptionFee, lpSupply, lpAmount) {
|
|
969
|
+
if (lpSupply <= new bn_js_1.default(0))
|
|
970
|
+
throw new Error("Invalid LP supply");
|
|
971
|
+
if (vaultTotalValue <= new bn_js_1.default(0))
|
|
972
|
+
throw new Error("Invalid total assets");
|
|
973
|
+
const lockedProfit = this.calculateLockedProfit(vaultLastUpdatedLockedProfit, vaultLockedProfitDegradationDuration, new bn_js_1.default(Date.now() / 1000));
|
|
974
|
+
const totalUnlockedValue = vaultTotalValue.sub(lockedProfit);
|
|
975
|
+
const unharvestedFeesLp = vaultAccumulatedLpAdminFees
|
|
976
|
+
.add(vaultAccumulatedLpManagerFees)
|
|
977
|
+
.add(vaultAccumulatedLpProtocolFees);
|
|
978
|
+
const lpSupplyInclFees = lpSupply.add(unharvestedFeesLp);
|
|
979
|
+
// asset_to_redeem_pre_fee = amount * (total_asset_pre_withdraw / total_lp_supply_pre_withdraw)
|
|
980
|
+
// asset_to_redeem_post_fee = asset_to_redeem_pre_fee * (10000 - redemption_fee_bps) / 10000
|
|
981
|
+
const assetToRedeemNumerator = lpAmount
|
|
982
|
+
.mul(totalUnlockedValue)
|
|
983
|
+
.mul(new bn_js_1.default(10000 - vaultRedemptionFee));
|
|
984
|
+
const assetToRedeemDenominator = lpSupplyInclFees.mul(new bn_js_1.default(10000));
|
|
985
|
+
const amount = assetToRedeemNumerator.div(assetToRedeemDenominator);
|
|
986
|
+
return amount;
|
|
987
|
+
}
|
|
950
988
|
/**
|
|
951
989
|
* Calculates the amount of assets that would be received for a given LP token amount
|
|
952
990
|
*
|
|
@@ -968,28 +1006,9 @@ class VoltrClient extends AccountUtils {
|
|
|
968
1006
|
async calculateAssetsForWithdraw(vaultPk, lpAmount) {
|
|
969
1007
|
try {
|
|
970
1008
|
const vault = await this.fetchVaultAccount(vaultPk);
|
|
971
|
-
const totalValue = vault.asset.totalValue;
|
|
972
|
-
const lockedProfit = this.calculateLockedProfit(vault.lockedProfitState.lastUpdatedLockedProfit, vault.vaultConfiguration.lockedProfitDegradationDuration, new bn_js_1.default(Date.now() / 1000));
|
|
973
|
-
const totalUnlockedValue = totalValue.sub(lockedProfit);
|
|
974
1009
|
const lpMint = this.findVaultLpMint(vaultPk);
|
|
975
1010
|
const lp = await (0, spl_token_1.getMint)(this.conn, lpMint);
|
|
976
|
-
const
|
|
977
|
-
// Validate inputs
|
|
978
|
-
if (lpSupply <= new bn_js_1.default(0))
|
|
979
|
-
throw new Error("Invalid LP supply");
|
|
980
|
-
if (totalValue <= new bn_js_1.default(0))
|
|
981
|
-
throw new Error("Invalid total assets");
|
|
982
|
-
const unharvestedFeesLp = vault.feeState.accumulatedLpAdminFees
|
|
983
|
-
.add(vault.feeState.accumulatedLpManagerFees)
|
|
984
|
-
.add(vault.feeState.accumulatedLpProtocolFees);
|
|
985
|
-
const lpSupplyInclFees = lpSupply.add(unharvestedFeesLp);
|
|
986
|
-
// asset_to_redeem_pre_fee = amount * (total_asset_pre_withdraw / total_lp_supply_pre_withdraw)
|
|
987
|
-
// asset_to_redeem_post_fee = asset_to_redeem_pre_fee * (10000 - redemption_fee_bps) / 10000
|
|
988
|
-
const assetToRedeemNumerator = lpAmount
|
|
989
|
-
.mul(totalUnlockedValue)
|
|
990
|
-
.mul(new bn_js_1.default(10000 - vault.feeConfiguration.redemptionFee));
|
|
991
|
-
const assetToRedeemDenominator = lpSupplyInclFees.mul(new bn_js_1.default(10000));
|
|
992
|
-
const amount = assetToRedeemNumerator.div(assetToRedeemDenominator);
|
|
1011
|
+
const amount = this.calculateAssetsForWithdrawHelper(vault.asset.totalValue, vault.lockedProfitState.lastUpdatedLockedProfit, vault.vaultConfiguration.lockedProfitDegradationDuration, vault.feeState.accumulatedLpAdminFees, vault.feeState.accumulatedLpManagerFees, vault.feeState.accumulatedLpProtocolFees, vault.feeConfiguration.redemptionFee, new bn_js_1.default(lp.supply.toString()), lpAmount);
|
|
993
1012
|
return amount;
|
|
994
1013
|
}
|
|
995
1014
|
catch (e) {
|