@suilend/sui-fe 0.3.1 → 0.3.2
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 +11 -5
- package/lib/keypair.js +92 -43
- package/package.json +1 -1
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 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;
|
|
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,103 @@ 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.
|
|
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;
|
|
16
18
|
const ed25519_1 = require("@mysten/sui/keypairs/ed25519");
|
|
17
19
|
const transactions_1 = require("@mysten/sui/transactions");
|
|
18
20
|
const utils_1 = require("@mysten/sui/utils");
|
|
19
21
|
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
20
22
|
const coinType_1 = require("./coinType");
|
|
21
23
|
const transactions_2 = require("./transactions");
|
|
24
|
+
// SIGN, EXECUTE, AND WAIT FOR
|
|
25
|
+
class TransactionStatusError extends Error {
|
|
26
|
+
constructor(message) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.name = "TransactionStatusError";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.TransactionStatusError = TransactionStatusError;
|
|
32
|
+
const keypairWaitForTransaction = (digest, suiClient) => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
|
+
var _a, _b;
|
|
34
|
+
const res = yield suiClient.waitForTransaction({
|
|
35
|
+
digest,
|
|
36
|
+
options: {
|
|
37
|
+
showBalanceChanges: true,
|
|
38
|
+
showEffects: true,
|
|
39
|
+
showEvents: true,
|
|
40
|
+
showObjectChanges: true,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
if (((_a = res.effects) === null || _a === void 0 ? void 0 : _a.status) !== undefined &&
|
|
44
|
+
res.effects.status.status === "failure")
|
|
45
|
+
throw new TransactionStatusError((_b = res.effects.status.error) !== null && _b !== void 0 ? _b : "Transaction failed");
|
|
46
|
+
return res;
|
|
47
|
+
});
|
|
48
|
+
exports.keypairWaitForTransaction = keypairWaitForTransaction;
|
|
49
|
+
const keypairSignExecuteAndWaitForTransaction = (transaction, keypair, suiClient, onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
// 1) Sign
|
|
51
|
+
const builtTransaction = yield transaction.build({
|
|
52
|
+
client: suiClient,
|
|
53
|
+
});
|
|
54
|
+
const signedTransaction = yield keypair.signTransaction(builtTransaction);
|
|
55
|
+
onSign === null || onSign === void 0 ? void 0 : onSign(signedTransaction);
|
|
56
|
+
// 2) Execute
|
|
57
|
+
const res1 = yield suiClient.executeTransactionBlock({
|
|
58
|
+
transactionBlock: signedTransaction.bytes,
|
|
59
|
+
signature: signedTransaction.signature,
|
|
60
|
+
});
|
|
61
|
+
onExecute === null || onExecute === void 0 ? void 0 : onExecute(res1);
|
|
62
|
+
// 3) Wait
|
|
63
|
+
const res2 = yield (0, exports.keypairWaitForTransaction)(res1.digest, suiClient);
|
|
64
|
+
return res2;
|
|
65
|
+
});
|
|
66
|
+
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);
|
|
81
|
+
setLastSignedTransaction(undefined);
|
|
82
|
+
}
|
|
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
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
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) {
|
|
103
|
+
return (signedTransaction) => {
|
|
104
|
+
console.log(`[onSign] Setting setLastSignedTransaction for ${type}${index !== undefined ? ` ${index}` : ""}`);
|
|
105
|
+
setLastSignedTransaction({
|
|
106
|
+
signedTransaction,
|
|
107
|
+
type,
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
// CREATE KEYPAIR
|
|
22
112
|
const KEYPAIR_SEED_MESSAGE = "send:wallet-connect";
|
|
23
113
|
const createKeypair = (account, signPersonalMessage) => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
114
|
const message = Buffer.from(KEYPAIR_SEED_MESSAGE);
|
|
@@ -35,6 +125,7 @@ const createKeypair = (account, signPersonalMessage) => __awaiter(void 0, void 0
|
|
|
35
125
|
return { keypair, address, privateKey };
|
|
36
126
|
});
|
|
37
127
|
exports.createKeypair = createKeypair;
|
|
128
|
+
// CHECK IF KEYPAIR CAN BE USED
|
|
38
129
|
const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keypair, suiClient) => __awaiter(void 0, void 0, void 0, function* () {
|
|
39
130
|
var _a, _b;
|
|
40
131
|
const [mostRecentFromAddressTransaction, mostRecentToAddressTransaction] = yield Promise.all([
|
|
@@ -81,48 +172,6 @@ const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keyp
|
|
|
81
172
|
}
|
|
82
173
|
});
|
|
83
174
|
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
175
|
const fundKeypair = (tokens, keypair, signExecuteAndWaitForTransaction, // From WalletContext
|
|
127
176
|
onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
128
177
|
console.log("[fundKeypair] tokens:", tokens.map((t) => ({
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@suilend/sui-fe","version":"0.3.
|
|
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"}}
|