@haneullabs/wallet-sdk 0.1.0

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 (133) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/README.md +1 -0
  3. package/dist/cjs/auto-approvals/analyzer.d.ts +71 -0
  4. package/dist/cjs/auto-approvals/analyzer.js +70 -0
  5. package/dist/cjs/auto-approvals/analyzer.js.map +7 -0
  6. package/dist/cjs/auto-approvals/index.d.ts +6 -0
  7. package/dist/cjs/auto-approvals/index.js +33 -0
  8. package/dist/cjs/auto-approvals/index.js.map +7 -0
  9. package/dist/cjs/auto-approvals/intent.d.ts +5 -0
  10. package/dist/cjs/auto-approvals/intent.js +65 -0
  11. package/dist/cjs/auto-approvals/intent.js.map +7 -0
  12. package/dist/cjs/auto-approvals/manager.d.ts +31 -0
  13. package/dist/cjs/auto-approvals/manager.js +328 -0
  14. package/dist/cjs/auto-approvals/manager.js.map +7 -0
  15. package/dist/cjs/auto-approvals/schemas/index.d.ts +4 -0
  16. package/dist/cjs/auto-approvals/schemas/index.js +28 -0
  17. package/dist/cjs/auto-approvals/schemas/index.js.map +7 -0
  18. package/dist/cjs/auto-approvals/schemas/policy.d.ts +145 -0
  19. package/dist/cjs/auto-approvals/schemas/policy.js +79 -0
  20. package/dist/cjs/auto-approvals/schemas/policy.js.map +7 -0
  21. package/dist/cjs/auto-approvals/schemas/state.d.ts +115 -0
  22. package/dist/cjs/auto-approvals/schemas/state.js +49 -0
  23. package/dist/cjs/auto-approvals/schemas/state.js.map +7 -0
  24. package/dist/cjs/index.d.ts +2 -0
  25. package/dist/cjs/index.js +20 -0
  26. package/dist/cjs/index.js.map +7 -0
  27. package/dist/cjs/package.json +5 -0
  28. package/dist/cjs/transaction-analyzer/analyzer.d.ts +34 -0
  29. package/dist/cjs/transaction-analyzer/analyzer.js +94 -0
  30. package/dist/cjs/transaction-analyzer/analyzer.js.map +7 -0
  31. package/dist/cjs/transaction-analyzer/index.d.ts +9 -0
  32. package/dist/cjs/transaction-analyzer/index.js +28 -0
  33. package/dist/cjs/transaction-analyzer/index.js.map +7 -0
  34. package/dist/cjs/transaction-analyzer/rules/accessLevel.d.ts +29 -0
  35. package/dist/cjs/transaction-analyzer/rules/accessLevel.js +106 -0
  36. package/dist/cjs/transaction-analyzer/rules/accessLevel.js.map +7 -0
  37. package/dist/cjs/transaction-analyzer/rules/coin-flows.d.ts +313 -0
  38. package/dist/cjs/transaction-analyzer/rules/coin-flows.js +179 -0
  39. package/dist/cjs/transaction-analyzer/rules/coin-flows.js.map +7 -0
  40. package/dist/cjs/transaction-analyzer/rules/coin-value.d.ts +42 -0
  41. package/dist/cjs/transaction-analyzer/rules/coin-value.js +60 -0
  42. package/dist/cjs/transaction-analyzer/rules/coin-value.js.map +7 -0
  43. package/dist/cjs/transaction-analyzer/rules/coins.d.ts +337 -0
  44. package/dist/cjs/transaction-analyzer/rules/coins.js +78 -0
  45. package/dist/cjs/transaction-analyzer/rules/coins.js.map +7 -0
  46. package/dist/cjs/transaction-analyzer/rules/commands.d.ts +366 -0
  47. package/dist/cjs/transaction-analyzer/rules/commands.js +148 -0
  48. package/dist/cjs/transaction-analyzer/rules/commands.js.map +7 -0
  49. package/dist/cjs/transaction-analyzer/rules/core.d.ts +314 -0
  50. package/dist/cjs/transaction-analyzer/rules/core.js +73 -0
  51. package/dist/cjs/transaction-analyzer/rules/core.js.map +7 -0
  52. package/dist/cjs/transaction-analyzer/rules/functions.d.ts +292 -0
  53. package/dist/cjs/transaction-analyzer/rules/functions.js +58 -0
  54. package/dist/cjs/transaction-analyzer/rules/functions.js.map +7 -0
  55. package/dist/cjs/transaction-analyzer/rules/index.d.ts +2244 -0
  56. package/dist/cjs/transaction-analyzer/rules/index.js +52 -0
  57. package/dist/cjs/transaction-analyzer/rules/index.js.map +7 -0
  58. package/dist/cjs/transaction-analyzer/rules/inputs.d.ts +313 -0
  59. package/dist/cjs/transaction-analyzer/rules/inputs.js +49 -0
  60. package/dist/cjs/transaction-analyzer/rules/inputs.js.map +7 -0
  61. package/dist/cjs/transaction-analyzer/rules/objects.d.ts +359 -0
  62. package/dist/cjs/transaction-analyzer/rules/objects.js +124 -0
  63. package/dist/cjs/transaction-analyzer/rules/objects.js.map +7 -0
  64. package/dist/cjs/util.d.ts +5 -0
  65. package/dist/cjs/util.js +17 -0
  66. package/dist/cjs/util.js.map +7 -0
  67. package/dist/esm/auto-approvals/analyzer.d.ts +71 -0
  68. package/dist/esm/auto-approvals/analyzer.js +50 -0
  69. package/dist/esm/auto-approvals/analyzer.js.map +7 -0
  70. package/dist/esm/auto-approvals/index.d.ts +6 -0
  71. package/dist/esm/auto-approvals/index.js +12 -0
  72. package/dist/esm/auto-approvals/index.js.map +7 -0
  73. package/dist/esm/auto-approvals/intent.d.ts +5 -0
  74. package/dist/esm/auto-approvals/intent.js +45 -0
  75. package/dist/esm/auto-approvals/intent.js.map +7 -0
  76. package/dist/esm/auto-approvals/manager.d.ts +31 -0
  77. package/dist/esm/auto-approvals/manager.js +308 -0
  78. package/dist/esm/auto-approvals/manager.js.map +7 -0
  79. package/dist/esm/auto-approvals/schemas/index.d.ts +4 -0
  80. package/dist/esm/auto-approvals/schemas/index.js +8 -0
  81. package/dist/esm/auto-approvals/schemas/index.js.map +7 -0
  82. package/dist/esm/auto-approvals/schemas/policy.d.ts +145 -0
  83. package/dist/esm/auto-approvals/schemas/policy.js +49 -0
  84. package/dist/esm/auto-approvals/schemas/policy.js.map +7 -0
  85. package/dist/esm/auto-approvals/schemas/state.d.ts +115 -0
  86. package/dist/esm/auto-approvals/schemas/state.js +19 -0
  87. package/dist/esm/auto-approvals/schemas/state.js.map +7 -0
  88. package/dist/esm/index.d.ts +2 -0
  89. package/dist/esm/index.js +3 -0
  90. package/dist/esm/index.js.map +7 -0
  91. package/dist/esm/package.json +5 -0
  92. package/dist/esm/transaction-analyzer/analyzer.d.ts +34 -0
  93. package/dist/esm/transaction-analyzer/analyzer.js +74 -0
  94. package/dist/esm/transaction-analyzer/analyzer.js.map +7 -0
  95. package/dist/esm/transaction-analyzer/index.d.ts +9 -0
  96. package/dist/esm/transaction-analyzer/index.js +8 -0
  97. package/dist/esm/transaction-analyzer/index.js.map +7 -0
  98. package/dist/esm/transaction-analyzer/rules/accessLevel.d.ts +29 -0
  99. package/dist/esm/transaction-analyzer/rules/accessLevel.js +86 -0
  100. package/dist/esm/transaction-analyzer/rules/accessLevel.js.map +7 -0
  101. package/dist/esm/transaction-analyzer/rules/coin-flows.d.ts +313 -0
  102. package/dist/esm/transaction-analyzer/rules/coin-flows.js +159 -0
  103. package/dist/esm/transaction-analyzer/rules/coin-flows.js.map +7 -0
  104. package/dist/esm/transaction-analyzer/rules/coin-value.d.ts +42 -0
  105. package/dist/esm/transaction-analyzer/rules/coin-value.js +40 -0
  106. package/dist/esm/transaction-analyzer/rules/coin-value.js.map +7 -0
  107. package/dist/esm/transaction-analyzer/rules/coins.d.ts +337 -0
  108. package/dist/esm/transaction-analyzer/rules/coins.js +58 -0
  109. package/dist/esm/transaction-analyzer/rules/coins.js.map +7 -0
  110. package/dist/esm/transaction-analyzer/rules/commands.d.ts +366 -0
  111. package/dist/esm/transaction-analyzer/rules/commands.js +128 -0
  112. package/dist/esm/transaction-analyzer/rules/commands.js.map +7 -0
  113. package/dist/esm/transaction-analyzer/rules/core.d.ts +314 -0
  114. package/dist/esm/transaction-analyzer/rules/core.js +53 -0
  115. package/dist/esm/transaction-analyzer/rules/core.js.map +7 -0
  116. package/dist/esm/transaction-analyzer/rules/functions.d.ts +292 -0
  117. package/dist/esm/transaction-analyzer/rules/functions.js +38 -0
  118. package/dist/esm/transaction-analyzer/rules/functions.js.map +7 -0
  119. package/dist/esm/transaction-analyzer/rules/index.d.ts +2244 -0
  120. package/dist/esm/transaction-analyzer/rules/index.js +32 -0
  121. package/dist/esm/transaction-analyzer/rules/index.js.map +7 -0
  122. package/dist/esm/transaction-analyzer/rules/inputs.d.ts +313 -0
  123. package/dist/esm/transaction-analyzer/rules/inputs.js +29 -0
  124. package/dist/esm/transaction-analyzer/rules/inputs.js.map +7 -0
  125. package/dist/esm/transaction-analyzer/rules/objects.d.ts +359 -0
  126. package/dist/esm/transaction-analyzer/rules/objects.js +104 -0
  127. package/dist/esm/transaction-analyzer/rules/objects.js.map +7 -0
  128. package/dist/esm/util.d.ts +5 -0
  129. package/dist/esm/util.js +1 -0
  130. package/dist/esm/util.js.map +7 -0
  131. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  132. package/dist/tsconfig.tsbuildinfo +1 -0
  133. package/package.json +59 -0
@@ -0,0 +1,313 @@
1
+ import type { AnalyzedCommand } from './commands.js';
2
+ export interface CoinFlow {
3
+ coinType: string;
4
+ amount: bigint;
5
+ }
6
+ export declare const coinFlows: import("../analyzer.js").Analyzer<{
7
+ outflows: CoinFlow[];
8
+ }, {
9
+ client: import("@haneullabs/haneul/dist/cjs/experimental/client.js").Experimental_BaseClient & {
10
+ core: import("@haneullabs/haneul/dist/cjs/experimental/core.js").Experimental_CoreClient;
11
+ };
12
+ }, {
13
+ data: {
14
+ version: 2;
15
+ sender?: string | null | undefined;
16
+ expiration?: import("@haneullabs/bcs").EnumOutputShapeWithKeys<{
17
+ None: true;
18
+ Epoch: string | number;
19
+ }, "None" | "Epoch"> | null | undefined;
20
+ gasData: {
21
+ budget: string | number | null;
22
+ price: string | number | null;
23
+ owner: string | null;
24
+ payment: {
25
+ objectId: string;
26
+ version: string | number;
27
+ digest: string;
28
+ }[] | null;
29
+ };
30
+ inputs: import("@haneullabs/bcs").EnumOutputShapeWithKeys<{
31
+ Object: import("@haneullabs/bcs").EnumOutputShapeWithKeys<{
32
+ ImmOrOwnedObject: {
33
+ objectId: string;
34
+ version: string | number;
35
+ digest: string;
36
+ };
37
+ SharedObject: {
38
+ objectId: string;
39
+ initialSharedVersion: string | number;
40
+ mutable: boolean;
41
+ };
42
+ Receiving: {
43
+ objectId: string;
44
+ version: string | number;
45
+ digest: string;
46
+ };
47
+ }, "ImmOrOwnedObject" | "SharedObject" | "Receiving">;
48
+ Pure: {
49
+ bytes: string;
50
+ };
51
+ UnresolvedPure: {
52
+ value: unknown;
53
+ };
54
+ UnresolvedObject: {
55
+ objectId: string;
56
+ version?: string | number | null | undefined;
57
+ digest?: string | null | undefined;
58
+ initialSharedVersion?: string | number | null | undefined;
59
+ mutable?: boolean | null | undefined;
60
+ };
61
+ }, "Pure" | "Object" | "UnresolvedPure" | "UnresolvedObject">[];
62
+ commands: import("@haneullabs/bcs").EnumOutputShapeWithKeys<{
63
+ MoveCall: {
64
+ package: string;
65
+ module: string;
66
+ function: string;
67
+ typeArguments: string[];
68
+ arguments: ({
69
+ $kind: "GasCoin";
70
+ GasCoin: true;
71
+ } | {
72
+ $kind: "Input";
73
+ Input: number;
74
+ type?: "pure";
75
+ } | {
76
+ $kind: "Input";
77
+ Input: number;
78
+ type?: "object";
79
+ } | {
80
+ $kind: "Result";
81
+ Result: number;
82
+ } | {
83
+ $kind: "NestedResult";
84
+ NestedResult: [number, number];
85
+ })[];
86
+ _argumentTypes?: {
87
+ ref: "&" | "&mut" | null;
88
+ body: import("@haneullabs/haneul/dist/cjs/transactions/index.js").OpenMoveTypeSignatureBody;
89
+ }[] | null | undefined;
90
+ };
91
+ TransferObjects: {
92
+ objects: ({
93
+ $kind: "GasCoin";
94
+ GasCoin: true;
95
+ } | {
96
+ $kind: "Input";
97
+ Input: number;
98
+ type?: "pure";
99
+ } | {
100
+ $kind: "Input";
101
+ Input: number;
102
+ type?: "object";
103
+ } | {
104
+ $kind: "Result";
105
+ Result: number;
106
+ } | {
107
+ $kind: "NestedResult";
108
+ NestedResult: [number, number];
109
+ })[];
110
+ address: {
111
+ $kind: "GasCoin";
112
+ GasCoin: true;
113
+ } | {
114
+ $kind: "Input";
115
+ Input: number;
116
+ type?: "pure";
117
+ } | {
118
+ $kind: "Input";
119
+ Input: number;
120
+ type?: "object";
121
+ } | {
122
+ $kind: "Result";
123
+ Result: number;
124
+ } | {
125
+ $kind: "NestedResult";
126
+ NestedResult: [number, number];
127
+ };
128
+ };
129
+ SplitCoins: {
130
+ coin: {
131
+ $kind: "GasCoin";
132
+ GasCoin: true;
133
+ } | {
134
+ $kind: "Input";
135
+ Input: number;
136
+ type?: "pure";
137
+ } | {
138
+ $kind: "Input";
139
+ Input: number;
140
+ type?: "object";
141
+ } | {
142
+ $kind: "Result";
143
+ Result: number;
144
+ } | {
145
+ $kind: "NestedResult";
146
+ NestedResult: [number, number];
147
+ };
148
+ amounts: ({
149
+ $kind: "GasCoin";
150
+ GasCoin: true;
151
+ } | {
152
+ $kind: "Input";
153
+ Input: number;
154
+ type?: "pure";
155
+ } | {
156
+ $kind: "Input";
157
+ Input: number;
158
+ type?: "object";
159
+ } | {
160
+ $kind: "Result";
161
+ Result: number;
162
+ } | {
163
+ $kind: "NestedResult";
164
+ NestedResult: [number, number];
165
+ })[];
166
+ };
167
+ MergeCoins: {
168
+ destination: {
169
+ $kind: "GasCoin";
170
+ GasCoin: true;
171
+ } | {
172
+ $kind: "Input";
173
+ Input: number;
174
+ type?: "pure";
175
+ } | {
176
+ $kind: "Input";
177
+ Input: number;
178
+ type?: "object";
179
+ } | {
180
+ $kind: "Result";
181
+ Result: number;
182
+ } | {
183
+ $kind: "NestedResult";
184
+ NestedResult: [number, number];
185
+ };
186
+ sources: ({
187
+ $kind: "GasCoin";
188
+ GasCoin: true;
189
+ } | {
190
+ $kind: "Input";
191
+ Input: number;
192
+ type?: "pure";
193
+ } | {
194
+ $kind: "Input";
195
+ Input: number;
196
+ type?: "object";
197
+ } | {
198
+ $kind: "Result";
199
+ Result: number;
200
+ } | {
201
+ $kind: "NestedResult";
202
+ NestedResult: [number, number];
203
+ })[];
204
+ };
205
+ Publish: {
206
+ modules: string[];
207
+ dependencies: string[];
208
+ };
209
+ MakeMoveVec: {
210
+ type: string | null;
211
+ elements: ({
212
+ $kind: "GasCoin";
213
+ GasCoin: true;
214
+ } | {
215
+ $kind: "Input";
216
+ Input: number;
217
+ type?: "pure";
218
+ } | {
219
+ $kind: "Input";
220
+ Input: number;
221
+ type?: "object";
222
+ } | {
223
+ $kind: "Result";
224
+ Result: number;
225
+ } | {
226
+ $kind: "NestedResult";
227
+ NestedResult: [number, number];
228
+ })[];
229
+ };
230
+ Upgrade: {
231
+ modules: string[];
232
+ dependencies: string[];
233
+ package: string;
234
+ ticket: {
235
+ $kind: "GasCoin";
236
+ GasCoin: true;
237
+ } | {
238
+ $kind: "Input";
239
+ Input: number;
240
+ type?: "pure";
241
+ } | {
242
+ $kind: "Input";
243
+ Input: number;
244
+ type?: "object";
245
+ } | {
246
+ $kind: "Result";
247
+ Result: number;
248
+ } | {
249
+ $kind: "NestedResult";
250
+ NestedResult: [number, number];
251
+ };
252
+ };
253
+ $Intent: {
254
+ name: string;
255
+ inputs: {
256
+ [x: string]: {
257
+ $kind: "GasCoin";
258
+ GasCoin: true;
259
+ } | {
260
+ $kind: "Input";
261
+ Input: number;
262
+ type?: "pure";
263
+ } | {
264
+ $kind: "Input";
265
+ Input: number;
266
+ type?: "object";
267
+ } | {
268
+ $kind: "Result";
269
+ Result: number;
270
+ } | {
271
+ $kind: "NestedResult";
272
+ NestedResult: [number, number];
273
+ } | ({
274
+ $kind: "GasCoin";
275
+ GasCoin: true;
276
+ } | {
277
+ $kind: "Input";
278
+ Input: number;
279
+ type?: "pure";
280
+ } | {
281
+ $kind: "Input";
282
+ Input: number;
283
+ type?: "object";
284
+ } | {
285
+ $kind: "Result";
286
+ Result: number;
287
+ } | {
288
+ $kind: "NestedResult";
289
+ NestedResult: [number, number];
290
+ })[];
291
+ };
292
+ data: {
293
+ [x: string]: unknown;
294
+ };
295
+ };
296
+ }, "MoveCall" | "TransferObjects" | "SplitCoins" | "MergeCoins" | "Publish" | "MakeMoveVec" | "Upgrade" | "$Intent">[];
297
+ };
298
+ commands: AnalyzedCommand[];
299
+ inputs: import("./inputs.js").AnalyzedCommandInput[];
300
+ coins: any;
301
+ gasCoins: {
302
+ coinType: string;
303
+ balance: bigint;
304
+ ownerAddress: string | null;
305
+ id: string;
306
+ version: string;
307
+ digest: string;
308
+ owner: import("@haneullabs/haneul/dist/cjs/experimental/types.js").Experimental_HaneulClientTypes.ObjectOwner;
309
+ type: string;
310
+ content: PromiseLike<Uint8Array>;
311
+ previousTransaction: string | null;
312
+ }[];
313
+ }>;
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var coin_flows_exports = {};
20
+ __export(coin_flows_exports, {
21
+ coinFlows: () => coinFlows
22
+ });
23
+ module.exports = __toCommonJS(coin_flows_exports);
24
+ var import_utils = require("@haneullabs/haneul/utils");
25
+ var import_analyzer = require("../analyzer.js");
26
+ var import_bcs = require("@haneullabs/haneul/bcs");
27
+ var import_commands = require("./commands.js");
28
+ var import_core = require("./core.js");
29
+ var import_inputs = require("./inputs.js");
30
+ var import_coins = require("./coins.js");
31
+ const coinFlows = (0, import_analyzer.createAnalyzer)({
32
+ dependencies: { data: import_core.data, commands: import_commands.commands, inputs: import_inputs.inputs, coins: import_coins.coins, gasCoins: import_coins.gasCoins },
33
+ analyze: () => async ({ data: data2, commands: commands2, inputs: inputs2, coins: coins2, gasCoins: gasCoins2 }) => {
34
+ const getTrackedCoin = (ref) => {
35
+ switch (ref.$kind) {
36
+ case "GasCoin":
37
+ return trackedCoins.get("gas") ?? null;
38
+ case "Object":
39
+ return trackedCoins.get(`input:${ref.index}`) ?? null;
40
+ case "Result":
41
+ return trackedCoins.get(`result:${ref.index[0]},${ref.index[1]}`) ?? null;
42
+ case "Unknown":
43
+ case "Pure":
44
+ return null;
45
+ }
46
+ };
47
+ const splitCoin = (command) => {
48
+ const coin = getTrackedCoin(command.coin);
49
+ if (!coin) {
50
+ return;
51
+ }
52
+ if (!command.amounts.every((a) => a.$kind === "Pure")) {
53
+ coin.consume();
54
+ return;
55
+ }
56
+ const amounts = command.amounts.map((a) => {
57
+ if (a.$kind !== "Pure") {
58
+ throw new Error("Expected pure value");
59
+ }
60
+ return BigInt(import_bcs.bcs.u64().fromBase64(a.bytes));
61
+ });
62
+ coin.remainingBalance -= amounts.reduce((a, b) => a + b, 0n);
63
+ amounts.forEach((amount, i) => {
64
+ trackedCoins.set(
65
+ `result:${command.index},${i}`,
66
+ new TrackedCoin(coin.coinType, amount, false)
67
+ );
68
+ });
69
+ };
70
+ const mergeCoins = (command) => {
71
+ const sources = command.sources.map(getTrackedCoin);
72
+ const amount = sources.reduce((a, c) => a + (c?.remainingBalance ?? 0n), 0n);
73
+ for (const src of sources) {
74
+ src?.consume();
75
+ }
76
+ const dest = getTrackedCoin(command.destination);
77
+ if (!dest) {
78
+ return;
79
+ }
80
+ dest.remainingBalance += amount;
81
+ };
82
+ const transferObjects = (command) => {
83
+ const address = command.address.$kind === "Pure" ? import_bcs.bcs.Address.fromBase64(command.address.bytes) : null;
84
+ for (const obj of command.objects) {
85
+ const tracked = getTrackedCoin(obj);
86
+ if (tracked && address && data2.sender === address) {
87
+ trackedCoins.get("gas").remainingBalance += tracked.remainingBalance;
88
+ }
89
+ tracked?.consume();
90
+ }
91
+ };
92
+ const issues = [];
93
+ const trackedCoins = /* @__PURE__ */ new Map();
94
+ trackedCoins.set(
95
+ "gas",
96
+ new TrackedCoin(
97
+ (0, import_utils.normalizeStructTag)("0x2::haneul::HANEUL"),
98
+ gasCoins2.reduce((a, c) => a + c.balance, 0n),
99
+ true
100
+ )
101
+ );
102
+ if (data2.gasData.budget) {
103
+ trackedCoins.get("gas").remainingBalance -= BigInt(data2.gasData.budget);
104
+ } else {
105
+ issues.push({ message: "Gas budget not set in Transaction" });
106
+ }
107
+ for (const input of inputs2) {
108
+ if (input.$kind === "Object" && coins2[input.object.id]) {
109
+ const coin = coins2[input.object.id];
110
+ trackedCoins.set(
111
+ `input:${input.index}`,
112
+ new TrackedCoin(coin.coinType, coin.balance, true)
113
+ );
114
+ }
115
+ }
116
+ for (const command of commands2) {
117
+ switch (command.$kind) {
118
+ case "SplitCoins":
119
+ splitCoin(command);
120
+ break;
121
+ case "MergeCoins":
122
+ mergeCoins(command);
123
+ break;
124
+ case "TransferObjects":
125
+ transferObjects(command);
126
+ break;
127
+ case "MakeMoveVec":
128
+ command.elements.forEach((el) => {
129
+ const tracked = getTrackedCoin(el);
130
+ tracked?.consume();
131
+ });
132
+ break;
133
+ case "MoveCall":
134
+ command.arguments.forEach((arg) => {
135
+ const tracked = getTrackedCoin(arg);
136
+ tracked?.consume();
137
+ });
138
+ break;
139
+ case "Upgrade":
140
+ case "Publish":
141
+ break;
142
+ default:
143
+ throw new Error(`Unsupported command type: ${command.$kind}`);
144
+ }
145
+ }
146
+ const outflows = {};
147
+ for (const coin of trackedCoins.values()) {
148
+ if (!coin.owned) {
149
+ continue;
150
+ }
151
+ if (!outflows[coin.coinType]) {
152
+ outflows[coin.coinType] = { coinType: coin.coinType, amount: 0n };
153
+ }
154
+ outflows[coin.coinType].amount += coin.initialBalance - coin.remainingBalance;
155
+ }
156
+ if (issues.length) {
157
+ return { issues };
158
+ }
159
+ return {
160
+ result: {
161
+ outflows: Object.values(outflows)
162
+ }
163
+ };
164
+ }
165
+ });
166
+ class TrackedCoin {
167
+ constructor(coinType, balance, owned) {
168
+ this.consumed = false;
169
+ this.coinType = coinType;
170
+ this.initialBalance = balance;
171
+ this.remainingBalance = balance;
172
+ this.owned = owned;
173
+ }
174
+ consume() {
175
+ this.remainingBalance = 0n;
176
+ this.consumed = true;
177
+ }
178
+ }
179
+ //# sourceMappingURL=coin-flows.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/transaction-analyzer/rules/coin-flows.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { normalizeStructTag } from '@haneullabs/haneul/utils';\nimport type { TransactionAnalysisIssue } from '../analyzer.js';\nimport { createAnalyzer } from '../analyzer.js';\nimport { bcs } from '@haneullabs/haneul/bcs';\nimport { commands } from './commands.js';\nimport type { AnalyzedCommand, AnalyzedCommandArgument } from './commands.js';\nimport { data } from './core.js';\nimport { inputs } from './inputs.js';\nimport { coins, gasCoins } from './coins.js';\n\nexport interface CoinFlow {\n\tcoinType: string;\n\tamount: bigint;\n}\n\nexport const coinFlows = createAnalyzer({\n\tdependencies: { data, commands, inputs, coins, gasCoins },\n\tanalyze:\n\t\t() =>\n\t\tasync ({ data, commands, inputs, coins, gasCoins }) => {\n\t\t\tconst getTrackedCoin = (ref: AnalyzedCommandArgument): TrackedCoin | null => {\n\t\t\t\tswitch (ref.$kind) {\n\t\t\t\t\tcase 'GasCoin':\n\t\t\t\t\t\treturn trackedCoins.get('gas') ?? null;\n\t\t\t\t\tcase 'Object':\n\t\t\t\t\t\treturn trackedCoins.get(`input:${ref.index}`) ?? null;\n\t\t\t\t\tcase 'Result':\n\t\t\t\t\t\treturn trackedCoins.get(`result:${ref.index[0]},${ref.index[1]}`) ?? null;\n\t\t\t\t\tcase 'Unknown':\n\t\t\t\t\tcase 'Pure':\n\t\t\t\t\t\treturn null;\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconst splitCoin = (command: Extract<AnalyzedCommand, { $kind: 'SplitCoins' }>) => {\n\t\t\t\tconst coin = getTrackedCoin(command.coin);\n\n\t\t\t\tif (!coin) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\t// If any amounts are dynamic we need to assume the coin is fully consumed\n\t\t\t\tif (!command.amounts.every((a) => a.$kind === 'Pure')) {\n\t\t\t\t\tcoin.consume();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst amounts = command.amounts.map((a) => {\n\t\t\t\t\tif (a.$kind !== 'Pure') {\n\t\t\t\t\t\tthrow new Error('Expected pure value');\n\t\t\t\t\t}\n\t\t\t\t\treturn BigInt(bcs.u64().fromBase64(a.bytes));\n\t\t\t\t});\n\n\t\t\t\tcoin.remainingBalance -= amounts.reduce((a, b) => a + b, 0n);\n\n\t\t\t\tamounts.forEach((amount, i) => {\n\t\t\t\t\ttrackedCoins.set(\n\t\t\t\t\t\t`result:${command.index},${i}`,\n\t\t\t\t\t\tnew TrackedCoin(coin.coinType, amount, false),\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t\t};\n\n\t\t\tconst mergeCoins = (command: Extract<AnalyzedCommand, { $kind: 'MergeCoins' }>) => {\n\t\t\t\tconst sources = command.sources.map(getTrackedCoin);\n\t\t\t\tconst amount = sources.reduce((a, c) => a + (c?.remainingBalance ?? 0n), 0n);\n\n\t\t\t\tfor (const src of sources) {\n\t\t\t\t\tsrc?.consume();\n\t\t\t\t}\n\n\t\t\t\tconst dest = getTrackedCoin(command.destination);\n\n\t\t\t\tif (!dest) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tdest.remainingBalance += amount;\n\t\t\t};\n\n\t\t\tconst transferObjects = (command: Extract<AnalyzedCommand, { $kind: 'TransferObjects' }>) => {\n\t\t\t\tconst address =\n\t\t\t\t\tcommand.address.$kind === 'Pure' ? bcs.Address.fromBase64(command.address.bytes) : null;\n\n\t\t\t\tfor (const obj of command.objects) {\n\t\t\t\t\tconst tracked = getTrackedCoin(obj);\n\n\t\t\t\t\t// If coin is transferred to the sender, we can track the transfer in the gas coin\n\t\t\t\t\tif (tracked && address && data.sender === address) {\n\t\t\t\t\t\ttrackedCoins.get('gas')!.remainingBalance += tracked.remainingBalance;\n\t\t\t\t\t}\n\n\t\t\t\t\ttracked?.consume();\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconst issues: TransactionAnalysisIssue[] = [];\n\n\t\t\tconst trackedCoins = new Map<string, TrackedCoin>();\n\n\t\t\ttrackedCoins.set(\n\t\t\t\t'gas',\n\t\t\t\tnew TrackedCoin(\n\t\t\t\t\tnormalizeStructTag('0x2::haneul::HANEUL'),\n\t\t\t\t\tgasCoins.reduce((a, c) => a + c.balance, 0n),\n\t\t\t\t\ttrue,\n\t\t\t\t),\n\t\t\t);\n\n\t\t\tif (data.gasData.budget) {\n\t\t\t\ttrackedCoins.get('gas')!.remainingBalance -= BigInt(data.gasData.budget);\n\t\t\t} else {\n\t\t\t\tissues.push({ message: 'Gas budget not set in Transaction' });\n\t\t\t}\n\n\t\t\tfor (const input of inputs) {\n\t\t\t\tif (input.$kind === 'Object' && coins[input.object.id]) {\n\t\t\t\t\tconst coin = coins[input.object.id];\n\t\t\t\t\ttrackedCoins.set(\n\t\t\t\t\t\t`input:${input.index}`,\n\t\t\t\t\t\tnew TrackedCoin(coin.coinType, coin.balance, true),\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor (const command of commands) {\n\t\t\t\tswitch (command.$kind) {\n\t\t\t\t\tcase 'SplitCoins':\n\t\t\t\t\t\tsplitCoin(command);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'MergeCoins':\n\t\t\t\t\t\tmergeCoins(command);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'TransferObjects':\n\t\t\t\t\t\ttransferObjects(command);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'MakeMoveVec':\n\t\t\t\t\t\tcommand.elements.forEach((el) => {\n\t\t\t\t\t\t\tconst tracked = getTrackedCoin(el);\n\t\t\t\t\t\t\ttracked?.consume();\n\t\t\t\t\t\t});\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'MoveCall':\n\t\t\t\t\t\tcommand.arguments.forEach((arg) => {\n\t\t\t\t\t\t\tconst tracked = getTrackedCoin(arg);\n\t\t\t\t\t\t\ttracked?.consume();\n\t\t\t\t\t\t});\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'Upgrade':\n\t\t\t\t\tcase 'Publish':\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(`Unsupported command type: ${command.$kind}`);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst outflows: Record<string, CoinFlow> = {};\n\n\t\t\tfor (const coin of trackedCoins.values()) {\n\t\t\t\tif (!coin.owned) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (!outflows[coin.coinType]) {\n\t\t\t\t\toutflows[coin.coinType] = { coinType: coin.coinType, amount: 0n };\n\t\t\t\t}\n\n\t\t\t\toutflows[coin.coinType].amount += coin.initialBalance - coin.remainingBalance;\n\t\t\t}\n\n\t\t\tif (issues.length) {\n\t\t\t\treturn { issues };\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tresult: {\n\t\t\t\t\toutflows: Object.values(outflows),\n\t\t\t\t},\n\t\t\t};\n\t\t},\n});\n\nclass TrackedCoin {\n\tcoinType: string;\n\tinitialBalance: bigint;\n\tremainingBalance: bigint;\n\towned: boolean;\n\tconsumed = false;\n\n\tconstructor(coinType: string, balance: bigint, owned: boolean) {\n\t\tthis.coinType = coinType;\n\t\tthis.initialBalance = balance;\n\t\tthis.remainingBalance = balance;\n\t\tthis.owned = owned;\n\t}\n\n\tconsume() {\n\t\tthis.remainingBalance = 0n;\n\t\tthis.consumed = true;\n\t}\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,mBAAmC;AAEnC,sBAA+B;AAC/B,iBAAoB;AACpB,sBAAyB;AAEzB,kBA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jB,YAAY,UAAkB,SAAiB,OAAgB;AAF/D,oBAAW;AAGV,SAAK,WAAW;AAChB,SAAK,iBAAiB;AACtB,SAAK,mBAAmB;AACxB,SAAK,QAAQ;AAAA,EACd;AAAA,EAEA,UAAU;AACT,SAAK,mBAAmB;AACxB,SAAK,WAAW;AAAA,EACjB;AACD;",
6
+ "names": ["data", "commands", "inputs", "coins", "gasCoins"]
7
+ }
@@ -0,0 +1,42 @@
1
+ export interface CoinValueAnalyzerOptions {
2
+ getCoinPrices: (coinTypes: string[]) => Promise<{
3
+ coinType: string;
4
+ decimals: number;
5
+ price: number | null;
6
+ }[]>;
7
+ }
8
+ export interface CoinValueAnalysis {
9
+ coinTypesWithoutPrice: string[];
10
+ total: number;
11
+ coinTypes: {
12
+ coinType: string;
13
+ decimals: number;
14
+ price: number;
15
+ amount: bigint;
16
+ convertedAmount: number;
17
+ }[];
18
+ }
19
+ export declare const coinValues: import("../analyzer.js").Analyzer<{
20
+ total: number;
21
+ coinTypesWithoutPrice: string[];
22
+ coinTypes: {
23
+ coinType: string;
24
+ decimals: number;
25
+ price: number;
26
+ amount: bigint;
27
+ convertedAmount: number;
28
+ }[];
29
+ }, {
30
+ client: import("@haneullabs/haneul/dist/cjs/experimental/client.js").Experimental_BaseClient & {
31
+ core: import("@haneullabs/haneul/dist/cjs/experimental/core.js").Experimental_CoreClient;
32
+ };
33
+ getCoinPrices: (coinTypes: string[]) => Promise<{
34
+ coinType: string;
35
+ decimals: number;
36
+ price: number | null;
37
+ }[]>;
38
+ }, {
39
+ coinFlows: {
40
+ outflows: import("./coin-flows.js").CoinFlow[];
41
+ };
42
+ }>;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var coin_value_exports = {};
20
+ __export(coin_value_exports, {
21
+ coinValues: () => coinValues
22
+ });
23
+ module.exports = __toCommonJS(coin_value_exports);
24
+ var import_analyzer = require("../analyzer.js");
25
+ var import_coin_flows = require("./coin-flows.js");
26
+ const coinValues = (0, import_analyzer.createAnalyzer)({
27
+ dependencies: { coinFlows: import_coin_flows.coinFlows },
28
+ analyze: ({ getCoinPrices }) => async ({ coinFlows: coinFlows2 }) => {
29
+ const prices = await getCoinPrices(coinFlows2.outflows.map((cf) => cf.coinType));
30
+ let total = 0;
31
+ const coinTypesWithoutPrice = [];
32
+ const coinTypes = [];
33
+ for (const flow of coinFlows2.outflows) {
34
+ if (flow.amount > 0n) {
35
+ const result = prices.find((p) => p.coinType === flow.coinType);
36
+ if (result?.price != null) {
37
+ const amount = Number(flow.amount) / 10 ** result.decimals * result.price;
38
+ total += amount;
39
+ coinTypes.push({
40
+ coinType: flow.coinType,
41
+ decimals: result.decimals,
42
+ price: result.price,
43
+ amount: flow.amount,
44
+ convertedAmount: amount
45
+ });
46
+ } else {
47
+ coinTypesWithoutPrice.push(flow.coinType);
48
+ }
49
+ }
50
+ }
51
+ return {
52
+ result: {
53
+ total,
54
+ coinTypesWithoutPrice,
55
+ coinTypes
56
+ }
57
+ };
58
+ }
59
+ });
60
+ //# sourceMappingURL=coin-value.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/transaction-analyzer/rules/coin-value.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { createAnalyzer } from '../analyzer.js';\nimport { coinFlows } from './coin-flows.js';\n\nexport interface CoinValueAnalyzerOptions {\n\tgetCoinPrices: (coinTypes: string[]) => Promise<\n\t\t{\n\t\t\tcoinType: string;\n\t\t\tdecimals: number;\n\t\t\tprice: number | null;\n\t\t}[]\n\t>;\n}\n\nexport interface CoinValueAnalysis {\n\tcoinTypesWithoutPrice: string[];\n\ttotal: number;\n\tcoinTypes: {\n\t\tcoinType: string;\n\t\tdecimals: number;\n\t\tprice: number;\n\t\tamount: bigint;\n\t\tconvertedAmount: number;\n\t}[];\n}\n\nexport const coinValues = createAnalyzer({\n\tdependencies: { coinFlows },\n\tanalyze:\n\t\t({ getCoinPrices }: CoinValueAnalyzerOptions) =>\n\t\tasync ({ coinFlows }) => {\n\t\t\tconst prices = await getCoinPrices(coinFlows.outflows.map((cf) => cf.coinType));\n\n\t\t\tlet total = 0;\n\t\t\tconst coinTypesWithoutPrice: string[] = [];\n\n\t\t\tconst coinTypes: {\n\t\t\t\tcoinType: string;\n\t\t\t\tdecimals: number;\n\t\t\t\tprice: number;\n\t\t\t\tamount: bigint;\n\t\t\t\tconvertedAmount: number;\n\t\t\t}[] = [];\n\n\t\t\tfor (const flow of coinFlows.outflows) {\n\t\t\t\tif (flow.amount > 0n) {\n\t\t\t\t\tconst result = prices.find((p) => p.coinType === flow.coinType);\n\n\t\t\t\t\tif (result?.price != null) {\n\t\t\t\t\t\tconst amount = (Number(flow.amount) / 10 ** result.decimals) * result.price;\n\t\t\t\t\t\ttotal += amount;\n\t\t\t\t\t\tcoinTypes.push({\n\t\t\t\t\t\t\tcoinType: flow.coinType,\n\t\t\t\t\t\t\tdecimals: result.decimals,\n\t\t\t\t\t\t\tprice: result.price,\n\t\t\t\t\t\t\tamount: flow.amount,\n\t\t\t\t\t\t\tconvertedAmount: amount,\n\t\t\t\t\t\t});\n\t\t\t\t\t} else {\n\t\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