@suilend/sui-fe 3.0.2 → 3.0.4

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.
@@ -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 = {
@@ -28,3 +28,4 @@ export declare const getAllOwnedObjects: (suiGrpcClient: SuiGrpcClient, address:
28
28
  export declare const getAllCoins: (suiGrpcClient: SuiGrpcClient, address: string, coinType: string) => Promise<SuiClientTypes.Coin[]>;
29
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, mergeCoins?: boolean) => Promise<TransactionObjectArgument>;
@@ -100,3 +100,68 @@ 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_1, address_1, coinType_1, value_1, transaction_1, ...args_1) => __awaiter(void 0, [suiGrpcClient_1, address_1, coinType_1, value_1, transaction_1, ...args_1], void 0, function* (suiGrpcClient, address, coinType, value, transaction, mergeCoins = true) {
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
+ let coinSource;
135
+ if (mergeCoins) {
136
+ const mergedCoin = mergeAllCoins(coinType, transaction, nonZeroCoins);
137
+ coinSource = isSui(coinType)
138
+ ? transaction.gas
139
+ : transaction.object(mergedCoin.objectId);
140
+ }
141
+ else {
142
+ coinSource = isSui(coinType)
143
+ ? transaction.gas
144
+ : transaction.object(nonZeroCoins[0].objectId);
145
+ }
146
+ // Coin objects only.
147
+ if (addressBalanceSpend === BigInt(0)) {
148
+ const [sendCoin] = transaction.splitCoins(coinSource, [value]);
149
+ return sendCoin;
150
+ }
151
+ // Mixed: split from coin objects, redeem the rest from address balance, merge.
152
+ const [objectCoin] = transaction.splitCoins(coinSource, [
153
+ objectCoinSpend.toString(),
154
+ ]);
155
+ const addressCoin = transaction.moveCall({
156
+ target: "0x2::coin::redeem_funds",
157
+ typeArguments: [coinType],
158
+ arguments: [
159
+ transaction.withdrawal({
160
+ amount: addressBalanceSpend.toString(),
161
+ type: coinType,
162
+ }),
163
+ ],
164
+ });
165
+ transaction.mergeCoins(objectCoin, [addressCoin]);
166
+ return objectCoin;
167
+ });
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@suilend/sui-fe","version":"3.0.2","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.4","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"}}