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

Support Quark v2 in QuarkBuilder #83

Merged
merged 4 commits into from
Sep 24, 2024
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
22 changes: 11 additions & 11 deletions src/builder/Accounts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ import {PaymentInfo} from "./PaymentInfo.sol";
import {TokenWrapper} from "./TokenWrapper.sol";

library Accounts {
error QuarkSecretNotFound(address account);

struct ChainAccounts {
uint256 chainId;
QuarkState[] quarkStates;
QuarkSecret[] quarkSecrets;
AssetPositions[] assetPositionsList;
CometPositions[] cometPositions;
MorphoPositions[] morphoPositions;
MorphoVaultPositions[] morphoVaultPositions;
}

// We map this to the Portfolio data structure that the client will already have.
// This includes fields that builder may not necessarily need, however it makes
// the client encoding that much simpler.
struct QuarkState {
struct QuarkSecret {
address account;
uint96 quarkNextNonce;
bytes32 nonceSecret;
}

// Similarly, this is designed to intentionally reduce the encoding burden for the client
Expand Down Expand Up @@ -182,16 +181,17 @@ library Accounts {
return findAssetPositions(assetAddress, chainAccounts.assetPositionsList);
}

function findQuarkState(address account, Accounts.QuarkState[] memory quarkStates)
function findQuarkSecret(address account, Accounts.QuarkSecret[] memory quarkSecrets)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do it this way? Wouldn't just "next random" work?

Copy link
Contributor Author

@kevincheng96 kevincheng96 Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Synced offline on this. I don't think we resolved this discussion yet, but here's my response:

I considered specifying nonceSecrets on a non-account basis, but I decided to keep it account-based for a few reasons.

  1. It conforms to the current interface that the client is passing in.
  2. It reduces the complexity of the QuarkBuilder having to pick out nonceSecrets and keep track of which ones are already assigned to which operations.
  3. It's kind of nice that this account-based approach guarantees there will always be enough nonceSecrets to construct the quark operations. It'll be one less thing that could go wrong to worry about in the QuarkBuilder

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBF, if we needed to do two operations on the same chain, this strategy wouldn't work? E.g. let's say we wanted to make two delayed operations on the same chain.

Copy link
Contributor Author

@kevincheng96 kevincheng96 Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have two operations on the same chain for the same account, we wrap those in a multicall operation. So having a single nonceSecret for an account on each chain is guaranteed to be sufficient.

EDIT: I see your point. If there are two operations that have to be separate, then this wouldn't work. Good point. I can work on the "next random" approach in a follow-up PR to see how complicated it is

internal
pure
returns (Accounts.QuarkState memory state)
returns (Accounts.QuarkSecret memory)
{
for (uint256 i = 0; i < quarkStates.length; ++i) {
if (quarkStates[i].account == account) {
return state = quarkStates[i];
for (uint256 i = 0; i < quarkSecrets.length; ++i) {
if (quarkSecrets[i].account == account) {
return quarkSecrets[i];
}
}
revert QuarkSecretNotFound(account);
}

function findChainAccountsWithPaymentInfo(
Expand Down
246 changes: 158 additions & 88 deletions src/builder/Actions.sol

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/builder/QuarkBuilder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {List} from "./List.sol";
contract QuarkBuilder {
/* ===== Constants ===== */

string constant VERSION = "0.1.0";
string constant VERSION = "0.1.1";

/* ===== Custom Errors ===== */

Expand Down
61 changes: 47 additions & 14 deletions test/builder/QuarkBuilderCometBorrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
chainPortfolios[0] = ChainPortfolio({
chainId: 1,
account: address(0xa11ce),
nextNonce: 12,
nonceSecret: bytes32(uint256(12)),
assetSymbols: Arrays.stringArray("USDC", "USDT", "LINK", "WETH"),
assetBalances: Arrays.uintArray(0, 0, 10e18, 0), // user has 10 LINK
cometPortfolios: emptyCometPortfolios_(),
Expand All @@ -94,7 +94,7 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
chainPortfolios[1] = ChainPortfolio({
chainId: 8453,
account: address(0xb0b),
nextNonce: 2,
nonceSecret: bytes32(uint256(2)),
assetSymbols: Arrays.stringArray("USDC", "USDT", "LINK", "WETH"),
assetBalances: Arrays.uintArray(0, 0, 0, 0),
cometPortfolios: emptyCometPortfolios_(),
Expand Down Expand Up @@ -154,6 +154,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(
result.quarkOperations[0].expiry, BLOCK_TIMESTAMP + 7 days, "expiry is current blockTimestamp + 7 days"
);
assertEq(result.quarkOperations[0].nonce, chainPortfolios[0].nonceSecret, "unexpected nonce");
assertEq(result.quarkOperations[0].isReplayable, false, "isReplayable is false");

// check the actions
assertEq(result.actions.length, 1, "one action");
Expand All @@ -163,6 +165,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(result.actions[0].paymentMethod, "OFFCHAIN", "payment method is 'OFFCHAIN'");
assertEq(result.actions[0].paymentToken, address(0), "payment token is null");
assertEq(result.actions[0].paymentMaxCost, 0, "payment has no max cost, since 'OFFCHAIN'");
assertEq(result.actions[0].nonceSecret, chainPortfolios[0].nonceSecret, "unexpected nonce secret");
assertEq(result.actions[0].totalPlays, 1, "total plays is 1");

uint256[] memory collateralTokenPrices = new uint256[](1);
collateralTokenPrices[0] = LINK_PRICE;
Expand Down Expand Up @@ -209,7 +213,7 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
chainPortfolios[0] = ChainPortfolio({
chainId: 1,
account: address(0xa11ce),
nextNonce: 12,
nonceSecret: bytes32(uint256(12)),
assetSymbols: Arrays.stringArray("USDC", "ETH", "LINK", "WETH"),
assetBalances: Arrays.uintArray(0, 10e18, 0, 0), // user has 10 ETH
cometPortfolios: emptyCometPortfolios_(),
Expand All @@ -219,7 +223,7 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
chainPortfolios[1] = ChainPortfolio({
chainId: 8453,
account: address(0xb0b),
nextNonce: 2,
nonceSecret: bytes32(uint256(2)),
assetSymbols: Arrays.stringArray("USDC", "ETH", "LINK", "WETH"),
assetBalances: Arrays.uintArray(0, 0, 0, 0),
cometPortfolios: emptyCometPortfolios_(),
Expand Down Expand Up @@ -275,6 +279,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(
result.quarkOperations[0].expiry, BLOCK_TIMESTAMP + 7 days, "expiry is current blockTimestamp + 7 days"
);
assertEq(result.quarkOperations[0].nonce, chainPortfolios[0].nonceSecret, "unexpected nonce");
assertEq(result.quarkOperations[0].isReplayable, false, "isReplayable is false");

// check the actions
assertEq(result.actions.length, 1, "one action");
Expand All @@ -284,6 +290,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(result.actions[0].paymentMethod, "OFFCHAIN", "payment method is 'OFFCHAIN'");
assertEq(result.actions[0].paymentToken, address(0), "payment token is null");
assertEq(result.actions[0].paymentMaxCost, 0, "payment has no max cost, since 'OFFCHAIN'");
assertEq(result.actions[0].nonceSecret, chainPortfolios[0].nonceSecret, "unexpected nonce secret");
assertEq(result.actions[0].totalPlays, 1, "total plays is 1");
assertEq(
result.actions[0].actionContext,
abi.encode(
Expand Down Expand Up @@ -320,7 +328,7 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
chainPortfolios[0] = ChainPortfolio({
chainId: 1,
account: address(0xa11ce),
nextNonce: 12,
nonceSecret: bytes32(uint256(12)),
assetSymbols: Arrays.stringArray("USDC", "USDT", "LINK", "WETH"),
assetBalances: Arrays.uintArray(1e6, 0, 10e18, 0), // user has 1 USDC, 10 LINK
cometPortfolios: emptyCometPortfolios_(),
Expand All @@ -330,7 +338,7 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
chainPortfolios[1] = ChainPortfolio({
chainId: 8453,
account: address(0xb0b),
nextNonce: 2,
nonceSecret: bytes32(uint256(2)),
assetSymbols: Arrays.stringArray("USDC", "USDT", "LINK", "WETH"),
assetBalances: Arrays.uintArray(0, 0, 0, 0),
cometPortfolios: emptyCometPortfolios_(),
Expand Down Expand Up @@ -397,6 +405,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(
result.quarkOperations[0].expiry, BLOCK_TIMESTAMP + 7 days, "expiry is current blockTimestamp + 7 days"
);
assertEq(result.quarkOperations[0].nonce, chainPortfolios[0].nonceSecret, "unexpected nonce");
assertEq(result.quarkOperations[0].isReplayable, false, "isReplayable is false");

// check the actions
assertEq(result.actions.length, 1, "one action");
Expand All @@ -406,6 +416,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(result.actions[0].paymentMethod, "PAY_CALL", "payment method is 'PAY_CALL'");
assertEq(result.actions[0].paymentToken, USDC_1, "payment token is USDC");
assertEq(result.actions[0].paymentMaxCost, 0.1e6, "payment max is set to .1e6 in this test case");
assertEq(result.actions[0].nonceSecret, chainPortfolios[0].nonceSecret, "unexpected nonce secret");
assertEq(result.actions[0].totalPlays, 1, "total plays is 1");

uint256[] memory collateralTokenPrices = new uint256[](1);
collateralTokenPrices[0] = LINK_PRICE;
Expand Down Expand Up @@ -450,7 +462,7 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
chainPortfolios[0] = ChainPortfolio({
chainId: 1,
account: address(0xa11ce),
nextNonce: 12,
nonceSecret: bytes32(uint256(12)),
assetSymbols: Arrays.stringArray("USDC", "USDT", "LINK", "WETH"),
assetBalances: Arrays.uintArray(0, 0, 10e18, 0), // user has 10 LINK and 0 USDC
cometPortfolios: emptyCometPortfolios_(),
Expand All @@ -460,7 +472,7 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
chainPortfolios[1] = ChainPortfolio({
chainId: 8453,
account: address(0xb0b),
nextNonce: 2,
nonceSecret: bytes32(uint256(2)),
assetSymbols: Arrays.stringArray("USDC", "USDT", "LINK", "WETH"),
assetBalances: Arrays.uintArray(0, 0, 0, 0),
cometPortfolios: emptyCometPortfolios_(),
Expand Down Expand Up @@ -522,6 +534,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(
result.quarkOperations[0].expiry, BLOCK_TIMESTAMP + 7 days, "expiry is current blockTimestamp + 7 days"
);
assertEq(result.quarkOperations[0].nonce, chainPortfolios[0].nonceSecret, "unexpected nonce");
assertEq(result.quarkOperations[0].isReplayable, false, "isReplayable is false");

// check the actions
assertEq(result.actions.length, 1, "one action");
Expand All @@ -531,6 +545,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(result.actions[0].paymentMethod, "PAY_CALL", "payment method is 'PAY_CALL'");
assertEq(result.actions[0].paymentToken, USDC_1, "payment token is USDC");
assertEq(result.actions[0].paymentMaxCost, 0.5e6, "payment max is set to .5e6 in this test case");
assertEq(result.actions[0].nonceSecret, chainPortfolios[0].nonceSecret, "unexpected nonce secret");
assertEq(result.actions[0].totalPlays, 1, "total plays is 1");

uint256[] memory collateralTokenPrices = new uint256[](1);
collateralTokenPrices[0] = LINK_PRICE;
Expand Down Expand Up @@ -577,7 +593,7 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
chainPortfolios[0] = ChainPortfolio({
chainId: 1,
account: address(0xa11ce),
nextNonce: 12,
nonceSecret: bytes32(uint256(12)),
assetSymbols: Arrays.stringArray("USDC", "USDT", "LINK", "WETH"),
assetBalances: Arrays.uintArray(3e6, 0, 0, 0), // 3 USDC on mainnet
cometPortfolios: emptyCometPortfolios_(),
Expand All @@ -586,8 +602,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
});
chainPortfolios[1] = ChainPortfolio({
chainId: 8453,
account: address(0xb0b),
nextNonce: 2,
account: address(0xa11ce),
nonceSecret: bytes32(uint256(2)),
assetSymbols: Arrays.stringArray("USDC", "USDT", "LINK", "WETH"),
assetBalances: Arrays.uintArray(0, 0, 5e18, 0),
cometPortfolios: emptyCometPortfolios_(),
Expand Down Expand Up @@ -649,6 +665,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(
result.quarkOperations[0].expiry, BLOCK_TIMESTAMP + 7 days, "expiry is current blockTimestamp + 7 days"
);
assertEq(result.quarkOperations[0].nonce, chainPortfolios[0].nonceSecret, "unexpected nonce");
assertEq(result.quarkOperations[0].isReplayable, false, "isReplayable is false");

// second operation
assertEq(
Expand Down Expand Up @@ -686,6 +704,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(
result.quarkOperations[1].expiry, BLOCK_TIMESTAMP + 7 days, "expiry is current blockTimestamp + 7 days"
);
assertEq(result.quarkOperations[1].nonce, chainPortfolios[1].nonceSecret, "unexpected nonce");
assertEq(result.quarkOperations[1].isReplayable, false, "isReplayable is false");

// Check the actions
assertEq(result.actions.length, 2, "two actions");
Expand All @@ -696,6 +716,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(result.actions[0].paymentMethod, "PAY_CALL", "payment method is 'PAY_CALL'");
assertEq(result.actions[0].paymentToken, USDC_1, "payment token is USDC on mainnet");
assertEq(result.actions[0].paymentMaxCost, 0.1e6, "payment should have max cost of 0.1e6");
assertEq(result.actions[0].nonceSecret, chainPortfolios[0].nonceSecret, "unexpected nonce secret");
assertEq(result.actions[0].totalPlays, 1, "total plays is 1");
assertEq(
result.actions[0].actionContext,
abi.encode(
Expand All @@ -719,6 +741,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(result.actions[1].paymentMethod, "PAY_CALL", "payment method is 'PAY_CALL'");
assertEq(result.actions[1].paymentToken, USDC_8453, "payment token is USDC on Base");
assertEq(result.actions[1].paymentMaxCost, 1e6, "payment should have max cost of 1e6");
assertEq(result.actions[1].nonceSecret, chainPortfolios[1].nonceSecret, "unexpected nonce secret");
assertEq(result.actions[1].totalPlays, 1, "total plays is 1");

uint256[] memory collateralTokenPrices = new uint256[](1);
collateralTokenPrices[0] = LINK_PRICE;
Expand Down Expand Up @@ -765,7 +789,7 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
chainPortfolios[0] = ChainPortfolio({
chainId: 1,
account: address(0xa11ce),
nextNonce: 12,
nonceSecret: bytes32(uint256(12)),
assetSymbols: Arrays.stringArray("USDC", "USDT", "LINK", "WETH"),
assetBalances: Arrays.uintArray(4e6, 0, 0, 0), // 4 USDC on mainnet
cometPortfolios: emptyCometPortfolios_(),
Expand All @@ -774,8 +798,9 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
});
chainPortfolios[1] = ChainPortfolio({
chainId: 8453,
account: address(0xb0b),
nextNonce: 2,
// TODO: if want to test different accounts, can set bridge as b0b and send as allice
account: address(0xa11ce),
nonceSecret: bytes32(uint256(2)),
assetSymbols: Arrays.stringArray("USDC", "USDT", "LINK", "WETH"),
assetBalances: Arrays.uintArray(0, 0, 0, 0), // no assets on base
cometPortfolios: emptyCometPortfolios_(),
Expand Down Expand Up @@ -837,6 +862,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(
result.quarkOperations[0].expiry, BLOCK_TIMESTAMP + 7 days, "expiry is current blockTimestamp + 7 days"
);
assertEq(result.quarkOperations[0].nonce, chainPortfolios[0].nonceSecret, "unexpected nonce");
assertEq(result.quarkOperations[0].isReplayable, false, "isReplayable is false");

// second operation
assertEq(
Expand Down Expand Up @@ -874,6 +901,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(
result.quarkOperations[1].expiry, BLOCK_TIMESTAMP + 7 days, "expiry is current blockTimestamp + 7 days"
);
assertEq(result.quarkOperations[1].nonce, chainPortfolios[1].nonceSecret, "unexpected nonce");
assertEq(result.quarkOperations[1].isReplayable, false, "isReplayable is false");

// Check the actions
assertEq(result.actions.length, 2, "two actions");
Expand All @@ -884,6 +913,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(result.actions[0].paymentMethod, "PAY_CALL", "payment method is 'PAY_CALL'");
assertEq(result.actions[0].paymentToken, USDC_1, "payment token is USDC on mainnet");
assertEq(result.actions[0].paymentMaxCost, 0.1e6, "payment should have max cost of 0.1e6");
assertEq(result.actions[0].nonceSecret, chainPortfolios[0].nonceSecret, "unexpected nonce secret");
assertEq(result.actions[0].totalPlays, 1, "total plays is 1");
assertEq(
result.actions[0].actionContext,
abi.encode(
Expand All @@ -907,6 +938,8 @@ contract QuarkBuilderCometBorrowTest is Test, QuarkBuilderTest {
assertEq(result.actions[1].paymentMethod, "PAY_CALL", "payment method is 'PAY_CALL'");
assertEq(result.actions[1].paymentToken, USDC_8453, "payment token is USDC on Base");
assertEq(result.actions[1].paymentMaxCost, 0.2e6, "payment should have max cost of 0.2e6");
assertEq(result.actions[1].nonceSecret, chainPortfolios[1].nonceSecret, "unexpected nonce secret");
assertEq(result.actions[1].totalPlays, 1, "total plays is 1");

uint256[] memory collateralTokenPrices = new uint256[](1);
collateralTokenPrices[0] = USDC_PRICE;
Expand Down
Loading
Loading