@7kprotocol/sdk-ts 3.4.2-beta.3 → 3.4.2-beta.5

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 (76) hide show
  1. package/README.md +151 -4
  2. package/lib/cjs/constants/_7k.js +5 -1
  3. package/lib/cjs/constants/sui.js +4 -0
  4. package/lib/cjs/features/metaAg/index.js +243 -0
  5. package/lib/cjs/features/metaAg/providers/bluefin.js +69 -0
  6. package/lib/cjs/features/metaAg/providers/cetus.js +67 -0
  7. package/lib/cjs/features/metaAg/providers/flowx.js +56 -0
  8. package/lib/cjs/features/swap/getQuote.js +4 -1
  9. package/lib/cjs/index.js +6 -1
  10. package/lib/cjs/types/constants/_7k.d.ts +4 -0
  11. package/lib/cjs/types/constants/_7k.d.ts.map +1 -1
  12. package/lib/cjs/types/constants/sui.d.ts +2 -0
  13. package/lib/cjs/types/constants/sui.d.ts.map +1 -0
  14. package/lib/cjs/types/features/metaAg/index.d.ts +28 -0
  15. package/lib/cjs/types/features/metaAg/index.d.ts.map +1 -0
  16. package/lib/cjs/types/features/metaAg/providers/bluefin.d.ts +11 -0
  17. package/lib/cjs/types/features/metaAg/providers/bluefin.d.ts.map +1 -0
  18. package/lib/cjs/types/features/metaAg/providers/cetus.d.ts +13 -0
  19. package/lib/cjs/types/features/metaAg/providers/cetus.d.ts.map +1 -0
  20. package/lib/cjs/types/features/metaAg/providers/flowx.d.ts +14 -0
  21. package/lib/cjs/types/features/metaAg/providers/flowx.d.ts.map +1 -0
  22. package/lib/cjs/types/features/swap/getQuote.d.ts +3 -1
  23. package/lib/cjs/types/features/swap/getQuote.d.ts.map +1 -1
  24. package/lib/cjs/types/index.d.ts +4 -1
  25. package/lib/cjs/types/index.d.ts.map +1 -1
  26. package/lib/cjs/types/metaAg.js +12 -0
  27. package/lib/cjs/types/types/metaAg.d.ts +127 -0
  28. package/lib/cjs/types/types/metaAg.d.ts.map +1 -0
  29. package/lib/cjs/types/utils/ObjectCache.d.ts +87 -0
  30. package/lib/cjs/types/utils/ObjectCache.d.ts.map +1 -0
  31. package/lib/cjs/types/utils/SuiClientUtils.d.ts +15 -0
  32. package/lib/cjs/types/utils/SuiClientUtils.d.ts.map +1 -0
  33. package/lib/cjs/types/utils/condition.d.ts +2 -0
  34. package/lib/cjs/types/utils/condition.d.ts.map +1 -0
  35. package/lib/cjs/types/utils/sui.d.ts.map +1 -1
  36. package/lib/cjs/utils/ObjectCache.js +273 -0
  37. package/lib/cjs/utils/SuiClientUtils.js +50 -0
  38. package/lib/cjs/utils/condition.js +8 -0
  39. package/lib/esm/constants/_7k.js +4 -0
  40. package/lib/esm/constants/sui.js +1 -0
  41. package/lib/esm/features/metaAg/index.js +209 -0
  42. package/lib/esm/features/metaAg/providers/bluefin.js +67 -0
  43. package/lib/esm/features/metaAg/providers/cetus.js +66 -0
  44. package/lib/esm/features/metaAg/providers/flowx.js +56 -0
  45. package/lib/esm/features/swap/getQuote.js +4 -1
  46. package/lib/esm/index.mjs +5 -1
  47. package/lib/esm/types/constants/_7k.d.ts +4 -0
  48. package/lib/esm/types/constants/_7k.d.ts.map +1 -1
  49. package/lib/esm/types/constants/sui.d.ts +2 -0
  50. package/lib/esm/types/constants/sui.d.ts.map +1 -0
  51. package/lib/esm/types/features/metaAg/index.d.ts +28 -0
  52. package/lib/esm/types/features/metaAg/index.d.ts.map +1 -0
  53. package/lib/esm/types/features/metaAg/providers/bluefin.d.ts +11 -0
  54. package/lib/esm/types/features/metaAg/providers/bluefin.d.ts.map +1 -0
  55. package/lib/esm/types/features/metaAg/providers/cetus.d.ts +13 -0
  56. package/lib/esm/types/features/metaAg/providers/cetus.d.ts.map +1 -0
  57. package/lib/esm/types/features/metaAg/providers/flowx.d.ts +14 -0
  58. package/lib/esm/types/features/metaAg/providers/flowx.d.ts.map +1 -0
  59. package/lib/esm/types/features/swap/getQuote.d.ts +3 -1
  60. package/lib/esm/types/features/swap/getQuote.d.ts.map +1 -1
  61. package/lib/esm/types/index.d.ts +4 -1
  62. package/lib/esm/types/index.d.ts.map +1 -1
  63. package/lib/esm/types/metaAg.js +8 -0
  64. package/lib/esm/types/types/metaAg.d.ts +127 -0
  65. package/lib/esm/types/types/metaAg.d.ts.map +1 -0
  66. package/lib/esm/types/utils/ObjectCache.d.ts +87 -0
  67. package/lib/esm/types/utils/ObjectCache.d.ts.map +1 -0
  68. package/lib/esm/types/utils/SuiClientUtils.d.ts +15 -0
  69. package/lib/esm/types/utils/SuiClientUtils.d.ts.map +1 -0
  70. package/lib/esm/types/utils/condition.d.ts +2 -0
  71. package/lib/esm/types/utils/condition.d.ts.map +1 -0
  72. package/lib/esm/types/utils/sui.d.ts.map +1 -1
  73. package/lib/esm/utils/ObjectCache.js +250 -0
  74. package/lib/esm/utils/SuiClientUtils.js +34 -0
  75. package/lib/esm/utils/condition.js +5 -0
  76. package/package.json +8 -1
@@ -0,0 +1,250 @@
1
+ import { normalizeSuiAddress } from "@mysten/sui/utils";
2
+ export class AsyncCache {
3
+ async getObject(id) {
4
+ const [owned, shared] = await Promise.all([
5
+ this.get("OwnedObject", id),
6
+ this.get("SharedOrImmutableObject", id),
7
+ ]);
8
+ return owned ?? shared ?? null;
9
+ }
10
+ async getObjects(ids) {
11
+ return Promise.all(ids.map((id) => this.getObject(id)));
12
+ }
13
+ async addObject(object) {
14
+ if (object.owner) {
15
+ await this.set("OwnedObject", object.objectId, object);
16
+ }
17
+ else {
18
+ await this.set("SharedOrImmutableObject", object.objectId, object);
19
+ }
20
+ return object;
21
+ }
22
+ async addObjects(objects) {
23
+ await Promise.all(objects.map(async (object) => this.addObject(object)));
24
+ }
25
+ async deleteObject(id) {
26
+ await Promise.all([
27
+ this.delete("OwnedObject", id),
28
+ this.delete("SharedOrImmutableObject", id),
29
+ ]);
30
+ }
31
+ async deleteObjects(ids) {
32
+ await Promise.all(ids.map((id) => this.deleteObject(id)));
33
+ }
34
+ async getMoveFunctionDefinition(ref) {
35
+ const functionName = `${normalizeSuiAddress(ref.package)}::${ref.module}::${ref.function}`;
36
+ return this.get("MoveFunction", functionName);
37
+ }
38
+ async addMoveFunctionDefinition(functionEntry) {
39
+ const pkg = normalizeSuiAddress(functionEntry.package);
40
+ const functionName = `${pkg}::${functionEntry.module}::${functionEntry.function}`;
41
+ const entry = {
42
+ ...functionEntry,
43
+ package: pkg,
44
+ };
45
+ await this.set("MoveFunction", functionName, entry);
46
+ return entry;
47
+ }
48
+ async deleteMoveFunctionDefinition(ref) {
49
+ const functionName = `${normalizeSuiAddress(ref.package)}::${ref.module}::${ref.function}`;
50
+ await this.delete("MoveFunction", functionName);
51
+ }
52
+ async getCustom(key) {
53
+ return this.get("Custom", key);
54
+ }
55
+ async setCustom(key, value) {
56
+ return this.set("Custom", key, value);
57
+ }
58
+ async deleteCustom(key) {
59
+ return this.delete("Custom", key);
60
+ }
61
+ }
62
+ export class InMemoryObjectCache extends AsyncCache {
63
+ #caches = {
64
+ OwnedObject: new Map(),
65
+ SharedOrImmutableObject: new Map(),
66
+ MoveFunction: new Map(),
67
+ Custom: new Map(),
68
+ };
69
+ async get(type, key) {
70
+ return this.#caches[type].get(key) ?? null;
71
+ }
72
+ async set(type, key, value) {
73
+ this.#caches[type].set(key, value);
74
+ }
75
+ async delete(type, key) {
76
+ this.#caches[type].delete(key);
77
+ }
78
+ async clear(type) {
79
+ if (type) {
80
+ this.#caches[type].clear();
81
+ }
82
+ else {
83
+ for (const cache of Object.values(this.#caches)) {
84
+ cache.clear();
85
+ }
86
+ }
87
+ }
88
+ }
89
+ export class CustomObjectCache {
90
+ #client;
91
+ #cache;
92
+ #onEffects;
93
+ constructor({ cache = new InMemoryObjectCache(), onEffects, client, }) {
94
+ this.#client = client;
95
+ this.#cache = cache;
96
+ this.#onEffects = onEffects;
97
+ }
98
+ async applyCache(transactionData, unresolvedObjects) {
99
+ const cached = (await this.#cache.getObjects(unresolvedObjects)).filter((obj) => obj !== null);
100
+ const byId = new Map(cached.map((obj) => [obj.objectId, obj]));
101
+ const unresolved = [];
102
+ for (const input of transactionData.inputs) {
103
+ if (!input.UnresolvedObject) {
104
+ continue;
105
+ }
106
+ const cached = byId.get(input.UnresolvedObject.objectId);
107
+ if (!cached) {
108
+ unresolved.push(input.UnresolvedObject.objectId);
109
+ continue;
110
+ }
111
+ if (cached.initialSharedVersion &&
112
+ !input.UnresolvedObject.initialSharedVersion) {
113
+ input.UnresolvedObject.initialSharedVersion =
114
+ cached.initialSharedVersion;
115
+ }
116
+ else {
117
+ if (cached.version && !input.UnresolvedObject.version) {
118
+ input.UnresolvedObject.version = cached.version;
119
+ }
120
+ if (cached.digest && !input.UnresolvedObject.digest) {
121
+ input.UnresolvedObject.digest = cached.digest;
122
+ }
123
+ }
124
+ }
125
+ return unresolved;
126
+ }
127
+ async resolveObjects(transactionData, unresolved) {
128
+ const unresolvedObjects = await this.applyCache(transactionData, unresolved);
129
+ if (unresolvedObjects.length === 0) {
130
+ return [];
131
+ }
132
+ const dedup = [...new Set(unresolvedObjects)];
133
+ const res = await this.#client.multiGetObjects({
134
+ ids: dedup,
135
+ options: {
136
+ showOwner: true,
137
+ },
138
+ });
139
+ const objects = [];
140
+ for (const obj of res || []) {
141
+ if (!obj.data) {
142
+ continue;
143
+ }
144
+ const owner = obj.data.owner;
145
+ const initialSharedVersion = owner && typeof owner === "object" && "Shared" in owner
146
+ ? owner.Shared.initial_shared_version
147
+ : null;
148
+ const ownerAddress = owner && typeof owner === "object" && "AddressOwner" in owner
149
+ ? owner.AddressOwner
150
+ : null;
151
+ const cached = {
152
+ objectId: obj.data.objectId,
153
+ version: obj.data.version,
154
+ digest: obj.data.digest,
155
+ initialSharedVersion,
156
+ owner: ownerAddress,
157
+ };
158
+ objects.push(cached);
159
+ }
160
+ await this.#cache.addObjects(objects);
161
+ return await this.applyCache(transactionData, unresolvedObjects);
162
+ }
163
+ asPlugin() {
164
+ return async (transactionData, _options, next) => {
165
+ const unresolvedObjects = transactionData.inputs
166
+ .filter((input) => input.UnresolvedObject)
167
+ .map((input) => input.UnresolvedObject.objectId);
168
+ // if object is not in cache yet, fetch from RPC and save to cache and resolve all objects
169
+ await this.resolveObjects(transactionData, unresolvedObjects);
170
+ await Promise.all(transactionData.commands.map(async (commands) => {
171
+ if (commands.MoveCall) {
172
+ const def = await this.getMoveFunctionDefinition({
173
+ package: commands.MoveCall.package,
174
+ module: commands.MoveCall.module,
175
+ function: commands.MoveCall.function,
176
+ });
177
+ if (def) {
178
+ commands.MoveCall._argumentTypes = def.parameters;
179
+ }
180
+ }
181
+ }));
182
+ await next();
183
+ await Promise.all(transactionData.commands.map(async (commands) => {
184
+ if (commands.MoveCall?._argumentTypes) {
185
+ await this.#cache.addMoveFunctionDefinition({
186
+ package: commands.MoveCall.package,
187
+ module: commands.MoveCall.module,
188
+ function: commands.MoveCall.function,
189
+ parameters: commands.MoveCall._argumentTypes,
190
+ });
191
+ }
192
+ }));
193
+ };
194
+ }
195
+ async clear() {
196
+ await this.#cache.clear();
197
+ }
198
+ async getMoveFunctionDefinition(ref) {
199
+ return this.#cache.getMoveFunctionDefinition(ref);
200
+ }
201
+ async getObjects(ids) {
202
+ return this.#cache.getObjects(ids);
203
+ }
204
+ async deleteObjects(ids) {
205
+ return this.#cache.deleteObjects(ids);
206
+ }
207
+ async clearOwnedObjects() {
208
+ await this.#cache.clear("OwnedObject");
209
+ }
210
+ async clearCustom() {
211
+ await this.#cache.clear("Custom");
212
+ }
213
+ async getCustom(key) {
214
+ return this.#cache.getCustom(key);
215
+ }
216
+ async setCustom(key, value) {
217
+ return this.#cache.setCustom(key, value);
218
+ }
219
+ async deleteCustom(key) {
220
+ return this.#cache.deleteCustom(key);
221
+ }
222
+ async applyEffects(effects) {
223
+ if (!effects.V2) {
224
+ throw new Error(`Unsupported transaction effects version ${effects.$kind}`);
225
+ }
226
+ const { lamportVersion, changedObjects } = effects.V2;
227
+ const deletedIds = [];
228
+ const addedObjects = [];
229
+ changedObjects.forEach(([id, change]) => {
230
+ if (change.outputState.NotExist) {
231
+ deletedIds.push(id);
232
+ }
233
+ else if (change.outputState.ObjectWrite) {
234
+ const [digest, owner] = change.outputState.ObjectWrite;
235
+ addedObjects.push({
236
+ objectId: id,
237
+ digest,
238
+ version: lamportVersion,
239
+ owner: owner.AddressOwner ?? owner.ObjectOwner ?? null,
240
+ initialSharedVersion: owner.Shared?.initialSharedVersion ?? null,
241
+ });
242
+ }
243
+ });
244
+ await Promise.all([
245
+ this.#cache.addObjects(addedObjects),
246
+ this.#cache.deleteObjects(deletedIds),
247
+ this.#onEffects?.(effects),
248
+ ]);
249
+ }
250
+ }
@@ -0,0 +1,34 @@
1
+ import { Transaction } from "@mysten/sui/transactions";
2
+ import { CustomObjectCache } from "./ObjectCache";
3
+ /**
4
+ * Utility class for interacting with Sui blockchain coins
5
+ */
6
+ export class SuiClientUtils {
7
+ client;
8
+ #devInspectCache;
9
+ /**
10
+ * Creates a new instance of SuiClientUtils
11
+ * @param client - The Sui client instance to use for blockchain interactions
12
+ */
13
+ constructor(client) {
14
+ this.client = client;
15
+ this.#devInspectCache = new CustomObjectCache({ client: this.client });
16
+ }
17
+ devInspectTransactionBlock = async (params) => {
18
+ if (params.transactionBlock instanceof Transaction) {
19
+ params.transactionBlock.addBuildPlugin(this.#devInspectCache.asPlugin());
20
+ }
21
+ try {
22
+ const res = await this.client.devInspectTransactionBlock(params);
23
+ return res;
24
+ }
25
+ catch (error) {
26
+ // clear all cache if devInspectTransactionBlock fails
27
+ await this.#devInspectCache.clear();
28
+ throw error;
29
+ }
30
+ finally {
31
+ await this.#devInspectCache.clearOwnedObjects();
32
+ }
33
+ };
34
+ }
@@ -0,0 +1,5 @@
1
+ export function assert(condition, message) {
2
+ if (!condition) {
3
+ throw new Error(message || "Assertion failed");
4
+ }
5
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@7kprotocol/sdk-ts",
3
- "version": "3.4.2-beta.3",
3
+ "version": "3.4.2-beta.5",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -73,5 +73,12 @@
73
73
  "peerDependencies": {
74
74
  "@mysten/sui": "^1.39.0",
75
75
  "@pythnetwork/pyth-sui-js": "^2.2.0"
76
+ },
77
+ "dependencies": {
78
+ "uuid": "^13.0.0"
79
+ },
80
+ "optionalDependencies": {
81
+ "@cetusprotocol/aggregator-sdk": "^1.4.1",
82
+ "@flowx-finance/sdk": "^1.13.8"
76
83
  }
77
84
  }