@suilend/sui-fe 3.0.1 → 3.0.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
@@ -36,7 +36,7 @@ export declare const createKeypair: (signature: string) => Promise<{
36
36
  address: string;
37
37
  privateKey: string;
38
38
  }>;
39
- export declare const checkIfKeypairCanBeUsed: (lastSignedTransaction: SignatureWithBytes | undefined, currentFlowDigests: string[], keypair: Ed25519Keypair, suiGrpcClient: SuiGrpcClient, graphqlClient: SuiGraphQLClient) => Promise<{
39
+ export declare const checkIfKeypairCanBeUsed: (lastSignedTransaction: SignatureWithBytes | undefined, currentFlowDigests: string[], keypair: Ed25519Keypair, suiGrpcClient: SuiGrpcClient, suiGraphQLClient: SuiGraphQLClient) => Promise<{
40
40
  lastCurrentFlowTransaction: GraphQLTransaction | undefined;
41
41
  }>;
42
42
  export type FundKeypairResult = {
package/lib/keypair.js CHANGED
@@ -121,11 +121,11 @@ export const createKeypair = (signature) => __awaiter(void 0, void 0, void 0, fu
121
121
  return { keypair, address, privateKey };
122
122
  });
123
123
  // CHECK IF KEYPAIR CAN BE USED
124
- export const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keypair, suiGrpcClient, graphqlClient) => __awaiter(void 0, void 0, void 0, function* () {
124
+ export const checkIfKeypairCanBeUsed = (lastSignedTransaction, currentFlowDigests, keypair, suiGrpcClient, suiGraphQLClient) => __awaiter(void 0, void 0, void 0, function* () {
125
125
  var _a;
126
126
  const [mostRecentFromAddressTransaction, mostRecentToAddressTransaction] = yield Promise.all([
127
- getMostRecentAddressTransaction(graphqlClient, keypair.toSuiAddress(), "from"),
128
- getMostRecentAddressTransaction(graphqlClient, keypair.toSuiAddress(), "to"),
127
+ getMostRecentAddressTransaction(suiGraphQLClient, keypair.toSuiAddress(), "from"),
128
+ getMostRecentAddressTransaction(suiGraphQLClient, keypair.toSuiAddress(), "to"),
129
129
  ]);
130
130
  const mostRecentFromOrToAddressTransaction = [
131
131
  mostRecentFromAddressTransaction,
@@ -1,7 +1,7 @@
1
1
  import { SuiGrpcClient } from "@mysten/sui/grpc";
2
2
  import { SuiGraphQLClient } from "@mysten/sui/graphql";
3
3
  import { SuiClientTypes } from "@mysten/sui/client";
4
- import { Transaction } from "@mysten/sui/transactions";
4
+ import { Transaction, TransactionObjectArgument } from "@mysten/sui/transactions";
5
5
  import BigNumber from "bignumber.js";
6
6
  import { Token } from "./coinMetadata";
7
7
  export type GraphQLTransaction = {
@@ -23,8 +23,9 @@ export declare const getBalanceChange: (res: SuiClientTypes.Transaction<{
23
23
  balanceChanges: true;
24
24
  }>, address: string, token: Token, multiplier?: -1 | 1) => BigNumber | undefined;
25
25
  export declare const getAllOwnedObjects: (suiGrpcClient: SuiGrpcClient, address: string, type?: string) => Promise<SuiClientTypes.Object<{
26
- content: true;
26
+ json: true;
27
27
  }>[]>;
28
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>;
29
+ export declare const getMostRecentAddressTransaction: (suiGraphQLClient: SuiGraphQLClient, address: string, direction: "from" | "to") => Promise<GraphQLTransaction | undefined>;
30
30
  export declare const mergeAllCoins: (coinType: string, transaction: Transaction, allCoins: SuiClientTypes.Coin[]) => SuiClientTypes.Coin;
31
+ export declare const getSpendableCoin: (suiGrpcClient: SuiGrpcClient, address: string, coinType: string, value: string, transaction: Transaction) => Promise<TransactionObjectArgument>;
@@ -37,7 +37,7 @@ export const getAllOwnedObjects = (suiGrpcClient, address, type) => __awaiter(vo
37
37
  owner: address,
38
38
  cursor,
39
39
  type,
40
- include: { content: true },
40
+ include: { json: true },
41
41
  });
42
42
  allObjs.push(...result.objects);
43
43
  cursor = result.cursor;
@@ -78,12 +78,12 @@ const QUERY_TRANSACTIONS = `
78
78
  }
79
79
  }
80
80
  `;
81
- export const getMostRecentAddressTransaction = (graphqlClient, address, direction) => __awaiter(void 0, void 0, void 0, function* () {
81
+ export const getMostRecentAddressTransaction = (suiGraphQLClient, address, direction) => __awaiter(void 0, void 0, void 0, function* () {
82
82
  var _a, _b;
83
83
  const filter = direction === "from"
84
84
  ? { sentAddress: address }
85
85
  : { affectedAddress: address };
86
- const result = yield graphqlClient.query({
86
+ const result = yield suiGraphQLClient.query({
87
87
  query: QUERY_TRANSACTIONS,
88
88
  variables: {
89
89
  filter,
@@ -100,3 +100,60 @@ export const mergeAllCoins = (coinType, transaction, allCoins) => {
100
100
  }
101
101
  return mergeCoin;
102
102
  };
103
+ // Build a spendable coin using owned coin objects, address balance, or both.
104
+ export const getSpendableCoin = (suiGrpcClient, address, coinType, value, transaction) => __awaiter(void 0, void 0, void 0, function* () {
105
+ var _a;
106
+ const spendValue = BigInt(value);
107
+ if (spendValue <= BigInt(0)) {
108
+ throw new Error("Spend amount must be positive");
109
+ }
110
+ // Address-balance withdrawals resolve against the transaction sender.
111
+ transaction.setSenderIfNotSet(address);
112
+ const [coins, balanceRes] = yield Promise.all([
113
+ getAllCoins(suiGrpcClient, address, coinType),
114
+ suiGrpcClient.getBalance({ owner: address, coinType }),
115
+ ]);
116
+ const nonZeroCoins = coins.filter((c) => BigInt(c.balance) > BigInt(0));
117
+ const objectCoinBalance = nonZeroCoins.reduce((sum, c) => sum + BigInt(c.balance), BigInt(0));
118
+ const addressBalanceAvailable = BigInt((_a = balanceRes.balance.addressBalance) !== null && _a !== void 0 ? _a : "0");
119
+ const totalAvailable = BigInt(balanceRes.balance.balance);
120
+ if (totalAvailable < spendValue) {
121
+ throw new Error(`Insufficient ${coinType} balance: need ${value}, have ${balanceRes.balance.balance} (${objectCoinBalance.toString()} in coins, ${addressBalanceAvailable.toString()} in address)`);
122
+ }
123
+ const objectCoinSpend = objectCoinBalance >= spendValue ? spendValue : objectCoinBalance;
124
+ const addressBalanceSpend = spendValue - objectCoinSpend;
125
+ console.log(`[getSpendableCoin] ${coinType} requested=${value} coinObjects=${objectCoinSpend.toString()} addressBalance=${addressBalanceSpend.toString()}`);
126
+ // Address balance only.
127
+ if (objectCoinSpend === BigInt(0)) {
128
+ return transaction.moveCall({
129
+ target: "0x2::coin::redeem_funds",
130
+ typeArguments: [coinType],
131
+ arguments: [transaction.withdrawal({ amount: value, type: coinType })],
132
+ });
133
+ }
134
+ const mergedCoin = mergeAllCoins(coinType, transaction, nonZeroCoins);
135
+ const coinSource = isSui(coinType)
136
+ ? transaction.gas
137
+ : transaction.object(mergedCoin.objectId);
138
+ // Coin objects only.
139
+ if (addressBalanceSpend === BigInt(0)) {
140
+ const [sendCoin] = transaction.splitCoins(coinSource, [value]);
141
+ return sendCoin;
142
+ }
143
+ // Mixed: split from coin objects, redeem the rest from address balance, merge.
144
+ const [objectCoin] = transaction.splitCoins(coinSource, [
145
+ objectCoinSpend.toString(),
146
+ ]);
147
+ const addressCoin = transaction.moveCall({
148
+ target: "0x2::coin::redeem_funds",
149
+ typeArguments: [coinType],
150
+ arguments: [
151
+ transaction.withdrawal({
152
+ amount: addressBalanceSpend.toString(),
153
+ type: coinType,
154
+ }),
155
+ ],
156
+ });
157
+ transaction.mergeCoins(objectCoin, [addressCoin]);
158
+ return objectCoin;
159
+ });
package/package.json CHANGED
@@ -1 +1 @@
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"}}
1
+ {"name":"@suilend/sui-fe","version":"3.0.3","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"}}