@suilend/sui-fe 2.0.10 → 3.0.1
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/coinMetadata.d.ts +4 -4
- package/lib/constants.d.ts +1 -0
- package/lib/constants.js +1 -0
- package/lib/keypair.d.ts +43 -12
- package/lib/keypair.js +46 -53
- package/lib/ledger.d.ts +2 -2
- package/lib/ledger.js +2 -2
- package/lib/transactions.d.ts +27 -7
- package/lib/transactions.js +43 -26
- package/package.json +1 -1
package/lib/coinMetadata.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type Token = CoinMetadata & {
|
|
1
|
+
import { SuiClientTypes } from "@mysten/sui/client";
|
|
2
|
+
export type Token = SuiClientTypes.CoinMetadata & {
|
|
3
3
|
coinType: string;
|
|
4
|
-
raw?: CoinMetadata;
|
|
4
|
+
raw?: SuiClientTypes.CoinMetadata;
|
|
5
5
|
};
|
|
6
|
-
export declare const getToken: (coinType: string, coinMetadata: CoinMetadata) => Token;
|
|
6
|
+
export declare const getToken: (coinType: string, coinMetadata: SuiClientTypes.CoinMetadata) => Token;
|
|
7
7
|
export declare const getCoinMetadataMap: (uniqueCoinTypes: string[]) => Promise<Record<string, Token>>;
|
|
8
8
|
export declare const removeCoinMetadataFromIndexedDB: (coinTypes: string[]) => Promise<void>;
|
package/lib/constants.d.ts
CHANGED
package/lib/constants.js
CHANGED
package/lib/keypair.d.ts
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SuiGrpcClient } from "@mysten/sui/grpc";
|
|
2
|
+
import { SuiGraphQLClient } from "@mysten/sui/graphql";
|
|
3
|
+
import { SuiClientTypes } from "@mysten/sui/client";
|
|
2
4
|
import { SignatureWithBytes } from "@mysten/sui/cryptography";
|
|
3
5
|
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
|
|
4
6
|
import { Transaction } from "@mysten/sui/transactions";
|
|
5
7
|
import BigNumber from "bignumber.js";
|
|
6
8
|
import { Token } from "./coinMetadata";
|
|
9
|
+
import { GraphQLTransaction } from "./transactions";
|
|
7
10
|
export declare class TransactionStatusError extends Error {
|
|
8
11
|
constructor(message: string);
|
|
9
12
|
}
|
|
10
|
-
export declare const keypairWaitForTransaction: (digest: string,
|
|
11
|
-
|
|
13
|
+
export declare const keypairWaitForTransaction: (digest: string, suiGrpcClient: SuiGrpcClient) => Promise<SuiClientTypes.Transaction<{
|
|
14
|
+
effects: true;
|
|
15
|
+
events: true;
|
|
16
|
+
balanceChanges: true;
|
|
17
|
+
}>>;
|
|
18
|
+
export declare const keypairSignExecuteAndWaitForTransaction: (transaction: Transaction, keypair: Ed25519Keypair, suiGrpcClient: SuiGrpcClient, onSign?: (signedTransaction: SignatureWithBytes) => void, onExecute?: (res: SuiClientTypes.Transaction) => void) => Promise<SuiClientTypes.Transaction<{
|
|
19
|
+
effects: true;
|
|
20
|
+
events: true;
|
|
21
|
+
balanceChanges: true;
|
|
22
|
+
}>>;
|
|
12
23
|
export type LastSignedTransaction<T> = {
|
|
13
24
|
signedTransaction: SignatureWithBytes;
|
|
14
25
|
type: T;
|
|
15
26
|
};
|
|
16
|
-
export declare const checkLastTransactionSignature: <T>(type: T, lastCurrentFlowTransaction:
|
|
27
|
+
export declare const checkLastTransactionSignature: <T>(type: T, lastCurrentFlowTransaction: GraphQLTransaction | undefined, lastSignedTransaction: LastSignedTransaction<T> | undefined, setLastSignedTransaction: (value: LastSignedTransaction<T> | undefined) => void, suiGrpcClient: SuiGrpcClient, validCallback: (res: SuiClientTypes.Transaction<{
|
|
28
|
+
effects: true;
|
|
29
|
+
events: true;
|
|
30
|
+
balanceChanges: true;
|
|
31
|
+
}>) => Promise<void>, invalidCallback: () => Promise<void>) => Promise<void>;
|
|
17
32
|
export declare const onSign: <T>(type: T, setLastSignedTransaction: (value: LastSignedTransaction<T> | undefined) => void, index?: number) => (signedTransaction: SignatureWithBytes) => void;
|
|
18
33
|
export declare const KEYPAIR_SEED_MESSAGE = "send:wallet-connect";
|
|
19
34
|
export declare const createKeypair: (signature: string) => Promise<{
|
|
@@ -21,19 +36,35 @@ export declare const createKeypair: (signature: string) => Promise<{
|
|
|
21
36
|
address: string;
|
|
22
37
|
privateKey: string;
|
|
23
38
|
}>;
|
|
24
|
-
export declare const checkIfKeypairCanBeUsed: (lastSignedTransaction: SignatureWithBytes | undefined, currentFlowDigests: string[], keypair: Ed25519Keypair,
|
|
25
|
-
lastCurrentFlowTransaction:
|
|
39
|
+
export declare const checkIfKeypairCanBeUsed: (lastSignedTransaction: SignatureWithBytes | undefined, currentFlowDigests: string[], keypair: Ed25519Keypair, suiGrpcClient: SuiGrpcClient, graphqlClient: SuiGraphQLClient) => Promise<{
|
|
40
|
+
lastCurrentFlowTransaction: GraphQLTransaction | undefined;
|
|
26
41
|
}>;
|
|
27
42
|
export type FundKeypairResult = {
|
|
28
|
-
res:
|
|
43
|
+
res: SuiClientTypes.Transaction<{
|
|
44
|
+
effects: true;
|
|
45
|
+
events: true;
|
|
46
|
+
balanceChanges: true;
|
|
47
|
+
}>;
|
|
29
48
|
};
|
|
30
49
|
export declare const fundKeypair: (tokens: (Token & {
|
|
31
50
|
amount: BigNumber;
|
|
32
|
-
})[], address: string, keypair: Ed25519Keypair,
|
|
33
|
-
|
|
34
|
-
|
|
51
|
+
})[], address: string, keypair: Ed25519Keypair, suiGrpcClient: SuiGrpcClient, signExecuteAndWaitForTransaction: (transaction: Transaction, onSetGasBudget?: (transaction: Transaction) => void, onSign?: (signedTransaction: SignatureWithBytes) => void, onExecute?: (res: SuiClientTypes.Transaction) => void) => Promise<SuiClientTypes.Transaction<{
|
|
52
|
+
effects: true;
|
|
53
|
+
events: true;
|
|
54
|
+
balanceChanges: true;
|
|
55
|
+
}>>, // From WalletContext
|
|
56
|
+
onSign?: (signedTransaction: SignatureWithBytes) => void, onExecute?: (res: SuiClientTypes.Transaction) => void) => Promise<{
|
|
57
|
+
res: SuiClientTypes.Transaction<{
|
|
58
|
+
effects: true;
|
|
59
|
+
events: true;
|
|
60
|
+
balanceChanges: true;
|
|
61
|
+
}>;
|
|
35
62
|
}>;
|
|
36
63
|
export type ReturnAllOwnedObjectsAndSuiToUserResult = {
|
|
37
|
-
res:
|
|
64
|
+
res: SuiClientTypes.Transaction<{
|
|
65
|
+
effects: true;
|
|
66
|
+
events: true;
|
|
67
|
+
balanceChanges: true;
|
|
68
|
+
}>;
|
|
38
69
|
};
|
|
39
|
-
export declare const returnAllOwnedObjectsAndSuiToUser: (address: string, keypair: Ed25519Keypair,
|
|
70
|
+
export declare const returnAllOwnedObjectsAndSuiToUser: (address: string, keypair: Ed25519Keypair, suiGrpcClient: SuiGrpcClient, onSign?: (signedTransaction: SignatureWithBytes) => void, onExecute?: (res: SuiClientTypes.Transaction) => void) => Promise<ReturnAllOwnedObjectsAndSuiToUserResult>;
|
package/lib/keypair.js
CHANGED
|
@@ -21,51 +21,56 @@ export class TransactionStatusError extends Error {
|
|
|
21
21
|
this.name = "TransactionStatusError";
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
-
export const keypairWaitForTransaction = (digest,
|
|
25
|
-
var _a, _b;
|
|
26
|
-
const
|
|
24
|
+
export const keypairWaitForTransaction = (digest, suiGrpcClient) => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
+
var _a, _b, _c;
|
|
26
|
+
const result = yield suiGrpcClient.waitForTransaction({
|
|
27
27
|
digest,
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
showObjectChanges: true,
|
|
28
|
+
include: {
|
|
29
|
+
effects: true,
|
|
30
|
+
events: true,
|
|
31
|
+
balanceChanges: true,
|
|
33
32
|
},
|
|
34
33
|
});
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
throw new TransactionStatusError(
|
|
38
|
-
|
|
34
|
+
const tx = (_a = result.Transaction) !== null && _a !== void 0 ? _a : result.FailedTransaction;
|
|
35
|
+
if (!tx)
|
|
36
|
+
throw new TransactionStatusError("Transaction not found");
|
|
37
|
+
if (!tx.status.success)
|
|
38
|
+
throw new TransactionStatusError((_c = (_b = tx.status.error) === null || _b === void 0 ? void 0 : _b.message) !== null && _c !== void 0 ? _c : "Transaction failed");
|
|
39
|
+
return tx;
|
|
39
40
|
});
|
|
40
|
-
export const keypairSignExecuteAndWaitForTransaction = (transaction, keypair,
|
|
41
|
+
export const keypairSignExecuteAndWaitForTransaction = (transaction, keypair, suiGrpcClient, onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
+
var _a;
|
|
41
43
|
// 1) Sign
|
|
42
44
|
const builtTransaction = yield transaction.build({
|
|
43
|
-
client:
|
|
45
|
+
client: suiGrpcClient,
|
|
44
46
|
});
|
|
45
47
|
const signedTransaction = yield keypair.signTransaction(builtTransaction);
|
|
46
48
|
onSign === null || onSign === void 0 ? void 0 : onSign(signedTransaction);
|
|
47
49
|
// 2) Execute
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
const execResult = yield suiGrpcClient.executeTransaction({
|
|
51
|
+
transaction: fromBase64(signedTransaction.bytes),
|
|
52
|
+
signatures: [signedTransaction.signature],
|
|
51
53
|
});
|
|
52
|
-
|
|
54
|
+
const execTx = (_a = execResult.Transaction) !== null && _a !== void 0 ? _a : execResult.FailedTransaction;
|
|
55
|
+
if (!execTx)
|
|
56
|
+
throw new TransactionStatusError("Transaction not found");
|
|
57
|
+
onExecute === null || onExecute === void 0 ? void 0 : onExecute(execTx);
|
|
53
58
|
// 3) Wait
|
|
54
|
-
const
|
|
55
|
-
return
|
|
59
|
+
const tx = yield keypairWaitForTransaction(execTx.digest, suiGrpcClient);
|
|
60
|
+
return tx;
|
|
56
61
|
});
|
|
57
|
-
export const checkLastTransactionSignature = (type, lastCurrentFlowTransaction, lastSignedTransaction, setLastSignedTransaction,
|
|
58
|
-
var _a
|
|
62
|
+
export const checkLastTransactionSignature = (type, lastCurrentFlowTransaction, lastSignedTransaction, setLastSignedTransaction, suiGrpcClient, validCallback, invalidCallback) => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
+
var _a;
|
|
59
64
|
console.log("[checkLastTransactionSignature]", {
|
|
60
65
|
type,
|
|
61
66
|
lastCurrentFlowTransaction,
|
|
62
67
|
lastSignedTransaction,
|
|
63
68
|
});
|
|
64
69
|
if ((lastSignedTransaction === null || lastSignedTransaction === void 0 ? void 0 : lastSignedTransaction.type) === type &&
|
|
65
|
-
((
|
|
70
|
+
((_a = lastCurrentFlowTransaction === null || lastCurrentFlowTransaction === void 0 ? void 0 : lastCurrentFlowTransaction.signatures) !== null && _a !== void 0 ? _a : []).includes(lastSignedTransaction.signedTransaction.signature)) {
|
|
66
71
|
console.log("[checkLastTransactionSignature] lastSignedTransaction type and signature MATCH");
|
|
67
72
|
try {
|
|
68
|
-
const res = yield keypairWaitForTransaction(lastCurrentFlowTransaction.digest,
|
|
73
|
+
const res = yield keypairWaitForTransaction(lastCurrentFlowTransaction.digest, suiGrpcClient); // Assumed to only throw if the transaction was executed but failed (e.g. insufficient gas), or if the connection was lost
|
|
69
74
|
yield validCallback(res);
|
|
70
75
|
setLastSignedTransaction(undefined);
|
|
71
76
|
}
|
|
@@ -116,16 +121,16 @@ export const createKeypair = (signature) => __awaiter(void 0, void 0, void 0, fu
|
|
|
116
121
|
return { keypair, address, privateKey };
|
|
117
122
|
});
|
|
118
123
|
// CHECK IF KEYPAIR CAN BE USED
|
|
119
|
-
export const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keypair,
|
|
120
|
-
var _a
|
|
124
|
+
export const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keypair, suiGrpcClient, graphqlClient) => __awaiter(void 0, void 0, void 0, function* () {
|
|
125
|
+
var _a;
|
|
121
126
|
const [mostRecentFromAddressTransaction, mostRecentToAddressTransaction] = yield Promise.all([
|
|
122
|
-
getMostRecentAddressTransaction(
|
|
123
|
-
getMostRecentAddressTransaction(
|
|
127
|
+
getMostRecentAddressTransaction(graphqlClient, keypair.toSuiAddress(), "from"),
|
|
128
|
+
getMostRecentAddressTransaction(graphqlClient, keypair.toSuiAddress(), "to"),
|
|
124
129
|
]);
|
|
125
130
|
const mostRecentFromOrToAddressTransaction = [
|
|
126
131
|
mostRecentFromAddressTransaction,
|
|
127
132
|
mostRecentToAddressTransaction,
|
|
128
|
-
].
|
|
133
|
+
].filter(Boolean)[0];
|
|
129
134
|
console.log("[checkIfKeypairCanBeUsed] mostRecentFromAddressTransaction:", mostRecentFromAddressTransaction, "mostRecentToAddressTransaction:", mostRecentToAddressTransaction, "mostRecentFromOrToAddressTransaction:", mostRecentFromOrToAddressTransaction, "lastSignedTransaction:", lastSignedTransaction, "currentFlowDigests:", currentFlowDigests);
|
|
130
135
|
// 1) Check if wallet has no TO/FROM transactions
|
|
131
136
|
if (mostRecentFromOrToAddressTransaction === undefined) {
|
|
@@ -136,7 +141,7 @@ export const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigest
|
|
|
136
141
|
else {
|
|
137
142
|
// 2) Check if resuming/retrying current flow
|
|
138
143
|
if ((lastSignedTransaction !== undefined &&
|
|
139
|
-
((
|
|
144
|
+
((_a = mostRecentFromOrToAddressTransaction.signatures) !== null && _a !== void 0 ? _a : []).includes(lastSignedTransaction.signature)) ||
|
|
140
145
|
currentFlowDigests.includes(mostRecentFromOrToAddressTransaction.digest)) {
|
|
141
146
|
// Resuming/retrying current flow
|
|
142
147
|
// CONTINUE
|
|
@@ -146,7 +151,7 @@ export const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigest
|
|
|
146
151
|
}
|
|
147
152
|
else {
|
|
148
153
|
// 3) Check if wallet has no owned objects
|
|
149
|
-
const ownedObjectIds = (yield getAllOwnedObjects(
|
|
154
|
+
const ownedObjectIds = (yield getAllOwnedObjects(suiGrpcClient, keypair.toSuiAddress())).map((obj) => obj.objectId);
|
|
150
155
|
console.log("[checkIfKeypairCanBeUsed] ownedObjectIds:", ownedObjectIds);
|
|
151
156
|
if (ownedObjectIds.length === 0) {
|
|
152
157
|
// Previous flow completed
|
|
@@ -154,25 +159,13 @@ export const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigest
|
|
|
154
159
|
return { lastCurrentFlowTransaction: undefined };
|
|
155
160
|
}
|
|
156
161
|
else {
|
|
157
|
-
//
|
|
158
|
-
|
|
159
|
-
const isRecent = !!timestampMs && +timestampMs > Date.now() - 1000 * 60 * 2; // Less than 2 minutes ago
|
|
160
|
-
console.log("[checkIfKeypairCanBeUsed] isRecent:", isRecent);
|
|
161
|
-
if (isRecent) {
|
|
162
|
-
// Last transaction from previous flow is recent
|
|
163
|
-
// STOP
|
|
164
|
-
throw new Error("Please wait for previous flow to complete");
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
// Last transaction from previous flow is NOT recent
|
|
168
|
-
// CONTINUE
|
|
169
|
-
return { lastCurrentFlowTransaction: undefined };
|
|
170
|
-
}
|
|
162
|
+
// CONTINUE (no timestampMs available via GraphQL query; rely on owned objects check)
|
|
163
|
+
return { lastCurrentFlowTransaction: undefined };
|
|
171
164
|
}
|
|
172
165
|
}
|
|
173
166
|
}
|
|
174
167
|
});
|
|
175
|
-
export const fundKeypair = (tokens, address, keypair,
|
|
168
|
+
export const fundKeypair = (tokens, address, keypair, suiGrpcClient, signExecuteAndWaitForTransaction, // From WalletContext
|
|
176
169
|
onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
177
170
|
const filteredTokens = tokens.filter((t) => t.amount.gt(0));
|
|
178
171
|
console.log("[fundKeypair] tokens:", tokens.map((t) => ({
|
|
@@ -182,7 +175,7 @@ onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
182
175
|
coinType: t.coinType,
|
|
183
176
|
amount: t.amount.toString(),
|
|
184
177
|
})));
|
|
185
|
-
const allCoins = yield Promise.all(filteredTokens.map((token) => getAllCoins(
|
|
178
|
+
const allCoins = yield Promise.all(filteredTokens.map((token) => getAllCoins(suiGrpcClient, address, token.coinType)));
|
|
186
179
|
const transaction = new Transaction();
|
|
187
180
|
transaction.setSender(address);
|
|
188
181
|
const mergeCoins = filteredTokens.map((token, index) => mergeAllCoins(token.coinType, transaction, allCoins[index]));
|
|
@@ -191,7 +184,7 @@ onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
191
184
|
const token = filteredTokens[i];
|
|
192
185
|
const [coin] = transaction.splitCoins(isSui(token.coinType)
|
|
193
186
|
? transaction.gas
|
|
194
|
-
: transaction.object(mergeCoins[i].
|
|
187
|
+
: transaction.object(mergeCoins[i].objectId), [
|
|
195
188
|
BigInt(token
|
|
196
189
|
.amount.times(10 ** token.decimals)
|
|
197
190
|
.integerValue(BigNumber.ROUND_DOWN)
|
|
@@ -203,17 +196,17 @@ onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
203
196
|
const res = yield signExecuteAndWaitForTransaction(transaction, undefined, onSign, onExecute);
|
|
204
197
|
return { res };
|
|
205
198
|
});
|
|
206
|
-
export const returnAllOwnedObjectsAndSuiToUser = (address, keypair,
|
|
199
|
+
export const returnAllOwnedObjectsAndSuiToUser = (address, keypair, suiGrpcClient, onSign, onExecute) => __awaiter(void 0, void 0, void 0, function* () {
|
|
207
200
|
console.log(`[returnAllOwnedObjectsAndSuiToUser] address: ${address}`);
|
|
208
201
|
const transaction = new Transaction();
|
|
209
202
|
transaction.setSender(keypair.toSuiAddress());
|
|
210
203
|
transaction.transferObjects([transaction.gas], address); // Will throw if there is no gas
|
|
211
|
-
const ownedObjectIds = (yield getAllOwnedObjects(
|
|
212
|
-
.filter((obj) =>
|
|
213
|
-
.map((obj) =>
|
|
204
|
+
const ownedObjectIds = (yield getAllOwnedObjects(suiGrpcClient, keypair.toSuiAddress()))
|
|
205
|
+
.filter((obj) => obj.type !== "0x2::coin::Coin<0x2::sui::SUI>")
|
|
206
|
+
.map((obj) => obj.objectId); // Assumed to be <512 objects
|
|
214
207
|
console.log(`[returnAllOwnedObjectsAndSuiToUser] ownedObjectIds: ${ownedObjectIds}`);
|
|
215
208
|
if (ownedObjectIds.length > 0)
|
|
216
209
|
transaction.transferObjects(ownedObjectIds, address);
|
|
217
|
-
const res = yield keypairSignExecuteAndWaitForTransaction(transaction, keypair,
|
|
210
|
+
const res = yield keypairSignExecuteAndWaitForTransaction(transaction, keypair, suiGrpcClient, onSign, onExecute);
|
|
218
211
|
return { res };
|
|
219
212
|
});
|
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,
|
|
3
|
+
export declare const getLedgerHash: (address: string, transaction: Transaction, suiGrpcClient: SuiGrpcClient) => Promise<string>;
|
package/lib/ledger.js
CHANGED
|
@@ -10,12 +10,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
import { messageWithIntent } from "@mysten/sui/cryptography";
|
|
11
11
|
import { Transaction } from "@mysten/sui/transactions";
|
|
12
12
|
import blake2b from "blake2b";
|
|
13
|
-
export const getLedgerHash = (address, transaction,
|
|
13
|
+
export const getLedgerHash = (address, transaction, suiGrpcClient) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
14
|
// Build transaction
|
|
15
15
|
const _transaction = Transaction.from(transaction);
|
|
16
16
|
_transaction.setSender(address);
|
|
17
17
|
const builtTransaction = yield _transaction.build({
|
|
18
|
-
client:
|
|
18
|
+
client: suiGrpcClient,
|
|
19
19
|
});
|
|
20
20
|
// Get hash
|
|
21
21
|
const intentMessage = messageWithIntent("TransactionData", builtTransaction);
|
package/lib/transactions.d.ts
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SuiGrpcClient } from "@mysten/sui/grpc";
|
|
2
|
+
import { SuiGraphQLClient } from "@mysten/sui/graphql";
|
|
3
|
+
import { SuiClientTypes } from "@mysten/sui/client";
|
|
2
4
|
import { Transaction } from "@mysten/sui/transactions";
|
|
3
5
|
import BigNumber from "bignumber.js";
|
|
4
6
|
import { Token } from "./coinMetadata";
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
export type GraphQLTransaction = {
|
|
8
|
+
digest: string;
|
|
9
|
+
expiration?: {
|
|
10
|
+
epochId: number;
|
|
11
|
+
} | null;
|
|
12
|
+
signatures?: string[];
|
|
13
|
+
sender?: {
|
|
14
|
+
address: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export declare const getTotalGasFee: (res: SuiClientTypes.Transaction<{
|
|
18
|
+
effects: true;
|
|
19
|
+
balanceChanges: true;
|
|
20
|
+
}>) => BigNumber;
|
|
21
|
+
export declare const getBalanceChange: (res: SuiClientTypes.Transaction<{
|
|
22
|
+
effects: true;
|
|
23
|
+
balanceChanges: true;
|
|
24
|
+
}>, address: string, token: Token, multiplier?: -1 | 1) => BigNumber | undefined;
|
|
25
|
+
export declare const getAllOwnedObjects: (suiGrpcClient: SuiGrpcClient, address: string, type?: string) => Promise<SuiClientTypes.Object<{
|
|
26
|
+
content: true;
|
|
27
|
+
}>[]>;
|
|
28
|
+
export declare const getAllCoins: (suiGrpcClient: SuiGrpcClient, address: string, coinType: string) => Promise<SuiClientTypes.Coin[]>;
|
|
29
|
+
export declare const getMostRecentAddressTransaction: (graphqlClient: SuiGraphQLClient, address: string, direction: "from" | "to") => Promise<GraphQLTransaction | undefined>;
|
|
30
|
+
export declare const mergeAllCoins: (coinType: string, transaction: Transaction, allCoins: SuiClientTypes.Coin[]) => SuiClientTypes.Coin;
|
package/lib/transactions.js
CHANGED
|
@@ -18,11 +18,8 @@ export const getTotalGasFee = (res) => res.effects
|
|
|
18
18
|
export const getBalanceChange = (res, address, token, multiplier = 1) => {
|
|
19
19
|
if (!res.balanceChanges)
|
|
20
20
|
return undefined;
|
|
21
|
-
const balanceChanges = res.balanceChanges.filter((bc) =>
|
|
22
|
-
|
|
23
|
-
return normalizeStructTag(bc.coinType) === token.coinType &&
|
|
24
|
-
((_a = bc.owner) === null || _a === void 0 ? void 0 : _a.AddressOwner) === address;
|
|
25
|
-
});
|
|
21
|
+
const balanceChanges = res.balanceChanges.filter((bc) => normalizeStructTag(bc.coinType) === token.coinType &&
|
|
22
|
+
bc.address === address);
|
|
26
23
|
if (balanceChanges.length === 0)
|
|
27
24
|
return undefined;
|
|
28
25
|
return balanceChanges
|
|
@@ -31,55 +28,75 @@ export const getBalanceChange = (res, address, token, multiplier = 1) => {
|
|
|
31
28
|
.plus(isSui(token.coinType) ? getTotalGasFee(res) : 0)
|
|
32
29
|
.times(multiplier);
|
|
33
30
|
};
|
|
34
|
-
export const getAllOwnedObjects = (
|
|
31
|
+
export const getAllOwnedObjects = (suiGrpcClient, address, type) => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
32
|
const allObjs = [];
|
|
36
33
|
let cursor = null;
|
|
37
34
|
let hasNextPage = true;
|
|
38
35
|
while (hasNextPage) {
|
|
39
|
-
const
|
|
36
|
+
const result = yield suiGrpcClient.listOwnedObjects({
|
|
40
37
|
owner: address,
|
|
41
38
|
cursor,
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
type,
|
|
40
|
+
include: { content: true },
|
|
44
41
|
});
|
|
45
|
-
allObjs.push(...
|
|
46
|
-
cursor =
|
|
47
|
-
hasNextPage =
|
|
42
|
+
allObjs.push(...result.objects);
|
|
43
|
+
cursor = result.cursor;
|
|
44
|
+
hasNextPage = result.hasNextPage;
|
|
48
45
|
}
|
|
49
46
|
return allObjs;
|
|
50
47
|
});
|
|
51
|
-
export const getAllCoins = (
|
|
48
|
+
export const getAllCoins = (suiGrpcClient, address, coinType) => __awaiter(void 0, void 0, void 0, function* () {
|
|
52
49
|
var _a;
|
|
53
50
|
const allCoins = [];
|
|
54
51
|
let cursor = undefined;
|
|
55
52
|
let hasNextPage = true;
|
|
56
53
|
while (hasNextPage) {
|
|
57
|
-
const
|
|
54
|
+
const result = yield suiGrpcClient.listCoins({
|
|
58
55
|
owner: address,
|
|
59
56
|
coinType,
|
|
60
57
|
cursor,
|
|
61
58
|
});
|
|
62
|
-
allCoins.push(...
|
|
63
|
-
cursor = (_a =
|
|
64
|
-
hasNextPage =
|
|
59
|
+
allCoins.push(...result.objects);
|
|
60
|
+
cursor = (_a = result.cursor) !== null && _a !== void 0 ? _a : undefined;
|
|
61
|
+
hasNextPage = result.hasNextPage;
|
|
65
62
|
}
|
|
66
63
|
return allCoins;
|
|
67
64
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
65
|
+
const QUERY_TRANSACTIONS = `
|
|
66
|
+
query QueryTransactions($filter: TransactionsFilter, $first: Int, $after: String) {
|
|
67
|
+
transactions(
|
|
68
|
+
first: $first
|
|
69
|
+
after: $after
|
|
70
|
+
filter: $filter
|
|
71
|
+
) {
|
|
72
|
+
nodes {
|
|
73
|
+
digest
|
|
74
|
+
expiration { epochId }
|
|
75
|
+
signatures
|
|
76
|
+
sender { address }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
`;
|
|
81
|
+
export const getMostRecentAddressTransaction = (graphqlClient, address, direction) => __awaiter(void 0, void 0, void 0, function* () {
|
|
82
|
+
var _a, _b;
|
|
83
|
+
const filter = direction === "from"
|
|
84
|
+
? { sentAddress: address }
|
|
85
|
+
: { affectedAddress: address };
|
|
86
|
+
const result = yield graphqlClient.query({
|
|
87
|
+
query: QUERY_TRANSACTIONS,
|
|
88
|
+
variables: {
|
|
89
|
+
filter,
|
|
90
|
+
first: 1,
|
|
75
91
|
},
|
|
76
92
|
});
|
|
77
|
-
|
|
93
|
+
const data = result.data;
|
|
94
|
+
return (_b = (_a = data === null || data === void 0 ? void 0 : data.transactions) === null || _a === void 0 ? void 0 : _a.nodes) === null || _b === void 0 ? void 0 : _b[0];
|
|
78
95
|
});
|
|
79
96
|
export const mergeAllCoins = (coinType, transaction, allCoins) => {
|
|
80
97
|
const mergeCoin = allCoins[0];
|
|
81
98
|
if (allCoins.length > 1 && !isSui(coinType)) {
|
|
82
|
-
transaction.mergeCoins(transaction.object(mergeCoin.
|
|
99
|
+
transaction.mergeCoins(transaction.object(mergeCoin.objectId), allCoins.map((c) => transaction.object(c.objectId)).slice(1));
|
|
83
100
|
}
|
|
84
101
|
return mergeCoin;
|
|
85
102
|
};
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@suilend/sui-fe","version":"
|
|
1
|
+
{"name":"@suilend/sui-fe","version":"3.0.1","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/","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.15.0"}}
|