@suilend/sui-fe 2.0.10 → 2.0.11
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/transactions.d.ts +2 -1
- package/lib/transactions.js +57 -0
- package/package.json +1 -1
package/lib/transactions.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CoinStruct, SuiJsonRpcClient, SuiObjectDataFilter, SuiObjectResponse, SuiTransactionBlockResponse } from "@mysten/sui/jsonRpc";
|
|
2
|
-
import { Transaction } from "@mysten/sui/transactions";
|
|
2
|
+
import { Transaction, TransactionObjectArgument } from "@mysten/sui/transactions";
|
|
3
3
|
import BigNumber from "bignumber.js";
|
|
4
4
|
import { Token } from "./coinMetadata";
|
|
5
5
|
export declare const getTotalGasFee: (res: SuiTransactionBlockResponse) => BigNumber;
|
|
@@ -8,3 +8,4 @@ export declare const getAllOwnedObjects: (suiClient: SuiJsonRpcClient, address:
|
|
|
8
8
|
export declare const getAllCoins: (suiClient: SuiJsonRpcClient, address: string, coinType: string) => Promise<CoinStruct[]>;
|
|
9
9
|
export declare const getMostRecentAddressTransaction: (suiClient: SuiJsonRpcClient, address: string, direction: "from" | "to") => Promise<SuiTransactionBlockResponse | undefined>;
|
|
10
10
|
export declare const mergeAllCoins: (coinType: string, transaction: Transaction, allCoins: CoinStruct[]) => CoinStruct;
|
|
11
|
+
export declare const getSpendableCoin: (suiClient: SuiJsonRpcClient, address: string, coinType: string, value: string, transaction: Transaction) => Promise<TransactionObjectArgument>;
|
package/lib/transactions.js
CHANGED
|
@@ -83,3 +83,60 @@ export const mergeAllCoins = (coinType, transaction, allCoins) => {
|
|
|
83
83
|
}
|
|
84
84
|
return mergeCoin;
|
|
85
85
|
};
|
|
86
|
+
// Build a spendable coin using owned coin objects, address balance, or both.
|
|
87
|
+
export const getSpendableCoin = (suiClient, address, coinType, value, transaction) => __awaiter(void 0, void 0, void 0, function* () {
|
|
88
|
+
var _a;
|
|
89
|
+
const spendValue = BigInt(value);
|
|
90
|
+
if (spendValue <= BigInt(0)) {
|
|
91
|
+
throw new Error("Spend amount must be positive");
|
|
92
|
+
}
|
|
93
|
+
// Address-balance withdrawals resolve against the transaction sender.
|
|
94
|
+
transaction.setSenderIfNotSet(address);
|
|
95
|
+
const [coins, balance] = yield Promise.all([
|
|
96
|
+
getAllCoins(suiClient, address, coinType),
|
|
97
|
+
suiClient.getBalance({ owner: address, coinType }),
|
|
98
|
+
]);
|
|
99
|
+
const nonZeroCoins = coins.filter((c) => BigInt(c.balance) > BigInt(0));
|
|
100
|
+
const objectCoinBalance = nonZeroCoins.reduce((sum, c) => sum + BigInt(c.balance), BigInt(0));
|
|
101
|
+
const addressBalanceAvailable = BigInt((_a = balance.fundsInAddressBalance) !== null && _a !== void 0 ? _a : "0");
|
|
102
|
+
const totalAvailable = BigInt(balance.totalBalance);
|
|
103
|
+
if (totalAvailable < spendValue) {
|
|
104
|
+
throw new Error(`Insufficient ${coinType} balance: need ${value}, have ${balance.totalBalance} (${objectCoinBalance.toString()} in coins, ${addressBalanceAvailable.toString()} in address)`);
|
|
105
|
+
}
|
|
106
|
+
const objectCoinSpend = objectCoinBalance >= spendValue ? spendValue : objectCoinBalance;
|
|
107
|
+
const addressBalanceSpend = spendValue - objectCoinSpend;
|
|
108
|
+
console.log(`[getSpendableCoin] ${coinType} requested=${value} coinObjects=${objectCoinSpend.toString()} addressBalance=${addressBalanceSpend.toString()}`);
|
|
109
|
+
// Address balance only.
|
|
110
|
+
if (objectCoinSpend === BigInt(0)) {
|
|
111
|
+
return transaction.moveCall({
|
|
112
|
+
target: "0x2::coin::redeem_funds",
|
|
113
|
+
typeArguments: [coinType],
|
|
114
|
+
arguments: [transaction.withdrawal({ amount: value, type: coinType })],
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
const mergedCoin = mergeAllCoins(coinType, transaction, nonZeroCoins);
|
|
118
|
+
const coinSource = isSui(coinType)
|
|
119
|
+
? transaction.gas
|
|
120
|
+
: transaction.object(mergedCoin.coinObjectId);
|
|
121
|
+
// Coin objects only.
|
|
122
|
+
if (addressBalanceSpend === BigInt(0)) {
|
|
123
|
+
const [sendCoin] = transaction.splitCoins(coinSource, [value]);
|
|
124
|
+
return sendCoin;
|
|
125
|
+
}
|
|
126
|
+
// Mixed: split from coin objects, redeem the rest from address balance, merge.
|
|
127
|
+
const [objectCoin] = transaction.splitCoins(coinSource, [
|
|
128
|
+
objectCoinSpend.toString(),
|
|
129
|
+
]);
|
|
130
|
+
const addressCoin = transaction.moveCall({
|
|
131
|
+
target: "0x2::coin::redeem_funds",
|
|
132
|
+
typeArguments: [coinType],
|
|
133
|
+
arguments: [
|
|
134
|
+
transaction.withdrawal({
|
|
135
|
+
amount: addressBalanceSpend.toString(),
|
|
136
|
+
type: coinType,
|
|
137
|
+
}),
|
|
138
|
+
],
|
|
139
|
+
});
|
|
140
|
+
transaction.mergeCoins(objectCoin, [addressCoin]);
|
|
141
|
+
return objectCoin;
|
|
142
|
+
});
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@suilend/sui-fe","version":"2.0.
|
|
1
|
+
{"name":"@suilend/sui-fe","version":"2.0.11","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.3.1"}}
|