Merge pull request #31 from propeller-heads/router/dc/ENG-4047-generic-callback
feat: Implement generic callback
This commit is contained in:
@@ -11,8 +11,6 @@ interface ICallbackVerifier {
|
|||||||
external
|
external
|
||||||
returns (
|
returns (
|
||||||
uint256 amountOwed,
|
uint256 amountOwed,
|
||||||
uint256 amountReceived,
|
address tokenOwed
|
||||||
address tokenOwed,
|
|
||||||
uint16 dataOffset
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,12 +50,7 @@ contract CallbackVerificationDispatcher {
|
|||||||
function _callVerifyCallback(bytes calldata data)
|
function _callVerifyCallback(bytes calldata data)
|
||||||
internal
|
internal
|
||||||
view
|
view
|
||||||
returns (
|
returns (uint256 amountOwed, address tokenOwed)
|
||||||
uint256 amountOwed,
|
|
||||||
uint256 amountReceived,
|
|
||||||
address tokenOwed,
|
|
||||||
uint16 dataOffset
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
address verifier;
|
address verifier;
|
||||||
bytes4 decodedSelector;
|
bytes4 decodedSelector;
|
||||||
@@ -87,8 +82,7 @@ contract CallbackVerificationDispatcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(amountOwed, amountReceived, tokenOwed, dataOffset) =
|
(amountOwed, tokenOwed) = abi.decode(result, (uint256, address));
|
||||||
abi.decode(result, (uint256, uint256, address, uint16));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// slither-disable-next-line dead-code
|
// slither-disable-next-line dead-code
|
||||||
|
|||||||
@@ -79,7 +79,17 @@ contract TychoRouter is
|
|||||||
* caller is not a pool.
|
* caller is not a pool.
|
||||||
*/
|
*/
|
||||||
fallback() external {
|
fallback() external {
|
||||||
// TODO execute generic callback
|
_executeGenericCallback(msg.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Check if the sender is correct and executes callback actions.
|
||||||
|
* @param msgData encoded data. It must includes data for the verification.
|
||||||
|
*/
|
||||||
|
function _executeGenericCallback(bytes calldata msgData) internal {
|
||||||
|
(uint256 amountOwed, address tokenOwed) = _callVerifyCallback(msgData);
|
||||||
|
|
||||||
|
IERC20(tokenOwed).safeTransfer(msg.sender, amountOwed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -10,12 +10,7 @@ contract CallbackVerificationDispatcherExposed is
|
|||||||
function exposedCallVerifier(bytes calldata data)
|
function exposedCallVerifier(bytes calldata data)
|
||||||
external
|
external
|
||||||
view
|
view
|
||||||
returns (
|
returns (uint256 amountOwed, address tokenOwed)
|
||||||
uint256 amountOwed,
|
|
||||||
uint256 amountReceived,
|
|
||||||
address tokenOwed,
|
|
||||||
uint16 dataOffset
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
return _callVerifyCallback(data);
|
return _callVerifyCallback(data);
|
||||||
}
|
}
|
||||||
@@ -96,20 +91,16 @@ contract CallbackVerificationDispatcherTest is Constants {
|
|||||||
bytes memory data =
|
bytes memory data =
|
||||||
hex"2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e876b20f8a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
|
hex"2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e876b20f8a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
|
||||||
vm.startPrank(address(0xD0b2F5018B5D22759724af6d4281AC0B13266360));
|
vm.startPrank(address(0xD0b2F5018B5D22759724af6d4281AC0B13266360));
|
||||||
(
|
(uint256 amountOwed, address tokenOwed) =
|
||||||
uint256 amountOwed,
|
dispatcherExposed.exposedCallVerifier(data);
|
||||||
uint256 amountReceived,
|
|
||||||
address tokenOwed,
|
|
||||||
uint16 dataOffset
|
|
||||||
) = dispatcherExposed.exposedCallVerifier(data);
|
|
||||||
vm.stopPrank();
|
vm.stopPrank();
|
||||||
|
|
||||||
// The values themselves are irrelevant, we just need to make sure that we
|
// The specific values returned are not important for this test.
|
||||||
// correctly parse the expected output of the existing Maverick verifier
|
// The goal is to ensure correct calling of the Maverick verifier's.
|
||||||
|
// Since the verifier's output format has changed, the asserted values may not be meaningful.
|
||||||
|
// Full validation of the functionality will be covered in the integration tests.
|
||||||
assert(amountOwed == 1);
|
assert(amountOwed == 1);
|
||||||
assert(amountReceived == 1);
|
assert(tokenOwed == address(0x0000000000000000000000000000000000000001));
|
||||||
assert(tokenOwed == address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48));
|
|
||||||
assert(dataOffset == 148);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testCallVerifierNoSelector() public {
|
function testCallVerifierNoSelector() public {
|
||||||
@@ -124,20 +115,16 @@ contract CallbackVerificationDispatcherTest is Constants {
|
|||||||
bytes memory data =
|
bytes memory data =
|
||||||
hex"2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e8000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
|
hex"2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e8000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
|
||||||
vm.startPrank(address(0xD0b2F5018B5D22759724af6d4281AC0B13266360));
|
vm.startPrank(address(0xD0b2F5018B5D22759724af6d4281AC0B13266360));
|
||||||
(
|
(uint256 amountOwed, address tokenOwed) =
|
||||||
uint256 amountOwed,
|
dispatcherExposed.exposedCallVerifier(data);
|
||||||
uint256 amountReceived,
|
|
||||||
address tokenOwed,
|
|
||||||
uint16 dataOffset
|
|
||||||
) = dispatcherExposed.exposedCallVerifier(data);
|
|
||||||
vm.stopPrank();
|
vm.stopPrank();
|
||||||
|
|
||||||
// The values themselves are irrelevant, we just need to make sure that we
|
// The specific values returned are not important for this test.
|
||||||
// correctly parse the expected output of the existing Maverick verifier
|
// The goal is to ensure correct calling of the Maverick verifier's.
|
||||||
|
// Since the verifier's output format has changed, the asserted values may not be meaningful.
|
||||||
|
// Full validation of the functionality will be covered in the integration tests.
|
||||||
assert(amountOwed == 1);
|
assert(amountOwed == 1);
|
||||||
assert(amountReceived == 1);
|
assert(tokenOwed == address(0x0000000000000000000000000000000000000001));
|
||||||
assert(tokenOwed == address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48));
|
|
||||||
assert(dataOffset == 148);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testCallVerifierBadSelector() public {
|
function testCallVerifierBadSelector() public {
|
||||||
|
|||||||
@@ -234,7 +234,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
|
|||||||
bytes[] memory swaps = new bytes[](1);
|
bytes[] memory swaps = new bytes[](1);
|
||||||
swaps[0] = swap;
|
swaps[0] = swap;
|
||||||
|
|
||||||
tychoRouter.ExposedSwap(amountIn, 2, pleEncode(swaps));
|
tychoRouter.exposedSwap(amountIn, 2, pleEncode(swaps));
|
||||||
|
|
||||||
uint256 daiBalance = IERC20(DAI_ADDR).balanceOf(tychoRouterAddr);
|
uint256 daiBalance = IERC20(DAI_ADDR).balanceOf(tychoRouterAddr);
|
||||||
assertEq(daiBalance, 2630432278145144658455);
|
assertEq(daiBalance, 2630432278145144658455);
|
||||||
@@ -271,7 +271,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
|
|||||||
encodeUniswapV2Swap(DAI_ADDR, DAI_USDC_POOL, tychoRouterAddr, true)
|
encodeUniswapV2Swap(DAI_ADDR, DAI_USDC_POOL, tychoRouterAddr, true)
|
||||||
);
|
);
|
||||||
|
|
||||||
tychoRouter.ExposedSwap(amountIn, 3, pleEncode(swaps));
|
tychoRouter.exposedSwap(amountIn, 3, pleEncode(swaps));
|
||||||
|
|
||||||
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(tychoRouterAddr);
|
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(tychoRouterAddr);
|
||||||
assertEq(usdcBalance, 2610580090);
|
assertEq(usdcBalance, 2610580090);
|
||||||
@@ -332,7 +332,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
|
|||||||
encodeUniswapV2Swap(DAI_ADDR, DAI_USDC_POOL, tychoRouterAddr, true)
|
encodeUniswapV2Swap(DAI_ADDR, DAI_USDC_POOL, tychoRouterAddr, true)
|
||||||
);
|
);
|
||||||
|
|
||||||
tychoRouter.ExposedSwap(amountIn, 4, pleEncode(swaps));
|
tychoRouter.exposedSwap(amountIn, 4, pleEncode(swaps));
|
||||||
|
|
||||||
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(tychoRouterAddr);
|
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(tychoRouterAddr);
|
||||||
assertEq(usdcBalance, 2581503157);
|
assertEq(usdcBalance, 2581503157);
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ contract TychoRouterExposed is TychoRouter {
|
|||||||
return _unwrapETH(amount);
|
return _unwrapETH(amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ExposedSwap(
|
function exposedSwap(
|
||||||
uint256 amountIn,
|
uint256 amountIn,
|
||||||
uint256 nTokens,
|
uint256 nTokens,
|
||||||
bytes calldata swaps
|
bytes calldata swaps
|
||||||
|
|||||||
Reference in New Issue
Block a user