@streamflow/stream 6.3.8 → 7.0.0-alpha.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.
Files changed (121) hide show
  1. package/README.md +33 -1
  2. package/dist/{__tests__ → cjs/__tests__}/aptos/streamClient.spec.js +22 -21
  3. package/dist/cjs/__tests__/common/GenericStreamClient.spec.js +45 -0
  4. package/dist/{aptos → cjs/aptos}/StreamClient.js +20 -20
  5. package/dist/cjs/aptos/constants.js +28 -0
  6. package/dist/{aptos → cjs/aptos}/index.js +3 -3
  7. package/dist/{aptos → cjs/aptos}/types.js +12 -11
  8. package/dist/{aptos → cjs/aptos}/utils.js +2 -2
  9. package/dist/{common → cjs/common}/GenericStreamClient.js +19 -19
  10. package/dist/{common → cjs/common}/contractUtils.js +4 -4
  11. package/dist/{common → cjs/common}/utils.js +4 -6
  12. package/dist/{evm → cjs/evm}/StreamClient.js +29 -29
  13. package/dist/cjs/evm/constants.js +39 -0
  14. package/dist/{evm → cjs/evm}/index.js +8 -8
  15. package/dist/{evm → cjs/evm}/types.js +5 -5
  16. package/dist/{evm → cjs/evm}/utils.js +2 -2
  17. package/dist/{solana → cjs/solana}/StreamClient.js +73 -66
  18. package/dist/cjs/solana/constants.js +60 -0
  19. package/dist/cjs/solana/index.js +39 -0
  20. package/dist/{solana → cjs/solana}/instructions.js +20 -12
  21. package/dist/{solana → cjs/solana}/layout.js +8 -2
  22. package/dist/{solana → cjs/solana}/types.js +5 -5
  23. package/dist/{solana → cjs/solana}/utils.js +6 -6
  24. package/dist/{sui → cjs/sui}/StreamClient.js +25 -25
  25. package/dist/cjs/sui/constants.js +43 -0
  26. package/dist/{sui → cjs/sui}/index.js +5 -5
  27. package/dist/{sui → cjs/sui}/types.js +5 -5
  28. package/dist/{sui → cjs/sui}/utils.js +3 -3
  29. package/dist/esm/__tests__/aptos/streamClient.spec.js +106 -0
  30. package/dist/esm/__tests__/common/GenericStreamClient.spec.js +40 -0
  31. package/dist/{aptos → esm/aptos}/StreamClient.d.ts +3 -3
  32. package/dist/esm/aptos/StreamClient.js +226 -0
  33. package/dist/{aptos → esm/aptos}/constants.d.ts +1 -1
  34. package/dist/esm/aptos/constants.js +24 -0
  35. package/dist/{aptos → esm/aptos}/index.d.ts +1 -1
  36. package/dist/{sui/index.d.ts → esm/aptos/index.js} +1 -1
  37. package/dist/{aptos → esm/aptos}/types.d.ts +1 -1
  38. package/dist/esm/aptos/types.js +105 -0
  39. package/dist/esm/aptos/utils.js +30 -0
  40. package/dist/esm/aptos/wallet.js +33 -0
  41. package/dist/{common → esm/common}/BaseStreamClient.d.ts +1 -1
  42. package/dist/esm/common/BaseStreamClient.js +19 -0
  43. package/dist/{common → esm/common}/GenericStreamClient.d.ts +2 -2
  44. package/dist/esm/common/GenericStreamClient.js +101 -0
  45. package/dist/esm/common/constants.js +3 -0
  46. package/dist/{common → esm/common}/contractUtils.d.ts +1 -1
  47. package/dist/esm/common/contractUtils.js +42 -0
  48. package/dist/esm/common/types.js +106 -0
  49. package/dist/{common → esm/common}/utils.d.ts +0 -1
  50. package/dist/esm/common/utils.js +59 -0
  51. package/dist/{evm → esm/evm}/StreamClient.d.ts +2 -2
  52. package/dist/esm/evm/StreamClient.js +237 -0
  53. package/dist/esm/evm/abi.js +1255 -0
  54. package/dist/{evm → esm/evm}/constants.d.ts +1 -1
  55. package/dist/esm/evm/constants.js +35 -0
  56. package/dist/esm/evm/ercAbi.js +222 -0
  57. package/dist/esm/evm/index.d.ts +5 -0
  58. package/dist/esm/evm/index.js +5 -0
  59. package/dist/{evm → esm/evm}/types.d.ts +1 -1
  60. package/dist/esm/evm/types.js +102 -0
  61. package/dist/esm/evm/utils.js +8 -0
  62. package/dist/esm/index.js +9 -0
  63. package/dist/{solana → esm/solana}/StreamClient.d.ts +6 -6
  64. package/dist/esm/solana/StreamClient.js +695 -0
  65. package/dist/{solana → esm/solana}/constants.d.ts +2 -0
  66. package/dist/esm/solana/constants.js +56 -0
  67. package/dist/esm/solana/index.d.ts +6 -0
  68. package/dist/esm/solana/index.js +6 -0
  69. package/dist/esm/solana/instructionTypes.js +1 -0
  70. package/dist/{solana → esm/solana}/instructions.d.ts +5 -1
  71. package/dist/esm/solana/instructions.js +256 -0
  72. package/dist/{solana → esm/solana}/layout.d.ts +2 -1
  73. package/dist/esm/solana/layout.js +155 -0
  74. package/dist/esm/solana/timelockIDL.js +420 -0
  75. package/dist/{solana → esm/solana}/types.d.ts +2 -1
  76. package/dist/esm/solana/types.js +101 -0
  77. package/dist/{solana → esm/solana}/utils.d.ts +2 -1
  78. package/dist/esm/solana/utils.js +117 -0
  79. package/dist/{sui → esm/sui}/StreamClient.d.ts +3 -3
  80. package/dist/esm/sui/StreamClient.js +403 -0
  81. package/dist/{sui → esm/sui}/constants.d.ts +1 -1
  82. package/dist/esm/sui/constants.js +39 -0
  83. package/dist/esm/sui/index.d.ts +4 -0
  84. package/dist/esm/sui/index.js +4 -0
  85. package/dist/{sui → esm/sui}/types.d.ts +1 -1
  86. package/dist/esm/sui/types.js +104 -0
  87. package/dist/{sui → esm/sui}/utils.d.ts +1 -1
  88. package/dist/esm/sui/utils.js +23 -0
  89. package/dist/{sui → esm/sui}/wallet.d.ts +1 -1
  90. package/dist/esm/sui/wallet.js +38 -0
  91. package/package.json +36 -33
  92. package/dist/__tests__/common/GenericStreamClient.spec.js +0 -44
  93. package/dist/aptos/constants.js +0 -28
  94. package/dist/evm/constants.js +0 -39
  95. package/dist/evm/index.d.ts +0 -5
  96. package/dist/solana/constants.js +0 -59
  97. package/dist/solana/index.d.ts +0 -7
  98. package/dist/solana/index.js +0 -43
  99. package/dist/sui/constants.js +0 -43
  100. /package/dist/{aptos → cjs/aptos}/wallet.js +0 -0
  101. /package/dist/{common → cjs/common}/BaseStreamClient.js +0 -0
  102. /package/dist/{common → cjs/common}/constants.js +0 -0
  103. /package/dist/{common → cjs/common}/types.js +0 -0
  104. /package/dist/{evm → cjs/evm}/abi.js +0 -0
  105. /package/dist/{evm → cjs/evm}/ercAbi.js +0 -0
  106. /package/dist/{index.js → cjs/index.js} +0 -0
  107. /package/dist/{solana → cjs/solana}/instructionTypes.js +0 -0
  108. /package/dist/{solana → cjs/solana}/timelockIDL.js +0 -0
  109. /package/dist/{sui → cjs/sui}/wallet.js +0 -0
  110. /package/dist/{__tests__ → esm/__tests__}/aptos/streamClient.spec.d.ts +0 -0
  111. /package/dist/{__tests__ → esm/__tests__}/common/GenericStreamClient.spec.d.ts +0 -0
  112. /package/dist/{aptos → esm/aptos}/utils.d.ts +0 -0
  113. /package/dist/{aptos → esm/aptos}/wallet.d.ts +0 -0
  114. /package/dist/{common → esm/common}/constants.d.ts +0 -0
  115. /package/dist/{common → esm/common}/types.d.ts +0 -0
  116. /package/dist/{evm → esm/evm}/abi.d.ts +0 -0
  117. /package/dist/{evm → esm/evm}/ercAbi.d.ts +0 -0
  118. /package/dist/{evm → esm/evm}/utils.d.ts +0 -0
  119. /package/dist/{index.d.ts → esm/index.d.ts} +0 -0
  120. /package/dist/{solana → esm/solana}/instructionTypes.d.ts +0 -0
  121. /package/dist/{solana → esm/solana}/timelockIDL.d.ts +0 -0
@@ -1,4 +1,6 @@
1
+ /// <reference types="node" />
1
2
  import { PublicKey } from "@solana/web3.js";
3
+ import { Buffer } from "buffer";
2
4
  export declare const TX_FINALITY_CONFIRMED = "confirmed";
3
5
  export declare const STREAM_STRUCT_OFFSET_SENDER = 49;
4
6
  export declare const STREAM_STRUCT_OFFSET_RECIPIENT = 113;
@@ -0,0 +1,56 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import { Buffer } from "buffer";
3
+ import { ICluster, SolanaContractErrorCode } from "../common/types.js";
4
+ export const TX_FINALITY_CONFIRMED = "confirmed";
5
+ export const STREAM_STRUCT_OFFSET_SENDER = 49;
6
+ export const STREAM_STRUCT_OFFSET_RECIPIENT = 113;
7
+ // Defined: https://github.com/streamflow-finance/protocol/blob/main/programs/protocol/src/state.rs#L25
8
+ export const CREATE_PARAMS_PADDING = 126;
9
+ export const PROGRAM_ID = {
10
+ [ICluster.Devnet]: "HqDGZjaVRXJ9MGRQEw7qDc2rAr6iH1n1kAQdCZaCMfMZ",
11
+ [ICluster.Mainnet]: "strmRqUCoQUgGUan5YhzUZa6KqdzwX5L6FpUxfmKg5m",
12
+ [ICluster.Testnet]: "HqDGZjaVRXJ9MGRQEw7qDc2rAr6iH1n1kAQdCZaCMfMZ",
13
+ [ICluster.Local]: "HqDGZjaVRXJ9MGRQEw7qDc2rAr6iH1n1kAQdCZaCMfMZ",
14
+ };
15
+ export const PARTNER_ORACLE_PROGRAM_ID = "pardpVtPjC8nLj1Dwncew62mUzfChdCX1EaoZe8oCAa";
16
+ export const STREAMFLOW_TREASURY_PUBLIC_KEY = new PublicKey("5SEpbdjFK5FxwTvfsGMXVQTD2v4M2c5tyRTxhdsPkgDw");
17
+ export const WITHDRAWOR_PUBLIC_KEY = new PublicKey("wdrwhnCv4pzW8beKsbPa4S2UDZrXenjg16KJdKSpb5u");
18
+ export const FEE_ORACLE_PUBLIC_KEY = new PublicKey("B743wFVk2pCYhV91cn287e1xY7f1vt4gdY48hhNiuQmT");
19
+ export const AIRDROP_TEST_TOKEN = "Gssm3vfi8s65R31SBdmQRq6cKeYojGgup7whkw4VCiQj";
20
+ export const FEES_METADATA_SEED = Buffer.from("strm_fees");
21
+ export const DEFAULT_STREAMFLOW_FEE = 0.99;
22
+ export const AIRDROP_AMOUNT = 1; // 1 SOL is the cap on the testnet
23
+ export const PARTNER_SCHEMA = {
24
+ struct: {
25
+ pubkey: { array: { type: "u8", len: 32 } },
26
+ partner_fee: "f32",
27
+ strm_fee: "f32",
28
+ },
29
+ };
30
+ export const PARTNERS_SCHEMA = { array: { type: PARTNER_SCHEMA } };
31
+ export const SOLANA_ERROR_MATCH_REGEX = /custom program error: (0x\d{2})/;
32
+ export const SOLANA_ERROR_MAP = {
33
+ 0x60: SolanaContractErrorCode.AccountsNotWritable,
34
+ 0x61: SolanaContractErrorCode.InvalidMetadata,
35
+ 0x62: SolanaContractErrorCode.InvalidMetadataAccount,
36
+ 0x63: SolanaContractErrorCode.MetadataAccountMismatch,
37
+ 0x64: SolanaContractErrorCode.InvalidEscrowAccount,
38
+ 0x65: SolanaContractErrorCode.NotAssociated,
39
+ 0x66: SolanaContractErrorCode.MintMismatch,
40
+ 0x67: SolanaContractErrorCode.TransferNotAllowed,
41
+ 0x68: SolanaContractErrorCode.ContractClosed,
42
+ 0x69: SolanaContractErrorCode.InvalidTreasury,
43
+ 0x70: SolanaContractErrorCode.InvalidTimestamps,
44
+ 0x71: SolanaContractErrorCode.InvalidDepositConfiguration,
45
+ 0x72: SolanaContractErrorCode.AmountIsZero,
46
+ 0x73: SolanaContractErrorCode.AmountMoreThanAvailable,
47
+ 0x74: SolanaContractErrorCode.AmountAvailableIsZero,
48
+ 0x80: SolanaContractErrorCode.ArithmeticError,
49
+ 0x81: SolanaContractErrorCode.InvalidMetadataSize,
50
+ 0x82: SolanaContractErrorCode.UninitializedMetadata,
51
+ 0x83: SolanaContractErrorCode.Unauthorized,
52
+ 0x84: SolanaContractErrorCode.SelfTransfer,
53
+ 0x85: SolanaContractErrorCode.AlreadyPaused,
54
+ 0x86: SolanaContractErrorCode.NotPaused,
55
+ 0x87: SolanaContractErrorCode.MetadataNotRentExempt,
56
+ };
@@ -0,0 +1,6 @@
1
+ export * from "./StreamClient.js";
2
+ export * from "./utils.js";
3
+ export * from "./types.js";
4
+ export * from "./instructions.js";
5
+ export * as constants from "./constants.js";
6
+ export { default as timelockIDL } from "./timelockIDL.js";
@@ -0,0 +1,6 @@
1
+ export * from "./StreamClient.js";
2
+ export * from "./utils.js";
3
+ export * from "./types.js";
4
+ export * from "./instructions.js";
5
+ export * as constants from "./constants.js";
6
+ export { default as timelockIDL } from "./timelockIDL.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +1,6 @@
1
1
  import { PublicKey, TransactionInstruction } from "@solana/web3.js";
2
2
  import BN from "bn.js";
3
- import { IUpdateData } from "../common/types";
3
+ import { IUpdateData } from "../common/types.js";
4
4
  interface CreateStreamData {
5
5
  start: BN;
6
6
  depositedAmount: BN;
@@ -14,6 +14,8 @@ interface CreateStreamData {
14
14
  transferableBySender: boolean;
15
15
  transferableByRecipient: boolean;
16
16
  canTopup: boolean;
17
+ canUpdateRate: boolean;
18
+ canPause: boolean;
17
19
  name: string;
18
20
  withdrawFrequency: BN;
19
21
  }
@@ -51,6 +53,8 @@ interface CreateUncheckedStreamData {
51
53
  transferableBySender: boolean;
52
54
  transferableByRecipient: boolean;
53
55
  canTopup: boolean;
56
+ canUpdateRate: boolean;
57
+ canPause: boolean;
54
58
  name: string;
55
59
  withdrawFrequency: BN;
56
60
  recipient: PublicKey;
@@ -0,0 +1,256 @@
1
+ import { Buffer } from "buffer";
2
+ import { TransactionInstruction } from "@solana/web3.js";
3
+ import { createHash } from "node:crypto";
4
+ import * as Layout from "./layout.js";
5
+ const hasher = () => createHash("sha256");
6
+ const sha256 = {
7
+ digest: (data) => new Uint8Array(hasher().update(data).digest()),
8
+ };
9
+ export const createStreamInstruction = (data, programId, accounts) => {
10
+ const keys = [
11
+ { pubkey: accounts.sender, isSigner: true, isWritable: true },
12
+ { pubkey: accounts.senderTokens, isSigner: false, isWritable: true },
13
+ { pubkey: accounts.recipient, isSigner: false, isWritable: true },
14
+ { pubkey: accounts.metadata, isSigner: true, isWritable: true },
15
+ { pubkey: accounts.escrowTokens, isSigner: false, isWritable: true },
16
+ { pubkey: accounts.recipientTokens, isSigner: false, isWritable: true },
17
+ {
18
+ pubkey: accounts.streamflowTreasury,
19
+ isSigner: false,
20
+ isWritable: true,
21
+ },
22
+ {
23
+ pubkey: accounts.streamflowTreasuryTokens,
24
+ isSigner: false,
25
+ isWritable: true,
26
+ },
27
+ { pubkey: accounts.withdrawor, isSigner: false, isWritable: true },
28
+ { pubkey: accounts.partner, isSigner: false, isWritable: true },
29
+ { pubkey: accounts.partnerTokens, isSigner: false, isWritable: true },
30
+ { pubkey: accounts.mint, isSigner: false, isWritable: false },
31
+ { pubkey: accounts.feeOracle, isSigner: false, isWritable: false },
32
+ { pubkey: accounts.rent, isSigner: false, isWritable: false },
33
+ { pubkey: accounts.timelockProgram, isSigner: false, isWritable: false },
34
+ { pubkey: accounts.tokenProgram, isSigner: false, isWritable: false },
35
+ {
36
+ pubkey: accounts.associatedTokenProgram,
37
+ isSigner: false,
38
+ isWritable: false,
39
+ },
40
+ { pubkey: accounts.systemProgram, isSigner: false, isWritable: false },
41
+ ];
42
+ let bufferData = Buffer.alloc(Layout.createStreamLayout.span);
43
+ const encodedUIntArray = new TextEncoder().encode(data.name);
44
+ const streamNameBuffer = Buffer.alloc(64).fill(encodedUIntArray, 0, encodedUIntArray.byteLength);
45
+ const decodedData = {
46
+ start_time: data.start.toArrayLike(Buffer, "le", 8),
47
+ net_amount_deposited: data.depositedAmount.toArrayLike(Buffer, "le", 8),
48
+ period: data.period.toArrayLike(Buffer, "le", 8),
49
+ amount_per_period: data.amountPerPeriod.toArrayLike(Buffer, "le", 8),
50
+ cliff: data.cliff.toArrayLike(Buffer, "le", 8),
51
+ cliff_amount: data.cliffAmount.toArrayLike(Buffer, "le", 8),
52
+ cancelable_by_sender: Number(data.cancelableBySender),
53
+ cancelable_by_recipient: Number(data.cancelableByRecipient),
54
+ automatic_withdrawal: Number(data.automaticWithdrawal),
55
+ transferable_by_sender: Number(data.transferableBySender),
56
+ transferable_by_recipient: Number(data.transferableByRecipient),
57
+ can_topup: Number(data.canTopup),
58
+ _pausable_discriminator: 1,
59
+ can_update_rate: Number(data.canUpdateRate),
60
+ _can_update_rate_discriminator: 1,
61
+ pausable: Number(data.canPause),
62
+ stream_name: streamNameBuffer,
63
+ withdraw_frequency: data.withdrawFrequency.toArrayLike(Buffer, "le", 8),
64
+ };
65
+ const encodeLength = Layout.createStreamLayout.encode(decodedData, bufferData);
66
+ bufferData = bufferData.slice(0, encodeLength);
67
+ bufferData = Buffer.concat([Buffer.from(sha256.digest("global:create")).slice(0, 8), bufferData, Buffer.alloc(10)]);
68
+ return new TransactionInstruction({
69
+ keys,
70
+ programId,
71
+ data: bufferData,
72
+ });
73
+ };
74
+ export const createUncheckedStreamInstruction = (data, programId, accounts) => {
75
+ const keys = [
76
+ { pubkey: accounts.sender, isSigner: true, isWritable: true },
77
+ { pubkey: accounts.senderTokens, isSigner: false, isWritable: true },
78
+ { pubkey: accounts.metadata, isSigner: false, isWritable: true },
79
+ { pubkey: accounts.escrowTokens, isSigner: false, isWritable: true },
80
+ { pubkey: accounts.withdrawor, isSigner: false, isWritable: true },
81
+ { pubkey: accounts.mint, isSigner: false, isWritable: false },
82
+ { pubkey: accounts.feeOracle, isSigner: false, isWritable: false },
83
+ { pubkey: accounts.rent, isSigner: false, isWritable: false },
84
+ { pubkey: accounts.timelockProgram, isSigner: false, isWritable: false },
85
+ { pubkey: accounts.tokenProgram, isSigner: false, isWritable: false },
86
+ { pubkey: accounts.systemProgram, isSigner: false, isWritable: false },
87
+ ];
88
+ if (accounts.payer) {
89
+ keys.unshift({
90
+ pubkey: accounts.payer,
91
+ isSigner: true,
92
+ isWritable: true,
93
+ });
94
+ }
95
+ let bufferData = Buffer.alloc(Layout.createUncheckedStreamLayout.span);
96
+ const encodedUIntArray = new TextEncoder().encode(data.name);
97
+ const streamNameBuffer = Buffer.alloc(64).fill(encodedUIntArray, 0, encodedUIntArray.byteLength);
98
+ const decodedData = {
99
+ start_time: data.start.toArrayLike(Buffer, "le", 8),
100
+ net_amount_deposited: data.depositedAmount.toArrayLike(Buffer, "le", 8),
101
+ period: data.period.toArrayLike(Buffer, "le", 8),
102
+ amount_per_period: data.amountPerPeriod.toArrayLike(Buffer, "le", 8),
103
+ cliff: data.cliff.toArrayLike(Buffer, "le", 8),
104
+ cliff_amount: data.cliffAmount.toArrayLike(Buffer, "le", 8),
105
+ cancelable_by_sender: Number(data.cancelableBySender),
106
+ cancelable_by_recipient: Number(data.cancelableByRecipient),
107
+ automatic_withdrawal: Number(data.automaticWithdrawal),
108
+ transferable_by_sender: Number(data.transferableBySender),
109
+ transferable_by_recipient: Number(data.transferableByRecipient),
110
+ can_topup: Number(data.canTopup),
111
+ stream_name: streamNameBuffer,
112
+ withdraw_frequency: data.withdrawFrequency.toArrayLike(Buffer, "le", 8),
113
+ recipient: data.recipient.toBuffer(),
114
+ partner: data.partner.toBuffer(),
115
+ pausable: Number(data.canPause),
116
+ can_update_rate: Number(data.canUpdateRate),
117
+ };
118
+ const encodeLength = Layout.createUncheckedStreamLayout.encode(decodedData, bufferData);
119
+ bufferData = bufferData.slice(0, encodeLength);
120
+ const digest = accounts.payer
121
+ ? sha256.digest("global:create_unchecked_with_payer")
122
+ : sha256.digest("global:create_unchecked");
123
+ bufferData = Buffer.concat([Buffer.from(digest).slice(0, 8), bufferData, Buffer.alloc(10)]);
124
+ return new TransactionInstruction({
125
+ keys,
126
+ programId,
127
+ data: bufferData,
128
+ });
129
+ };
130
+ export const withdrawStreamInstruction = (amount, programId, { authority, recipient, recipientTokens, metadata, escrowTokens, streamflowTreasury, streamflowTreasuryTokens, partner, partnerTokens, mint, tokenProgram, }) => {
131
+ const keys = [
132
+ { pubkey: authority, isSigner: true, isWritable: true },
133
+ { pubkey: recipient, isSigner: false, isWritable: true },
134
+ { pubkey: recipientTokens, isSigner: false, isWritable: true },
135
+ { pubkey: metadata, isSigner: false, isWritable: true },
136
+ { pubkey: escrowTokens, isSigner: false, isWritable: true },
137
+ {
138
+ pubkey: streamflowTreasury,
139
+ isSigner: false,
140
+ isWritable: true,
141
+ },
142
+ { pubkey: streamflowTreasuryTokens, isSigner: false, isWritable: true },
143
+ { pubkey: partner, isSigner: false, isWritable: true },
144
+ { pubkey: partnerTokens, isSigner: false, isWritable: true },
145
+ { pubkey: mint, isSigner: false, isWritable: true },
146
+ { pubkey: tokenProgram, isSigner: false, isWritable: false },
147
+ ];
148
+ let data = Buffer.alloc(Layout.withdrawStreamLayout.span);
149
+ const decodedData = { amount: amount.toArrayLike(Buffer, "le", 8) };
150
+ const encodeLength = Layout.withdrawStreamLayout.encode(decodedData, data);
151
+ data = data.slice(0, encodeLength);
152
+ data = Buffer.concat([Buffer.from(sha256.digest("global:withdraw")).slice(0, 8), data, Buffer.alloc(10)]);
153
+ return new TransactionInstruction({
154
+ keys,
155
+ programId,
156
+ data,
157
+ });
158
+ };
159
+ export const updateStreamInstruction = (params, programId, { authority, metadata, withdrawor, systemProgram }) => {
160
+ const keys = [
161
+ { pubkey: authority, isSigner: true, isWritable: true },
162
+ { pubkey: metadata, isSigner: false, isWritable: true },
163
+ { pubkey: withdrawor, isSigner: false, isWritable: true },
164
+ { pubkey: systemProgram, isSigner: false, isWritable: false },
165
+ ];
166
+ let data = Buffer.alloc(100);
167
+ const decodedData = {
168
+ enable_automatic_withdrawal: Number(params.enableAutomaticWithdrawal),
169
+ withdraw_frequency: params.withdrawFrequency ? params.withdrawFrequency.toArrayLike(Buffer, "le", 8) : undefined,
170
+ amount_per_period: params.amountPerPeriod ? params.amountPerPeriod.toArrayLike(Buffer, "le", 8) : undefined,
171
+ };
172
+ const encodeLength = Layout.encodeUpdateStream(decodedData, data);
173
+ data = data.slice(0, encodeLength);
174
+ data = Buffer.concat([Buffer.from(sha256.digest("global:update")).slice(0, 8), data, Buffer.alloc(20)]);
175
+ return new TransactionInstruction({
176
+ keys: keys,
177
+ programId: programId,
178
+ data: data,
179
+ });
180
+ };
181
+ export const cancelStreamInstruction = (programId, { authority, sender, senderTokens, recipient, recipientTokens, metadata, escrowTokens, streamflowTreasury, streamflowTreasuryTokens, partner, partnerTokens, mint, tokenProgram, }) => {
182
+ const keys = [
183
+ { pubkey: authority, isSigner: true, isWritable: false },
184
+ { pubkey: sender, isSigner: false, isWritable: true },
185
+ { pubkey: senderTokens, isSigner: false, isWritable: true },
186
+ { pubkey: recipient, isSigner: false, isWritable: true },
187
+ { pubkey: recipientTokens, isSigner: false, isWritable: true },
188
+ { pubkey: metadata, isSigner: false, isWritable: true },
189
+ { pubkey: escrowTokens, isSigner: false, isWritable: true },
190
+ {
191
+ pubkey: streamflowTreasury,
192
+ isSigner: false,
193
+ isWritable: true,
194
+ },
195
+ { pubkey: streamflowTreasuryTokens, isSigner: false, isWritable: true },
196
+ { pubkey: partner, isSigner: false, isWritable: true },
197
+ { pubkey: partnerTokens, isSigner: false, isWritable: true },
198
+ { pubkey: mint, isSigner: false, isWritable: true },
199
+ { pubkey: tokenProgram, isSigner: false, isWritable: false },
200
+ ];
201
+ const data = Buffer.concat([Buffer.from(sha256.digest("global:cancel")).slice(0, 8), Buffer.alloc(10)]);
202
+ return new TransactionInstruction({
203
+ keys,
204
+ programId,
205
+ data,
206
+ });
207
+ };
208
+ export const transferStreamInstruction = (programId, { authority, newRecipient, newRecipientTokens, metadata, mint, rent, tokenProgram, associatedTokenProgram, systemProgram, }) => {
209
+ const keys = [
210
+ { pubkey: authority, isSigner: true, isWritable: true },
211
+ { pubkey: newRecipient, isSigner: false, isWritable: true },
212
+ { pubkey: newRecipientTokens, isSigner: false, isWritable: true },
213
+ { pubkey: metadata, isSigner: false, isWritable: true },
214
+ { pubkey: mint, isSigner: false, isWritable: false },
215
+ { pubkey: rent, isSigner: false, isWritable: false },
216
+ { pubkey: tokenProgram, isSigner: false, isWritable: false },
217
+ { pubkey: associatedTokenProgram, isSigner: false, isWritable: false },
218
+ { pubkey: systemProgram, isSigner: false, isWritable: false },
219
+ ];
220
+ const data = Buffer.concat([Buffer.from(sha256.digest("global:transfer_recipient")).slice(0, 8), Buffer.alloc(10)]);
221
+ return new TransactionInstruction({
222
+ keys,
223
+ programId,
224
+ data,
225
+ });
226
+ };
227
+ export const topupStreamInstruction = (amount, programId, { sender, senderTokens, metadata, escrowTokens, streamflowTreasury, streamflowTreasuryTokens, partner, partnerTokens, mint, tokenProgram, withdrawor, systemProgram, }) => {
228
+ const keys = [
229
+ { pubkey: sender, isSigner: true, isWritable: true },
230
+ { pubkey: senderTokens, isSigner: false, isWritable: true },
231
+ { pubkey: metadata, isSigner: false, isWritable: true },
232
+ { pubkey: escrowTokens, isSigner: false, isWritable: true },
233
+ {
234
+ pubkey: streamflowTreasury,
235
+ isSigner: false,
236
+ isWritable: true,
237
+ },
238
+ { pubkey: streamflowTreasuryTokens, isSigner: false, isWritable: true },
239
+ { pubkey: withdrawor, isSigner: false, isWritable: true },
240
+ { pubkey: partner, isSigner: false, isWritable: true },
241
+ { pubkey: partnerTokens, isSigner: false, isWritable: true },
242
+ { pubkey: mint, isSigner: false, isWritable: false },
243
+ { pubkey: tokenProgram, isSigner: false, isWritable: false },
244
+ { pubkey: systemProgram, isSigner: false, isWritable: false },
245
+ ];
246
+ let data = Buffer.alloc(Layout.topupStreamLayout.span);
247
+ const decodedData = { amount: amount.toArrayLike(Buffer, "le", 8) };
248
+ const encodeLength = Layout.topupStreamLayout.encode(decodedData, data);
249
+ data = data.slice(0, encodeLength);
250
+ data = Buffer.concat([Buffer.from(sha256.digest("global:topup")).slice(0, 8), data, Buffer.alloc(10)]);
251
+ return new TransactionInstruction({
252
+ keys,
253
+ programId,
254
+ data,
255
+ });
256
+ };
@@ -1,5 +1,6 @@
1
+ /// <reference types="node" />
1
2
  import * as BufferLayout from "@solana/buffer-layout";
2
- import { ICreateStreamLayout, ICreateUncheckedStreamLayout, IPartnerLayout, IStreamLayout, ITopupStreamLayout, IUpdateStreamLayout, IWithdrawStreamLayout } from "./instructionTypes";
3
+ import { ICreateStreamLayout, ICreateUncheckedStreamLayout, IPartnerLayout, IStreamLayout, ITopupStreamLayout, IUpdateStreamLayout, IWithdrawStreamLayout } from "./instructionTypes.js";
3
4
  /**
4
5
  * Stream layout
5
6
  */
@@ -0,0 +1,155 @@
1
+ import * as BufferLayout from "@solana/buffer-layout";
2
+ import { CREATE_PARAMS_PADDING } from "./constants.js";
3
+ /**
4
+ * Stream layout
5
+ */
6
+ export const streamLayout = BufferLayout.struct([
7
+ BufferLayout.blob(8, "magic"),
8
+ BufferLayout.blob(1, "version"),
9
+ BufferLayout.blob(8, "created_at"),
10
+ BufferLayout.blob(8, "withdrawn_amount"),
11
+ BufferLayout.blob(8, "canceled_at"),
12
+ BufferLayout.blob(8, "end_time"),
13
+ BufferLayout.blob(8, "last_withdrawn_at"),
14
+ BufferLayout.blob(32, "sender"),
15
+ BufferLayout.blob(32, "sender_tokens"),
16
+ BufferLayout.blob(32, "recipient"),
17
+ BufferLayout.blob(32, "recipient_tokens"),
18
+ BufferLayout.blob(32, "mint"),
19
+ BufferLayout.blob(32, "escrow_tokens"),
20
+ BufferLayout.blob(32, "streamflow_treasury"),
21
+ BufferLayout.blob(32, "streamflow_treasury_tokens"),
22
+ BufferLayout.blob(8, "streamflow_fee_total"),
23
+ BufferLayout.blob(8, "streamflow_fee_withdrawn"),
24
+ BufferLayout.f32("streamflow_fee_percent"),
25
+ BufferLayout.blob(32, "partner"),
26
+ BufferLayout.blob(32, "partner_tokens"),
27
+ BufferLayout.blob(8, "partner_fee_total"),
28
+ BufferLayout.blob(8, "partner_fee_withdrawn"),
29
+ BufferLayout.f32("partner_fee_percent"),
30
+ BufferLayout.blob(8, "start_time"),
31
+ BufferLayout.blob(8, "net_amount_deposited"),
32
+ BufferLayout.blob(8, "period"),
33
+ BufferLayout.blob(8, "amount_per_period"),
34
+ BufferLayout.blob(8, "cliff"),
35
+ BufferLayout.blob(8, "cliff_amount"),
36
+ BufferLayout.u8("cancelable_by_sender"),
37
+ BufferLayout.u8("cancelable_by_recipient"),
38
+ BufferLayout.u8("automatic_withdrawal"),
39
+ BufferLayout.u8("transferable_by_sender"),
40
+ BufferLayout.u8("transferable_by_recipient"),
41
+ BufferLayout.u8("can_topup"),
42
+ BufferLayout.blob(64, "stream_name"),
43
+ BufferLayout.blob(8, "withdraw_frequency"),
44
+ // Unused, kept for backward compatibility™
45
+ BufferLayout.blob(4, "ghost"),
46
+ BufferLayout.u8("pausable"),
47
+ BufferLayout.u8("can_update_rate"),
48
+ BufferLayout.blob(4, "create_stream_params_padding_length"),
49
+ BufferLayout.seq(BufferLayout.u8(), CREATE_PARAMS_PADDING, "create_params_padding"),
50
+ BufferLayout.u8("closed"),
51
+ BufferLayout.blob(8, "current_pause_start"),
52
+ BufferLayout.blob(8, "pause_cumulative"),
53
+ BufferLayout.blob(8, "last_rate_change_time"),
54
+ BufferLayout.blob(8, "funds_unlocked_at_last_rate_change"),
55
+ ]);
56
+ export const partnerLayout = BufferLayout.struct([
57
+ BufferLayout.blob(32, "pubkey"),
58
+ BufferLayout.f32("partner_fee"),
59
+ BufferLayout.f32("strm_fee"),
60
+ ]);
61
+ /**
62
+ * Create stream instruction layout
63
+ */
64
+ export const createStreamLayout = BufferLayout.struct([
65
+ BufferLayout.blob(8, "start_time"),
66
+ BufferLayout.blob(8, "net_amount_deposited"),
67
+ BufferLayout.blob(8, "period"),
68
+ BufferLayout.blob(8, "amount_per_period"),
69
+ BufferLayout.blob(8, "cliff"),
70
+ BufferLayout.blob(8, "cliff_amount"),
71
+ BufferLayout.u8("cancelable_by_sender"),
72
+ BufferLayout.u8("cancelable_by_recipient"),
73
+ BufferLayout.u8("automatic_withdrawal"),
74
+ BufferLayout.u8("transferable_by_sender"),
75
+ BufferLayout.u8("transferable_by_recipient"),
76
+ BufferLayout.u8("can_topup"),
77
+ BufferLayout.blob(64, "stream_name"),
78
+ BufferLayout.blob(8, "withdraw_frequency"),
79
+ // discriminator for optional field
80
+ BufferLayout.u8("_pausable_discriminator"),
81
+ BufferLayout.u8("pausable"),
82
+ // discriminator for optional field
83
+ BufferLayout.u8("_can_update_rate_discriminator"),
84
+ BufferLayout.u8("can_update_rate"),
85
+ ]);
86
+ /**
87
+ * Create unchecked stream instruction layout
88
+ */
89
+ export const createUncheckedStreamLayout = BufferLayout.struct([
90
+ BufferLayout.blob(8, "start_time"),
91
+ BufferLayout.blob(8, "net_amount_deposited"),
92
+ BufferLayout.blob(8, "period"),
93
+ BufferLayout.blob(8, "amount_per_period"),
94
+ BufferLayout.blob(8, "cliff"),
95
+ BufferLayout.blob(8, "cliff_amount"),
96
+ BufferLayout.u8("cancelable_by_sender"),
97
+ BufferLayout.u8("cancelable_by_recipient"),
98
+ BufferLayout.u8("automatic_withdrawal"),
99
+ BufferLayout.u8("transferable_by_sender"),
100
+ BufferLayout.u8("transferable_by_recipient"),
101
+ BufferLayout.u8("can_topup"),
102
+ BufferLayout.blob(64, "stream_name"),
103
+ BufferLayout.blob(8, "withdraw_frequency"),
104
+ BufferLayout.blob(32, "recipient"),
105
+ BufferLayout.blob(32, "partner"),
106
+ BufferLayout.u8("pausable"),
107
+ BufferLayout.u8("can_update_rate"),
108
+ ]);
109
+ /**
110
+ * Withdraw stream instruction layout
111
+ */
112
+ export const withdrawStreamLayout = BufferLayout.struct([
113
+ BufferLayout.blob(8, "amount"),
114
+ ]);
115
+ /**
116
+ * Encode stream instruction layout
117
+ */
118
+ export const encodeUpdateStream = (values, data) => {
119
+ const structs = [];
120
+ if (values.enable_automatic_withdrawal) {
121
+ structs.push(BufferLayout.u8("enable_automatic_withdrawal_exists"));
122
+ structs.push(BufferLayout.u8("enable_automatic_withdrawal"));
123
+ }
124
+ else {
125
+ structs.push(BufferLayout.u8("enable_automatic_withdrawal_exists"));
126
+ }
127
+ if (values.withdraw_frequency) {
128
+ structs.push(BufferLayout.u8("withdraw_frequency_exists"));
129
+ structs.push(BufferLayout.blob(8, "withdraw_frequency"));
130
+ }
131
+ else {
132
+ structs.push(BufferLayout.u8("withdraw_frequency_exists"));
133
+ }
134
+ if (values.amount_per_period) {
135
+ structs.push(BufferLayout.u8("amount_per_period_exists"));
136
+ structs.push(BufferLayout.blob(8, "amount_per_period"));
137
+ }
138
+ else {
139
+ structs.push(BufferLayout.u8("amount_per_period_exists"));
140
+ }
141
+ return BufferLayout.struct(structs).encode({
142
+ enable_automatic_withdrawal_exists: values.enable_automatic_withdrawal ? 1 : 0,
143
+ enable_automatic_withdrawal: values.enable_automatic_withdrawal ?? 0,
144
+ withdraw_frequency_exists: values.withdraw_frequency ? 1 : 0,
145
+ withdraw_frequency: values.withdraw_frequency ?? 0,
146
+ amount_per_period_exists: values.amount_per_period ? 1 : 0,
147
+ amount_per_period: values.amount_per_period ?? 0,
148
+ }, data);
149
+ };
150
+ /**
151
+ * Topup stream instruction layout
152
+ */
153
+ export const topupStreamLayout = BufferLayout.struct([
154
+ BufferLayout.blob(8, "amount"),
155
+ ]);