Orimex Trader receives the USDT from the contract and performs an arbitrage, distributing the profit to the investors.
Copy function checkArbitrageOpportunity(uint amountIn) external view returns (uint bnbOut, bool hasProfit) {
require(msg.sender == aiAgent, "Only AI Agent");
address[] memory pathBnbToUsdc = new address[](2);
pathBnbToUsdc[0] = WBNB;
pathBnbToUsdc[1] = USDC;
address[] memory pathUsdcToThird = new address[](2);
pathUsdcToThird[0] = USDC;
pathUsdcToThird[1] = thirdToken;
address[] memory pathThirdToBnb = new address[](2);
pathThirdToBnb[0] = thirdToken;
pathThirdToBnb[1] = WBNB;
try PancakeswapRouter.getAmountsOut(amountIn, pathBnbToUsdc) returns (uint[] memory usdcOut) {
try PancakeswapRouter.getAmountsOut(usdcOut[1], pathUsdcToThird) returns (uint[] memory thirdOut) {
try PancakeswapRouter.getAmountsOut(thirdOut[1], pathThirdToBnb) returns (uint[] memory bnbOutArray) {
bnbOut = bnbOutArray[1];
hasProfit = bnbOut > amountIn;
return (bnbOut, hasProfit);
} catch {
return (0, false);
}
} catch {
return (0, false);
}
} catch {
return (0, false);
}
}
Copy ```remix-solidity
function executeTriangularTrade() external payable {
require(msg.sender == aiAgent, "Only ai Agent");
require(msg.value > 0, "Must send BNB");
uint initialBnb = msg.value;
emit Debug("BNB Received", initialBnb);
emit Debug("Initial BNB Balance", address(this).balance);
emit DebugAddress("WBNB Address", WBNB);
emit DebugAddress("USDC Address", USDC);
emit DebugAddress("Third Token Address", thirdToken);
address[] memory pathBnbToUsdc = new address[](2);
pathBnbToUsdc[0] = WBNB;
pathBnbToUsdc[1] = USDC;
address[] memory pathUsdcToThird = new address[](2);
pathUsdcToThird[0] = USDC;
pathUsdcToThird[1] = thirdToken;
address[] memory pathThirdToBnb = new address[](2);
pathThirdToBnb[0] = thirdToken;
pathThirdToBnb[1] = WBNB;
// 1. BNB -> USDC
emit Debug("Preparing BNB to USDC swap", initialBnb);
emit Debug("Calling swapExactETHForTokens for BNB->USDC", initialBnb);
uint[] memory usdcAmounts;
try PancakeswapRouter.swapExactETHForTokens{value: initialBnb}(
0,
pathBnbToUsdc,
address(this),
block.timestamp + 300
) returns (uint[] memory amounts) {
usdcAmounts = amounts;
emit Debug("USDC Received", usdcAmounts[1]);
} catch Error(string memory reason) {
emit Error(string(abi.encodePacked("BNB to USDC swap failed: ", reason)));
revert(string(abi.encodePacked("Swap BNB to USDC failed: ", reason)));
} catch {
emit Error("BNB to USDC swap failed - Unknown reason");
revert("Swap BNB to USDC failed - Unknown");
}
uint usdcReceived = usdcAmounts[1];
// 2. USDC -> Third Token
emit Debug("Preparing USDC to Third Token swap", usdcReceived);
uint[] memory thirdAmounts;
try PancakeswapRouter.swapExactTokensForTokens(
usdcReceived,
0,
pathUsdcToThird,
address(this),
block.timestamp + 300
) returns (uint[] memory amounts) {
thirdAmounts = amounts;
emit Debug("Third Token Received", thirdAmounts[1]);
} catch Error(string memory reason) {
emit Error(string(abi.encodePacked("USDC to Third Token swap failed: ", reason)));
revert(string(abi.encodePacked("Swap USDC to Third Token failed: ", reason)));
} catch {
emit Error("USDC to Third Token swap failed - Unknown reason");
revert("Swap USDC to Third Token failed - Unknown");
}
uint thirdReceived = thirdAmounts[1];
3. Third Token -> BNB
emit Debug("Preparing Third Token to BNB swap", thirdReceived);
uint[] memory bnbAmounts;
try PancakeswapRouter.swapExactTokensForETH(
thirdReceived,
0,
pathThirdToBnb,
address(this),
block.timestamp + 300
) returns (uint[] memory amounts) {
bnbAmounts = amounts;
emit Debug("BNB Received Back", bnbAmounts[1]);
} catch Error(string memory reason) {
emit Error(string(abi.encodePacked("Third Token to BNB swap failed: ", reason)));
revert(string(abi.encodePacked("Swap Third Token to BNB failed: ", reason)));
} catch {
emit Error("Third Token to BNB swap failed - Unknown reason");
revert("Swap Third Token to BNB failed - Unknown");
}
uint bnbReceived = bnbAmounts[1];
emit Debug("Checking profit", bnbReceived);
if (bnbReceived > initialBnb) {
payable(aiAgent).transfer(bnbReceived);
emit Debug("Profit", bnbReceived - initialBnb);
} else {
emit Error("No profit");
revert("No arbitrage profit");
}
}
```