@shapeshiftoss/swapper 17.7.3 → 17.8.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.
Files changed (124) hide show
  1. package/dist/cjs/.tsbuildinfo +1 -1
  2. package/dist/cjs/constants.js +35 -26
  3. package/dist/cjs/index.d.ts +1 -0
  4. package/dist/cjs/index.js +1 -0
  5. package/dist/cjs/swappers/ArbitrumBridgeSwapper/getTradeQuote/getTradeQuote.test.js +12 -4
  6. package/dist/cjs/swappers/BebopSwapper/getBebopSolanaTradeRate/getBebopSolanaTradeRate.js +9 -5
  7. package/dist/cjs/swappers/BebopSwapper/getBebopTradeQuote/getBebopTradeQuote.js +1 -9
  8. package/dist/cjs/swappers/BebopSwapper/getBebopTradeRate/getBebopTradeRate.js +9 -13
  9. package/dist/cjs/swappers/BebopSwapper/types.d.ts +64 -78
  10. package/dist/cjs/swappers/BebopSwapper/utils/fetchFromBebop.d.ts +0 -16
  11. package/dist/cjs/swappers/BebopSwapper/utils/fetchFromBebop.js +49 -82
  12. package/dist/cjs/swappers/BobGatewaySwapper/BobGatewaySwapper.d.ts +2 -0
  13. package/dist/cjs/swappers/BobGatewaySwapper/BobGatewaySwapper.js +8 -0
  14. package/dist/cjs/swappers/BobGatewaySwapper/endpoints.d.ts +2 -0
  15. package/dist/cjs/swappers/BobGatewaySwapper/endpoints.js +143 -0
  16. package/dist/cjs/swappers/BobGatewaySwapper/index.d.ts +3 -0
  17. package/dist/cjs/swappers/BobGatewaySwapper/index.js +22 -0
  18. package/dist/cjs/swappers/BobGatewaySwapper/swapperApi/getTradeQuote.d.ts +3 -0
  19. package/dist/cjs/swappers/BobGatewaySwapper/swapperApi/getTradeQuote.js +90 -0
  20. package/dist/cjs/swappers/BobGatewaySwapper/swapperApi/getTradeRate.d.ts +3 -0
  21. package/dist/cjs/swappers/BobGatewaySwapper/swapperApi/getTradeRate.js +73 -0
  22. package/dist/cjs/swappers/BobGatewaySwapper/types.d.ts +20 -0
  23. package/dist/cjs/swappers/BobGatewaySwapper/types.js +2 -0
  24. package/dist/cjs/swappers/BobGatewaySwapper/utils/constants.d.ts +13 -0
  25. package/dist/cjs/swappers/BobGatewaySwapper/utils/constants.js +31 -0
  26. package/dist/cjs/swappers/BobGatewaySwapper/utils/helpers.d.ts +49 -0
  27. package/dist/cjs/swappers/BobGatewaySwapper/utils/helpers.js +314 -0
  28. package/dist/cjs/swappers/ChainflipSwapper/ChainflipSwapper.js +1 -0
  29. package/dist/cjs/swappers/ChainflipSwapper/constants.d.ts +1 -1
  30. package/dist/cjs/swappers/ChainflipSwapper/constants.js +3 -0
  31. package/dist/cjs/swappers/ChainflipSwapper/endpoints.js +20 -0
  32. package/dist/cjs/swappers/ChainflipSwapper/types.d.ts +2 -1
  33. package/dist/cjs/swappers/ChainflipSwapper/types.js +1 -0
  34. package/dist/cjs/swappers/ChainflipSwapper/utils/getQuoteOrRate.js +23 -3
  35. package/dist/cjs/swappers/ChainflipSwapper/utils/helpers.d.ts +2 -2
  36. package/dist/cjs/swappers/ChainflipSwapper/utils/helpers.js +2 -1
  37. package/dist/cjs/swappers/CowSwapper/utils/helpers/helpers.test.js +3 -3
  38. package/dist/cjs/swappers/RelaySwapper/constant.js +1 -1
  39. package/dist/cjs/swappers/SunioSwapper/endpoints.js +19 -36
  40. package/dist/cjs/swappers/SunioSwapper/utils/buildSwapContractCall.d.ts +6 -0
  41. package/dist/cjs/swappers/SunioSwapper/utils/buildSwapContractCall.js +33 -0
  42. package/dist/cjs/swappers/SunioSwapper/utils/buildSwapRouteParameters.js +6 -6
  43. package/dist/cjs/swappers/SunioSwapper/utils/constants.d.ts +2 -0
  44. package/dist/cjs/swappers/SunioSwapper/utils/constants.js +7 -1
  45. package/dist/cjs/swappers/SunioSwapper/utils/estimateSunioNetworkFee.d.ts +12 -0
  46. package/dist/cjs/swappers/SunioSwapper/utils/estimateSunioNetworkFee.js +78 -0
  47. package/dist/cjs/swappers/SunioSwapper/utils/getQuoteOrRate.js +16 -74
  48. package/dist/cjs/swappers/SunioSwapper/utils/getSunioTransactionFees.d.ts +2 -0
  49. package/dist/cjs/swappers/SunioSwapper/utils/getSunioTransactionFees.js +36 -0
  50. package/dist/cjs/swappers/utils/helpers/helpers.js +1 -0
  51. package/dist/cjs/thorchain-utils/getL1RateOrQuote.js +5 -2
  52. package/dist/cjs/thorchain-utils/getQuote.js +27 -29
  53. package/dist/cjs/thorchain-utils/tron/getTronTransactionFees.js +8 -5
  54. package/dist/cjs/thorchain-utils/types.d.ts +1 -1
  55. package/dist/cjs/types.d.ts +10 -3
  56. package/dist/cjs/types.js +5 -0
  57. package/dist/esm/.tsbuildinfo +1 -1
  58. package/dist/esm/constants.js +9 -0
  59. package/dist/esm/index.d.ts +1 -0
  60. package/dist/esm/index.js +1 -0
  61. package/dist/esm/swappers/ArbitrumBridgeSwapper/getTradeQuote/getTradeQuote.test.js +12 -4
  62. package/dist/esm/swappers/BebopSwapper/getBebopSolanaTradeRate/getBebopSolanaTradeRate.js +10 -6
  63. package/dist/esm/swappers/BebopSwapper/getBebopTradeQuote/getBebopTradeQuote.js +1 -9
  64. package/dist/esm/swappers/BebopSwapper/getBebopTradeRate/getBebopTradeRate.js +10 -14
  65. package/dist/esm/swappers/BebopSwapper/types.d.ts +64 -78
  66. package/dist/esm/swappers/BebopSwapper/utils/fetchFromBebop.d.ts +0 -16
  67. package/dist/esm/swappers/BebopSwapper/utils/fetchFromBebop.js +45 -76
  68. package/dist/esm/swappers/BobGatewaySwapper/BobGatewaySwapper.d.ts +2 -0
  69. package/dist/esm/swappers/BobGatewaySwapper/BobGatewaySwapper.js +5 -0
  70. package/dist/esm/swappers/BobGatewaySwapper/endpoints.d.ts +2 -0
  71. package/dist/esm/swappers/BobGatewaySwapper/endpoints.js +140 -0
  72. package/dist/esm/swappers/BobGatewaySwapper/index.d.ts +3 -0
  73. package/dist/esm/swappers/BobGatewaySwapper/index.js +3 -0
  74. package/dist/esm/swappers/BobGatewaySwapper/swapperApi/getTradeQuote.d.ts +3 -0
  75. package/dist/esm/swappers/BobGatewaySwapper/swapperApi/getTradeQuote.js +86 -0
  76. package/dist/esm/swappers/BobGatewaySwapper/swapperApi/getTradeRate.d.ts +3 -0
  77. package/dist/esm/swappers/BobGatewaySwapper/swapperApi/getTradeRate.js +69 -0
  78. package/dist/esm/swappers/BobGatewaySwapper/types.d.ts +20 -0
  79. package/dist/esm/swappers/BobGatewaySwapper/utils/constants.d.ts +13 -0
  80. package/dist/esm/swappers/BobGatewaySwapper/utils/constants.js +27 -0
  81. package/dist/esm/swappers/BobGatewaySwapper/utils/helpers.d.ts +49 -0
  82. package/dist/esm/swappers/BobGatewaySwapper/utils/helpers.js +299 -0
  83. package/dist/esm/swappers/ChainflipSwapper/ChainflipSwapper.js +2 -1
  84. package/dist/esm/swappers/ChainflipSwapper/constants.d.ts +1 -1
  85. package/dist/esm/swappers/ChainflipSwapper/constants.js +4 -1
  86. package/dist/esm/swappers/ChainflipSwapper/endpoints.js +20 -0
  87. package/dist/esm/swappers/ChainflipSwapper/types.d.ts +2 -1
  88. package/dist/esm/swappers/ChainflipSwapper/types.js +1 -0
  89. package/dist/esm/swappers/ChainflipSwapper/utils/getQuoteOrRate.js +24 -4
  90. package/dist/esm/swappers/ChainflipSwapper/utils/helpers.d.ts +2 -2
  91. package/dist/esm/swappers/ChainflipSwapper/utils/helpers.js +2 -1
  92. package/dist/esm/swappers/CowSwapper/utils/helpers/helpers.test.js +3 -3
  93. package/dist/esm/swappers/RelaySwapper/constant.js +1 -1
  94. package/dist/esm/swappers/SunioSwapper/endpoints.js +19 -36
  95. package/dist/esm/swappers/SunioSwapper/utils/buildSwapContractCall.d.ts +6 -0
  96. package/dist/esm/swappers/SunioSwapper/utils/buildSwapContractCall.js +29 -0
  97. package/dist/esm/swappers/SunioSwapper/utils/buildSwapRouteParameters.js +6 -6
  98. package/dist/esm/swappers/SunioSwapper/utils/constants.d.ts +2 -0
  99. package/dist/esm/swappers/SunioSwapper/utils/constants.js +6 -0
  100. package/dist/esm/swappers/SunioSwapper/utils/estimateSunioNetworkFee.d.ts +12 -0
  101. package/dist/esm/swappers/SunioSwapper/utils/estimateSunioNetworkFee.js +74 -0
  102. package/dist/esm/swappers/SunioSwapper/utils/getQuoteOrRate.js +16 -74
  103. package/dist/esm/swappers/SunioSwapper/utils/getSunioTransactionFees.d.ts +2 -0
  104. package/dist/esm/swappers/SunioSwapper/utils/getSunioTransactionFees.js +32 -0
  105. package/dist/esm/swappers/utils/helpers/helpers.js +2 -1
  106. package/dist/esm/thorchain-utils/getL1RateOrQuote.js +5 -2
  107. package/dist/esm/thorchain-utils/getQuote.js +27 -29
  108. package/dist/esm/thorchain-utils/tron/getTronTransactionFees.js +8 -5
  109. package/dist/esm/thorchain-utils/types.d.ts +1 -1
  110. package/dist/esm/types.d.ts +10 -3
  111. package/dist/esm/types.js +5 -0
  112. package/package.json +8 -7
  113. package/dist/cjs/swappers/SunioSwapper/utils/buildSwapTransaction.d.ts +0 -11
  114. package/dist/cjs/swappers/SunioSwapper/utils/buildSwapTransaction.js +0 -57
  115. package/dist/cjs/thorchain-utils/getThresholdedAffiliateBps/getThresholdedAffiliateBps.d.ts +0 -12
  116. package/dist/cjs/thorchain-utils/getThresholdedAffiliateBps/getThresholdedAffiliateBps.js +0 -47
  117. package/dist/cjs/thorchain-utils/getThresholdedAffiliateBps/getThresholdedAffiliateBps.test.js +0 -100
  118. package/dist/esm/swappers/SunioSwapper/utils/buildSwapTransaction.d.ts +0 -11
  119. package/dist/esm/swappers/SunioSwapper/utils/buildSwapTransaction.js +0 -53
  120. package/dist/esm/thorchain-utils/getThresholdedAffiliateBps/getThresholdedAffiliateBps.d.ts +0 -12
  121. package/dist/esm/thorchain-utils/getThresholdedAffiliateBps/getThresholdedAffiliateBps.js +0 -41
  122. package/dist/esm/thorchain-utils/getThresholdedAffiliateBps/getThresholdedAffiliateBps.test.d.ts +0 -1
  123. package/dist/esm/thorchain-utils/getThresholdedAffiliateBps/getThresholdedAffiliateBps.test.js +0 -98
  124. /package/dist/{cjs/thorchain-utils/getThresholdedAffiliateBps/getThresholdedAffiliateBps.test.d.ts → esm/swappers/BobGatewaySwapper/types.js} +0 -0
@@ -1,57 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildSunioSwapTransaction = void 0;
4
- const tronweb_1 = require("tronweb");
5
- const constants_1 = require("./constants");
6
- const convertAddressesToEvmFormat = (value) => {
7
- if (Array.isArray(value)) {
8
- return value.map(v => convertAddressesToEvmFormat(v));
9
- }
10
- if (typeof value === 'string' && value.startsWith('T') && tronweb_1.TronWeb.isAddress(value)) {
11
- const hex = tronweb_1.TronWeb.address.toHex(value);
12
- return hex.replace(/^41/, '0x');
13
- }
14
- return value;
15
- };
16
- const buildSunioSwapTransaction = async (args) => {
17
- const { route, from, to, sellAmountCryptoBaseUnit, minBuyAmountCryptoBaseUnit, rpcUrl, deadline, } = args;
18
- const tronWeb = new tronweb_1.TronWeb({
19
- fullHost: rpcUrl,
20
- });
21
- const path = route.tokens;
22
- const poolVersion = route.poolVersions;
23
- const versionLen = Array(poolVersion.length).fill(2);
24
- const fees = route.poolFees.map(fee => Number(fee));
25
- const swapData = {
26
- amountIn: sellAmountCryptoBaseUnit,
27
- amountOutMin: minBuyAmountCryptoBaseUnit,
28
- recipient: to,
29
- deadline: deadline ?? Math.floor(Date.now() / 1000) + 60 * 20,
30
- };
31
- const parameters = [
32
- { type: 'address[]', value: path },
33
- { type: 'string[]', value: poolVersion },
34
- { type: 'uint256[]', value: versionLen },
35
- { type: 'uint24[]', value: fees },
36
- {
37
- type: 'tuple(uint256,uint256,address,uint256)',
38
- value: convertAddressesToEvmFormat([
39
- swapData.amountIn,
40
- swapData.amountOutMin,
41
- swapData.recipient,
42
- swapData.deadline,
43
- ]),
44
- },
45
- ];
46
- const functionSelector = 'swapExactInput(address[],string[],uint256[],uint24[],(uint256,uint256,address,uint256))';
47
- const options = {
48
- feeLimit: 100_000_000,
49
- callValue: 0,
50
- };
51
- const txData = await tronWeb.transactionBuilder.triggerSmartContract(constants_1.SUNIO_SMART_ROUTER_CONTRACT, functionSelector, options, parameters, from);
52
- if (!txData.result || !txData.result.result) {
53
- throw new Error('[Sun.io] Failed to build swap transaction');
54
- }
55
- return txData.transaction;
56
- };
57
- exports.buildSunioSwapTransaction = buildSunioSwapTransaction;
@@ -1,12 +0,0 @@
1
- import type { AssetId } from '@shapeshiftoss/caip';
2
- import type { Asset } from '@shapeshiftoss/types';
3
- import type { SwapperConfig, SwapperName } from '../../types';
4
- export declare const getOutboundFeeSellAssetThorBaseUnit: (runePerAsset: string, sellAssetId: AssetId, swapperName: SwapperName) => BigNumber;
5
- export declare const getExpectedAffiliateFeeSellAssetThorBaseUnit: (sellAmountCryptoBaseUnit: string, sellAsset: Asset, affiliateBps: string, swapperName: SwapperName) => BigNumber;
6
- export declare const getThresholdedAffiliateBps: ({ sellAsset, affiliateBps, sellAmountCryptoBaseUnit, config, swapperName, }: {
7
- sellAsset: Asset;
8
- affiliateBps: string;
9
- sellAmountCryptoBaseUnit: string;
10
- config: SwapperConfig;
11
- swapperName: SwapperName;
12
- }) => Promise<string>;
@@ -1,47 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getThresholdedAffiliateBps = exports.getExpectedAffiliateFeeSellAssetThorBaseUnit = exports.getOutboundFeeSellAssetThorBaseUnit = void 0;
4
- const utils_1 = require("@shapeshiftoss/utils");
5
- const index_1 = require("../index");
6
- // Convert native fee to sell asset's native precision before dividing by `assetPrice`.
7
- // MAYA's CACAO is 10-dec but pool assets are 8-dec — skip this and the result is 100x off.
8
- const getOutboundFeeSellAssetThorBaseUnit = (runePerAsset, sellAssetId, swapperName) => {
9
- const nativeFeeSellAssetThorBaseUnit = (0, utils_1.convertPrecision)({
10
- value: (0, index_1.getNativeFee)(swapperName),
11
- inputExponent: (0, index_1.getNativePrecision)((0, index_1.getSwapperNativeAssetId)(swapperName), swapperName),
12
- outputExponent: (0, index_1.getNativePrecision)(sellAssetId, swapperName),
13
- });
14
- return nativeFeeSellAssetThorBaseUnit.dividedBy(runePerAsset);
15
- };
16
- exports.getOutboundFeeSellAssetThorBaseUnit = getOutboundFeeSellAssetThorBaseUnit;
17
- const getExpectedAffiliateFeeSellAssetThorBaseUnit = (sellAmountCryptoBaseUnit, sellAsset, affiliateBps, swapperName) => {
18
- const sellAmountThorBaseUnit = (0, utils_1.convertPrecision)({
19
- value: sellAmountCryptoBaseUnit,
20
- inputExponent: sellAsset.precision,
21
- outputExponent: (0, index_1.getNativePrecision)(sellAsset.assetId, swapperName),
22
- });
23
- const affiliatePercent = (0, utils_1.convertBasisPointsToDecimalPercentage)(affiliateBps);
24
- return sellAmountThorBaseUnit.times(affiliatePercent);
25
- };
26
- exports.getExpectedAffiliateFeeSellAssetThorBaseUnit = getExpectedAffiliateFeeSellAssetThorBaseUnit;
27
- // don't apply an affiliate fee if it's below the outbound fee for the inbound pool
28
- const getThresholdedAffiliateBps = async ({ sellAsset, affiliateBps, sellAmountCryptoBaseUnit, config, swapperName, }) => {
29
- const midgardUrl = (0, index_1.getMidgardUrl)(config, swapperName);
30
- const outboundFeeSellAssetThorBaseUnit = await (async () => {
31
- if ((0, index_1.isNativeAsset)(sellAsset.assetId, swapperName))
32
- return (0, index_1.getNativeFee)(swapperName);
33
- const sellPoolId = (0, index_1.getPoolAssetId)({ assetId: sellAsset.assetId, swapperName });
34
- // get pool data for the sell asset
35
- const res = await index_1.thorService.get(`${midgardUrl}/pool/${sellPoolId}`);
36
- if (res.isErr())
37
- throw res.unwrapErr();
38
- const pool = res.unwrap().data;
39
- // calculate the rune outbound fee denominated in the sell asset, in thor base units
40
- return (0, exports.getOutboundFeeSellAssetThorBaseUnit)(pool.assetPrice, sellAsset.assetId, swapperName);
41
- })();
42
- // calculate the expected affiliate fee, in thor base units
43
- const expectedAffiliateFeeSellAssetThorBaseUnit = (0, exports.getExpectedAffiliateFeeSellAssetThorBaseUnit)(sellAmountCryptoBaseUnit, sellAsset, affiliateBps, swapperName);
44
- const isAffiliateFeeBelowOutboundFee = expectedAffiliateFeeSellAssetThorBaseUnit.lte(outboundFeeSellAssetThorBaseUnit);
45
- return isAffiliateFeeBelowOutboundFee ? '0' : affiliateBps;
46
- };
47
- exports.getThresholdedAffiliateBps = getThresholdedAffiliateBps;
@@ -1,100 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
4
- const utils_1 = require("@shapeshiftoss/utils");
5
- const monads_1 = require("@sniptt/monads");
6
- const vitest_1 = require("vitest");
7
- const test_data_1 = require("../../swappers/utils/test-data");
8
- const types_1 = require("../../types");
9
- const service_1 = require("../service");
10
- const getThresholdedAffiliateBps_1 = require("./getThresholdedAffiliateBps");
11
- const mockedThorService = vitest_1.vi.mocked(service_1.thorService);
12
- const mocks = vitest_1.vi.hoisted(() => ({
13
- get: vitest_1.vi.fn(),
14
- post: vitest_1.vi.fn(),
15
- }));
16
- vitest_1.vi.mock('../service', () => {
17
- const mockAxios = {
18
- default: {
19
- create: vitest_1.vi.fn(() => ({
20
- get: mocks.get,
21
- post: mocks.post,
22
- })),
23
- },
24
- };
25
- return {
26
- thorService: mockAxios.default.create(),
27
- };
28
- });
29
- (0, vitest_1.describe)('getOutboundFeeSellAssetThorBaseUnit', () => {
30
- (0, vitest_1.it)('should return 0.02 rune denominated in the target asset, in thor units', () => {
31
- const assetPricePrecision = '4.00000'; // 1 token is 4x as valuable as 1 rune
32
- (0, vitest_1.expect)((0, getThresholdedAffiliateBps_1.getOutboundFeeSellAssetThorBaseUnit)(assetPricePrecision, test_data_1.ETH.assetId, types_1.SwapperName.Thorchain).toNumber()).toEqual(Number(chain_adapters_1.thorchain.NATIVE_FEE) / 4);
33
- });
34
- (0, vitest_1.it)('should scale CACAO 10-dec native fee down to non-CACAO 8-dec sell asset precision on MAYA', () => {
35
- // 1 eth = 100 cacao, NATIVE_FEE = 2e9 (10-dec CACAO = 0.2 CACAO)
36
- // 0.2 CACAO / 100 (CACAO/ETH) = 0.002 ETH = 200_000 at 8-dec ETH
37
- const assetPrice = '100';
38
- (0, vitest_1.expect)((0, getThresholdedAffiliateBps_1.getOutboundFeeSellAssetThorBaseUnit)(assetPrice, test_data_1.ETH.assetId, types_1.SwapperName.Mayachain).toNumber()).toEqual(200_000);
39
- });
40
- });
41
- (0, vitest_1.describe)('getExpectedAffiliateFeeSellAssetThorBaseUnit', () => {
42
- (0, vitest_1.it)('should return the correct affiliate fee in thor base units', () => {
43
- const sellAmountCryptoBaseUnit = '1000000000000000000'; // 1 eth
44
- const affiliateBps = '35';
45
- const sellAsset = test_data_1.ETH;
46
- const result = (0, getThresholdedAffiliateBps_1.getExpectedAffiliateFeeSellAssetThorBaseUnit)(sellAmountCryptoBaseUnit, sellAsset, affiliateBps, types_1.SwapperName.Thorchain);
47
- const expectation = (0, utils_1.bn)('100000000').times('0.0035').toFixed(0);
48
- (0, vitest_1.expect)(result.toFixed(0)).toEqual(expectation);
49
- });
50
- (0, vitest_1.it)('returns 0 when affiliateBps is 0', () => {
51
- const sellAmountCryptoBaseUnit = '1000000000000000000'; // 1 eth
52
- const affiliateBps = '0';
53
- const sellAsset = test_data_1.ETH;
54
- const result = (0, getThresholdedAffiliateBps_1.getExpectedAffiliateFeeSellAssetThorBaseUnit)(sellAmountCryptoBaseUnit, sellAsset, affiliateBps, types_1.SwapperName.Thorchain);
55
- const expectation = '0';
56
- (0, vitest_1.expect)(result.toFixed(0)).toEqual(expectation);
57
- });
58
- });
59
- (0, vitest_1.describe)('getThresholdedAffiliateBps', () => {
60
- mockedThorService.get.mockImplementation(() => {
61
- return Promise.resolve((0, monads_1.Ok)({
62
- data: {
63
- assetPrice: '100', // 1 eth = 100 rune
64
- },
65
- }));
66
- });
67
- // outbound fee threshold is 0.02 rune.
68
- // assuming eth price at 100 rune:
69
- // 0.02 rune = 0.02 / 100 = 0.0002 eth
70
- // give an affiliate bps of 50 (0.5%):
71
- // min input amount = threshold eth / affiliate percentage = 0.0002 eth / 0.5% = 0.04 eth
72
- (0, vitest_1.it)('should return 0 when the sell amount is less than or equal to the rune outbound fee', async () => {
73
- const sellAmountCryptoBaseUnit = '40000000000000000'; // 0.04 eth
74
- const affiliateBps = '50'; // 0.5%
75
- const sellAsset = test_data_1.ETH;
76
- const result = await (0, getThresholdedAffiliateBps_1.getThresholdedAffiliateBps)({
77
- sellAsset,
78
- affiliateBps,
79
- sellAmountCryptoBaseUnit,
80
- config: {},
81
- swapperName: types_1.SwapperName.Thorchain,
82
- });
83
- const expectation = '0';
84
- (0, vitest_1.expect)(result).toEqual(expectation);
85
- });
86
- (0, vitest_1.it)('should return input affiliate bps when the sell amount is greater than the rune outbound fee', async () => {
87
- const sellAmountCryptoBaseUnit = '40000000000000001'; // > 0.04 eth
88
- const affiliateBps = '50'; // 0.5%
89
- const sellAsset = test_data_1.ETH;
90
- const result = await (0, getThresholdedAffiliateBps_1.getThresholdedAffiliateBps)({
91
- sellAsset,
92
- affiliateBps,
93
- sellAmountCryptoBaseUnit,
94
- config: {},
95
- swapperName: types_1.SwapperName.Thorchain,
96
- });
97
- const expectation = affiliateBps;
98
- (0, vitest_1.expect)(result).toEqual(expectation);
99
- });
100
- });
@@ -1,11 +0,0 @@
1
- import type { SunioRoute } from '../types.js';
2
- export type BuildSwapTransactionArgs = {
3
- route: SunioRoute;
4
- from: string;
5
- to: string;
6
- sellAmountCryptoBaseUnit: string;
7
- minBuyAmountCryptoBaseUnit: string;
8
- rpcUrl: string;
9
- deadline?: number;
10
- };
11
- export declare const buildSunioSwapTransaction: (args: BuildSwapTransactionArgs) => Promise<any>;
@@ -1,53 +0,0 @@
1
- import { TronWeb } from 'tronweb';
2
- import { SUNIO_SMART_ROUTER_CONTRACT } from './constants.js';
3
- const convertAddressesToEvmFormat = (value) => {
4
- if (Array.isArray(value)) {
5
- return value.map(v => convertAddressesToEvmFormat(v));
6
- }
7
- if (typeof value === 'string' && value.startsWith('T') && TronWeb.isAddress(value)) {
8
- const hex = TronWeb.address.toHex(value);
9
- return hex.replace(/^41/, '0x');
10
- }
11
- return value;
12
- };
13
- export const buildSunioSwapTransaction = async (args) => {
14
- const { route, from, to, sellAmountCryptoBaseUnit, minBuyAmountCryptoBaseUnit, rpcUrl, deadline, } = args;
15
- const tronWeb = new TronWeb({
16
- fullHost: rpcUrl,
17
- });
18
- const path = route.tokens;
19
- const poolVersion = route.poolVersions;
20
- const versionLen = Array(poolVersion.length).fill(2);
21
- const fees = route.poolFees.map(fee => Number(fee));
22
- const swapData = {
23
- amountIn: sellAmountCryptoBaseUnit,
24
- amountOutMin: minBuyAmountCryptoBaseUnit,
25
- recipient: to,
26
- deadline: deadline ?? Math.floor(Date.now() / 1000) + 60 * 20,
27
- };
28
- const parameters = [
29
- { type: 'address[]', value: path },
30
- { type: 'string[]', value: poolVersion },
31
- { type: 'uint256[]', value: versionLen },
32
- { type: 'uint24[]', value: fees },
33
- {
34
- type: 'tuple(uint256,uint256,address,uint256)',
35
- value: convertAddressesToEvmFormat([
36
- swapData.amountIn,
37
- swapData.amountOutMin,
38
- swapData.recipient,
39
- swapData.deadline,
40
- ]),
41
- },
42
- ];
43
- const functionSelector = 'swapExactInput(address[],string[],uint256[],uint24[],(uint256,uint256,address,uint256))';
44
- const options = {
45
- feeLimit: 100_000_000,
46
- callValue: 0,
47
- };
48
- const txData = await tronWeb.transactionBuilder.triggerSmartContract(SUNIO_SMART_ROUTER_CONTRACT, functionSelector, options, parameters, from);
49
- if (!txData.result || !txData.result.result) {
50
- throw new Error('[Sun.io] Failed to build swap transaction');
51
- }
52
- return txData.transaction;
53
- };
@@ -1,12 +0,0 @@
1
- import type { AssetId } from '@shapeshiftoss/caip';
2
- import type { Asset } from '@shapeshiftoss/types';
3
- import type { SwapperConfig, SwapperName } from '../../types.js';
4
- export declare const getOutboundFeeSellAssetThorBaseUnit: (runePerAsset: string, sellAssetId: AssetId, swapperName: SwapperName) => BigNumber;
5
- export declare const getExpectedAffiliateFeeSellAssetThorBaseUnit: (sellAmountCryptoBaseUnit: string, sellAsset: Asset, affiliateBps: string, swapperName: SwapperName) => BigNumber;
6
- export declare const getThresholdedAffiliateBps: ({ sellAsset, affiliateBps, sellAmountCryptoBaseUnit, config, swapperName, }: {
7
- sellAsset: Asset;
8
- affiliateBps: string;
9
- sellAmountCryptoBaseUnit: string;
10
- config: SwapperConfig;
11
- swapperName: SwapperName;
12
- }) => Promise<string>;
@@ -1,41 +0,0 @@
1
- import { convertBasisPointsToDecimalPercentage, convertPrecision } from '@shapeshiftoss/utils';
2
- import { getMidgardUrl, getNativeFee, getNativePrecision, getPoolAssetId, getSwapperNativeAssetId, isNativeAsset, thorService, } from '../index.js';
3
- // Convert native fee to sell asset's native precision before dividing by `assetPrice`.
4
- // MAYA's CACAO is 10-dec but pool assets are 8-dec — skip this and the result is 100x off.
5
- export const getOutboundFeeSellAssetThorBaseUnit = (runePerAsset, sellAssetId, swapperName) => {
6
- const nativeFeeSellAssetThorBaseUnit = convertPrecision({
7
- value: getNativeFee(swapperName),
8
- inputExponent: getNativePrecision(getSwapperNativeAssetId(swapperName), swapperName),
9
- outputExponent: getNativePrecision(sellAssetId, swapperName),
10
- });
11
- return nativeFeeSellAssetThorBaseUnit.dividedBy(runePerAsset);
12
- };
13
- export const getExpectedAffiliateFeeSellAssetThorBaseUnit = (sellAmountCryptoBaseUnit, sellAsset, affiliateBps, swapperName) => {
14
- const sellAmountThorBaseUnit = convertPrecision({
15
- value: sellAmountCryptoBaseUnit,
16
- inputExponent: sellAsset.precision,
17
- outputExponent: getNativePrecision(sellAsset.assetId, swapperName),
18
- });
19
- const affiliatePercent = convertBasisPointsToDecimalPercentage(affiliateBps);
20
- return sellAmountThorBaseUnit.times(affiliatePercent);
21
- };
22
- // don't apply an affiliate fee if it's below the outbound fee for the inbound pool
23
- export const getThresholdedAffiliateBps = async ({ sellAsset, affiliateBps, sellAmountCryptoBaseUnit, config, swapperName, }) => {
24
- const midgardUrl = getMidgardUrl(config, swapperName);
25
- const outboundFeeSellAssetThorBaseUnit = await (async () => {
26
- if (isNativeAsset(sellAsset.assetId, swapperName))
27
- return getNativeFee(swapperName);
28
- const sellPoolId = getPoolAssetId({ assetId: sellAsset.assetId, swapperName });
29
- // get pool data for the sell asset
30
- const res = await thorService.get(`${midgardUrl}/pool/${sellPoolId}`);
31
- if (res.isErr())
32
- throw res.unwrapErr();
33
- const pool = res.unwrap().data;
34
- // calculate the rune outbound fee denominated in the sell asset, in thor base units
35
- return getOutboundFeeSellAssetThorBaseUnit(pool.assetPrice, sellAsset.assetId, swapperName);
36
- })();
37
- // calculate the expected affiliate fee, in thor base units
38
- const expectedAffiliateFeeSellAssetThorBaseUnit = getExpectedAffiliateFeeSellAssetThorBaseUnit(sellAmountCryptoBaseUnit, sellAsset, affiliateBps, swapperName);
39
- const isAffiliateFeeBelowOutboundFee = expectedAffiliateFeeSellAssetThorBaseUnit.lte(outboundFeeSellAssetThorBaseUnit);
40
- return isAffiliateFeeBelowOutboundFee ? '0' : affiliateBps;
41
- };
@@ -1,98 +0,0 @@
1
- import { thorchain } from '@shapeshiftoss/chain-adapters';
2
- import { bn } from '@shapeshiftoss/utils';
3
- import { Ok } from '@sniptt/monads';
4
- import { describe, expect, it, vi } from 'vitest';
5
- import { ETH } from '../../swappers/utils/test-data/index.js';
6
- import { SwapperName } from '../../types.js';
7
- import { thorService } from '../service.js';
8
- import { getExpectedAffiliateFeeSellAssetThorBaseUnit, getOutboundFeeSellAssetThorBaseUnit, getThresholdedAffiliateBps, } from './getThresholdedAffiliateBps.js';
9
- const mockedThorService = vi.mocked(thorService);
10
- const mocks = vi.hoisted(() => ({
11
- get: vi.fn(),
12
- post: vi.fn(),
13
- }));
14
- vi.mock('../service', () => {
15
- const mockAxios = {
16
- default: {
17
- create: vi.fn(() => ({
18
- get: mocks.get,
19
- post: mocks.post,
20
- })),
21
- },
22
- };
23
- return {
24
- thorService: mockAxios.default.create(),
25
- };
26
- });
27
- describe('getOutboundFeeSellAssetThorBaseUnit', () => {
28
- it('should return 0.02 rune denominated in the target asset, in thor units', () => {
29
- const assetPricePrecision = '4.00000'; // 1 token is 4x as valuable as 1 rune
30
- expect(getOutboundFeeSellAssetThorBaseUnit(assetPricePrecision, ETH.assetId, SwapperName.Thorchain).toNumber()).toEqual(Number(thorchain.NATIVE_FEE) / 4);
31
- });
32
- it('should scale CACAO 10-dec native fee down to non-CACAO 8-dec sell asset precision on MAYA', () => {
33
- // 1 eth = 100 cacao, NATIVE_FEE = 2e9 (10-dec CACAO = 0.2 CACAO)
34
- // 0.2 CACAO / 100 (CACAO/ETH) = 0.002 ETH = 200_000 at 8-dec ETH
35
- const assetPrice = '100';
36
- expect(getOutboundFeeSellAssetThorBaseUnit(assetPrice, ETH.assetId, SwapperName.Mayachain).toNumber()).toEqual(200_000);
37
- });
38
- });
39
- describe('getExpectedAffiliateFeeSellAssetThorBaseUnit', () => {
40
- it('should return the correct affiliate fee in thor base units', () => {
41
- const sellAmountCryptoBaseUnit = '1000000000000000000'; // 1 eth
42
- const affiliateBps = '35';
43
- const sellAsset = ETH;
44
- const result = getExpectedAffiliateFeeSellAssetThorBaseUnit(sellAmountCryptoBaseUnit, sellAsset, affiliateBps, SwapperName.Thorchain);
45
- const expectation = bn('100000000').times('0.0035').toFixed(0);
46
- expect(result.toFixed(0)).toEqual(expectation);
47
- });
48
- it('returns 0 when affiliateBps is 0', () => {
49
- const sellAmountCryptoBaseUnit = '1000000000000000000'; // 1 eth
50
- const affiliateBps = '0';
51
- const sellAsset = ETH;
52
- const result = getExpectedAffiliateFeeSellAssetThorBaseUnit(sellAmountCryptoBaseUnit, sellAsset, affiliateBps, SwapperName.Thorchain);
53
- const expectation = '0';
54
- expect(result.toFixed(0)).toEqual(expectation);
55
- });
56
- });
57
- describe('getThresholdedAffiliateBps', () => {
58
- mockedThorService.get.mockImplementation(() => {
59
- return Promise.resolve(Ok({
60
- data: {
61
- assetPrice: '100', // 1 eth = 100 rune
62
- },
63
- }));
64
- });
65
- // outbound fee threshold is 0.02 rune.
66
- // assuming eth price at 100 rune:
67
- // 0.02 rune = 0.02 / 100 = 0.0002 eth
68
- // give an affiliate bps of 50 (0.5%):
69
- // min input amount = threshold eth / affiliate percentage = 0.0002 eth / 0.5% = 0.04 eth
70
- it('should return 0 when the sell amount is less than or equal to the rune outbound fee', async () => {
71
- const sellAmountCryptoBaseUnit = '40000000000000000'; // 0.04 eth
72
- const affiliateBps = '50'; // 0.5%
73
- const sellAsset = ETH;
74
- const result = await getThresholdedAffiliateBps({
75
- sellAsset,
76
- affiliateBps,
77
- sellAmountCryptoBaseUnit,
78
- config: {},
79
- swapperName: SwapperName.Thorchain,
80
- });
81
- const expectation = '0';
82
- expect(result).toEqual(expectation);
83
- });
84
- it('should return input affiliate bps when the sell amount is greater than the rune outbound fee', async () => {
85
- const sellAmountCryptoBaseUnit = '40000000000000001'; // > 0.04 eth
86
- const affiliateBps = '50'; // 0.5%
87
- const sellAsset = ETH;
88
- const result = await getThresholdedAffiliateBps({
89
- sellAsset,
90
- affiliateBps,
91
- sellAmountCryptoBaseUnit,
92
- config: {},
93
- swapperName: SwapperName.Thorchain,
94
- });
95
- const expectation = affiliateBps;
96
- expect(result).toEqual(expectation);
97
- });
98
- });