swapCallParameters static method
Cannot be constructed. Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade. @param trade to produce call parameters for @param options options for the call parameters
Implementation
// Router._() {}
/// Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
/// @param trade to produce call parameters for
/// @param options options for the call parameters
static SwapParameters swapCallParameters(Trade trade, TradeOptions options) {
bool etherIn = false;
bool etherOut = false;
if(trade.inputAmount.currency is Token){
etherIn = trade.inputAmount.currency == Token.WETH[ChainId.MAINNET.value]!;
}else{
etherIn = trade.inputAmount.currency == ETHER;
}
if(trade.outputAmount.currency is Token){
etherOut = trade.outputAmount.currency == Token.WETH[ChainId.MAINNET.value]!;
}else{
etherOut = trade.outputAmount.currency == ETHER;
}
// the router does not support both ether in and out
invariant(!(etherIn && etherOut), 'ETHER_IN_OUT');
invariant(options.ttl > 0, 'TTL');
final dateTimeNow = DateTime.now();
final to = validateAndParseAddress(options.recipient);
String amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
String amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
List<String> path = trade.route.path.map((token) => token.address.toString()).toList();
final deadline =
'0x${BigInt.from((dateTimeNow.millisecondsSinceEpoch / 1000).floor() + options.ttl).toRadixString(16)}';
final useFeeOnTransfer = options.feeOnTransfer ?? false;
String methodName;
List<dynamic> args;
String value;
switch (trade.tradeType) {
case TradeType.EXACT_INPUT:
if (etherIn) {
methodName = useFeeOnTransfer
? 'swapExactETHForTokensSupportingFeeOnTransferTokens'
: 'swapExactETHForTokens';
// (uint amountOutMin, address[] calldata path, address to, uint deadline)
args = [amountOut, path, to.toString(), deadline];
value = amountIn;
} else if (etherOut) {
methodName = useFeeOnTransfer
? 'swapExactTokensForETHSupportingFeeOnTransferTokens'
: 'swapExactTokensForETH';
// (uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
args = [
amountIn,
amountOut,
path,
to.toString(),
deadline
];
value = ZERO_HEX;
} else {
methodName = useFeeOnTransfer
? 'swapExactTokensForTokensSupportingFeeOnTransferTokens'
: 'swapExactTokensForTokens';
// (uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
args = [
amountIn,
amountOut,
path,
to.toString(),
deadline
];
value = ZERO_HEX;
}
break;
case TradeType.EXACT_OUTPUT:
invariant(!useFeeOnTransfer, 'EXACT_OUT_FOT');
if (etherIn) {
methodName = 'swapETHForExactTokens';
// (uint amountOut, address[] calldata path, address to, uint deadline)
args = [amountOut, path, to.toString(), deadline];
value = amountIn;
} else if (etherOut) {
methodName = 'swapTokensForExactETH';
// (uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
args = [
amountOut,
amountIn,
path,
to.toString(),
deadline
];
value = ZERO_HEX;
} else {
methodName = 'swapTokensForExactTokens';
// (uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
args = [
amountOut,
amountIn,
path,
to.toString(),
deadline
];
value = ZERO_HEX;
}
break;
}
return SwapParameters(methodName: methodName, args: args, value: value);
}