@streamflow/stream 7.0.0-alpha.11 → 7.0.0-alpha.13

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 (56) hide show
  1. package/README.md +22 -17
  2. package/dist/cjs/__tests__/aptos/streamClient.spec.js +4 -4
  3. package/dist/cjs/aptos/types.js +12 -12
  4. package/dist/cjs/common/constants.js +4 -3
  5. package/dist/cjs/common/contractUtils.js +23 -7
  6. package/dist/cjs/common/types.js +30 -1
  7. package/dist/cjs/common/utils.js +22 -19
  8. package/dist/cjs/evm/StreamClient.js +7 -7
  9. package/dist/cjs/evm/types.js +12 -12
  10. package/dist/cjs/solana/StreamClient.js +256 -111
  11. package/dist/cjs/solana/constants.js +29 -1
  12. package/dist/cjs/solana/descriptor/idl/streamflow_aligned_unlocks.json +1471 -0
  13. package/dist/cjs/solana/descriptor/streamflow_aligned_unlocks.js +2 -0
  14. package/dist/cjs/solana/index.js +2 -1
  15. package/dist/cjs/solana/instructions.js +18 -19
  16. package/dist/cjs/solana/lib/derive-accounts.js +17 -0
  17. package/dist/cjs/solana/{utils.js → lib/utils.js} +29 -36
  18. package/dist/cjs/solana/types.js +26 -3
  19. package/dist/cjs/sui/StreamClient.js +1 -1
  20. package/dist/cjs/sui/types.js +12 -12
  21. package/dist/esm/__tests__/aptos/streamClient.spec.js +4 -4
  22. package/dist/esm/aptos/types.d.ts +14 -14
  23. package/dist/esm/aptos/types.js +13 -13
  24. package/dist/esm/common/constants.d.ts +3 -2
  25. package/dist/esm/common/constants.js +3 -2
  26. package/dist/esm/common/contractUtils.d.ts +18 -15
  27. package/dist/esm/common/contractUtils.js +19 -6
  28. package/dist/esm/common/types.d.ts +112 -57
  29. package/dist/esm/common/types.js +29 -0
  30. package/dist/esm/common/utils.d.ts +11 -11
  31. package/dist/esm/common/utils.js +19 -16
  32. package/dist/esm/evm/StreamClient.d.ts +2 -2
  33. package/dist/esm/evm/StreamClient.js +7 -7
  34. package/dist/esm/evm/types.d.ts +14 -14
  35. package/dist/esm/evm/types.js +13 -13
  36. package/dist/esm/solana/StreamClient.d.ts +20 -14
  37. package/dist/esm/solana/StreamClient.js +259 -113
  38. package/dist/esm/solana/constants.d.ts +14 -0
  39. package/dist/esm/solana/constants.js +29 -1
  40. package/dist/esm/solana/descriptor/idl/streamflow_aligned_unlocks.json +1471 -0
  41. package/dist/esm/solana/descriptor/streamflow_aligned_unlocks.d.ts +1272 -0
  42. package/dist/esm/solana/descriptor/streamflow_aligned_unlocks.js +1 -0
  43. package/dist/esm/solana/index.d.ts +2 -1
  44. package/dist/esm/solana/index.js +2 -1
  45. package/dist/esm/solana/instructions.d.ts +17 -17
  46. package/dist/esm/solana/instructions.js +18 -19
  47. package/dist/esm/solana/lib/derive-accounts.d.ts +4 -0
  48. package/dist/esm/solana/lib/derive-accounts.js +11 -0
  49. package/dist/esm/solana/{utils.d.ts → lib/utils.d.ts} +1 -3
  50. package/dist/esm/solana/{utils.js → lib/utils.js} +28 -34
  51. package/dist/esm/solana/types.d.ts +67 -39
  52. package/dist/esm/solana/types.js +32 -4
  53. package/dist/esm/sui/StreamClient.js +1 -1
  54. package/dist/esm/sui/types.d.ts +14 -14
  55. package/dist/esm/sui/types.js +13 -13
  56. package/package.json +7 -5
@@ -1,20 +1,21 @@
1
- import BigNumber from "bignumber.js";
1
+ import BN from "bn.js";
2
2
  import { StreamType } from "./types.js";
3
+ import { MAX_SAFE_UNIX_TIME_VALUE } from "./constants.js";
3
4
  export const calculateUnlockedAmount = ({ depositedAmount, cliff, cliffAmount, end, currentTimestamp, lastRateChangeTime, period, amountPerPeriod, fundsUnlockedAtLastRateChange, }) => {
4
5
  const deposited = depositedAmount;
5
6
  if (currentTimestamp < cliff)
6
- return BigNumber(0);
7
+ return new BN(0);
7
8
  if (currentTimestamp > end)
8
9
  return deposited;
9
10
  const savedUnlockedFunds = lastRateChangeTime === 0 ? cliffAmount : fundsUnlockedAtLastRateChange;
10
11
  const savedUnlockedFundsTime = lastRateChangeTime === 0 ? cliff : lastRateChangeTime;
11
- const streamed = BigNumber(Math.floor((currentTimestamp - savedUnlockedFundsTime) / period))
12
- .times(amountPerPeriod)
13
- .plus(savedUnlockedFunds);
12
+ const streamed = new BN(Math.floor((currentTimestamp - savedUnlockedFundsTime) / period))
13
+ .mul(amountPerPeriod)
14
+ .add(savedUnlockedFunds);
14
15
  return streamed.lt(deposited) ? streamed : deposited;
15
16
  };
16
17
  export const isCliffCloseToDepositedAmount = (streamData) => {
17
- return streamData.cliffAmount.gte(streamData.depositedAmount.minus(BigNumber(1)));
18
+ return streamData.cliffAmount.gte(streamData.depositedAmount.sub(new BN(1)));
18
19
  };
19
20
  export const isPayment = (streamData) => {
20
21
  return streamData.canTopup;
@@ -22,6 +23,12 @@ export const isPayment = (streamData) => {
22
23
  export const isVesting = (streamData) => {
23
24
  return !streamData.canTopup && !isCliffCloseToDepositedAmount(streamData);
24
25
  };
26
+ export const isAligned = (stream) => {
27
+ return "minPrice" in stream && "maxPrice" in stream && "minPercentage" in stream && "maxPercentage" in stream;
28
+ };
29
+ export const isCreateAlignedStreamData = (obj) => {
30
+ return "minPrice" in obj && "maxPrice" in obj && "minPercentage" in obj && "maxPercentage" in obj;
31
+ };
25
32
  export const isTokenLock = (streamData) => {
26
33
  return (!streamData.canTopup &&
27
34
  !streamData.automaticWithdrawal &&
@@ -40,3 +47,9 @@ export const buildStreamType = (streamData) => {
40
47
  }
41
48
  return StreamType.Payment;
42
49
  };
50
+ export const decodeEndTime = (endTime) => {
51
+ if (endTime.gt(new BN(MAX_SAFE_UNIX_TIME_VALUE))) {
52
+ return MAX_SAFE_UNIX_TIME_VALUE;
53
+ }
54
+ return endTime.toNumber();
55
+ };
@@ -1,15 +1,16 @@
1
+ import { Address } from "@coral-xyz/anchor";
1
2
  import { TransactionInstruction } from "@solana/web3.js";
2
3
  import { Types } from "aptos";
3
- import BigNumber from "bignumber.js";
4
+ import BN from "bn.js";
4
5
  export { IChain, ICluster, ContractError } from "@streamflow/common";
5
6
  export interface IRecipient {
6
7
  recipient: string;
7
- amount: BigNumber;
8
+ amount: BN;
8
9
  name: string;
9
- cliffAmount: BigNumber;
10
- amountPerPeriod: BigNumber;
10
+ cliffAmount: BN;
11
+ amountPerPeriod: BN;
11
12
  }
12
- export interface IStreamConfig {
13
+ export interface IBaseStreamConfig {
13
14
  period: number;
14
15
  start: number;
15
16
  cliff: number;
@@ -25,27 +26,41 @@ export interface IStreamConfig {
25
26
  canUpdateRate?: boolean;
26
27
  partner?: string;
27
28
  }
28
- export type ICreateStreamData = IStreamConfig & IRecipient;
29
- export type ICreateMultipleStreamData = IStreamConfig & {
29
+ export type IAlignedStreamConfig = {
30
+ minPrice: number;
31
+ maxPrice: number;
32
+ minPercentage: number;
33
+ maxPercentage: number;
34
+ oracleType?: OracleTypeName;
35
+ priceOracle?: Address;
36
+ skipInitial?: boolean;
37
+ tickSize?: number;
38
+ };
39
+ export type ICreateLinearStreamData = IBaseStreamConfig & IRecipient;
40
+ export type ICreateAlignedStreamData = ICreateLinearStreamData & IAlignedStreamConfig;
41
+ export type ICreateStreamData = ICreateLinearStreamData | ICreateAlignedStreamData;
42
+ export type ICreateMultipleLinearStreamData = IBaseStreamConfig & {
30
43
  recipients: IRecipient[];
31
44
  };
45
+ export type ICreateMultipleAlignedStreamData = ICreateMultipleLinearStreamData & IAlignedStreamConfig;
46
+ export type ICreateMultipleStreamData = ICreateMultipleLinearStreamData | ICreateMultipleAlignedStreamData;
32
47
  export interface IInteractData {
33
48
  id: string;
34
49
  }
35
50
  export interface IWithdrawData extends IInteractData {
36
- amount?: BigNumber;
51
+ amount?: BN;
37
52
  }
38
53
  export interface IUpdateData extends IInteractData {
39
54
  enableAutomaticWithdrawal?: boolean;
40
- withdrawFrequency?: BigNumber;
41
- amountPerPeriod?: BigNumber;
55
+ withdrawFrequency?: BN;
56
+ amountPerPeriod?: BN;
42
57
  }
43
58
  export type ICancelData = IInteractData;
44
59
  export interface ITransferData extends IInteractData {
45
60
  newRecipient: string;
46
61
  }
47
62
  export interface ITopUpData extends IInteractData {
48
- amount: BigNumber;
63
+ amount: BN;
49
64
  }
50
65
  export type IGetOneData = IInteractData;
51
66
  export interface IGetFeesData {
@@ -79,6 +94,7 @@ export interface IMultiTransactionResult {
79
94
  metadataToRecipient: Record<MetadataId, IRecipient>;
80
95
  errors: ICreateMultiError[];
81
96
  }
97
+ export type OracleTypeName = "none" | "pyth" | "switchboard" | "test";
82
98
  export declare enum StreamDirection {
83
99
  Outgoing = "outgoing",
84
100
  Incoming = "incoming",
@@ -129,6 +145,64 @@ export declare enum ContractErrorCode {
129
145
  /** Aptos: user opt out from direct coin transfers feature and has not coin wallet registered */
130
146
  ENO_RECIPIENT_COIN_ADDRESS = "ENO_RECIPIENT_COIN_ADDRESS"
131
147
  }
148
+ export interface LinearStream {
149
+ magic: number;
150
+ version: number;
151
+ createdAt: number;
152
+ withdrawnAmount: BN;
153
+ canceledAt: number;
154
+ end: number;
155
+ lastWithdrawnAt: number;
156
+ sender: string;
157
+ senderTokens: string;
158
+ recipient: string;
159
+ recipientTokens: string;
160
+ mint: string;
161
+ escrowTokens: string;
162
+ streamflowTreasury: string;
163
+ streamflowTreasuryTokens: string;
164
+ streamflowFeeTotal: BN;
165
+ streamflowFeeWithdrawn: BN;
166
+ streamflowFeePercent: number;
167
+ partnerFeeTotal: BN;
168
+ partnerFeeWithdrawn: BN;
169
+ partnerFeePercent: number;
170
+ partner: string;
171
+ partnerTokens: string;
172
+ start: number;
173
+ depositedAmount: BN;
174
+ period: number;
175
+ amountPerPeriod: BN;
176
+ cliff: number;
177
+ cliffAmount: BN;
178
+ cancelableBySender: boolean;
179
+ cancelableByRecipient: boolean;
180
+ automaticWithdrawal: boolean;
181
+ transferableBySender: boolean;
182
+ transferableByRecipient: boolean;
183
+ canTopup: boolean;
184
+ name: string;
185
+ withdrawalFrequency: number;
186
+ closed: boolean;
187
+ currentPauseStart: number;
188
+ pauseCumulative: BN;
189
+ lastRateChangeTime: number;
190
+ fundsUnlockedAtLastRateChange: BN;
191
+ type: StreamType;
192
+ unlocked(currentTimestamp: number): BN;
193
+ remaining(decimals: number): number;
194
+ }
195
+ export type AlignedStreamData = {
196
+ minPrice: number;
197
+ maxPrice: number;
198
+ minPercentage: number;
199
+ maxPercentage: number;
200
+ oracleType: OracleTypeName;
201
+ priceOracle: string | undefined;
202
+ tickSize: number;
203
+ };
204
+ export type AlignedStream = LinearStream & AlignedStreamData;
205
+ export type Stream = LinearStream | AlignedStream;
132
206
  /**
133
207
  * Error codes raised by Solana protocol specifically
134
208
  */
@@ -180,50 +254,31 @@ export declare enum SolanaContractErrorCode {
180
254
  /** Meta account is not rent exempt */
181
255
  MetadataNotRentExempt = "MetadataNotRentExempt"
182
256
  }
183
- export interface Stream {
184
- magic: number;
185
- version: number;
186
- createdAt: number;
187
- withdrawnAmount: BigNumber;
188
- canceledAt: number;
189
- end: number;
190
- lastWithdrawnAt: number;
191
- sender: string;
192
- senderTokens: string;
193
- recipient: string;
194
- recipientTokens: string;
195
- mint: string;
196
- escrowTokens: string;
197
- streamflowTreasury: string;
198
- streamflowTreasuryTokens: string;
199
- streamflowFeeTotal: BigNumber;
200
- streamflowFeeWithdrawn: BigNumber;
201
- streamflowFeePercent: number;
202
- partnerFeeTotal: BigNumber;
203
- partnerFeeWithdrawn: BigNumber;
204
- partnerFeePercent: number;
205
- partner: string;
206
- partnerTokens: string;
207
- start: number;
208
- depositedAmount: BigNumber;
209
- period: number;
210
- amountPerPeriod: BigNumber;
211
- cliff: number;
212
- cliffAmount: BigNumber;
213
- cancelableBySender: boolean;
214
- cancelableByRecipient: boolean;
215
- automaticWithdrawal: boolean;
216
- transferableBySender: boolean;
217
- transferableByRecipient: boolean;
218
- canTopup: boolean;
219
- name: string;
220
- withdrawalFrequency: number;
221
- closed: boolean;
222
- currentPauseStart: number;
223
- pauseCumulative: BigNumber;
224
- lastRateChangeTime: number;
225
- fundsUnlockedAtLastRateChange: BigNumber;
226
- type: StreamType;
227
- unlocked(currentTimestamp: number): BigNumber;
228
- remaining(decimals: number): number;
257
+ export declare enum SolanaAlignedProxyErrorCode {
258
+ /** Authority does not have permission for this action */
259
+ Unauthorized = "Unauthorized",
260
+ /** Arithmetic error */
261
+ ArithmeticError = "ArithmeticError",
262
+ /** Mint has unsupported Token Extensions */
263
+ UnsupportedTokenExtensions = "UnsupportedTokenExtensions",
264
+ /** Provided period is too short, should be equal or more than 30 seconds */
265
+ PeriodTooShort = "PeriodTooShort",
266
+ /** Provided percentage tick size is invalid */
267
+ InvalidTickSize = "InvalidTickSize",
268
+ /** Provided percentage bounds are invalid */
269
+ InvalidPercentageBoundaries = "InvalidPercentageBoundaries",
270
+ /** Provided price bounds are invalid */
271
+ InvalidPriceBoundaries = "InvalidPriceBoundaries",
272
+ /** Unsupported price oracle */
273
+ UnsupportedOracle = "UnsupportedOracle",
274
+ /** Invalid oracle account */
275
+ InvalidOracleAccount = "InvalidOracleAccount",
276
+ /** Invalid oracle price */
277
+ InvalidOraclePrice = "InvalidOraclePrice",
278
+ /** Invalid Stream Metadata */
279
+ InvalidStreamMetadata = "InvalidStreamMetadata",
280
+ /** Release amount has already been updated in this period */
281
+ AmountAlreadyUpdated = "AmountAlreadyUpdated",
282
+ /** All funds are already unlocked */
283
+ AllFundsUnlocked = "AllFundsUnlocked"
229
284
  }
@@ -104,3 +104,32 @@ export var SolanaContractErrorCode;
104
104
  /** Meta account is not rent exempt */
105
105
  SolanaContractErrorCode["MetadataNotRentExempt"] = "MetadataNotRentExempt";
106
106
  })(SolanaContractErrorCode || (SolanaContractErrorCode = {}));
107
+ export var SolanaAlignedProxyErrorCode;
108
+ (function (SolanaAlignedProxyErrorCode) {
109
+ /** Authority does not have permission for this action */
110
+ SolanaAlignedProxyErrorCode["Unauthorized"] = "Unauthorized";
111
+ /** Arithmetic error */
112
+ SolanaAlignedProxyErrorCode["ArithmeticError"] = "ArithmeticError";
113
+ /** Mint has unsupported Token Extensions */
114
+ SolanaAlignedProxyErrorCode["UnsupportedTokenExtensions"] = "UnsupportedTokenExtensions";
115
+ /** Provided period is too short, should be equal or more than 30 seconds */
116
+ SolanaAlignedProxyErrorCode["PeriodTooShort"] = "PeriodTooShort";
117
+ /** Provided percentage tick size is invalid */
118
+ SolanaAlignedProxyErrorCode["InvalidTickSize"] = "InvalidTickSize";
119
+ /** Provided percentage bounds are invalid */
120
+ SolanaAlignedProxyErrorCode["InvalidPercentageBoundaries"] = "InvalidPercentageBoundaries";
121
+ /** Provided price bounds are invalid */
122
+ SolanaAlignedProxyErrorCode["InvalidPriceBoundaries"] = "InvalidPriceBoundaries";
123
+ /** Unsupported price oracle */
124
+ SolanaAlignedProxyErrorCode["UnsupportedOracle"] = "UnsupportedOracle";
125
+ /** Invalid oracle account */
126
+ SolanaAlignedProxyErrorCode["InvalidOracleAccount"] = "InvalidOracleAccount";
127
+ /** Invalid oracle price */
128
+ SolanaAlignedProxyErrorCode["InvalidOraclePrice"] = "InvalidOraclePrice";
129
+ /** Invalid Stream Metadata */
130
+ SolanaAlignedProxyErrorCode["InvalidStreamMetadata"] = "InvalidStreamMetadata";
131
+ /** Release amount has already been updated in this period */
132
+ SolanaAlignedProxyErrorCode["AmountAlreadyUpdated"] = "AmountAlreadyUpdated";
133
+ /** All funds are already unlocked */
134
+ SolanaAlignedProxyErrorCode["AllFundsUnlocked"] = "AllFundsUnlocked";
135
+ })(SolanaAlignedProxyErrorCode || (SolanaAlignedProxyErrorCode = {}));
@@ -1,28 +1,28 @@
1
- import BigNumber from "bignumber.js";
1
+ import BN from "bn.js";
2
2
  /**
3
- * Used for token amounts conversion from their Big Number representation to number.
4
- * Get value in the highest units from BigNumber representation of the same value in the smallest units.
5
- * @param {BigNumber} value - Big Number representation of value in the smallest units.
3
+ * Used for conversion of token amounts to their Big Number representation.
4
+ * Get Big Number representation in the smallest units from the same value in the highest units.
5
+ * @param {number} value - Number of tokens you want to convert to its BN representation.
6
6
  * @param {number} decimals - Number of decimals the token has.
7
7
  */
8
- export declare const getNumberFromBigNumber: (bigNum: BigNumber, decimals: number) => number;
8
+ export declare const getBN: (value: number, decimals: number) => BN;
9
9
  /**
10
- * Used for conversion of token amounts to their Big Number representation.
11
- * Get Big Number representation in the smallest units from the same value in the highest units.
12
- * @param {number} value - Number of tokens you want to convert to its BigNumber representation.
10
+ * Used for token amounts conversion from their Big Number representation to number.
11
+ * Get value in the highest units from BN representation of the same value in the smallest units.
12
+ * @param {BN} value - Big Number representation of value in the smallest units.
13
13
  * @param {number} decimals - Number of decimals the token has.
14
14
  */
15
- export declare const getScaledBigNumber: (value: number | string | BigNumber, decimals: number) => BigNumber;
15
+ export declare const getNumberFromBN: (value: BN, decimals: number) => number;
16
16
  /**
17
17
  * Calculate total amount of a Contract including all fees.
18
- * - first we convert fee floating to a BigNumber with up to 4 decimals precision
18
+ * - first we convert fee floating to a BN with up to 4 decimals precision
19
19
  * - then we reverse the fee with `FEE_MULTIPLIER` to safely multiply it by depositedAmount
20
20
  * to receive a total number and not percentage of depositedAmount
21
21
  * @param depositedAmount deposited raw tokens
22
22
  * @param totalFee sum of all fees in percentage as floating number, e.g. 0.99% should be supplied as 0.99
23
23
  * @returns total tokens amount that Contract will retrieve from the Sender
24
24
  */
25
- export declare const calculateTotalAmountToDeposit: (depositedAmount: BigNumber, totalFee: number) => BigNumber;
25
+ export declare const calculateTotalAmountToDeposit: (depositedAmount: BN, totalFee: number) => BN;
26
26
  /**
27
27
  * Used to make on chain calls to the contract and wrap raised errors if any
28
28
  * @param func function that interacts with the contract
@@ -1,29 +1,32 @@
1
- import BigNumber from "bignumber.js";
1
+ import BN from "bn.js";
2
2
  import { ContractError } from "./types.js";
3
3
  const FEE_PRECISION = 4;
4
4
  const FEE_NORMALIZER = 10 ** FEE_PRECISION;
5
- const FEE_MULTIPLIER = BigNumber(10 ** 6);
5
+ const FEE_MULTIPLIER = new BN(10 ** 6);
6
6
  /**
7
- * Used for token amounts conversion from their Big Number representation to number.
8
- * Get value in the highest units from BigNumber representation of the same value in the smallest units.
9
- * @param {BigNumber} value - Big Number representation of value in the smallest units.
7
+ * Used for conversion of token amounts to their Big Number representation.
8
+ * Get Big Number representation in the smallest units from the same value in the highest units.
9
+ * @param {number} value - Number of tokens you want to convert to its BN representation.
10
10
  * @param {number} decimals - Number of decimals the token has.
11
11
  */
12
- export const getNumberFromBigNumber = (bigNum, decimals) => {
13
- return bigNum.div(BigNumber(10).pow(decimals)).toNumber();
12
+ export const getBN = (value, decimals) => {
13
+ const decimalPart = value - Math.trunc(value);
14
+ const integerPart = new BN(Math.trunc(value));
15
+ const decimalE = new BN(decimalPart * 1e9);
16
+ const sum = integerPart.mul(new BN(1e9)).add(decimalE);
17
+ const resultE = sum.mul(new BN(10).pow(new BN(decimals)));
18
+ return resultE.div(new BN(1e9));
14
19
  };
15
20
  /**
16
- * Used for conversion of token amounts to their Big Number representation.
17
- * Get Big Number representation in the smallest units from the same value in the highest units.
18
- * @param {number} value - Number of tokens you want to convert to its BigNumber representation.
21
+ * Used for token amounts conversion from their Big Number representation to number.
22
+ * Get value in the highest units from BN representation of the same value in the smallest units.
23
+ * @param {BN} value - Big Number representation of value in the smallest units.
19
24
  * @param {number} decimals - Number of decimals the token has.
20
25
  */
21
- export const getScaledBigNumber = (value, decimals) => {
22
- return new BigNumber(value).times(new BigNumber(10).pow(decimals));
23
- };
26
+ export const getNumberFromBN = (value, decimals) => value.gt(new BN(2 ** 53 - 1)) ? value.div(new BN(10 ** decimals)).toNumber() : value.toNumber() / 10 ** decimals;
24
27
  /**
25
28
  * Calculate total amount of a Contract including all fees.
26
- * - first we convert fee floating to a BigNumber with up to 4 decimals precision
29
+ * - first we convert fee floating to a BN with up to 4 decimals precision
27
30
  * - then we reverse the fee with `FEE_MULTIPLIER` to safely multiply it by depositedAmount
28
31
  * to receive a total number and not percentage of depositedAmount
29
32
  * @param depositedAmount deposited raw tokens
@@ -31,8 +34,8 @@ export const getScaledBigNumber = (value, decimals) => {
31
34
  * @returns total tokens amount that Contract will retrieve from the Sender
32
35
  */
33
36
  export const calculateTotalAmountToDeposit = (depositedAmount, totalFee) => {
34
- const totalFeeNormalized = BigNumber(totalFee).times(FEE_NORMALIZER);
35
- return depositedAmount.times(totalFeeNormalized.plus(FEE_MULTIPLIER)).div(FEE_MULTIPLIER);
37
+ const totalFeeNormalized = new BN(totalFee * FEE_NORMALIZER);
38
+ return depositedAmount.mul(totalFeeNormalized.add(FEE_MULTIPLIER)).div(FEE_MULTIPLIER);
36
39
  };
37
40
  /**
38
41
  * Used to make on chain calls to the contract and wrap raised errors if any
@@ -1,5 +1,5 @@
1
1
  import { ethers } from "ethers";
2
- import BigNumber from "bignumber.js";
2
+ import BN from "bn.js";
3
3
  import { BaseStreamClient } from "../common/BaseStreamClient.js";
4
4
  import { ICancelData, IChain, ICluster, ICreateMultipleStreamData, ICreateResult, ICreateStreamData, IGetFeesData, IGetAllData, IGetOneData, IFees, IMultiTransactionResult, ITopUpData, ITransactionResult, ITransferData, IUpdateData, IWithdrawData, Stream } from "../common/types.js";
5
5
  export default class EvmStreamClient extends BaseStreamClient {
@@ -25,7 +25,7 @@ export default class EvmStreamClient extends BaseStreamClient {
25
25
  * Returns StreamClient protocol program ID.
26
26
  */
27
27
  getProgramId(): string;
28
- approveTokens(tokenId: string, amount: BigNumber): Promise<void>;
28
+ approveTokens(tokenId: string, amount: BN): Promise<void>;
29
29
  private generateMultiPayloads;
30
30
  private getFeeParams;
31
31
  private formatMetadataId;
@@ -1,5 +1,5 @@
1
1
  import { ethers } from "ethers";
2
- import BigNumber from "bignumber.js";
2
+ import BN from "bn.js";
3
3
  import { toChecksumAddress } from "ethereum-checksum-address";
4
4
  import { BaseStreamClient } from "../common/BaseStreamClient.js";
5
5
  import { IChain, ICluster, StreamDirection, } from "../common/types.js";
@@ -49,7 +49,7 @@ export default class EvmStreamClient extends BaseStreamClient {
49
49
  };
50
50
  const fees = await this.readContract.getWithdrawalFees(...this.getFeeParams(multiParams));
51
51
  const args = (await this.generateMultiPayloads(multiParams))[0];
52
- const sum = streamData.amount.times(BigNumber(BASE_FEE)).div(BigNumber(1000000));
52
+ const sum = streamData.amount.mul(new BN(BASE_FEE)).div(new BN(1000000));
53
53
  await this.approveTokens(streamData.tokenId, sum);
54
54
  const result = await this.writeContract.create(...args, {
55
55
  value: fees.value,
@@ -66,9 +66,9 @@ export default class EvmStreamClient extends BaseStreamClient {
66
66
  const fees = await this.readContract.getWithdrawalFees(...this.getFeeParams(multipleStreamData));
67
67
  const args = await this.generateMultiPayloads(multipleStreamData);
68
68
  const sum = multipleStreamData.recipients
69
- .reduce((acc, v) => acc.plus(v.amount), BigNumber(0))
70
- .times(BigNumber(BASE_FEE))
71
- .div(BigNumber(1000000));
69
+ .reduce((acc, v) => acc.add(v.amount), new BN(0))
70
+ .mul(new BN(BASE_FEE))
71
+ .div(new BN(1000000));
72
72
  await this.approveTokens(multipleStreamData.tokenId, sum);
73
73
  const creationPromises = args.map((item) => this.writeContract.create(...item, { value: fees.value }));
74
74
  const signatures = [];
@@ -120,7 +120,7 @@ export default class EvmStreamClient extends BaseStreamClient {
120
120
  }
121
121
  async topup(topupData) {
122
122
  const fees = await this.readContract.getTopUpWithdrawalFees(topupData.id, topupData.amount.toString());
123
- const sum = topupData.amount.times(BigNumber(BASE_FEE)).div(BigNumber(1000000));
123
+ const sum = topupData.amount.mul(new BN(BASE_FEE)).div(new BN(1000000));
124
124
  const stream = await this.getOne({ id: topupData.id });
125
125
  await this.approveTokens(stream.mint, sum);
126
126
  const result = await this.writeContract.topUp(topupData.id, topupData.amount.toString(), {
@@ -185,7 +185,7 @@ export default class EvmStreamClient extends BaseStreamClient {
185
185
  const tokenContract = new ethers.Contract(tokenId, ercAbi, this.signer);
186
186
  const address = await this.signer.getAddress();
187
187
  const allowance = await tokenContract.allowance(address, this.programId);
188
- if (BigNumber(allowance.toString()).gte(amount)) {
188
+ if (new BN(allowance.toString()).gte(amount)) {
189
189
  return;
190
190
  }
191
191
  const approvalTx = await tokenContract.approve(this.programId, amount.toString());
@@ -1,6 +1,6 @@
1
- import BigNumber from "bignumber.js";
1
+ import BN from "bn.js";
2
2
  import { BigNumber as BigNumberEvm } from "ethers";
3
- import { Stream, StreamType } from "../common/types.js";
3
+ import { LinearStream, StreamType } from "../common/types.js";
4
4
  export interface StreamAbiResult {
5
5
  amount: BigNumberEvm;
6
6
  amount_per_period: BigNumberEvm;
@@ -48,11 +48,11 @@ export interface FeesAbiResult {
48
48
  streamflow_fee: BigNumberEvm;
49
49
  partner_fee: BigNumberEvm;
50
50
  }
51
- export declare class EvmContract implements Stream {
51
+ export declare class EvmContract implements LinearStream {
52
52
  magic: number;
53
53
  version: number;
54
54
  createdAt: number;
55
- withdrawnAmount: BigNumber;
55
+ withdrawnAmount: BN;
56
56
  canceledAt: number;
57
57
  end: number;
58
58
  lastWithdrawnAt: number;
@@ -64,20 +64,20 @@ export declare class EvmContract implements Stream {
64
64
  escrowTokens: string;
65
65
  streamflowTreasury: string;
66
66
  streamflowTreasuryTokens: string;
67
- streamflowFeeTotal: BigNumber;
68
- streamflowFeeWithdrawn: BigNumber;
67
+ streamflowFeeTotal: BN;
68
+ streamflowFeeWithdrawn: BN;
69
69
  streamflowFeePercent: number;
70
- partnerFeeTotal: BigNumber;
71
- partnerFeeWithdrawn: BigNumber;
70
+ partnerFeeTotal: BN;
71
+ partnerFeeWithdrawn: BN;
72
72
  partnerFeePercent: number;
73
73
  partner: string;
74
74
  partnerTokens: string;
75
75
  start: number;
76
- depositedAmount: BigNumber;
76
+ depositedAmount: BN;
77
77
  period: number;
78
- amountPerPeriod: BigNumber;
78
+ amountPerPeriod: BN;
79
79
  cliff: number;
80
- cliffAmount: BigNumber;
80
+ cliffAmount: BN;
81
81
  cancelableBySender: boolean;
82
82
  cancelableByRecipient: boolean;
83
83
  automaticWithdrawal: boolean;
@@ -88,11 +88,11 @@ export declare class EvmContract implements Stream {
88
88
  withdrawalFrequency: number;
89
89
  closed: boolean;
90
90
  currentPauseStart: number;
91
- pauseCumulative: BigNumber;
91
+ pauseCumulative: BN;
92
92
  lastRateChangeTime: number;
93
- fundsUnlockedAtLastRateChange: BigNumber;
93
+ fundsUnlockedAtLastRateChange: BN;
94
94
  type: StreamType;
95
95
  constructor(stream: StreamAbiResult);
96
- unlocked(currentTimestamp: number): BigNumber;
96
+ unlocked(currentTimestamp: number): BN;
97
97
  remaining(decimals: number): number;
98
98
  }
@@ -1,6 +1,6 @@
1
- import BigNumber from "bignumber.js";
1
+ import BN from "bn.js";
2
2
  import { buildStreamType, calculateUnlockedAmount } from "../common/contractUtils.js";
3
- import { getNumberFromBigNumber } from "../common/utils.js";
3
+ import { getNumberFromBN } from "../common/utils.js";
4
4
  export class EvmContract {
5
5
  magic;
6
6
  version;
@@ -49,7 +49,7 @@ export class EvmContract {
49
49
  this.magic = 0;
50
50
  this.version = 0;
51
51
  this.createdAt = stream.created.toNumber();
52
- this.withdrawnAmount = BigNumber(stream.withdrawn.toString());
52
+ this.withdrawnAmount = new BN(stream.withdrawn.toString());
53
53
  this.canceledAt = stream.canceled_at.toNumber();
54
54
  this.end = stream.end.toNumber();
55
55
  this.lastWithdrawnAt = stream.last_withdrawn_at.toNumber();
@@ -61,20 +61,20 @@ export class EvmContract {
61
61
  this.escrowTokens = "";
62
62
  this.streamflowTreasury = "";
63
63
  this.streamflowTreasuryTokens = "";
64
- this.streamflowFeeTotal = BigNumber(stream.fees.streamflow_fee.toString());
65
- this.streamflowFeeWithdrawn = BigNumber(stream.fees.streamflow_fee_withdrawn.toString());
64
+ this.streamflowFeeTotal = new BN(stream.fees.streamflow_fee.toString());
65
+ this.streamflowFeeWithdrawn = new BN(stream.fees.streamflow_fee_withdrawn.toString());
66
66
  this.streamflowFeePercent = stream.fees.streamflow_fee_percentage.toNumber() / 10000;
67
- this.partnerFeeTotal = BigNumber(0);
68
- this.partnerFeeWithdrawn = BigNumber(0);
67
+ this.partnerFeeTotal = new BN(0);
68
+ this.partnerFeeWithdrawn = new BN(0);
69
69
  this.partnerFeePercent = 0;
70
70
  this.partner = "";
71
71
  this.partnerTokens = "";
72
72
  this.start = stream.start.toNumber();
73
- this.depositedAmount = BigNumber(stream.amount.toString());
73
+ this.depositedAmount = new BN(stream.amount.toString());
74
74
  this.period = stream.period.toNumber();
75
- this.amountPerPeriod = BigNumber(stream.amount_per_period.toString());
75
+ this.amountPerPeriod = new BN(stream.amount_per_period.toString());
76
76
  this.cliff = stream.start.toNumber();
77
- this.cliffAmount = BigNumber(stream.cliff_amount.toString());
77
+ this.cliffAmount = new BN(stream.cliff_amount.toString());
78
78
  this.cancelableBySender = stream.meta.cancelable_by_sender;
79
79
  this.cancelableByRecipient = stream.meta.cancelable_by_recipient;
80
80
  this.automaticWithdrawal = stream.meta.automatic_withdrawal;
@@ -85,9 +85,9 @@ export class EvmContract {
85
85
  this.withdrawalFrequency = stream.meta.withdrawal_frequency.toNumber();
86
86
  this.closed = stream.closed;
87
87
  this.currentPauseStart = stream.current_pause_start.toNumber();
88
- this.pauseCumulative = BigNumber(stream.pause_cumulative.toString());
88
+ this.pauseCumulative = new BN(stream.pause_cumulative.toString());
89
89
  this.lastRateChangeTime = stream.last_rate_change_time.toNumber();
90
- this.fundsUnlockedAtLastRateChange = BigNumber(stream.funds_unlocked_at_last_rate_change.toString());
90
+ this.fundsUnlockedAtLastRateChange = new BN(stream.funds_unlocked_at_last_rate_change.toString());
91
91
  this.type = buildStreamType(this);
92
92
  }
93
93
  unlocked(currentTimestamp) {
@@ -97,6 +97,6 @@ export class EvmContract {
97
97
  });
98
98
  }
99
99
  remaining(decimals) {
100
- return getNumberFromBigNumber(this.depositedAmount.minus(this.withdrawnAmount), decimals);
100
+ return getNumberFromBN(this.depositedAmount.sub(this.withdrawnAmount), decimals);
101
101
  }
102
102
  }