@pioneer-platform/uniswap-client 0.0.28 → 0.1.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/CHANGELOG.md +16 -0
- package/lib/constants/chainInfo.d.ts +2 -2
- package/lib/constants/chainInfo.js +40 -43
- package/lib/constants/chains.d.ts +20 -20
- package/lib/constants/chains.js +25 -26
- package/lib/constants/governance.js +13 -14
- package/lib/constants/lists.js +20 -28
- package/lib/constants/misc.d.ts +4 -5
- package/lib/constants/misc.js +2 -2
- package/lib/constants/networks.js +45 -46
- package/lib/constants/proposals/uniswap_grants_proposal_description.js +106 -1
- package/lib/constants/providers.d.ts +16 -15
- package/lib/constants/providers.js +23 -24
- package/lib/constants/routing.js +44 -61
- package/lib/constants/tokens.d.ts +49 -49
- package/lib/constants/tokens.js +133 -170
- package/lib/index.js +556 -877
- package/lib/routing/clientSideSmartOrderRouter.js +72 -121
- package/lib/routing/gas.js +63 -150
- package/lib/routing/types.d.ts +7 -7
- package/lib/routing/types.js +160 -264
- package/lib/routing/utils.js +141 -196
- package/lib/rpc/AppJsonRpcProvider.js +40 -131
- package/lib/rpc/ConfiguredJsonRpcProvider.d.ts +1 -1
- package/lib/rpc/ConfiguredJsonRpcProvider.js +8 -27
- package/lib/utils/contracts/getContract.js +6 -7
- package/lib/utils/transformSwapRouteToGetQuoteResult.js +24 -27
- package/lib/utils/uniswapData.d.ts +6 -6
- package/lib/utils/uniswapData.js +64 -142
- package/package.json +19 -19
- package/tsconfig.json +27 -8
- package/lib/constants/chains.test.d.ts +0 -1
- package/lib/constants/chains.test.js +0 -26
- package/lib/constants/localCurrencies.d.ts +0 -6
- package/lib/constants/localCurrencies.js +0 -94
- package/lib/constants/localCurrencyIcons.d.ts +0 -18
- package/lib/constants/localCurrencyIcons.js +0 -298
- package/lib/constants/routing.test.d.ts +0 -1
- package/lib/constants/routing.test.js +0 -32
- package/lib/constants/tokenLists/broken.tokenlist.json +0 -22
- package/lib/constants/tokenLogoLookup.d.ts +0 -8
- package/lib/constants/tokenLogoLookup.js +0 -48
- package/lib/constants/tokenSafety.js +0 -129
- package/lib/constants/tokenSafetyLookup.d.ts +0 -18
- package/lib/constants/tokenSafetyLookup.js +0 -66
- package/lib/constants/tokenSaftey.test.d.ts +0 -1
- package/lib/constants/tokenSaftey.test.js +0 -31
|
@@ -1,56 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
4
|
};
|
|
41
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.
|
|
43
|
-
|
|
6
|
+
exports.getRouter = getRouter;
|
|
7
|
+
exports.getClientSideQuote = getClientSideQuote;
|
|
8
|
+
const sdk_core_1 = require("@uniswap/sdk-core");
|
|
44
9
|
// This file is lazy-loaded, so the import of smart-order-router is intentional.
|
|
45
10
|
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
11
|
+
const smart_order_router_1 = require("@uniswap/smart-order-router");
|
|
12
|
+
const chains_1 = require("../constants/chains");
|
|
13
|
+
const providers_1 = require("../constants/providers");
|
|
14
|
+
const tokens_1 = require("../constants/tokens");
|
|
15
|
+
const jsbi_1 = __importDefault(require("jsbi"));
|
|
16
|
+
const types_1 = require("../routing/types");
|
|
17
|
+
const transformSwapRouteToGetQuoteResult_1 = require("../utils/transformSwapRouteToGetQuoteResult");
|
|
18
|
+
const CLIENT_SIDE_ROUTING_ALLOW_LIST = [
|
|
54
19
|
sdk_core_1.ChainId.MAINNET,
|
|
55
20
|
sdk_core_1.ChainId.OPTIMISM,
|
|
56
21
|
sdk_core_1.ChainId.OPTIMISM_GOERLI,
|
|
@@ -66,86 +31,72 @@ var CLIENT_SIDE_ROUTING_ALLOW_LIST = [
|
|
|
66
31
|
sdk_core_1.ChainId.AVALANCHE,
|
|
67
32
|
sdk_core_1.ChainId.BASE,
|
|
68
33
|
];
|
|
69
|
-
|
|
34
|
+
const routers = new Map();
|
|
70
35
|
function getRouter(chainId) {
|
|
71
|
-
|
|
36
|
+
const router = routers.get(chainId);
|
|
72
37
|
if (router)
|
|
73
38
|
return router;
|
|
74
|
-
|
|
39
|
+
const supportedChainId = (0, chains_1.asSupportedChain)(chainId);
|
|
75
40
|
if (supportedChainId && CLIENT_SIDE_ROUTING_ALLOW_LIST.includes(chainId)) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
routers.set(chainId,
|
|
79
|
-
return
|
|
41
|
+
const provider = providers_1.RPC_PROVIDERS[supportedChainId];
|
|
42
|
+
const router = new smart_order_router_1.AlphaRouter({ chainId, provider });
|
|
43
|
+
routers.set(chainId, router);
|
|
44
|
+
return router;
|
|
80
45
|
}
|
|
81
|
-
throw new Error(
|
|
46
|
+
throw new Error(`Router does not support this chain (chainId: ${chainId}).`);
|
|
82
47
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
console.log("swapRoute", swapRoute);
|
|
120
|
-
if (!swapRoute) {
|
|
121
|
-
return [2 /*return*/, { state: types_1.QuoteState.NOT_FOUND }];
|
|
122
|
-
}
|
|
123
|
-
return [2 /*return*/, (0, transformSwapRouteToGetQuoteResult_1.transformSwapRouteToGetQuoteResult)(tradeType, amount, swapRoute)];
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
});
|
|
48
|
+
async function getQuote({ tradeType, tokenIn, tokenOut, amount: amountRaw, }, router, routerConfig) {
|
|
49
|
+
console.log("Checkpoint: getQuote");
|
|
50
|
+
const tokenInIsNative = Object.values(types_1.SwapRouterNativeAssets).includes(tokenIn.address);
|
|
51
|
+
const tokenOutIsNative = Object.values(types_1.SwapRouterNativeAssets).includes(tokenOut.address);
|
|
52
|
+
console.log("Checkpoint: getQuote2");
|
|
53
|
+
let currencyIn;
|
|
54
|
+
if (tokenInIsNative) {
|
|
55
|
+
console.log("Checkpoint: tokenInIsNative");
|
|
56
|
+
currencyIn = (0, tokens_1.nativeOnChain)(tokenIn.chainId); // Use the native token for the chain if tokenIn is native
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
console.log("Checkpoint: !tokenInIsNative tokenIn: ", tokenIn);
|
|
60
|
+
currencyIn = new sdk_core_1.Token(tokenIn.chainId, tokenIn.address, tokenIn.decimals, tokenIn.symbol); // Otherwise, create a new token
|
|
61
|
+
}
|
|
62
|
+
// Determine the output currency type
|
|
63
|
+
let currencyOut;
|
|
64
|
+
if (tokenOutIsNative) {
|
|
65
|
+
console.log("Checkpoint: tokenOutIsNative");
|
|
66
|
+
currencyOut = (0, tokens_1.nativeOnChain)(tokenOut.chainId); // Use the native token for the chain if tokenOut is native
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
console.log("Checkpoint: !tokenOutIsNative");
|
|
70
|
+
currencyOut = new sdk_core_1.Token(tokenOut.chainId, tokenOut.address, tokenOut.decimals, tokenOut.symbol); // Otherwise, create a new token
|
|
71
|
+
}
|
|
72
|
+
const baseCurrency = tradeType === sdk_core_1.TradeType.EXACT_INPUT ? currencyIn : currencyOut;
|
|
73
|
+
const quoteCurrency = tradeType === sdk_core_1.TradeType.EXACT_INPUT ? currencyOut : currencyIn;
|
|
74
|
+
console.log("baseCurrency", baseCurrency);
|
|
75
|
+
console.log("quoteCurrency", quoteCurrency);
|
|
76
|
+
const amount = sdk_core_1.CurrencyAmount.fromRawAmount(baseCurrency, jsbi_1.default.BigInt(amountRaw));
|
|
77
|
+
// TODO (WEB-2055): explore initializing client side routing on first load (when amountRaw is null) if there are enough users using client-side router preference.
|
|
78
|
+
const swapRoute = await router.route(amount, quoteCurrency, tradeType, /*swapConfig=*/ undefined, routerConfig);
|
|
79
|
+
console.log("swapRoute", swapRoute);
|
|
80
|
+
if (!swapRoute) {
|
|
81
|
+
return { state: types_1.QuoteState.NOT_FOUND };
|
|
82
|
+
}
|
|
83
|
+
return (0, transformSwapRouteToGetQuoteResult_1.transformSwapRouteToGetQuoteResult)(tradeType, amount, swapRoute);
|
|
127
84
|
}
|
|
128
|
-
function getClientSideQuote(
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
},
|
|
146
|
-
amount: amount,
|
|
147
|
-
}, router, config)];
|
|
148
|
-
});
|
|
149
|
-
});
|
|
85
|
+
async function getClientSideQuote({ tokenInAddress, tokenInChainId, tokenInDecimals, tokenInSymbol, tokenOutAddress, tokenOutChainId, tokenOutDecimals, tokenOutSymbol, amount, tradeType, }, router, config) {
|
|
86
|
+
return getQuote({
|
|
87
|
+
tradeType,
|
|
88
|
+
tokenIn: {
|
|
89
|
+
address: tokenInAddress,
|
|
90
|
+
chainId: tokenInChainId,
|
|
91
|
+
decimals: tokenInDecimals,
|
|
92
|
+
symbol: tokenInSymbol,
|
|
93
|
+
},
|
|
94
|
+
tokenOut: {
|
|
95
|
+
address: tokenOutAddress,
|
|
96
|
+
chainId: tokenOutChainId,
|
|
97
|
+
decimals: tokenOutDecimals,
|
|
98
|
+
symbol: tokenOutSymbol,
|
|
99
|
+
},
|
|
100
|
+
amount,
|
|
101
|
+
}, router, config);
|
|
150
102
|
}
|
|
151
|
-
exports.getClientSideQuote = getClientSideQuote;
|
package/lib/routing/gas.js
CHANGED
|
@@ -1,158 +1,71 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
4
|
};
|
|
52
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
-
exports.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
6
|
+
exports.getApproveInfo = getApproveInfo;
|
|
7
|
+
exports.getWrapInfo = getWrapInfo;
|
|
8
|
+
const permit2_sdk_1 = require("@uniswap/permit2-sdk");
|
|
9
|
+
const sdk_core_1 = require("@uniswap/sdk-core");
|
|
10
|
+
const providers_1 = require("../constants/providers");
|
|
11
|
+
const tokens_1 = require("../constants/tokens");
|
|
12
|
+
const erc20_json_1 = __importDefault(require("../uniswap/src/abis/erc20.json"));
|
|
13
|
+
const weth_json_1 = __importDefault(require("../uniswap/src/abis/weth.json"));
|
|
14
|
+
const getContract_1 = require("../utils/contracts/getContract");
|
|
61
15
|
// TODO(UniswapX): add fallback gas limits per chain? l2s have higher costs
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
function getApproveInfo(account, currency, amount, usdCostPerGas) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
case 3:
|
|
98
|
-
_1 = _a.sent();
|
|
99
|
-
// If contract lookup fails (eg if Infura goes down), then don't show gas info for approving the token
|
|
100
|
-
return [2 /*return*/, { needsApprove: false }];
|
|
101
|
-
case 4:
|
|
102
|
-
_a.trys.push([4, 7, , 8]);
|
|
103
|
-
return [4 /*yield*/, tokenContract.populateTransaction.approve(permit2_sdk_1.PERMIT2_ADDRESS, permit2_sdk_1.MaxUint256)];
|
|
104
|
-
case 5:
|
|
105
|
-
approveTx = _a.sent();
|
|
106
|
-
return [4 /*yield*/, provider.estimateGas(__assign({ from: account }, approveTx))];
|
|
107
|
-
case 6:
|
|
108
|
-
approveGasUseEstimate = (_a.sent()).toNumber();
|
|
109
|
-
return [3 /*break*/, 8];
|
|
110
|
-
case 7:
|
|
111
|
-
_2 = _a.sent();
|
|
112
|
-
// estimateGas will error if the account doesn't have sufficient token balance, but we should show an estimated cost anyway
|
|
113
|
-
approveGasUseEstimate = APPROVE_FALLBACK_GAS_LIMIT;
|
|
114
|
-
return [3 /*break*/, 8];
|
|
115
|
-
case 8: return [2 /*return*/, { needsApprove: true, approveGasEstimateUSD: approveGasUseEstimate * usdCostPerGas }];
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
});
|
|
16
|
+
const WRAP_FALLBACK_GAS_LIMIT = 45000;
|
|
17
|
+
const APPROVE_FALLBACK_GAS_LIMIT = 65000;
|
|
18
|
+
async function getApproveInfo(account, currency, amount, usdCostPerGas) {
|
|
19
|
+
// native currencies do not need token approvals
|
|
20
|
+
if (currency.isNative)
|
|
21
|
+
return { needsApprove: false };
|
|
22
|
+
// If any of these arguments aren't provided, then we cannot generate approval cost info
|
|
23
|
+
if (!account || !usdCostPerGas)
|
|
24
|
+
return { needsApprove: false };
|
|
25
|
+
// routing-api under estimates gas for Arbitrum swaps so it inflates cost per gas by a lot
|
|
26
|
+
// so disable showing approves for Arbitrum until routing-api gives more accurate gas estimates
|
|
27
|
+
if (currency.chainId === sdk_core_1.ChainId.ARBITRUM_ONE || currency.chainId === sdk_core_1.ChainId.ARBITRUM_GOERLI) {
|
|
28
|
+
return { needsApprove: false };
|
|
29
|
+
}
|
|
30
|
+
const provider = providers_1.RPC_PROVIDERS[currency.chainId];
|
|
31
|
+
const tokenContract = (0, getContract_1.getContract)(currency.address, erc20_json_1.default, provider);
|
|
32
|
+
let approveGasUseEstimate;
|
|
33
|
+
try {
|
|
34
|
+
const allowance = await tokenContract.callStatic.allowance(account, permit2_sdk_1.PERMIT2_ADDRESS);
|
|
35
|
+
if (!allowance.lt(amount))
|
|
36
|
+
return { needsApprove: false };
|
|
37
|
+
}
|
|
38
|
+
catch (_) {
|
|
39
|
+
// If contract lookup fails (eg if Infura goes down), then don't show gas info for approving the token
|
|
40
|
+
return { needsApprove: false };
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
const approveTx = await tokenContract.populateTransaction.approve(permit2_sdk_1.PERMIT2_ADDRESS, permit2_sdk_1.MaxUint256);
|
|
44
|
+
approveGasUseEstimate = (await provider.estimateGas({ from: account, ...approveTx })).toNumber();
|
|
45
|
+
}
|
|
46
|
+
catch (_) {
|
|
47
|
+
// estimateGas will error if the account doesn't have sufficient token balance, but we should show an estimated cost anyway
|
|
48
|
+
approveGasUseEstimate = APPROVE_FALLBACK_GAS_LIMIT;
|
|
49
|
+
}
|
|
50
|
+
return { needsApprove: true, approveGasEstimateUSD: approveGasUseEstimate * usdCostPerGas };
|
|
119
51
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
return [4 /*yield*/, wethContract.populateTransaction.deposit({ value: amount })
|
|
140
|
-
// estimateGas will error if the account doesn't have sufficient ETH balance, but we should show an estimated cost anyway
|
|
141
|
-
];
|
|
142
|
-
case 2:
|
|
143
|
-
wethTx = _b.sent();
|
|
144
|
-
return [4 /*yield*/, provider.estimateGas(__assign({ from: account }, wethTx))];
|
|
145
|
-
case 3:
|
|
146
|
-
// estimateGas will error if the account doesn't have sufficient ETH balance, but we should show an estimated cost anyway
|
|
147
|
-
wrapGasUseEstimate = (_b.sent()).toNumber();
|
|
148
|
-
return [3 /*break*/, 5];
|
|
149
|
-
case 4:
|
|
150
|
-
_3 = _b.sent();
|
|
151
|
-
wrapGasUseEstimate = WRAP_FALLBACK_GAS_LIMIT;
|
|
152
|
-
return [3 /*break*/, 5];
|
|
153
|
-
case 5: return [2 /*return*/, { needsWrap: true, wrapGasEstimateUSD: wrapGasUseEstimate * usdCostPerGas }];
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
});
|
|
52
|
+
async function getWrapInfo(needsWrap, account, chainId, amount, usdCostPerGas) {
|
|
53
|
+
if (!needsWrap)
|
|
54
|
+
return { needsWrap: false };
|
|
55
|
+
const provider = providers_1.RPC_PROVIDERS[chainId];
|
|
56
|
+
const wethAddress = tokens_1.WRAPPED_NATIVE_CURRENCY[chainId]?.address;
|
|
57
|
+
// If any of these arguments aren't provided, then we cannot generate wrap cost info
|
|
58
|
+
if (!wethAddress || !usdCostPerGas)
|
|
59
|
+
return { needsWrap: false };
|
|
60
|
+
let wrapGasUseEstimate;
|
|
61
|
+
try {
|
|
62
|
+
const wethContract = (0, getContract_1.getContract)(wethAddress, weth_json_1.default, provider, account);
|
|
63
|
+
const wethTx = await wethContract.populateTransaction.deposit({ value: amount });
|
|
64
|
+
// estimateGas will error if the account doesn't have sufficient ETH balance, but we should show an estimated cost anyway
|
|
65
|
+
wrapGasUseEstimate = (await provider.estimateGas({ from: account, ...wethTx })).toNumber();
|
|
66
|
+
}
|
|
67
|
+
catch (_) {
|
|
68
|
+
wrapGasUseEstimate = WRAP_FALLBACK_GAS_LIMIT;
|
|
69
|
+
}
|
|
70
|
+
return { needsWrap: true, wrapGasEstimateUSD: wrapGasUseEstimate * usdCostPerGas };
|
|
157
71
|
}
|
|
158
|
-
exports.getWrapInfo = getWrapInfo;
|
package/lib/routing/types.d.ts
CHANGED
|
@@ -241,8 +241,8 @@ export declare class DutchOrderTrade extends IDutchOrderTrade<Currency, Currency
|
|
|
241
241
|
startTimeBufferSecs: number;
|
|
242
242
|
deadlineBufferSecs: number;
|
|
243
243
|
slippageTolerance: Percent;
|
|
244
|
-
inputTax:
|
|
245
|
-
outputTax:
|
|
244
|
+
inputTax: any;
|
|
245
|
+
outputTax: any;
|
|
246
246
|
swapFee: SwapFeeInfo | undefined;
|
|
247
247
|
constructor({ currencyIn, currenciesOut, orderInfo, tradeType, quoteId, requestId, wrapInfo, approveInfo, classicGasUseEstimateUSD, auctionPeriodSecs, startTimeBufferSecs, deadlineBufferSecs, slippageTolerance, swapFee, }: {
|
|
248
248
|
currencyIn: Currency;
|
|
@@ -303,11 +303,11 @@ export declare class LimitOrderTrade {
|
|
|
303
303
|
tradeType: TradeType;
|
|
304
304
|
swapper: string;
|
|
305
305
|
deadline: number;
|
|
306
|
-
inputTax:
|
|
307
|
-
outputTax:
|
|
308
|
-
slippageTolerance:
|
|
309
|
-
quoteId:
|
|
310
|
-
requestId:
|
|
306
|
+
inputTax: any;
|
|
307
|
+
outputTax: any;
|
|
308
|
+
slippageTolerance: any;
|
|
309
|
+
quoteId: any;
|
|
310
|
+
requestId: any;
|
|
311
311
|
constructor({ tradeType, amountIn, amountOut, deadlineBufferSecs, swapFee, wrapInfo, approveInfo, swapper, }: {
|
|
312
312
|
tradeType: TradeType;
|
|
313
313
|
amountIn: CurrencyAmount<Token>;
|