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