@streamflow/stream 6.5.0 → 7.0.0-alpha.10

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 (137) hide show
  1. package/README.md +17 -22
  2. package/dist/cjs/__tests__/aptos/streamClient.spec.js +111 -0
  3. package/dist/cjs/__tests__/common/GenericStreamClient.spec.js +45 -0
  4. package/dist/cjs/aptos/StreamClient.js +226 -0
  5. package/dist/cjs/aptos/constants.js +27 -0
  6. package/dist/{aptos → cjs/aptos}/index.js +6 -6
  7. package/dist/cjs/aptos/types.js +69 -0
  8. package/dist/{aptos → cjs/aptos}/utils.js +4 -4
  9. package/dist/cjs/aptos/wallet.js +34 -0
  10. package/dist/cjs/common/BaseStreamClient.js +23 -0
  11. package/dist/cjs/common/GenericStreamClient.js +102 -0
  12. package/dist/{common → cjs/common}/constants.js +2 -2
  13. package/dist/{common → cjs/common}/contractUtils.js +19 -20
  14. package/dist/cjs/common/utils.js +66 -0
  15. package/dist/cjs/evm/StreamClient.js +238 -0
  16. package/dist/cjs/evm/constants.js +38 -0
  17. package/dist/{evm → cjs/evm}/index.js +8 -8
  18. package/dist/{evm → cjs/evm}/types.js +26 -35
  19. package/dist/{evm → cjs/evm}/utils.js +2 -2
  20. package/dist/{index.js → cjs/index.js} +10 -10
  21. package/dist/cjs/solana/StreamClient.js +737 -0
  22. package/dist/{solana → cjs/solana}/constants.js +33 -33
  23. package/dist/cjs/solana/index.js +39 -0
  24. package/dist/{solana → cjs/solana}/instructions.js +83 -83
  25. package/dist/{solana → cjs/solana}/layout.js +8 -9
  26. package/dist/{solana → cjs/solana}/types.js +16 -26
  27. package/dist/cjs/solana/utils.js +134 -0
  28. package/dist/cjs/sui/StreamClient.js +402 -0
  29. package/dist/cjs/sui/constants.js +42 -0
  30. package/dist/{sui → cjs/sui}/index.js +5 -5
  31. package/dist/cjs/sui/types.js +68 -0
  32. package/dist/{sui → cjs/sui}/utils.js +7 -7
  33. package/dist/cjs/sui/wallet.js +40 -0
  34. package/dist/esm/__tests__/aptos/streamClient.spec.js +106 -0
  35. package/dist/esm/__tests__/common/GenericStreamClient.spec.js +40 -0
  36. package/dist/{aptos → esm/aptos}/StreamClient.d.ts +3 -3
  37. package/dist/esm/aptos/StreamClient.js +226 -0
  38. package/dist/{aptos → esm/aptos}/constants.d.ts +1 -1
  39. package/dist/esm/aptos/constants.js +24 -0
  40. package/dist/esm/aptos/index.d.ts +4 -0
  41. package/dist/esm/aptos/index.js +4 -0
  42. package/dist/{aptos → esm/aptos}/types.d.ts +13 -13
  43. package/dist/esm/aptos/types.js +105 -0
  44. package/dist/esm/aptos/utils.js +30 -0
  45. package/dist/esm/aptos/wallet.js +33 -0
  46. package/dist/{common → esm/common}/BaseStreamClient.d.ts +1 -1
  47. package/dist/esm/common/BaseStreamClient.js +19 -0
  48. package/dist/{common → esm/common}/GenericStreamClient.d.ts +6 -6
  49. package/dist/esm/common/GenericStreamClient.js +101 -0
  50. package/dist/esm/common/constants.d.ts +3 -0
  51. package/dist/esm/common/constants.js +3 -0
  52. package/dist/{common → esm/common}/contractUtils.d.ts +15 -15
  53. package/dist/esm/common/contractUtils.js +42 -0
  54. package/dist/{common → esm/common}/types.d.ts +19 -19
  55. package/dist/esm/common/types.js +106 -0
  56. package/dist/{common → esm/common}/utils.d.ts +11 -12
  57. package/dist/esm/common/utils.js +56 -0
  58. package/dist/{evm → esm/evm}/StreamClient.d.ts +4 -4
  59. package/dist/esm/evm/StreamClient.js +237 -0
  60. package/dist/esm/evm/abi.js +1255 -0
  61. package/dist/{evm → esm/evm}/constants.d.ts +1 -1
  62. package/dist/esm/evm/constants.js +35 -0
  63. package/dist/esm/evm/ercAbi.js +222 -0
  64. package/dist/esm/evm/index.d.ts +5 -0
  65. package/dist/esm/evm/index.js +5 -0
  66. package/dist/{evm → esm/evm}/types.d.ts +38 -38
  67. package/dist/esm/evm/types.js +102 -0
  68. package/dist/esm/evm/utils.js +8 -0
  69. package/dist/esm/index.d.ts +9 -0
  70. package/dist/esm/index.js +9 -0
  71. package/dist/{solana → esm/solana}/StreamClient.d.ts +4 -4
  72. package/dist/esm/solana/StreamClient.js +711 -0
  73. package/dist/{solana → esm/solana}/constants.d.ts +3 -1
  74. package/dist/esm/solana/constants.js +62 -0
  75. package/dist/esm/solana/index.d.ts +6 -0
  76. package/dist/esm/solana/index.js +6 -0
  77. package/dist/esm/solana/instructionTypes.js +1 -0
  78. package/dist/{solana → esm/solana}/instructions.d.ts +18 -18
  79. package/dist/esm/solana/instructions.js +257 -0
  80. package/dist/{solana → esm/solana}/layout.d.ts +2 -1
  81. package/dist/esm/solana/layout.js +155 -0
  82. package/dist/esm/solana/timelockIDL.js +420 -0
  83. package/dist/{solana → esm/solana}/types.d.ts +38 -37
  84. package/dist/esm/solana/types.js +101 -0
  85. package/dist/{solana → esm/solana}/utils.d.ts +4 -1
  86. package/dist/esm/solana/utils.js +123 -0
  87. package/dist/{sui → esm/sui}/StreamClient.d.ts +3 -3
  88. package/dist/esm/sui/StreamClient.js +403 -0
  89. package/dist/{sui → esm/sui}/constants.d.ts +1 -1
  90. package/dist/esm/sui/constants.js +39 -0
  91. package/dist/esm/sui/index.d.ts +4 -0
  92. package/dist/esm/sui/index.js +4 -0
  93. package/dist/{sui → esm/sui}/types.d.ts +13 -13
  94. package/dist/esm/sui/types.js +104 -0
  95. package/dist/{sui → esm/sui}/utils.d.ts +1 -1
  96. package/dist/esm/sui/utils.js +23 -0
  97. package/dist/{sui → esm/sui}/wallet.d.ts +1 -1
  98. package/dist/esm/sui/wallet.js +38 -0
  99. package/package.json +37 -35
  100. package/dist/__tests__/aptos/streamClient.spec.js +0 -161
  101. package/dist/__tests__/common/GenericStreamClient.spec.js +0 -44
  102. package/dist/aptos/StreamClient.js +0 -431
  103. package/dist/aptos/constants.js +0 -28
  104. package/dist/aptos/index.d.ts +0 -4
  105. package/dist/aptos/types.js +0 -77
  106. package/dist/aptos/wallet.js +0 -89
  107. package/dist/common/BaseStreamClient.js +0 -71
  108. package/dist/common/GenericStreamClient.js +0 -128
  109. package/dist/common/constants.d.ts +0 -3
  110. package/dist/common/utils.js +0 -118
  111. package/dist/evm/StreamClient.js +0 -496
  112. package/dist/evm/constants.js +0 -39
  113. package/dist/evm/index.d.ts +0 -5
  114. package/dist/index.d.ts +0 -9
  115. package/dist/solana/StreamClient.js +0 -1253
  116. package/dist/solana/index.d.ts +0 -7
  117. package/dist/solana/index.js +0 -43
  118. package/dist/solana/utils.js +0 -211
  119. package/dist/sui/StreamClient.js +0 -648
  120. package/dist/sui/constants.js +0 -43
  121. package/dist/sui/index.d.ts +0 -4
  122. package/dist/sui/types.js +0 -102
  123. package/dist/sui/wallet.js +0 -90
  124. /package/dist/{common → cjs/common}/types.js +0 -0
  125. /package/dist/{evm → cjs/evm}/abi.js +0 -0
  126. /package/dist/{evm → cjs/evm}/ercAbi.js +0 -0
  127. /package/dist/{solana → cjs/solana}/instructionTypes.js +0 -0
  128. /package/dist/{solana → cjs/solana}/timelockIDL.js +0 -0
  129. /package/dist/{__tests__ → esm/__tests__}/aptos/streamClient.spec.d.ts +0 -0
  130. /package/dist/{__tests__ → esm/__tests__}/common/GenericStreamClient.spec.d.ts +0 -0
  131. /package/dist/{aptos → esm/aptos}/utils.d.ts +0 -0
  132. /package/dist/{aptos → esm/aptos}/wallet.d.ts +0 -0
  133. /package/dist/{evm → esm/evm}/abi.d.ts +0 -0
  134. /package/dist/{evm → esm/evm}/ercAbi.d.ts +0 -0
  135. /package/dist/{evm → esm/evm}/utils.d.ts +0 -0
  136. /package/dist/{solana → esm/solana}/instructionTypes.d.ts +0 -0
  137. /package/dist/{solana → esm/solana}/timelockIDL.d.ts +0 -0
@@ -1,17 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.extractSuiErrorInfo = void 0;
4
- var constants_1 = require("./constants");
4
+ const constants_js_1 = require("./constants.js");
5
5
  function extractSuiErrorInfo(errorText) {
6
- var error = { text: errorText };
7
- var match = constants_1.SUI_ERROR_MATCH_REGEX.exec(errorText);
6
+ const error = { text: errorText };
7
+ const match = constants_js_1.SUI_ERROR_MATCH_REGEX.exec(errorText);
8
8
  if (!match) {
9
9
  return error;
10
10
  }
11
- var moduleName = match[2];
12
- var errorCode = Number(match[4]);
13
- var errorName = constants_1.SUI_MODULE_ERROR_MAP[moduleName] ? constants_1.SUI_MODULE_ERROR_MAP[moduleName][errorCode] : undefined;
14
- var index = Number(match[5]);
11
+ const moduleName = match[2];
12
+ const errorCode = Number(match[4]);
13
+ const errorName = constants_js_1.SUI_MODULE_ERROR_MAP[moduleName] ? constants_js_1.SUI_MODULE_ERROR_MAP[moduleName][errorCode] : undefined;
14
+ const index = Number(match[5]);
15
15
  if (index) {
16
16
  error.index = index;
17
17
  }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SuiWalletWrapper = exports.isSignerKeypair = void 0;
4
+ const cryptography_1 = require("@mysten/sui.js/cryptography");
5
+ /**
6
+ * Utility function to check if the transaction initiator is a Wallet object
7
+ * @param {Keypair | WalletContextState} walletOrKeypair - Wallet or Keypair in question
8
+ * @return {boolean} - Returns true if parameter is a Wallet.
9
+ */
10
+ function isSignerKeypair(walletOrKeypair) {
11
+ return (walletOrKeypair instanceof cryptography_1.Keypair ||
12
+ walletOrKeypair.constructor === cryptography_1.Keypair ||
13
+ walletOrKeypair.constructor.name === cryptography_1.Keypair.prototype.constructor.name ||
14
+ "export" in walletOrKeypair);
15
+ }
16
+ exports.isSignerKeypair = isSignerKeypair;
17
+ class SuiWalletWrapper {
18
+ constructor(wallet, client) {
19
+ this.client = client;
20
+ this.wallet = wallet;
21
+ if (isSignerKeypair(wallet)) {
22
+ this.address = wallet.toSuiAddress();
23
+ }
24
+ else {
25
+ this.address = wallet.address;
26
+ }
27
+ }
28
+ async signAndExecuteTransactionBlock(input) {
29
+ if (isSignerKeypair(this.wallet)) {
30
+ return this.client.signAndExecuteTransactionBlock({
31
+ ...input,
32
+ // @ts-ignore
33
+ signer: this.wallet,
34
+ });
35
+ }
36
+ // @ts-ignore
37
+ return this.wallet.signAndExecuteTransactionBlock(input);
38
+ }
39
+ }
40
+ exports.SuiWalletWrapper = SuiWalletWrapper;
@@ -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: BigNumber(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
+ };
@@ -0,0 +1,4 @@
1
+ export { default, default as AptosStreamClient } from "./StreamClient.js";
2
+ export * from "./utils.js";
3
+ export * from "./types.js";
4
+ export * as constants from "./constants.js";
@@ -0,0 +1,4 @@
1
+ export { default, default as AptosStreamClient } from "./StreamClient.js";
2
+ export * from "./utils.js";
3
+ export * from "./types.js";
4
+ export * as constants from "./constants.js";
@@ -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
+ }