@shapeshiftoss/swapper 1.13.0 → 1.15.1
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 +2 -1
- package/dist/swappers/thorchain/ThorchainSwapper.d.ts +2 -1
- package/dist/swappers/zrx/ZrxApprovalNeeded/ZrxApprovalNeeded.js +2 -2
- package/dist/swappers/zrx/ZrxApproveInfinite/ZrxApproveInfinite.js +2 -2
- package/dist/swappers/zrx/ZrxBuildQuoteTx/ZrxBuildQuoteTx.js +2 -2
- package/dist/swappers/zrx/ZrxExecuteQuote/ZrxExecuteQuote.js +2 -2
- package/dist/swappers/zrx/ZrxSwapper.d.ts +2 -1
- package/dist/swappers/zrx/ZrxSwapper.js +2 -2
- package/dist/swappers/zrx/getZrxSendMaxAmount/getZrxSendMaxAmount.js +2 -2
- package/dist/swappers/zrx/utils/helpers/helpers.js +33 -13
- package/dist/swappers/zrx/utils/test-data/assets.js +3 -0
- package/dist/swappers/zrx/utils/test-data/setupZrxDeps.d.ts +1 -1
- package/dist/swappers/zrx/utils/test-data/setupZrxDeps.js +1 -1
- package/package.json +1 -1
package/dist/api.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CAIP19 } from '@shapeshiftoss/caip';
|
|
1
2
|
import { HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
2
3
|
import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, Asset, BuildQuoteTxInput, ChainTypes, ExecQuoteInput, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SendMaxAmountInput, SwapperType } from '@shapeshiftoss/types';
|
|
3
4
|
export declare class SwapError extends Error {
|
|
@@ -28,7 +29,7 @@ export interface Swapper {
|
|
|
28
29
|
/**
|
|
29
30
|
* Get the default pair of the swapper
|
|
30
31
|
*/
|
|
31
|
-
getDefaultPair():
|
|
32
|
+
getDefaultPair(): [CAIP19, CAIP19];
|
|
32
33
|
/**
|
|
33
34
|
* Get the minimum and maximum trade value of the sellAsset and buyAsset
|
|
34
35
|
*/
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CAIP19 } from '@shapeshiftoss/caip';
|
|
1
2
|
import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SwapperType } from '@shapeshiftoss/types';
|
|
2
3
|
import { Swapper } from '../../api';
|
|
3
4
|
export declare class ThorchainSwapper implements Swapper {
|
|
@@ -9,7 +10,7 @@ export declare class ThorchainSwapper implements Swapper {
|
|
|
9
10
|
getAvailableAssets(assets: Asset[]): Asset[];
|
|
10
11
|
canTradePair(sellAsset: Asset, buyAsset: Asset): boolean;
|
|
11
12
|
executeQuote(): Promise<ExecQuoteOutput>;
|
|
12
|
-
getDefaultPair():
|
|
13
|
+
getDefaultPair(): [CAIP19, CAIP19];
|
|
13
14
|
approvalNeeded(): Promise<ApprovalNeededOutput>;
|
|
14
15
|
approveInfinite(): Promise<string>;
|
|
15
16
|
getSendMaxAmount(): Promise<string>;
|
|
@@ -18,8 +18,8 @@ async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
|
|
|
18
18
|
}
|
|
19
19
|
const accountNumber = quote.sellAssetAccountId ? Number(quote.sellAssetAccountId) : 0;
|
|
20
20
|
const adapter = adapterManager.byChain(sellAsset.chain);
|
|
21
|
-
const
|
|
22
|
-
const receiveAddress = await adapter.getAddress({ wallet,
|
|
21
|
+
const bip44Params = adapter.buildBIP44Params({ accountNumber });
|
|
22
|
+
const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
|
|
23
23
|
/**
|
|
24
24
|
* /swap/v1/quote
|
|
25
25
|
* params: {
|
|
@@ -10,10 +10,10 @@ const helpers_1 = require("../utils/helpers/helpers");
|
|
|
10
10
|
const zrxService_1 = require("../utils/zrxService");
|
|
11
11
|
async function ZrxApproveInfinite({ adapterManager, web3 }, { quote, wallet }) {
|
|
12
12
|
const adapter = adapterManager.byChain(types_1.ChainTypes.Ethereum);
|
|
13
|
-
const
|
|
13
|
+
const bip44Params = adapter.buildBIP44Params({
|
|
14
14
|
accountNumber: (0, bignumber_1.bnOrZero)(quote.sellAssetAccountId).toNumber()
|
|
15
15
|
}); // TODO: Add account number
|
|
16
|
-
const receiveAddress = await adapter.getAddress({ wallet,
|
|
16
|
+
const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
|
|
17
17
|
/**
|
|
18
18
|
* /swap/v1/quote
|
|
19
19
|
* params: {
|
|
@@ -53,8 +53,8 @@ async function ZrxBuildQuoteTx({ adapterManager, web3 }, { input, wallet }) {
|
|
|
53
53
|
throw new __1.SwapError(`ZrxSwapper:ZrxBuildQuoteTx buyAsset must be on chain [${types_1.ChainTypes.Ethereum}]`);
|
|
54
54
|
}
|
|
55
55
|
const adapter = adapterManager.byChain(buyAsset.chain);
|
|
56
|
-
const
|
|
57
|
-
const receiveAddress = await adapter.getAddress({ wallet,
|
|
56
|
+
const bip44Params = adapter.buildBIP44Params({ accountNumber: Number(buyAssetAccountId) });
|
|
57
|
+
const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
|
|
58
58
|
if (new bignumber_js_1.default(slippage || 0).gt(constants_1.MAX_SLIPPAGE)) {
|
|
59
59
|
throw new __1.SwapError(`ZrxSwapper:ZrxBuildQuoteTx slippage value of ${slippage} is greater than max slippage value of ${constants_1.MAX_SLIPPAGE}`);
|
|
60
60
|
}
|
|
@@ -24,7 +24,7 @@ async function ZrxExecuteQuote({ adapterManager }, { quote, wallet }) {
|
|
|
24
24
|
// value is 0 for erc20s
|
|
25
25
|
const value = sellAsset.symbol === 'ETH' ? quote.sellAmount : '0';
|
|
26
26
|
const adapter = adapterManager.byChain(sellAsset.chain);
|
|
27
|
-
const
|
|
27
|
+
const bip44Params = adapter.buildBIP44Params({
|
|
28
28
|
accountNumber: Number(quote.sellAssetAccountId)
|
|
29
29
|
});
|
|
30
30
|
let buildTxResponse, signedTx, txid;
|
|
@@ -37,7 +37,7 @@ async function ZrxExecuteQuote({ adapterManager }, { quote, wallet }) {
|
|
|
37
37
|
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.gasPrice) || 0),
|
|
38
38
|
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)
|
|
39
39
|
},
|
|
40
|
-
|
|
40
|
+
bip44Params
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
catch (error) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CAIP19 } from '@shapeshiftoss/caip';
|
|
1
2
|
import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
|
|
2
3
|
import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, Asset, BuildQuoteTxInput, ChainTypes, ExecQuoteInput, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SendMaxAmountInput, SwapperType } from '@shapeshiftoss/types';
|
|
3
4
|
import Web3 from 'web3';
|
|
@@ -20,7 +21,7 @@ export declare class ZrxSwapper implements Swapper {
|
|
|
20
21
|
getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
|
|
21
22
|
getAvailableAssets(assets: Asset[]): Asset[];
|
|
22
23
|
canTradePair(sellAsset: Asset, buyAsset: Asset): boolean;
|
|
23
|
-
getDefaultPair():
|
|
24
|
+
getDefaultPair(): [CAIP19, CAIP19];
|
|
24
25
|
executeQuote(args: ExecQuoteInput<ChainTypes, SwapperType>): Promise<ExecQuoteOutput>;
|
|
25
26
|
approvalNeeded(args: ApprovalNeededInput<ChainTypes, SwapperType>): Promise<ApprovalNeededOutput>;
|
|
26
27
|
approveInfinite(args: ApproveInfiniteInput<ChainTypes, SwapperType>): Promise<string>;
|
|
@@ -44,8 +44,8 @@ class ZrxSwapper {
|
|
|
44
44
|
return availableAssets.length === 2;
|
|
45
45
|
}
|
|
46
46
|
getDefaultPair() {
|
|
47
|
-
const ETH =
|
|
48
|
-
const FOX =
|
|
47
|
+
const ETH = 'eip155:1/slip44:60';
|
|
48
|
+
const FOX = 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d';
|
|
49
49
|
return [ETH, FOX];
|
|
50
50
|
}
|
|
51
51
|
async executeQuote(args) {
|
|
@@ -13,10 +13,10 @@ const constants_1 = require("../utils/constants");
|
|
|
13
13
|
async function getZrxSendMaxAmount({ adapterManager }, { wallet, quote, sellAssetAccountId, feeEstimateKey = types_1.chainAdapters.FeeDataKey.Average }) {
|
|
14
14
|
var _a, _b, _c;
|
|
15
15
|
const adapter = adapterManager.byChain(types_1.ChainTypes.Ethereum);
|
|
16
|
-
const
|
|
16
|
+
const bip44Params = adapter.buildBIP44Params({
|
|
17
17
|
accountNumber: (0, bignumber_1.bnOrZero)(sellAssetAccountId).toNumber()
|
|
18
18
|
});
|
|
19
|
-
const ethAddress = await adapter.getAddress({ wallet,
|
|
19
|
+
const ethAddress = await adapter.getAddress({ wallet, bip44Params });
|
|
20
20
|
const account = await adapter.getAccount(ethAddress);
|
|
21
21
|
if (!quote.sellAsset) {
|
|
22
22
|
throw new api_1.SwapError('quote.sellAsset is required');
|
|
@@ -73,7 +73,7 @@ const getUsdRate = async (input) => {
|
|
|
73
73
|
};
|
|
74
74
|
exports.getUsdRate = getUsdRate;
|
|
75
75
|
const grantAllowance = async ({ quote, wallet, adapter, erc20Abi, web3 }) => {
|
|
76
|
-
var _a, _b, _c, _d;
|
|
76
|
+
var _a, _b, _c, _d, _e;
|
|
77
77
|
if (!quote.sellAsset.tokenId) {
|
|
78
78
|
throw new Error('sellAsset.tokenId is required');
|
|
79
79
|
}
|
|
@@ -81,7 +81,7 @@ const grantAllowance = async ({ quote, wallet, adapter, erc20Abi, web3 }) => {
|
|
|
81
81
|
const approveTx = erc20Contract.methods
|
|
82
82
|
.approve(quote.allowanceContract, quote.sellAmount)
|
|
83
83
|
.encodeABI();
|
|
84
|
-
const
|
|
84
|
+
const bip44Params = adapter.buildBIP44Params({
|
|
85
85
|
accountNumber: Number(quote.sellAssetAccountId) || 0
|
|
86
86
|
});
|
|
87
87
|
let grantAllowanceTxToSign, signedTx, broadcastedTxId;
|
|
@@ -89,7 +89,7 @@ const grantAllowance = async ({ quote, wallet, adapter, erc20Abi, web3 }) => {
|
|
|
89
89
|
const { txToSign } = await adapter.buildSendTransaction({
|
|
90
90
|
wallet,
|
|
91
91
|
to: quote.sellAsset.tokenId,
|
|
92
|
-
|
|
92
|
+
bip44Params,
|
|
93
93
|
value: '0',
|
|
94
94
|
chainSpecific: {
|
|
95
95
|
erc20ContractAddress: quote.sellAsset.tokenId,
|
|
@@ -105,18 +105,38 @@ const grantAllowance = async ({ quote, wallet, adapter, erc20Abi, web3 }) => {
|
|
|
105
105
|
catch (error) {
|
|
106
106
|
throw new Error(`grantAllowance - buildSendTransaction: ${error}`);
|
|
107
107
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
108
|
+
if (wallet.supportsOfflineSigning()) {
|
|
109
|
+
try {
|
|
110
|
+
signedTx = await adapter.signTransaction({ txToSign: grantAllowanceTxToSign, wallet });
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
throw new api_1.SwapError(`grantAllowance - signTransaction error: ${error}`);
|
|
114
|
+
}
|
|
115
|
+
if (!signedTx) {
|
|
116
|
+
throw new api_1.SwapError(`grantAllowance - Signed transaction is required: ${signedTx}`);
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
broadcastedTxId = await adapter.broadcastTransaction(signedTx);
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
throw new api_1.SwapError(`grantAllowance - broadcastTransaction error: ${error}`);
|
|
123
|
+
}
|
|
124
|
+
return broadcastedTxId;
|
|
113
125
|
}
|
|
114
|
-
|
|
115
|
-
|
|
126
|
+
else if (wallet.supportsBroadcast() && adapter.signAndBroadcastTransaction) {
|
|
127
|
+
try {
|
|
128
|
+
broadcastedTxId = await ((_e = adapter.signAndBroadcastTransaction) === null || _e === void 0 ? void 0 : _e.call(adapter, {
|
|
129
|
+
txToSign: grantAllowanceTxToSign,
|
|
130
|
+
wallet
|
|
131
|
+
}));
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
throw new api_1.SwapError(`grantAllowance - signAndBroadcastTransaction error: ${error}`);
|
|
135
|
+
}
|
|
136
|
+
return broadcastedTxId;
|
|
116
137
|
}
|
|
117
|
-
|
|
118
|
-
throw new
|
|
138
|
+
else {
|
|
139
|
+
throw new api_1.SwapError('grantAllowance - invalid HDWallet config');
|
|
119
140
|
}
|
|
120
|
-
return broadcastedTxId;
|
|
121
141
|
};
|
|
122
142
|
exports.grantAllowance = grantAllowance;
|
|
@@ -4,6 +4,7 @@ exports.FOX = exports.WETH = exports.BTC = void 0;
|
|
|
4
4
|
const types_1 = require("@shapeshiftoss/types");
|
|
5
5
|
exports.BTC = {
|
|
6
6
|
caip19: 'bip122:000000000019d6689c085ae165831e93/slip44:0',
|
|
7
|
+
caip2: 'bip122:000000000019d6689c085ae165831e93',
|
|
7
8
|
name: 'bitcoin',
|
|
8
9
|
chain: types_1.ChainTypes.Bitcoin,
|
|
9
10
|
dataSource: types_1.AssetDataSource.CoinGecko,
|
|
@@ -23,6 +24,7 @@ exports.BTC = {
|
|
|
23
24
|
};
|
|
24
25
|
exports.WETH = {
|
|
25
26
|
caip19: 'eip155:1/erc20:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
|
|
27
|
+
caip2: 'eip155:1',
|
|
26
28
|
name: 'WETH',
|
|
27
29
|
chain: types_1.ChainTypes.Ethereum,
|
|
28
30
|
dataSource: types_1.AssetDataSource.CoinGecko,
|
|
@@ -43,6 +45,7 @@ exports.WETH = {
|
|
|
43
45
|
};
|
|
44
46
|
exports.FOX = {
|
|
45
47
|
caip19: 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d',
|
|
48
|
+
caip2: 'eip155:1',
|
|
46
49
|
name: 'FOX',
|
|
47
50
|
chain: types_1.ChainTypes.Ethereum,
|
|
48
51
|
dataSource: types_1.AssetDataSource.CoinGecko,
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
|
|
3
3
|
import Web3 from 'web3';
|
|
4
4
|
export declare const chainAdapterMockFuncs: {
|
|
5
|
-
|
|
5
|
+
buildBIP44Params: jest.Mock<{
|
|
6
6
|
purpose: number;
|
|
7
7
|
coinType: number;
|
|
8
8
|
accountNumber: number;
|
|
@@ -9,7 +9,7 @@ const types_1 = require("@shapeshiftoss/types");
|
|
|
9
9
|
const web3_1 = __importDefault(require("web3"));
|
|
10
10
|
jest.mock('@shapeshiftoss/chain-adapters');
|
|
11
11
|
exports.chainAdapterMockFuncs = {
|
|
12
|
-
|
|
12
|
+
buildBIP44Params: jest.fn(() => ({ purpose: 44, coinType: 60, accountNumber: 0 })),
|
|
13
13
|
buildSendTransaction: jest.fn(() => Promise.resolve({ txToSign: {} })),
|
|
14
14
|
signTransaction: jest.fn(() => Promise.resolve('signedTx')),
|
|
15
15
|
broadcastTransaction: jest.fn(() => Promise.resolve('broadcastedTx')),
|