@shapeshiftoss/swapper 17.6.10 → 17.6.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/LICENSE.md +21 -0
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/constants.js +29 -18
- package/dist/cjs/cowswap-utils/constants.js +3 -0
- package/dist/cjs/cowswap-utils/index.js +6 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/swappers/AcrossSwapper/constant.js +2 -0
- package/dist/cjs/swappers/AcrossSwapper/utils/getTrade.js +15 -1
- package/dist/cjs/swappers/AvnuSwapper/swapperApi/getTradeQuote.js +9 -0
- package/dist/cjs/swappers/AvnuSwapper/swapperApi/getTradeRate.js +9 -0
- package/dist/cjs/swappers/BebopSwapper/BebopSwapper.js +2 -0
- package/dist/cjs/swappers/BebopSwapper/endpoints.js +35 -2
- package/dist/cjs/swappers/BebopSwapper/executeSolanaMessage.d.ts +2 -0
- package/dist/cjs/swappers/BebopSwapper/executeSolanaMessage.js +47 -0
- package/dist/cjs/swappers/BebopSwapper/getBebopSolanaTradeQuote/getBebopSolanaTradeQuote.d.ts +4 -0
- package/dist/cjs/swappers/BebopSwapper/getBebopSolanaTradeQuote/getBebopSolanaTradeQuote.js +91 -0
- package/dist/cjs/swappers/BebopSwapper/getBebopSolanaTradeRate/getBebopSolanaTradeRate.d.ts +4 -0
- package/dist/cjs/swappers/BebopSwapper/getBebopSolanaTradeRate/getBebopSolanaTradeRate.js +88 -0
- package/dist/cjs/swappers/BebopSwapper/getBebopTradeQuote/getBebopTradeQuote.js +10 -0
- package/dist/cjs/swappers/BebopSwapper/getBebopTradeRate/getBebopTradeRate.js +10 -0
- package/dist/cjs/swappers/BebopSwapper/types.d.ts +49 -1
- package/dist/cjs/swappers/BebopSwapper/types.js +9 -2
- package/dist/cjs/swappers/BebopSwapper/utils/fetchFromBebop.d.ts +19 -1
- package/dist/cjs/swappers/BebopSwapper/utils/fetchFromBebop.js +86 -1
- package/dist/cjs/swappers/BebopSwapper/utils/helpers/helpers.d.ts +13 -1
- package/dist/cjs/swappers/BebopSwapper/utils/helpers/helpers.js +47 -1
- package/dist/cjs/swappers/ButterSwap/swapperApi/getTradeQuote.js +17 -1
- package/dist/cjs/swappers/ButterSwap/swapperApi/getTradeRate.js +10 -0
- package/dist/cjs/swappers/CetusSwapper/swapperApi/getTradeQuote.js +10 -0
- package/dist/cjs/swappers/CetusSwapper/swapperApi/getTradeRate.js +10 -0
- package/dist/cjs/swappers/CetusSwapper/utils/helpers.d.ts +1 -1
- package/dist/cjs/swappers/CetusSwapper/utils/helpers.js +1 -1
- package/dist/cjs/swappers/ChainflipSwapper/constants.js +7 -3
- package/dist/cjs/swappers/ChainflipSwapper/utils/getQuoteOrRate.js +17 -0
- package/dist/cjs/swappers/CowSwapper/getCowSwapTradeQuote/getCowSwapTradeQuote.js +10 -0
- package/dist/cjs/swappers/CowSwapper/getCowSwapTradeRate/getCowSwapTradeRate.js +9 -0
- package/dist/cjs/swappers/DebridgeSwapper/DebridgeSwapper.d.ts +2 -0
- package/dist/cjs/swappers/DebridgeSwapper/DebridgeSwapper.js +7 -0
- package/dist/cjs/swappers/DebridgeSwapper/constant.d.ts +5 -0
- package/dist/cjs/swappers/DebridgeSwapper/constant.js +36 -0
- package/dist/cjs/swappers/DebridgeSwapper/endpoints.d.ts +2 -0
- package/dist/cjs/swappers/DebridgeSwapper/endpoints.js +217 -0
- package/dist/cjs/swappers/DebridgeSwapper/getTradeQuote/getTradeQuote.d.ts +3 -0
- package/dist/cjs/swappers/DebridgeSwapper/getTradeQuote/getTradeQuote.js +31 -0
- package/dist/cjs/swappers/DebridgeSwapper/getTradeRate/getTradeRate.d.ts +3 -0
- package/dist/cjs/swappers/DebridgeSwapper/getTradeRate/getTradeRate.js +19 -0
- package/dist/cjs/swappers/DebridgeSwapper/index.d.ts +1 -0
- package/dist/cjs/swappers/DebridgeSwapper/index.js +5 -0
- package/dist/cjs/swappers/DebridgeSwapper/utils/debridgeService.d.ts +5 -0
- package/dist/cjs/swappers/DebridgeSwapper/utils/debridgeService.js +17 -0
- package/dist/cjs/swappers/DebridgeSwapper/utils/fetchDebridgeSingleChainTrade.d.ts +16 -0
- package/dist/cjs/swappers/DebridgeSwapper/utils/fetchDebridgeSingleChainTrade.js +23 -0
- package/dist/cjs/swappers/DebridgeSwapper/utils/fetchDebridgeTrade.d.ts +20 -0
- package/dist/cjs/swappers/DebridgeSwapper/utils/fetchDebridgeTrade.js +29 -0
- package/dist/cjs/swappers/DebridgeSwapper/utils/getTrade.d.ts +11 -0
- package/dist/cjs/swappers/DebridgeSwapper/utils/getTrade.js +369 -0
- package/dist/cjs/swappers/DebridgeSwapper/utils/types.d.ts +155 -0
- package/dist/cjs/swappers/DebridgeSwapper/utils/types.js +9 -0
- package/dist/cjs/swappers/JupiterSwapper/swapperApi/getTradeQuote.js +32 -0
- package/dist/cjs/swappers/JupiterSwapper/swapperApi/getTradeRate.js +9 -0
- package/dist/cjs/swappers/MayachainSwapper/utils/poolAssetHelpers/poolAssetHelpers.js +1 -1
- package/dist/cjs/swappers/NearIntentsSwapper/swapperApi/getTradeQuote.js +12 -0
- package/dist/cjs/swappers/NearIntentsSwapper/swapperApi/getTradeRate.js +12 -0
- package/dist/cjs/swappers/PortalsSwapper/getPortalsTradeQuote/getPortalsTradeQuote.js +10 -0
- package/dist/cjs/swappers/RelaySwapper/constant.js +4 -3
- package/dist/cjs/swappers/RelaySwapper/utils/getTrade.js +44 -5
- package/dist/cjs/swappers/RelaySwapper/utils/relayTokenToAssetId.js +17 -7
- package/dist/cjs/swappers/StonfiSwapper/swapperApi/getTradeQuote.js +10 -0
- package/dist/cjs/swappers/StonfiSwapper/swapperApi/getTradeRate.js +10 -0
- package/dist/cjs/swappers/SunioSwapper/utils/getQuoteOrRate.js +10 -0
- package/dist/cjs/swappers/ThorchainSwapper/ThorchainSwapper.js +3 -0
- package/dist/cjs/swappers/ThorchainSwapper/endpoints.js +2 -0
- package/dist/cjs/swappers/ThorchainSwapper/generated/generatedTradableAssetMap.json +1 -0
- package/dist/cjs/swappers/ThorchainSwapper/utils/poolAssetHelpers/poolAssetHelpers.d.ts +1 -1
- package/dist/cjs/swappers/ThorchainSwapper/utils/poolAssetHelpers/poolAssetHelpers.js +2 -1
- package/dist/cjs/swappers/ThorchainSwapper/utils/poolAssetHelpers/poolAssetHelpers.test.js +10 -0
- package/dist/cjs/swappers/ZrxSwapper/getZrxTradeQuote/getZrxTradeQuote.js +9 -0
- package/dist/cjs/swappers/ZrxSwapper/getZrxTradeRate/getZrxTradeRate.js +9 -0
- package/dist/cjs/swappers/utils/affiliateFee.d.ts +17 -0
- package/dist/cjs/swappers/utils/affiliateFee.js +43 -0
- package/dist/cjs/thorchain-utils/getL1RateOrQuote.js +57 -4
- package/dist/cjs/thorchain-utils/index.d.ts +2 -1
- package/dist/cjs/thorchain-utils/index.js +2 -1
- package/dist/cjs/thorchain-utils/solana/constants.d.ts +1 -0
- package/dist/cjs/thorchain-utils/solana/constants.js +6 -0
- package/dist/cjs/thorchain-utils/solana/getSolanaTransactionFees.d.ts +2 -0
- package/dist/cjs/thorchain-utils/solana/getSolanaTransactionFees.js +41 -0
- package/dist/cjs/thorchain-utils/solana/getThorTxData.d.ts +12 -0
- package/dist/cjs/thorchain-utils/solana/getThorTxData.js +14 -0
- package/dist/cjs/thorchain-utils/solana/getUnsignedSolanaTransaction.d.ts +3 -0
- package/dist/cjs/thorchain-utils/solana/getUnsignedSolanaTransaction.js +64 -0
- package/dist/cjs/thorchain-utils/solana/index.d.ts +3 -0
- package/dist/cjs/thorchain-utils/solana/index.js +19 -0
- package/dist/cjs/types.d.ts +44 -5
- package/dist/cjs/types.js +1 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/constants.js +11 -0
- package/dist/esm/cowswap-utils/constants.js +3 -0
- package/dist/esm/cowswap-utils/index.js +6 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/swappers/AcrossSwapper/constant.js +4 -2
- package/dist/esm/swappers/AcrossSwapper/utils/getTrade.js +15 -1
- package/dist/esm/swappers/AvnuSwapper/swapperApi/getTradeQuote.js +9 -0
- package/dist/esm/swappers/AvnuSwapper/swapperApi/getTradeRate.js +9 -0
- package/dist/esm/swappers/BebopSwapper/BebopSwapper.js +2 -0
- package/dist/esm/swappers/BebopSwapper/endpoints.js +36 -3
- package/dist/esm/swappers/BebopSwapper/executeSolanaMessage.d.ts +2 -0
- package/dist/esm/swappers/BebopSwapper/executeSolanaMessage.js +40 -0
- package/dist/esm/swappers/BebopSwapper/getBebopSolanaTradeQuote/getBebopSolanaTradeQuote.d.ts +4 -0
- package/dist/esm/swappers/BebopSwapper/getBebopSolanaTradeQuote/getBebopSolanaTradeQuote.js +87 -0
- package/dist/esm/swappers/BebopSwapper/getBebopSolanaTradeRate/getBebopSolanaTradeRate.d.ts +4 -0
- package/dist/esm/swappers/BebopSwapper/getBebopSolanaTradeRate/getBebopSolanaTradeRate.js +84 -0
- package/dist/esm/swappers/BebopSwapper/getBebopTradeQuote/getBebopTradeQuote.js +10 -0
- package/dist/esm/swappers/BebopSwapper/getBebopTradeRate/getBebopTradeRate.js +10 -0
- package/dist/esm/swappers/BebopSwapper/types.d.ts +49 -1
- package/dist/esm/swappers/BebopSwapper/types.js +8 -1
- package/dist/esm/swappers/BebopSwapper/utils/fetchFromBebop.d.ts +19 -1
- package/dist/esm/swappers/BebopSwapper/utils/fetchFromBebop.js +85 -2
- package/dist/esm/swappers/BebopSwapper/utils/helpers/helpers.d.ts +13 -1
- package/dist/esm/swappers/BebopSwapper/utils/helpers/helpers.js +44 -1
- package/dist/esm/swappers/ButterSwap/swapperApi/getTradeQuote.js +17 -1
- package/dist/esm/swappers/ButterSwap/swapperApi/getTradeRate.js +10 -0
- package/dist/esm/swappers/CetusSwapper/swapperApi/getTradeQuote.js +10 -0
- package/dist/esm/swappers/CetusSwapper/swapperApi/getTradeRate.js +10 -0
- package/dist/esm/swappers/CetusSwapper/utils/helpers.d.ts +1 -1
- package/dist/esm/swappers/CetusSwapper/utils/helpers.js +1 -1
- package/dist/esm/swappers/ChainflipSwapper/constants.js +8 -4
- package/dist/esm/swappers/ChainflipSwapper/utils/getQuoteOrRate.js +17 -0
- package/dist/esm/swappers/CowSwapper/getCowSwapTradeQuote/getCowSwapTradeQuote.js +10 -0
- package/dist/esm/swappers/CowSwapper/getCowSwapTradeRate/getCowSwapTradeRate.js +9 -0
- package/dist/esm/swappers/DebridgeSwapper/DebridgeSwapper.d.ts +2 -0
- package/dist/esm/swappers/DebridgeSwapper/DebridgeSwapper.js +4 -0
- package/dist/esm/swappers/DebridgeSwapper/constant.d.ts +5 -0
- package/dist/esm/swappers/DebridgeSwapper/constant.js +30 -0
- package/dist/esm/swappers/DebridgeSwapper/endpoints.d.ts +2 -0
- package/dist/esm/swappers/DebridgeSwapper/endpoints.js +211 -0
- package/dist/esm/swappers/DebridgeSwapper/getTradeQuote/getTradeQuote.d.ts +3 -0
- package/dist/esm/swappers/DebridgeSwapper/getTradeQuote/getTradeQuote.js +27 -0
- package/dist/esm/swappers/DebridgeSwapper/getTradeRate/getTradeRate.d.ts +3 -0
- package/dist/esm/swappers/DebridgeSwapper/getTradeRate/getTradeRate.js +15 -0
- package/dist/esm/swappers/DebridgeSwapper/index.d.ts +1 -0
- package/dist/esm/swappers/DebridgeSwapper/index.js +1 -0
- package/dist/esm/swappers/DebridgeSwapper/utils/debridgeService.d.ts +5 -0
- package/dist/esm/swappers/DebridgeSwapper/utils/debridgeService.js +11 -0
- package/dist/esm/swappers/DebridgeSwapper/utils/fetchDebridgeSingleChainTrade.d.ts +16 -0
- package/dist/esm/swappers/DebridgeSwapper/utils/fetchDebridgeSingleChainTrade.js +19 -0
- package/dist/esm/swappers/DebridgeSwapper/utils/fetchDebridgeTrade.d.ts +20 -0
- package/dist/esm/swappers/DebridgeSwapper/utils/fetchDebridgeTrade.js +25 -0
- package/dist/esm/swappers/DebridgeSwapper/utils/getTrade.d.ts +11 -0
- package/dist/esm/swappers/DebridgeSwapper/utils/getTrade.js +362 -0
- package/dist/esm/swappers/DebridgeSwapper/utils/types.d.ts +155 -0
- package/dist/esm/swappers/DebridgeSwapper/utils/types.js +5 -0
- package/dist/esm/swappers/JupiterSwapper/swapperApi/getTradeQuote.js +33 -1
- package/dist/esm/swappers/JupiterSwapper/swapperApi/getTradeRate.js +9 -0
- package/dist/esm/swappers/MayachainSwapper/utils/poolAssetHelpers/poolAssetHelpers.js +1 -1
- package/dist/esm/swappers/NearIntentsSwapper/swapperApi/getTradeQuote.js +14 -2
- package/dist/esm/swappers/NearIntentsSwapper/swapperApi/getTradeRate.js +14 -2
- package/dist/esm/swappers/PortalsSwapper/getPortalsTradeQuote/getPortalsTradeQuote.js +10 -0
- package/dist/esm/swappers/RelaySwapper/constant.js +6 -5
- package/dist/esm/swappers/RelaySwapper/utils/getTrade.js +47 -8
- package/dist/esm/swappers/RelaySwapper/utils/relayTokenToAssetId.js +17 -7
- package/dist/esm/swappers/StonfiSwapper/swapperApi/getTradeQuote.js +10 -0
- package/dist/esm/swappers/StonfiSwapper/swapperApi/getTradeRate.js +10 -0
- package/dist/esm/swappers/SunioSwapper/utils/getQuoteOrRate.js +10 -0
- package/dist/esm/swappers/ThorchainSwapper/ThorchainSwapper.js +3 -0
- package/dist/esm/swappers/ThorchainSwapper/endpoints.js +3 -1
- package/dist/esm/swappers/ThorchainSwapper/generated/generatedTradableAssetMap.json +1 -0
- package/dist/esm/swappers/ThorchainSwapper/utils/poolAssetHelpers/poolAssetHelpers.d.ts +1 -1
- package/dist/esm/swappers/ThorchainSwapper/utils/poolAssetHelpers/poolAssetHelpers.js +2 -1
- package/dist/esm/swappers/ThorchainSwapper/utils/poolAssetHelpers/poolAssetHelpers.test.js +11 -1
- package/dist/esm/swappers/ZrxSwapper/getZrxTradeQuote/getZrxTradeQuote.js +9 -0
- package/dist/esm/swappers/ZrxSwapper/getZrxTradeRate/getZrxTradeRate.js +9 -0
- package/dist/esm/swappers/utils/affiliateFee.d.ts +17 -0
- package/dist/esm/swappers/utils/affiliateFee.js +39 -0
- package/dist/esm/thorchain-utils/getL1RateOrQuote.js +57 -4
- package/dist/esm/thorchain-utils/index.d.ts +2 -1
- package/dist/esm/thorchain-utils/index.js +1 -0
- package/dist/esm/thorchain-utils/solana/constants.d.ts +1 -0
- package/dist/esm/thorchain-utils/solana/constants.js +3 -0
- package/dist/esm/thorchain-utils/solana/getSolanaTransactionFees.d.ts +2 -0
- package/dist/esm/thorchain-utils/solana/getSolanaTransactionFees.js +37 -0
- package/dist/esm/thorchain-utils/solana/getThorTxData.d.ts +12 -0
- package/dist/esm/thorchain-utils/solana/getThorTxData.js +10 -0
- package/dist/esm/thorchain-utils/solana/getUnsignedSolanaTransaction.d.ts +3 -0
- package/dist/esm/thorchain-utils/solana/getUnsignedSolanaTransaction.js +60 -0
- package/dist/esm/thorchain-utils/solana/index.d.ts +3 -0
- package/dist/esm/thorchain-utils/solana/index.js +3 -0
- package/dist/esm/types.d.ts +44 -5
- package/dist/esm/types.js +1 -0
- package/package.json +19 -18
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { fromAssetId, isAssetReference } from '@shapeshiftoss/caip';
|
|
2
|
+
import { evm, isEvmChainId } from '@shapeshiftoss/chain-adapters';
|
|
3
|
+
import { BigAmount, bnOrZero, chainIdToFeeAssetId, getBaseAsset, isTreasuryChainId, } from '@shapeshiftoss/utils';
|
|
4
|
+
import { Err, Ok } from '@sniptt/monads';
|
|
5
|
+
import axios from 'axios';
|
|
6
|
+
import { v4 as uuid } from 'uuid';
|
|
7
|
+
import { zeroAddress } from 'viem';
|
|
8
|
+
import { SwapperName, TradeQuoteError } from '../../../types.js';
|
|
9
|
+
import { getInputOutputRate, makeSwapErrorRight } from '../../../utils.js';
|
|
10
|
+
import { getTreasuryAddressFromChainId, isNativeEvmAsset } from '../../utils/helpers/helpers.js';
|
|
11
|
+
import { chainIdToDebridgeChainId, debridgeChainIdToChainId, DEFAULT_DEBRIDGE_TOKEN_ADDRESS, DEFAULT_DEBRIDGE_USER_ADDRESS, } from '../constant.js';
|
|
12
|
+
import { fetchDebridgeSingleChainTrade } from './fetchDebridgeSingleChainTrade.js';
|
|
13
|
+
import { fetchDebridgeTrade } from './fetchDebridgeTrade.js';
|
|
14
|
+
import { isDebridgeError } from './types.js';
|
|
15
|
+
const getDebridgeAssetAddress = (assetId) => {
|
|
16
|
+
if (isNativeEvmAsset(assetId))
|
|
17
|
+
return DEFAULT_DEBRIDGE_TOKEN_ADDRESS;
|
|
18
|
+
const { assetReference } = fromAssetId(assetId);
|
|
19
|
+
return isAssetReference(assetReference) ? zeroAddress : assetReference;
|
|
20
|
+
};
|
|
21
|
+
export async function getTrade({ input, deps, }) {
|
|
22
|
+
const { sellAsset, buyAsset, sellAmountIncludingProtocolFeesCryptoBaseUnit, affiliateBps } = input;
|
|
23
|
+
const isSameChainSwap = sellAsset.chainId === buyAsset.chainId;
|
|
24
|
+
const sellDebridgeChainId = chainIdToDebridgeChainId[sellAsset.chainId];
|
|
25
|
+
if (sellDebridgeChainId === undefined) {
|
|
26
|
+
return Err(makeSwapErrorRight({
|
|
27
|
+
message: `Sell asset chain '${sellAsset.chainId}' not supported by deBridge`,
|
|
28
|
+
code: TradeQuoteError.UnsupportedChain,
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
if (!isSameChainSwap) {
|
|
32
|
+
const buyDebridgeChainId = chainIdToDebridgeChainId[buyAsset.chainId];
|
|
33
|
+
if (buyDebridgeChainId === undefined) {
|
|
34
|
+
return Err(makeSwapErrorRight({
|
|
35
|
+
message: `Buy asset chain '${buyAsset.chainId}' not supported by deBridge`,
|
|
36
|
+
code: TradeQuoteError.UnsupportedChain,
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const senderAddress = (() => {
|
|
41
|
+
if (input.quoteOrRate === 'rate') {
|
|
42
|
+
if (input.sendAddress)
|
|
43
|
+
return input.sendAddress;
|
|
44
|
+
return DEFAULT_DEBRIDGE_USER_ADDRESS;
|
|
45
|
+
}
|
|
46
|
+
return input.sendAddress ?? DEFAULT_DEBRIDGE_USER_ADDRESS;
|
|
47
|
+
})();
|
|
48
|
+
const recipientAddress = (() => {
|
|
49
|
+
if (input.quoteOrRate === 'rate') {
|
|
50
|
+
if (input.receiveAddress)
|
|
51
|
+
return input.receiveAddress;
|
|
52
|
+
return DEFAULT_DEBRIDGE_USER_ADDRESS;
|
|
53
|
+
}
|
|
54
|
+
return input.receiveAddress;
|
|
55
|
+
})();
|
|
56
|
+
const affiliateFeePercent = (() => {
|
|
57
|
+
if (!isTreasuryChainId(buyAsset.chainId))
|
|
58
|
+
return undefined;
|
|
59
|
+
const bps = bnOrZero(affiliateBps);
|
|
60
|
+
if (!bps.isFinite() || bps.lte(0))
|
|
61
|
+
return undefined;
|
|
62
|
+
return bps.div(100).toFixed();
|
|
63
|
+
})();
|
|
64
|
+
const affiliateFeeRecipient = (() => {
|
|
65
|
+
if (affiliateFeePercent === undefined)
|
|
66
|
+
return undefined;
|
|
67
|
+
try {
|
|
68
|
+
return getTreasuryAddressFromChainId(buyAsset.chainId).toLowerCase();
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
console.error(`[getTrade] Failed to get treasury address for chainId ${buyAsset.chainId}, affiliate fee will not be applied`, e);
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
})();
|
|
75
|
+
if (isSameChainSwap) {
|
|
76
|
+
return getSameChainTrade({
|
|
77
|
+
input,
|
|
78
|
+
deps,
|
|
79
|
+
sellDebridgeChainId,
|
|
80
|
+
senderAddress,
|
|
81
|
+
recipientAddress,
|
|
82
|
+
affiliateFeePercent,
|
|
83
|
+
affiliateFeeRecipient,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const buyDebridgeChainId = chainIdToDebridgeChainId[buyAsset.chainId];
|
|
87
|
+
if (buyDebridgeChainId === undefined) {
|
|
88
|
+
return Err(makeSwapErrorRight({
|
|
89
|
+
message: `Buy asset chain '${buyAsset.chainId}' not supported by deBridge`,
|
|
90
|
+
code: TradeQuoteError.UnsupportedChain,
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
const maybeQuote = await fetchDebridgeTrade({
|
|
94
|
+
srcChainId: sellDebridgeChainId,
|
|
95
|
+
srcChainTokenIn: getDebridgeAssetAddress(sellAsset.assetId),
|
|
96
|
+
srcChainTokenInAmount: sellAmountIncludingProtocolFeesCryptoBaseUnit,
|
|
97
|
+
dstChainId: buyDebridgeChainId,
|
|
98
|
+
dstChainTokenOut: getDebridgeAssetAddress(buyAsset.assetId),
|
|
99
|
+
dstChainTokenOutAmount: 'auto',
|
|
100
|
+
dstChainTokenOutRecipient: recipientAddress,
|
|
101
|
+
srcChainOrderAuthorityAddress: senderAddress,
|
|
102
|
+
dstChainOrderAuthorityAddress: recipientAddress,
|
|
103
|
+
senderAddress,
|
|
104
|
+
prependOperatingExpenses: 'true',
|
|
105
|
+
affiliateFeePercent,
|
|
106
|
+
affiliateFeeRecipient,
|
|
107
|
+
}, deps.config);
|
|
108
|
+
if (maybeQuote.isErr()) {
|
|
109
|
+
return handleDebridgeError(maybeQuote.unwrapErr());
|
|
110
|
+
}
|
|
111
|
+
const { data: quote } = maybeQuote.unwrap();
|
|
112
|
+
const buyAmountAfterFeesCryptoBaseUnit = quote.estimation.dstChainTokenOut.recommendedAmount;
|
|
113
|
+
const rate = getInputOutputRate({
|
|
114
|
+
sellAmountCryptoBaseUnit: sellAmountIncludingProtocolFeesCryptoBaseUnit,
|
|
115
|
+
buyAmountCryptoBaseUnit: buyAmountAfterFeesCryptoBaseUnit,
|
|
116
|
+
sellAsset,
|
|
117
|
+
buyAsset,
|
|
118
|
+
});
|
|
119
|
+
const nativePreFee = bnOrZero(quote.fixFee)
|
|
120
|
+
.plus(bnOrZero(quote.prependedOperatingExpenseCost))
|
|
121
|
+
.toFixed();
|
|
122
|
+
const buyAmountBeforeFeesCryptoBaseUnit = buyAmountAfterFeesCryptoBaseUnit;
|
|
123
|
+
const allowanceContract = isEvmChainId(sellAsset.chainId) ? quote.tx.to : '';
|
|
124
|
+
const debridgeTransactionMetadata = {
|
|
125
|
+
to: quote.tx.to,
|
|
126
|
+
data: quote.tx.data,
|
|
127
|
+
value: quote.tx.value,
|
|
128
|
+
orderId: quote.orderId,
|
|
129
|
+
gasLimit: quote.estimatedTransactionFee?.details.gasLimit,
|
|
130
|
+
};
|
|
131
|
+
const networkFeeCryptoBaseUnit = await getNetworkFee({
|
|
132
|
+
sellAsset,
|
|
133
|
+
debridgeTransactionMetadata,
|
|
134
|
+
senderAddress,
|
|
135
|
+
deps,
|
|
136
|
+
});
|
|
137
|
+
const protocolFeeAssetCaipChainId = debridgeChainIdToChainId[sellDebridgeChainId.toString()];
|
|
138
|
+
const protocolFeeAssetIdForFees = (() => {
|
|
139
|
+
if (!protocolFeeAssetCaipChainId)
|
|
140
|
+
return undefined;
|
|
141
|
+
return chainIdToFeeAssetId(protocolFeeAssetCaipChainId);
|
|
142
|
+
})();
|
|
143
|
+
const accountNumber = input.accountNumber;
|
|
144
|
+
const step = {
|
|
145
|
+
allowanceContract,
|
|
146
|
+
rate,
|
|
147
|
+
buyAmountBeforeFeesCryptoBaseUnit,
|
|
148
|
+
buyAmountAfterFeesCryptoBaseUnit,
|
|
149
|
+
sellAmountIncludingProtocolFeesCryptoBaseUnit,
|
|
150
|
+
buyAsset,
|
|
151
|
+
sellAsset,
|
|
152
|
+
accountNumber,
|
|
153
|
+
feeData: {
|
|
154
|
+
networkFeeCryptoBaseUnit,
|
|
155
|
+
protocolFees: protocolFeeAssetIdForFees && bnOrZero(nativePreFee).gt(0)
|
|
156
|
+
? {
|
|
157
|
+
[protocolFeeAssetIdForFees]: {
|
|
158
|
+
amountCryptoBaseUnit: nativePreFee,
|
|
159
|
+
asset: getBaseAsset(sellAsset.chainId),
|
|
160
|
+
requiresBalance: true,
|
|
161
|
+
},
|
|
162
|
+
}
|
|
163
|
+
: {},
|
|
164
|
+
},
|
|
165
|
+
source: SwapperName.Debridge,
|
|
166
|
+
estimatedExecutionTimeMs: quote.order.approximateFulfillmentDelay * 1000,
|
|
167
|
+
debridgeTransactionMetadata,
|
|
168
|
+
};
|
|
169
|
+
const baseQuoteOrRate = {
|
|
170
|
+
id: quote.orderId,
|
|
171
|
+
rate,
|
|
172
|
+
swapperName: SwapperName.Debridge,
|
|
173
|
+
affiliateBps,
|
|
174
|
+
slippageTolerancePercentageDecimal: input.slippageTolerancePercentageDecimal,
|
|
175
|
+
};
|
|
176
|
+
if (input.quoteOrRate === 'quote') {
|
|
177
|
+
if (!input.receiveAddress) {
|
|
178
|
+
return Err(makeSwapErrorRight({
|
|
179
|
+
message: 'Receive address is required for quote',
|
|
180
|
+
code: TradeQuoteError.InternalError,
|
|
181
|
+
}));
|
|
182
|
+
}
|
|
183
|
+
const tradeQuote = {
|
|
184
|
+
...baseQuoteOrRate,
|
|
185
|
+
steps: [step],
|
|
186
|
+
receiveAddress: input.receiveAddress,
|
|
187
|
+
quoteOrRate: 'quote',
|
|
188
|
+
};
|
|
189
|
+
return Ok([tradeQuote]);
|
|
190
|
+
}
|
|
191
|
+
const tradeRate = {
|
|
192
|
+
...baseQuoteOrRate,
|
|
193
|
+
steps: [step],
|
|
194
|
+
receiveAddress: recipientAddress,
|
|
195
|
+
quoteOrRate: 'rate',
|
|
196
|
+
};
|
|
197
|
+
return Ok([tradeRate]);
|
|
198
|
+
}
|
|
199
|
+
function handleDebridgeError(error) {
|
|
200
|
+
if (!axios.isAxiosError(error.cause)) {
|
|
201
|
+
return Err(makeSwapErrorRight({
|
|
202
|
+
message: 'Unknown error',
|
|
203
|
+
code: TradeQuoteError.UnknownError,
|
|
204
|
+
}));
|
|
205
|
+
}
|
|
206
|
+
const debridgeError = error.cause?.response?.data;
|
|
207
|
+
if (!isDebridgeError(debridgeError)) {
|
|
208
|
+
return Err(makeSwapErrorRight({
|
|
209
|
+
message: 'Unknown error',
|
|
210
|
+
code: TradeQuoteError.UnknownError,
|
|
211
|
+
}));
|
|
212
|
+
}
|
|
213
|
+
const errorMessage = debridgeError.errorMessage ?? debridgeError.message ?? 'Unknown error';
|
|
214
|
+
return Err(makeSwapErrorRight({
|
|
215
|
+
message: errorMessage,
|
|
216
|
+
code: TradeQuoteError.UnknownError,
|
|
217
|
+
}));
|
|
218
|
+
}
|
|
219
|
+
async function getNetworkFee({ sellAsset, debridgeTransactionMetadata, senderAddress, deps, }) {
|
|
220
|
+
if (!isEvmChainId(sellAsset.chainId))
|
|
221
|
+
return undefined;
|
|
222
|
+
const adapter = deps.assertGetEvmChainAdapter(sellAsset.chainId);
|
|
223
|
+
const { average } = await adapter.getGasFeeData();
|
|
224
|
+
const supportsEIP1559 = 'maxFeePerGas' in average;
|
|
225
|
+
try {
|
|
226
|
+
const feeData = await evm.getFees({
|
|
227
|
+
adapter,
|
|
228
|
+
data: debridgeTransactionMetadata.data,
|
|
229
|
+
to: debridgeTransactionMetadata.to,
|
|
230
|
+
value: debridgeTransactionMetadata.value,
|
|
231
|
+
from: senderAddress,
|
|
232
|
+
supportsEIP1559,
|
|
233
|
+
});
|
|
234
|
+
return feeData.networkFeeCryptoBaseUnit;
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
const gasLimitFromApi = debridgeTransactionMetadata.gasLimit;
|
|
238
|
+
if (!gasLimitFromApi)
|
|
239
|
+
return undefined;
|
|
240
|
+
return evm.calcNetworkFeeCryptoBaseUnit({
|
|
241
|
+
...average,
|
|
242
|
+
gasLimit: gasLimitFromApi,
|
|
243
|
+
supportsEIP1559,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
async function getSameChainTrade({ input, deps, sellDebridgeChainId, senderAddress, recipientAddress, affiliateFeePercent, affiliateFeeRecipient, }) {
|
|
248
|
+
const { sellAsset, buyAsset, sellAmountIncludingProtocolFeesCryptoBaseUnit, affiliateBps } = input;
|
|
249
|
+
const slippage = input.slippageTolerancePercentageDecimal
|
|
250
|
+
? bnOrZero(input.slippageTolerancePercentageDecimal).times(100).toFixed()
|
|
251
|
+
: '5';
|
|
252
|
+
const maybeQuote = await fetchDebridgeSingleChainTrade({
|
|
253
|
+
chainId: sellDebridgeChainId,
|
|
254
|
+
tokenIn: getDebridgeAssetAddress(sellAsset.assetId),
|
|
255
|
+
tokenInAmount: sellAmountIncludingProtocolFeesCryptoBaseUnit,
|
|
256
|
+
tokenOut: getDebridgeAssetAddress(buyAsset.assetId),
|
|
257
|
+
tokenOutRecipient: recipientAddress,
|
|
258
|
+
senderAddress,
|
|
259
|
+
slippage,
|
|
260
|
+
affiliateFeePercent,
|
|
261
|
+
affiliateFeeRecipient,
|
|
262
|
+
}, deps.config);
|
|
263
|
+
if (maybeQuote.isErr()) {
|
|
264
|
+
return handleDebridgeError(maybeQuote.unwrapErr());
|
|
265
|
+
}
|
|
266
|
+
const { data: quote } = maybeQuote.unwrap();
|
|
267
|
+
const buyAmountAfterFeesCryptoBaseUnit = quote.tokenOut.amount;
|
|
268
|
+
const rate = getInputOutputRate({
|
|
269
|
+
sellAmountCryptoBaseUnit: sellAmountIncludingProtocolFeesCryptoBaseUnit,
|
|
270
|
+
buyAmountCryptoBaseUnit: buyAmountAfterFeesCryptoBaseUnit,
|
|
271
|
+
sellAsset,
|
|
272
|
+
buyAsset,
|
|
273
|
+
});
|
|
274
|
+
const protocolFeeAmount = quote.protocolFee ?? '0';
|
|
275
|
+
const buyAmountBeforeFeesCryptoBaseUnit = (() => {
|
|
276
|
+
if (protocolFeeAmount === '0')
|
|
277
|
+
return buyAmountAfterFeesCryptoBaseUnit;
|
|
278
|
+
return BigAmount.fromBaseUnit({
|
|
279
|
+
value: buyAmountAfterFeesCryptoBaseUnit,
|
|
280
|
+
precision: buyAsset.precision,
|
|
281
|
+
})
|
|
282
|
+
.plus(BigAmount.fromBaseUnit({
|
|
283
|
+
value: protocolFeeAmount,
|
|
284
|
+
precision: buyAsset.precision,
|
|
285
|
+
}))
|
|
286
|
+
.toBaseUnit();
|
|
287
|
+
})();
|
|
288
|
+
const allowanceContract = isEvmChainId(sellAsset.chainId) ? quote.tx.to : '';
|
|
289
|
+
const debridgeTransactionMetadata = {
|
|
290
|
+
to: quote.tx.to,
|
|
291
|
+
data: quote.tx.data,
|
|
292
|
+
value: quote.tx.value,
|
|
293
|
+
gasLimit: quote.estimatedTransactionFee?.details.gasLimit,
|
|
294
|
+
isSameChainSwap: true,
|
|
295
|
+
};
|
|
296
|
+
const networkFeeCryptoBaseUnit = await getNetworkFee({
|
|
297
|
+
sellAsset,
|
|
298
|
+
debridgeTransactionMetadata,
|
|
299
|
+
senderAddress,
|
|
300
|
+
deps,
|
|
301
|
+
});
|
|
302
|
+
const tradeId = uuid();
|
|
303
|
+
const step = {
|
|
304
|
+
allowanceContract,
|
|
305
|
+
rate,
|
|
306
|
+
buyAmountBeforeFeesCryptoBaseUnit,
|
|
307
|
+
buyAmountAfterFeesCryptoBaseUnit,
|
|
308
|
+
sellAmountIncludingProtocolFeesCryptoBaseUnit,
|
|
309
|
+
buyAsset,
|
|
310
|
+
sellAsset,
|
|
311
|
+
accountNumber: input.accountNumber,
|
|
312
|
+
feeData: {
|
|
313
|
+
networkFeeCryptoBaseUnit,
|
|
314
|
+
protocolFees: protocolFeeAmount !== '0'
|
|
315
|
+
? {
|
|
316
|
+
[buyAsset.assetId]: {
|
|
317
|
+
amountCryptoBaseUnit: protocolFeeAmount,
|
|
318
|
+
asset: {
|
|
319
|
+
symbol: quote.tokenOut.symbol,
|
|
320
|
+
chainId: buyAsset.chainId,
|
|
321
|
+
precision: buyAsset.precision,
|
|
322
|
+
},
|
|
323
|
+
requiresBalance: false,
|
|
324
|
+
},
|
|
325
|
+
}
|
|
326
|
+
: {},
|
|
327
|
+
},
|
|
328
|
+
source: SwapperName.Debridge,
|
|
329
|
+
estimatedExecutionTimeMs: 15_000,
|
|
330
|
+
debridgeTransactionMetadata,
|
|
331
|
+
};
|
|
332
|
+
const baseQuoteOrRate = {
|
|
333
|
+
id: tradeId,
|
|
334
|
+
rate,
|
|
335
|
+
swapperName: SwapperName.Debridge,
|
|
336
|
+
affiliateBps,
|
|
337
|
+
slippageTolerancePercentageDecimal: input.slippageTolerancePercentageDecimal ??
|
|
338
|
+
(quote.slippage ? bnOrZero(quote.slippage).div(100).toString() : undefined),
|
|
339
|
+
};
|
|
340
|
+
if (input.quoteOrRate === 'quote') {
|
|
341
|
+
if (!input.receiveAddress) {
|
|
342
|
+
return Err(makeSwapErrorRight({
|
|
343
|
+
message: 'Receive address is required for quote',
|
|
344
|
+
code: TradeQuoteError.InternalError,
|
|
345
|
+
}));
|
|
346
|
+
}
|
|
347
|
+
const tradeQuote = {
|
|
348
|
+
...baseQuoteOrRate,
|
|
349
|
+
steps: [step],
|
|
350
|
+
receiveAddress: input.receiveAddress,
|
|
351
|
+
quoteOrRate: 'quote',
|
|
352
|
+
};
|
|
353
|
+
return Ok([tradeQuote]);
|
|
354
|
+
}
|
|
355
|
+
const tradeRate = {
|
|
356
|
+
...baseQuoteOrRate,
|
|
357
|
+
steps: [step],
|
|
358
|
+
receiveAddress: recipientAddress,
|
|
359
|
+
quoteOrRate: 'rate',
|
|
360
|
+
};
|
|
361
|
+
return Ok([tradeRate]);
|
|
362
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { Asset } from '@shapeshiftoss/types';
|
|
2
|
+
export type DebridgeTradeBaseParams = {
|
|
3
|
+
buyAsset: Asset;
|
|
4
|
+
sellAsset: Asset;
|
|
5
|
+
sellAmountIncludingProtocolFeesCryptoBaseUnit: string;
|
|
6
|
+
affiliateBps: string;
|
|
7
|
+
};
|
|
8
|
+
export type DebridgeTradeInputParams<T extends 'rate' | 'quote'> = DebridgeTradeBaseParams & {
|
|
9
|
+
quoteOrRate: T;
|
|
10
|
+
receiveAddress: T extends 'rate' ? string | undefined : string;
|
|
11
|
+
sendAddress: T extends 'rate' ? undefined : string;
|
|
12
|
+
accountNumber: T extends 'rate' ? undefined : number;
|
|
13
|
+
slippageTolerancePercentageDecimal?: string;
|
|
14
|
+
};
|
|
15
|
+
export type DebridgeTransactionMetadata = {
|
|
16
|
+
to: string;
|
|
17
|
+
data: string;
|
|
18
|
+
value: string;
|
|
19
|
+
gasLimit?: string;
|
|
20
|
+
orderId?: string;
|
|
21
|
+
isSameChainSwap?: boolean;
|
|
22
|
+
};
|
|
23
|
+
export type DebridgeTokenInfo = {
|
|
24
|
+
address: string;
|
|
25
|
+
chainId: number;
|
|
26
|
+
decimals: number;
|
|
27
|
+
name: string;
|
|
28
|
+
symbol: string;
|
|
29
|
+
amount: string;
|
|
30
|
+
approximateOperatingExpense?: string;
|
|
31
|
+
mutatedWithOperatingExpense?: boolean;
|
|
32
|
+
approximateUsdValue?: number;
|
|
33
|
+
maxRefundAmount?: string;
|
|
34
|
+
};
|
|
35
|
+
export type DebridgeCostDetail = {
|
|
36
|
+
type: 'DlnProtocolFee' | 'TakerMargin' | 'EstimatedOperatingExpenses' | 'InputTokenFee' | 'OutputTokenFee';
|
|
37
|
+
payload: {
|
|
38
|
+
feeAmount: string;
|
|
39
|
+
feeBps?: string;
|
|
40
|
+
chain?: string;
|
|
41
|
+
tokenAddress?: string;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
export type DebridgeEstimation = {
|
|
45
|
+
srcChainTokenIn: DebridgeTokenInfo;
|
|
46
|
+
srcChainTokenOut?: DebridgeTokenInfo;
|
|
47
|
+
dstChainTokenOut: DebridgeTokenInfo & {
|
|
48
|
+
recommendedAmount: string;
|
|
49
|
+
};
|
|
50
|
+
costsDetails: DebridgeCostDetail[];
|
|
51
|
+
recommendedSlippage: number;
|
|
52
|
+
};
|
|
53
|
+
export type DebridgeTx = {
|
|
54
|
+
to: string;
|
|
55
|
+
data: string;
|
|
56
|
+
value: string;
|
|
57
|
+
};
|
|
58
|
+
export type DebridgeOrder = {
|
|
59
|
+
approximateFulfillmentDelay: number;
|
|
60
|
+
salt: number;
|
|
61
|
+
metadata: string;
|
|
62
|
+
};
|
|
63
|
+
export type DebridgeCreateTxResponse = {
|
|
64
|
+
estimation: DebridgeEstimation;
|
|
65
|
+
tx: DebridgeTx;
|
|
66
|
+
orderId: string;
|
|
67
|
+
order: DebridgeOrder;
|
|
68
|
+
fixFee: string;
|
|
69
|
+
prependedOperatingExpenseCost?: string;
|
|
70
|
+
estimatedTransactionFee?: DebridgeSingleChainEstimatedTransactionFee;
|
|
71
|
+
};
|
|
72
|
+
export type DebridgeOrderStatus = {
|
|
73
|
+
orderId: string;
|
|
74
|
+
status: 'None' | 'Created' | 'Fulfilled' | 'SentUnlock' | 'ClaimedUnlock' | 'OrderCancelled' | 'SentOrderCancel' | 'ClaimedOrderCancel';
|
|
75
|
+
};
|
|
76
|
+
export type DebridgeHashObject = {
|
|
77
|
+
stringValue: string;
|
|
78
|
+
bytesValue: string;
|
|
79
|
+
bytesArrayValue: string;
|
|
80
|
+
};
|
|
81
|
+
export type DebridgeOrderDetail = {
|
|
82
|
+
orderId: DebridgeHashObject;
|
|
83
|
+
fulfilledDstEventMetadata?: {
|
|
84
|
+
transactionHash?: DebridgeHashObject;
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
export type DebridgeOrderId = {
|
|
88
|
+
bytesValue: string;
|
|
89
|
+
bytesArrayValue: string;
|
|
90
|
+
stringValue: string;
|
|
91
|
+
};
|
|
92
|
+
export type DebridgeOrderIdsResponse = {
|
|
93
|
+
orderIds: DebridgeOrderId[];
|
|
94
|
+
};
|
|
95
|
+
export type DebridgeSingleChainTokenInfo = {
|
|
96
|
+
address: string;
|
|
97
|
+
name: string;
|
|
98
|
+
symbol: string;
|
|
99
|
+
decimals: number;
|
|
100
|
+
amount: string;
|
|
101
|
+
approximateUsdValue?: number;
|
|
102
|
+
};
|
|
103
|
+
export type DebridgeSingleChainTokenOut = DebridgeSingleChainTokenInfo & {
|
|
104
|
+
minAmount: string;
|
|
105
|
+
};
|
|
106
|
+
export type DebridgeSingleChainCostDetail = {
|
|
107
|
+
chain: string;
|
|
108
|
+
tokenIn: string;
|
|
109
|
+
tokenOut: string;
|
|
110
|
+
amountIn: string;
|
|
111
|
+
amountOut: string;
|
|
112
|
+
type: string;
|
|
113
|
+
payload: {
|
|
114
|
+
feeAmount?: string;
|
|
115
|
+
feeBps?: string;
|
|
116
|
+
amountOutBeforeCorrection?: string;
|
|
117
|
+
estimatedVolatilityBps?: string;
|
|
118
|
+
actualFeeAmount?: string;
|
|
119
|
+
actualFeeBps?: string;
|
|
120
|
+
subsidyAmount?: string;
|
|
121
|
+
feeApproximateUsdValue?: string;
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
export type DebridgeSingleChainEstimatedTransactionFee = {
|
|
125
|
+
total: string;
|
|
126
|
+
details: {
|
|
127
|
+
gasLimit: string;
|
|
128
|
+
baseFee?: string;
|
|
129
|
+
maxFeePerGas?: string;
|
|
130
|
+
maxPriorityFeePerGas?: string;
|
|
131
|
+
};
|
|
132
|
+
approximateUsdValue?: number;
|
|
133
|
+
};
|
|
134
|
+
export type DebridgeSingleChainTransactionResponse = {
|
|
135
|
+
tokenIn: DebridgeSingleChainTokenInfo;
|
|
136
|
+
tokenOut: DebridgeSingleChainTokenOut;
|
|
137
|
+
slippage: number;
|
|
138
|
+
recommendedSlippage: number;
|
|
139
|
+
protocolFee?: string;
|
|
140
|
+
costsDetails: DebridgeSingleChainCostDetail[];
|
|
141
|
+
estimatedTransactionFee?: DebridgeSingleChainEstimatedTransactionFee;
|
|
142
|
+
tx: {
|
|
143
|
+
to: string;
|
|
144
|
+
data: string;
|
|
145
|
+
value: string;
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
export type DebridgeError = {
|
|
149
|
+
errorCode?: number;
|
|
150
|
+
errorId?: string;
|
|
151
|
+
errorMessage?: string;
|
|
152
|
+
statusCode?: number;
|
|
153
|
+
message?: string;
|
|
154
|
+
};
|
|
155
|
+
export declare const isDebridgeError: (error: unknown) => error is DebridgeError;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { ASSET_NAMESPACE, CHAIN_NAMESPACE, CHAIN_REFERENCE, fromAssetId, solAssetId, toAssetId, wrappedSolAssetId, } from '@shapeshiftoss/caip';
|
|
2
2
|
import { bn, bnOrZero, convertDecimalPercentageToBasisPoints } from '@shapeshiftoss/utils';
|
|
3
3
|
import { Err, Ok } from '@sniptt/monads';
|
|
4
|
-
import { PublicKey } from '@solana/web3.js';
|
|
4
|
+
import { AddressLookupTableAccount, MessageV0, PublicKey, VersionedTransaction, } from '@solana/web3.js';
|
|
5
5
|
import { v4 as uuid } from 'uuid';
|
|
6
6
|
import { SwapperName, TradeQuoteError } from '../../../types.js';
|
|
7
7
|
import { getInputOutputRate, makeSwapErrorRight } from '../../../utils.js';
|
|
8
|
+
import { buildAffiliateFee } from '../../utils/affiliateFee.js';
|
|
8
9
|
import { COMPUTE_UNIT_MARGIN_MULTIPLIER, TOKEN_2022_PROGRAM_ID } from '../utils/constants.js';
|
|
9
10
|
import { calculateAccountCreationCosts, createSwapInstructions, getJupiterPrice, isSupportedChainId, } from '../utils/helpers.js';
|
|
10
11
|
export const getTradeQuote = async (input, deps) => {
|
|
@@ -89,6 +90,28 @@ export const getTradeQuote = async (input, deps) => {
|
|
|
89
90
|
adapter,
|
|
90
91
|
jupiterUrl,
|
|
91
92
|
});
|
|
93
|
+
// Build a trial VersionedTransaction to check if it exceeds the 1232-byte Solana tx size limit
|
|
94
|
+
const isOversized = await (async () => {
|
|
95
|
+
try {
|
|
96
|
+
const lookupTableInfos = await adapter.getAddressLookupTableAccounts(addressLookupTableAddresses);
|
|
97
|
+
const lookupTableAccounts = lookupTableInfos.map(info => new AddressLookupTableAccount({
|
|
98
|
+
key: new PublicKey(info.key),
|
|
99
|
+
state: AddressLookupTableAccount.deserialize(new Uint8Array(info.data)),
|
|
100
|
+
}));
|
|
101
|
+
const messageV0 = MessageV0.compile({
|
|
102
|
+
payerKey: new PublicKey(sendAddress),
|
|
103
|
+
instructions,
|
|
104
|
+
// Blockhash doesn't affect size, use a dummy one
|
|
105
|
+
recentBlockhash: PublicKey.default.toString(),
|
|
106
|
+
addressLookupTableAccounts: lookupTableAccounts,
|
|
107
|
+
});
|
|
108
|
+
const txBytes = new VersionedTransaction(messageV0).serialize();
|
|
109
|
+
return txBytes.length > 1232;
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
})();
|
|
92
115
|
const getFeeData = async () => {
|
|
93
116
|
const sellAdapter = deps.assertGetSolanaChainAdapter(sellAsset.chainId);
|
|
94
117
|
const getFeeDataInput = {
|
|
@@ -166,6 +189,7 @@ export const getTradeQuote = async (input, deps) => {
|
|
|
166
189
|
solanaTransactionMetadata: {
|
|
167
190
|
addressLookupTableAddresses,
|
|
168
191
|
instructions,
|
|
192
|
+
isOversized,
|
|
169
193
|
},
|
|
170
194
|
feeData: {
|
|
171
195
|
protocolFees,
|
|
@@ -179,6 +203,14 @@ export const getTradeQuote = async (input, deps) => {
|
|
|
179
203
|
allowanceContract: '0x0',
|
|
180
204
|
// Swap are so fasts on solana that times are under 100ms displaying 0 or very small amount of time is not user friendly
|
|
181
205
|
estimatedExecutionTimeMs: undefined,
|
|
206
|
+
affiliateFee: buildAffiliateFee({
|
|
207
|
+
strategy: 'buy_asset',
|
|
208
|
+
affiliateBps,
|
|
209
|
+
sellAsset,
|
|
210
|
+
buyAsset,
|
|
211
|
+
sellAmountCryptoBaseUnit: priceResponse.inAmount,
|
|
212
|
+
buyAmountCryptoBaseUnit: priceResponse.outAmount,
|
|
213
|
+
}),
|
|
182
214
|
},
|
|
183
215
|
],
|
|
184
216
|
};
|
|
@@ -5,6 +5,7 @@ import { PublicKey } from '@solana/web3.js';
|
|
|
5
5
|
import { v4 as uuid } from 'uuid';
|
|
6
6
|
import { SwapperName, TradeQuoteError } from '../../../types.js';
|
|
7
7
|
import { getInputOutputRate, makeSwapErrorRight } from '../../../utils.js';
|
|
8
|
+
import { buildAffiliateFee } from '../../utils/affiliateFee.js';
|
|
8
9
|
import { SOLANA_RANDOM_ADDRESS, TOKEN_2022_PROGRAM_ID } from '../utils/constants.js';
|
|
9
10
|
import { calculateAccountCreationCosts, createSwapInstructions, getJupiterPrice, isSupportedChainId, } from '../utils/helpers.js';
|
|
10
11
|
export const getTradeRate = async (input, deps) => {
|
|
@@ -160,6 +161,14 @@ export const getTradeRate = async (input, deps) => {
|
|
|
160
161
|
allowanceContract: '0x0',
|
|
161
162
|
// Swap are so fasts on solana that times are under 100ms displaying 0 or very small amount of time is not user friendly
|
|
162
163
|
estimatedExecutionTimeMs: undefined,
|
|
164
|
+
affiliateFee: buildAffiliateFee({
|
|
165
|
+
strategy: 'buy_asset',
|
|
166
|
+
affiliateBps,
|
|
167
|
+
sellAsset,
|
|
168
|
+
buyAsset,
|
|
169
|
+
sellAmountCryptoBaseUnit: priceResponse.inAmount,
|
|
170
|
+
buyAmountCryptoBaseUnit: priceResponse.outAmount,
|
|
171
|
+
}),
|
|
163
172
|
},
|
|
164
173
|
],
|
|
165
174
|
};
|
|
@@ -3,4 +3,4 @@ import generatedTradableAssetMap from '../../generated/generatedTradableAssetMap
|
|
|
3
3
|
const mayaPoolIdAssetIdSymbolMap = generatedTradableAssetMap;
|
|
4
4
|
export const assetIdToMayaPoolAssetIdMap = invert(mayaPoolIdAssetIdSymbolMap);
|
|
5
5
|
export const mayaPoolAssetIdToAssetId = (id) => mayaPoolIdAssetIdSymbolMap[id.toUpperCase()];
|
|
6
|
-
export const assetIdToMayaPoolAssetId = ({ assetId }) => assetIdToMayaPoolAssetIdMap[assetId
|
|
6
|
+
export const assetIdToMayaPoolAssetId = ({ assetId }) => assetIdToMayaPoolAssetIdMap[assetId];
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { CHAIN_NAMESPACE, fromAssetId } from '@shapeshiftoss/caip';
|
|
1
|
+
import { CHAIN_NAMESPACE, fromAssetId, nearChainId } from '@shapeshiftoss/caip';
|
|
2
2
|
import { evm } from '@shapeshiftoss/chain-adapters';
|
|
3
|
-
import { bn, bnOrZero, contractAddressOrUndefined, DAO_TREASURY_NEAR, isToken, } from '@shapeshiftoss/utils';
|
|
3
|
+
import { bn, bnOrZero, chainIdToFeeAssetId, contractAddressOrUndefined, DAO_TREASURY_NEAR, isToken, } from '@shapeshiftoss/utils';
|
|
4
4
|
import { Err, Ok } from '@sniptt/monads';
|
|
5
5
|
import { v4 as uuid } from 'uuid';
|
|
6
6
|
import { getDefaultSlippageDecimalPercentageForSwapper } from '../../../constants.js';
|
|
7
7
|
import { SwapperName, TradeQuoteError } from '../../../types.js';
|
|
8
8
|
import { createTradeAmountTooSmallErr, getInputOutputRate, makeSwapErrorRight, } from '../../../utils.js';
|
|
9
|
+
import { buildAffiliateFee } from '../../utils/affiliateFee.js';
|
|
9
10
|
import { isNativeEvmAsset } from '../../utils/helpers/helpers.js';
|
|
10
11
|
import { DEFAULT_QUOTE_DEADLINE_MS, DEFAULT_SLIPPAGE_BPS } from '../constants.js';
|
|
11
12
|
import { QuoteRequest } from '../types.js';
|
|
@@ -275,6 +276,17 @@ export const getTradeQuote = async (input, deps) => {
|
|
|
275
276
|
timeEstimate: quote.timeEstimate,
|
|
276
277
|
deadline: quote.deadline ?? '',
|
|
277
278
|
},
|
|
279
|
+
affiliateFee: buildAffiliateFee({
|
|
280
|
+
strategy: 'fixed_asset',
|
|
281
|
+
affiliateBps,
|
|
282
|
+
sellAsset,
|
|
283
|
+
buyAsset,
|
|
284
|
+
sellAmountCryptoBaseUnit: quote.amountIn,
|
|
285
|
+
buyAmountCryptoBaseUnit: quote.amountOut,
|
|
286
|
+
fixedAssetId: chainIdToFeeAssetId(nearChainId),
|
|
287
|
+
fixedAsset: deps.assetsById[chainIdToFeeAssetId(nearChainId)],
|
|
288
|
+
isEstimate: true,
|
|
289
|
+
}),
|
|
278
290
|
},
|
|
279
291
|
],
|
|
280
292
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { CHAIN_NAMESPACE, fromAssetId, monadChainId } from '@shapeshiftoss/caip';
|
|
1
|
+
import { CHAIN_NAMESPACE, fromAssetId, monadChainId, nearChainId } from '@shapeshiftoss/caip';
|
|
2
2
|
import { evm } from '@shapeshiftoss/chain-adapters';
|
|
3
|
-
import { bn, bnOrZero, contractAddressOrUndefined, DAO_TREASURY_NEAR, isToken, } from '@shapeshiftoss/utils';
|
|
3
|
+
import { bn, bnOrZero, chainIdToFeeAssetId, contractAddressOrUndefined, DAO_TREASURY_NEAR, isToken, } from '@shapeshiftoss/utils';
|
|
4
4
|
import { Err, Ok } from '@sniptt/monads';
|
|
5
5
|
import { v4 as uuid } from 'uuid';
|
|
6
6
|
import { getAddress } from 'viem';
|
|
@@ -8,6 +8,7 @@ import { getDefaultSlippageDecimalPercentageForSwapper } from '../../../constant
|
|
|
8
8
|
import { SwapperName, TradeQuoteError } from '../../../types.js';
|
|
9
9
|
import { createTradeAmountTooSmallErr, getInputOutputRate, makeSwapErrorRight, } from '../../../utils.js';
|
|
10
10
|
import { simulateWithStateOverrides } from '../../../utils/tenderly/index.js';
|
|
11
|
+
import { buildAffiliateFee } from '../../utils/affiliateFee.js';
|
|
11
12
|
import { isNativeEvmAsset } from '../../utils/helpers/helpers.js';
|
|
12
13
|
import { DEFAULT_QUOTE_DEADLINE_MS, DEFAULT_SLIPPAGE_BPS } from '../constants.js';
|
|
13
14
|
import { QuoteRequest } from '../types.js';
|
|
@@ -334,6 +335,17 @@ export const getTradeRate = async (input, deps) => {
|
|
|
334
335
|
sellAsset,
|
|
335
336
|
source: SwapperName.NearIntents,
|
|
336
337
|
estimatedExecutionTimeMs: quote.timeEstimate ? quote.timeEstimate * 1000 : undefined,
|
|
338
|
+
affiliateFee: buildAffiliateFee({
|
|
339
|
+
strategy: 'fixed_asset',
|
|
340
|
+
affiliateBps,
|
|
341
|
+
sellAsset,
|
|
342
|
+
buyAsset,
|
|
343
|
+
sellAmountCryptoBaseUnit: quote.amountIn,
|
|
344
|
+
buyAmountCryptoBaseUnit: quote.amountOut,
|
|
345
|
+
fixedAssetId: chainIdToFeeAssetId(nearChainId),
|
|
346
|
+
fixedAsset: deps.assetsById[chainIdToFeeAssetId(nearChainId)],
|
|
347
|
+
isEstimate: true,
|
|
348
|
+
}),
|
|
337
349
|
},
|
|
338
350
|
],
|
|
339
351
|
};
|