@suilend/sui-fe 0.3.1 → 0.3.3

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
@@ -5,6 +5,17 @@ import { Transaction } from "@mysten/sui/transactions";
5
5
  import { SuiSignPersonalMessageMethod, WalletAccount } from "@mysten/wallet-standard";
6
6
  import BigNumber from "bignumber.js";
7
7
  import { Token } from "./coinMetadata";
8
+ export declare class TransactionStatusError extends Error {
9
+ constructor(message: string);
10
+ }
11
+ export declare const keypairWaitForTransaction: (digest: string, suiClient: SuiClient) => Promise<SuiTransactionBlockResponse>;
12
+ export declare const keypairSignExecuteAndWaitForTransaction: (transaction: Transaction, keypair: Ed25519Keypair, suiClient: SuiClient, onSign?: (signedTransaction: SignatureWithBytes) => void, onExecute?: (res: SuiTransactionBlockResponse) => void) => Promise<SuiTransactionBlockResponse>;
13
+ export type LastSignedTransaction<T> = {
14
+ signedTransaction: SignatureWithBytes;
15
+ type: T;
16
+ };
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;
8
19
  export declare const createKeypair: (account: WalletAccount, signPersonalMessage: SuiSignPersonalMessageMethod) => Promise<{
9
20
  keypair: Ed25519Keypair;
10
21
  address: string;
@@ -13,11 +24,6 @@ export declare const createKeypair: (account: WalletAccount, signPersonalMessage
13
24
  export declare const checkIfKeypairCanBeUsed: (lastSignedTransaction: SignatureWithBytes | undefined, currentFlowDigests: string[], keypair: Ed25519Keypair, suiClient: SuiClient) => Promise<{
14
25
  lastCurrentFlowTransaction: SuiTransactionBlockResponse | undefined;
15
26
  }>;
16
- export declare class TransactionStatusError extends Error {
17
- constructor(message: string);
18
- }
19
- export declare const keypairWaitForTransaction: (digest: string, suiClient: SuiClient) => Promise<SuiTransactionBlockResponse>;
20
- export declare const keypairSignExecuteAndWaitForTransaction: (transaction: Transaction, keypair: Ed25519Keypair, suiClient: SuiClient, onSign?: (signedTransaction: SignatureWithBytes) => void, onExecute?: (res: SuiTransactionBlockResponse) => void) => Promise<SuiTransactionBlockResponse>;
21
27
  export type FundKeypairResult = {
22
28
  res: SuiTransactionBlockResponse;
23
29
  };
package/lib/keypair.js CHANGED
@@ -12,13 +12,101 @@ 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.keypairSignExecuteAndWaitForTransaction = exports.keypairWaitForTransaction = exports.TransactionStatusError = exports.checkIfKeypairCanBeUsed = exports.createKeypair = void 0;
15
+ exports.returnAllOwnedObjectsAndSuiToUser = exports.fundKeypair = exports.checkIfKeypairCanBeUsed = exports.createKeypair = exports.onSign = exports.checkLastTransactionSignature = exports.keypairSignExecuteAndWaitForTransaction = exports.keypairWaitForTransaction = exports.TransactionStatusError = void 0;
16
16
  const ed25519_1 = require("@mysten/sui/keypairs/ed25519");
17
17
  const transactions_1 = require("@mysten/sui/transactions");
18
18
  const utils_1 = require("@mysten/sui/utils");
19
19
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
20
20
  const coinType_1 = require("./coinType");
21
21
  const transactions_2 = require("./transactions");
22
+ // SIGN, EXECUTE, AND WAIT FOR
23
+ class TransactionStatusError extends Error {
24
+ constructor(message) {
25
+ super(message);
26
+ this.name = "TransactionStatusError";
27
+ }
28
+ }
29
+ exports.TransactionStatusError = TransactionStatusError;
30
+ const keypairWaitForTransaction = (digest, suiClient) => __awaiter(void 0, void 0, void 0, function* () {
31
+ var _a, _b;
32
+ const res = yield suiClient.waitForTransaction({
33
+ digest,
34
+ options: {
35
+ showBalanceChanges: true,
36
+ showEffects: true,
37
+ showEvents: true,
38
+ showObjectChanges: true,
39
+ },
40
+ });
41
+ if (((_a = res.effects) === null || _a === void 0 ? void 0 : _a.status) !== undefined &&
42
+ res.effects.status.status === "failure")
43
+ throw new TransactionStatusError((_b = res.effects.status.error) !== null && _b !== void 0 ? _b : "Transaction failed");
44
+ return res;
45
+ });
46
+ exports.keypairWaitForTransaction = keypairWaitForTransaction;
47
+ const keypairSignExecuteAndWaitForTransaction = (transaction, keypair, suiClient, onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
48
+ // 1) Sign
49
+ const builtTransaction = yield transaction.build({
50
+ client: suiClient,
51
+ });
52
+ const signedTransaction = yield keypair.signTransaction(builtTransaction);
53
+ onSign === null || onSign === void 0 ? void 0 : onSign(signedTransaction);
54
+ // 2) Execute
55
+ const res1 = yield suiClient.executeTransactionBlock({
56
+ transactionBlock: signedTransaction.bytes,
57
+ signature: signedTransaction.signature,
58
+ });
59
+ onExecute === null || onExecute === void 0 ? void 0 : onExecute(res1);
60
+ // 3) Wait
61
+ const res2 = yield (0, exports.keypairWaitForTransaction)(res1.digest, suiClient);
62
+ return res2;
63
+ });
64
+ exports.keypairSignExecuteAndWaitForTransaction = keypairSignExecuteAndWaitForTransaction;
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");
84
+ setLastSignedTransaction(undefined);
85
+ yield invalidCallback();
86
+ }
87
+ else {
88
+ throw err;
89
+ }
90
+ }
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) => {
100
+ return (signedTransaction) => {
101
+ console.log(`[onSign] Setting setLastSignedTransaction for ${type}${index !== undefined ? ` ${index}` : ""}`);
102
+ setLastSignedTransaction({
103
+ signedTransaction,
104
+ type,
105
+ });
106
+ };
107
+ };
108
+ exports.onSign = onSign;
109
+ // CREATE KEYPAIR
22
110
  const KEYPAIR_SEED_MESSAGE = "send:wallet-connect";
23
111
  const createKeypair = (account, signPersonalMessage) => __awaiter(void 0, void 0, void 0, function* () {
24
112
  const message = Buffer.from(KEYPAIR_SEED_MESSAGE);
@@ -35,6 +123,7 @@ const createKeypair = (account, signPersonalMessage) => __awaiter(void 0, void 0
35
123
  return { keypair, address, privateKey };
36
124
  });
37
125
  exports.createKeypair = createKeypair;
126
+ // CHECK IF KEYPAIR CAN BE USED
38
127
  const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keypair, suiClient) => __awaiter(void 0, void 0, void 0, function* () {
39
128
  var _a, _b;
40
129
  const [mostRecentFromAddressTransaction, mostRecentToAddressTransaction] = yield Promise.all([
@@ -81,48 +170,6 @@ const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keyp
81
170
  }
82
171
  });
83
172
  exports.checkIfKeypairCanBeUsed = checkIfKeypairCanBeUsed;
84
- class TransactionStatusError extends Error {
85
- constructor(message) {
86
- super(message);
87
- this.name = "TransactionStatusError";
88
- }
89
- }
90
- exports.TransactionStatusError = TransactionStatusError;
91
- const keypairWaitForTransaction = (digest, suiClient) => __awaiter(void 0, void 0, void 0, function* () {
92
- var _a, _b;
93
- const res = yield suiClient.waitForTransaction({
94
- digest,
95
- options: {
96
- showBalanceChanges: true,
97
- showEffects: true,
98
- showEvents: true,
99
- showObjectChanges: true,
100
- },
101
- });
102
- if (((_a = res.effects) === null || _a === void 0 ? void 0 : _a.status) !== undefined &&
103
- res.effects.status.status === "failure")
104
- throw new TransactionStatusError((_b = res.effects.status.error) !== null && _b !== void 0 ? _b : "Transaction failed");
105
- return res;
106
- });
107
- exports.keypairWaitForTransaction = keypairWaitForTransaction;
108
- const keypairSignExecuteAndWaitForTransaction = (transaction, keypair, suiClient, onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
109
- // 1) Sign
110
- const builtTransaction = yield transaction.build({
111
- client: suiClient,
112
- });
113
- const signedTransaction = yield keypair.signTransaction(builtTransaction);
114
- onSign === null || onSign === void 0 ? void 0 : onSign(signedTransaction);
115
- // 2) Execute
116
- const res1 = yield suiClient.executeTransactionBlock({
117
- transactionBlock: signedTransaction.bytes,
118
- signature: signedTransaction.signature,
119
- });
120
- onExecute === null || onExecute === void 0 ? void 0 : onExecute(res1);
121
- // 3) Wait
122
- const res2 = yield (0, exports.keypairWaitForTransaction)(res1.digest, suiClient);
123
- return res2;
124
- });
125
- exports.keypairSignExecuteAndWaitForTransaction = keypairSignExecuteAndWaitForTransaction;
126
173
  const fundKeypair = (tokens, keypair, signExecuteAndWaitForTransaction, // From WalletContext
127
174
  onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
128
175
  console.log("[fundKeypair] tokens:", tokens.map((t) => ({
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@suilend/sui-fe","version":"0.3.1","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.3","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"}}