@interest-protocol/vortex-sdk 8.4.0 → 9.0.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/dist/index.mjs CHANGED
@@ -8969,6 +8969,164 @@ class SuiJsonRpcClient extends Experimental_BaseClient {
8969
8969
  }
8970
8970
  }
8971
8971
 
8972
+ const COIN_WITH_BALANCE = "CoinWithBalance";
8973
+ const SUI_TYPE = normalizeStructTag("0x2::sui::SUI");
8974
+ function coinWithBalance({
8975
+ type = SUI_TYPE,
8976
+ balance,
8977
+ useGasCoin = true
8978
+ }) {
8979
+ let coinResult = null;
8980
+ return (tx) => {
8981
+ if (coinResult) {
8982
+ return coinResult;
8983
+ }
8984
+ tx.addIntentResolver(COIN_WITH_BALANCE, resolveCoinBalance);
8985
+ const coinType = type === "gas" ? type : normalizeStructTag(type);
8986
+ coinResult = tx.add(
8987
+ Commands.Intent({
8988
+ name: COIN_WITH_BALANCE,
8989
+ inputs: {},
8990
+ data: {
8991
+ type: coinType === SUI_TYPE && useGasCoin ? "gas" : coinType,
8992
+ balance: BigInt(balance)
8993
+ }
8994
+ })
8995
+ );
8996
+ return coinResult;
8997
+ };
8998
+ }
8999
+ const CoinWithBalanceData = object({
9000
+ type: string(),
9001
+ balance: bigint()
9002
+ });
9003
+ async function resolveCoinBalance(transactionData, buildOptions, next) {
9004
+ const coinTypes = /* @__PURE__ */ new Set();
9005
+ const totalByType = /* @__PURE__ */ new Map();
9006
+ if (!transactionData.sender) {
9007
+ throw new Error("Sender must be set to resolve CoinWithBalance");
9008
+ }
9009
+ for (const command of transactionData.commands) {
9010
+ if (command.$kind === "$Intent" && command.$Intent.name === COIN_WITH_BALANCE) {
9011
+ const { type, balance } = parse(CoinWithBalanceData, command.$Intent.data);
9012
+ if (type !== "gas" && balance > 0n) {
9013
+ coinTypes.add(type);
9014
+ }
9015
+ totalByType.set(type, (totalByType.get(type) ?? 0n) + balance);
9016
+ }
9017
+ }
9018
+ const usedIds = /* @__PURE__ */ new Set();
9019
+ for (const input of transactionData.inputs) {
9020
+ if (input.Object?.ImmOrOwnedObject) {
9021
+ usedIds.add(input.Object.ImmOrOwnedObject.objectId);
9022
+ }
9023
+ if (input.UnresolvedObject?.objectId) {
9024
+ usedIds.add(input.UnresolvedObject.objectId);
9025
+ }
9026
+ }
9027
+ const coinsByType = /* @__PURE__ */ new Map();
9028
+ const client = getSuiClient(buildOptions);
9029
+ await Promise.all(
9030
+ [...coinTypes].map(async (coinType) => {
9031
+ coinsByType.set(
9032
+ coinType,
9033
+ await getCoinsOfType({
9034
+ coinType,
9035
+ balance: totalByType.get(coinType),
9036
+ client,
9037
+ owner: transactionData.sender,
9038
+ usedIds
9039
+ })
9040
+ );
9041
+ })
9042
+ );
9043
+ const mergedCoins = /* @__PURE__ */ new Map();
9044
+ mergedCoins.set("gas", { $kind: "GasCoin", GasCoin: true });
9045
+ for (const [index, transaction] of transactionData.commands.entries()) {
9046
+ if (transaction.$kind !== "$Intent" || transaction.$Intent.name !== COIN_WITH_BALANCE) {
9047
+ continue;
9048
+ }
9049
+ const { type, balance } = transaction.$Intent.data;
9050
+ if (balance === 0n && type !== "gas") {
9051
+ transactionData.replaceCommand(
9052
+ index,
9053
+ Commands.MoveCall({ target: "0x2::coin::zero", typeArguments: [type] })
9054
+ );
9055
+ continue;
9056
+ }
9057
+ const commands = [];
9058
+ if (!mergedCoins.has(type)) {
9059
+ const [first, ...rest] = coinsByType.get(type).map(
9060
+ (coin) => transactionData.addInput(
9061
+ "object",
9062
+ Inputs.ObjectRef({
9063
+ objectId: coin.coinObjectId,
9064
+ digest: coin.digest,
9065
+ version: coin.version
9066
+ })
9067
+ )
9068
+ );
9069
+ if (rest.length > 0) {
9070
+ commands.push(Commands.MergeCoins(first, rest));
9071
+ }
9072
+ mergedCoins.set(type, first);
9073
+ }
9074
+ commands.push(
9075
+ Commands.SplitCoins(mergedCoins.get(type), [
9076
+ transactionData.addInput("pure", Inputs.Pure(suiBcs.u64().serialize(balance)))
9077
+ ])
9078
+ );
9079
+ transactionData.replaceCommand(index, commands);
9080
+ transactionData.mapArguments((arg) => {
9081
+ if (arg.$kind === "Result" && arg.Result === index) {
9082
+ return {
9083
+ $kind: "NestedResult",
9084
+ NestedResult: [index + commands.length - 1, 0]
9085
+ };
9086
+ }
9087
+ return arg;
9088
+ });
9089
+ }
9090
+ return next();
9091
+ }
9092
+ async function getCoinsOfType({
9093
+ coinType,
9094
+ balance,
9095
+ client,
9096
+ owner,
9097
+ usedIds
9098
+ }) {
9099
+ let remainingBalance = balance;
9100
+ const coins = [];
9101
+ return loadMoreCoins();
9102
+ async function loadMoreCoins(cursor = null) {
9103
+ const { data, hasNextPage, nextCursor } = await client.getCoins({ owner, coinType, cursor });
9104
+ const sortedCoins = data.sort((a, b) => Number(BigInt(b.balance) - BigInt(a.balance)));
9105
+ for (const coin of sortedCoins) {
9106
+ if (usedIds.has(coin.coinObjectId)) {
9107
+ continue;
9108
+ }
9109
+ const coinBalance = BigInt(coin.balance);
9110
+ coins.push(coin);
9111
+ remainingBalance -= coinBalance;
9112
+ if (remainingBalance <= 0) {
9113
+ return coins;
9114
+ }
9115
+ }
9116
+ if (hasNextPage) {
9117
+ return loadMoreCoins(nextCursor);
9118
+ }
9119
+ throw new Error(`Not enough coins of type ${coinType} to satisfy requested balance`);
9120
+ }
9121
+ }
9122
+ function getSuiClient(options) {
9123
+ const client = getClient$1(options);
9124
+ if (!client.jsonRpc) {
9125
+ throw new Error(`CoinWithBalance intent currently only works with SuiClient`);
9126
+ }
9127
+ return client;
9128
+ }
9129
+
8972
9130
  var Action;
8973
9131
  (function (Action) {
8974
9132
  Action[Action["Deposit"] = 0] = "Deposit";
@@ -39605,7 +39763,7 @@ const deposit = async ({ tx = new Transaction(), amount, unspentUtxos = [], vort
39605
39763
  invariant(unspentUtxos.length <= 2, 'Unspent UTXOs must be at most 2');
39606
39764
  invariant(BN254_FIELD_MODULUS > amount, 'Amount must be less than field modulus');
39607
39765
  const accountSecret = 0n;
39608
- const { extData, tx: tx3, moveProof, } = await prepareDepositProof({
39766
+ const { extData, tx: tx2, moveProof, } = await prepareDepositProof({
39609
39767
  tx,
39610
39768
  amount,
39611
39769
  accountSecret,
@@ -39617,13 +39775,17 @@ const deposit = async ({ tx = new Transaction(), amount, unspentUtxos = [], vort
39617
39775
  relayer,
39618
39776
  relayerFee,
39619
39777
  });
39620
- const suiCoinDeposit = tx3.splitCoins(tx3.gas, [tx3.pure.u64(amount)]);
39778
+ const vortexPoolObject = await vortexSdk.resolveVortexPool(vortexPool);
39779
+ const deposit = coinWithBalance({
39780
+ balance: amount,
39781
+ type: vortexPoolObject.coinType,
39782
+ })(tx2);
39621
39783
  return vortexSdk.transact({
39622
39784
  vortexPool,
39623
- tx: tx3,
39785
+ tx: tx2,
39624
39786
  proof: moveProof,
39625
39787
  extData: extData,
39626
- deposit: suiCoinDeposit,
39788
+ deposit,
39627
39789
  });
39628
39790
  };
39629
39791