matcha => 0x

This commit is contained in:
Thrilok Kumar 2021-03-07 18:00:23 +05:30
parent c2dd118e89
commit 852890c7a6

View File

@ -6,11 +6,11 @@ import {TokenInterface, MemoryInterface, EventInterface} from "../common/interfa
import {Stores} from "../common/stores.sol"; import {Stores} from "../common/stores.sol";
import {DSMath} from "../common/math.sol"; import {DSMath} from "../common/math.sol";
contract MatchaHelpers is Stores, DSMath { contract Helpers is Stores, DSMath {
/** /**
* @dev Return Matcha Address * @dev Return 0x Address
*/ */
function getMatchaAddress() internal pure returns (address) { function get0xAddress() internal pure returns (address) {
return 0xDef1C0ded9bec7F1a1670819833240f027b25EfF; return 0xDef1C0ded9bec7F1a1670819833240f027b25EfF;
} }
@ -31,10 +31,7 @@ contract MatchaHelpers is Stores, DSMath {
sellDec = address(sellAddr) == getEthAddr() ? 18 : sellAddr.decimals(); sellDec = address(sellAddr) == getEthAddr() ? 18 : sellAddr.decimals();
} }
} struct SwapData {
contract MatchaResolver is MatchaHelpers {
struct MatchaData {
TokenInterface sellToken; TokenInterface sellToken;
TokenInterface buyToken; TokenInterface buyToken;
uint256 _sellAmt; uint256 _sellAmt;
@ -43,16 +40,32 @@ contract MatchaResolver is MatchaHelpers {
bytes callData; bytes callData;
} }
function matchaSwap(MatchaData memory matchaData, uint256 wethAmt) internal returns (uint256 buyAmt) { }
TokenInterface buyToken = matchaData.buyToken;
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(buyToken, matchaData.sellToken); contract EventResolver is Helpers {
uint256 _sellAmt18 = convertTo18(_sellDec, matchaData._sellAmt); event LogSwap(address indexed buyToken, address indexed sellToken, uint256 buyAmt, uint256 sellAmt, uint256 getId, uint256 setId);
uint256 _slippageAmt = convert18ToDec(_buyDec, wmul(matchaData.unitAmt, _sellAmt18));
function emitLogSwap(SwapData memory swapData, uint256 setId) internal {
bytes32 _eventCode;
bytes memory _eventParam;
emit LogSwap(address(swapData.buyToken), address(swapData.sellToken), swapData._buyAmt, swapData._sellAmt, 0, setId);
_eventCode = keccak256("LogSwap(address,address,uint256,uint256,uint256,uint256)");
_eventParam = abi.encode(address(swapData.buyToken), address(swapData.sellToken), swapData._buyAmt, swapData._sellAmt, 0, setId);
emitEvent(_eventCode, _eventParam);
}
}
contract Resolver is EventResolver {
function _swapHelper(SwapData memory swapData, uint256 wethAmt) internal returns (uint256 buyAmt) {
TokenInterface buyToken = swapData.buyToken;
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(buyToken, swapData.sellToken);
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
uint256 _slippageAmt = convert18ToDec(_buyDec, wmul(swapData.unitAmt, _sellAmt18));
uint256 initalBal = getTokenBal(buyToken); uint256 initalBal = getTokenBal(buyToken);
(bool success, ) = address(getMatchaAddress()).call.value(wethAmt)(matchaData.callData); (bool success, ) = address(get0xAddress()).call.value(wethAmt)(swapData.callData);
if (!success) revert("matcha-swap-failed"); if (!success) revert("0x-swap-failed");
uint256 finalBal = getTokenBal(buyToken); uint256 finalBal = getTokenBal(buyToken);
@ -60,42 +73,27 @@ contract MatchaResolver is MatchaHelpers {
require(_slippageAmt <= buyAmt, "Too much slippage"); require(_slippageAmt <= buyAmt, "Too much slippage");
} }
}
contract MatchaEventResolver is MatchaResolver { function _swap(SwapData memory swapData, uint256 setId) internal {
event LogSwap(address indexed buyToken, address indexed sellToken, uint256 buyAmt, uint256 sellAmt, uint256 getId, uint256 setId); TokenInterface _sellAddr = swapData.sellToken;
function emitLogSwap(MatchaData memory matchaData, uint256 setId) internal {
bytes32 _eventCode;
bytes memory _eventParam;
emit LogSwap(address(matchaData.buyToken), address(matchaData.sellToken), matchaData._buyAmt, matchaData._sellAmt, 0, setId);
_eventCode = keccak256("LogSwap(address,address,uint256,uint256,uint256,uint256)");
_eventParam = abi.encode(address(matchaData.buyToken), address(matchaData.sellToken), matchaData._buyAmt, matchaData._sellAmt, 0, setId);
emitEvent(_eventCode, _eventParam);
}
}
contract MatchaResolverHelpers is MatchaEventResolver {
function _swap(MatchaData memory matchaData, uint256 setId) internal {
TokenInterface _sellAddr = matchaData.sellToken;
uint256 ethAmt; uint256 ethAmt;
if (address(_sellAddr) == getEthAddr()) { if (address(_sellAddr) == getEthAddr()) {
ethAmt = matchaData._sellAmt; ethAmt = swapData._sellAmt;
} else { } else {
TokenInterface(_sellAddr).approve(getMatchaAddress(), matchaData._sellAmt); TokenInterface(_sellAddr).approve(get0xAddress(), swapData._sellAmt);
} }
matchaData._buyAmt = matchaSwap(matchaData, ethAmt); swapData._buyAmt = _swapHelper(swapData, ethAmt);
setUint(setId, matchaData._buyAmt); setUint(setId, swapData._buyAmt);
emitLogSwap(matchaData, setId); emitLogSwap(swapData, setId);
} }
} }
contract Matcha is MatchaResolverHelpers { contract Connector is Resolver {
/** /**
* @dev Swap ETH/ERC20_Token using 0x. * @dev Swap ETH/ERC20_Token using 0x.
* @param buyAddr buying token address.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param buyAddr buying token address.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
@ -113,8 +111,8 @@ contract Matcha is MatchaResolverHelpers {
bytes calldata callData, bytes calldata callData,
uint256 setId uint256 setId
) external payable { ) external payable {
MatchaData memory matchaData = SwapData memory swapData =
MatchaData({ SwapData({
buyToken: TokenInterface(buyAddr), buyToken: TokenInterface(buyAddr),
sellToken: TokenInterface(sellAddr), sellToken: TokenInterface(sellAddr),
unitAmt: unitAmt, unitAmt: unitAmt,
@ -123,10 +121,10 @@ contract Matcha is MatchaResolverHelpers {
_buyAmt: 0 _buyAmt: 0
}); });
_swap(matchaData, setId); _swap(swapData, setId);
} }
} }
contract ConnectMatcha is MatchaResolver { contract Connect0x is Connector {
string public name = "Matcha-v1"; string public name = "0x-v1";
} }