@suilend/sui-fe 0.3.2 → 0.3.4

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.
package/lib/keypair.d.ts CHANGED
@@ -14,8 +14,8 @@ export type LastSignedTransaction<T> = {
14
14
  signedTransaction: SignatureWithBytes;
15
15
  type: T;
16
16
  };
17
- export declare function checkLastTransactionSignature<T>(type: T, lastCurrentFlowTransaction: SuiTransactionBlockResponse | undefined, lastSignedTransaction: LastSignedTransaction<T> | undefined, setLastSignedTransaction: (value: LastSignedTransaction<T> | undefined) => void, suiClient: SuiClient, validCallback: (res: SuiTransactionBlockResponse) => Promise<void>, invalidCallback: () => Promise<void>): Promise<void>;
18
- export declare function onSign<T>(type: T, setLastSignedTransaction: (value: LastSignedTransaction<T> | undefined) => void, index?: number): (signedTransaction: SignatureWithBytes) => void;
17
+ export declare const checkLastTransactionSignature: <T>(type: T, lastCurrentFlowTransaction: SuiTransactionBlockResponse | undefined, lastSignedTransaction: LastSignedTransaction<T> | undefined, setLastSignedTransaction: (value: LastSignedTransaction<T> | undefined) => void, suiClient: SuiClient, validCallback: (res: SuiTransactionBlockResponse) => Promise<void>, invalidCallback: () => Promise<void>) => Promise<void>;
18
+ export declare const onSign: <T>(type: T, setLastSignedTransaction: (value: LastSignedTransaction<T> | undefined) => void, index?: number) => (signedTransaction: SignatureWithBytes) => void;
19
19
  export declare const createKeypair: (account: WalletAccount, signPersonalMessage: SuiSignPersonalMessageMethod) => Promise<{
20
20
  keypair: Ed25519Keypair;
21
21
  address: string;
package/lib/keypair.js CHANGED
@@ -12,9 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.returnAllOwnedObjectsAndSuiToUser = exports.fundKeypair = exports.checkIfKeypairCanBeUsed = exports.createKeypair = exports.keypairSignExecuteAndWaitForTransaction = exports.keypairWaitForTransaction = exports.TransactionStatusError = void 0;
16
- exports.checkLastTransactionSignature = checkLastTransactionSignature;
17
- exports.onSign = onSign;
15
+ exports.returnAllOwnedObjectsAndSuiToUser = exports.fundKeypair = exports.checkIfKeypairCanBeUsed = exports.createKeypair = exports.onSign = exports.checkLastTransactionSignature = exports.keypairSignExecuteAndWaitForTransaction = exports.keypairWaitForTransaction = exports.TransactionStatusError = void 0;
18
16
  const ed25519_1 = require("@mysten/sui/keypairs/ed25519");
19
17
  const transactions_1 = require("@mysten/sui/transactions");
20
18
  const utils_1 = require("@mysten/sui/utils");
@@ -64,42 +62,41 @@ const keypairSignExecuteAndWaitForTransaction = (transaction, keypair, suiClient
64
62
  return res2;
65
63
  });
66
64
  exports.keypairSignExecuteAndWaitForTransaction = keypairSignExecuteAndWaitForTransaction;
67
- function checkLastTransactionSignature(type, lastCurrentFlowTransaction, lastSignedTransaction, setLastSignedTransaction, suiClient, validCallback, invalidCallback) {
68
- return __awaiter(this, void 0, void 0, function* () {
69
- var _a, _b;
70
- console.log("[checkLastTransactionSignature]", {
71
- type,
72
- lastCurrentFlowTransaction,
73
- lastSignedTransaction,
74
- });
75
- if ((lastSignedTransaction === null || lastSignedTransaction === void 0 ? void 0 : lastSignedTransaction.type) === type &&
76
- ((_b = (_a = lastCurrentFlowTransaction === null || lastCurrentFlowTransaction === void 0 ? void 0 : lastCurrentFlowTransaction.transaction) === null || _a === void 0 ? void 0 : _a.txSignatures) !== null && _b !== void 0 ? _b : []).includes(lastSignedTransaction.signedTransaction.signature)) {
77
- console.log("[checkLastTransactionSignature] lastSignedTransaction type and signature MATCH");
78
- try {
79
- const res = yield (0, exports.keypairWaitForTransaction)(lastCurrentFlowTransaction.digest, suiClient); // Assumed to only throw if the transaction was executed but failed (e.g. insufficient gas), or if the connection was lost
80
- yield validCallback(res);
65
+ const checkLastTransactionSignature = (type, lastCurrentFlowTransaction, lastSignedTransaction, setLastSignedTransaction, suiClient, validCallback, invalidCallback) => __awaiter(void 0, void 0, void 0, function* () {
66
+ var _a, _b;
67
+ console.log("[checkLastTransactionSignature]", {
68
+ type,
69
+ lastCurrentFlowTransaction,
70
+ lastSignedTransaction,
71
+ });
72
+ if ((lastSignedTransaction === null || lastSignedTransaction === void 0 ? void 0 : lastSignedTransaction.type) === type &&
73
+ ((_b = (_a = lastCurrentFlowTransaction === null || lastCurrentFlowTransaction === void 0 ? void 0 : lastCurrentFlowTransaction.transaction) === null || _a === void 0 ? void 0 : _a.txSignatures) !== null && _b !== void 0 ? _b : []).includes(lastSignedTransaction.signedTransaction.signature)) {
74
+ console.log("[checkLastTransactionSignature] lastSignedTransaction type and signature MATCH");
75
+ try {
76
+ const res = yield (0, exports.keypairWaitForTransaction)(lastCurrentFlowTransaction.digest, suiClient); // Assumed to only throw if the transaction was executed but failed (e.g. insufficient gas), or if the connection was lost
77
+ yield validCallback(res);
78
+ setLastSignedTransaction(undefined);
79
+ }
80
+ catch (err) {
81
+ console.error("[checkLastTransactionSignature]", err);
82
+ if (err instanceof TransactionStatusError) {
83
+ console.log("[checkLastTransactionSignature] TransactionStatusError");
81
84
  setLastSignedTransaction(undefined);
85
+ yield invalidCallback();
82
86
  }
83
- catch (err) {
84
- console.error("[checkLastTransactionSignature]", err);
85
- if (err instanceof TransactionStatusError) {
86
- console.log("[checkLastTransactionSignature] TransactionStatusError");
87
- setLastSignedTransaction(undefined);
88
- yield invalidCallback();
89
- }
90
- else {
91
- throw err;
92
- }
87
+ else {
88
+ throw err;
93
89
  }
94
90
  }
95
- else {
96
- console.log("[checkLastTransactionSignature] lastSignedTransaction type and signature DO NOT MATCH");
97
- setLastSignedTransaction(undefined);
98
- yield invalidCallback();
99
- }
100
- });
101
- }
102
- function onSign(type, setLastSignedTransaction, index) {
91
+ }
92
+ else {
93
+ console.log("[checkLastTransactionSignature] lastSignedTransaction type and signature DO NOT MATCH");
94
+ setLastSignedTransaction(undefined);
95
+ yield invalidCallback();
96
+ }
97
+ });
98
+ exports.checkLastTransactionSignature = checkLastTransactionSignature;
99
+ const onSign = (type, setLastSignedTransaction, index) => {
103
100
  return (signedTransaction) => {
104
101
  console.log(`[onSign] Setting setLastSignedTransaction for ${type}${index !== undefined ? ` ${index}` : ""}`);
105
102
  setLastSignedTransaction({
@@ -107,7 +104,8 @@ function onSign(type, setLastSignedTransaction, index) {
107
104
  type,
108
105
  });
109
106
  };
110
- }
107
+ };
108
+ exports.onSign = onSign;
111
109
  // CREATE KEYPAIR
112
110
  const KEYPAIR_SEED_MESSAGE = "send:wallet-connect";
113
111
  const createKeypair = (account, signPersonalMessage) => __awaiter(void 0, void 0, void 0, function* () {
@@ -137,37 +135,48 @@ const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keyp
137
135
  mostRecentToAddressTransaction,
138
136
  ].sort((a, b) => { var _a, _b; return +((_a = b === null || b === void 0 ? void 0 : b.timestampMs) !== null && _a !== void 0 ? _a : 0) - +((_b = a === null || a === void 0 ? void 0 : a.timestampMs) !== null && _b !== void 0 ? _b : 0); })[0];
139
137
  console.log("[checkIfKeypairCanBeUsed] mostRecentFromAddressTransaction:", mostRecentFromAddressTransaction, "mostRecentToAddressTransaction:", mostRecentToAddressTransaction, "mostRecentFromOrToAddressTransaction:", mostRecentFromOrToAddressTransaction, "lastSignedTransaction:", lastSignedTransaction, "currentFlowDigests:", currentFlowDigests);
138
+ // 1) Check if wallet has no TO/FROM transactions
140
139
  if (mostRecentFromOrToAddressTransaction === undefined) {
141
- // Wallet with no TO/FROM transactions
140
+ // Wallet has no TO/FROM transactions
142
141
  // CONTINUE
143
142
  return { lastCurrentFlowTransaction: undefined };
144
143
  }
145
144
  else {
146
- // Wallet with TO/FROM transactions
145
+ // 2) Check if resuming/retrying current flow
147
146
  if ((lastSignedTransaction !== undefined &&
148
147
  ((_b = (_a = mostRecentFromOrToAddressTransaction.transaction) === null || _a === void 0 ? void 0 : _a.txSignatures) !== null && _b !== void 0 ? _b : []).includes(lastSignedTransaction.signature)) ||
149
148
  currentFlowDigests.includes(mostRecentFromOrToAddressTransaction.digest)) {
150
- // Retrying current flow
149
+ // Resuming/retrying current flow
151
150
  // CONTINUE
152
151
  return {
153
152
  lastCurrentFlowTransaction: mostRecentFromOrToAddressTransaction,
154
153
  };
155
154
  }
156
155
  else {
157
- // Previous flow
158
- const timestampMs = mostRecentFromOrToAddressTransaction.timestampMs;
159
- const isRecent = !!timestampMs && +timestampMs > Date.now() - 1000 * 60 * 2; // Less than 2 minutes ago
160
- console.log("[checkIfKeypairCanBeUsed] isRecent:", isRecent, timestampMs, Date.now() - 1000 * 60 * 2);
161
- if (isRecent) {
162
- // Previous flow may be ongoing
163
- // STOP
164
- throw new Error("Please wait for previous flow to complete");
165
- }
166
- else {
167
- // Previous flow stopped before completion
156
+ // 3) Check if wallet has no owned objects
157
+ const ownedObjectIds = (yield (0, transactions_2.getAllOwnedObjects)(suiClient, keypair.toSuiAddress())).map((obj) => { var _a; return (_a = obj.data) === null || _a === void 0 ? void 0 : _a.objectId; });
158
+ console.log("[checkIfKeypairCanBeUsed] ownedObjectIds:", ownedObjectIds);
159
+ if (ownedObjectIds.length === 0) {
160
+ // Previous flow completed
168
161
  // CONTINUE
169
162
  return { lastCurrentFlowTransaction: undefined };
170
163
  }
164
+ else {
165
+ // 4) Check if last transaction from previous flow is recent
166
+ const timestampMs = mostRecentFromOrToAddressTransaction.timestampMs;
167
+ const isRecent = !!timestampMs && +timestampMs > Date.now() - 1000 * 60 * 5; // Less than 5 minutes ago
168
+ console.log("[checkIfKeypairCanBeUsed] isRecent:", isRecent);
169
+ if (isRecent) {
170
+ // Last transaction from previous flow is recent
171
+ // STOP
172
+ throw new Error("Please wait for previous flow to complete");
173
+ }
174
+ else {
175
+ // Last transaction from previous flow is NOT recent
176
+ // CONTINUE
177
+ return { lastCurrentFlowTransaction: undefined };
178
+ }
179
+ }
171
180
  }
172
181
  }
173
182
  });
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@suilend/sui-fe","version":"0.3.2","private":false,"description":"A collection of TypeScript frontend libraries","author":"Suilend","license":"MIT","main":"./index.js","exports":{".":"./index.js","./lib/api":"./lib/api.js","./lib/coin":"./lib/coin.js","./lib/coinMetadata":"./lib/coinMetadata.js","./lib/coinType":"./lib/coinType.js","./lib/constants":"./lib/constants.js","./lib/format":"./lib/format.js","./lib":"./lib/index.js","./lib/indexedDB":"./lib/indexedDB.js","./lib/keypair":"./lib/keypair.js","./lib/msafe":"./lib/msafe.js","./lib/track":"./lib/track.js","./lib/transactions":"./lib/transactions.js"},"types":"./index.js","scripts":{"build":"rm -rf ./dist && bun tsc","eslint":"eslint --fix \"./src/**/*.ts\"","prettier":"prettier --write \"./src/**/*\"","lint":"bun eslint && bun prettier && bun tsc --noEmit","release":"bun run build && bun ts-node ./release.ts && cd ./dist && npm publish --access public"},"repository":{"type":"git","url":"git+https://github.com/suilend/sui-fe.git"},"bugs":{"url":"https://github.com/suilend/sui-fe/issues"},"dependencies":{"@mysten/wallet-standard":"0.14.7","@pythnetwork/pyth-sui-js":"^2.1.0","bignumber.js":"^9.1.2","lodash":"^4.17.21","mixpanel-browser":"^2.56.0","next":"^15.0.3","p-limit":"3.1.0"},"devDependencies":{"@tsconfig/recommended":"^1.0.8","@types/lodash":"^4.17.13","@types/mixpanel-browser":"^2.50.2","@types/node":"^22.9.0","@typescript-eslint/eslint-plugin":"^8.14.0","@typescript-eslint/parser":"^8.14.0","eslint":"^9.14.0","eslint-config-next":"^15.0.3","eslint-config-prettier":"^9.1.0","eslint-plugin-import":"^2.31.0","eslint-plugin-prettier":"^5.2.1","prettier":"^3.3.3","ts-node":"^10.9.2","typescript":"^5.6.3"},"peerDependencies":{"@mysten/sui":"1.28.2","date-fns":"^4.1.0"}}
1
+ {"name":"@suilend/sui-fe","version":"0.3.4","private":false,"description":"A collection of TypeScript frontend libraries","author":"Suilend","license":"MIT","main":"./index.js","exports":{".":"./index.js","./lib/api":"./lib/api.js","./lib/coin":"./lib/coin.js","./lib/coinMetadata":"./lib/coinMetadata.js","./lib/coinType":"./lib/coinType.js","./lib/constants":"./lib/constants.js","./lib/format":"./lib/format.js","./lib":"./lib/index.js","./lib/indexedDB":"./lib/indexedDB.js","./lib/keypair":"./lib/keypair.js","./lib/msafe":"./lib/msafe.js","./lib/track":"./lib/track.js","./lib/transactions":"./lib/transactions.js"},"types":"./index.js","scripts":{"build":"rm -rf ./dist && bun tsc","eslint":"eslint --fix \"./src/**/*.ts\"","prettier":"prettier --write \"./src/**/*\"","lint":"bun eslint && bun prettier && bun tsc --noEmit","release":"bun run build && bun ts-node ./release.ts && cd ./dist && npm publish --access public"},"repository":{"type":"git","url":"git+https://github.com/suilend/sui-fe.git"},"bugs":{"url":"https://github.com/suilend/sui-fe/issues"},"dependencies":{"@mysten/wallet-standard":"0.14.7","@pythnetwork/pyth-sui-js":"^2.1.0","bignumber.js":"^9.1.2","lodash":"^4.17.21","mixpanel-browser":"^2.56.0","next":"^15.0.3","p-limit":"3.1.0"},"devDependencies":{"@tsconfig/recommended":"^1.0.8","@types/lodash":"^4.17.13","@types/mixpanel-browser":"^2.50.2","@types/node":"^22.9.0","@typescript-eslint/eslint-plugin":"^8.14.0","@typescript-eslint/parser":"^8.14.0","eslint":"^9.14.0","eslint-config-next":"^15.0.3","eslint-config-prettier":"^9.1.0","eslint-plugin-import":"^2.31.0","eslint-plugin-prettier":"^5.2.1","prettier":"^3.3.3","ts-node":"^10.9.2","typescript":"^5.6.3"},"peerDependencies":{"@mysten/sui":"1.28.2","date-fns":"^4.1.0"}}