@streamflow/stream 6.3.9 → 7.0.0-alpha.2

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 (122) hide show
  1. package/README.md +17 -18
  2. package/dist/{__tests__ → cjs/__tests__}/aptos/streamClient.spec.js +26 -25
  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 +23 -22
  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}/constants.js +2 -2
  11. package/dist/{common → cjs/common}/contractUtils.js +10 -10
  12. package/dist/{common → cjs/common}/utils.js +21 -28
  13. package/dist/{evm → cjs/evm}/StreamClient.js +33 -33
  14. package/dist/cjs/evm/constants.js +39 -0
  15. package/dist/{evm → cjs/evm}/index.js +8 -8
  16. package/dist/{evm → cjs/evm}/types.js +16 -16
  17. package/dist/{evm → cjs/evm}/utils.js +2 -2
  18. package/dist/{solana → cjs/solana}/StreamClient.js +80 -79
  19. package/dist/cjs/solana/constants.js +60 -0
  20. package/dist/cjs/solana/index.js +39 -0
  21. package/dist/{solana → cjs/solana}/instructions.js +33 -28
  22. package/dist/{solana → cjs/solana}/layout.js +2 -2
  23. package/dist/{solana → cjs/solana}/types.js +5 -5
  24. package/dist/{solana → cjs/solana}/utils.js +44 -33
  25. package/dist/{sui → cjs/sui}/StreamClient.js +26 -26
  26. package/dist/cjs/sui/constants.js +43 -0
  27. package/dist/{sui → cjs/sui}/index.js +5 -5
  28. package/dist/{sui → cjs/sui}/types.js +16 -16
  29. package/dist/{sui → cjs/sui}/utils.js +3 -3
  30. package/dist/esm/__tests__/aptos/streamClient.spec.js +106 -0
  31. package/dist/esm/__tests__/common/GenericStreamClient.spec.js +40 -0
  32. package/dist/{aptos → esm/aptos}/StreamClient.d.ts +3 -3
  33. package/dist/esm/aptos/StreamClient.js +226 -0
  34. package/dist/{aptos → esm/aptos}/constants.d.ts +1 -1
  35. package/dist/esm/aptos/constants.js +24 -0
  36. package/dist/{aptos → esm/aptos}/index.d.ts +1 -1
  37. package/dist/{sui/index.d.ts → esm/aptos/index.js} +1 -1
  38. package/dist/{aptos → esm/aptos}/types.d.ts +13 -13
  39. package/dist/esm/aptos/types.js +105 -0
  40. package/dist/esm/aptos/utils.js +30 -0
  41. package/dist/esm/aptos/wallet.js +33 -0
  42. package/dist/{common → esm/common}/BaseStreamClient.d.ts +1 -1
  43. package/dist/esm/common/BaseStreamClient.js +19 -0
  44. package/dist/{common → esm/common}/GenericStreamClient.d.ts +2 -2
  45. package/dist/esm/common/GenericStreamClient.js +101 -0
  46. package/dist/esm/common/constants.d.ts +3 -0
  47. package/dist/esm/common/constants.js +3 -0
  48. package/dist/{common → esm/common}/contractUtils.d.ts +15 -15
  49. package/dist/esm/common/contractUtils.js +42 -0
  50. package/dist/{common → esm/common}/types.d.ts +19 -19
  51. package/dist/esm/common/types.js +106 -0
  52. package/dist/{common → esm/common}/utils.d.ts +11 -12
  53. package/dist/esm/common/utils.js +56 -0
  54. package/dist/{evm → esm/evm}/StreamClient.d.ts +4 -4
  55. package/dist/esm/evm/StreamClient.js +237 -0
  56. package/dist/esm/evm/abi.js +1255 -0
  57. package/dist/{evm → esm/evm}/constants.d.ts +1 -1
  58. package/dist/esm/evm/constants.js +35 -0
  59. package/dist/esm/evm/ercAbi.js +222 -0
  60. package/dist/esm/evm/index.d.ts +5 -0
  61. package/dist/esm/evm/index.js +5 -0
  62. package/dist/{evm → esm/evm}/types.d.ts +38 -38
  63. package/dist/esm/evm/types.js +102 -0
  64. package/dist/esm/evm/utils.js +8 -0
  65. package/dist/esm/index.js +9 -0
  66. package/dist/{solana → esm/solana}/StreamClient.d.ts +4 -4
  67. package/dist/esm/solana/StreamClient.js +695 -0
  68. package/dist/{solana → esm/solana}/constants.d.ts +2 -0
  69. package/dist/esm/solana/constants.js +56 -0
  70. package/dist/esm/solana/index.d.ts +6 -0
  71. package/dist/esm/solana/index.js +6 -0
  72. package/dist/esm/solana/instructionTypes.js +1 -0
  73. package/dist/{solana → esm/solana}/instructions.d.ts +18 -18
  74. package/dist/esm/solana/instructions.js +257 -0
  75. package/dist/{solana → esm/solana}/layout.d.ts +2 -1
  76. package/dist/esm/solana/layout.js +155 -0
  77. package/dist/esm/solana/timelockIDL.js +420 -0
  78. package/dist/{solana → esm/solana}/types.d.ts +38 -37
  79. package/dist/esm/solana/types.js +101 -0
  80. package/dist/{solana → esm/solana}/utils.d.ts +4 -1
  81. package/dist/esm/solana/utils.js +123 -0
  82. package/dist/{sui → esm/sui}/StreamClient.d.ts +3 -3
  83. package/dist/esm/sui/StreamClient.js +403 -0
  84. package/dist/{sui → esm/sui}/constants.d.ts +1 -1
  85. package/dist/esm/sui/constants.js +39 -0
  86. package/dist/esm/sui/index.d.ts +4 -0
  87. package/dist/esm/sui/index.js +4 -0
  88. package/dist/{sui → esm/sui}/types.d.ts +13 -13
  89. package/dist/esm/sui/types.js +104 -0
  90. package/dist/{sui → esm/sui}/utils.d.ts +1 -1
  91. package/dist/esm/sui/utils.js +23 -0
  92. package/dist/{sui → esm/sui}/wallet.d.ts +1 -1
  93. package/dist/esm/sui/wallet.js +38 -0
  94. package/package.json +37 -35
  95. package/dist/__tests__/common/GenericStreamClient.spec.js +0 -44
  96. package/dist/aptos/constants.js +0 -28
  97. package/dist/common/constants.d.ts +0 -3
  98. package/dist/evm/constants.js +0 -39
  99. package/dist/evm/index.d.ts +0 -5
  100. package/dist/solana/constants.js +0 -59
  101. package/dist/solana/index.d.ts +0 -7
  102. package/dist/solana/index.js +0 -43
  103. package/dist/sui/constants.js +0 -43
  104. /package/dist/{aptos → cjs/aptos}/wallet.js +0 -0
  105. /package/dist/{common → cjs/common}/BaseStreamClient.js +0 -0
  106. /package/dist/{common → cjs/common}/types.js +0 -0
  107. /package/dist/{evm → cjs/evm}/abi.js +0 -0
  108. /package/dist/{evm → cjs/evm}/ercAbi.js +0 -0
  109. /package/dist/{index.js → cjs/index.js} +0 -0
  110. /package/dist/{solana → cjs/solana}/instructionTypes.js +0 -0
  111. /package/dist/{solana → cjs/solana}/timelockIDL.js +0 -0
  112. /package/dist/{sui → cjs/sui}/wallet.js +0 -0
  113. /package/dist/{__tests__ → esm/__tests__}/aptos/streamClient.spec.d.ts +0 -0
  114. /package/dist/{__tests__ → esm/__tests__}/common/GenericStreamClient.spec.d.ts +0 -0
  115. /package/dist/{aptos → esm/aptos}/utils.d.ts +0 -0
  116. /package/dist/{aptos → esm/aptos}/wallet.d.ts +0 -0
  117. /package/dist/{evm → esm/evm}/abi.d.ts +0 -0
  118. /package/dist/{evm → esm/evm}/ercAbi.d.ts +0 -0
  119. /package/dist/{evm → esm/evm}/utils.d.ts +0 -0
  120. /package/dist/{index.d.ts → esm/index.d.ts} +0 -0
  121. /package/dist/{solana → esm/solana}/instructionTypes.d.ts +0 -0
  122. /package/dist/{solana → esm/solana}/timelockIDL.d.ts +0 -0
@@ -0,0 +1,106 @@
1
+ import { BigNumber } from "bignumber.js";
2
+ import { describe, expect, test, beforeEach, vi } from "vitest";
3
+ import AptosStreamClient from "../../aptos/StreamClient.js";
4
+ import { ICluster } from "../../common/types.js";
5
+ describe("AptosStreamClient", () => {
6
+ describe("init", () => {
7
+ test("should successfully create AptosStreamInstance", () => {
8
+ const instance = new AptosStreamClient(ICluster.Devnet);
9
+ expect(instance).toBeInstanceOf(AptosStreamClient);
10
+ });
11
+ test("should correctly set passed params", () => {
12
+ const pid = "PID";
13
+ const gas = "30";
14
+ const instance = new AptosStreamClient("https://cluster", ICluster.Devnet, gas, pid);
15
+ expect(instance.getMaxGas()).toEqual({ max_gas_amount: gas });
16
+ expect(instance.getProgramId()).toBe(pid);
17
+ });
18
+ });
19
+ describe("create", () => {
20
+ const pid = "PID";
21
+ const gas = "30";
22
+ let instance = new AptosStreamClient("https://cluster", ICluster.Devnet, gas, pid);
23
+ beforeEach(() => {
24
+ instance = new AptosStreamClient("https://cluster", ICluster.Devnet, gas, pid);
25
+ });
26
+ test("should create a stream with passed parameters", async () => {
27
+ // Arrange
28
+ const mockSigner = vi.fn();
29
+ const mockWallet = {
30
+ signAndSubmitTransaction: mockSigner,
31
+ account: { address: "" },
32
+ };
33
+ const mockRecipient = {
34
+ recipient: "0xtest",
35
+ amount: BigNumber(1000),
36
+ name: "test name",
37
+ cliffAmount: 1,
38
+ amountPerPeriod: BigNumber(1),
39
+ };
40
+ const mockData = {
41
+ period: 100,
42
+ start: 10,
43
+ cliff: 12,
44
+ cancelableBySender: true,
45
+ cancelableByRecipient: false,
46
+ transferableBySender: false,
47
+ transferableByRecipient: true,
48
+ canTopup: true,
49
+ automaticWithdrawal: false,
50
+ withdrawalFrequency: 10,
51
+ tokenId: "0xtest::token::Token",
52
+ canPause: false,
53
+ canUpdateRate: false,
54
+ };
55
+ const mockReturn = "hash";
56
+ mockSigner.mockResolvedValue({ hash: mockReturn });
57
+ // Act
58
+ const result = await instance.create({
59
+ ...mockData,
60
+ ...mockRecipient,
61
+ }, { senderWallet: mockWallet });
62
+ // Assert
63
+ expect(mockSigner).toHaveBeenCalledTimes(1);
64
+ const callParam = mockSigner.mock.calls[0][0];
65
+ const args = callParam.arguments;
66
+ delete callParam.arguments;
67
+ args.shift();
68
+ expect(args).toEqual([
69
+ mockRecipient.amount.toString(),
70
+ 100,
71
+ mockRecipient.amountPerPeriod.toString(),
72
+ 10,
73
+ mockRecipient.cliffAmount.toString(),
74
+ true,
75
+ false,
76
+ false,
77
+ true,
78
+ true,
79
+ false,
80
+ false,
81
+ false,
82
+ 10,
83
+ "test name",
84
+ "0xtest",
85
+ ]);
86
+ expect(callParam).toEqual({
87
+ function: "PID::protocol::create",
88
+ type: "create",
89
+ type_arguments: ["0xtest::token::Token"],
90
+ });
91
+ expect(result.txId).toEqual(mockReturn);
92
+ });
93
+ });
94
+ // describe("createMultiple", () => {
95
+ // });
96
+ // describe("withdraw", () => {
97
+ // });
98
+ // describe("cancel", () => {
99
+ // });
100
+ // describe("transfer", () => {
101
+ // });
102
+ // describe("topup", () => {
103
+ // });
104
+ // describe("getOne", () => {
105
+ // });
106
+ });
@@ -0,0 +1,40 @@
1
+ import { Wallet } from "ethers";
2
+ import { describe, expect, test } from "vitest";
3
+ import { StreamflowAptos, StreamflowEVM, StreamflowSolana, StreamflowSui } from "../../index.js";
4
+ import { default as GenericStreamClient, } from "../../common/GenericStreamClient.js";
5
+ import { IChain, ICluster } from "../../common/types.js";
6
+ describe("GenericStreamClient", () => {
7
+ describe.each([
8
+ {
9
+ chain: IChain.Aptos,
10
+ cluster: ICluster.Devnet,
11
+ clusterUrl: "https://cluster",
12
+ expected: StreamflowAptos.AptosStreamClient,
13
+ },
14
+ {
15
+ chain: IChain.Solana,
16
+ cluster: ICluster.Devnet,
17
+ clusterUrl: "https://cluster",
18
+ expected: StreamflowSolana.SolanaStreamClient,
19
+ },
20
+ {
21
+ chain: IChain.Ethereum,
22
+ cluster: ICluster.Devnet,
23
+ clusterUrl: "https://cluster",
24
+ signer: Wallet.createRandom(),
25
+ expected: StreamflowEVM.EvmStreamClient,
26
+ },
27
+ {
28
+ chain: IChain.Sui,
29
+ cluster: ICluster.Devnet,
30
+ clusterUrl: "https://cluster",
31
+ expected: StreamflowSui.SuiStreamClient,
32
+ },
33
+ ])(".init($chain)", (config) => {
34
+ test("should successfully create GenericStreamClient instance", () => {
35
+ const instance = new GenericStreamClient(config);
36
+ expect(instance).toBeInstanceOf(GenericStreamClient);
37
+ expect(instance.nativeStreamClient).toBeInstanceOf(config.expected);
38
+ });
39
+ });
40
+ });
@@ -1,6 +1,6 @@
1
- import { BaseStreamClient } from "../common/BaseStreamClient";
2
- import { ICancelData, ICluster, ICreateMultipleStreamData, ICreateResult, ICreateStreamData, IGetFeesData, IGetOneData, IFees, IMultiTransactionResult, ITopUpData, ITransactionResult, ITransferData, IUpdateData, IWithdrawData } from "../common/types";
3
- import { Contract, ICreateStreamAptosExt, ITransactionAptosExt } from "./types";
1
+ import { BaseStreamClient } from "../common/BaseStreamClient.js";
2
+ import { ICancelData, ICluster, ICreateMultipleStreamData, ICreateResult, ICreateStreamData, IGetFeesData, IGetOneData, IFees, IMultiTransactionResult, ITopUpData, ITransactionResult, ITransferData, IUpdateData, IWithdrawData } from "../common/types.js";
3
+ import { Contract, ICreateStreamAptosExt, ITransactionAptosExt } from "./types.js";
4
4
  export default class AptosStreamClient extends BaseStreamClient {
5
5
  private programId;
6
6
  private maxGas;
@@ -0,0 +1,226 @@
1
+ import { AptosAccount, AptosClient } from "aptos";
2
+ import { BaseStreamClient } from "../common/BaseStreamClient.js";
3
+ import { ICluster, } from "../common/types.js";
4
+ import { APTOS_PROGRAM_IDS } from "./constants.js";
5
+ import { Contract, } from "./types.js";
6
+ import { AptosWalletWrapper } from "./wallet.js";
7
+ import { extractAptosErrorCode } from "./utils.js";
8
+ import { WITHDRAW_AVAILABLE_AMOUNT } from "../common/constants.js";
9
+ export default class AptosStreamClient extends BaseStreamClient {
10
+ programId;
11
+ maxGas;
12
+ client;
13
+ constructor(clusterUrl, cluster = ICluster.Mainnet, maxGas = "10000", programId) {
14
+ super();
15
+ this.programId = programId ? programId : APTOS_PROGRAM_IDS[cluster];
16
+ this.maxGas = maxGas;
17
+ this.client = new AptosClient(clusterUrl);
18
+ }
19
+ /**
20
+ * Creates a new stream/vesting contract.
21
+ */
22
+ async create(streamData, { senderWallet }) {
23
+ const wallet = new AptosWalletWrapper(senderWallet, this.client);
24
+ const [metadataId, payload] = this.generateMultiPayloads({
25
+ ...streamData,
26
+ recipients: [{ ...streamData }],
27
+ }, wallet)[0];
28
+ const hash = await wallet.signAndSubmitTransaction(payload);
29
+ return { ixs: [payload], txId: hash, metadataId };
30
+ }
31
+ /**
32
+ * Creates multiple stream/vesting contracts.
33
+ */
34
+ async createMultiple(multipleStreamData, { senderWallet }) {
35
+ const wallet = new AptosWalletWrapper(senderWallet, this.client);
36
+ const payloads = this.generateMultiPayloads(multipleStreamData, wallet);
37
+ const txs = [];
38
+ const metadatas = [];
39
+ const metadataToRecipient = {};
40
+ const errors = [];
41
+ for (let i = 0; i < payloads.length; i++) {
42
+ const [metadataId, payload] = payloads[i];
43
+ const recipient = multipleStreamData.recipients[i];
44
+ try {
45
+ const hash = await wallet.signAndSubmitTransaction(payload);
46
+ txs.push(hash);
47
+ }
48
+ catch (e) {
49
+ errors.push({
50
+ error: e?.toString() ?? "Unknown error!",
51
+ recipient: recipient.recipient,
52
+ });
53
+ }
54
+ finally {
55
+ metadatas.push(metadataId);
56
+ metadataToRecipient[metadataId] = recipient;
57
+ }
58
+ }
59
+ return {
60
+ txs,
61
+ metadatas,
62
+ metadataToRecipient,
63
+ errors,
64
+ };
65
+ }
66
+ /**
67
+ * Attempts withdrawing from the specified stream.
68
+ */
69
+ async withdraw({ id, amount = WITHDRAW_AVAILABLE_AMOUNT }, { senderWallet, tokenId }) {
70
+ const payload = {
71
+ type: "withdraw",
72
+ function: `${this.programId}::protocol::withdraw`,
73
+ type_arguments: [tokenId],
74
+ arguments: [id, amount.toString()],
75
+ };
76
+ const wallet = new AptosWalletWrapper(senderWallet, this.client);
77
+ const hash = await wallet.signAndSubmitTransaction(payload);
78
+ return { ixs: [payload], txId: hash };
79
+ }
80
+ /**
81
+ * Attempts canceling the specified stream.
82
+ */
83
+ async cancel(cancelData, { senderWallet, tokenId }) {
84
+ const payload = {
85
+ type: "cancel",
86
+ function: `${this.programId}::protocol::cancel`,
87
+ type_arguments: [tokenId],
88
+ arguments: [cancelData.id],
89
+ };
90
+ const wallet = new AptosWalletWrapper(senderWallet, this.client);
91
+ const hash = await wallet.signAndSubmitTransaction(payload);
92
+ return { ixs: [payload], txId: hash };
93
+ }
94
+ /**
95
+ * Attempts changing the stream/vesting contract's recipient (effectively transferring the stream/vesting contract).
96
+ */
97
+ async transfer(transferData, { senderWallet, tokenId }) {
98
+ const payload = {
99
+ type: "transfer",
100
+ function: `${this.programId}::protocol::transfer`,
101
+ type_arguments: [tokenId],
102
+ arguments: [transferData.id, transferData.newRecipient],
103
+ };
104
+ const wallet = new AptosWalletWrapper(senderWallet, this.client);
105
+ const hash = await wallet.signAndSubmitTransaction(payload);
106
+ return { ixs: [payload], txId: hash };
107
+ }
108
+ /**
109
+ * Tops up stream account with specified amount.
110
+ */
111
+ async topup(topupData, { senderWallet, tokenId }) {
112
+ const payload = {
113
+ type: "topup",
114
+ function: `${this.programId}::protocol::topup`,
115
+ type_arguments: [tokenId],
116
+ arguments: [topupData.id, topupData.amount.toString()],
117
+ };
118
+ const wallet = new AptosWalletWrapper(senderWallet, this.client);
119
+ const hash = await wallet.signAndSubmitTransaction(payload);
120
+ return { ixs: [payload], txId: hash };
121
+ }
122
+ /**
123
+ * Fetch stream data by its id (address).
124
+ */
125
+ async getOne({ id }) {
126
+ const contractResources = await this.client.getAccountResources(id);
127
+ const contract = contractResources.find((r) => r.type.includes("protocol::Contract"));
128
+ if (!contract) {
129
+ throw new Error(`Contract with id ${id} could not be found!`);
130
+ }
131
+ const tokenIdMatch = contract.type.match(/0x[0-9a-f]+::protocol::Contract<(.*)>/);
132
+ const tokenId = tokenIdMatch?.[1] ?? "";
133
+ const { data } = contract;
134
+ return new Contract(data, tokenId);
135
+ }
136
+ async get() {
137
+ throw new Error("Get all method is not supported for Aptos chain!");
138
+ }
139
+ /**
140
+ * Attempts updating the stream auto withdrawal params and amount per period
141
+ */
142
+ async update(updateData, { senderWallet, tokenId }) {
143
+ const wallet = new AptosWalletWrapper(senderWallet, this.client);
144
+ const payload = {
145
+ type: "update",
146
+ function: `${this.programId}::protocol::update`,
147
+ type_arguments: [tokenId],
148
+ arguments: [
149
+ updateData.id,
150
+ updateData.enableAutomaticWithdrawal ? [true] : [],
151
+ updateData.withdrawFrequency ? [updateData.withdrawFrequency.toString()] : [],
152
+ updateData.amountPerPeriod ? [updateData.amountPerPeriod.toString()] : [],
153
+ ],
154
+ };
155
+ const hash = await wallet.signAndSubmitTransaction(payload);
156
+ return { ixs: [payload], txId: hash };
157
+ }
158
+ async getFees({ address }) {
159
+ const resource = await this.client.getAccountResource(this.programId, `${this.programId}::fees::FeeTable`);
160
+ const data = resource.data;
161
+ const value = await this.client.getTableItem(data.values.handle, {
162
+ key_type: "address",
163
+ key: address,
164
+ value_type: "u64",
165
+ });
166
+ if (!value) {
167
+ return null;
168
+ }
169
+ return { streamflowFee: Number(value) / 100, partnerFee: 0 };
170
+ }
171
+ async getDefaultStreamflowFee() {
172
+ const resource = await this.client.getAccountResource(this.programId, `${this.programId}::admin::ConfigV2`);
173
+ return Number(resource.data.streamflow_fees) / 100;
174
+ }
175
+ extractErrorCode(err) {
176
+ return extractAptosErrorCode(err.toString() ?? "Unknown error!");
177
+ }
178
+ /**
179
+ * Returns StreamClient protocol program ID.
180
+ */
181
+ getProgramId() {
182
+ return this.programId;
183
+ }
184
+ getMaxGas() {
185
+ return { max_gas_amount: this.maxGas };
186
+ }
187
+ // Utility function to prepare transaction payloads for multiple recipients.
188
+ generateMultiPayloads(multipleStreamData, wallet) {
189
+ return multipleStreamData.recipients.map((recipient) => {
190
+ const acc = new AptosAccount(); // Generate random address as seeds for deriving "escrow" account
191
+ const seeds = acc.address();
192
+ const encoder = new TextEncoder();
193
+ // A workaround to pass a String in seeds because different wallets seem
194
+ // to serialize vector<u8> differently and String should be safer that Uin8Array
195
+ const actualSeeds = encoder.encode(seeds.hex());
196
+ const metadataId = AptosAccount.getResourceAccountAddress(wallet.address, actualSeeds);
197
+ return [
198
+ metadataId.toString(),
199
+ {
200
+ type: "create",
201
+ function: `${this.programId}::protocol::create`,
202
+ type_arguments: [multipleStreamData.tokenId],
203
+ arguments: [
204
+ seeds.hex(),
205
+ recipient.amount.toString(),
206
+ multipleStreamData.period,
207
+ recipient.amountPerPeriod.toString(),
208
+ multipleStreamData.start,
209
+ recipient.cliffAmount.toString(),
210
+ multipleStreamData.cancelableBySender,
211
+ multipleStreamData.cancelableByRecipient,
212
+ multipleStreamData.transferableBySender,
213
+ multipleStreamData.transferableByRecipient,
214
+ multipleStreamData.canTopup,
215
+ multipleStreamData.canPause || false,
216
+ multipleStreamData.canUpdateRate || false,
217
+ multipleStreamData.automaticWithdrawal || false,
218
+ multipleStreamData.withdrawalFrequency || 0,
219
+ recipient.name,
220
+ recipient.recipient,
221
+ ],
222
+ },
223
+ ];
224
+ });
225
+ }
226
+ }
@@ -1,4 +1,4 @@
1
- import { ICluster } from "../common/types";
1
+ import { ICluster } from "../common/types.js";
2
2
  export declare const APTOS_PROGRAM_IDS: Record<ICluster, string>;
3
3
  export declare const APTOS_ERROR_MATCH_REGEX: RegExp;
4
4
  export declare const APTOS_MODULE_ERROR_MAP: {
@@ -0,0 +1,24 @@
1
+ import { ContractErrorCode, ICluster } from "../common/types.js";
2
+ export const APTOS_PROGRAM_IDS = {
3
+ [ICluster.Mainnet]: "0x9009d93d52576bf9ac6dc6cf10b870610bcb316342fef6eff80662fbbfce51b0",
4
+ // TODO: Add a correct programID for Test net
5
+ [ICluster.Devnet]: "0xc6737de143d91b2f99a7e490d4f8348fdfa3bdd1eb8737a27d0455f8a3625688",
6
+ [ICluster.Testnet]: "0xc6737de143d91b2f99a7e490d4f8348fdfa3bdd1eb8737a27d0455f8a3625688",
7
+ [ICluster.Local]: "0x9009d93d52576bf9ac6dc6cf10b870610bcb316342fef6eff80662fbbfce51b0",
8
+ };
9
+ export const APTOS_ERROR_MATCH_REGEX = /Move abort in [a-z0-9]+::([^:]+): ([A-Z_]+)/;
10
+ export const APTOS_MODULE_ERROR_MAP = {
11
+ protocol: {
12
+ 1: ContractErrorCode.ECONTRACT_NOT_INIT,
13
+ 2: ContractErrorCode.EBAD_AMOUNT,
14
+ 3: ContractErrorCode.ENO_PERMISSIONS,
15
+ 4: ContractErrorCode.EBADINPUT,
16
+ 5: ContractErrorCode.ECLOSED,
17
+ 6: ContractErrorCode.EBAD_INPUT_AMOUNT_PER_PERIOD,
18
+ 8: ContractErrorCode.EBAD_INPUT_UPDATE_RATE,
19
+ 9: ContractErrorCode.EBAD_INPUT_CLIFF_AMOUNT,
20
+ 10: ContractErrorCode.EBAD_INPUT_PERIOD,
21
+ 11: ContractErrorCode.EBAD_INPUT_START,
22
+ 12: ContractErrorCode.ENO_RECIPIENT_COIN_ADDRESS,
23
+ },
24
+ };
@@ -1,4 +1,4 @@
1
- export { default, default as AptosStreamClient } from "./StreamClient";
1
+ export { default, default as AptosStreamClient } from "./StreamClient.js";
2
2
  export * from "./utils";
3
3
  export * from "./types";
4
4
  export * as constants from "./constants";
@@ -1,4 +1,4 @@
1
- export { default as SuiStreamClient } from "./StreamClient";
1
+ export { default, default as AptosStreamClient } from "./StreamClient.js";
2
2
  export * from "./utils";
3
3
  export * from "./types";
4
4
  export * as constants from "./constants";
@@ -1,7 +1,7 @@
1
1
  import { AptosAccount } from "aptos";
2
2
  import { WalletContextState } from "@manahippo/aptos-wallet-adapter";
3
- import BN from "bn.js";
4
- import { Stream, StreamType } from "../common/types";
3
+ import BigNumber from "bignumber.js";
4
+ import { Stream, StreamType } from "../common/types.js";
5
5
  export interface ICreateStreamAptosExt {
6
6
  senderWallet: WalletContextState | AptosAccount;
7
7
  }
@@ -63,7 +63,7 @@ export declare class Contract implements Stream {
63
63
  magic: number;
64
64
  version: number;
65
65
  createdAt: number;
66
- withdrawnAmount: BN;
66
+ withdrawnAmount: BigNumber;
67
67
  canceledAt: number;
68
68
  end: number;
69
69
  lastWithdrawnAt: number;
@@ -75,20 +75,20 @@ export declare class Contract implements Stream {
75
75
  escrowTokens: string;
76
76
  streamflowTreasury: string;
77
77
  streamflowTreasuryTokens: string;
78
- streamflowFeeTotal: BN;
79
- streamflowFeeWithdrawn: BN;
78
+ streamflowFeeTotal: BigNumber;
79
+ streamflowFeeWithdrawn: BigNumber;
80
80
  streamflowFeePercent: number;
81
- partnerFeeTotal: BN;
82
- partnerFeeWithdrawn: BN;
81
+ partnerFeeTotal: BigNumber;
82
+ partnerFeeWithdrawn: BigNumber;
83
83
  partnerFeePercent: number;
84
84
  partner: string;
85
85
  partnerTokens: string;
86
86
  start: number;
87
- depositedAmount: BN;
87
+ depositedAmount: BigNumber;
88
88
  period: number;
89
- amountPerPeriod: BN;
89
+ amountPerPeriod: BigNumber;
90
90
  cliff: number;
91
- cliffAmount: BN;
91
+ cliffAmount: BigNumber;
92
92
  cancelableBySender: boolean;
93
93
  cancelableByRecipient: boolean;
94
94
  automaticWithdrawal: boolean;
@@ -99,11 +99,11 @@ export declare class Contract implements Stream {
99
99
  withdrawalFrequency: number;
100
100
  closed: boolean;
101
101
  currentPauseStart: number;
102
- pauseCumulative: BN;
102
+ pauseCumulative: BigNumber;
103
103
  lastRateChangeTime: number;
104
- fundsUnlockedAtLastRateChange: BN;
104
+ fundsUnlockedAtLastRateChange: BigNumber;
105
105
  type: StreamType;
106
106
  constructor(stream: StreamResource, tokenId: string);
107
- unlocked(currentTimestamp: number): BN;
107
+ unlocked(currentTimestamp: number): BigNumber;
108
108
  remaining(decimals: number): number;
109
109
  }
@@ -0,0 +1,105 @@
1
+ import BigNumber from "bignumber.js";
2
+ import { Buffer } from "buffer";
3
+ import { buildStreamType, calculateUnlockedAmount } from "../common/contractUtils.js";
4
+ import { normalizeAptosAddress } from "./utils.js";
5
+ import { getNumberFromBigNumber } from "../common/utils.js";
6
+ export class Contract {
7
+ magic;
8
+ version;
9
+ createdAt;
10
+ withdrawnAmount;
11
+ canceledAt;
12
+ end;
13
+ lastWithdrawnAt;
14
+ sender;
15
+ senderTokens;
16
+ recipient;
17
+ recipientTokens;
18
+ mint;
19
+ escrowTokens;
20
+ streamflowTreasury;
21
+ streamflowTreasuryTokens;
22
+ streamflowFeeTotal;
23
+ streamflowFeeWithdrawn;
24
+ streamflowFeePercent;
25
+ partnerFeeTotal;
26
+ partnerFeeWithdrawn;
27
+ partnerFeePercent;
28
+ partner;
29
+ partnerTokens;
30
+ start;
31
+ depositedAmount;
32
+ period;
33
+ amountPerPeriod;
34
+ cliff;
35
+ cliffAmount;
36
+ cancelableBySender;
37
+ cancelableByRecipient;
38
+ automaticWithdrawal;
39
+ transferableBySender;
40
+ transferableByRecipient;
41
+ canTopup;
42
+ name;
43
+ withdrawalFrequency;
44
+ closed;
45
+ currentPauseStart;
46
+ pauseCumulative;
47
+ lastRateChangeTime;
48
+ fundsUnlockedAtLastRateChange;
49
+ type;
50
+ constructor(stream, tokenId) {
51
+ this.magic = 0;
52
+ this.version = 0;
53
+ this.createdAt = parseInt(stream.created);
54
+ this.withdrawnAmount = BigNumber(stream.withdrawn);
55
+ this.canceledAt = parseInt(stream.canceled_at);
56
+ this.end = parseInt(stream.end);
57
+ this.lastWithdrawnAt = parseInt(stream.last_withdrawn_at);
58
+ this.sender = normalizeAptosAddress(stream.sender);
59
+ this.senderTokens = normalizeAptosAddress(stream.sender);
60
+ this.recipient = normalizeAptosAddress(stream.recipient);
61
+ this.recipientTokens = normalizeAptosAddress(stream.recipient);
62
+ this.mint = tokenId;
63
+ this.escrowTokens = "";
64
+ this.streamflowTreasury = "";
65
+ this.streamflowTreasuryTokens = "";
66
+ this.streamflowFeeTotal = BigNumber(0);
67
+ this.streamflowFeeWithdrawn = BigNumber(0);
68
+ this.streamflowFeePercent = parseInt(stream.fees.streamflow_fee_percentage) / 10000;
69
+ this.partnerFeeTotal = BigNumber(0);
70
+ this.partnerFeeWithdrawn = BigNumber(0);
71
+ this.partnerFeePercent = 0;
72
+ this.partner = "";
73
+ this.partnerTokens = "";
74
+ this.start = parseInt(stream.start);
75
+ this.depositedAmount = BigNumber(stream.amount);
76
+ this.period = parseInt(stream.period);
77
+ this.amountPerPeriod = BigNumber(stream.amount_per_period);
78
+ this.cliff = parseInt(stream.start);
79
+ this.cliffAmount = BigNumber(stream.cliff_amount);
80
+ this.cancelableBySender = stream.meta.cancelable_by_sender;
81
+ this.cancelableByRecipient = stream.meta.cancelable_by_recipient;
82
+ this.automaticWithdrawal = stream.meta.automatic_withdrawal;
83
+ this.transferableBySender = stream.meta.transferable_by_sender;
84
+ this.transferableByRecipient = stream.meta.transferable_by_recipient;
85
+ this.canTopup = stream.meta.can_topup;
86
+ const name = stream.meta.contract_name.replace("0x", "");
87
+ this.name = Buffer.from(name, "hex").toString("utf8");
88
+ this.withdrawalFrequency = parseInt(stream.meta.withdrawal_frequency);
89
+ this.closed = stream.closed;
90
+ this.currentPauseStart = parseInt(stream.current_pause_start);
91
+ this.pauseCumulative = BigNumber(stream.pause_cumulative);
92
+ this.lastRateChangeTime = parseInt(stream.last_rate_change_time);
93
+ this.fundsUnlockedAtLastRateChange = BigNumber(stream.funds_unlocked_at_last_rate_change);
94
+ this.type = buildStreamType(this);
95
+ }
96
+ unlocked(currentTimestamp) {
97
+ return calculateUnlockedAmount({
98
+ ...this,
99
+ currentTimestamp,
100
+ });
101
+ }
102
+ remaining(decimals) {
103
+ return getNumberFromBigNumber(this.depositedAmount.minus(this.withdrawnAmount), decimals);
104
+ }
105
+ }
@@ -0,0 +1,30 @@
1
+ import { APTOS_ERROR_MATCH_REGEX } from "./constants.js";
2
+ export function extractAptosErrorCode(errorText) {
3
+ const match = APTOS_ERROR_MATCH_REGEX.exec(errorText);
4
+ if (!match) {
5
+ return null;
6
+ }
7
+ return match[2];
8
+ }
9
+ function isAddressSpecial(address) {
10
+ if (address.length === 3) {
11
+ return true;
12
+ }
13
+ return false;
14
+ }
15
+ /**
16
+ * Aptos has inconsistencies in how it returns addresses.
17
+ * This method normalizes them to be 64+2(0x...) characters long, or leaves it as SPECIAL ADDRESS (0x0 - 0xf inclusive)
18
+ * Per this: https://github.com/aptos-labs/aptos-ts-sdk/blob/main/src/core/accountAddress.ts#L115
19
+ * */
20
+ export function normalizeAptosAddress(address) {
21
+ if (isAddressSpecial(address)) {
22
+ return address;
23
+ }
24
+ const length = address.length;
25
+ if (length === 66 || length < 3) {
26
+ return address;
27
+ }
28
+ const missingZeros = 66 - length;
29
+ return address.slice(0, 2) + "0".repeat(missingZeros) + address.slice(2);
30
+ }
@@ -0,0 +1,33 @@
1
+ import { AptosAccount, TransactionBuilderRemoteABI } from "aptos";
2
+ export class AptosWalletWrapper {
3
+ wallet;
4
+ address;
5
+ client;
6
+ constructor(wallet, client) {
7
+ this.client = client;
8
+ this.wallet = wallet;
9
+ if (wallet instanceof AptosAccount) {
10
+ this.address = wallet.address();
11
+ }
12
+ else {
13
+ this.address = wallet.account.address;
14
+ }
15
+ }
16
+ async signAndSubmitTransaction(input) {
17
+ if (this.wallet instanceof AptosAccount) {
18
+ const builder = new TransactionBuilderRemoteABI(this.client, {
19
+ sender: this.address,
20
+ });
21
+ const rawTxn = await builder.build(input.function, input.type_arguments, input.arguments);
22
+ const res = await this.client.simulateTransaction(this.wallet, rawTxn);
23
+ if (!res[0].success) {
24
+ throw new Error(`Transaction Simulation failed: ${JSON.stringify(res)}`);
25
+ }
26
+ const signedTx = await this.client.signTransaction(this.wallet, rawTxn);
27
+ const tx = await this.client.submitSignedBCSTransaction(signedTx);
28
+ await this.client.waitForTransaction(tx.hash);
29
+ return tx.hash;
30
+ }
31
+ return (await this.wallet.signAndSubmitTransaction(input)).hash;
32
+ }
33
+ }
@@ -1,4 +1,4 @@
1
- import { ICreateStreamData, ICreateMultipleStreamData, IFees, IWithdrawData, ICancelData, ITransferData, ITopUpData, IGetFeesData, IGetOneData, IUpdateData, ITransactionResult, IMultiTransactionResult, ICreateResult, IGetAllData, Stream } from "./types";
1
+ import { ICancelData, ICreateMultipleStreamData, ICreateResult, ICreateStreamData, IFees, IGetAllData, IGetFeesData, IGetOneData, IMultiTransactionResult, ITopUpData, ITransactionResult, ITransferData, IUpdateData, IWithdrawData, Stream } from "./types.js";
2
2
  export declare abstract class BaseStreamClient {
3
3
  abstract create(streamData: ICreateStreamData, chainSpecificParams: any): Promise<ICreateResult>;
4
4
  abstract createMultiple(multipleStreamData: ICreateMultipleStreamData, chainSpecificParams: any): Promise<IMultiTransactionResult>;