Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: batchSwap accounting #49

Merged
merged 3 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions src/balancer/BalancerController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,35 @@ contract BalancerController is IController {
returns (bool, address[] memory, address[] memory)
{
(
,
IVault.SwapKind kind,
IVault.BatchSwapStep[] memory swaps,
IAsset[] memory assets,
,
,
) = abi.decode(data, (
uint8, IVault.BatchSwapStep[], IAsset[], IVault.FundManagement, uint256[], uint256
IVault.SwapKind,
IVault.BatchSwapStep[],
IAsset[],
IVault.FundManagement,
uint256[],
uint256
)
);

if (!isMultiHopSwap(swaps))
return (false, new address[](0), new address[](0));

uint tokenInIndex = swaps[swaps.length - 1].assetOutIndex;
uint tokenOutIndex = swaps[0].assetInIndex;
uint tokenInIndex;
uint tokenOutIndex;

if (kind == IVault.SwapKind.GIVEN_IN) {
if (!isMultiHopSwapGivenIn(swaps))
return (false, new address[](0), new address[](0));
tokenInIndex = swaps[swaps.length - 1].assetOutIndex;
tokenOutIndex = swaps[0].assetInIndex;
} else {
if (!isMultiHopSwapGivenOut(swaps))
return (false, new address[](0), new address[](0));
tokenOutIndex = swaps[swaps.length - 1].assetInIndex;
tokenInIndex = swaps[0].assetOutIndex;
}

address[] memory tokensIn;
address[] memory tokensOut;
Expand Down Expand Up @@ -228,14 +242,33 @@ contract BalancerController is IController {
);
}

function isMultiHopSwap(IVault.BatchSwapStep[] memory swaps)
function isMultiHopSwapGivenIn(IVault.BatchSwapStep[] memory swaps)
internal
pure
returns (bool)
{
uint steps = swaps.length;
for (uint i; i < steps - 1; i++) {
if (
swaps[i].assetOutIndex != swaps[i+1].assetInIndex ||
swaps[i+1].amount > 0
)
r0ohafza marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
return true;
}

function isMultiHopSwapGivenOut(IVault.BatchSwapStep[] memory swaps)
internal
pure
returns (bool)
{
uint steps = swaps.length;
for (uint i; i < steps - 1; i++) {
if (swaps[i].assetOutIndex != swaps[i+1].assetInIndex)
if (
swaps[i].assetInIndex != swaps[i+1].assetOutIndex ||
swaps[i+1].amount > 0
)
return false;
}
return true;
Expand Down
4 changes: 3 additions & 1 deletion src/balancer/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface IVault {
}

function batchSwap(
uint8 kind,
SwapKind kind,
BatchSwapStep[] memory swaps,
IAsset[] memory assets,
FundManagement memory funds,
Expand All @@ -74,4 +74,6 @@ interface IVault {
uint256 amount;
bytes userData;
}

enum SwapKind { GIVEN_IN, GIVEN_OUT }
}
157 changes: 157 additions & 0 deletions src/tests/Balancer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,161 @@ contract TestBalancer is TestBase {
assertEq(tokensOut[0], token);
assertEq(tokensIn.length, 1);
}

function testCanSwap() public {
// Setup
controllerFacade.toggleTokenAllowance(0xC0c293ce456fF0ED870ADd98a0828Dd4d2903DBF);

IVault.SingleSwap memory swap = IVault.SingleSwap(
"0",
0,
IAsset(0xCfCA23cA9CA720B6E98E3Eb9B6aa0fFC4a5C08B9),
IAsset(0xC0c293ce456fF0ED870ADd98a0828Dd4d2903DBF),
0,
"0"
);

IVault.FundManagement memory funds = IVault.FundManagement(
address(0),
false,
payable(address(0)),
false
);

bytes memory data = abi.encodeWithSelector(0x52bbbe29,
swap,
funds,
0,
0
);

// Test
(bool canCall, address[] memory tokensIn, address[] memory tokensOut)
= controllerFacade.canCall(vault, true, data);

// Assert
assertTrue(canCall);
assertEq(tokensOut[0], 0xCfCA23cA9CA720B6E98E3Eb9B6aa0fFC4a5C08B9);
assertEq(tokensIn[0], 0xC0c293ce456fF0ED870ADd98a0828Dd4d2903DBF);
assertEq(tokensIn.length, 1);
assertEq(tokensOut.length, 1);
}

function testCanBatchSwapGivenIn() public {
// Setup
controllerFacade.toggleTokenAllowance(0xC0c293ce456fF0ED870ADd98a0828Dd4d2903DBF);

int256[] memory limits = new int256[](3);

IAsset[] memory assets = new IAsset[](3);
assets[0] = IAsset(0xBA12222222228d8Ba445958a75a0704d566BF2C8);
assets[1] = IAsset(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
assets[2] = IAsset(0xC0c293ce456fF0ED870ADd98a0828Dd4d2903DBF);

IVault.BatchSwapStep memory swap1 = IVault.BatchSwapStep(
"0",
0,
1,
10,
"0"
);

IVault.BatchSwapStep memory swap2 = IVault.BatchSwapStep(
"0",
1,
2,
0,
"0"
);

IVault.FundManagement memory funds = IVault.FundManagement(
address(0),
false,
payable(address(0)),
false
);

IVault.BatchSwapStep[] memory swaps = new IVault.BatchSwapStep[](2);
swaps[0] = swap1;
swaps[1] = swap2;

bytes memory data = abi.encodeWithSelector(0x945bcec9,
IVault.SwapKind.GIVEN_IN,
swaps,
assets,
funds,
limits,
0
);

// Test
(bool canCall, address[] memory tokensIn, address[] memory tokensOut)
= controllerFacade.canCall(vault, true, data);

// Assert
assertTrue(canCall);
assertEq(tokensOut[0], 0xBA12222222228d8Ba445958a75a0704d566BF2C8);
assertEq(tokensIn[0], 0xC0c293ce456fF0ED870ADd98a0828Dd4d2903DBF);
assertEq(tokensIn.length, 1);
assertEq(tokensOut.length, 1);
}

function testCanBatchSwapGivenOut() public {
// Setup
controllerFacade.toggleTokenAllowance(0xC0c293ce456fF0ED870ADd98a0828Dd4d2903DBF);

int256[] memory limits = new int256[](3);

IAsset[] memory assets = new IAsset[](3);
assets[0] = IAsset(0xBA12222222228d8Ba445958a75a0704d566BF2C8);
assets[1] = IAsset(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
assets[2] = IAsset(0xC0c293ce456fF0ED870ADd98a0828Dd4d2903DBF);

IVault.BatchSwapStep memory swap1 = IVault.BatchSwapStep(
"0",
1,
2,
10,
"0"
);

IVault.BatchSwapStep memory swap2 = IVault.BatchSwapStep(
"0",
0,
1,
0,
"0"
);

IVault.FundManagement memory funds = IVault.FundManagement(
address(0),
false,
payable(address(0)),
false
);

IVault.BatchSwapStep[] memory swaps = new IVault.BatchSwapStep[](2);
swaps[0] = swap1;
swaps[1] = swap2;

bytes memory data = abi.encodeWithSelector(0x945bcec9,
IVault.SwapKind.GIVEN_OUT,
swaps,
assets,
funds,
limits,
0
);

// Test
(bool canCall, address[] memory tokensIn, address[] memory tokensOut)
= controllerFacade.canCall(vault, true, data);

// Assert
assertTrue(canCall);
assertEq(tokensOut[0], 0xBA12222222228d8Ba445958a75a0704d566BF2C8);
assertEq(tokensIn[0], 0xC0c293ce456fF0ED870ADd98a0828Dd4d2903DBF);
assertEq(tokensIn.length, 1);
assertEq(tokensOut.length, 1);
}
}