Skip to content

Commit

Permalink
Use wrap up to scripts in QuarkBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincheng96 committed Nov 18, 2024
1 parent 108c525 commit 5672d15
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 50 deletions.
9 changes: 9 additions & 0 deletions src/builder/QuarkBuilderBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ contract QuarkBuilderBase {
uint256 supplementalBalance = HashMap.contains(assetsBridged, abi.encode(assetSymbolOut))
? HashMap.getUint256(assetsBridged, abi.encode(assetSymbolOut))
: 0;
// Note: Right now, ETH/WETH is only bridged via Across. Across has a weird quirk where it will send ETH to EOAs and
// WETH to contracts. Since the QuarkBuilder cannot know if a QuarkWallet is deployed before the operation is actually
// executed on-chain, it needs to handle the worst case scenario and assume wrapping will be needed.
if (
Strings.stringEqIgnoreCase(assetSymbolOut, "ETH")
|| Strings.stringEqIgnoreCase(assetSymbolOut, "WETH")
) {
supplementalBalance = 0;
}
checkAndInsertWrapOrUnwrapAction({
actions: actions,
quarkOperations: quarkOperations,
Expand Down
4 changes: 2 additions & 2 deletions src/builder/TokenWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ library TokenWrapper {
{
if (Strings.stringEqIgnoreCase(tokenSymbol, "ETH")) {
return abi.encodeWithSelector(
WrapperActions.wrapETH.selector, getKnownWrapperTokenPair(chainId, tokenSymbol).wrapper, amount
WrapperActions.wrapETHUpTo.selector, getKnownWrapperTokenPair(chainId, tokenSymbol).wrapper, amount
);
} else if (Strings.stringEqIgnoreCase(tokenSymbol, "stETH")) {
return abi.encodeWithSelector(
Expand All @@ -148,7 +148,7 @@ library TokenWrapper {
{
if (Strings.stringEqIgnoreCase(tokenSymbol, "WETH")) {
return abi.encodeWithSelector(
WrapperActions.unwrapWETH.selector, getKnownWrapperTokenPair(chainId, tokenSymbol).wrapper, amount
WrapperActions.unwrapWETHUpTo.selector, getKnownWrapperTokenPair(chainId, tokenSymbol).wrapper, amount
);
} else if (Strings.stringEqIgnoreCase(tokenSymbol, "wstETH")) {
return abi.encodeWithSelector(
Expand Down
36 changes: 17 additions & 19 deletions test/builder/BridgingLogic.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {PaymentInfo} from "src/builder/PaymentInfo.sol";
import {QuarkBuilder} from "src/builder/QuarkBuilder.sol";
import {QuarkBuilderBase} from "src/builder/QuarkBuilderBase.sol";
import {Quotecall} from "src/Quotecall.sol";
import {TokenWrapper} from "src/builder/TokenWrapper.sol";
import {YulHelper} from "test/lib/YulHelper.sol";

import {AcrossFFI} from "test/builder/mocks/AcrossFFI.sol";
Expand Down Expand Up @@ -69,6 +70,10 @@ contract BridgingLogicTest is Test, QuarkBuilderTest {

assertEq(result.paymentCurrency, "usd", "usd currency");

address multicallAddress = CodeJarHelper.getCodeAddress(type(Multicall).creationCode);
address wrapperActionsAddress = CodeJarHelper.getCodeAddress(type(WrapperActions).creationCode);
address transferActionsAddress = CodeJarHelper.getCodeAddress(type(TransferActions).creationCode);

// Check the quark operations
assertEq(result.quarkOperations.length, 2, "two operations");
assertEq(
Expand Down Expand Up @@ -122,28 +127,21 @@ contract BridgingLogicTest is Test, QuarkBuilderTest {

assertEq(
result.quarkOperations[1].scriptAddress,
address(
uint160(
uint256(
keccak256(
abi.encodePacked(
bytes1(0xff),
/* codeJar address */
address(CodeJarHelper.CODE_JAR_ADDRESS),
uint256(0),
/* script bytecode */
keccak256(type(TransferActions).creationCode)
)
)
)
)
),
"script address for transfer is correct given the code jar address"
multicallAddress,
"script address for Multicall is correct given the code jar address"
);
address[] memory callContracts = new address[](2);
callContracts[0] = wrapperActionsAddress;
callContracts[1] = transferActionsAddress;
bytes[] memory callDatas = new bytes[](2);
callDatas[0] = abi.encodeWithSelector(
WrapperActions.wrapETHUpTo.selector, TokenWrapper.getKnownWrapperTokenPair(8453, "WETH").wrapper, 1e18
);
callDatas[1] = abi.encodeCall(TransferActions.transferERC20Token, (weth_(8453), address(0xceecee), 1e18));
assertEq(
result.quarkOperations[1].scriptCalldata,
abi.encodeCall(TransferActions.transferERC20Token, (weth_(8453), address(0xceecee), 1e18)),
"calldata is TransferActions.transferERC20Token(USDC_8453, address(0xceecee), 5e6);"
abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas),
"calldata is Multicall.run([wrapperActionsAddress, transferActionsAddress], [WrapperActions.wrapETHUpTo(USDC_8453, 1e18), TransferActions.transferERC20Token(USDC_8453, address(0xceecee), 1e18))"
);
assertEq(
result.quarkOperations[1].expiry, BLOCK_TIMESTAMP + 7 days, "expiry is current blockTimestamp + 7 days"
Expand Down
7 changes: 4 additions & 3 deletions test/builder/QuarkBuilderCometBorrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,17 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
callContracts[0] = wrapperActionsAddress;
callContracts[1] = cometSupplyMultipleAssetsAndBorrowAddress;
bytes[] memory callDatas = new bytes[](2);
callDatas[0] =
abi.encodeWithSelector(WrapperActions.wrapETH.selector, 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18);
callDatas[0] = abi.encodeWithSelector(
WrapperActions.wrapETHUpTo.selector, 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18
);
callDatas[1] = abi.encodeCall(
CometSupplyMultipleAssetsAndBorrow.run, (cometUsdc_(1), collateralTokens, collateralAmounts, usdc_(1), 1e6)
);

assertEq(
result.quarkOperations[0].scriptCalldata,
abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas),
"calldata is Multicall.run([wrapperActionsAddress, cometSupplyMultipleAssetsAndBorrowAddress], [WrapperActions.wrapWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 10e18), CometSupplyMultipleAssetsAndBorrow.run(COMET_1, collateralTokens, collateralAmounts, usdc_(1), 1e6)"
"calldata is Multicall.run([wrapperActionsAddress, cometSupplyMultipleAssetsAndBorrowAddress], [WrapperActions.wrapETHUpTo(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 10e18), CometSupplyMultipleAssetsAndBorrow.run(COMET_1, collateralTokens, collateralAmounts, usdc_(1), 1e6)"
);
assertEq(result.quarkOperations[0].scriptSources.length, 3);
assertEq(result.quarkOperations[0].scriptSources[0], type(WrapperActions).creationCode);
Expand Down
7 changes: 4 additions & 3 deletions test/builder/QuarkBuilderCometRepay.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,9 @@ contract QuarkBuilderCometRepayTest is Test, QuarkBuilderTest {
callContracts[0] = wrapperActionsAddress;
callContracts[1] = cometRepayAndWithdrawMultipleAssetsAddress;
bytes[] memory callDatas = new bytes[](2);
callDatas[0] =
abi.encodeWithSelector(WrapperActions.wrapETH.selector, 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18);
callDatas[0] = abi.encodeWithSelector(
WrapperActions.wrapETHUpTo.selector, 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18
);
callDatas[1] = abi.encodeCall(
CometRepayAndWithdrawMultipleAssets.run,
(cometWeth_(1), collateralTokens, collateralAmounts, weth_(1), 1e18)
Expand All @@ -300,7 +301,7 @@ contract QuarkBuilderCometRepayTest is Test, QuarkBuilderTest {
assertEq(
result.quarkOperations[0].scriptCalldata,
abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas),
"calldata is Multicall.run([wrapperActionsAddress, cometRepayAndWithdrawMultipleAssetsAddress], [WrapperActions.wrapWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18), CometRepayAndWithdrawMultipleAssets.run(COMET_1_WETH, collateralTokens, collateralAmounts, weth_(1), 1e18)"
"calldata is Multicall.run([wrapperActionsAddress, cometRepayAndWithdrawMultipleAssetsAddress], [WrapperActions.wrapETHUpTo(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18), CometRepayAndWithdrawMultipleAssets.run(COMET_1_WETH, collateralTokens, collateralAmounts, weth_(1), 1e18)"
);
assertEq(result.quarkOperations[0].scriptSources.length, 3);
assertEq(result.quarkOperations[0].scriptSources[0], type(WrapperActions).creationCode);
Expand Down
7 changes: 4 additions & 3 deletions test/builder/QuarkBuilderCometSupply.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,14 @@ contract QuarkBuilderCometSupplyTest is Test, QuarkBuilderTest {
callContracts[0] = wrapperActionsAddress;
callContracts[1] = cometSupplyActionsAddress;
bytes[] memory callDatas = new bytes[](2);
callDatas[0] =
abi.encodeWithSelector(WrapperActions.wrapETH.selector, 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18);
callDatas[0] = abi.encodeWithSelector(
WrapperActions.wrapETHUpTo.selector, 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18
);
callDatas[1] = abi.encodeCall(CometSupplyActions.supply, (COMET_ETH, weth_(1), 1e18));
assertEq(
result.quarkOperations[0].scriptCalldata,
abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas),
"calldata is Multicall.run([wrapperActionsAddress, cometSupplyActionsAddress], [WrapperActions.wrapWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18), CometSupplyActions.supply(COMET_ETH, weth_(1), 1e18)"
"calldata is Multicall.run([wrapperActionsAddress, cometSupplyActionsAddress], [WrapperActions.wrapETHUpTo(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18), CometSupplyActions.supply(COMET_ETH, weth_(1), 1e18)"
);
assertEq(
result.quarkOperations[0].expiry, BLOCK_TIMESTAMP + 7 days, "expiry is current blockTimestamp + 3 days"
Expand Down
4 changes: 2 additions & 2 deletions test/builder/QuarkBuilderMorphoBorrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ contract QuarkBuilderMorphoBorrowTest is Test, QuarkBuilderTest {
callContracts[1] = MorphoActionsAddress;
bytes[] memory callDatas = new bytes[](2);
callDatas[0] = abi.encodeWithSelector(
WrapperActions.wrapETH.selector, TokenWrapper.getKnownWrapperTokenPair(8453, "WETH").wrapper, 1e18
WrapperActions.wrapETHUpTo.selector, TokenWrapper.getKnownWrapperTokenPair(8453, "WETH").wrapper, 1e18
);
callDatas[1] = abi.encodeCall(
MorphoActions.supplyCollateralAndBorrow,
Expand All @@ -243,7 +243,7 @@ contract QuarkBuilderMorphoBorrowTest is Test, QuarkBuilderTest {
assertEq(
result.quarkOperations[0].scriptCalldata,
abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas),
"calldata is Multicall.run([wrapperActionsAddress, MorphoActionsAddress], [WrapperActions.wrapWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 10e18), MorphoActions.supplyCollateralAndBorrow(MorphoInfo.getMorphoAddress(8453), MorphoInfo.getMarketParams(8453, WETH, USDC), 1e18, 1e6, address(0xa11ce), address(0xa11ce))"
"calldata is Multicall.run([wrapperActionsAddress, MorphoActionsAddress], [WrapperActions.wrapETHUpTo(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 10e18), MorphoActions.supplyCollateralAndBorrow(MorphoInfo.getMorphoAddress(8453), MorphoInfo.getMarketParams(8453, WETH, USDC), 1e18, 1e6, address(0xa11ce), address(0xa11ce))"
);
assertEq(result.quarkOperations[0].scriptSources.length, 3);
assertEq(result.quarkOperations[0].scriptSources[0], type(WrapperActions).creationCode);
Expand Down
4 changes: 2 additions & 2 deletions test/builder/QuarkBuilderMorphoRepay.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ contract QuarkBuilderMorphoRepayTest is Test, QuarkBuilderTest {
callContracts[1] = morphoActionsAddress;
bytes[] memory callDatas = new bytes[](2);
callDatas[0] = abi.encodeWithSelector(
WrapperActions.wrapETH.selector, TokenWrapper.getKnownWrapperTokenPair(8453, "WETH").wrapper, 1e18
WrapperActions.wrapETHUpTo.selector, TokenWrapper.getKnownWrapperTokenPair(8453, "WETH").wrapper, 1e18
);
callDatas[1] = abi.encodeCall(
MorphoActions.repayAndWithdrawCollateral,
Expand All @@ -255,7 +255,7 @@ contract QuarkBuilderMorphoRepayTest is Test, QuarkBuilderTest {
assertEq(
result.quarkOperations[0].scriptCalldata,
abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas),
"calldata is Multicall.run([wrapperActionsAddress, morphoActionsAddress], [WrapperActions.wrapWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18), MorphoActions.repayAndWithdrawCollateral(MorphoInfo.getMorphoAddress(8453), MorphoInfo.getMarketParams(8453, WETH, USDC), 1e18, 0, 0e18, address(0xa11ce), address(0xa11ce))"
"calldata is Multicall.run([wrapperActionsAddress, morphoActionsAddress], [WrapperActions.wrapETHUpTo(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18), MorphoActions.repayAndWithdrawCollateral(MorphoInfo.getMorphoAddress(8453), MorphoInfo.getMarketParams(8453, WETH, USDC), 1e18, 0, 0e18, address(0xa11ce), address(0xa11ce))"
);
assertEq(result.quarkOperations[0].scriptSources.length, 3);
assertEq(result.quarkOperations[0].scriptSources[0], type(WrapperActions).creationCode);
Expand Down
7 changes: 4 additions & 3 deletions test/builder/QuarkBuilderMorphoVaultSupply.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,15 @@ contract QuarkBuilderMorphoVaultTest is Test, QuarkBuilderTest {
callContracts[0] = wrapperActionsAddress;
callContracts[1] = morphoVaultActionsAddress;
bytes[] memory callDatas = new bytes[](2);
callDatas[0] =
abi.encodeWithSelector(WrapperActions.wrapETH.selector, 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18);
callDatas[0] = abi.encodeWithSelector(
WrapperActions.wrapETHUpTo.selector, 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18
);
callDatas[1] =
abi.encodeCall(MorphoVaultActions.deposit, (MorphoInfo.getMorphoVaultAddress(1, "WETH"), weth_(1), 1e18));
assertEq(
result.quarkOperations[0].scriptCalldata,
abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas),
"calldata is Multicall.run([wrapperActionsAddress, morphoVaultActionsAddress], [WrapperActions.wrapWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18), MorphoVaultActions.deposit(MorphoInfo.getMorphoVaultAddress(1, WETH), weth_(1), 1e18)"
"calldata is Multicall.run([wrapperActionsAddress, morphoVaultActionsAddress], [WrapperActions.wrapETHUpTo(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18), MorphoVaultActions.deposit(MorphoInfo.getMorphoVaultAddress(1, WETH), weth_(1), 1e18)"
);
assertEq(
result.quarkOperations[0].expiry, BLOCK_TIMESTAMP + 7 days, "expiry is current blockTimestamp + 3 days"
Expand Down
7 changes: 4 additions & 3 deletions test/builder/QuarkBuilderSwap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,15 @@ contract QuarkBuilderSwapTest is Test, QuarkBuilderTest {
callContracts[0] = wrapperActionsAddress;
callContracts[1] = approveAndSwapAddress;
bytes[] memory callDatas = new bytes[](2);
callDatas[0] =
abi.encodeWithSelector(WrapperActions.wrapETH.selector, 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18);
callDatas[0] = abi.encodeWithSelector(
WrapperActions.wrapETHUpTo.selector, 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18
);
callDatas[1] =
abi.encodeCall(ApproveAndSwap.run, (ZERO_EX_ENTRY_POINT, WETH_1, 1e18, USDC_1, 3000e6, ZERO_EX_SWAP_DATA));
assertEq(
result.quarkOperations[0].scriptCalldata,
abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas),
"calldata is Multicall.run([wrapperActionsAddress, approveAndSwapAddress], [WrapperActions.wrapWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18), ApproveAndSwap.run (ZERO_EX_ENTRY_POINT, WETH_1, 1e18, USDC_1, 3000e6, ZERO_EX_SWAP_DATA)]);"
"calldata is Multicall.run([wrapperActionsAddress, approveAndSwapAddress], [WrapperActions.wrapETHUpTo(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 1e18), ApproveAndSwap.run (ZERO_EX_ENTRY_POINT, WETH_1, 1e18, USDC_1, 3000e6, ZERO_EX_SWAP_DATA)]);"
);
assertEq(
result.quarkOperations[0].expiry, BLOCK_TIMESTAMP + 3 days, "expiry is current blockTimestamp + 3 days"
Expand Down
Loading

0 comments on commit 5672d15

Please sign in to comment.