@zebec-network/zebec-vault-sdk 5.1.2 → 5.2.0-dev.1
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/artifacts/zebec_vault.d.ts +444 -26
- package/dist/artifacts/zebec_vault.json +820 -552
- package/dist/pda.d.ts +1 -12
- package/dist/pda.js +0 -71
- package/dist/service.d.ts +219 -9
- package/dist/service.js +153 -55
- package/dist/types.d.ts +4 -2
- package/dist/utils.d.ts +2 -2
- package/package.json +2 -2
package/dist/service.js
CHANGED
|
@@ -4,8 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ZebecVaultService = void 0;
|
|
7
|
-
const assert_1 = __importDefault(require("assert"));
|
|
8
|
-
const bignumber_js_1 = require("bignumber.js");
|
|
9
7
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
10
8
|
const spl_token_1 = require("@solana/spl-token");
|
|
11
9
|
const web3_js_1 = require("@solana/web3.js");
|
|
@@ -14,6 +12,8 @@ const solana_common_1 = require("@zebec-network/solana-common");
|
|
|
14
12
|
const zebec_card_v2_sdk_1 = require("@zebec-network/zebec-card-v2-sdk");
|
|
15
13
|
const zebec_stake_sdk_1 = require("@zebec-network/zebec-stake-sdk");
|
|
16
14
|
const zebec_stream_sdk_1 = require("@zebec-network/zebec-stream-sdk");
|
|
15
|
+
const assert_1 = __importDefault(require("assert"));
|
|
16
|
+
const bignumber_js_1 = require("bignumber.js");
|
|
17
17
|
const artifacts_1 = require("./artifacts");
|
|
18
18
|
const constants_1 = require("./constants");
|
|
19
19
|
const errors_1 = require("./errors");
|
|
@@ -247,15 +247,13 @@ class ZebecVaultService {
|
|
|
247
247
|
const decimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, tokenMint);
|
|
248
248
|
const amount = new anchor_1.BN((0, solana_common_1.parseToken)(params.amount, decimals).toString());
|
|
249
249
|
const instructions = [];
|
|
250
|
-
const
|
|
250
|
+
const withdrawerTokenAccount = (0, solana_common_1.getAssociatedTokenAddressSync)(tokenMint, // mint
|
|
251
251
|
withdrawer, // owner
|
|
252
252
|
true);
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
instructions.push(createWsolAccountIx);
|
|
258
|
-
}
|
|
253
|
+
const tokenAccountInfo = await this.connection.getAccountInfo(withdrawerTokenAccount, this.connection.commitment);
|
|
254
|
+
if (!tokenAccountInfo) {
|
|
255
|
+
const createTokenAccountIx = (0, solana_common_1.createAssociatedTokenAccountInstruction)(withdrawer, withdrawerTokenAccount, withdrawer, tokenMint);
|
|
256
|
+
instructions.push(createTokenAccountIx);
|
|
259
257
|
}
|
|
260
258
|
const ix = await this.getWithdrawTokenInstruction(withdrawer, tokenMint, amount, decimals);
|
|
261
259
|
instructions.push(ix);
|
|
@@ -583,7 +581,7 @@ class ZebecVaultService {
|
|
|
583
581
|
}
|
|
584
582
|
}
|
|
585
583
|
async getNextCardIndex() {
|
|
586
|
-
const [cardConfig] = (0,
|
|
584
|
+
const [cardConfig] = (0, zebec_card_v2_sdk_1.deriveCardConfigPda)(this.cardV2Program.programId);
|
|
587
585
|
const decoded = await this.cardV2Program.account.card.fetch(cardConfig, "confirmed");
|
|
588
586
|
return BigInt(decoded.index.addn(1).toString());
|
|
589
587
|
}
|
|
@@ -597,8 +595,8 @@ class ZebecVaultService {
|
|
|
597
595
|
const [vaultSigner] = (0, pda_1.deriveVaultSigner)(vault, this.vaultV1ProgramId);
|
|
598
596
|
const usdcMint = (0, anchor_1.translateAddress)(params.usdcAddress);
|
|
599
597
|
const amount = (0, bignumber_js_1.BigNumber)(params.amount);
|
|
600
|
-
const [cardConfig] = (0,
|
|
601
|
-
const [userPurchaseRecord] = (0,
|
|
598
|
+
const [cardConfig] = (0, zebec_card_v2_sdk_1.deriveCardConfigPda)(this.cardV2Program.programId);
|
|
599
|
+
const [userPurchaseRecord] = (0, zebec_card_v2_sdk_1.deriveUserPurchaseRecordPda)(vaultSigner, this.cardV2Program.programId);
|
|
602
600
|
const cardConfigInfo = await this.cardV2Program.account.card.fetch(cardConfig, "confirmed");
|
|
603
601
|
const cardVault = cardConfigInfo.cardVault;
|
|
604
602
|
const revenueVault = cardConfigInfo.revenueVault;
|
|
@@ -644,8 +642,8 @@ class ZebecVaultService {
|
|
|
644
642
|
const [vaultSigner] = (0, pda_1.deriveVaultSigner)(vault, this.vaultV1ProgramId);
|
|
645
643
|
const usdcMint = (0, anchor_1.translateAddress)(params.usdcAddress);
|
|
646
644
|
const amount = (0, bignumber_js_1.BigNumber)(params.amount);
|
|
647
|
-
const [cardConfig] = (0,
|
|
648
|
-
const [userPurchaseRecord] = (0,
|
|
645
|
+
const [cardConfig] = (0, zebec_card_v2_sdk_1.deriveCardConfigPda)(this.cardV2Program.programId);
|
|
646
|
+
const [userPurchaseRecord] = (0, zebec_card_v2_sdk_1.deriveUserPurchaseRecordPda)(vaultSigner, this.cardV2Program.programId);
|
|
649
647
|
const cardConfigInfo = await this.cardV2Program.account.card.fetch(cardConfig, "confirmed");
|
|
650
648
|
const cardVault = cardConfigInfo.cardVault;
|
|
651
649
|
const revenueVault = cardConfigInfo.revenueVault;
|
|
@@ -693,8 +691,8 @@ class ZebecVaultService {
|
|
|
693
691
|
const [vaultSigner] = (0, pda_1.deriveVaultSigner)(vault, this.vaultV1ProgramId);
|
|
694
692
|
const inputMint = (0, anchor_1.translateAddress)(quoteInfo.inputMint);
|
|
695
693
|
const usdc = (0, anchor_1.translateAddress)(quoteInfo.outputMint);
|
|
696
|
-
const [userPurchaseRecord] = (0,
|
|
697
|
-
const [cardConfig] = (0,
|
|
694
|
+
const [userPurchaseRecord] = (0, zebec_card_v2_sdk_1.deriveUserPurchaseRecordPda)(vaultSigner, this.cardV2ProgramId);
|
|
695
|
+
const [cardConfig] = (0, zebec_card_v2_sdk_1.deriveCardConfigPda)(this.cardV2ProgramId);
|
|
698
696
|
const cardConfigInfo = await this.cardV2Program.account.card.fetch(cardConfig, this.connection.commitment);
|
|
699
697
|
const revenueVault = cardConfigInfo.revenueVault;
|
|
700
698
|
const cardVault = cardConfigInfo.cardVault;
|
|
@@ -712,7 +710,7 @@ class ZebecVaultService {
|
|
|
712
710
|
if (!inputMint.equals(solana_common_1.WSOL) || !isSolWrapUnwrapNeeded) {
|
|
713
711
|
const userInputMintAtaInfo = await this.connection.getAccountInfo(userInputMintAta, this.connection.commitment);
|
|
714
712
|
if (!userInputMintAtaInfo) {
|
|
715
|
-
throw new errors_1.AssociatedTokenAccountDoesNotExistsError(
|
|
713
|
+
throw new errors_1.AssociatedTokenAccountDoesNotExistsError(`User doesn't have associated token account of input mint: ${inputMint.toString()}`);
|
|
716
714
|
}
|
|
717
715
|
const resAndCtx = await this.connection.getTokenAccountBalance(userInputMintAta, this.connection.commitment);
|
|
718
716
|
const balance = resAndCtx.value.amount;
|
|
@@ -754,15 +752,25 @@ class ZebecVaultService {
|
|
|
754
752
|
const transaction = web3_js_1.VersionedTransaction.deserialize(swapTransactionBuf);
|
|
755
753
|
// get address lookup table accounts
|
|
756
754
|
const addressLookupTableAccounts = await Promise.all(transaction.message.addressTableLookups.map(async (lookup) => {
|
|
757
|
-
const data = await this.connection.getAccountInfo(lookup.accountKey).then((res) =>
|
|
755
|
+
const data = await this.connection.getAccountInfo(lookup.accountKey).then((res) => {
|
|
756
|
+
if (!res) {
|
|
757
|
+
throw new Error(`Lookup table account not found for key: ${(0, anchor_1.translateAddress)(constants_1.CARD_LOOKUP_TABLE_ADDRESS[this.network]).toString()}`);
|
|
758
|
+
}
|
|
759
|
+
return res.data;
|
|
760
|
+
});
|
|
758
761
|
return new web3_js_1.AddressLookupTableAccount({
|
|
759
762
|
key: lookup.accountKey,
|
|
760
763
|
state: web3_js_1.AddressLookupTableAccount.deserialize(data),
|
|
761
764
|
});
|
|
762
765
|
}));
|
|
763
|
-
const lookupTableData = await
|
|
766
|
+
const lookupTableData = await this.connection
|
|
764
767
|
.getAccountInfo((0, anchor_1.translateAddress)(constants_1.CARD_LOOKUP_TABLE_ADDRESS[this.network]))
|
|
765
|
-
.then((res) =>
|
|
768
|
+
.then((res) => {
|
|
769
|
+
if (!res) {
|
|
770
|
+
throw new Error(`Lookup table account not found for key: ${(0, anchor_1.translateAddress)(constants_1.CARD_LOOKUP_TABLE_ADDRESS[this.network]).toString()}`);
|
|
771
|
+
}
|
|
772
|
+
return res.data;
|
|
773
|
+
});
|
|
766
774
|
addressLookupTableAccounts.push(new web3_js_1.AddressLookupTableAccount({
|
|
767
775
|
key: (0, anchor_1.translateAddress)(constants_1.CARD_LOOKUP_TABLE_ADDRESS[this.network]),
|
|
768
776
|
state: web3_js_1.AddressLookupTableAccount.deserialize(lookupTableData),
|
|
@@ -797,8 +805,8 @@ class ZebecVaultService {
|
|
|
797
805
|
const [vaultSigner] = (0, pda_1.deriveVaultSigner)(vault, this.vaultV1ProgramId);
|
|
798
806
|
const inputMint = (0, anchor_1.translateAddress)(quoteInfo.inputMint);
|
|
799
807
|
const usdc = (0, anchor_1.translateAddress)(quoteInfo.outputMint);
|
|
800
|
-
const [userPurchaseRecord] = (0,
|
|
801
|
-
const [cardConfig] = (0,
|
|
808
|
+
const [userPurchaseRecord] = (0, zebec_card_v2_sdk_1.deriveUserPurchaseRecordPda)(vaultSigner, this.cardV2ProgramId);
|
|
809
|
+
const [cardConfig] = (0, zebec_card_v2_sdk_1.deriveCardConfigPda)(this.cardV2ProgramId);
|
|
802
810
|
const cardConfigInfo = await this.cardV2Program.account.card.fetch(cardConfig, this.connection.commitment);
|
|
803
811
|
const revenueVault = cardConfigInfo.revenueVault;
|
|
804
812
|
const cardVault = cardConfigInfo.cardVault;
|
|
@@ -858,15 +866,25 @@ class ZebecVaultService {
|
|
|
858
866
|
const transaction = web3_js_1.VersionedTransaction.deserialize(swapTransactionBuf);
|
|
859
867
|
// get address lookup table accounts
|
|
860
868
|
const addressLookupTableAccounts = await Promise.all(transaction.message.addressTableLookups.map(async (lookup) => {
|
|
861
|
-
const data = await this.connection.getAccountInfo(lookup.accountKey).then((res) =>
|
|
869
|
+
const data = await this.connection.getAccountInfo(lookup.accountKey).then((res) => {
|
|
870
|
+
if (!res) {
|
|
871
|
+
throw new Error(`Lookup table account not found for key: ${lookup.accountKey.toString()}`);
|
|
872
|
+
}
|
|
873
|
+
return res.data;
|
|
874
|
+
});
|
|
862
875
|
return new web3_js_1.AddressLookupTableAccount({
|
|
863
876
|
key: lookup.accountKey,
|
|
864
877
|
state: web3_js_1.AddressLookupTableAccount.deserialize(data),
|
|
865
878
|
});
|
|
866
879
|
}));
|
|
867
|
-
const lookupTableData = await
|
|
880
|
+
const lookupTableData = await this.connection
|
|
868
881
|
.getAccountInfo((0, anchor_1.translateAddress)(constants_1.CARD_LOOKUP_TABLE_ADDRESS[this.network]))
|
|
869
|
-
.then((res) =>
|
|
882
|
+
.then((res) => {
|
|
883
|
+
if (!res) {
|
|
884
|
+
throw new Error(`Lookup table account not found for key: ${(0, anchor_1.translateAddress)(constants_1.CARD_LOOKUP_TABLE_ADDRESS[this.network]).toString()}`);
|
|
885
|
+
}
|
|
886
|
+
return res.data;
|
|
887
|
+
});
|
|
870
888
|
addressLookupTableAccounts.push(new web3_js_1.AddressLookupTableAccount({
|
|
871
889
|
key: (0, anchor_1.translateAddress)(constants_1.CARD_LOOKUP_TABLE_ADDRESS[this.network]),
|
|
872
890
|
state: web3_js_1.AddressLookupTableAccount.deserialize(lookupTableData),
|
|
@@ -892,7 +910,7 @@ class ZebecVaultService {
|
|
|
892
910
|
return this._createTransactionPayload(vaultOwner, otherIxs, [], addressLookupTableAccounts);
|
|
893
911
|
}
|
|
894
912
|
async getCardCustomTokenFees() {
|
|
895
|
-
const [tokeFeeMapPda] = (0,
|
|
913
|
+
const [tokeFeeMapPda] = (0, zebec_card_v2_sdk_1.deriveTokenFeeMapPda)(this.cardV2ProgramId);
|
|
896
914
|
const decoded = await this.cardV2Program.account.customFeeMap.fetch(tokeFeeMapPda, this.connection.commitment);
|
|
897
915
|
const tokenFeeList = decoded.feeMap.map((item) => ({
|
|
898
916
|
tokenAddress: item.tokenAddress,
|
|
@@ -900,6 +918,21 @@ class ZebecVaultService {
|
|
|
900
918
|
}));
|
|
901
919
|
return tokenFeeList;
|
|
902
920
|
}
|
|
921
|
+
async getTransfeFeesForStreamInstruction(streamConfig, feeVault, tokenMint, vaultOwner, feeVaultTokenAccount, vault, vaultSigner, vaultSignerTokenAccount, data) {
|
|
922
|
+
return this.vaultV1Program.methods
|
|
923
|
+
.transferFees({ decimals: data.decimals, feeAmount: data.feeAmount })
|
|
924
|
+
.accountsPartial({
|
|
925
|
+
streamConfig,
|
|
926
|
+
feeVault,
|
|
927
|
+
tokenMint,
|
|
928
|
+
vaultOwner,
|
|
929
|
+
feeVaultTokenAccount,
|
|
930
|
+
vault,
|
|
931
|
+
vaultSigner,
|
|
932
|
+
vaultSignerTokenAccount,
|
|
933
|
+
})
|
|
934
|
+
.instruction();
|
|
935
|
+
}
|
|
903
936
|
async getCreateStreamFromVaultInstruction(vaultOwner, vault, vaultSigner, vaultSignerAta, receiver, receiverAta, streamToken, streamMetadata, streamConfig, withdrawAccount, streamVault, streamVaultAta, zebecStreamProgram, streamData) {
|
|
904
937
|
return this.vaultV1Program.methods
|
|
905
938
|
.createStream({
|
|
@@ -986,6 +1019,19 @@ class ZebecVaultService {
|
|
|
986
1019
|
})
|
|
987
1020
|
.instruction();
|
|
988
1021
|
}
|
|
1022
|
+
async getTransferFeesForStreamInstruction(feeVault, tokenMint, vaultOwner, feeVaultTokenAccount, vault, vaultSigner, vaultSignerTokenAccount, data) {
|
|
1023
|
+
return this.vaultV1Program.methods
|
|
1024
|
+
.transferFees({ decimals: data.decimals, feeAmount: data.feeAmount })
|
|
1025
|
+
.accountsPartial({
|
|
1026
|
+
feeVault,
|
|
1027
|
+
tokenMint,
|
|
1028
|
+
vaultOwner,
|
|
1029
|
+
feeVaultTokenAccount,
|
|
1030
|
+
vault,
|
|
1031
|
+
vaultSigner,
|
|
1032
|
+
vaultSignerTokenAccount,
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
989
1035
|
async createStreamFromVault(params) {
|
|
990
1036
|
const vaultOwner = (0, anchor_1.translateAddress)(params.vaultOwner);
|
|
991
1037
|
const [vault] = (0, pda_1.deriveUserVault)(vaultOwner, this.vaultV1ProgramId);
|
|
@@ -996,15 +1042,28 @@ class ZebecVaultService {
|
|
|
996
1042
|
const streamToken = (0, anchor_1.translateAddress)(params.streamToken);
|
|
997
1043
|
const vaultSignerAta = (0, solana_common_1.getAssociatedTokenAddressSync)(streamToken, vaultSigner, true);
|
|
998
1044
|
const receiverVaultSignerAta = (0, solana_common_1.getAssociatedTokenAddressSync)(streamToken, receiverVaultSigner, true);
|
|
999
|
-
const [streamConfig] = (0,
|
|
1045
|
+
const [streamConfig] = (0, zebec_stream_sdk_1.deriveStreamConfigPda)(params.streamConfigName, this.streamProgramId);
|
|
1000
1046
|
const streamConfigInfo = await this.streamProgram.account.streamConfig.fetch(streamConfig, this.connection.commitment);
|
|
1001
1047
|
const withdrawAccount = streamConfigInfo.withdrawAccount;
|
|
1002
1048
|
const streamMetatdataKeypair = params.streamMetadataKeypair ?? web3_js_1.Keypair.generate();
|
|
1003
1049
|
const streamMetadata = streamMetatdataKeypair.publicKey;
|
|
1004
|
-
const [streamVault] = (0,
|
|
1050
|
+
const [streamVault] = (0, zebec_stream_sdk_1.deriveStreamVaultPda)(streamMetadata, this.streamProgramId);
|
|
1005
1051
|
const streamVaultAta = (0, solana_common_1.getAssociatedTokenAddressSync)(streamToken, streamVault, true);
|
|
1006
1052
|
const streamTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, streamToken);
|
|
1007
|
-
const amount =
|
|
1053
|
+
const amount = (0, bignumber_js_1.BigNumber)(params.amount).times(solana_common_1.TEN_BIGNUM.pow(streamTokenDecimals)).toFixed(0);
|
|
1054
|
+
const feeInfo = await (0, zebec_stream_sdk_1.getFeeInfoForStream)(streamToken, amount, streamTokenDecimals, this.network);
|
|
1055
|
+
const feeToken = (0, anchor_1.translateAddress)(feeInfo.feeToken.mintAddress);
|
|
1056
|
+
const feeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, feeToken);
|
|
1057
|
+
const parsedFeeAmount = feeInfo.feeAmountRaw;
|
|
1058
|
+
const feeVault = streamConfigInfo.feeVault;
|
|
1059
|
+
const feeVaultAta = (0, solana_common_1.getAssociatedTokenAddressSync)(feeToken, feeVault, true);
|
|
1060
|
+
const vaultSignerFeeTokenAta = (0, solana_common_1.getAssociatedTokenAddressSync)(feeToken, vaultSigner, true);
|
|
1061
|
+
const ixs = [];
|
|
1062
|
+
const transferFeesIx = await this.getTransfeFeesForStreamInstruction(streamConfig, feeVault, feeToken, vaultOwner, feeVaultAta, vault, vaultSigner, vaultSignerFeeTokenAta, {
|
|
1063
|
+
decimals: feeTokenDecimals,
|
|
1064
|
+
feeAmount: new anchor_1.BN(parsedFeeAmount),
|
|
1065
|
+
});
|
|
1066
|
+
ixs.push(transferFeesIx);
|
|
1008
1067
|
const cliffPercentage = new anchor_1.BN((0, core_utils_1.percentToBps)(params.cliffPercentage));
|
|
1009
1068
|
const STREAM_NAME_BUFFER_SIZE = 128;
|
|
1010
1069
|
const streamNameArray = new Uint8Array(STREAM_NAME_BUFFER_SIZE);
|
|
@@ -1013,8 +1072,8 @@ class ZebecVaultService {
|
|
|
1013
1072
|
if (params.automaticWithdrawal && autoWithdrawFrequency <= 0) {
|
|
1014
1073
|
throw new Error("autoWithdrawFrequency must be greater than 0 when automaticWithdrawal is enabled");
|
|
1015
1074
|
}
|
|
1016
|
-
const
|
|
1017
|
-
amount,
|
|
1075
|
+
const createStreamIx = await this.getCreateStreamFromVaultInstruction(vaultOwner, vault, vaultSigner, vaultSignerAta, receiverVaultSigner, receiverVaultSignerAta, streamToken, streamMetadata, streamConfig, withdrawAccount, streamVault, streamVaultAta, this.streamProgramId, {
|
|
1076
|
+
amount: new anchor_1.BN(amount),
|
|
1018
1077
|
automaticWithdrawal: params.automaticWithdrawal,
|
|
1019
1078
|
autoWithdrawFrequency: new anchor_1.BN(autoWithdrawFrequency),
|
|
1020
1079
|
cancelableByRecipient: params.cancelableByRecipient,
|
|
@@ -1031,12 +1090,35 @@ class ZebecVaultService {
|
|
|
1031
1090
|
transferableByRecipient: params.transferableByRecipient,
|
|
1032
1091
|
transferableBySender: params.transferableBySender,
|
|
1033
1092
|
});
|
|
1034
|
-
|
|
1093
|
+
const lookupTable = zebec_stream_sdk_1.STREAM_PROGRAM_LOOKUP_TABLE_ADDRESS[this.network];
|
|
1094
|
+
const lookupTableData = await this.connection.getAccountInfo((0, anchor_1.translateAddress)(lookupTable)).then((res) => {
|
|
1095
|
+
if (!res) {
|
|
1096
|
+
throw new Error(`Lookup table account not found for key: ${(0, anchor_1.translateAddress)(constants_1.CARD_LOOKUP_TABLE_ADDRESS[this.network]).toString()}`);
|
|
1097
|
+
}
|
|
1098
|
+
return res.data;
|
|
1099
|
+
});
|
|
1100
|
+
const addressLookupTableAccount = new web3_js_1.AddressLookupTableAccount({
|
|
1101
|
+
key: (0, anchor_1.translateAddress)(lookupTable),
|
|
1102
|
+
state: web3_js_1.AddressLookupTableAccount.deserialize(lookupTableData),
|
|
1103
|
+
});
|
|
1104
|
+
ixs.push(createStreamIx);
|
|
1105
|
+
return this._createTransactionPayload(vaultOwner, ixs, [streamMetatdataKeypair], [addressLookupTableAccount]);
|
|
1035
1106
|
}
|
|
1036
1107
|
async createMultipleStreamFromVault(params) {
|
|
1037
1108
|
const vaultOwner = (0, anchor_1.translateAddress)(params.vaultOwner);
|
|
1038
1109
|
const [vault] = (0, pda_1.deriveUserVault)(vaultOwner, this.vaultV1ProgramId);
|
|
1039
1110
|
const [vaultSigner] = (0, pda_1.deriveVaultSigner)(vault, this.vaultV1ProgramId);
|
|
1111
|
+
const lookupTable = zebec_stream_sdk_1.STREAM_PROGRAM_LOOKUP_TABLE_ADDRESS[this.network];
|
|
1112
|
+
const lookupTableData = await this.connection.getAccountInfo((0, anchor_1.translateAddress)(lookupTable)).then((res) => {
|
|
1113
|
+
if (!res) {
|
|
1114
|
+
throw new Error(`Lookup table account not found for key: ${(0, anchor_1.translateAddress)(constants_1.CARD_LOOKUP_TABLE_ADDRESS[this.network]).toString()}`);
|
|
1115
|
+
}
|
|
1116
|
+
return res.data;
|
|
1117
|
+
});
|
|
1118
|
+
const addressLookupTableAccount = new web3_js_1.AddressLookupTableAccount({
|
|
1119
|
+
key: (0, anchor_1.translateAddress)(lookupTable),
|
|
1120
|
+
state: web3_js_1.AddressLookupTableAccount.deserialize(lookupTableData),
|
|
1121
|
+
});
|
|
1040
1122
|
const transactionArtifacts = await Promise.all(params.streamInfo.map(async (info) => {
|
|
1041
1123
|
const receiver = (0, anchor_1.translateAddress)(info.receiver);
|
|
1042
1124
|
const [receiverVault] = (0, pda_1.deriveUserVault)(receiver, this.vaultV1ProgramId);
|
|
@@ -1044,15 +1126,28 @@ class ZebecVaultService {
|
|
|
1044
1126
|
const streamToken = (0, anchor_1.translateAddress)(info.streamToken);
|
|
1045
1127
|
const vaultSignerAta = (0, solana_common_1.getAssociatedTokenAddressSync)(streamToken, vaultSigner, true);
|
|
1046
1128
|
const receiverVaultSignerAta = (0, solana_common_1.getAssociatedTokenAddressSync)(streamToken, receiverVaultSigner, true);
|
|
1047
|
-
const [streamConfig] = (0,
|
|
1129
|
+
const [streamConfig] = (0, zebec_stream_sdk_1.deriveStreamConfigPda)(params.streamConfigName, this.streamProgramId);
|
|
1048
1130
|
const streamConfigInfo = await this.streamProgram.account.streamConfig.fetch(streamConfig, this.connection.commitment);
|
|
1049
1131
|
const withdrawAccount = streamConfigInfo.withdrawAccount;
|
|
1050
1132
|
const streamMetatdataKeypair = info.streamMetadataKeypair ?? web3_js_1.Keypair.generate();
|
|
1051
1133
|
const streamMetadata = streamMetatdataKeypair.publicKey;
|
|
1052
|
-
const [streamVault] = (0,
|
|
1134
|
+
const [streamVault] = (0, zebec_stream_sdk_1.deriveStreamVaultPda)(streamMetadata, this.streamProgramId);
|
|
1053
1135
|
const streamVaultAta = (0, solana_common_1.getAssociatedTokenAddressSync)(streamToken, streamVault, true);
|
|
1054
1136
|
const streamTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, streamToken);
|
|
1055
|
-
const amount =
|
|
1137
|
+
const amount = (0, bignumber_js_1.BigNumber)(info.amount).times(solana_common_1.TEN_BIGNUM.pow(streamTokenDecimals)).toFixed(0);
|
|
1138
|
+
const feeInfo = await (0, zebec_stream_sdk_1.getFeeInfoForStream)(streamToken, amount, streamTokenDecimals, this.network);
|
|
1139
|
+
const feeToken = (0, anchor_1.translateAddress)(feeInfo.feeToken.mintAddress);
|
|
1140
|
+
const feeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, feeToken);
|
|
1141
|
+
const parsedFeeAmount = feeInfo.feeAmountRaw;
|
|
1142
|
+
const feeVault = streamConfigInfo.feeVault;
|
|
1143
|
+
const feeVaultAta = (0, solana_common_1.getAssociatedTokenAddressSync)(feeToken, feeVault, true);
|
|
1144
|
+
const vaultSignerFeeTokenAta = (0, solana_common_1.getAssociatedTokenAddressSync)(feeToken, vaultSigner, true);
|
|
1145
|
+
const ixs = [];
|
|
1146
|
+
const transferFeesIx = await this.getTransfeFeesForStreamInstruction(streamConfig, feeVault, feeToken, vaultOwner, feeVaultAta, vault, vaultSigner, vaultSignerFeeTokenAta, {
|
|
1147
|
+
decimals: feeTokenDecimals,
|
|
1148
|
+
feeAmount: new anchor_1.BN(parsedFeeAmount),
|
|
1149
|
+
});
|
|
1150
|
+
ixs.push(transferFeesIx);
|
|
1056
1151
|
const cliffPercentage = new anchor_1.BN((0, core_utils_1.percentToBps)(info.cliffPercentage));
|
|
1057
1152
|
const STREAM_NAME_BUFFER_SIZE = 128;
|
|
1058
1153
|
const streamNameArray = new Uint8Array(STREAM_NAME_BUFFER_SIZE);
|
|
@@ -1061,8 +1156,8 @@ class ZebecVaultService {
|
|
|
1061
1156
|
if (info.automaticWithdrawal && autoWithdrawFrequency <= 0) {
|
|
1062
1157
|
throw new Error("autoWithdrawFrequency must be greater than 0 when automaticWithdrawal is enabled");
|
|
1063
1158
|
}
|
|
1064
|
-
const
|
|
1065
|
-
amount,
|
|
1159
|
+
const createStreamIx = await this.getCreateStreamFromVaultInstruction(vaultOwner, vault, vaultSigner, vaultSignerAta, receiverVaultSigner, receiverVaultSignerAta, streamToken, streamMetadata, streamConfig, withdrawAccount, streamVault, streamVaultAta, this.streamProgramId, {
|
|
1160
|
+
amount: new anchor_1.BN(amount),
|
|
1066
1161
|
automaticWithdrawal: info.automaticWithdrawal,
|
|
1067
1162
|
autoWithdrawFrequency: new anchor_1.BN(autoWithdrawFrequency),
|
|
1068
1163
|
cancelableByRecipient: info.cancelableByRecipient,
|
|
@@ -1079,9 +1174,11 @@ class ZebecVaultService {
|
|
|
1079
1174
|
transferableByRecipient: info.transferableByRecipient,
|
|
1080
1175
|
transferableBySender: info.transferableBySender,
|
|
1081
1176
|
});
|
|
1177
|
+
ixs.push(createStreamIx);
|
|
1082
1178
|
return {
|
|
1083
|
-
instructions:
|
|
1179
|
+
instructions: ixs,
|
|
1084
1180
|
signers: [streamMetatdataKeypair],
|
|
1181
|
+
addressLookupTableAccounts: [addressLookupTableAccount],
|
|
1085
1182
|
};
|
|
1086
1183
|
}));
|
|
1087
1184
|
return this._createMultiTransactionPayload(vaultOwner, transactionArtifacts);
|
|
@@ -1111,7 +1208,7 @@ class ZebecVaultService {
|
|
|
1111
1208
|
const feePayer = vaultOwner;
|
|
1112
1209
|
const userAta = (0, solana_common_1.getAssociatedTokenAddressSync)(streamToken, vaultSigner, true);
|
|
1113
1210
|
const otherPartyAta = (0, solana_common_1.getAssociatedTokenAddressSync)(streamToken, otherParty, true);
|
|
1114
|
-
const [streamVault] = (0,
|
|
1211
|
+
const [streamVault] = (0, zebec_stream_sdk_1.deriveStreamVaultPda)(streamMetadata, this.streamProgramId);
|
|
1115
1212
|
const streamVaultAta = (0, solana_common_1.getAssociatedTokenAddressSync)(streamToken, streamVault, true);
|
|
1116
1213
|
const ix = await this.getCancelStreamInstruction(feePayer, otherParty, otherPartyAta, vaultSigner, userAta, streamMetadata, streamToken, streamVault, streamVaultAta);
|
|
1117
1214
|
const actions = [(0, utils_1.transactionInstructionToPropoalAction)(ix)];
|
|
@@ -1208,7 +1305,7 @@ class ZebecVaultService {
|
|
|
1208
1305
|
const streamMetadataAccount = await this.streamProgram.account.paymentStream.fetch(streamMetadata);
|
|
1209
1306
|
const streamToken = streamMetadataAccount.financials.streamToken;
|
|
1210
1307
|
const vaultSignerAta = (0, solana_common_1.getAssociatedTokenAddressSync)(streamToken, vaultSigner, true);
|
|
1211
|
-
const [streamVault] = (0,
|
|
1308
|
+
const [streamVault] = (0, zebec_stream_sdk_1.deriveStreamVaultPda)(streamMetadata, this.streamProgramId);
|
|
1212
1309
|
const streamVaultAta = (0, solana_common_1.getAssociatedTokenAddressSync)(streamToken, streamVault, true);
|
|
1213
1310
|
const ix = await this.getWithdrawStreamInstruction(vaultSigner, vaultSignerAta, streamMetadata, streamToken, streamVault, streamVaultAta, withdrawer, feePayer);
|
|
1214
1311
|
const actions = [(0, utils_1.transactionInstructionToPropoalAction)(ix)];
|
|
@@ -1315,7 +1412,7 @@ class ZebecVaultService {
|
|
|
1315
1412
|
const feePayer = vaultOwner;
|
|
1316
1413
|
const [vault] = (0, pda_1.deriveUserVault)(vaultOwner, this.vaultV1ProgramId);
|
|
1317
1414
|
const [vaultSigner] = (0, pda_1.deriveVaultSigner)(vault, this.vaultV1ProgramId);
|
|
1318
|
-
const lockup = (0,
|
|
1415
|
+
const lockup = (0, zebec_stake_sdk_1.deriveLockupAddress)(params.lockupName, this.stakeProgramId);
|
|
1319
1416
|
const lockupAccount = await this.stakeProgram.account.lockup.fetchNullable(lockup, this.connection.commitment);
|
|
1320
1417
|
if (!lockupAccount) {
|
|
1321
1418
|
throw new Error("Lockup account does not exists for address: " + lockup);
|
|
@@ -1325,14 +1422,14 @@ class ZebecVaultService {
|
|
|
1325
1422
|
throw new Error("Invalid lockperiod. Available options are: " + lockPeriods.map((l) => l.toString()).concat(", "));
|
|
1326
1423
|
}
|
|
1327
1424
|
const stakeToken = lockupAccount.stakedToken.tokenAddress;
|
|
1328
|
-
const stakeVault = (0,
|
|
1329
|
-
const userNonce = (0,
|
|
1425
|
+
const stakeVault = (0, zebec_stake_sdk_1.deriveStakeVaultAddress)(lockup, this.stakeProgramId);
|
|
1426
|
+
const userNonce = (0, zebec_stake_sdk_1.deriveUserNonceAddress)(vaultSigner, lockup, this.stakeProgramId);
|
|
1330
1427
|
const userNonceAccount = await this.stakeProgram.account.userNonce.fetchNullable(userNonce, this.connection.commitment);
|
|
1331
1428
|
let nonce = BigInt(0);
|
|
1332
1429
|
if (userNonceAccount) {
|
|
1333
1430
|
nonce = BigInt(userNonceAccount.nonce.toString());
|
|
1334
1431
|
}
|
|
1335
|
-
const stakePda = (0,
|
|
1432
|
+
const stakePda = (0, zebec_stake_sdk_1.deriveStakeAddress)(vaultSigner, lockup, nonce, this.stakeProgramId);
|
|
1336
1433
|
const stakeVaultTokenAccount = (0, solana_common_1.getAssociatedTokenAddressSync)(stakeToken, stakeVault, true);
|
|
1337
1434
|
const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, stakeToken);
|
|
1338
1435
|
const UNITS_PER_STAKE_TOKEN = solana_common_1.TEN_BIGNUM.pow(stakeTokenDecimals);
|
|
@@ -1369,17 +1466,17 @@ class ZebecVaultService {
|
|
|
1369
1466
|
const [vault] = (0, pda_1.deriveUserVault)(vaultOwner, this.vaultV1ProgramId);
|
|
1370
1467
|
const [vaultSigner] = (0, pda_1.deriveVaultSigner)(vault, this.vaultV1ProgramId);
|
|
1371
1468
|
const feePayer = vaultOwner;
|
|
1372
|
-
const lockup = (0,
|
|
1469
|
+
const lockup = (0, zebec_stake_sdk_1.deriveLockupAddress)(params.lockupName, this.stakeProgramId);
|
|
1373
1470
|
const lockupAccount = await this.stakeProgram.account.lockup.fetchNullable(lockup, this.connection.commitment);
|
|
1374
1471
|
if (!lockupAccount) {
|
|
1375
|
-
throw new Error(
|
|
1472
|
+
throw new Error(`Lockup account does not exists for address: ${lockup}`);
|
|
1376
1473
|
}
|
|
1377
1474
|
const stakeToken = lockupAccount.stakedToken.tokenAddress;
|
|
1378
1475
|
const rewardToken = lockupAccount.rewardToken.tokenAddress;
|
|
1379
1476
|
const feeVault = lockupAccount.feeInfo.feeVault;
|
|
1380
|
-
const stakePda = (0,
|
|
1381
|
-
const rewardVault = (0,
|
|
1382
|
-
const stakeVault = (0,
|
|
1477
|
+
const stakePda = (0, zebec_stake_sdk_1.deriveStakeAddress)(vaultSigner, lockup, params.nonce, this.stakeProgramId);
|
|
1478
|
+
const rewardVault = (0, zebec_stake_sdk_1.deriveRewardVaultAddress)(lockup, this.stakeProgramId);
|
|
1479
|
+
const stakeVault = (0, zebec_stake_sdk_1.deriveStakeVaultAddress)(lockup, this.stakeProgramId);
|
|
1383
1480
|
const stakerTokenAccount = (0, solana_common_1.getAssociatedTokenAddressSync)(stakeToken, vaultSigner, true);
|
|
1384
1481
|
const instruction = await this.getUnstakeInstruction(feePayer, feeVault, lockup, stakePda, rewardToken, rewardVault, stakeToken, stakeVault, vaultSigner, stakerTokenAccount, new anchor_1.BN(params.nonce.toString()));
|
|
1385
1482
|
const actions = [(0, utils_1.transactionInstructionToPropoalAction)(instruction)];
|
|
@@ -1406,8 +1503,8 @@ class ZebecVaultService {
|
|
|
1406
1503
|
const vaultOwner = (0, anchor_1.translateAddress)(vaultOwnerAddress);
|
|
1407
1504
|
const [vault] = (0, pda_1.deriveUserVault)(vaultOwner, this.vaultV1ProgramId);
|
|
1408
1505
|
const [vaultSigner] = (0, pda_1.deriveVaultSigner)(vault, this.vaultV1ProgramId);
|
|
1409
|
-
const lockup = (0,
|
|
1410
|
-
const userNonceAddress = (0,
|
|
1506
|
+
const lockup = (0, zebec_stake_sdk_1.deriveLockupAddress)(lockupName, this.stakeProgramId);
|
|
1507
|
+
const userNonceAddress = (0, zebec_stake_sdk_1.deriveUserNonceAddress)(vaultSigner, lockup, this.stakeProgramId);
|
|
1411
1508
|
const userNonceAccount = await this.stakeProgram.account.userNonce.fetchNullable(userNonceAddress, commitment ?? this.connection.commitment);
|
|
1412
1509
|
if (!userNonceAccount) {
|
|
1413
1510
|
return null;
|
|
@@ -1419,8 +1516,8 @@ class ZebecVaultService {
|
|
|
1419
1516
|
}
|
|
1420
1517
|
async _createTransactionPayload(payerKey, instructions, signers, addressLookupTableAccounts) {
|
|
1421
1518
|
const errorMap = new Map();
|
|
1422
|
-
this.vaultV1Program.idl.errors.
|
|
1423
|
-
let signTransaction
|
|
1519
|
+
this.vaultV1Program.idl.errors.map((error) => errorMap.set(error.code, error.msg));
|
|
1520
|
+
let signTransaction;
|
|
1424
1521
|
const provider = this.provider;
|
|
1425
1522
|
if (provider instanceof anchor_1.AnchorProvider) {
|
|
1426
1523
|
signTransaction = async (tx) => {
|
|
@@ -1431,8 +1528,8 @@ class ZebecVaultService {
|
|
|
1431
1528
|
}
|
|
1432
1529
|
async _createMultiTransactionPayload(payerKey, transactionArtifacts) {
|
|
1433
1530
|
const errorMap = new Map();
|
|
1434
|
-
this.vaultV1Program.idl.errors.
|
|
1435
|
-
let signAllTransactions
|
|
1531
|
+
this.vaultV1Program.idl.errors.map((error) => errorMap.set(error.code, error.msg));
|
|
1532
|
+
let signAllTransactions;
|
|
1436
1533
|
const provider = this.provider;
|
|
1437
1534
|
if (provider instanceof anchor_1.AnchorProvider) {
|
|
1438
1535
|
signAllTransactions = async (txns) => {
|
|
@@ -1443,6 +1540,7 @@ class ZebecVaultService {
|
|
|
1443
1540
|
instructions: data.instructions,
|
|
1444
1541
|
feePayer: payerKey,
|
|
1445
1542
|
signers: data.signers,
|
|
1543
|
+
addressLookupTableAccounts: data.addressLookupTableAccounts,
|
|
1446
1544
|
}));
|
|
1447
1545
|
return new solana_common_1.MultiTransactionPayload(this.connection, errorMap, transactionData, signAllTransactions);
|
|
1448
1546
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Address, BN } from "@coral-xyz/anchor";
|
|
2
|
-
import { Keypair, PublicKey } from "@solana/web3.js";
|
|
1
|
+
import type { Address, BN } from "@coral-xyz/anchor";
|
|
2
|
+
import type { Keypair, PublicKey } from "@solana/web3.js";
|
|
3
3
|
export type Numeric = number | string;
|
|
4
4
|
export type ProposalAction = {
|
|
5
5
|
programId: PublicKey;
|
|
@@ -163,6 +163,7 @@ export type CreateStreamFromVaultParams = {
|
|
|
163
163
|
vaultOwner: Address;
|
|
164
164
|
receiver: Address;
|
|
165
165
|
streamToken: Address;
|
|
166
|
+
streamConfigName: string;
|
|
166
167
|
amount: Numeric;
|
|
167
168
|
automaticWithdrawal: boolean;
|
|
168
169
|
cancelableByRecipient: boolean;
|
|
@@ -182,6 +183,7 @@ export type CreateStreamFromVaultParams = {
|
|
|
182
183
|
};
|
|
183
184
|
export type createMultipleStreamFromVaultParams = {
|
|
184
185
|
vaultOwner: Address;
|
|
186
|
+
streamConfigName: string;
|
|
185
187
|
streamInfo: {
|
|
186
188
|
receiver: Address;
|
|
187
189
|
streamToken: Address;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { TransactionInstruction } from "@solana/web3.js";
|
|
2
|
-
import { ProposalAction } from "./types";
|
|
1
|
+
import type { TransactionInstruction } from "@solana/web3.js";
|
|
2
|
+
import type { ProposalAction } from "./types";
|
|
3
3
|
export declare function calculateActionsSize(actions: ProposalAction[]): number;
|
|
4
4
|
export declare function calculateProposalSize(name: string, actions: ProposalAction[]): number;
|
|
5
5
|
export declare function transactionInstructionToPropoalAction(ix: TransactionInstruction): ProposalAction;
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"@zebec-network/solana-common": "^2.3.1",
|
|
10
10
|
"@zebec-network/zebec-card-v2-sdk": "^2.5.4",
|
|
11
11
|
"@zebec-network/zebec-stake-sdk": "^1.2.2",
|
|
12
|
-
"@zebec-network/zebec-stream-sdk": "^
|
|
12
|
+
"@zebec-network/zebec-stream-sdk": "^3.1.0-dev.1",
|
|
13
13
|
"bignumber.js": "^9.3.1",
|
|
14
14
|
"buffer": "^6.0.3"
|
|
15
15
|
},
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"test:single": "ts-mocha -p ./tsconfig.json -t 1000000000"
|
|
48
48
|
},
|
|
49
49
|
"types": "dist/index.d.ts",
|
|
50
|
-
"version": "5.1
|
|
50
|
+
"version": "5.2.0-dev.1"
|
|
51
51
|
}
|