@voltr/vault-sdk 1.0.4 → 1.0.6
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/README.md +4 -0
- package/dist/client.d.ts +86 -7
- package/dist/client.js +122 -35
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -265,6 +265,7 @@ pendingWithdrawals.forEach((withdrawal, index) => {
|
|
|
265
265
|
- `fetchVaultAccount(vault)`
|
|
266
266
|
- `fetchStrategyInitReceiptAccount(strategyInitReceipt)`
|
|
267
267
|
- `fetchAdaptorAddReceiptAccount(adaptorAddReceipt)`
|
|
268
|
+
- `fetchRequestWithdrawVaultReceiptAccount(requestWithdrawVaultReceipt)`
|
|
268
269
|
- `fetchAllStrategyInitReceiptAccounts()`
|
|
269
270
|
- `fetchAllStrategyInitReceiptAccountsOfVault(vault)`
|
|
270
271
|
- `fetchAllAdaptorAddReceiptAccountsOfVault(vault)`
|
|
@@ -272,7 +273,10 @@ pendingWithdrawals.forEach((withdrawal, index) => {
|
|
|
272
273
|
- `getPositionAndTotalValuesForVault(vault)`
|
|
273
274
|
- `getAccumulatedAdminFeesForVault(vault)`
|
|
274
275
|
- `getAccumulatedManagerFeesForVault(vault)`
|
|
276
|
+
- `getPendingWithdrawalForUser(vault, user)`
|
|
275
277
|
- `getAllPendingWithdrawalsForVault(vault)`
|
|
278
|
+
- `getCurrentAssetPerLpForVault(vault)`
|
|
279
|
+
- `getHighWaterMarkForVault(vault)`
|
|
276
280
|
|
|
277
281
|
#### PDA Finding
|
|
278
282
|
|
package/dist/client.d.ts
CHANGED
|
@@ -726,13 +726,6 @@ export declare class VoltrClient extends AccountUtils {
|
|
|
726
726
|
lastUpdatedEpoch: BN;
|
|
727
727
|
reserved: number[];
|
|
728
728
|
}>[]>;
|
|
729
|
-
getPositionAndTotalValuesForVault(vault: PublicKey): Promise<{
|
|
730
|
-
totalValue: number;
|
|
731
|
-
strategies: {
|
|
732
|
-
strategyId: string;
|
|
733
|
-
amount: number;
|
|
734
|
-
}[];
|
|
735
|
-
}>;
|
|
736
729
|
/**
|
|
737
730
|
* Fetches a vault account's data
|
|
738
731
|
* @param vault - Public key of the vault
|
|
@@ -833,6 +826,42 @@ export declare class VoltrClient extends AccountUtils {
|
|
|
833
826
|
lastUpdatedEpoch: BN;
|
|
834
827
|
reserved: number[];
|
|
835
828
|
}>;
|
|
829
|
+
/**
|
|
830
|
+
* Fetches a request withdraw vault receipt account's data
|
|
831
|
+
* @param requestWithdrawVaultReceipt - Public key of the request withdraw vault receipt account
|
|
832
|
+
* @returns Promise resolving to the request withdraw vault receipt account data
|
|
833
|
+
*
|
|
834
|
+
* @example
|
|
835
|
+
* ```typescript
|
|
836
|
+
* const requestWithdrawVaultReceiptAccount = await client.fetchRequestWithdrawVaultReceiptAccount(requestWithdrawVaultReceiptPubkey);
|
|
837
|
+
* ```
|
|
838
|
+
*/
|
|
839
|
+
fetchRequestWithdrawVaultReceiptAccount(requestWithdrawVaultReceipt: PublicKey): Promise<{
|
|
840
|
+
vault: PublicKey;
|
|
841
|
+
user: PublicKey;
|
|
842
|
+
amountLpEscrowed: BN;
|
|
843
|
+
amountAssetToWithdrawDecimalBits: BN;
|
|
844
|
+
withdrawableFromTs: BN;
|
|
845
|
+
bump: number;
|
|
846
|
+
version: number;
|
|
847
|
+
}>;
|
|
848
|
+
/**
|
|
849
|
+
* Fetches the position and total values for a vault
|
|
850
|
+
* @param vault - Public key of the vault
|
|
851
|
+
* @returns Promise resolving to the position and total values
|
|
852
|
+
*
|
|
853
|
+
* @example
|
|
854
|
+
* ```typescript
|
|
855
|
+
* const positionAndTotalValues = await client.getPositionAndTotalValuesForVault(vaultPubkey);
|
|
856
|
+
* ```
|
|
857
|
+
*/
|
|
858
|
+
getPositionAndTotalValuesForVault(vault: PublicKey): Promise<{
|
|
859
|
+
totalValue: number;
|
|
860
|
+
strategies: {
|
|
861
|
+
strategyId: string;
|
|
862
|
+
amount: number;
|
|
863
|
+
}[];
|
|
864
|
+
}>;
|
|
836
865
|
/**
|
|
837
866
|
* Fetches the accumulated admin fees for a vault
|
|
838
867
|
* @param vault - Public key of the vault
|
|
@@ -855,6 +884,55 @@ export declare class VoltrClient extends AccountUtils {
|
|
|
855
884
|
* ```
|
|
856
885
|
*/
|
|
857
886
|
getAccumulatedManagerFeesForVault(vault: PublicKey): Promise<BN>;
|
|
887
|
+
/**
|
|
888
|
+
* Fetches the current asset per LP for a vault
|
|
889
|
+
* @param vault - Public key of the vault
|
|
890
|
+
* @returns Promise resolving to the current asset per LP
|
|
891
|
+
*
|
|
892
|
+
* @example
|
|
893
|
+
* ```typescript
|
|
894
|
+
* const currentAssetPerLp = await client.getCurrentAssetPerLpForVault(vaultPubkey);
|
|
895
|
+
* ```
|
|
896
|
+
*/
|
|
897
|
+
getCurrentAssetPerLpForVault(vault: PublicKey): Promise<number>;
|
|
898
|
+
/**
|
|
899
|
+
* Fetches the high water mark for a vault
|
|
900
|
+
* @param vault - Public key of the vault
|
|
901
|
+
* @returns Promise resolving to the high water mark
|
|
902
|
+
*
|
|
903
|
+
* @example
|
|
904
|
+
* ```typescript
|
|
905
|
+
* const highWaterMark = await client.getHighWaterMarkForVault(vaultPubkey);
|
|
906
|
+
* ```
|
|
907
|
+
*/
|
|
908
|
+
getHighWaterMarkForVault(vault: PublicKey): Promise<{
|
|
909
|
+
highestAssetPerLp: number;
|
|
910
|
+
lastUpdatedTs: number;
|
|
911
|
+
}>;
|
|
912
|
+
/**
|
|
913
|
+
* Processes a withdrawal receipt into a standardized withdrawal info object
|
|
914
|
+
* @private
|
|
915
|
+
*/
|
|
916
|
+
private processWithdrawalReceipt;
|
|
917
|
+
/**
|
|
918
|
+
* Fetches the pending withdrawal for a user
|
|
919
|
+
* @param vault - Public key of the vault
|
|
920
|
+
* @param user - Public key of the user
|
|
921
|
+
* @returns Promise resolving to the pending withdrawal
|
|
922
|
+
*
|
|
923
|
+
* @example
|
|
924
|
+
* ```typescript
|
|
925
|
+
* const pendingWithdrawal = await client.getPendingWithdrawalForUser(vaultPubkey, userPubkey);
|
|
926
|
+
* ```
|
|
927
|
+
*/
|
|
928
|
+
getPendingWithdrawalForUser(vault: PublicKey, user: PublicKey): Promise<{
|
|
929
|
+
user: PublicKey;
|
|
930
|
+
amountAssetToWithdrawEffective: number;
|
|
931
|
+
amountAssetToWithdrawAtRequest: number;
|
|
932
|
+
amountAssetToWithdrawAtPresent: number;
|
|
933
|
+
amountLpEscrowed: number;
|
|
934
|
+
withdrawableFromTs: number;
|
|
935
|
+
}>;
|
|
858
936
|
/**
|
|
859
937
|
* Fetches all pending withdrawals for a vault
|
|
860
938
|
* @param vault - Public key of the vault
|
|
@@ -866,6 +944,7 @@ export declare class VoltrClient extends AccountUtils {
|
|
|
866
944
|
* ```
|
|
867
945
|
*/
|
|
868
946
|
getAllPendingWithdrawalsForVault(vault: PublicKey): Promise<{
|
|
947
|
+
user: PublicKey;
|
|
869
948
|
amountAssetToWithdrawEffective: number;
|
|
870
949
|
amountAssetToWithdrawAtRequest: number;
|
|
871
950
|
amountAssetToWithdrawAtPresent: number;
|
package/dist/client.js
CHANGED
|
@@ -904,19 +904,6 @@ class VoltrClient extends AccountUtils {
|
|
|
904
904
|
},
|
|
905
905
|
]);
|
|
906
906
|
}
|
|
907
|
-
async getPositionAndTotalValuesForVault(vault) {
|
|
908
|
-
const vaultAccount = await this.fetchVaultAccount(vault);
|
|
909
|
-
const totalAssetValue = vaultAccount.asset.totalValue;
|
|
910
|
-
const strategyInitReceiptAccounts = await this.fetchAllStrategyInitReceiptAccountsOfVault(vault);
|
|
911
|
-
const strategyInfo = strategyInitReceiptAccounts.map((vaultStrategyAccount) => ({
|
|
912
|
-
strategyId: vaultStrategyAccount.account.strategy.toBase58(),
|
|
913
|
-
amount: vaultStrategyAccount.account.positionValue.toNumber(),
|
|
914
|
-
}));
|
|
915
|
-
return {
|
|
916
|
-
totalValue: totalAssetValue.toNumber(),
|
|
917
|
-
strategies: strategyInfo,
|
|
918
|
-
};
|
|
919
|
-
}
|
|
920
907
|
// --------------------------------------- Account Fetching
|
|
921
908
|
/**
|
|
922
909
|
* Fetches a vault account's data
|
|
@@ -952,7 +939,43 @@ class VoltrClient extends AccountUtils {
|
|
|
952
939
|
async fetchAdaptorAddReceiptAccount(adaptorAddReceipt) {
|
|
953
940
|
return await this.vaultProgram.account.adaptorAddReceipt.fetch(adaptorAddReceipt, "confirmed");
|
|
954
941
|
}
|
|
942
|
+
/**
|
|
943
|
+
* Fetches a request withdraw vault receipt account's data
|
|
944
|
+
* @param requestWithdrawVaultReceipt - Public key of the request withdraw vault receipt account
|
|
945
|
+
* @returns Promise resolving to the request withdraw vault receipt account data
|
|
946
|
+
*
|
|
947
|
+
* @example
|
|
948
|
+
* ```typescript
|
|
949
|
+
* const requestWithdrawVaultReceiptAccount = await client.fetchRequestWithdrawVaultReceiptAccount(requestWithdrawVaultReceiptPubkey);
|
|
950
|
+
* ```
|
|
951
|
+
*/
|
|
952
|
+
async fetchRequestWithdrawVaultReceiptAccount(requestWithdrawVaultReceipt) {
|
|
953
|
+
return await this.vaultProgram.account.requestWithdrawVaultReceipt.fetch(requestWithdrawVaultReceipt, "confirmed");
|
|
954
|
+
}
|
|
955
955
|
// --------------------------------------- Helpers
|
|
956
|
+
/**
|
|
957
|
+
* Fetches the position and total values for a vault
|
|
958
|
+
* @param vault - Public key of the vault
|
|
959
|
+
* @returns Promise resolving to the position and total values
|
|
960
|
+
*
|
|
961
|
+
* @example
|
|
962
|
+
* ```typescript
|
|
963
|
+
* const positionAndTotalValues = await client.getPositionAndTotalValuesForVault(vaultPubkey);
|
|
964
|
+
* ```
|
|
965
|
+
*/
|
|
966
|
+
async getPositionAndTotalValuesForVault(vault) {
|
|
967
|
+
const vaultAccount = await this.fetchVaultAccount(vault);
|
|
968
|
+
const totalAssetValue = vaultAccount.asset.totalValue;
|
|
969
|
+
const strategyInitReceiptAccounts = await this.fetchAllStrategyInitReceiptAccountsOfVault(vault);
|
|
970
|
+
const strategyInfo = strategyInitReceiptAccounts.map((vaultStrategyAccount) => ({
|
|
971
|
+
strategyId: vaultStrategyAccount.account.strategy.toBase58(),
|
|
972
|
+
amount: vaultStrategyAccount.account.positionValue.toNumber(),
|
|
973
|
+
}));
|
|
974
|
+
return {
|
|
975
|
+
totalValue: totalAssetValue.toNumber(),
|
|
976
|
+
strategies: strategyInfo,
|
|
977
|
+
};
|
|
978
|
+
}
|
|
956
979
|
/**
|
|
957
980
|
* Fetches the accumulated admin fees for a vault
|
|
958
981
|
* @param vault - Public key of the vault
|
|
@@ -981,6 +1004,85 @@ class VoltrClient extends AccountUtils {
|
|
|
981
1004
|
const vaultAccount = await this.fetchVaultAccount(vault);
|
|
982
1005
|
return vaultAccount.feeState.accumulatedLpManagerFees;
|
|
983
1006
|
}
|
|
1007
|
+
/**
|
|
1008
|
+
* Fetches the current asset per LP for a vault
|
|
1009
|
+
* @param vault - Public key of the vault
|
|
1010
|
+
* @returns Promise resolving to the current asset per LP
|
|
1011
|
+
*
|
|
1012
|
+
* @example
|
|
1013
|
+
* ```typescript
|
|
1014
|
+
* const currentAssetPerLp = await client.getCurrentAssetPerLpForVault(vaultPubkey);
|
|
1015
|
+
* ```
|
|
1016
|
+
*/
|
|
1017
|
+
async getCurrentAssetPerLpForVault(vault) {
|
|
1018
|
+
const vaultLpMint = this.findVaultLpMint(vault);
|
|
1019
|
+
const vaultAccount = await this.fetchVaultAccount(vault);
|
|
1020
|
+
const lpSupply = await (0, spl_token_1.getMint)(this.conn, vaultLpMint).then((lp) => new bn_js_1.default(lp.supply.toString()));
|
|
1021
|
+
const unharvestedFeesLp = vaultAccount.feeState.accumulatedLpAdminFees
|
|
1022
|
+
.add(vaultAccount.feeState.accumulatedLpManagerFees)
|
|
1023
|
+
.add(vaultAccount.feeState.accumulatedLpProtocolFees);
|
|
1024
|
+
const totalLpSupply = unharvestedFeesLp.add(lpSupply);
|
|
1025
|
+
const currentAssetPerLp = vaultAccount.asset.totalValue.toNumber() / totalLpSupply.toNumber();
|
|
1026
|
+
return currentAssetPerLp;
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
* Fetches the high water mark for a vault
|
|
1030
|
+
* @param vault - Public key of the vault
|
|
1031
|
+
* @returns Promise resolving to the high water mark
|
|
1032
|
+
*
|
|
1033
|
+
* @example
|
|
1034
|
+
* ```typescript
|
|
1035
|
+
* const highWaterMark = await client.getHighWaterMarkForVault(vaultPubkey);
|
|
1036
|
+
* ```
|
|
1037
|
+
*/
|
|
1038
|
+
async getHighWaterMarkForVault(vault) {
|
|
1039
|
+
const vaultAccount = await this.fetchVaultAccount(vault);
|
|
1040
|
+
const highWaterMarkValue = (0, decimals_1.convertDecimalBitsToDecimal)(vaultAccount.highWaterMark.highestAssetPerLpDecimalBits);
|
|
1041
|
+
return {
|
|
1042
|
+
highestAssetPerLp: highWaterMarkValue.toNumber(),
|
|
1043
|
+
lastUpdatedTs: vaultAccount.highWaterMark.lastUpdatedTs.toNumber(),
|
|
1044
|
+
};
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* Processes a withdrawal receipt into a standardized withdrawal info object
|
|
1048
|
+
* @private
|
|
1049
|
+
*/
|
|
1050
|
+
async processWithdrawalReceipt(receipt, vaultAccount, lpSupply) {
|
|
1051
|
+
const amountAssetToWithdrawDecimal = (0, decimals_1.convertDecimalBitsToDecimal)(receipt.account.amountAssetToWithdrawDecimalBits);
|
|
1052
|
+
const amountAssetToWithdrawAtRequest = amountAssetToWithdrawDecimal.toNumber();
|
|
1053
|
+
const amountLpEscrowed = receipt.account.amountLpEscrowed;
|
|
1054
|
+
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();
|
|
1055
|
+
// Cap the withdrawal amount to the initial request amount
|
|
1056
|
+
const amountAssetToWithdrawEffective = Math.min(amountAssetToWithdrawAtPresent, amountAssetToWithdrawAtRequest);
|
|
1057
|
+
return {
|
|
1058
|
+
user: receipt.account.user,
|
|
1059
|
+
amountAssetToWithdrawEffective,
|
|
1060
|
+
amountAssetToWithdrawAtRequest,
|
|
1061
|
+
amountAssetToWithdrawAtPresent,
|
|
1062
|
+
amountLpEscrowed: amountLpEscrowed.toNumber(),
|
|
1063
|
+
withdrawableFromTs: receipt.account.withdrawableFromTs.toNumber(),
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* Fetches the pending withdrawal for a user
|
|
1068
|
+
* @param vault - Public key of the vault
|
|
1069
|
+
* @param user - Public key of the user
|
|
1070
|
+
* @returns Promise resolving to the pending withdrawal
|
|
1071
|
+
*
|
|
1072
|
+
* @example
|
|
1073
|
+
* ```typescript
|
|
1074
|
+
* const pendingWithdrawal = await client.getPendingWithdrawalForUser(vaultPubkey, userPubkey);
|
|
1075
|
+
* ```
|
|
1076
|
+
*/
|
|
1077
|
+
async getPendingWithdrawalForUser(vault, user) {
|
|
1078
|
+
const [vaultAccount, lp] = await Promise.all([
|
|
1079
|
+
this.fetchVaultAccount(vault),
|
|
1080
|
+
(0, spl_token_1.getMint)(this.conn, this.findVaultLpMint(vault)),
|
|
1081
|
+
]);
|
|
1082
|
+
const requestWithdrawVaultReceiptAddress = this.findRequestWithdrawVaultReceipt(vault, user);
|
|
1083
|
+
const receipt = await this.fetchRequestWithdrawVaultReceiptAccount(requestWithdrawVaultReceiptAddress);
|
|
1084
|
+
return this.processWithdrawalReceipt({ account: receipt }, vaultAccount, new bn_js_1.default(lp.supply.toString()));
|
|
1085
|
+
}
|
|
984
1086
|
/**
|
|
985
1087
|
* Fetches all pending withdrawals for a vault
|
|
986
1088
|
* @param vault - Public key of the vault
|
|
@@ -992,28 +1094,13 @@ class VoltrClient extends AccountUtils {
|
|
|
992
1094
|
* ```
|
|
993
1095
|
*/
|
|
994
1096
|
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
|
-
});
|
|
1097
|
+
const [requestWithdrawVaultReceipts, vaultAccount, lp] = await Promise.all([
|
|
1098
|
+
this.fetchAllRequestWithdrawVaultReceiptsOfVault(vault),
|
|
1099
|
+
this.fetchVaultAccount(vault),
|
|
1100
|
+
(0, spl_token_1.getMint)(this.conn, this.findVaultLpMint(vault)),
|
|
1101
|
+
]);
|
|
1102
|
+
const lpSupply = new bn_js_1.default(lp.supply.toString());
|
|
1103
|
+
return Promise.all(requestWithdrawVaultReceipts.map((receipt) => this.processWithdrawalReceipt(receipt, vaultAccount, lpSupply)));
|
|
1017
1104
|
}
|
|
1018
1105
|
calculateLockedProfit(lastUpdatedLockedProfit, lockedProfitDegradationDuration, currentTime) {
|
|
1019
1106
|
if (lockedProfitDegradationDuration.eq(new bn_js_1.default(0)))
|