@velora-dex/sdk 9.5.2 → 9.5.3-dev.1

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.
@@ -6800,6 +6800,467 @@ function constructSimpleContractCaller(providerOptions) {
6800
6800
  };
6801
6801
  }
6802
6802
 
6803
+ var _OrderKindToSwapSide;
6804
+ ///// CHECKS //////
6805
+ /**
6806
+ * @description Checks whether an order is a TWAP Sell or TWAP Buy order.
6807
+ */
6808
+ function isTWAPOrder(order) {
6809
+ return 'numSlices' in order && typeof order.numSlices === 'number';
6810
+ }
6811
+ /**
6812
+ * @description Checks whether an order is a TWAP Sell order.
6813
+ */
6814
+ function isTWAPSellOrder(order) {
6815
+ return isTWAPOrder(order) && 'totalSrcAmount' in order && typeof order.totalSrcAmount === 'string';
6816
+ }
6817
+ /**
6818
+ * @description Checks whether an order is a TWAP Buy order.
6819
+ */
6820
+ function isTWAPBuyOrder(order) {
6821
+ return isTWAPOrder(order) && 'totalDestAmount' in order && typeof order.totalDestAmount === 'string';
6822
+ }
6823
+ /**
6824
+ * @description Checks whether an order is an External order.
6825
+ */
6826
+ function isExternalOrder(order) {
6827
+ return 'handler' in order;
6828
+ }
6829
+ /**
6830
+ * @description Checks whether an order is a regular Delta auction order.
6831
+ */
6832
+ function isDeltaOrder(order) {
6833
+ return !isExternalOrder(order) && 'kind' in order && typeof order.kind === 'number';
6834
+ }
6835
+ /**
6836
+ * @description Checks whether an auction is a TWAP auction.
6837
+ */
6838
+ function isTWAPAuction(auction) {
6839
+ return isTWAPSellAuction(auction) || isTWAPBuyAuction(auction);
6840
+ }
6841
+ /**
6842
+ * @description Checks whether an auction is a TWAP Sell auction.
6843
+ */
6844
+ function isTWAPSellAuction(auction) {
6845
+ return auction.onChainOrderType === 'TWAPOrder';
6846
+ }
6847
+ /**
6848
+ * @description Checks whether an auction is a TWAP Buy auction.
6849
+ */
6850
+ function isTWAPBuyAuction(auction) {
6851
+ return auction.onChainOrderType === 'TWAPBuyOrder';
6852
+ }
6853
+ /**
6854
+ * @description Checks whether an auction is a Delta auction.
6855
+ */
6856
+ function isDeltaAuction(auction) {
6857
+ return auction.onChainOrderType === 'Order';
6858
+ }
6859
+ /**
6860
+ * @description Checks whether an auction is an External auction.
6861
+ */
6862
+ function isExternalAuction(auction) {
6863
+ return auction.onChainOrderType === 'ExternalOrder';
6864
+ }
6865
+ var checks = {
6866
+ isTWAPOrder: isTWAPOrder,
6867
+ isTWAPSellOrder: isTWAPSellOrder,
6868
+ isTWAPBuyOrder: isTWAPBuyOrder,
6869
+ isExternalOrder: isExternalOrder,
6870
+ isDeltaOrder: isDeltaOrder,
6871
+ isTWAPAuction: isTWAPAuction,
6872
+ isTWAPSellAuction: isTWAPSellAuction,
6873
+ isTWAPBuyAuction: isTWAPBuyAuction,
6874
+ isDeltaAuction: isDeltaAuction,
6875
+ isExternalAuction: isExternalAuction,
6876
+ isOrderCrosschain: isOrderCrosschain,
6877
+ isExecutedAuction: isExecutedAuction,
6878
+ isPartiallyExecutedAuction: isPartiallyExecutedAuction,
6879
+ isFailedAuction: isFailedAuction,
6880
+ isCanceledAuction: isCanceledAuction,
6881
+ isExpiredAuction: isExpiredAuction,
6882
+ isPendingAuction: isPendingAuction
6883
+ };
6884
+ ///// GETTERS //////
6885
+ /**
6886
+ * @description Returns the expected source amount for a TWAP order.
6887
+ */
6888
+ function getExpectedTwapSrcAmount(order) {
6889
+ if ('totalSrcAmount' in order) {
6890
+ // SELL
6891
+ return order.totalSrcAmount;
6892
+ }
6893
+ return order.maxSrcAmount; // BUY
6894
+ }
6895
+ /**
6896
+ * @description Returns the expected destination amount for a TWAP order.
6897
+ */
6898
+ function getExpectedTwapDestAmount(order) {
6899
+ var destAmount = 'destAmountPerSlice' in order ? BigInt(order.destAmountPerSlice) * BigInt(order.numSlices) // SELL
6900
+ : BigInt(order.totalDestAmount); // BUY
6901
+ if (isOrderCrosschain(order)) {
6902
+ return scaleByFactor(destAmount, order.bridge.scalingFactor).toString();
6903
+ }
6904
+ return destAmount.toString();
6905
+ }
6906
+ /**
6907
+ * @description Returns expected source and destination amounts for a TWAP order.
6908
+ */
6909
+ function getExpectedTwapOrderAmounts(order) {
6910
+ var srcAmount = getExpectedTwapSrcAmount(order);
6911
+ var destAmount = getExpectedTwapDestAmount(order);
6912
+ return {
6913
+ srcAmount: srcAmount,
6914
+ destAmount: destAmount
6915
+ };
6916
+ }
6917
+ /**
6918
+ * @description Returns expected and, when available, final amounts for a TWAP auction.
6919
+ */
6920
+ function getTwapAuctionAmounts(twapAuction) {
6921
+ var isExecuted = isExecutedAuction(twapAuction);
6922
+ var expected = getExpectedTwapOrderAmounts(twapAuction.order);
6923
+ if (isExecuted) {
6924
+ var _final = getTransactionAmounts(twapAuction.transactions);
6925
+ return {
6926
+ "final": _final,
6927
+ expected: expected
6928
+ };
6929
+ }
6930
+ return {
6931
+ expected: expected
6932
+ };
6933
+ }
6934
+ var getters = {
6935
+ getUnifiedDeltaOrderData: getUnifiedDeltaOrderData,
6936
+ getExpectedTwapSrcAmount: getExpectedTwapSrcAmount,
6937
+ getExpectedTwapDestAmount: getExpectedTwapDestAmount,
6938
+ getExpectedTwapOrderAmounts: getExpectedTwapOrderAmounts,
6939
+ getTwapAuctionAmounts: getTwapAuctionAmounts,
6940
+ getAuctionDestChainId: getAuctionDestChainId,
6941
+ getSwapSideFromDeltaOrder: getSwapSideFromDeltaOrder,
6942
+ getSwapSideFromTwapOrderType: getSwapSideFromTwapOrderType,
6943
+ getAuctionSwapSide: getAuctionSwapSide,
6944
+ getOrderTokenAddresses: getOrderTokenAddresses,
6945
+ getTransactionAmounts: getTransactionAmounts,
6946
+ getAuctionAmounts: getAuctionAmounts,
6947
+ getFilledPercent: getFilledPercent
6948
+ };
6949
+ var OrderHelpers = {
6950
+ checks: checks,
6951
+ getters: getters
6952
+ };
6953
+ // -------------------- Auction Unified Data --------------------
6954
+ /**
6955
+ * @description Returns the destination chain id for the auction.
6956
+ */
6957
+ function getAuctionDestChainId(_ref) {
6958
+ var order = _ref.order,
6959
+ chainId = _ref.chainId;
6960
+ return isOrderCrosschain(order) ? order.bridge.destinationChainId : chainId;
6961
+ }
6962
+ var OrderKindToSwapSide = (_OrderKindToSwapSide = {}, _OrderKindToSwapSide[OrderKind.Sell] = 'SELL', _OrderKindToSwapSide[OrderKind.Buy] = 'BUY', _OrderKindToSwapSide);
6963
+ /**
6964
+ * @description Returns swap side from a Delta or External order kind.
6965
+ */
6966
+ function getSwapSideFromDeltaOrder(order) {
6967
+ return OrderKindToSwapSide[order.kind];
6968
+ }
6969
+ var TwapTypeToSwapSide = {
6970
+ TWAPOrder: 'SELL',
6971
+ TWAPBuyOrder: 'BUY'
6972
+ };
6973
+ /**
6974
+ * @description Returns swap side from TWAP on-chain order type.
6975
+ */
6976
+ function getSwapSideFromTwapOrderType(onChainOrderType) {
6977
+ return TwapTypeToSwapSide[onChainOrderType];
6978
+ }
6979
+ /**
6980
+ * @description Returns swap side for any auction type.
6981
+ */
6982
+ function getAuctionSwapSide(auction) {
6983
+ if (isTWAPAuction(auction)) {
6984
+ // TWAP orders have onChainOrderType instead of kind
6985
+ return getSwapSideFromTwapOrderType(auction.onChainOrderType);
6986
+ }
6987
+ return getSwapSideFromDeltaOrder(auction.order);
6988
+ }
6989
+ /**
6990
+ * @description Returns unified order data with normalized amounts, tokens, and side.
6991
+ */
6992
+ function getUnifiedDeltaOrderData(auction) {
6993
+ var order = auction.order,
6994
+ chainId = auction.chainId;
6995
+ var _getOrderTokenAddress = getOrderTokenAddresses(order),
6996
+ srcToken = _getOrderTokenAddress.srcToken,
6997
+ destToken = _getOrderTokenAddress.destToken;
6998
+ var _getAuctionAmounts = getAuctionAmounts(auction),
6999
+ expected = _getAuctionAmounts.expected,
7000
+ _final2 = _getAuctionAmounts["final"];
7001
+ var srcChainId = chainId;
7002
+ var destChainId = getAuctionDestChainId({
7003
+ order: order,
7004
+ chainId: chainId
7005
+ });
7006
+ var swapSide = getAuctionSwapSide(auction);
7007
+ var filledPercent = getFilledPercent(auction);
7008
+ return {
7009
+ srcChainId: srcChainId,
7010
+ destChainId: destChainId,
7011
+ srcAmount: (_final2 == null ? void 0 : _final2.srcAmount) || expected.srcAmount,
7012
+ destAmount: (_final2 == null ? void 0 : _final2.destAmount) || expected.destAmount,
7013
+ amounts: {
7014
+ expected: expected,
7015
+ "final": _final2
7016
+ },
7017
+ srcToken: srcToken,
7018
+ destToken: destToken,
7019
+ swapSide: swapSide,
7020
+ filledPercent: filledPercent
7021
+ };
7022
+ }
7023
+ /**
7024
+ * @description Returns source and destination token addresses for an order.
7025
+ */
7026
+ function getOrderTokenAddresses(order) {
7027
+ var srcToken = order.srcToken;
7028
+ var destToken = isOrderCrosschain(order) ? order.bridge.outputToken : order.destToken;
7029
+ return {
7030
+ srcToken: srcToken,
7031
+ destToken: destToken
7032
+ };
7033
+ }
7034
+ /**
7035
+ * @description Aggregates transaction amounts into total source and destination values.
7036
+ */
7037
+ function getTransactionAmounts(transactions) {
7038
+ var _transactions$reduce = transactions.reduce(function (acc, _ref2) {
7039
+ var spentAmount = _ref2.spentAmount,
7040
+ receivedAmount = _ref2.receivedAmount,
7041
+ bridgeMetadata = _ref2.bridgeMetadata;
7042
+ return {
7043
+ srcAmount: acc.srcAmount + BigInt(spentAmount),
7044
+ destAmount: acc.destAmount + BigInt(bridgeMetadata ? bridgeMetadata.outputAmount : receivedAmount)
7045
+ };
7046
+ }, {
7047
+ srcAmount: 0n,
7048
+ destAmount: 0n
7049
+ }),
7050
+ srcAmount = _transactions$reduce.srcAmount,
7051
+ destAmount = _transactions$reduce.destAmount;
7052
+ return {
7053
+ srcAmount: srcAmount.toString(),
7054
+ destAmount: destAmount.toString()
7055
+ };
7056
+ }
7057
+ /**
7058
+ * @description Returns expected and, when available, final amounts for an auction.
7059
+ */
7060
+ function getAuctionAmounts(auction) {
7061
+ var isTwap = checks.isTWAPAuction(auction);
7062
+ if (isTwap) {
7063
+ return getTwapAuctionAmounts(auction);
7064
+ }
7065
+ var expected = {
7066
+ srcAmount: auction.order.srcAmount,
7067
+ destAmount: auction.order.expectedAmount || auction.order.destAmount
7068
+ };
7069
+ var order = auction.order;
7070
+ if (isOrderCrosschain(order)) {
7071
+ expected = {
7072
+ srcAmount: expected.srcAmount,
7073
+ destAmount: scaleByFactor(BigInt(expected.destAmount), order.bridge.scalingFactor).toString()
7074
+ };
7075
+ }
7076
+ var isExecuted = isExecutedAuction(auction);
7077
+ if (isExecuted) {
7078
+ var _final3 = getTransactionAmounts(auction.transactions);
7079
+ return {
7080
+ "final": _final3,
7081
+ expected: expected
7082
+ };
7083
+ }
7084
+ return {
7085
+ expected: expected
7086
+ };
7087
+ }
7088
+ /**
7089
+ * @description Checks whether an order includes valid cross-chain bridge details.
7090
+ */
7091
+ function isOrderCrosschain(order
7092
+ // Extract<ExternalOrder, { bridge?: Bridge }> == never
7093
+ ) {
7094
+ return 'bridge' in order && !!order.bridge && order.bridge.destinationChainId !== 0;
7095
+ }
7096
+ function scaleByFactor(amount, scalingFactor) {
7097
+ if (!amount) return 0n;
7098
+ if (scalingFactor === undefined) return amount;
7099
+ var base = 10n;
7100
+ return scalingFactor < 0 ? amount / base ** BigInt(-scalingFactor) : amount * base ** BigInt(scalingFactor);
7101
+ }
7102
+ /**
7103
+ * @description Checks whether an auction is fully executed.
7104
+ */
7105
+ function isExecutedAuction(auction) {
7106
+ if (auction.status !== 'EXECUTED') return false;
7107
+ if (isOrderCrosschain(auction.order)) {
7108
+ var filledPercent = getFilledPercent(auction);
7109
+ return filledPercent === 100;
7110
+ }
7111
+ return true;
7112
+ }
7113
+ var failedAuctionStatuses = ['FAILED', 'EXPIRED', 'CANCELLED', 'REFUNDED'];
7114
+ var failedAuctionStatusesSet = /*#__PURE__*/new Set(failedAuctionStatuses);
7115
+ /**
7116
+ * @description Checks whether an auction is failed on source or destination chain.
7117
+ */
7118
+ function isFailedAuction(auction) {
7119
+ // already failed on srcChain, whether Order is crosschain or not
7120
+ if (failedAuctionStatusesSet.has(auction.status)) return true;
7121
+ // crosschain Order is executed on srcChain, but failed on destChain
7122
+ if (auction.status === 'EXECUTED' && isOrderCrosschain(auction.order)) {
7123
+ return auction.bridgeStatus === 'expired' || auction.bridgeStatus === 'refunded';
7124
+ }
7125
+ return false;
7126
+ }
7127
+ /**
7128
+ * @description Checks whether an auction status is cancelled.
7129
+ */
7130
+ function isCanceledAuction(auction) {
7131
+ return auction.status === 'CANCELLED';
7132
+ }
7133
+ /**
7134
+ * @description Checks whether an auction status is expired.
7135
+ */
7136
+ function isExpiredAuction(auction) {
7137
+ return auction.status === 'EXPIRED';
7138
+ }
7139
+ var pendingAuctionStatuses = ['NOT_STARTED', 'AWAITING_PRE_SIGNATURE', 'RUNNING', 'EXECUTING'];
7140
+ var pendingAuctionStatusesSet = /*#__PURE__*/new Set(pendingAuctionStatuses);
7141
+ /**
7142
+ * @description Checks whether an auction status is in pending execution states.
7143
+ */
7144
+ function isPendingAuction(auction) {
7145
+ return pendingAuctionStatusesSet.has(auction.status);
7146
+ }
7147
+ /**
7148
+ * @description Auction can be cancelled in the middle of execution,
7149
+ * or crosschain-TWAP slices may not all be bridged,
7150
+ * or order can be suspended if it runs out of user balance/allowance.
7151
+ * Orders in the middle of normal execution can also be considered partially executed if they have any transactions.
7152
+ */
7153
+ function isPartiallyExecutedAuction(auction) {
7154
+ if (auction.transactions.length === 0) return false;
7155
+ var filledPercent = getFilledPercent(auction);
7156
+ return filledPercent > 0 && filledPercent < 100;
7157
+ }
7158
+ /**
7159
+ * @description Calculates filled percentage from auction transaction filled bps values.
7160
+ */
7161
+ function getFilledPercent(auction) {
7162
+ var transaction = !isOrderCrosschain(auction.order) ? auction.transactions : auction.transactions.filter(function (transaction) {
7163
+ return transaction.bridgeStatus === 'filled';
7164
+ });
7165
+ var filledPercentBps = transaction.reduce(function (acc, _ref3) {
7166
+ var filledPercent = _ref3.filledPercent;
7167
+ return acc + filledPercent;
7168
+ }, 0);
7169
+ var filledPercent = filledPercentBps / 100;
7170
+ return filledPercent;
7171
+ }
7172
+ ///// TESTS //////
7173
+ // @TODO remove
7174
+ var auction = {};
7175
+ var minAuction = {
7176
+ onChainOrderType: 'TWAPOrder',
7177
+ gas: 6
7178
+ };
7179
+ if (isTWAPAuction(minAuction)) {
7180
+ console.log('🚀 ~ auction:', minAuction);
7181
+ /**
7182
+ * {
7183
+ onChainOrderType: OnChainOrderType;
7184
+ gas: number;
7185
+ } & {
7186
+ onChainOrderType: "TWAPOrder" | "TWAPBuyOrder";
7187
+ }
7188
+ */
7189
+ }
7190
+ if (isTWAPAuction(auction)) {
7191
+ console.log('🚀 ~ auction:', auction);
7192
+ /**
7193
+ (DeltaAuctionBase & {
7194
+ onChainOrderType: "TWAPOrder";
7195
+ order: TWAPDeltaOrder;
7196
+ }) | (DeltaAuctionBase & {
7197
+ onChainOrderType: "TWAPBuyOrder";
7198
+ order: TWAPBuyDeltaOrder;
7199
+ })
7200
+ */
7201
+ }
7202
+ if (isTWAPSellAuction(auction)) {
7203
+ console.log('🚀 ~ auction:', auction);
7204
+ /**
7205
+ * DeltaAuctionBase & {
7206
+ onChainOrderType: "TWAPOrder";
7207
+ order: TWAPDeltaOrder;
7208
+ }
7209
+ */
7210
+ }
7211
+ if (isTWAPBuyAuction(auction)) {
7212
+ console.log('🚀 ~ auction:', auction);
7213
+ /**
7214
+ * DeltaAuctionBase & {
7215
+ onChainOrderType: "TWAPBuyOrder";
7216
+ order: TWAPBuyDeltaOrder;
7217
+ }
7218
+ */
7219
+ }
7220
+ if (isDeltaAuction(auction)) {
7221
+ console.log('🚀 ~ auction:', auction);
7222
+ /**
7223
+ * DeltaAuctionBase & {
7224
+ onChainOrderType: "Order";
7225
+ order: DeltaAuctionOrder;
7226
+ }
7227
+ */
7228
+ }
7229
+ if (isExternalAuction(auction)) {
7230
+ console.log('🚀 ~ auction:', auction);
7231
+ /**
7232
+ * DeltaAuctionBase & {
7233
+ onChainOrderType: "ExternalOrder";
7234
+ order: ExternalDeltaOrder;
7235
+ }
7236
+ */
7237
+ }
7238
+ // ------------------------------------------------------------ //
7239
+ var orderAny = {};
7240
+ if (isOrderCrosschain(orderAny)) {
7241
+ console.log('🚀 ~ order:', orderAny);
7242
+ /**
7243
+ DeltaAuctionOrder | TWAPDeltaOrder | TWAPBuyDeltaOrder
7244
+ */
7245
+ }
7246
+ var orderExternal = {};
7247
+ if (isOrderCrosschain(orderExternal)) {
7248
+ console.log('🚀 ~ order:', orderExternal);
7249
+ /**
7250
+ never
7251
+ */
7252
+ }
7253
+ var orderLike = {};
7254
+ if (isOrderCrosschain(orderLike)) {
7255
+ console.log('🚀 ~ order:', orderLike);
7256
+ /**
7257
+ * {
7258
+ bridge: Bridge;
7259
+ a: 2;
7260
+ }
7261
+ */
7262
+ }
7263
+
6803
7264
  Object.defineProperty(exports, 'ContractMethod', {
6804
7265
  enumerable: true,
6805
7266
  get: function () { return core.ContractMethod; }
@@ -6819,6 +7280,7 @@ Object.defineProperty(exports, 'SwapSide', {
6819
7280
  exports.API_URL = API_URL;
6820
7281
  exports.AssetType = AssetType;
6821
7282
  exports.DEFAULT_VERSION = DEFAULT_VERSION;
7283
+ exports.OrderHelpers = OrderHelpers;
6822
7284
  exports.constructAllDeltaOrdersHandlers = constructAllDeltaOrdersHandlers;
6823
7285
  exports.constructAllLimitOrdersHandlers = constructAllLimitOrdersHandlers;
6824
7286
  exports.constructAllNFTOrdersHandlers = constructAllNFTOrdersHandlers;