Skip to content

Commit

Permalink
Bridge via Hinting (#103)
Browse files Browse the repository at this point in the history
This patch simply reverses the order s.t. we bridge via Across if
possible, then CCTP otherwise based on a user hint (`preferAcross`).

Also, we move first-class support for console logs. Note: (a) we'll need
to implement console logs in Eth.swift, and (b) we should just update
`forge-std` and then use `import {console} from
"forge-std/console.sol";` but upgrading forge-std causes stack too deep
issues.
  • Loading branch information
hayesgm authored Nov 20, 2024
1 parent 5b863e8 commit 5c3d0d7
Show file tree
Hide file tree
Showing 21 changed files with 1,846 additions and 45 deletions.
7 changes: 5 additions & 2 deletions src/builder/QuarkBuilderBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ contract QuarkBuilderBase {
bool useQuotecall;
bool bridgeEnabled;
bool autoWrapperEnabled;
bool preferAcross;
}

/**
Expand Down Expand Up @@ -135,7 +136,8 @@ contract QuarkBuilderBase {
dstChainId: actionIntent.chainId,
recipient: actionIntent.actor,
blockTimestamp: actionIntent.blockTimestamp,
useQuotecall: actionIntent.useQuotecall
useQuotecall: actionIntent.useQuotecall,
preferAcross: actionIntent.preferAcross
}),
chainAccountsList,
payment
Expand Down Expand Up @@ -193,7 +195,8 @@ contract QuarkBuilderBase {
dstChainId: actionIntent.chainId,
recipient: actionIntent.actor,
blockTimestamp: actionIntent.blockTimestamp,
useQuotecall: actionIntent.useQuotecall
useQuotecall: actionIntent.useQuotecall,
preferAcross: actionIntent.preferAcross
}),
chainAccountsList,
payment
Expand Down
45 changes: 33 additions & 12 deletions src/builder/actions/Actions.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.8.27;

import {console} from "src/builder/console.sol";

import {Accounts} from "src/builder/Accounts.sol";
import {Across, BridgeRoutes, CCTP} from "src/builder/BridgeRoutes.sol";
import {CodeJarHelper} from "src/builder/CodeJarHelper.sol";
Expand Down Expand Up @@ -248,6 +250,7 @@ library Actions {
address recipient;
uint256 blockTimestamp;
bool useQuotecall;
bool preferAcross;
}

// Note: To avoid stack too deep errors
Expand Down Expand Up @@ -585,7 +588,8 @@ library Actions {
blockTimestamp: bridgeInfo.blockTimestamp
}),
payment,
bridgeInfo.useQuotecall
bridgeInfo.useQuotecall,
bridgeInfo.preferAcross
);

List.addAction(actions, action);
Expand All @@ -605,25 +609,40 @@ library Actions {
return (List.toQuarkOperationArray(quarkOperations), List.toActionArray(actions));
}

function bridgeAsset(BridgeAsset memory bridge, PaymentInfo.Payment memory payment, bool useQuotecall)
internal
pure
returns (IQuarkWallet.QuarkOperation memory, Action memory)
{
if (CCTP.canBridge(bridge.srcChainId, bridge.destinationChainId, bridge.assetSymbol)) {
return bridgeUSDC(bridge, payment, useQuotecall);
} else if (Across.canBridge(bridge.srcChainId, bridge.destinationChainId, bridge.assetSymbol)) {
return bridgeAcross(bridge, payment, useQuotecall);
function bridgeAsset(
BridgeAsset memory bridge,
PaymentInfo.Payment memory payment,
bool useQuotecall,
bool preferAcross
) internal pure returns (IQuarkWallet.QuarkOperation memory, Action memory) {
bool acrossCanBridge = Across.canBridge(bridge.srcChainId, bridge.destinationChainId, bridge.assetSymbol);
bool cctpCanBridge = CCTP.canBridge(bridge.srcChainId, bridge.destinationChainId, bridge.assetSymbol);

// Choose order of actions based on user bridge preference.
if (preferAcross) {
if (acrossCanBridge) {
return bridgeAcross(bridge, payment, useQuotecall);
} else if (cctpCanBridge) {
return bridgeCCTP(bridge, payment, useQuotecall);
}
} else {
revert BridgingUnsupportedForAsset();
if (cctpCanBridge) {
return bridgeCCTP(bridge, payment, useQuotecall);
} else if (acrossCanBridge) {
return bridgeAcross(bridge, payment, useQuotecall);
}
}

revert BridgingUnsupportedForAsset();
}

function bridgeUSDC(BridgeAsset memory bridge, PaymentInfo.Payment memory payment, bool useQuotecall)
function bridgeCCTP(BridgeAsset memory bridge, PaymentInfo.Payment memory payment, bool useQuotecall)
internal
pure
returns (IQuarkWallet.QuarkOperation memory, Action memory)
{
console.log("Bridging via CCTP", bridge.assetSymbol);

if (!Strings.stringEqIgnoreCase(bridge.assetSymbol, "USDC")) {
revert InvalidAssetForBridge();
}
Expand Down Expand Up @@ -688,6 +707,8 @@ library Actions {
pure
returns (IQuarkWallet.QuarkOperation memory, Action memory)
{
console.log("Bridging via Across", bridge.assetSymbol);

Accounts.ChainAccounts memory srcChainAccounts =
Accounts.findChainAccounts(bridge.srcChainId, bridge.chainAccountsList);

Expand Down
16 changes: 12 additions & 4 deletions src/builder/actions/CometActionsBuilder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ contract CometActionsBuilder is QuarkBuilderBase {
string[] collateralAssetSymbols;
address comet;
address repayer;
bool preferAcross;
}

function cometRepay(
Expand Down Expand Up @@ -83,7 +84,8 @@ contract CometActionsBuilder is QuarkBuilderBase {
chainId: repayIntent.chainId,
useQuotecall: useQuotecall,
bridgeEnabled: true,
autoWrapperEnabled: true
autoWrapperEnabled: true,
preferAcross: repayIntent.preferAcross
});
}

Expand Down Expand Up @@ -114,6 +116,7 @@ contract CometActionsBuilder is QuarkBuilderBase {
uint256[] collateralAmounts;
string[] collateralAssetSymbols;
address comet;
bool preferAcross;
}

function cometBorrow(
Expand Down Expand Up @@ -160,7 +163,8 @@ contract CometActionsBuilder is QuarkBuilderBase {
chainId: borrowIntent.chainId,
useQuotecall: useQuotecall,
bridgeEnabled: true,
autoWrapperEnabled: true
autoWrapperEnabled: true,
preferAcross: borrowIntent.preferAcross
});
}

Expand Down Expand Up @@ -189,6 +193,7 @@ contract CometActionsBuilder is QuarkBuilderBase {
uint256 chainId;
address comet;
address sender;
bool preferAcross;
}

function cometSupply(
Expand Down Expand Up @@ -245,7 +250,8 @@ contract CometActionsBuilder is QuarkBuilderBase {
chainId: cometSupplyIntent.chainId,
useQuotecall: isMaxSupply,
bridgeEnabled: true,
autoWrapperEnabled: true
autoWrapperEnabled: true,
preferAcross: cometSupplyIntent.preferAcross
}),
chainAccountsList: chainAccountsList,
payment: payment,
Expand All @@ -270,6 +276,7 @@ contract CometActionsBuilder is QuarkBuilderBase {
uint256 chainId;
address comet;
address withdrawer;
bool preferAcross;
}

function cometWithdraw(
Expand Down Expand Up @@ -330,7 +337,8 @@ contract CometActionsBuilder is QuarkBuilderBase {
chainId: cometWithdrawIntent.chainId,
useQuotecall: useQuotecall,
bridgeEnabled: true,
autoWrapperEnabled: true
autoWrapperEnabled: true,
preferAcross: cometWithdrawIntent.preferAcross
}),
chainAccountsList: chainAccountsList,
payment: payment,
Expand Down
12 changes: 9 additions & 3 deletions src/builder/actions/MorphoActionsBuilder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ contract MorphoActionsBuilder is QuarkBuilderBase {
uint256 chainId;
uint256 collateralAmount;
string collateralAssetSymbol;
bool preferAcross;
}

function morphoBorrow(
Expand Down Expand Up @@ -71,7 +72,8 @@ contract MorphoActionsBuilder is QuarkBuilderBase {
chainId: borrowIntent.chainId,
useQuotecall: useQuotecall,
bridgeEnabled: true,
autoWrapperEnabled: true
autoWrapperEnabled: true,
preferAcross: borrowIntent.preferAcross
});
}

Expand Down Expand Up @@ -101,6 +103,7 @@ contract MorphoActionsBuilder is QuarkBuilderBase {
uint256 chainId;
uint256 collateralAmount;
string collateralAssetSymbol;
bool preferAcross;
}

function morphoRepay(
Expand Down Expand Up @@ -163,7 +166,8 @@ contract MorphoActionsBuilder is QuarkBuilderBase {
chainId: repayIntent.chainId,
useQuotecall: useQuotecall,
bridgeEnabled: true,
autoWrapperEnabled: true
autoWrapperEnabled: true,
preferAcross: repayIntent.preferAcross
}),
chainAccountsList: chainAccountsList,
payment: payment,
Expand All @@ -190,6 +194,7 @@ contract MorphoActionsBuilder is QuarkBuilderBase {
address[] distributors;
address[] rewards;
bytes32[][] proofs;
bool preferAcross;
}

function morphoClaimRewards(
Expand Down Expand Up @@ -246,7 +251,8 @@ contract MorphoActionsBuilder is QuarkBuilderBase {
chainId: claimIntent.chainId,
useQuotecall: useQuotecall,
bridgeEnabled: true,
autoWrapperEnabled: true
autoWrapperEnabled: true,
preferAcross: claimIntent.preferAcross
});
}

Expand Down
8 changes: 6 additions & 2 deletions src/builder/actions/MorphoVaultActionsBuilder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ contract MorphoVaultActionsBuilder is QuarkBuilderBase {
uint256 blockTimestamp;
address sender;
uint256 chainId;
bool preferAcross;
}

function morphoVaultSupply(
Expand Down Expand Up @@ -80,7 +81,8 @@ contract MorphoVaultActionsBuilder is QuarkBuilderBase {
chainId: supplyIntent.chainId,
useQuotecall: useQuotecall,
bridgeEnabled: true,
autoWrapperEnabled: true
autoWrapperEnabled: true,
preferAcross: supplyIntent.preferAcross
}),
chainAccountsList: chainAccountsList,
payment: payment,
Expand All @@ -104,6 +106,7 @@ contract MorphoVaultActionsBuilder is QuarkBuilderBase {
uint256 blockTimestamp;
uint256 chainId;
address withdrawer;
bool preferAcross;
}

function morphoVaultWithdraw(
Expand Down Expand Up @@ -166,7 +169,8 @@ contract MorphoVaultActionsBuilder is QuarkBuilderBase {
chainId: withdrawIntent.chainId,
useQuotecall: useQuotecall,
bridgeEnabled: true,
autoWrapperEnabled: true
autoWrapperEnabled: true,
preferAcross: withdrawIntent.preferAcross
});
}

Expand Down
8 changes: 6 additions & 2 deletions src/builder/actions/SwapActionsBuilder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ contract SwapActionsBuilder is QuarkBuilderBase {
address sender;
bool isExactOut;
uint256 blockTimestamp;
bool preferAcross;
}

function swap(
Expand Down Expand Up @@ -111,7 +112,8 @@ contract SwapActionsBuilder is QuarkBuilderBase {
chainId: swapIntent.chainId,
useQuotecall: isMaxSwap,
bridgeEnabled: true,
autoWrapperEnabled: true
autoWrapperEnabled: true,
preferAcross: swapIntent.preferAcross
});
}

Expand Down Expand Up @@ -145,6 +147,7 @@ contract SwapActionsBuilder is QuarkBuilderBase {
uint256 interval;
address sender;
uint256 blockTimestamp;
bool preferAcross;
}

// Note: We don't currently bridge the input token or the payment token for recurring swaps. Recurring swaps
Expand Down Expand Up @@ -205,7 +208,8 @@ contract SwapActionsBuilder is QuarkBuilderBase {
chainId: swapIntent.chainId,
useQuotecall: false,
bridgeEnabled: false,
autoWrapperEnabled: false
autoWrapperEnabled: false,
preferAcross: swapIntent.preferAcross
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/builder/actions/TransferActionsBuilder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ contract TransferActionsBuilder is QuarkBuilderBase {
address sender;
address recipient;
uint256 blockTimestamp;
bool preferAcross;
}

function transfer(
Expand Down Expand Up @@ -81,7 +82,8 @@ contract TransferActionsBuilder is QuarkBuilderBase {
chainId: transferIntent.chainId,
useQuotecall: useQuotecall,
bridgeEnabled: true,
autoWrapperEnabled: true
autoWrapperEnabled: true,
preferAcross: transferIntent.preferAcross
});
}

Expand Down
Loading

0 comments on commit 5c3d0d7

Please sign in to comment.