@shapeshiftoss/swapper 17.4.3 → 17.6.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/api.d.ts +7 -0
- package/dist/api.js +1 -0
- package/dist/manager/testData.d.ts +1 -0
- package/dist/manager/testData.js +9 -1
- package/dist/swappers/thorchain/ThorchainSwapper.d.ts +1 -0
- package/dist/swappers/thorchain/ThorchainSwapper.js +1 -3
- package/dist/swappers/utils/helpers/helpers.d.ts +1 -1
- package/dist/swappers/utils/helpers/helpers.js +1 -1
- package/dist/swappers/utils/test-data/assets.d.ts +1 -0
- package/dist/swappers/utils/test-data/assets.js +14 -1
- package/dist/swappers/utils/test-data/setupSwapQuote.d.ts +1 -1
- package/dist/swappers/zrx/ZrxSwapper.d.ts +4 -4
- package/dist/swappers/zrx/ZrxSwapper.js +2 -0
- package/dist/swappers/zrx/getZrxTradeQuote/getZrxTradeQuote.js +7 -9
- package/dist/swappers/zrx/utils/helpers/helpers.d.ts +4 -3
- package/dist/swappers/zrx/utils/helpers/helpers.js +22 -11
- package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.js +2 -7
- package/package.json +1 -1
package/dist/api.d.ts
CHANGED
|
@@ -44,6 +44,12 @@ declare type ChainSpecificQuoteFeeData<T extends ChainId> = ChainSpecific<T, {
|
|
|
44
44
|
approvalFeeCryptoBaseUnit?: string;
|
|
45
45
|
totalFee?: string;
|
|
46
46
|
};
|
|
47
|
+
[KnownChainIds.BnbSmartChainMainnet]: {
|
|
48
|
+
estimatedGas?: string;
|
|
49
|
+
gasPriceCryptoBaseUnit?: string;
|
|
50
|
+
approvalFeeCryptoBaseUnit?: string;
|
|
51
|
+
totalFee?: string;
|
|
52
|
+
};
|
|
47
53
|
[KnownChainIds.BitcoinMainnet]: {
|
|
48
54
|
byteCount: string;
|
|
49
55
|
satsPerByte: string;
|
|
@@ -182,6 +188,7 @@ export declare enum SwapperType {
|
|
|
182
188
|
ZrxEthereum = "0xEthereum",
|
|
183
189
|
ZrxAvalanche = "0xAvalanche",
|
|
184
190
|
ZrxOptimism = "0xOptimism",
|
|
191
|
+
ZrxBnbSmartChain = "0xBnbSmartChain",
|
|
185
192
|
Thorchain = "Thorchain",
|
|
186
193
|
Osmosis = "Osmosis",
|
|
187
194
|
CowSwap = "CoW Swap",
|
package/dist/api.js
CHANGED
|
@@ -18,6 +18,7 @@ var SwapperType;
|
|
|
18
18
|
SwapperType["ZrxEthereum"] = "0xEthereum";
|
|
19
19
|
SwapperType["ZrxAvalanche"] = "0xAvalanche";
|
|
20
20
|
SwapperType["ZrxOptimism"] = "0xOptimism";
|
|
21
|
+
SwapperType["ZrxBnbSmartChain"] = "0xBnbSmartChain";
|
|
21
22
|
SwapperType["Thorchain"] = "Thorchain";
|
|
22
23
|
SwapperType["Osmosis"] = "Osmosis";
|
|
23
24
|
SwapperType["CowSwap"] = "CoW Swap";
|
|
@@ -4,6 +4,7 @@ import { CowSwapper, ThorchainSwapper, ZrxSwapper } from '../swappers';
|
|
|
4
4
|
export declare const getZrxEthereumSwapper: () => ZrxSwapper<import("../swappers").ZrxSupportedChainId>;
|
|
5
5
|
export declare const getZrxAvalancheSwapper: () => ZrxSwapper<import("../swappers").ZrxSupportedChainId>;
|
|
6
6
|
export declare const getZrxOptimismSwapper: () => ZrxSwapper<import("../swappers").ZrxSupportedChainId>;
|
|
7
|
+
export declare const getZrxBscwapper: () => ZrxSwapper<import("../swappers").ZrxSupportedChainId>;
|
|
7
8
|
export declare const getCowSwapper: () => CowSwapper;
|
|
8
9
|
export declare const getThorchainSwapper: () => ThorchainSwapper;
|
|
9
10
|
export declare const tradeQuote: TradeQuote<KnownChainIds.EthereumMainnet>;
|
package/dist/manager/testData.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.badTradeQuote = exports.goodTradeQuote = exports.tradeQuote = exports.getThorchainSwapper = exports.getCowSwapper = exports.getZrxOptimismSwapper = exports.getZrxAvalancheSwapper = exports.getZrxEthereumSwapper = void 0;
|
|
3
|
+
exports.badTradeQuote = exports.goodTradeQuote = exports.tradeQuote = exports.getThorchainSwapper = exports.getCowSwapper = exports.getZrxBscwapper = exports.getZrxOptimismSwapper = exports.getZrxAvalancheSwapper = exports.getZrxEthereumSwapper = void 0;
|
|
4
4
|
const types_1 = require("@shapeshiftoss/types");
|
|
5
5
|
const api_1 = require("../api");
|
|
6
6
|
const swappers_1 = require("../swappers");
|
|
@@ -29,6 +29,14 @@ const zrxOptimismSwapperDeps = {
|
|
|
29
29
|
};
|
|
30
30
|
const getZrxOptimismSwapper = () => new swappers_1.ZrxSwapper(zrxOptimismSwapperDeps);
|
|
31
31
|
exports.getZrxOptimismSwapper = getZrxOptimismSwapper;
|
|
32
|
+
const zrxBscSwapperDeps = {
|
|
33
|
+
web3: {},
|
|
34
|
+
adapter: {
|
|
35
|
+
getChainId: () => types_1.KnownChainIds.BnbSmartChainMainnet,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
const getZrxBscwapper = () => new swappers_1.ZrxSwapper(zrxBscSwapperDeps);
|
|
39
|
+
exports.getZrxBscwapper = getZrxBscwapper;
|
|
32
40
|
const cowSwapperDeps = {
|
|
33
41
|
apiUrl: 'https://api.cow.fi/mainnet/api/',
|
|
34
42
|
adapter: {
|
|
@@ -10,6 +10,7 @@ export * from './types';
|
|
|
10
10
|
export declare type ThorUtxoSupportedChainId = KnownChainIds.BitcoinMainnet | KnownChainIds.DogecoinMainnet | KnownChainIds.LitecoinMainnet | KnownChainIds.BitcoinCashMainnet;
|
|
11
11
|
export declare type ThorEvmSupportedChainId = KnownChainIds.EthereumMainnet | KnownChainIds.AvalancheMainnet;
|
|
12
12
|
export declare type ThorCosmosSdkSupportedChainId = KnownChainIds.ThorchainMainnet | KnownChainIds.CosmosMainnet;
|
|
13
|
+
export declare type ThorChainId = ThorCosmosSdkSupportedChainId | ThorEvmSupportedChainId | ThorUtxoSupportedChainId;
|
|
13
14
|
export declare class ThorchainSwapper implements Swapper<ChainId> {
|
|
14
15
|
readonly name = SwapperName.Thorchain;
|
|
15
16
|
private sellSupportedChainIds;
|
|
@@ -37,7 +37,6 @@ class ThorchainSwapper {
|
|
|
37
37
|
[types_1.KnownChainIds.CosmosMainnet]: true,
|
|
38
38
|
[types_1.KnownChainIds.ThorchainMainnet]: true,
|
|
39
39
|
[types_1.KnownChainIds.AvalancheMainnet]: true,
|
|
40
|
-
[types_1.KnownChainIds.OptimismMainnet]: false,
|
|
41
40
|
};
|
|
42
41
|
this.buySupportedChainIds = {
|
|
43
42
|
[types_1.KnownChainIds.EthereumMainnet]: true,
|
|
@@ -48,7 +47,6 @@ class ThorchainSwapper {
|
|
|
48
47
|
[types_1.KnownChainIds.CosmosMainnet]: true,
|
|
49
48
|
[types_1.KnownChainIds.ThorchainMainnet]: true,
|
|
50
49
|
[types_1.KnownChainIds.AvalancheMainnet]: true,
|
|
51
|
-
[types_1.KnownChainIds.OptimismMainnet]: false,
|
|
52
50
|
};
|
|
53
51
|
this.supportedSellAssetIds = [caip_1.thorchainAssetId];
|
|
54
52
|
this.supportedBuyAssetIds = [caip_1.thorchainAssetId];
|
|
@@ -66,7 +64,7 @@ class ThorchainSwapper {
|
|
|
66
64
|
const assetId = caip_1.adapters.poolAssetIdToAssetId(pool.asset);
|
|
67
65
|
if (!assetId)
|
|
68
66
|
return;
|
|
69
|
-
const
|
|
67
|
+
const chainId = (0, caip_1.fromAssetId)(assetId).chainId;
|
|
70
68
|
this.sellSupportedChainIds[chainId] && this.supportedSellAssetIds.push(assetId);
|
|
71
69
|
this.buySupportedChainIds[chainId] && this.supportedBuyAssetIds.push(assetId);
|
|
72
70
|
});
|
|
@@ -35,7 +35,7 @@ declare type GrantAllowanceArgs<T extends EvmChainId> = {
|
|
|
35
35
|
};
|
|
36
36
|
export declare const getERC20Allowance: ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress, }: GetERC20AllowanceArgs) => Promise<any>;
|
|
37
37
|
export declare const isApprovalRequired: ({ adapter, receiveAddress, allowanceContract, sellAsset, sellAmountExcludeFeeCryptoBaseUnit, web3, erc20AllowanceAbi, }: IsApprovalRequiredArgs) => Promise<boolean>;
|
|
38
|
-
export declare const grantAllowance: <T extends import("@shapeshiftoss/types").KnownChainIds.EthereumMainnet | import("@shapeshiftoss/types").KnownChainIds.AvalancheMainnet | import("@shapeshiftoss/types").KnownChainIds.OptimismMainnet>({ quote, wallet, adapter, erc20Abi, web3, }: GrantAllowanceArgs<T>) => Promise<string>;
|
|
38
|
+
export declare const grantAllowance: <T extends import("@shapeshiftoss/types").KnownChainIds.EthereumMainnet | import("@shapeshiftoss/types").KnownChainIds.AvalancheMainnet | import("@shapeshiftoss/types").KnownChainIds.OptimismMainnet | import("@shapeshiftoss/types").KnownChainIds.BnbSmartChainMainnet>({ quote, wallet, adapter, erc20Abi, web3, }: GrantAllowanceArgs<T>) => Promise<string>;
|
|
39
39
|
/**
|
|
40
40
|
* This function keeps 17 significant digits, so even if we try to trade 1 Billion of an
|
|
41
41
|
* ETH or ERC20, we still keep 7 decimal places.
|
|
@@ -63,7 +63,7 @@ const grantAllowance = async ({ quote, wallet, adapter, erc20Abi, web3, }) => {
|
|
|
63
63
|
accountNumber,
|
|
64
64
|
value: '0',
|
|
65
65
|
chainSpecific: {
|
|
66
|
-
|
|
66
|
+
tokenContractAddress: sellAssetErc20Address,
|
|
67
67
|
gasPrice: (0, web3_utils_1.numberToHex)(((_b = (_a = quote.feeData) === null || _a === void 0 ? void 0 : _a.chainSpecific) === null || _b === void 0 ? void 0 : _b.gasPriceCryptoBaseUnit) || 0),
|
|
68
68
|
gasLimit: (0, web3_utils_1.numberToHex)(((_d = (_c = quote.feeData) === null || _c === void 0 ? void 0 : _c.chainSpecific) === null || _d === void 0 ? void 0 : _d.estimatedGas) || 0),
|
|
69
69
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RUNE = exports.OPTIMISM = exports.AVAX = exports.USDC = exports.UNSUPPORTED = exports.ETH = exports.WBTC = exports.FOX = exports.WETH = exports.BTC = void 0;
|
|
3
|
+
exports.RUNE = exports.BSC = exports.OPTIMISM = exports.AVAX = exports.USDC = exports.UNSUPPORTED = exports.ETH = exports.WBTC = exports.FOX = exports.WETH = exports.BTC = void 0;
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
5
|
exports.BTC = {
|
|
6
6
|
assetId: 'bip122:000000000019d6689c085ae165831e93/slip44:0',
|
|
@@ -113,6 +113,19 @@ exports.OPTIMISM = {
|
|
|
113
113
|
explorerAddressLink: 'https://optimistic.etherscan.io/address/',
|
|
114
114
|
explorerTxLink: 'https://optimistic.etherscan.io/tx/',
|
|
115
115
|
};
|
|
116
|
+
exports.BSC = {
|
|
117
|
+
assetId: caip_1.bscAssetId,
|
|
118
|
+
chainId: caip_1.bscChainId,
|
|
119
|
+
name: 'BNB',
|
|
120
|
+
networkName: 'BNB Smart Chain',
|
|
121
|
+
symbol: 'BNB',
|
|
122
|
+
precision: 18,
|
|
123
|
+
color: '#F0B90B',
|
|
124
|
+
icon: 'https://assets.coingecko.com/coins/images/825/thumb/bnb-icon2_2x.png?1644979850',
|
|
125
|
+
explorer: 'https://bscscan.com',
|
|
126
|
+
explorerAddressLink: 'https://bscscan.com/address/',
|
|
127
|
+
explorerTxLink: 'https://bscscan.com/tx/',
|
|
128
|
+
};
|
|
116
129
|
exports.RUNE = {
|
|
117
130
|
assetId: caip_1.thorchainAssetId,
|
|
118
131
|
chainId: caip_1.thorchainChainId,
|
|
@@ -18,7 +18,7 @@ export declare const setupBuildTrade: () => {
|
|
|
18
18
|
accountNumber: number;
|
|
19
19
|
receiveAccountNumber?: number | undefined;
|
|
20
20
|
} & {
|
|
21
|
-
chainId: KnownChainIds.EthereumMainnet | KnownChainIds.AvalancheMainnet | KnownChainIds.OptimismMainnet;
|
|
21
|
+
chainId: KnownChainIds.EthereumMainnet | KnownChainIds.AvalancheMainnet | KnownChainIds.OptimismMainnet | KnownChainIds.BnbSmartChainMainnet;
|
|
22
22
|
} & {
|
|
23
23
|
slippage?: string | undefined;
|
|
24
24
|
wallet: HDWallet;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { Asset } from '@shapeshiftoss/asset-service';
|
|
2
2
|
import { AssetId, ChainId } from '@shapeshiftoss/caip';
|
|
3
|
-
import { avalanche, ethereum, optimism } from '@shapeshiftoss/chain-adapters';
|
|
3
|
+
import { avalanche, bnbsmartchain, ethereum, optimism } from '@shapeshiftoss/chain-adapters';
|
|
4
4
|
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
5
5
|
import { ApprovalNeededInput, ApprovalNeededOutput, ApproveAmountInput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, GetEvmTradeQuoteInput, Swapper, SwapperName, SwapperType, TradeQuote, TradeResult, TradeTxs } from '../../api';
|
|
6
6
|
import { ZrxExecuteTradeInput, ZrxSwapperDeps, ZrxTrade } from './types';
|
|
7
|
-
export declare type ZrxSupportedChainId = KnownChainIds.EthereumMainnet | KnownChainIds.AvalancheMainnet | KnownChainIds.OptimismMainnet;
|
|
8
|
-
export declare type ZrxSupportedChainAdapter = ethereum.ChainAdapter | avalanche.ChainAdapter | optimism.ChainAdapter;
|
|
7
|
+
export declare type ZrxSupportedChainId = KnownChainIds.EthereumMainnet | KnownChainIds.AvalancheMainnet | KnownChainIds.OptimismMainnet | KnownChainIds.BnbSmartChainMainnet;
|
|
8
|
+
export declare type ZrxSupportedChainAdapter = ethereum.ChainAdapter | avalanche.ChainAdapter | optimism.ChainAdapter | bnbsmartchain.ChainAdapter;
|
|
9
9
|
export declare class ZrxSwapper<T extends ZrxSupportedChainId> implements Swapper<T> {
|
|
10
10
|
readonly name = SwapperName.Zrx;
|
|
11
11
|
deps: ZrxSwapperDeps;
|
|
12
12
|
chainId: ChainId;
|
|
13
13
|
constructor(deps: ZrxSwapperDeps);
|
|
14
|
-
getType(): SwapperType.ZrxEthereum | SwapperType.ZrxAvalanche | SwapperType.ZrxOptimism;
|
|
14
|
+
getType(): SwapperType.ZrxEthereum | SwapperType.ZrxAvalanche | SwapperType.ZrxOptimism | SwapperType.ZrxBnbSmartChain;
|
|
15
15
|
buildTrade(args: BuildTradeInput): Promise<ZrxTrade<T>>;
|
|
16
16
|
getTradeQuote(input: GetEvmTradeQuoteInput): Promise<TradeQuote<T>>;
|
|
17
17
|
getUsdRate(input: Asset): Promise<string>;
|
|
@@ -25,6 +25,8 @@ class ZrxSwapper {
|
|
|
25
25
|
return api_1.SwapperType.ZrxAvalanche;
|
|
26
26
|
case types_1.KnownChainIds.OptimismMainnet:
|
|
27
27
|
return api_1.SwapperType.ZrxOptimism;
|
|
28
|
+
case types_1.KnownChainIds.BnbSmartChainMainnet:
|
|
29
|
+
return api_1.SwapperType.ZrxBnbSmartChain;
|
|
28
30
|
default:
|
|
29
31
|
throw new api_1.SwapError('[getType]', {
|
|
30
32
|
code: api_1.SwapErrorType.UNSUPPORTED_CHAIN,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getZrxTradeQuote = void 0;
|
|
4
|
-
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
4
|
const api_1 = require("../../../api");
|
|
6
5
|
const bignumber_1 = require("../../utils/bignumber");
|
|
7
6
|
const constants_1 = require("../../utils/constants");
|
|
@@ -19,11 +18,8 @@ async function getZrxTradeQuote(input) {
|
|
|
19
18
|
details: { buyAssetChainId: buyAsset.chainId, sellAssetChainId: sellAsset.chainId },
|
|
20
19
|
});
|
|
21
20
|
}
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const useSellAmount = !!sellAmountCryptoBaseUnit;
|
|
25
|
-
const buyToken = buyAssetNamespace === 'erc20' ? buyAssetErc20Address : buyAsset.symbol;
|
|
26
|
-
const sellToken = sellAssetNamespace === 'erc20' ? sellAssetErc20Address : sellAsset.symbol;
|
|
21
|
+
const buyToken = (0, helpers_2.assetToToken)(buyAsset);
|
|
22
|
+
const sellToken = (0, helpers_2.assetToToken)(sellAsset);
|
|
27
23
|
const { minimum, maximum } = await (0, getZrxMinMax_1.getZrxMinMax)(sellAsset, buyAsset);
|
|
28
24
|
const minQuotesellAmountCryptoBaseUnit = (0, bignumber_1.bnOrZero)(minimum).times((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
|
|
29
25
|
const normalizedSellAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(sellAmountCryptoBaseUnit).eq(0)
|
|
@@ -48,13 +44,15 @@ async function getZrxTradeQuote(input) {
|
|
|
48
44
|
},
|
|
49
45
|
});
|
|
50
46
|
const { data: { estimatedGas: estimatedGasResponse, gasPrice: gasPriceCryptoBaseUnit, price, sellAmount: sellAmountResponse, buyAmount, sources, allowanceTarget, }, } = quoteResponse;
|
|
51
|
-
const
|
|
47
|
+
const useSellAmount = !!sellAmountCryptoBaseUnit;
|
|
52
48
|
const rate = useSellAmount ? price : (0, bignumber_1.bn)(1).div(price).toString();
|
|
49
|
+
const estimatedGas = (0, bignumber_1.bnOrZero)(estimatedGasResponse).times(1.5);
|
|
53
50
|
const fee = estimatedGas.multipliedBy((0, bignumber_1.bnOrZero)(gasPriceCryptoBaseUnit)).toString();
|
|
54
51
|
// 0x approvals are cheaper than trades, but we don't have dynamic quote data for them.
|
|
55
52
|
// Instead, we use a hardcoded gasLimit estimate in place of the estimatedGas in the 0x quote response.
|
|
56
|
-
const approvalFeeCryptoBaseUnit =
|
|
57
|
-
|
|
53
|
+
const approvalFeeCryptoBaseUnit = (0, bignumber_1.bnOrZero)(constants_1.APPROVAL_GAS_LIMIT)
|
|
54
|
+
.multipliedBy((0, bignumber_1.bnOrZero)(gasPriceCryptoBaseUnit))
|
|
55
|
+
.toFixed();
|
|
58
56
|
const tradeQuote = {
|
|
59
57
|
rate,
|
|
60
58
|
minimumCryptoHuman: minimum,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Asset } from '@shapeshiftoss/asset-service';
|
|
2
|
-
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
+
import { AssetId, ChainId } from '@shapeshiftoss/caip';
|
|
3
3
|
export declare const baseUrlFromChainId: (chainId: string) => string;
|
|
4
|
-
export declare const
|
|
4
|
+
export declare const usdcContractAddressFromChainId: (chainId: ChainId) => string;
|
|
5
5
|
export declare const isNativeEvmAsset: (assetId: AssetId) => boolean;
|
|
6
|
-
export declare const
|
|
6
|
+
export declare const assetToToken: (asset: Asset) => string;
|
|
7
|
+
export declare const getUsdRate: (sellAsset: Asset) => Promise<string>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getUsdRate = exports.isNativeEvmAsset = exports.
|
|
3
|
+
exports.getUsdRate = exports.assetToToken = exports.isNativeEvmAsset = exports.usdcContractAddressFromChainId = exports.baseUrlFromChainId = void 0;
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
5
|
const types_1 = require("@shapeshiftoss/types");
|
|
6
6
|
const api_1 = require("../../../../api");
|
|
@@ -14,6 +14,8 @@ const baseUrlFromChainId = (chainId) => {
|
|
|
14
14
|
return 'https://avalanche.api.0x.org/';
|
|
15
15
|
case types_1.KnownChainIds.OptimismMainnet:
|
|
16
16
|
return 'https://optimism.api.0x.org/';
|
|
17
|
+
case types_1.KnownChainIds.BnbSmartChainMainnet:
|
|
18
|
+
return 'https://bsc.api.0x.org/';
|
|
17
19
|
default:
|
|
18
20
|
throw new api_1.SwapError(`baseUrlFromChainId] - Unsupported chainId: ${chainId}`, {
|
|
19
21
|
code: api_1.SwapErrorType.UNSUPPORTED_CHAIN,
|
|
@@ -21,7 +23,7 @@ const baseUrlFromChainId = (chainId) => {
|
|
|
21
23
|
}
|
|
22
24
|
};
|
|
23
25
|
exports.baseUrlFromChainId = baseUrlFromChainId;
|
|
24
|
-
const
|
|
26
|
+
const usdcContractAddressFromChainId = (chainId) => {
|
|
25
27
|
switch (chainId) {
|
|
26
28
|
case types_1.KnownChainIds.EthereumMainnet:
|
|
27
29
|
return '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
|
|
@@ -29,13 +31,15 @@ const usdcContractFromChainId = (chainId) => {
|
|
|
29
31
|
return '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e';
|
|
30
32
|
case types_1.KnownChainIds.OptimismMainnet:
|
|
31
33
|
return '0x7f5c764cbc14f9669b88837ca1490cca17c31607';
|
|
34
|
+
case types_1.KnownChainIds.BnbSmartChainMainnet:
|
|
35
|
+
return '0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d';
|
|
32
36
|
default:
|
|
33
37
|
throw new api_1.SwapError(`usdcContractFromChainId] - Unsupported chainId: ${chainId}`, {
|
|
34
38
|
code: api_1.SwapErrorType.UNSUPPORTED_CHAIN,
|
|
35
39
|
});
|
|
36
40
|
}
|
|
37
41
|
};
|
|
38
|
-
exports.
|
|
42
|
+
exports.usdcContractAddressFromChainId = usdcContractAddressFromChainId;
|
|
39
43
|
const isNativeEvmAsset = (assetId) => {
|
|
40
44
|
const { chainId } = (0, caip_1.fromAssetId)(assetId);
|
|
41
45
|
switch (chainId) {
|
|
@@ -45,25 +49,32 @@ const isNativeEvmAsset = (assetId) => {
|
|
|
45
49
|
return assetId === caip_1.avalancheAssetId;
|
|
46
50
|
case types_1.KnownChainIds.OptimismMainnet:
|
|
47
51
|
return assetId === caip_1.optimismAssetId;
|
|
52
|
+
case types_1.KnownChainIds.BnbSmartChainMainnet:
|
|
53
|
+
return assetId === caip_1.bscAssetId;
|
|
48
54
|
default:
|
|
49
55
|
return false;
|
|
50
56
|
}
|
|
51
57
|
};
|
|
52
58
|
exports.isNativeEvmAsset = isNativeEvmAsset;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const {
|
|
59
|
+
// converts an asset to zrx token (symbol or contract address)
|
|
60
|
+
const assetToToken = (asset) => {
|
|
61
|
+
const { assetReference, assetNamespace } = (0, caip_1.fromAssetId)(asset.assetId);
|
|
62
|
+
return assetNamespace === 'slip44' ? asset.symbol : assetReference;
|
|
63
|
+
};
|
|
64
|
+
exports.assetToToken = assetToToken;
|
|
65
|
+
const getUsdRate = async (sellAsset) => {
|
|
56
66
|
try {
|
|
57
|
-
const
|
|
58
|
-
|
|
67
|
+
const usdcContractAddress = (0, exports.usdcContractAddressFromChainId)(sellAsset.chainId);
|
|
68
|
+
const sellAssetContractAddress = (0, caip_1.fromAssetId)(sellAsset.assetId).assetReference;
|
|
69
|
+
if (sellAssetContractAddress === usdcContractAddress)
|
|
59
70
|
return '1'; // Will break if comparing against usdc
|
|
60
|
-
const baseUrl = (0, exports.baseUrlFromChainId)(
|
|
71
|
+
const baseUrl = (0, exports.baseUrlFromChainId)(sellAsset.chainId);
|
|
61
72
|
const zrxService = (0, zrxService_1.zrxServiceFactory)(baseUrl);
|
|
62
73
|
const rateResponse = await zrxService.get('/swap/v1/price', {
|
|
63
74
|
params: {
|
|
64
|
-
buyToken:
|
|
75
|
+
buyToken: usdcContractAddress,
|
|
65
76
|
buyAmount: '1000000000',
|
|
66
|
-
sellToken:
|
|
77
|
+
sellToken: (0, exports.assetToToken)(sellAsset),
|
|
67
78
|
},
|
|
68
79
|
});
|
|
69
80
|
const price = (0, bignumber_1.bnOrZero)(rateResponse.data.price);
|
|
@@ -24,7 +24,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.zrxBuildTrade = void 0;
|
|
27
|
-
const caip_1 = require("@shapeshiftoss/caip");
|
|
28
27
|
const rax = __importStar(require("retry-axios"));
|
|
29
28
|
const api_1 = require("../../../api");
|
|
30
29
|
const erc20Allowance_abi_1 = require("../../utils/abi/erc20Allowance-abi");
|
|
@@ -38,10 +37,6 @@ const zrxService_1 = require("../utils/zrxService");
|
|
|
38
37
|
async function zrxBuildTrade({ adapter, web3 }, input) {
|
|
39
38
|
const { sellAsset, buyAsset, sellAmountBeforeFeesCryptoBaseUnit: sellAmountExcludeFeeCryptoBaseUnit, slippage, accountNumber, receiveAddress, } = input;
|
|
40
39
|
try {
|
|
41
|
-
const { assetReference: buyAssetErc20Address, assetNamespace: buyAssetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
|
|
42
|
-
const { assetReference: sellAssetErc20Address, assetNamespace: sellAssetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
43
|
-
const buyToken = buyAssetNamespace === 'erc20' ? buyAssetErc20Address : buyAsset.symbol;
|
|
44
|
-
const sellToken = sellAssetNamespace === 'erc20' ? sellAssetErc20Address : sellAsset.symbol;
|
|
45
40
|
const adapterChainId = adapter.getChainId();
|
|
46
41
|
if (buyAsset.chainId !== adapterChainId) {
|
|
47
42
|
throw new api_1.SwapError(`[zrxBuildTrade] - buyAsset must be on chainId ${adapterChainId}`, {
|
|
@@ -79,8 +74,8 @@ async function zrxBuildTrade({ adapter, web3 }, input) {
|
|
|
79
74
|
});
|
|
80
75
|
const quoteResponse = await zrxRetry.get('/swap/v1/quote', {
|
|
81
76
|
params: {
|
|
82
|
-
buyToken,
|
|
83
|
-
sellToken,
|
|
77
|
+
buyToken: (0, helpers_2.assetToToken)(buyAsset),
|
|
78
|
+
sellToken: (0, helpers_2.assetToToken)(sellAsset),
|
|
84
79
|
sellAmount: (0, helpers_1.normalizeAmount)(sellAmountExcludeFeeCryptoBaseUnit),
|
|
85
80
|
takerAddress: receiveAddress,
|
|
86
81
|
slippagePercentage,
|
package/package.json
CHANGED