EVM Implementation
On-Chain AMM Calls to MAYAChain
SwapIn
SwapOut
Last updated
Was this helpful?
Was this helpful?
function swapIn(
address tcVault,
address tcRouter,
string calldata tcMemo,
address token,
uint amount,
uint amountOutMin,
uint256 deadline
) public nonReentrant {
uint256 _safeAmount = safeTransferFrom(token, amount); // Transfer asset
safeApprove(token, address(swapRouter), amount);
address[] memory path = new address[](2);
path[0] = token; path[1] = WETH;
swapRouter.swapExactTokensForETH(_safeAmount, amountOutMin, path, address(this), deadline);
_safeAmount = address(this).balance;
iROUTER(tcRouter).depositWithExpiry{value:_safeAmount}(payable(tcVault), ETH, _safeAmount, tcMemo, deadline);
}function transferOutAndCall(address payable target, address finalToken, address to, uint256 amountOutMin, string memory memo) public payable nonReentrant {
uint256 _safeAmount = msg.value;
(bool erc20Success, ) = target.call{value:_safeAmount}(abi.encodeWithSignature("swapOut(address,address,uint256)", finalToken, to, amountOutMin));
if (!erc20Success) {
bool ethSuccess = payable(to).send(_safeAmount); // If can't swap, just send the recipient the ETH
if (!ethSuccess) {
payable(address(msg.sender)).transfer(_safeAmount); // For failure, bounce back to Yggdrasil & continue.
}
}
emit TransferOutAndCall(msg.sender, target, _safeAmount, finalToken, to, amountOutMin, memo);
}function swapOut(address token, address to, uint256 amountOutMin) public payable nonReentrant {
address[] memory path = nelw address[](2);
path[0] = WETH; path[1] = token;
swapRouter.swapExactETHForTokens{value: msg.value}(amountOutMin, path, to, type(uint).max);
}