@shapeshiftoss/swapper 17.6.11 → 17.7.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/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/constants.js +16 -26
- package/dist/cjs/index.d.ts +0 -1
- package/dist/cjs/index.js +0 -1
- package/dist/cjs/solana-utils/getUnsignedSolanaTransaction.js +4 -2
- package/dist/cjs/swappers/AvnuSwapper/swapperApi/getTradeQuote.js +3 -1
- package/dist/cjs/swappers/ChainflipSwapper/endpoints.js +14 -4
- package/dist/cjs/swappers/ChainflipSwapper/types.d.ts +1 -0
- package/dist/cjs/swappers/DebridgeSwapper/utils/fetchDebridgeTrade.d.ts +1 -1
- package/dist/cjs/swappers/DebridgeSwapper/utils/getTrade.js +7 -9
- package/dist/cjs/swappers/RelaySwapper/constant.js +3 -7
- package/dist/cjs/swappers/RelaySwapper/endpoints.js +1 -1
- package/dist/cjs/swappers/RelaySwapper/utils/getTrade.js +12 -2
- package/dist/cjs/swappers/RelaySwapper/utils/relayTokenToAssetId.js +5 -0
- package/dist/cjs/swappers/StonfiSwapper/swapperApi/getTradeQuote.js +2 -1
- package/dist/cjs/tron-utils/getUnsignedTronTransaction.js +16 -0
- package/dist/cjs/types.d.ts +3 -6
- package/dist/cjs/types.js +0 -1
- package/dist/cjs/utils.d.ts +3 -0
- package/dist/cjs/utils.js +9 -3
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/constants.js +0 -10
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +0 -1
- package/dist/esm/solana-utils/getUnsignedSolanaTransaction.js +3 -1
- package/dist/esm/swappers/AvnuSwapper/swapperApi/getTradeQuote.js +3 -1
- package/dist/esm/swappers/ChainflipSwapper/endpoints.js +14 -4
- package/dist/esm/swappers/ChainflipSwapper/types.d.ts +1 -0
- package/dist/esm/swappers/DebridgeSwapper/utils/fetchDebridgeTrade.d.ts +1 -1
- package/dist/esm/swappers/DebridgeSwapper/utils/getTrade.js +7 -9
- package/dist/esm/swappers/RelaySwapper/constant.js +4 -8
- package/dist/esm/swappers/RelaySwapper/endpoints.js +2 -2
- package/dist/esm/swappers/RelaySwapper/utils/getTrade.js +13 -3
- package/dist/esm/swappers/RelaySwapper/utils/relayTokenToAssetId.js +5 -0
- package/dist/esm/swappers/StonfiSwapper/swapperApi/getTradeQuote.js +2 -1
- package/dist/esm/tron-utils/getUnsignedTronTransaction.js +16 -0
- package/dist/esm/types.d.ts +3 -6
- package/dist/esm/types.js +0 -1
- package/dist/esm/utils.d.ts +3 -0
- package/dist/esm/utils.js +7 -2
- package/package.json +6 -7
- package/dist/cjs/swappers/JupiterSwapper/JupiterSwapper.d.ts +0 -2
- package/dist/cjs/swappers/JupiterSwapper/JupiterSwapper.js +0 -27
- package/dist/cjs/swappers/JupiterSwapper/endpoints.d.ts +0 -2
- package/dist/cjs/swappers/JupiterSwapper/endpoints.js +0 -15
- package/dist/cjs/swappers/JupiterSwapper/errorPatterns.d.ts +0 -15
- package/dist/cjs/swappers/JupiterSwapper/errorPatterns.js +0 -29
- package/dist/cjs/swappers/JupiterSwapper/idls/referral.d.ts +0 -739
- package/dist/cjs/swappers/JupiterSwapper/idls/referral.js +0 -741
- package/dist/cjs/swappers/JupiterSwapper/index.d.ts +0 -2
- package/dist/cjs/swappers/JupiterSwapper/index.js +0 -18
- package/dist/cjs/swappers/JupiterSwapper/swapperApi/getTradeQuote.d.ts +0 -3
- package/dist/cjs/swappers/JupiterSwapper/swapperApi/getTradeQuote.js +0 -223
- package/dist/cjs/swappers/JupiterSwapper/swapperApi/getTradeRate.d.ts +0 -3
- package/dist/cjs/swappers/JupiterSwapper/swapperApi/getTradeRate.js +0 -181
- package/dist/cjs/swappers/JupiterSwapper/utils/constants.d.ts +0 -9
- package/dist/cjs/swappers/JupiterSwapper/utils/constants.js +0 -14
- package/dist/cjs/swappers/JupiterSwapper/utils/helpers.d.ts +0 -56
- package/dist/cjs/swappers/JupiterSwapper/utils/helpers.js +0 -178
- package/dist/cjs/swappers/JupiterSwapper/utils/jupiterService.d.ts +0 -5
- package/dist/cjs/swappers/JupiterSwapper/utils/jupiterService.js +0 -15
- package/dist/esm/swappers/JupiterSwapper/JupiterSwapper.d.ts +0 -2
- package/dist/esm/swappers/JupiterSwapper/JupiterSwapper.js +0 -24
- package/dist/esm/swappers/JupiterSwapper/endpoints.d.ts +0 -2
- package/dist/esm/swappers/JupiterSwapper/endpoints.js +0 -12
- package/dist/esm/swappers/JupiterSwapper/errorPatterns.d.ts +0 -15
- package/dist/esm/swappers/JupiterSwapper/errorPatterns.js +0 -25
- package/dist/esm/swappers/JupiterSwapper/idls/referral.d.ts +0 -739
- package/dist/esm/swappers/JupiterSwapper/idls/referral.js +0 -738
- package/dist/esm/swappers/JupiterSwapper/index.d.ts +0 -2
- package/dist/esm/swappers/JupiterSwapper/index.js +0 -2
- package/dist/esm/swappers/JupiterSwapper/swapperApi/getTradeQuote.d.ts +0 -3
- package/dist/esm/swappers/JupiterSwapper/swapperApi/getTradeQuote.js +0 -219
- package/dist/esm/swappers/JupiterSwapper/swapperApi/getTradeRate.d.ts +0 -3
- package/dist/esm/swappers/JupiterSwapper/swapperApi/getTradeRate.js +0 -177
- package/dist/esm/swappers/JupiterSwapper/utils/constants.d.ts +0 -9
- package/dist/esm/swappers/JupiterSwapper/utils/constants.js +0 -11
- package/dist/esm/swappers/JupiterSwapper/utils/helpers.d.ts +0 -56
- package/dist/esm/swappers/JupiterSwapper/utils/helpers.js +0 -169
- package/dist/esm/swappers/JupiterSwapper/utils/jupiterService.d.ts +0 -5
- package/dist/esm/swappers/JupiterSwapper/utils/jupiterService.js +0 -12
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./utils/constants"), exports);
|
|
18
|
-
__exportStar(require("./errorPatterns"), exports);
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { Result } from '@sniptt/monads';
|
|
2
|
-
import type { CommonTradeQuoteInput, SwapErrorRight, SwapperDeps, TradeQuote } from '../../../types';
|
|
3
|
-
export declare const getTradeQuote: (input: CommonTradeQuoteInput, deps: SwapperDeps) => Promise<Result<TradeQuote[], SwapErrorRight>>;
|
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTradeQuote = void 0;
|
|
4
|
-
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
-
const utils_1 = require("@shapeshiftoss/utils");
|
|
6
|
-
const monads_1 = require("@sniptt/monads");
|
|
7
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
8
|
-
const uuid_1 = require("uuid");
|
|
9
|
-
const types_1 = require("../../../types");
|
|
10
|
-
const utils_2 = require("../../../utils");
|
|
11
|
-
const affiliateFee_1 = require("../../utils/affiliateFee");
|
|
12
|
-
const constants_1 = require("../utils/constants");
|
|
13
|
-
const helpers_1 = require("../utils/helpers");
|
|
14
|
-
const getTradeQuote = async (input, deps) => {
|
|
15
|
-
const { sellAsset, buyAsset, receiveAddress, accountNumber, sendAddress, sellAmountIncludingProtocolFeesCryptoBaseUnit: sellAmount, } = input;
|
|
16
|
-
const { assetsById } = deps;
|
|
17
|
-
const jupiterUrl = deps.config.VITE_JUPITER_API_URL;
|
|
18
|
-
const solAsset = assetsById[caip_1.solAssetId];
|
|
19
|
-
if (accountNumber === undefined) {
|
|
20
|
-
return (0, monads_1.Err)((0, utils_2.makeSwapErrorRight)({
|
|
21
|
-
message: `accountNumber is required`,
|
|
22
|
-
code: types_1.TradeQuoteError.UnknownError,
|
|
23
|
-
}));
|
|
24
|
-
}
|
|
25
|
-
if (!(0, helpers_1.isSupportedChainId)(sellAsset.chainId)) {
|
|
26
|
-
return (0, monads_1.Err)((0, utils_2.makeSwapErrorRight)({
|
|
27
|
-
message: `unsupported chainId`,
|
|
28
|
-
code: types_1.TradeQuoteError.UnsupportedChain,
|
|
29
|
-
details: { chainId: sellAsset.chainId },
|
|
30
|
-
}));
|
|
31
|
-
}
|
|
32
|
-
if (!(0, helpers_1.isSupportedChainId)(buyAsset.chainId)) {
|
|
33
|
-
return (0, monads_1.Err)((0, utils_2.makeSwapErrorRight)({
|
|
34
|
-
message: `unsupported chainId`,
|
|
35
|
-
code: types_1.TradeQuoteError.UnsupportedChain,
|
|
36
|
-
details: { chainId: sellAsset.chainId },
|
|
37
|
-
}));
|
|
38
|
-
}
|
|
39
|
-
if (!sendAddress) {
|
|
40
|
-
return (0, monads_1.Err)((0, utils_2.makeSwapErrorRight)({
|
|
41
|
-
message: `sendAddress is required`,
|
|
42
|
-
code: types_1.TradeQuoteError.UnknownError,
|
|
43
|
-
}));
|
|
44
|
-
}
|
|
45
|
-
if (!solAsset) {
|
|
46
|
-
return (0, monads_1.Err)((0, utils_2.makeSwapErrorRight)({
|
|
47
|
-
message: `solAsset is required`,
|
|
48
|
-
code: types_1.TradeQuoteError.UnknownError,
|
|
49
|
-
}));
|
|
50
|
-
}
|
|
51
|
-
const adapter = deps.assertGetSolanaChainAdapter(sellAsset.chainId);
|
|
52
|
-
const buyAssetAddress = buyAsset.assetId === caip_1.solAssetId
|
|
53
|
-
? (0, caip_1.fromAssetId)(caip_1.wrappedSolAssetId).assetReference
|
|
54
|
-
: (0, caip_1.fromAssetId)(buyAsset.assetId).assetReference;
|
|
55
|
-
const sellAssetAddress = sellAsset.assetId === caip_1.solAssetId
|
|
56
|
-
? (0, caip_1.fromAssetId)(caip_1.wrappedSolAssetId).assetReference
|
|
57
|
-
: (0, caip_1.fromAssetId)(sellAsset.assetId).assetReference;
|
|
58
|
-
const sellTokenInfo = await adapter
|
|
59
|
-
.getConnection()
|
|
60
|
-
.getAccountInfo(new web3_js_1.PublicKey(sellAssetAddress));
|
|
61
|
-
const buyTokenInfo = await adapter.getConnection().getAccountInfo(new web3_js_1.PublicKey(buyAssetAddress));
|
|
62
|
-
const isSellTokenToken2022 = sellTokenInfo?.owner.toString() === constants_1.TOKEN_2022_PROGRAM_ID.toString();
|
|
63
|
-
const isBuyTokenToken2022 = buyTokenInfo?.owner.toString() === constants_1.TOKEN_2022_PROGRAM_ID.toString();
|
|
64
|
-
const affiliateBps = isSellTokenToken2022 || isBuyTokenToken2022 ? '0' : input.affiliateBps;
|
|
65
|
-
const maybePriceResponse = await (0, helpers_1.getJupiterPrice)({
|
|
66
|
-
apiUrl: jupiterUrl,
|
|
67
|
-
sourceAssetAddress: sellAssetAddress,
|
|
68
|
-
destinationAssetAddress: buyAssetAddress,
|
|
69
|
-
commissionBps: affiliateBps,
|
|
70
|
-
amount: sellAmount,
|
|
71
|
-
slippageBps: input.slippageTolerancePercentageDecimal
|
|
72
|
-
? (0, utils_1.convertDecimalPercentageToBasisPoints)(input.slippageTolerancePercentageDecimal).toFixed()
|
|
73
|
-
: undefined,
|
|
74
|
-
});
|
|
75
|
-
if (maybePriceResponse.isErr()) {
|
|
76
|
-
return (0, monads_1.Err)((0, utils_2.makeSwapErrorRight)({
|
|
77
|
-
message: 'Quote request failed',
|
|
78
|
-
code: types_1.TradeQuoteError.NoRouteFound,
|
|
79
|
-
}));
|
|
80
|
-
}
|
|
81
|
-
const { data: priceResponse } = maybePriceResponse.unwrap();
|
|
82
|
-
const slippageTolerancePercentageDecimal =
|
|
83
|
-
// Divide by 100 to get actual decimal percentage from bps
|
|
84
|
-
// e.g for 0.5% bps, Jupiter represents this as 50. 50/100 = 0.5, then we div by 100 again to honour our decimal format e.g 0.5/100 = 0.005
|
|
85
|
-
(0, utils_1.bn)(priceResponse.slippageBps).div(100).div(100).toString();
|
|
86
|
-
const { instructions, addressLookupTableAddresses } = await (0, helpers_1.createSwapInstructions)({
|
|
87
|
-
priceResponse,
|
|
88
|
-
sendAddress,
|
|
89
|
-
receiveAddress,
|
|
90
|
-
affiliateBps,
|
|
91
|
-
buyAsset,
|
|
92
|
-
sellAsset,
|
|
93
|
-
adapter,
|
|
94
|
-
jupiterUrl,
|
|
95
|
-
});
|
|
96
|
-
// Build a trial VersionedTransaction to check if it exceeds the 1232-byte Solana tx size limit
|
|
97
|
-
const isOversized = await (async () => {
|
|
98
|
-
try {
|
|
99
|
-
const lookupTableInfos = await adapter.getAddressLookupTableAccounts(addressLookupTableAddresses);
|
|
100
|
-
const lookupTableAccounts = lookupTableInfos.map(info => new web3_js_1.AddressLookupTableAccount({
|
|
101
|
-
key: new web3_js_1.PublicKey(info.key),
|
|
102
|
-
state: web3_js_1.AddressLookupTableAccount.deserialize(new Uint8Array(info.data)),
|
|
103
|
-
}));
|
|
104
|
-
const messageV0 = web3_js_1.MessageV0.compile({
|
|
105
|
-
payerKey: new web3_js_1.PublicKey(sendAddress),
|
|
106
|
-
instructions,
|
|
107
|
-
// Blockhash doesn't affect size, use a dummy one
|
|
108
|
-
recentBlockhash: web3_js_1.PublicKey.default.toString(),
|
|
109
|
-
addressLookupTableAccounts: lookupTableAccounts,
|
|
110
|
-
});
|
|
111
|
-
const txBytes = new web3_js_1.VersionedTransaction(messageV0).serialize();
|
|
112
|
-
return txBytes.length > 1232;
|
|
113
|
-
}
|
|
114
|
-
catch {
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
})();
|
|
118
|
-
const getFeeData = async () => {
|
|
119
|
-
const sellAdapter = deps.assertGetSolanaChainAdapter(sellAsset.chainId);
|
|
120
|
-
const getFeeDataInput = {
|
|
121
|
-
to: '',
|
|
122
|
-
value: '0',
|
|
123
|
-
chainSpecific: {
|
|
124
|
-
from: sendAddress,
|
|
125
|
-
addressLookupTableAccounts: addressLookupTableAddresses,
|
|
126
|
-
instructions,
|
|
127
|
-
},
|
|
128
|
-
};
|
|
129
|
-
const feeData = await sellAdapter.getFeeData(getFeeDataInput);
|
|
130
|
-
return {
|
|
131
|
-
txFee: feeData.fast.txFee,
|
|
132
|
-
chainSpecific: {
|
|
133
|
-
computeUnits: (0, utils_1.bnOrZero)(feeData.fast.chainSpecific.computeUnits)
|
|
134
|
-
.times(constants_1.COMPUTE_UNIT_MARGIN_MULTIPLIER)
|
|
135
|
-
.toFixed(0),
|
|
136
|
-
priorityFee: feeData.fast.chainSpecific.priorityFee,
|
|
137
|
-
},
|
|
138
|
-
};
|
|
139
|
-
};
|
|
140
|
-
const protocolFees = priceResponse.routePlan.reduce((acc, route) => {
|
|
141
|
-
const feeAssetId = (0, caip_1.toAssetId)({
|
|
142
|
-
assetReference: route.swapInfo.feeMint,
|
|
143
|
-
assetNamespace: caip_1.ASSET_NAMESPACE.splToken,
|
|
144
|
-
chainNamespace: caip_1.CHAIN_NAMESPACE.Solana,
|
|
145
|
-
chainReference: caip_1.CHAIN_REFERENCE.SolanaMainnet,
|
|
146
|
-
});
|
|
147
|
-
const feeAsset = assetsById[feeAssetId];
|
|
148
|
-
// If we can't find the feeAsset, we can't provide a protocol fee to display
|
|
149
|
-
// But these fees exists at protocol level, it's mostly to make TS happy as we should have the market data and assets
|
|
150
|
-
if (!feeAsset)
|
|
151
|
-
return acc;
|
|
152
|
-
acc[feeAssetId] = {
|
|
153
|
-
requiresBalance: false,
|
|
154
|
-
amountCryptoBaseUnit: (0, utils_1.bnOrZero)(route.swapInfo.feeAmount).toFixed(0),
|
|
155
|
-
asset: feeAsset,
|
|
156
|
-
};
|
|
157
|
-
return acc;
|
|
158
|
-
}, {});
|
|
159
|
-
const quotes = [];
|
|
160
|
-
const feeData = await getFeeData();
|
|
161
|
-
const rate = (0, utils_2.getInputOutputRate)({
|
|
162
|
-
sellAmountCryptoBaseUnit: priceResponse.inAmount,
|
|
163
|
-
buyAmountCryptoBaseUnit: priceResponse.outAmount,
|
|
164
|
-
sellAsset,
|
|
165
|
-
buyAsset,
|
|
166
|
-
});
|
|
167
|
-
const accountCreationFees = (0, helpers_1.calculateAccountCreationCosts)(instructions);
|
|
168
|
-
if (accountCreationFees !== '0') {
|
|
169
|
-
const solProtocolFeeAmount = (0, utils_1.bnOrZero)(protocolFees[caip_1.solAssetId]?.amountCryptoBaseUnit);
|
|
170
|
-
protocolFees[caip_1.solAssetId] = {
|
|
171
|
-
requiresBalance: true,
|
|
172
|
-
amountCryptoBaseUnit: (0, utils_1.bnOrZero)(solProtocolFeeAmount).plus(accountCreationFees).toFixed(),
|
|
173
|
-
asset: solAsset,
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
const tradeQuote = {
|
|
177
|
-
id: (0, uuid_1.v4)(),
|
|
178
|
-
quoteOrRate: 'quote',
|
|
179
|
-
rate,
|
|
180
|
-
affiliateBps,
|
|
181
|
-
receiveAddress,
|
|
182
|
-
slippageTolerancePercentageDecimal,
|
|
183
|
-
priceImpactPercentageDecimal: priceResponse.priceImpactPct,
|
|
184
|
-
swapperName: types_1.SwapperName.Jupiter,
|
|
185
|
-
steps: [
|
|
186
|
-
{
|
|
187
|
-
accountNumber,
|
|
188
|
-
buyAmountBeforeFeesCryptoBaseUnit: priceResponse.outAmount,
|
|
189
|
-
buyAmountAfterFeesCryptoBaseUnit: priceResponse.outAmount,
|
|
190
|
-
sellAmountIncludingProtocolFeesCryptoBaseUnit: priceResponse.inAmount,
|
|
191
|
-
jupiterQuoteResponse: priceResponse,
|
|
192
|
-
solanaTransactionMetadata: {
|
|
193
|
-
addressLookupTableAddresses,
|
|
194
|
-
instructions,
|
|
195
|
-
isOversized,
|
|
196
|
-
},
|
|
197
|
-
feeData: {
|
|
198
|
-
protocolFees,
|
|
199
|
-
networkFeeCryptoBaseUnit: feeData.txFee,
|
|
200
|
-
chainSpecific: feeData.chainSpecific,
|
|
201
|
-
},
|
|
202
|
-
rate,
|
|
203
|
-
source: types_1.SwapperName.Jupiter,
|
|
204
|
-
buyAsset,
|
|
205
|
-
sellAsset,
|
|
206
|
-
allowanceContract: '0x0',
|
|
207
|
-
// Swap are so fasts on solana that times are under 100ms displaying 0 or very small amount of time is not user friendly
|
|
208
|
-
estimatedExecutionTimeMs: undefined,
|
|
209
|
-
affiliateFee: (0, affiliateFee_1.buildAffiliateFee)({
|
|
210
|
-
strategy: 'buy_asset',
|
|
211
|
-
affiliateBps,
|
|
212
|
-
sellAsset,
|
|
213
|
-
buyAsset,
|
|
214
|
-
sellAmountCryptoBaseUnit: priceResponse.inAmount,
|
|
215
|
-
buyAmountCryptoBaseUnit: priceResponse.outAmount,
|
|
216
|
-
}),
|
|
217
|
-
},
|
|
218
|
-
],
|
|
219
|
-
};
|
|
220
|
-
quotes.push(tradeQuote);
|
|
221
|
-
return (0, monads_1.Ok)(quotes);
|
|
222
|
-
};
|
|
223
|
-
exports.getTradeQuote = getTradeQuote;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { Result } from '@sniptt/monads';
|
|
2
|
-
import type { GetTradeRateInput, SwapErrorRight, SwapperDeps, TradeRate } from '../../../types';
|
|
3
|
-
export declare const getTradeRate: (input: GetTradeRateInput, deps: SwapperDeps) => Promise<Result<TradeRate[], SwapErrorRight>>;
|
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTradeRate = void 0;
|
|
4
|
-
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
-
const utils_1 = require("@shapeshiftoss/utils");
|
|
6
|
-
const monads_1 = require("@sniptt/monads");
|
|
7
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
8
|
-
const uuid_1 = require("uuid");
|
|
9
|
-
const types_1 = require("../../../types");
|
|
10
|
-
const utils_2 = require("../../../utils");
|
|
11
|
-
const affiliateFee_1 = require("../../utils/affiliateFee");
|
|
12
|
-
const constants_1 = require("../utils/constants");
|
|
13
|
-
const helpers_1 = require("../utils/helpers");
|
|
14
|
-
const getTradeRate = async (input, deps) => {
|
|
15
|
-
const { sellAsset, buyAsset, sellAmountIncludingProtocolFeesCryptoBaseUnit: sellAmount, receiveAddress, accountNumber, } = input;
|
|
16
|
-
const { assetsById } = deps;
|
|
17
|
-
const jupiterUrl = deps.config.VITE_JUPITER_API_URL;
|
|
18
|
-
const solAsset = assetsById[caip_1.solAssetId];
|
|
19
|
-
if (!(0, helpers_1.isSupportedChainId)(sellAsset.chainId)) {
|
|
20
|
-
return (0, monads_1.Err)((0, utils_2.makeSwapErrorRight)({
|
|
21
|
-
message: `unsupported chainId`,
|
|
22
|
-
code: types_1.TradeQuoteError.UnsupportedChain,
|
|
23
|
-
details: { chainId: sellAsset.chainId },
|
|
24
|
-
}));
|
|
25
|
-
}
|
|
26
|
-
if (!(0, helpers_1.isSupportedChainId)(buyAsset.chainId)) {
|
|
27
|
-
return (0, monads_1.Err)((0, utils_2.makeSwapErrorRight)({
|
|
28
|
-
message: `unsupported chainId`,
|
|
29
|
-
code: types_1.TradeQuoteError.UnsupportedChain,
|
|
30
|
-
details: { chainId: sellAsset.chainId },
|
|
31
|
-
}));
|
|
32
|
-
}
|
|
33
|
-
if (buyAsset.assetId === caip_1.wrappedSolAssetId || sellAsset.assetId === caip_1.wrappedSolAssetId) {
|
|
34
|
-
return (0, monads_1.Err)((0, utils_2.makeSwapErrorRight)({
|
|
35
|
-
message: `Unsupported trade pair`,
|
|
36
|
-
code: types_1.TradeQuoteError.UnsupportedTradePair,
|
|
37
|
-
}));
|
|
38
|
-
}
|
|
39
|
-
if (!solAsset) {
|
|
40
|
-
return (0, monads_1.Err)((0, utils_2.makeSwapErrorRight)({
|
|
41
|
-
message: `solAsset should be defined`,
|
|
42
|
-
code: types_1.TradeQuoteError.UnknownError,
|
|
43
|
-
}));
|
|
44
|
-
}
|
|
45
|
-
const adapter = deps.assertGetSolanaChainAdapter(sellAsset.chainId);
|
|
46
|
-
const buyAssetAddress = buyAsset.assetId === caip_1.solAssetId
|
|
47
|
-
? (0, caip_1.fromAssetId)(caip_1.wrappedSolAssetId).assetReference
|
|
48
|
-
: (0, caip_1.fromAssetId)(buyAsset.assetId).assetReference;
|
|
49
|
-
const sellAssetAddress = sellAsset.assetId === caip_1.solAssetId
|
|
50
|
-
? (0, caip_1.fromAssetId)(caip_1.wrappedSolAssetId).assetReference
|
|
51
|
-
: (0, caip_1.fromAssetId)(sellAsset.assetId).assetReference;
|
|
52
|
-
const sellTokenInfo = await adapter
|
|
53
|
-
.getConnection()
|
|
54
|
-
.getAccountInfo(new web3_js_1.PublicKey(sellAssetAddress));
|
|
55
|
-
const buyTokenInfo = await adapter.getConnection().getAccountInfo(new web3_js_1.PublicKey(buyAssetAddress));
|
|
56
|
-
const isSellTokenToken2022 = sellTokenInfo?.owner.toString() === constants_1.TOKEN_2022_PROGRAM_ID.toString();
|
|
57
|
-
const isBuyTokenToken2022 = buyTokenInfo?.owner.toString() === constants_1.TOKEN_2022_PROGRAM_ID.toString();
|
|
58
|
-
const affiliateBps = isSellTokenToken2022 || isBuyTokenToken2022 ? '0' : input.affiliateBps;
|
|
59
|
-
const maybePriceResponse = await (0, helpers_1.getJupiterPrice)({
|
|
60
|
-
apiUrl: jupiterUrl,
|
|
61
|
-
sourceAssetAddress: sellAssetAddress,
|
|
62
|
-
destinationAssetAddress: buyAssetAddress,
|
|
63
|
-
commissionBps: affiliateBps,
|
|
64
|
-
amount: sellAmount,
|
|
65
|
-
slippageBps: input.slippageTolerancePercentageDecimal
|
|
66
|
-
? (0, utils_1.convertDecimalPercentageToBasisPoints)(input.slippageTolerancePercentageDecimal).toFixed()
|
|
67
|
-
: undefined,
|
|
68
|
-
});
|
|
69
|
-
if (maybePriceResponse.isErr()) {
|
|
70
|
-
return (0, monads_1.Err)((0, utils_2.makeSwapErrorRight)({
|
|
71
|
-
message: 'Quote request failed',
|
|
72
|
-
code: types_1.TradeQuoteError.NoRouteFound,
|
|
73
|
-
}));
|
|
74
|
-
}
|
|
75
|
-
const { data: priceResponse } = maybePriceResponse.unwrap();
|
|
76
|
-
const { instructions } = await (0, helpers_1.createSwapInstructions)({
|
|
77
|
-
priceResponse,
|
|
78
|
-
sendAddress: input.sendAddress ?? constants_1.SOLANA_RANDOM_ADDRESS,
|
|
79
|
-
receiveAddress,
|
|
80
|
-
affiliateBps,
|
|
81
|
-
buyAsset,
|
|
82
|
-
sellAsset,
|
|
83
|
-
adapter,
|
|
84
|
-
jupiterUrl,
|
|
85
|
-
});
|
|
86
|
-
const getFeeData = async () => {
|
|
87
|
-
const sellAdapter = deps.assertGetSolanaChainAdapter(sellAsset.chainId);
|
|
88
|
-
const getFeeDataInput = {
|
|
89
|
-
// Use real address and instructions for accurate fee estimation whenever possible (Solana account connected)
|
|
90
|
-
to: input.sendAddress ?? constants_1.SOLANA_RANDOM_ADDRESS,
|
|
91
|
-
value: sellAmount,
|
|
92
|
-
chainSpecific: {
|
|
93
|
-
from: input.sendAddress ?? constants_1.SOLANA_RANDOM_ADDRESS,
|
|
94
|
-
instructions,
|
|
95
|
-
},
|
|
96
|
-
};
|
|
97
|
-
const { fast } = await sellAdapter.getFeeData(getFeeDataInput);
|
|
98
|
-
return { networkFeeCryptoBaseUnit: fast.txFee };
|
|
99
|
-
};
|
|
100
|
-
const protocolFees = priceResponse.routePlan.reduce((acc, route) => {
|
|
101
|
-
const feeAssetId = (0, caip_1.toAssetId)({
|
|
102
|
-
assetReference: route.swapInfo.feeMint,
|
|
103
|
-
assetNamespace: caip_1.ASSET_NAMESPACE.splToken,
|
|
104
|
-
chainNamespace: caip_1.CHAIN_NAMESPACE.Solana,
|
|
105
|
-
chainReference: caip_1.CHAIN_REFERENCE.SolanaMainnet,
|
|
106
|
-
});
|
|
107
|
-
const feeAsset = assetsById[feeAssetId];
|
|
108
|
-
// If we can't find the feeAsset, we can't provide a protocol fee to display
|
|
109
|
-
// But these fees exists at protocol level, it's mostly to make TS happy as we should have the market data and assets
|
|
110
|
-
if (!feeAsset)
|
|
111
|
-
return acc;
|
|
112
|
-
acc[feeAssetId] = {
|
|
113
|
-
requiresBalance: false,
|
|
114
|
-
amountCryptoBaseUnit: (0, utils_1.bnOrZero)(route.swapInfo.feeAmount).toFixed(0),
|
|
115
|
-
asset: feeAsset,
|
|
116
|
-
};
|
|
117
|
-
return acc;
|
|
118
|
-
}, {});
|
|
119
|
-
const rates = [];
|
|
120
|
-
const feeData = await getFeeData();
|
|
121
|
-
const inputOutputRate = (0, utils_2.getInputOutputRate)({
|
|
122
|
-
sellAmountCryptoBaseUnit: priceResponse.inAmount,
|
|
123
|
-
buyAmountCryptoBaseUnit: priceResponse.outAmount,
|
|
124
|
-
sellAsset,
|
|
125
|
-
buyAsset,
|
|
126
|
-
});
|
|
127
|
-
const slippageTolerancePercentageDecimal =
|
|
128
|
-
// Divide by 100 to get actual decimal percentage from bps
|
|
129
|
-
// e.g for 0.5% bps, Jupiter represents this as 50. 50/100 = 0.5, then we div by 100 again to honour our decimal format e.g 0.5/100 = 0.005
|
|
130
|
-
(0, utils_1.bn)(priceResponse.slippageBps).div(100).div(100).toString();
|
|
131
|
-
const accountCreationFees = (0, helpers_1.calculateAccountCreationCosts)(instructions);
|
|
132
|
-
if (accountCreationFees !== '0') {
|
|
133
|
-
const solProtocolFeeAmount = (0, utils_1.bnOrZero)(protocolFees[caip_1.solAssetId]?.amountCryptoBaseUnit);
|
|
134
|
-
protocolFees[caip_1.solAssetId] = {
|
|
135
|
-
requiresBalance: true,
|
|
136
|
-
amountCryptoBaseUnit: (0, utils_1.bnOrZero)(solProtocolFeeAmount).plus(accountCreationFees).toFixed(),
|
|
137
|
-
asset: solAsset,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
const tradeRate = {
|
|
141
|
-
id: (0, uuid_1.v4)(),
|
|
142
|
-
quoteOrRate: 'rate',
|
|
143
|
-
rate: inputOutputRate,
|
|
144
|
-
receiveAddress,
|
|
145
|
-
affiliateBps,
|
|
146
|
-
priceImpactPercentageDecimal: priceResponse.priceImpactPct,
|
|
147
|
-
slippageTolerancePercentageDecimal,
|
|
148
|
-
swapperName: types_1.SwapperName.Jupiter,
|
|
149
|
-
steps: [
|
|
150
|
-
{
|
|
151
|
-
buyAmountBeforeFeesCryptoBaseUnit: priceResponse.outAmount,
|
|
152
|
-
buyAmountAfterFeesCryptoBaseUnit: priceResponse.outAmount,
|
|
153
|
-
sellAmountIncludingProtocolFeesCryptoBaseUnit: priceResponse.inAmount,
|
|
154
|
-
jupiterQuoteResponse: priceResponse,
|
|
155
|
-
feeData: {
|
|
156
|
-
protocolFees,
|
|
157
|
-
...feeData,
|
|
158
|
-
},
|
|
159
|
-
rate: inputOutputRate,
|
|
160
|
-
source: types_1.SwapperName.Jupiter,
|
|
161
|
-
buyAsset,
|
|
162
|
-
sellAsset,
|
|
163
|
-
accountNumber,
|
|
164
|
-
allowanceContract: '0x0',
|
|
165
|
-
// Swap are so fasts on solana that times are under 100ms displaying 0 or very small amount of time is not user friendly
|
|
166
|
-
estimatedExecutionTimeMs: undefined,
|
|
167
|
-
affiliateFee: (0, affiliateFee_1.buildAffiliateFee)({
|
|
168
|
-
strategy: 'buy_asset',
|
|
169
|
-
affiliateBps,
|
|
170
|
-
sellAsset,
|
|
171
|
-
buyAsset,
|
|
172
|
-
sellAmountCryptoBaseUnit: priceResponse.inAmount,
|
|
173
|
-
buyAmountCryptoBaseUnit: priceResponse.outAmount,
|
|
174
|
-
}),
|
|
175
|
-
},
|
|
176
|
-
],
|
|
177
|
-
};
|
|
178
|
-
rates.push(tradeRate);
|
|
179
|
-
return (0, monads_1.Ok)(rates);
|
|
180
|
-
};
|
|
181
|
-
exports.getTradeRate = getTradeRate;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ChainId } from '@shapeshiftoss/caip';
|
|
2
|
-
export declare const jupiterSupportedChainIds: ChainId[];
|
|
3
|
-
export declare const PDA_ACCOUNT_CREATION_COST = 2040000;
|
|
4
|
-
export declare const SOLANA_RANDOM_ADDRESS = "2zHKF6tqam3tnNFPK2E9nBDkV7GMXnvdJautmzqQdn8A";
|
|
5
|
-
export declare const TOKEN_2022_PROGRAM_ID = "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb";
|
|
6
|
-
export declare const COMPUTE_UNIT_MARGIN_MULTIPLIER = 1.6;
|
|
7
|
-
export declare const SHAPESHIFT_JUPITER_REFERRAL_KEY = "Ajgmo453yGmcHDPoJBrMUj3GFwLVL7HaaZGNLkB8vREG";
|
|
8
|
-
export declare const JUPITER_AFFILIATE_CONTRACT_ADDRESS = "REFER4ZgmyYx9c6He5XfaTMiGfdLwRnkV4RPp9t9iF3";
|
|
9
|
-
export declare const JUPITER_REFERALL_FEE_PROJECT_ACCOUNT = "45ruCyfdRkWpRNGEqWzjCiXRHkZs8WXCLQ67Pnpye7Hp";
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.JUPITER_REFERALL_FEE_PROJECT_ACCOUNT = exports.JUPITER_AFFILIATE_CONTRACT_ADDRESS = exports.SHAPESHIFT_JUPITER_REFERRAL_KEY = exports.COMPUTE_UNIT_MARGIN_MULTIPLIER = exports.TOKEN_2022_PROGRAM_ID = exports.SOLANA_RANDOM_ADDRESS = exports.PDA_ACCOUNT_CREATION_COST = exports.jupiterSupportedChainIds = void 0;
|
|
4
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
5
|
-
exports.jupiterSupportedChainIds = [types_1.KnownChainIds.SolanaMainnet];
|
|
6
|
-
exports.PDA_ACCOUNT_CREATION_COST = 2040000;
|
|
7
|
-
exports.SOLANA_RANDOM_ADDRESS = '2zHKF6tqam3tnNFPK2E9nBDkV7GMXnvdJautmzqQdn8A';
|
|
8
|
-
exports.TOKEN_2022_PROGRAM_ID = 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb';
|
|
9
|
-
// Jupiter use 40% as a compute unit margin while calculating them, some TX reverts without this, as we add some compute units
|
|
10
|
-
// for account creations and referral, for stability purposes we add 60% margin
|
|
11
|
-
exports.COMPUTE_UNIT_MARGIN_MULTIPLIER = 1.6;
|
|
12
|
-
exports.SHAPESHIFT_JUPITER_REFERRAL_KEY = 'Ajgmo453yGmcHDPoJBrMUj3GFwLVL7HaaZGNLkB8vREG';
|
|
13
|
-
exports.JUPITER_AFFILIATE_CONTRACT_ADDRESS = 'REFER4ZgmyYx9c6He5XfaTMiGfdLwRnkV4RPp9t9iF3';
|
|
14
|
-
exports.JUPITER_REFERALL_FEE_PROJECT_ACCOUNT = '45ruCyfdRkWpRNGEqWzjCiXRHkZs8WXCLQ67Pnpye7Hp';
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import type { QuoteResponse, SwapInstructionsResponse } from '@jup-ag/api';
|
|
2
|
-
import type { ChainId } from '@shapeshiftoss/caip';
|
|
3
|
-
import type { solana } from '@shapeshiftoss/chain-adapters';
|
|
4
|
-
import type { Asset, KnownChainIds } from '@shapeshiftoss/types';
|
|
5
|
-
import type { Result } from '@sniptt/monads';
|
|
6
|
-
import type { Connection } from '@solana/web3.js';
|
|
7
|
-
import { PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
8
|
-
import type { AxiosResponse } from 'axios';
|
|
9
|
-
import type { SwapErrorRight } from '../../../types';
|
|
10
|
-
export declare const isSupportedChainId: (chainId: ChainId) => chainId is KnownChainIds.SolanaMainnet;
|
|
11
|
-
type GetJupiterQuoteArgs = {
|
|
12
|
-
apiUrl: string;
|
|
13
|
-
sourceAssetAddress: string;
|
|
14
|
-
destinationAssetAddress: string;
|
|
15
|
-
commissionBps: string;
|
|
16
|
-
amount: string;
|
|
17
|
-
slippageBps: string | undefined;
|
|
18
|
-
};
|
|
19
|
-
type GetJupiterSwapArgs = {
|
|
20
|
-
apiUrl: string;
|
|
21
|
-
fromAddress: string;
|
|
22
|
-
rawQuote: unknown;
|
|
23
|
-
toAddress?: string;
|
|
24
|
-
feeAccount: string | undefined;
|
|
25
|
-
};
|
|
26
|
-
type CreateInstructionsParams = {
|
|
27
|
-
priceResponse: QuoteResponse;
|
|
28
|
-
sendAddress: string;
|
|
29
|
-
receiveAddress?: string;
|
|
30
|
-
affiliateBps: string;
|
|
31
|
-
buyAsset: Asset;
|
|
32
|
-
sellAsset: any;
|
|
33
|
-
adapter: solana.ChainAdapter;
|
|
34
|
-
jupiterUrl: string;
|
|
35
|
-
};
|
|
36
|
-
export declare const getJupiterPrice: ({ apiUrl, sourceAssetAddress, destinationAssetAddress, commissionBps, amount, slippageBps, }: GetJupiterQuoteArgs) => Promise<Result<AxiosResponse<QuoteResponse, any>, SwapErrorRight>>;
|
|
37
|
-
export declare const getJupiterSwapInstructions: ({ apiUrl, fromAddress, toAddress, rawQuote, feeAccount, }: GetJupiterSwapArgs) => Promise<Result<AxiosResponse<SwapInstructionsResponse, any>, SwapErrorRight>>;
|
|
38
|
-
export declare const getFeeTokenAccountAndInstruction: ({ feePayerPubKey, programId, buyAssetReferralPubKey, sellAssetReferralPubKey, buyTokenId, sellTokenId, instructionData, connection, }: {
|
|
39
|
-
feePayerPubKey: PublicKey;
|
|
40
|
-
programId: PublicKey;
|
|
41
|
-
buyAssetReferralPubKey: PublicKey;
|
|
42
|
-
sellAssetReferralPubKey: PublicKey;
|
|
43
|
-
buyTokenId: string;
|
|
44
|
-
sellTokenId: string;
|
|
45
|
-
instructionData: Buffer;
|
|
46
|
-
connection: Connection;
|
|
47
|
-
}) => Promise<{
|
|
48
|
-
tokenAccount?: PublicKey;
|
|
49
|
-
instruction?: TransactionInstruction | undefined;
|
|
50
|
-
}>;
|
|
51
|
-
export declare const calculateAccountCreationCosts: (instructions: TransactionInstruction[]) => string;
|
|
52
|
-
export declare const createSwapInstructions: ({ priceResponse, sendAddress, receiveAddress, affiliateBps, buyAsset, sellAsset, adapter, jupiterUrl, }: CreateInstructionsParams) => Promise<{
|
|
53
|
-
instructions: TransactionInstruction[];
|
|
54
|
-
addressLookupTableAddresses: string[];
|
|
55
|
-
}>;
|
|
56
|
-
export {};
|