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