if(!s) { revert("Failed to swap"); } } uint out = request.tokenOut.token.balanceOf(address(this)); if(meta.outAmount < out) { meta.outAmount = out - meta.outAmount; } else { meta.outAmount = 0; } console.log("Expected", request.tokenOut.amount, "Received", meta.outAmount); //first, make sure enough output was generated require(meta.outAmount >= request.tokenOut.amount, "Insufficient output generated"); return meta; }
library SwapTypes {
/** * Individual router called to execute some action. Only approved * router addresses will execute successfully */ struct RouterRequest { //router contract that handles the specific route data address router;
//the amount to send to the router TokenTypes.TokenAmount routeAmount;
//the data to use for calling the router } }
function preCheck(SwapTypes.SwapRequest calldata request, SwapMeta memory meta) internal { //make sure fee token is allowed address fToken = address(request.executionRequest.fee.feeToken); DexibleStorage.DexibleData storage dd = DexibleStorage.load(); require( dd.communityVault.isFeeTokenAllowed(fToken), "Fee token is not allowed" );
//and that it's one of the tokens swapped require(fToken == address(request.tokenIn.token) || fToken == address(request.tokenOut.token), "Fee token must be input or output token");
//get the current DXBL balance at the start to apply discounts meta.preDXBLBalance = dd.dxblToken.balanceOf(request.executionRequest.requester); //flag whether the input token is the fee token meta.feeIsInput = address(request.tokenIn.token) == address(request.executionRequest.fee.feeToken); //transfer input tokens for router so it can perform swap //console.log("Transfering input for trading:", request.routes[0].routeAmount.amount); request.tokenIn.token.safeTransferFrom(request.executionRequest.requester, address(this), request.routes[0].routeAmount.amount); //console.log("Expected output", request.tokenOut.amount); }
//and that it's one of the tokens swapped require(fToken == address(request.tokenIn.token) || fToken == address(request.tokenOut.token), "Fee token must be input or output token");