@voltr/vault-sdk 1.0.4 → 1.0.5
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 +44 -0
- package/dist/client.js +60 -22
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -833,6 +833,25 @@ export declare class VoltrClient extends AccountUtils {
|
|
|
833
833
|
lastUpdatedEpoch: BN;
|
|
834
834
|
reserved: number[];
|
|
835
835
|
}>;
|
|
836
|
+
/**
|
|
837
|
+
* Fetches a request withdraw vault receipt account's data
|
|
838
|
+
* @param requestWithdrawVaultReceipt - Public key of the request withdraw vault receipt account
|
|
839
|
+
* @returns Promise resolving to the request withdraw vault receipt account data
|
|
840
|
+
*
|
|
841
|
+
* @example
|
|
842
|
+
* ```typescript
|
|
843
|
+
* const requestWithdrawVaultReceiptAccount = await client.fetchRequestWithdrawVaultReceiptAccount(requestWithdrawVaultReceiptPubkey);
|
|
844
|
+
* ```
|
|
845
|
+
*/
|
|
846
|
+
fetchRequestWithdrawVaultReceiptAccount(requestWithdrawVaultReceipt: PublicKey): Promise<{
|
|
847
|
+
vault: PublicKey;
|
|
848
|
+
user: PublicKey;
|
|
849
|
+
amountLpEscrowed: BN;
|
|
850
|
+
amountAssetToWithdrawDecimalBits: BN;
|
|
851
|
+
withdrawableFromTs: BN;
|
|
852
|
+
bump: number;
|
|
853
|
+
version: number;
|
|
854
|
+
}>;
|
|
836
855
|
/**
|
|
837
856
|
* Fetches the accumulated admin fees for a vault
|
|
838
857
|
* @param vault - Public key of the vault
|
|
@@ -855,6 +874,30 @@ export declare class VoltrClient extends AccountUtils {
|
|
|
855
874
|
* ```
|
|
856
875
|
*/
|
|
857
876
|
getAccumulatedManagerFeesForVault(vault: PublicKey): Promise<BN>;
|
|
877
|
+
/**
|
|
878
|
+
* Processes a withdrawal receipt into a standardized withdrawal info object
|
|
879
|
+
* @private
|
|
880
|
+
*/
|
|
881
|
+
private processWithdrawalReceipt;
|
|
882
|
+
/**
|
|
883
|
+
* Fetches the pending withdrawal for a user
|
|
884
|
+
* @param vault - Public key of the vault
|
|
885
|
+
* @param user - Public key of the user
|
|
886
|
+
* @returns Promise resolving to the pending withdrawal
|
|
887
|
+
*
|
|
888
|
+
* @example
|
|
889
|
+
* ```typescript
|
|
890
|
+
* const pendingWithdrawal = await client.getPendingWithdrawalForUser(vaultPubkey, userPubkey);
|
|
891
|
+
* ```
|
|
892
|
+
*/
|
|
893
|
+
getPendingWithdrawalForUser(vault: PublicKey, user: PublicKey): Promise<{
|
|
894
|
+
user: PublicKey;
|
|
895
|
+
amountAssetToWithdrawEffective: number;
|
|
896
|
+
amountAssetToWithdrawAtRequest: number;
|
|
897
|
+
amountAssetToWithdrawAtPresent: number;
|
|
898
|
+
amountLpEscrowed: number;
|
|
899
|
+
withdrawableFromTs: number;
|
|
900
|
+
}>;
|
|
858
901
|
/**
|
|
859
902
|
* Fetches all pending withdrawals for a vault
|
|
860
903
|
* @param vault - Public key of the vault
|
|
@@ -866,6 +909,7 @@ export declare class VoltrClient extends AccountUtils {
|
|
|
866
909
|
* ```
|
|
867
910
|
*/
|
|
868
911
|
getAllPendingWithdrawalsForVault(vault: PublicKey): Promise<{
|
|
912
|
+
user: PublicKey;
|
|
869
913
|
amountAssetToWithdrawEffective: number;
|
|
870
914
|
amountAssetToWithdrawAtRequest: number;
|
|
871
915
|
amountAssetToWithdrawAtPresent: number;
|
package/dist/client.js
CHANGED
|
@@ -952,6 +952,19 @@ class VoltrClient extends AccountUtils {
|
|
|
952
952
|
async fetchAdaptorAddReceiptAccount(adaptorAddReceipt) {
|
|
953
953
|
return await this.vaultProgram.account.adaptorAddReceipt.fetch(adaptorAddReceipt, "confirmed");
|
|
954
954
|
}
|
|
955
|
+
/**
|
|
956
|
+
* Fetches a request withdraw vault receipt account's data
|
|
957
|
+
* @param requestWithdrawVaultReceipt - Public key of the request withdraw vault receipt account
|
|
958
|
+
* @returns Promise resolving to the request withdraw vault receipt account data
|
|
959
|
+
*
|
|
960
|
+
* @example
|
|
961
|
+
* ```typescript
|
|
962
|
+
* const requestWithdrawVaultReceiptAccount = await client.fetchRequestWithdrawVaultReceiptAccount(requestWithdrawVaultReceiptPubkey);
|
|
963
|
+
* ```
|
|
964
|
+
*/
|
|
965
|
+
async fetchRequestWithdrawVaultReceiptAccount(requestWithdrawVaultReceipt) {
|
|
966
|
+
return await this.vaultProgram.account.requestWithdrawVaultReceipt.fetch(requestWithdrawVaultReceipt, "confirmed");
|
|
967
|
+
}
|
|
955
968
|
// --------------------------------------- Helpers
|
|
956
969
|
/**
|
|
957
970
|
* Fetches the accumulated admin fees for a vault
|
|
@@ -981,6 +994,46 @@ class VoltrClient extends AccountUtils {
|
|
|
981
994
|
const vaultAccount = await this.fetchVaultAccount(vault);
|
|
982
995
|
return vaultAccount.feeState.accumulatedLpManagerFees;
|
|
983
996
|
}
|
|
997
|
+
/**
|
|
998
|
+
* Processes a withdrawal receipt into a standardized withdrawal info object
|
|
999
|
+
* @private
|
|
1000
|
+
*/
|
|
1001
|
+
async processWithdrawalReceipt(receipt, vaultAccount, lpSupply) {
|
|
1002
|
+
const amountAssetToWithdrawDecimal = (0, decimals_1.convertDecimalBitsToDecimal)(receipt.account.amountAssetToWithdrawDecimalBits);
|
|
1003
|
+
const amountAssetToWithdrawAtRequest = amountAssetToWithdrawDecimal.toNumber();
|
|
1004
|
+
const amountLpEscrowed = receipt.account.amountLpEscrowed;
|
|
1005
|
+
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, lpSupply, amountLpEscrowed).toNumber();
|
|
1006
|
+
// Cap the withdrawal amount to the initial request amount
|
|
1007
|
+
const amountAssetToWithdrawEffective = Math.min(amountAssetToWithdrawAtPresent, amountAssetToWithdrawAtRequest);
|
|
1008
|
+
return {
|
|
1009
|
+
user: receipt.account.user,
|
|
1010
|
+
amountAssetToWithdrawEffective,
|
|
1011
|
+
amountAssetToWithdrawAtRequest,
|
|
1012
|
+
amountAssetToWithdrawAtPresent,
|
|
1013
|
+
amountLpEscrowed: amountLpEscrowed.toNumber(),
|
|
1014
|
+
withdrawableFromTs: receipt.account.withdrawableFromTs.toNumber(),
|
|
1015
|
+
};
|
|
1016
|
+
}
|
|
1017
|
+
/**
|
|
1018
|
+
* Fetches the pending withdrawal for a user
|
|
1019
|
+
* @param vault - Public key of the vault
|
|
1020
|
+
* @param user - Public key of the user
|
|
1021
|
+
* @returns Promise resolving to the pending withdrawal
|
|
1022
|
+
*
|
|
1023
|
+
* @example
|
|
1024
|
+
* ```typescript
|
|
1025
|
+
* const pendingWithdrawal = await client.getPendingWithdrawalForUser(vaultPubkey, userPubkey);
|
|
1026
|
+
* ```
|
|
1027
|
+
*/
|
|
1028
|
+
async getPendingWithdrawalForUser(vault, user) {
|
|
1029
|
+
const [vaultAccount, lp] = await Promise.all([
|
|
1030
|
+
this.fetchVaultAccount(vault),
|
|
1031
|
+
(0, spl_token_1.getMint)(this.conn, this.findVaultLpMint(vault)),
|
|
1032
|
+
]);
|
|
1033
|
+
const requestWithdrawVaultReceiptAddress = this.findRequestWithdrawVaultReceipt(vault, user);
|
|
1034
|
+
const receipt = await this.fetchRequestWithdrawVaultReceiptAccount(requestWithdrawVaultReceiptAddress);
|
|
1035
|
+
return this.processWithdrawalReceipt({ account: receipt }, vaultAccount, new bn_js_1.default(lp.supply.toString()));
|
|
1036
|
+
}
|
|
984
1037
|
/**
|
|
985
1038
|
* Fetches all pending withdrawals for a vault
|
|
986
1039
|
* @param vault - Public key of the vault
|
|
@@ -992,28 +1045,13 @@ class VoltrClient extends AccountUtils {
|
|
|
992
1045
|
* ```
|
|
993
1046
|
*/
|
|
994
1047
|
async getAllPendingWithdrawalsForVault(vault) {
|
|
995
|
-
const requestWithdrawVaultReceipts = await
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
const amountLpEscrowed = receipt.account.amountLpEscrowed;
|
|
1003
|
-
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();
|
|
1004
|
-
// Get the capped amount of asset to withdraw
|
|
1005
|
-
// If the latest amount of asset to withdraw is greater than the initial amount of asset to withdraw,
|
|
1006
|
-
// then return the initial amount of asset to withdraw
|
|
1007
|
-
// Otherwise, return the latest amount of asset to withdraw
|
|
1008
|
-
const amountAssetToWithdrawEffective = Math.min(amountAssetToWithdrawAtPresent, amountAssetToWithdrawAtRequest);
|
|
1009
|
-
return {
|
|
1010
|
-
amountAssetToWithdrawEffective,
|
|
1011
|
-
amountAssetToWithdrawAtRequest,
|
|
1012
|
-
amountAssetToWithdrawAtPresent,
|
|
1013
|
-
amountLpEscrowed: amountLpEscrowed.toNumber(),
|
|
1014
|
-
withdrawableFromTs: receipt.account.withdrawableFromTs.toNumber(),
|
|
1015
|
-
};
|
|
1016
|
-
});
|
|
1048
|
+
const [requestWithdrawVaultReceipts, vaultAccount, lp] = await Promise.all([
|
|
1049
|
+
this.fetchAllRequestWithdrawVaultReceiptsOfVault(vault),
|
|
1050
|
+
this.fetchVaultAccount(vault),
|
|
1051
|
+
(0, spl_token_1.getMint)(this.conn, this.findVaultLpMint(vault)),
|
|
1052
|
+
]);
|
|
1053
|
+
const lpSupply = new bn_js_1.default(lp.supply.toString());
|
|
1054
|
+
return Promise.all(requestWithdrawVaultReceipts.map((receipt) => this.processWithdrawalReceipt(receipt, vaultAccount, lpSupply)));
|
|
1017
1055
|
}
|
|
1018
1056
|
calculateLockedProfit(lastUpdatedLockedProfit, lockedProfitDegradationDuration, currentTime) {
|
|
1019
1057
|
if (lockedProfitDegradationDuration.eq(new bn_js_1.default(0)))
|