@suilend/sui-fe 0.3.52 → 0.4.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.
- package/index.js +1 -17
- package/lib/api.js +8 -13
- package/lib/coin.js +1 -4
- package/lib/coinMetadata.d.ts +1 -1
- package/lib/coinMetadata.js +23 -32
- package/lib/coinType.js +277 -297
- package/lib/constants.js +13 -19
- package/lib/format.js +46 -68
- package/lib/index.js +11 -27
- package/lib/indexedDB.js +3 -9
- package/lib/keypair.d.ts +19 -12
- package/lib/keypair.js +53 -67
- package/lib/ledger.d.ts +2 -2
- package/lib/ledger.js +7 -14
- package/lib/msafe.js +3 -9
- package/lib/transactions.d.ts +16 -7
- package/lib/transactions.js +33 -43
- package/package.json +1 -1
package/lib/keypair.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,44 +7,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
8
|
});
|
|
10
9
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const utils_1 = require("@mysten/sui/utils");
|
|
19
|
-
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
20
|
-
const coinType_1 = require("./coinType");
|
|
21
|
-
const transactions_2 = require("./transactions");
|
|
22
|
-
const constants_1 = require("./constants");
|
|
10
|
+
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
|
|
11
|
+
import { Transaction, } from "@mysten/sui/transactions";
|
|
12
|
+
import { fromBase64, toHex } from "@mysten/sui/utils";
|
|
13
|
+
import BigNumber from "bignumber.js";
|
|
14
|
+
import { isSui } from "./coinType";
|
|
15
|
+
import { getAllCoins, getAllOwnedObjects, getMostRecentAddressTransaction, mergeAllCoins, } from "./transactions";
|
|
16
|
+
import { API_URL } from "./constants";
|
|
23
17
|
// SIGN, EXECUTE, AND WAIT FOR
|
|
24
|
-
class TransactionStatusError extends Error {
|
|
18
|
+
export class TransactionStatusError extends Error {
|
|
25
19
|
constructor(message) {
|
|
26
20
|
super(message);
|
|
27
21
|
this.name = "TransactionStatusError";
|
|
28
22
|
}
|
|
29
23
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const res = yield suiClient.waitForTransaction({
|
|
24
|
+
export const keypairWaitForTransaction = (digest, suiClient) => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
26
|
+
const res = yield suiClient.core.waitForTransaction({
|
|
34
27
|
digest,
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
28
|
+
include: {
|
|
29
|
+
balanceChanges: true,
|
|
30
|
+
effects: true,
|
|
31
|
+
events: true,
|
|
32
|
+
objectChanges: true,
|
|
40
33
|
},
|
|
41
34
|
});
|
|
42
|
-
if (((_a = res.
|
|
43
|
-
res.effects.status.
|
|
44
|
-
throw new TransactionStatusError((
|
|
35
|
+
if (((_b = (_a = res.Transaction) === null || _a === void 0 ? void 0 : _a.effects) === null || _b === void 0 ? void 0 : _b.status) !== undefined &&
|
|
36
|
+
!!((_d = (_c = res.Transaction) === null || _c === void 0 ? void 0 : _c.effects) === null || _d === void 0 ? void 0 : _d.status.error))
|
|
37
|
+
throw new TransactionStatusError((_g = (_f = (_e = res.Transaction) === null || _e === void 0 ? void 0 : _e.effects) === null || _f === void 0 ? void 0 : _f.status.error.message) !== null && _g !== void 0 ? _g : "Transaction failed");
|
|
45
38
|
return res;
|
|
46
39
|
});
|
|
47
|
-
|
|
48
|
-
|
|
40
|
+
export const keypairSignExecuteAndWaitForTransaction = (transaction, keypair, suiClient, onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
41
|
+
var _a, _b;
|
|
49
42
|
// 1) Sign
|
|
50
43
|
const builtTransaction = yield transaction.build({
|
|
51
44
|
client: suiClient,
|
|
@@ -53,28 +46,27 @@ const keypairSignExecuteAndWaitForTransaction = (transaction, keypair, suiClient
|
|
|
53
46
|
const signedTransaction = yield keypair.signTransaction(builtTransaction);
|
|
54
47
|
onSign === null || onSign === void 0 ? void 0 : onSign(signedTransaction);
|
|
55
48
|
// 2) Execute
|
|
56
|
-
const res1 = yield suiClient.
|
|
57
|
-
|
|
58
|
-
|
|
49
|
+
const res1 = yield suiClient.core.executeTransaction({
|
|
50
|
+
transaction: builtTransaction,
|
|
51
|
+
signatures: [signedTransaction.signature],
|
|
59
52
|
});
|
|
60
53
|
onExecute === null || onExecute === void 0 ? void 0 : onExecute(res1);
|
|
61
54
|
// 3) Wait
|
|
62
|
-
const res2 = yield (
|
|
55
|
+
const res2 = yield keypairWaitForTransaction((_b = (_a = res1.Transaction) === null || _a === void 0 ? void 0 : _a.digest) !== null && _b !== void 0 ? _b : "", suiClient);
|
|
63
56
|
return res2;
|
|
64
57
|
});
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
var _a, _b;
|
|
58
|
+
export const checkLastTransactionSignature = (type, lastCurrentFlowTransaction, lastSignedTransaction, setLastSignedTransaction, suiClient, validCallback, invalidCallback) => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
var _a, _b, _c, _d;
|
|
68
60
|
console.log("[checkLastTransactionSignature]", {
|
|
69
61
|
type,
|
|
70
62
|
lastCurrentFlowTransaction,
|
|
71
63
|
lastSignedTransaction,
|
|
72
64
|
});
|
|
73
65
|
if ((lastSignedTransaction === null || lastSignedTransaction === void 0 ? void 0 : lastSignedTransaction.type) === type &&
|
|
74
|
-
((_b = (_a = lastCurrentFlowTransaction === null || lastCurrentFlowTransaction === void 0 ? void 0 : lastCurrentFlowTransaction.
|
|
66
|
+
((_b = (_a = lastCurrentFlowTransaction === null || lastCurrentFlowTransaction === void 0 ? void 0 : lastCurrentFlowTransaction.Transaction) === null || _a === void 0 ? void 0 : _a.signatures) !== null && _b !== void 0 ? _b : []).includes(lastSignedTransaction.signedTransaction.signature)) {
|
|
75
67
|
console.log("[checkLastTransactionSignature] lastSignedTransaction type and signature MATCH");
|
|
76
68
|
try {
|
|
77
|
-
const res = yield (0
|
|
69
|
+
const res = yield keypairWaitForTransaction((_d = (_c = lastCurrentFlowTransaction === null || lastCurrentFlowTransaction === void 0 ? void 0 : lastCurrentFlowTransaction.Transaction) === null || _c === void 0 ? void 0 : _c.digest) !== null && _d !== void 0 ? _d : "", suiClient); // Assumed to only throw if the transaction was executed but failed (e.g. insufficient gas), or if the connection was lost
|
|
78
70
|
yield validCallback(res);
|
|
79
71
|
setLastSignedTransaction(undefined);
|
|
80
72
|
}
|
|
@@ -96,8 +88,7 @@ const checkLastTransactionSignature = (type, lastCurrentFlowTransaction, lastSig
|
|
|
96
88
|
yield invalidCallback();
|
|
97
89
|
}
|
|
98
90
|
});
|
|
99
|
-
|
|
100
|
-
const onSign = (type, setLastSignedTransaction, index) => {
|
|
91
|
+
export const onSign = (type, setLastSignedTransaction, index) => {
|
|
101
92
|
return (signedTransaction) => {
|
|
102
93
|
console.log(`[onSign] Setting setLastSignedTransaction for ${type}${index !== undefined ? ` ${index}` : ""}`);
|
|
103
94
|
setLastSignedTransaction({
|
|
@@ -106,17 +97,16 @@ const onSign = (type, setLastSignedTransaction, index) => {
|
|
|
106
97
|
});
|
|
107
98
|
};
|
|
108
99
|
};
|
|
109
|
-
exports.onSign = onSign;
|
|
110
100
|
// CREATE KEYPAIR
|
|
111
|
-
|
|
112
|
-
const createKeypair = (signature) => __awaiter(void 0, void 0, void 0, function* () {
|
|
113
|
-
const signatureBytes =
|
|
114
|
-
const signatureHex =
|
|
115
|
-
const keypair =
|
|
101
|
+
export const KEYPAIR_SEED_MESSAGE = "send:wallet-connect";
|
|
102
|
+
export const createKeypair = (signature) => __awaiter(void 0, void 0, void 0, function* () {
|
|
103
|
+
const signatureBytes = fromBase64(signature);
|
|
104
|
+
const signatureHex = toHex(signatureBytes);
|
|
105
|
+
const keypair = Ed25519Keypair.deriveKeypairFromSeed(signatureHex);
|
|
116
106
|
const address = keypair.toSuiAddress();
|
|
117
107
|
const privateKey = keypair.getSecretKey();
|
|
118
108
|
console.log("[createKeypair] keypair:", keypair, "address:", address);
|
|
119
|
-
yield fetch(`${
|
|
109
|
+
yield fetch(`${API_URL}/steamm/proxy-wallet-mapping`, {
|
|
120
110
|
method: "POST",
|
|
121
111
|
body: JSON.stringify({
|
|
122
112
|
proxyWalletAddress: address,
|
|
@@ -126,13 +116,12 @@ const createKeypair = (signature) => __awaiter(void 0, void 0, void 0, function*
|
|
|
126
116
|
});
|
|
127
117
|
return { keypair, address, privateKey };
|
|
128
118
|
});
|
|
129
|
-
exports.createKeypair = createKeypair;
|
|
130
119
|
// CHECK IF KEYPAIR CAN BE USED
|
|
131
|
-
const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keypair, suiClient) => __awaiter(void 0, void 0, void 0, function* () {
|
|
120
|
+
export const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keypair, suiClient, suiJsonRpcClient) => __awaiter(void 0, void 0, void 0, function* () {
|
|
132
121
|
var _a, _b;
|
|
133
122
|
const [mostRecentFromAddressTransaction, mostRecentToAddressTransaction] = yield Promise.all([
|
|
134
|
-
|
|
135
|
-
|
|
123
|
+
getMostRecentAddressTransaction(suiJsonRpcClient, keypair.toSuiAddress(), "from"),
|
|
124
|
+
getMostRecentAddressTransaction(suiJsonRpcClient, keypair.toSuiAddress(), "to"),
|
|
136
125
|
]);
|
|
137
126
|
const mostRecentFromOrToAddressTransaction = [
|
|
138
127
|
mostRecentFromAddressTransaction,
|
|
@@ -158,7 +147,7 @@ const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keyp
|
|
|
158
147
|
}
|
|
159
148
|
else {
|
|
160
149
|
// 3) Check if wallet has no owned objects
|
|
161
|
-
const ownedObjectIds = (yield
|
|
150
|
+
const ownedObjectIds = (yield getAllOwnedObjects(suiClient, keypair.toSuiAddress())).map((obj) => obj.objectId);
|
|
162
151
|
console.log("[checkIfKeypairCanBeUsed] ownedObjectIds:", ownedObjectIds);
|
|
163
152
|
if (ownedObjectIds.length === 0) {
|
|
164
153
|
// Previous flow completed
|
|
@@ -184,8 +173,7 @@ const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keyp
|
|
|
184
173
|
}
|
|
185
174
|
}
|
|
186
175
|
});
|
|
187
|
-
|
|
188
|
-
const fundKeypair = (tokens, address, keypair, suiClient, signExecuteAndWaitForTransaction, // From WalletContext
|
|
176
|
+
export const fundKeypair = (tokens, address, keypair, suiClient, signExecuteAndWaitForTransaction, // From WalletContext
|
|
189
177
|
onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
190
178
|
const filteredTokens = tokens.filter((t) => t.amount.gt(0));
|
|
191
179
|
console.log("[fundKeypair] tokens:", tokens.map((t) => ({
|
|
@@ -195,19 +183,19 @@ onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
195
183
|
coinType: t.coinType,
|
|
196
184
|
amount: t.amount.toString(),
|
|
197
185
|
})));
|
|
198
|
-
const allCoins = yield Promise.all(filteredTokens.map((token) =>
|
|
199
|
-
const transaction = new
|
|
186
|
+
const allCoins = yield Promise.all(filteredTokens.map((token) => getAllCoins(suiClient, address, token.coinType)));
|
|
187
|
+
const transaction = new Transaction();
|
|
200
188
|
transaction.setSender(address);
|
|
201
|
-
const mergeCoins = filteredTokens.map((token, index) =>
|
|
189
|
+
const mergeCoins = filteredTokens.map((token, index) => mergeAllCoins(token.coinType, transaction, allCoins[index]));
|
|
202
190
|
const coins = [];
|
|
203
191
|
for (let i = 0; i < filteredTokens.length; i++) {
|
|
204
192
|
const token = filteredTokens[i];
|
|
205
|
-
const [coin] = transaction.splitCoins(
|
|
193
|
+
const [coin] = transaction.splitCoins(isSui(token.coinType)
|
|
206
194
|
? transaction.gas
|
|
207
|
-
: transaction.object(mergeCoins[i].
|
|
195
|
+
: transaction.object(mergeCoins[i].objectId), [
|
|
208
196
|
BigInt(token
|
|
209
197
|
.amount.times(10 ** token.decimals)
|
|
210
|
-
.integerValue(
|
|
198
|
+
.integerValue(BigNumber.ROUND_DOWN)
|
|
211
199
|
.toString()),
|
|
212
200
|
]);
|
|
213
201
|
coins.push(coin);
|
|
@@ -216,19 +204,17 @@ onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
216
204
|
const res = yield signExecuteAndWaitForTransaction(transaction, undefined, undefined, onSign, onExecute);
|
|
217
205
|
return { res };
|
|
218
206
|
});
|
|
219
|
-
|
|
220
|
-
const returnAllOwnedObjectsAndSuiToUser = (address, keypair, suiClient, onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
207
|
+
export const returnAllOwnedObjectsAndSuiToUser = (address, keypair, suiClient, onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
221
208
|
console.log(`[returnAllOwnedObjectsAndSuiToUser] address: ${address}`);
|
|
222
|
-
const transaction = new
|
|
209
|
+
const transaction = new Transaction();
|
|
223
210
|
transaction.setSender(keypair.toSuiAddress());
|
|
224
211
|
transaction.transferObjects([transaction.gas], address); // Will throw if there is no gas
|
|
225
|
-
const ownedObjectIds = (yield
|
|
226
|
-
.filter((obj) =>
|
|
227
|
-
.map((obj) =>
|
|
212
|
+
const ownedObjectIds = (yield getAllOwnedObjects(suiClient, keypair.toSuiAddress()))
|
|
213
|
+
.filter((obj) => obj.type !== "0x2::coin::Coin<0x2::sui::SUI>")
|
|
214
|
+
.map((obj) => obj.objectId); // Assumed to be <512 objects
|
|
228
215
|
console.log(`[returnAllOwnedObjectsAndSuiToUser] ownedObjectIds: ${ownedObjectIds}`);
|
|
229
216
|
if (ownedObjectIds.length > 0)
|
|
230
217
|
transaction.transferObjects(ownedObjectIds, address);
|
|
231
|
-
const res = yield
|
|
218
|
+
const res = yield keypairSignExecuteAndWaitForTransaction(transaction, keypair, suiClient, onSign, onExecute);
|
|
232
219
|
return { res };
|
|
233
220
|
});
|
|
234
|
-
exports.returnAllOwnedObjectsAndSuiToUser = returnAllOwnedObjectsAndSuiToUser;
|
package/lib/ledger.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SuiGrpcClient } from "@mysten/sui/grpc";
|
|
2
2
|
import { Transaction } from "@mysten/sui/transactions";
|
|
3
|
-
export declare const getLedgerHash: (address: string, transaction: Transaction, suiClient:
|
|
3
|
+
export declare const getLedgerHash: (address: string, transaction: Transaction, suiClient: SuiGrpcClient) => Promise<string>;
|
package/lib/ledger.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,26 +7,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
8
|
});
|
|
10
9
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
exports.getLedgerHash = void 0;
|
|
16
|
-
const cryptography_1 = require("@mysten/sui/cryptography");
|
|
17
|
-
const transactions_1 = require("@mysten/sui/transactions");
|
|
18
|
-
const blake2b_1 = __importDefault(require("blake2b"));
|
|
19
|
-
const getLedgerHash = (address, transaction, suiClient) => __awaiter(void 0, void 0, void 0, function* () {
|
|
10
|
+
import { messageWithIntent } from "@mysten/sui/cryptography";
|
|
11
|
+
import { Transaction } from "@mysten/sui/transactions";
|
|
12
|
+
import blake2b from "blake2b";
|
|
13
|
+
export const getLedgerHash = (address, transaction, suiClient) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
14
|
// Build transaction
|
|
21
|
-
const _transaction =
|
|
15
|
+
const _transaction = Transaction.from(transaction);
|
|
22
16
|
_transaction.setSender(address);
|
|
23
17
|
const builtTransaction = yield _transaction.build({
|
|
24
18
|
client: suiClient,
|
|
25
19
|
});
|
|
26
20
|
// Get hash
|
|
27
|
-
const intentMessage =
|
|
28
|
-
const hasher = (
|
|
21
|
+
const intentMessage = messageWithIntent("TransactionData", builtTransaction);
|
|
22
|
+
const hasher = blake2b(32);
|
|
29
23
|
hasher.update(intentMessage);
|
|
30
24
|
const hash = hasher.digest("hex");
|
|
31
25
|
return `0x${hash}`;
|
|
32
26
|
});
|
|
33
|
-
exports.getLedgerHash = getLedgerHash;
|
package/lib/msafe.js
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMsafeAppStoreUrl = exports.getMsafeBaseUrl = exports.isInMsafeApp = void 0;
|
|
4
|
-
const isInMsafeApp = () => {
|
|
1
|
+
export const isInMsafeApp = () => {
|
|
5
2
|
var _a, _b;
|
|
6
3
|
return typeof window !== "undefined" &&
|
|
7
4
|
((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.ancestorOrigins) &&
|
|
8
5
|
Array.from(window.location.ancestorOrigins).length > 0 &&
|
|
9
6
|
((_b = Array.from(window.location.ancestorOrigins)[0]) === null || _b === void 0 ? void 0 : _b.includes("m-safe.io"));
|
|
10
7
|
};
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
exports.getMsafeBaseUrl = getMsafeBaseUrl;
|
|
14
|
-
const getMsafeAppStoreUrl = (appName) => `${(0, exports.getMsafeBaseUrl)()}/store/${appName}`;
|
|
15
|
-
exports.getMsafeAppStoreUrl = getMsafeAppStoreUrl;
|
|
8
|
+
export const getMsafeBaseUrl = () => Array.from(window.location.ancestorOrigins)[0];
|
|
9
|
+
export const getMsafeAppStoreUrl = (appName) => `${getMsafeBaseUrl()}/store/${appName}`;
|
package/lib/transactions.d.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SuiClientTypes } from "@mysten/sui/client";
|
|
2
|
+
import { SuiJsonRpcClient, SuiTransactionBlockResponse } from "@mysten/sui/jsonRpc";
|
|
3
|
+
import { SuiGrpcClient } from "@mysten/sui/grpc";
|
|
2
4
|
import { Transaction } from "@mysten/sui/transactions";
|
|
3
5
|
import BigNumber from "bignumber.js";
|
|
4
6
|
import { Token } from "./coinMetadata";
|
|
5
|
-
export declare const getTotalGasFee: (res:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export declare const
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
export declare const getTotalGasFee: (res: SuiClientTypes.TransactionResult<{
|
|
8
|
+
effects: true;
|
|
9
|
+
}>) => BigNumber;
|
|
10
|
+
export declare const getBalanceChange: (res: SuiClientTypes.TransactionResult<{
|
|
11
|
+
balanceChanges: true;
|
|
12
|
+
effects: true;
|
|
13
|
+
}>, address: string, token: Token, multiplier?: -1 | 1) => BigNumber | undefined;
|
|
14
|
+
export declare const getAllOwnedObjects: (suiClient: SuiGrpcClient, address: string, limit?: number, type?: string) => Promise<SuiClientTypes.Object<{
|
|
15
|
+
content: true;
|
|
16
|
+
}>[]>;
|
|
17
|
+
export declare const getAllCoins: (suiClient: SuiGrpcClient, address: string, coinType: string) => Promise<SuiClientTypes.Coin[]>;
|
|
18
|
+
export declare const getMostRecentAddressTransaction: (suiJsonRpcClient: SuiJsonRpcClient, address: string, direction: "from" | "to") => Promise<SuiTransactionBlockResponse | undefined>;
|
|
19
|
+
export declare const mergeAllCoins: (coinType: string, transaction: Transaction, allCoins: SuiClientTypes.Coin[]) => SuiClientTypes.Coin;
|
package/lib/transactions.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,75 +7,68 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
8
|
});
|
|
10
9
|
};
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
import { SUI_DECIMALS, normalizeStructTag } from "@mysten/sui/utils";
|
|
11
|
+
import BigNumber from "bignumber.js";
|
|
12
|
+
import { isSui } from "./coinType";
|
|
13
|
+
export const getTotalGasFee = (res) => {
|
|
14
|
+
var _a;
|
|
15
|
+
return ((_a = res.Transaction) === null || _a === void 0 ? void 0 : _a.effects)
|
|
16
|
+
? new BigNumber(+res.Transaction.effects.gasUsed.computationCost +
|
|
17
|
+
+res.Transaction.effects.gasUsed.storageCost -
|
|
18
|
+
+res.Transaction.effects.gasUsed.storageRebate).div(10 ** SUI_DECIMALS)
|
|
19
|
+
: new BigNumber(0);
|
|
13
20
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
18
|
-
const coinType_1 = require("./coinType");
|
|
19
|
-
const getTotalGasFee = (res) => res.effects
|
|
20
|
-
? new bignumber_js_1.default(+res.effects.gasUsed.computationCost +
|
|
21
|
-
+res.effects.gasUsed.storageCost -
|
|
22
|
-
+res.effects.gasUsed.storageRebate).div(10 ** utils_1.SUI_DECIMALS)
|
|
23
|
-
: new bignumber_js_1.default(0);
|
|
24
|
-
exports.getTotalGasFee = getTotalGasFee;
|
|
25
|
-
const getBalanceChange = (res, address, token, multiplier = 1) => {
|
|
26
|
-
if (!res.balanceChanges)
|
|
21
|
+
export const getBalanceChange = (res, address, token, multiplier = 1) => {
|
|
22
|
+
var _a;
|
|
23
|
+
if (!((_a = res.Transaction) === null || _a === void 0 ? void 0 : _a.balanceChanges))
|
|
27
24
|
return undefined;
|
|
28
|
-
const balanceChanges = res.balanceChanges.filter((bc) =>
|
|
29
|
-
|
|
30
|
-
return (0, utils_1.normalizeStructTag)(bc.coinType) === token.coinType &&
|
|
31
|
-
((_a = bc.owner) === null || _a === void 0 ? void 0 : _a.AddressOwner) === address;
|
|
32
|
-
});
|
|
25
|
+
const balanceChanges = res.Transaction.balanceChanges.filter((bc) => normalizeStructTag(bc.coinType) === token.coinType &&
|
|
26
|
+
bc.address === address);
|
|
33
27
|
if (balanceChanges.length === 0)
|
|
34
28
|
return undefined;
|
|
35
29
|
return balanceChanges
|
|
36
|
-
.reduce((acc, balanceChange) => acc.plus(new
|
|
30
|
+
.reduce((acc, balanceChange) => acc.plus(new BigNumber(+balanceChange.amount)), new BigNumber(0))
|
|
37
31
|
.div(10 ** token.decimals)
|
|
38
|
-
.plus(
|
|
32
|
+
.plus(isSui(token.coinType) ? getTotalGasFee(res) : 0)
|
|
39
33
|
.times(multiplier);
|
|
40
34
|
};
|
|
41
|
-
|
|
42
|
-
const getAllOwnedObjects = (suiClient, address, filter) => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
+
export const getAllOwnedObjects = (suiClient, address, limit, type) => __awaiter(void 0, void 0, void 0, function* () {
|
|
43
36
|
const allObjs = [];
|
|
44
37
|
let cursor = null;
|
|
45
38
|
let hasNextPage = true;
|
|
46
39
|
while (hasNextPage) {
|
|
47
|
-
const objs = yield suiClient.
|
|
40
|
+
const objs = yield suiClient.core.listOwnedObjects({
|
|
48
41
|
owner: address,
|
|
42
|
+
limit,
|
|
49
43
|
cursor,
|
|
50
|
-
|
|
51
|
-
|
|
44
|
+
type,
|
|
45
|
+
include: { content: true },
|
|
52
46
|
});
|
|
53
|
-
allObjs.push(...objs.
|
|
54
|
-
cursor = objs.
|
|
47
|
+
allObjs.push(...objs.objects);
|
|
48
|
+
cursor = objs.cursor;
|
|
55
49
|
hasNextPage = objs.hasNextPage;
|
|
56
50
|
}
|
|
57
51
|
return allObjs;
|
|
58
52
|
});
|
|
59
|
-
|
|
60
|
-
const getAllCoins = (suiClient, address, coinType) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
export const getAllCoins = (suiClient, address, coinType) => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
54
|
var _a;
|
|
62
55
|
const allCoins = [];
|
|
63
56
|
let cursor = undefined;
|
|
64
57
|
let hasNextPage = true;
|
|
65
58
|
while (hasNextPage) {
|
|
66
|
-
const coins = yield suiClient.
|
|
59
|
+
const coins = yield suiClient.core.listCoins({
|
|
67
60
|
owner: address,
|
|
68
61
|
coinType,
|
|
69
62
|
cursor,
|
|
70
63
|
});
|
|
71
|
-
allCoins.push(...coins.
|
|
72
|
-
cursor = (_a = coins.
|
|
64
|
+
allCoins.push(...coins.objects);
|
|
65
|
+
cursor = (_a = coins.cursor) !== null && _a !== void 0 ? _a : undefined;
|
|
73
66
|
hasNextPage = coins.hasNextPage;
|
|
74
67
|
}
|
|
75
68
|
return allCoins;
|
|
76
69
|
});
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
const transactions = yield suiClient.queryTransactionBlocks({
|
|
70
|
+
export const getMostRecentAddressTransaction = (suiJsonRpcClient, address, direction) => __awaiter(void 0, void 0, void 0, function* () {
|
|
71
|
+
const transactions = yield suiJsonRpcClient.queryTransactionBlocks({
|
|
80
72
|
filter: direction === "from" ? { FromAddress: address } : { ToAddress: address },
|
|
81
73
|
limit: 1,
|
|
82
74
|
order: "descending",
|
|
@@ -86,12 +78,10 @@ const getMostRecentAddressTransaction = (suiClient, address, direction) => __awa
|
|
|
86
78
|
});
|
|
87
79
|
return transactions.data[0];
|
|
88
80
|
});
|
|
89
|
-
|
|
90
|
-
const mergeAllCoins = (coinType, transaction, allCoins) => {
|
|
81
|
+
export const mergeAllCoins = (coinType, transaction, allCoins) => {
|
|
91
82
|
const mergeCoin = allCoins[0];
|
|
92
|
-
if (allCoins.length > 1 && !
|
|
93
|
-
transaction.mergeCoins(transaction.object(mergeCoin.
|
|
83
|
+
if (allCoins.length > 1 && !isSui(coinType)) {
|
|
84
|
+
transaction.mergeCoins(transaction.object(mergeCoin.objectId), allCoins.map((c) => transaction.object(c.objectId)).slice(1));
|
|
94
85
|
}
|
|
95
86
|
return mergeCoin;
|
|
96
87
|
};
|
|
97
|
-
exports.mergeAllCoins = mergeAllCoins;
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@suilend/sui-fe","version":"0.
|
|
1
|
+
{"name":"@suilend/sui-fe","version":"0.4.0","private":false,"description":"A collection of TypeScript frontend libraries","author":"Suilend","license":"MIT","main":"./index.js","exports":{".":"./index.js","./lib/constants":"./lib/constants.js","./lib/transactions":"./lib/transactions.js","./lib/format":"./lib/format.js","./lib/ledger":"./lib/ledger.js","./lib/coinMetadata":"./lib/coinMetadata.js","./lib":"./lib/index.js","./lib/indexedDB":"./lib/indexedDB.js","./lib/coinType":"./lib/coinType.js","./lib/coin":"./lib/coin.js","./lib/msafe":"./lib/msafe.js","./lib/keypair":"./lib/keypair.js","./lib/api":"./lib/api.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 ./release.js && 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.20.0","bignumber.js":"^9.1.2","blake2b":"^2.1.4","lodash":"^4.17.21","p-limit":"3.1.0"},"devDependencies":{"@tsconfig/recommended":"^1.0.8","@types/blake2b":"^2.1.3","@types/lodash":"^4.17.13","@types/node":"^22.9.0","@typescript-eslint/eslint-plugin":"^8.14.0","@typescript-eslint/parser":"^8.14.0","eslint":"^9.14.0","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":"2.1.0"}}
|