Skip to content

Commit

Permalink
Merge branch 'main' into add-deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
fedgiac authored Feb 7, 2024
2 parents d5102e2 + 82f34c5 commit 12b7853
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 28 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,14 @@ $ forge test
```shell
$ forge fmt
```

### Deploy

All contracts in this repo can be deployed and verified on the block explorer as follows:

```sh
export ETHERSCAN_API_KEY='your API key here'
PK='the private key of the deployer'
ETH_RPC_URL='https://rpc.node.url.here.example.com'
forge script 'script/DeployAllContracts.s.sol:DeployAllContracts' -vvvv --rpc-url "$ETH_RPC_URL" --private-key "$PK" --verify --broadcast
```
12 changes: 0 additions & 12 deletions script/Counter.s.sol

This file was deleted.

12 changes: 0 additions & 12 deletions script/Deploy.sol

This file was deleted.

18 changes: 18 additions & 0 deletions script/DeployAllContracts.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;

import {Script} from "forge-std/Script.sol";

import {DeployUniswapV2PriceOracle, UniswapV2PriceOracle} from "./single-deployment/UniswapV2PriceOracle.s.sol";
import {DeployConstantProduct, ConstantProduct} from "./single-deployment/ConstantProduct.s.sol";

contract DeployAllContracts is DeployConstantProduct, DeployUniswapV2PriceOracle {
function run() public override(DeployConstantProduct, DeployUniswapV2PriceOracle) {
deployAll();
}

function deployAll() public returns (ConstantProduct constantProduct, UniswapV2PriceOracle uniswapV2PriceOracle) {
constantProduct = deployConstantProduct();
uniswapV2PriceOracle = deployUniswapV2PriceOracle();
}
}
17 changes: 17 additions & 0 deletions script/single-deployment/ConstantProduct.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;

import {Script} from "forge-std/Script.sol";

import {ConstantProduct} from "src/ConstantProduct.sol";

contract DeployConstantProduct is Script {
function run() public virtual {
deployConstantProduct();
}

function deployConstantProduct() internal returns (ConstantProduct) {
vm.broadcast();
return new ConstantProduct();
}
}
17 changes: 17 additions & 0 deletions script/single-deployment/UniswapV2PriceOracle.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;

import {Script} from "forge-std/Script.sol";

import {UniswapV2PriceOracle} from "src/oracles/UniswapV2PriceOracle.sol";

contract DeployUniswapV2PriceOracle is Script {
function run() public virtual {
deployUniswapV2PriceOracle();
}

function deployUniswapV2PriceOracle() internal returns (UniswapV2PriceOracle) {
vm.broadcast();
return new UniswapV2PriceOracle();
}
}
5 changes: 4 additions & 1 deletion src/ConstantProduct.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "lib/composable-cow/src/BaseConditionalOrder.sol";

import {IPriceOracle} from "./interfaces/IPriceOracle.sol";
import {IWatchtowerCustomErrors} from "./interfaces/IWatchtowerCustomErrors.sol";

/**
* @title CoW AMM
Expand Down Expand Up @@ -128,7 +129,9 @@ contract ConstantProduct is IConditionalOrderGenerator {
}

if (tradedAmountToken0 < data.minTradedToken0) {
revert IConditionalOrder.OrderNotValid("traded amount too small");
revert IWatchtowerCustomErrors.PollTryAtEpoch(
Utils.validToBucket(MAX_ORDER_DURATION) + 1, "traded amount too small"
);
}

order = GPv2Order.Data(
Expand Down
19 changes: 19 additions & 0 deletions src/interfaces/IWatchtowerCustomErrors.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;

/**
* @title Watchtower Custom Error Interface
* @author CoW Protocol Developers
* @dev An interface that collects all custom error message for the watchtower.
* Different error messages lead to different watchtower behaviors when creating
* an order.
* @dev The watchtower is a service that automatically posts orders to the CoW
* Protocol orderbook at regular intervals.
*/
contract IWatchtowerCustomErrors {
/**
* No order is currently available for trading, but the watchtower should
* try again at the specified timestamp.
*/
error PollTryAtEpoch(uint256 timestamp, string message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
pragma solidity ^0.8.13;

import {ConstantProductTestHarness} from "../ConstantProductTestHarness.sol";
import {ConstantProduct, GPv2Order, IConditionalOrder} from "../../../src/ConstantProduct.sol";
import {ConstantProduct, GPv2Order, IConditionalOrder} from "src/ConstantProduct.sol";
import {IWatchtowerCustomErrors} from "src/interfaces/IWatchtowerCustomErrors.sol";

abstract contract ValidateOrderParametersTest is ConstantProductTestHarness {
function testValidOrderParameters() public {
Expand Down Expand Up @@ -44,12 +45,26 @@ abstract contract ValidateOrderParametersTest is ConstantProductTestHarness {
setUpDefaultReserves(orderOwner);
setUpDefaultReferencePairReserves(42, 1337);

uint256 smallOffset = 42;
require(smallOffset < constantProduct.MAX_ORDER_DURATION());
uint256 nextTimestamp = 1337 * constantProduct.MAX_ORDER_DURATION() + smallOffset;
uint256 nextBucket = 1338 * constantProduct.MAX_ORDER_DURATION();
require(
nextTimestamp % constantProduct.MAX_ORDER_DURATION() != 0,
"test was designed so that the timestamp doesn't fall exactly at the start of a bucket, please change the offset"
);
vm.warp(nextTimestamp);

GPv2Order.Data memory order = getTradeableOrderWrapper(orderOwner, defaultData);
require(order.sellToken == defaultData.token0, "test was design for token0 to be the sell token");
defaultData.minTradedToken0 = order.sellAmount;
order = getTradeableOrderWrapper(orderOwner, defaultData);
defaultData.minTradedToken0 = order.sellAmount + 1;
vm.expectRevert(abi.encodeWithSelector(IConditionalOrder.OrderNotValid.selector, "traded amount too small"));
vm.expectRevert(
abi.encodeWithSelector(
IWatchtowerCustomErrors.PollTryAtEpoch.selector, nextBucket + 1, "traded amount too small"
)
);
getTradeableOrderUncheckedWrapper(orderOwner, defaultData);
}

Expand All @@ -58,12 +73,26 @@ abstract contract ValidateOrderParametersTest is ConstantProductTestHarness {
setUpDefaultReserves(orderOwner);
setUpDefaultReferencePairReserves(1337, 42);

uint256 smallOffset = 42;
require(smallOffset < constantProduct.MAX_ORDER_DURATION());
uint256 nextTimestamp = 1337 * constantProduct.MAX_ORDER_DURATION() + smallOffset;
uint256 nextBucket = 1338 * constantProduct.MAX_ORDER_DURATION();
require(
nextTimestamp % constantProduct.MAX_ORDER_DURATION() != 0,
"test was designed so that the timestamp doesn't fall exactly at the start of a bucket, please change the offset"
);
vm.warp(nextTimestamp);

GPv2Order.Data memory order = getTradeableOrderWrapper(orderOwner, defaultData);
require(order.buyToken == defaultData.token0, "test was design for token0 to be the buy token");
defaultData.minTradedToken0 = order.buyAmount;
order = getTradeableOrderWrapper(orderOwner, defaultData);
defaultData.minTradedToken0 = order.buyAmount + 1;
vm.expectRevert(abi.encodeWithSelector(IConditionalOrder.OrderNotValid.selector, "traded amount too small"));
vm.expectRevert(
abi.encodeWithSelector(
IWatchtowerCustomErrors.PollTryAtEpoch.selector, nextBucket + 1, "traded amount too small"
)
);
getTradeableOrderUncheckedWrapper(orderOwner, defaultData);
}
}
18 changes: 18 additions & 0 deletions test/script/DeployAllContracts.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;

import {Test} from "forge-std/Test.sol";

import {DeployAllContracts} from "script/DeployAllContracts.s.sol";

contract DeployAllContractsTest is Test {
DeployAllContracts script;

function setUp() public {
script = new DeployAllContracts();
}

function testDoesNotRevert() public {
script.run();
}
}
18 changes: 18 additions & 0 deletions test/script/single-deployment/DeployConstantProduct.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;

import {Test} from "forge-std/Test.sol";

import {DeployConstantProduct} from "script/single-deployment/ConstantProduct.s.sol";

contract DeployConstantProductTest is Test {
DeployConstantProduct script;

function setUp() public {
script = new DeployConstantProduct();
}

function testDoesNotRevert() public {
script.run();
}
}
18 changes: 18 additions & 0 deletions test/script/single-deployment/DeployUniswapV2PriceOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;

import {Test} from "forge-std/Test.sol";

import {DeployUniswapV2PriceOracle} from "script/single-deployment/UniswapV2PriceOracle.s.sol";

contract DeployUniswapV2PriceOracleTest is Test {
DeployUniswapV2PriceOracle script;

function setUp() public {
script = new DeployUniswapV2PriceOracle();
}

function testDoesNotRevert() public {
script.run();
}
}

0 comments on commit 12b7853

Please sign in to comment.