Skip to content

Commit

Permalink
Merge pull request #46 from rainprotocol/2023-11-21-npe2
Browse files Browse the repository at this point in the history
update i9r
  • Loading branch information
thedavidmeister authored Nov 24, 2023
2 parents e794cc9 + 3d805fd commit e611e45
Show file tree
Hide file tree
Showing 52 changed files with 951 additions and 602 deletions.
151 changes: 79 additions & 72 deletions .gas-snapshot

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
[submodule "lib/rain.lib.memkv"]
path = lib/rain.lib.memkv
url = https://github.com/rainprotocol/rain.lib.memkv
[submodule "lib/rain.factory"]
path = lib/rain.factory
url = https://github.com/rainprotocol/rain.factory
[submodule "lib/sushixswap-v2"]
path = lib/sushixswap-v2
url = https://github.com/rainprotocol/sushixswap-v2
Expand All @@ -22,9 +19,12 @@
[submodule "lib/rain.solmem"]
path = lib/rain.solmem
url = https://github.com/rainprotocol/rain.solmem
[submodule "lib/rain.interpreter"]
path = lib/rain.interpreter
url = https://github.com/rainprotocol/rain.interpreter
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "lib/rain.interpreter"]
path = lib/rain.interpreter
url = https://github.com/rainprotocol/rain.interpreter
[submodule "lib/rain.factory"]
path = lib/rain.factory
url = https://github.com/rainprotocol/rain.factory
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ cbor_metadata = false

# Build metadata used for testing rain meta aware contracts in this folder rather
# than expose ffi to forge.
fs_permissions = [{ access = "read", path = "./meta"}]
fs_permissions = [{ access = "read", path = "./meta"}, {access = "read", path = "./lib/rain.interpreter/meta/RainterpreterExpressionDeployerNPE2.rain.meta"}]

[fuzz]
runs = 5096
2 changes: 1 addition & 1 deletion lib/rain.factory
2 changes: 1 addition & 1 deletion lib/rain.interpreter
Binary file modified meta/GenericPoolOrderBookV3ArbOrderTaker.rain.meta
Binary file not shown.
Binary file modified meta/GenericPoolOrderBookV3FlashBorrower.rain.meta
Binary file not shown.
Binary file modified meta/OrderBook.rain.meta
Binary file not shown.
Binary file modified meta/RouteProcessorOrderBookV3ArbOrderTaker.rain.meta
Binary file not shown.
6 changes: 6 additions & 0 deletions script/DeployConstants.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.19;

// @todo pull this from subgraph.
// hardcoded from CI https://github.com/rainprotocol/rain.interpreter/actions/runs/6953107467/job/18917750124
address constant I9R_DEPLOYER = 0xa5aDC3F2A7A8Cf7b5172D76d8b26c3d49272297B;
16 changes: 7 additions & 9 deletions script/DeployGenericPoolOrderBookV3ArbOrderTaker.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.19;

import "forge-std/Script.sol";
import "src/concrete/GenericPoolOrderBookV3ArbOrderTaker.sol";
import {Script, console2} from "forge-std/Script.sol";
import {
GenericPoolOrderBookV3ArbOrderTaker,
DeployerDiscoverableMetaV3ConstructionConfig
} from "src/concrete/GenericPoolOrderBookV3ArbOrderTaker.sol";
import {I9R_DEPLOYER} from "./DeployConstants.sol";

/// @title DeployGenericPoolOrderBookV3ArbOrderTaker
/// @notice A script that deploys a `GenericPoolOrderBookV3ArbOrderTaker`. This
Expand All @@ -13,19 +17,13 @@ contract DeployGenericPoolOrderBookV3ArbOrderTaker is Script {
/// provide the built metadata. On CI this is achieved by using the rain cli.
function run(bytes memory meta) external {
uint256 deployerPrivateKey = vm.envUint("DEPLOYMENT_KEY");
// @todo pull this from subgraph.
// hardcoded from CI https://github.com/rainprotocol/rain.interpreter/actions/runs/6101787278/job/16558857505
address i9rDeployer = 0xAb0A13cC2654CbaDABabC9952a090928F4ff569A;

console2.log("GenericPoolOrderBookV3ArbOrderTaker meta hash:");
console2.logBytes32(keccak256(meta));

vm.startBroadcast(deployerPrivateKey);
GenericPoolOrderBookV3ArbOrderTaker deployed =
new GenericPoolOrderBookV3ArbOrderTaker(DeployerDiscoverableMetaV2ConstructionConfig (
i9rDeployer,
meta
));
new GenericPoolOrderBookV3ArbOrderTaker(DeployerDiscoverableMetaV3ConstructionConfig(I9R_DEPLOYER, meta));
(deployed);
vm.stopBroadcast();
}
Expand Down
16 changes: 7 additions & 9 deletions script/DeployGenericPoolOrderBookV3FlashBorrower.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.19;

import "lib/forge-std/src/Script.sol";
import "src/concrete/GenericPoolOrderBookV3FlashBorrower.sol";
import {Script, console2} from "forge-std/Script.sol";
import {
GenericPoolOrderBookV3FlashBorrower,
DeployerDiscoverableMetaV3ConstructionConfig
} from "src/concrete/GenericPoolOrderBookV3FlashBorrower.sol";
import {I9R_DEPLOYER} from "./DeployConstants.sol";

/// @title DeployGenericPoolOrderBookV3FlashBorrower
/// @notice A script that deploys a `GenericPoolOrderBookV3FlashBorrower`. This
Expand All @@ -13,19 +17,13 @@ contract DeployGenericPoolOrderBookV3FlashBorrower is Script {
/// provide the built metadata. On CI this is achieved by using the rain cli.
function run(bytes memory meta) external {
uint256 deployerPrivateKey = vm.envUint("DEPLOYMENT_KEY");
// @todo pull this from subgraph.
// hardcoded from CI https://github.com/rainprotocol/rain.interpreter/actions/runs/6101787278/job/16558857505
address i9rDeployer = 0xAb0A13cC2654CbaDABabC9952a090928F4ff569A;

console2.log("DeployGenericPoolOrderBookV3FlashBorrower meta hash:");
console2.logBytes32(keccak256(meta));

vm.startBroadcast(deployerPrivateKey);
GenericPoolOrderBookV3FlashBorrower deployed =
new GenericPoolOrderBookV3FlashBorrower(DeployerDiscoverableMetaV2ConstructionConfig (
i9rDeployer,
meta
));
new GenericPoolOrderBookV3FlashBorrower(DeployerDiscoverableMetaV3ConstructionConfig(I9R_DEPLOYER, meta));
(deployed);
vm.stopBroadcast();
}
Expand Down
13 changes: 4 additions & 9 deletions script/DeployOrderBook.sol
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.19;

import "lib/forge-std/src/Script.sol";
import "src/concrete/OrderBook.sol";
import {Script, console2} from "forge-std/Script.sol";
import {OrderBook, DeployerDiscoverableMetaV3ConstructionConfig} from "src/concrete/OrderBook.sol";
import {I9R_DEPLOYER} from "./DeployConstants.sol";

contract DeployOrderBook is Script {
function run(bytes memory meta) external {
uint256 deployerPrivateKey = vm.envUint("DEPLOYMENT_KEY");
// @todo pull this from subgraph.
// hardcoded from CI https://github.com/rainprotocol/rain.interpreter/actions/runs/6101787278/job/16558857505
address i9rDeployer = 0xAb0A13cC2654CbaDABabC9952a090928F4ff569A;

console2.log("DeployOrderBook meta hash:");
console2.logBytes32(keccak256(meta));

vm.startBroadcast(deployerPrivateKey);
OrderBook deployed = new OrderBook(DeployerDiscoverableMetaV2ConstructionConfig (
i9rDeployer,
meta
));
OrderBook deployed = new OrderBook(DeployerDiscoverableMetaV3ConstructionConfig(I9R_DEPLOYER, meta));
(deployed);
vm.stopBroadcast();
}
Expand Down
16 changes: 7 additions & 9 deletions script/DeployRouteProcessorOrderBookV3ArbOrderTaker.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.19;

import "forge-std/Script.sol";
import "src/concrete/RouteProcessorOrderBookV3ArbOrderTaker.sol";
import {Script, console2} from "forge-std/Script.sol";
import {
RouteProcessorOrderBookV3ArbOrderTaker,
DeployerDiscoverableMetaV3ConstructionConfig
} from "src/concrete/RouteProcessorOrderBookV3ArbOrderTaker.sol";
import {I9R_DEPLOYER} from "./DeployConstants.sol";

/// @title DeployRouteProcessorOrderBookV3ArbOrderTaker
/// @notice A script that deploys a `RouteProcessorOrderBookV3ArbOrderTaker`. This
Expand All @@ -13,19 +17,13 @@ contract DeployRouteProcessorOrderBookV3ArbOrderTaker is Script {
/// provide the built metadata. On CI this is achieved by using the rain cli.
function run(bytes memory meta) external {
uint256 deployerPrivateKey = vm.envUint("DEPLOYMENT_KEY");
// @todo pull this from subgraph.
// hardcoded from CI https://github.com/rainprotocol/rain.interpreter/actions/runs/6101787278/job/16558857505
address i9rDeployer = 0xAb0A13cC2654CbaDABabC9952a090928F4ff569A;

console2.log("RouteProcessorOrderBookV3ArbOrderTaker meta hash:");
console2.logBytes32(keccak256(meta));

vm.startBroadcast(deployerPrivateKey);
RouteProcessorOrderBookV3ArbOrderTaker deployed =
new RouteProcessorOrderBookV3ArbOrderTaker(DeployerDiscoverableMetaV2ConstructionConfig (
i9rDeployer,
meta
));
new RouteProcessorOrderBookV3ArbOrderTaker(DeployerDiscoverableMetaV3ConstructionConfig(I9R_DEPLOYER, meta));
(deployed);
vm.stopBroadcast();
}
Expand Down
84 changes: 50 additions & 34 deletions src/abstract/OrderBookV3ArbOrderTaker.sol
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.19;

import {ERC165, IERC165} from "lib/openzeppelin-contracts/contracts/utils/introspection/ERC165.sol";
import {ReentrancyGuard} from "lib/openzeppelin-contracts/contracts/security/ReentrancyGuard.sol";
import {IERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {Initializable} from "lib/openzeppelin-contracts/contracts/proxy/utils/Initializable.sol";
import {SafeERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import {Address} from "lib/openzeppelin-contracts/contracts/utils/Address.sol";
import {console2} from "forge-std/console2.sol";

import {ERC165, IERC165} from "openzeppelin-contracts/contracts/utils/introspection/ERC165.sol";
import {ReentrancyGuard} from "openzeppelin-contracts/contracts/security/ReentrancyGuard.sol";
import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {Initializable} from "openzeppelin-contracts/contracts/proxy/utils/Initializable.sol";
import {SafeERC20} from "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import {Address} from "openzeppelin-contracts/contracts/utils/Address.sol";
import {
DeployerDiscoverableMetaV2,
DeployerDiscoverableMetaV2ConstructionConfig,
DeployerDiscoverableMetaV3,
DeployerDiscoverableMetaV3ConstructionConfig,
LibMeta
} from "lib/rain.interpreter/src/abstract/DeployerDiscoverableMetaV2.sol";
import "lib/rain.factory/src/interface/ICloneableV2.sol";
import "lib/rain.interpreter/src/lib/caller/LibContext.sol";
import "lib/rain.interpreter/src/lib/caller/LibEncodedDispatch.sol";
import "lib/rain.interpreter/src/lib/bytecode/LibBytecode.sol";

import "../interface/unstable/IOrderBookV3.sol";
import "../interface/unstable/IOrderBookV3OrderTaker.sol";
import "../interface/unstable/IOrderBookV3ArbOrderTaker.sol";

import "./OrderBookV3ArbCommon.sol";
} from "rain.interpreter/src/abstract/DeployerDiscoverableMetaV3.sol";
import {EvaluableConfigV3, SignedContextV1} from "rain.interpreter/src/interface/IInterpreterCallerV2.sol";
import {SourceIndexV2} from "rain.interpreter/src/interface/unstable/IInterpreterV2.sol";
import {ICloneableV2, ICLONEABLE_V2_SUCCESS} from "rain.factory/src/interface/ICloneableV2.sol";
import {EncodedDispatch, LibEncodedDispatch} from "rain.interpreter/src/lib/caller/LibEncodedDispatch.sol";
import {LibNamespace} from "rain.interpreter/src/lib/ns/LibNamespace.sol";
import {IOrderBookV3, NoOrders} from "../interface/unstable/IOrderBookV3.sol";
import {IOrderBookV3ArbOrderTaker, IOrderBookV3OrderTaker} from "../interface/unstable/IOrderBookV3ArbOrderTaker.sol";
import {IInterpreterV2, DEFAULT_STATE_NAMESPACE} from "rain.interpreter/src/interface/unstable/IInterpreterV2.sol";
import {IInterpreterStoreV1} from "rain.interpreter/src/interface/IInterpreterStoreV1.sol";
import {TakeOrdersConfigV2} from "../interface/unstable/IOrderBookV3.sol";
import {BadLender, MinimumOutput, NonZeroBeforeArbStack, Initializing} from "./OrderBookV3ArbCommon.sol";
import {LibContext} from "rain.interpreter/src/lib/caller/LibContext.sol";
import {LibBytecode} from "rain.interpreter/src/lib/bytecode/LibBytecode.sol";

/// Thrown when "before arb" wants inputs that we don't have.
error NonZeroBeforeArbInputs(uint256 inputs);

/// Config for `OrderBookV3ArbOrderTakerConfigV1` to initialize.
/// @param orderBook The `IOrderBookV3` to use for `takeOrders`.
Expand All @@ -30,13 +38,13 @@ import "./OrderBookV3ArbCommon.sol";
/// the `beforeInitialize` hook.
struct OrderBookV3ArbOrderTakerConfigV1 {
address orderBook;
EvaluableConfigV2 evaluableConfig;
EvaluableConfigV3 evaluableConfig;
bytes implementationData;
}

/// @dev "Before arb" is evaluabled before the arb is executed. Ostensibly this
/// is to allow for access control to the arb, the return values are ignored.
SourceIndex constant BEFORE_ARB_SOURCE_INDEX = SourceIndex.wrap(0);
SourceIndexV2 constant BEFORE_ARB_SOURCE_INDEX = SourceIndexV2.wrap(0);
/// @dev "Before arb" has no return values.
uint256 constant BEFORE_ARB_MIN_OUTPUTS = 0;
/// @dev "Before arb" has no return values.
Expand All @@ -47,7 +55,7 @@ abstract contract OrderBookV3ArbOrderTaker is
ReentrancyGuard,
Initializable,
ICloneableV2,
DeployerDiscoverableMetaV2,
DeployerDiscoverableMetaV3,
ERC165
{
using SafeERC20 for IERC20;
Expand All @@ -56,11 +64,11 @@ abstract contract OrderBookV3ArbOrderTaker is

IOrderBookV3 public sOrderBook;
EncodedDispatch public sI9rDispatch;
IInterpreterV1 public sI9r;
IInterpreterV2 public sI9r;
IInterpreterStoreV1 public sI9rStore;

constructor(bytes32 metaHash, DeployerDiscoverableMetaV2ConstructionConfig memory config)
DeployerDiscoverableMetaV2(metaHash, config)
constructor(bytes32 metaHash, DeployerDiscoverableMetaV3ConstructionConfig memory config)
DeployerDiscoverableMetaV3(metaHash, config)
{
_disableInitializers();
}
Expand Down Expand Up @@ -103,18 +111,25 @@ abstract contract OrderBookV3ArbOrderTaker is
if (LibBytecode.sourceCount(config.evaluableConfig.bytecode) > 0) {
address expression;

uint256[] memory entrypoints = new uint256[](1);
entrypoints[SourceIndex.unwrap(BEFORE_ARB_SOURCE_INDEX)] = BEFORE_ARB_MIN_OUTPUTS;

bytes memory io;
// We have to trust the deployer because it produces the expression
// address for dispatch anyway.
// All external functions on this contract have `onlyNotInitializing`
// modifier on them so can't be reentered here anyway.
//slither-disable-next-line reentrancy-benign
(sI9r, sI9rStore, expression) = config.evaluableConfig.deployer.deployExpression(
config.evaluableConfig.bytecode, config.evaluableConfig.constants, entrypoints
(sI9r, sI9rStore, expression, io) = config.evaluableConfig.deployer.deployExpression2(
config.evaluableConfig.bytecode, config.evaluableConfig.constants
);
sI9rDispatch = LibEncodedDispatch.encode(expression, BEFORE_ARB_SOURCE_INDEX, BEFORE_ARB_MAX_OUTPUTS);
{
uint256 inputs;
assembly ("memory-safe") {
inputs := and(mload(add(io, 1)), 0xFF)
}
if (inputs != 0) {
revert NonZeroBeforeArbInputs(inputs);
}
}
sI9rDispatch = LibEncodedDispatch.encode2(expression, BEFORE_ARB_SOURCE_INDEX, BEFORE_ARB_MAX_OUTPUTS);
}

return ICLONEABLE_V2_SUCCESS;
Expand All @@ -138,11 +153,12 @@ abstract contract OrderBookV3ArbOrderTaker is
// Run the access control dispatch if it is set.
EncodedDispatch dispatch = sI9rDispatch;
if (EncodedDispatch.unwrap(dispatch) > 0) {
(uint256[] memory stack, uint256[] memory kvs) = sI9r.eval(
(uint256[] memory stack, uint256[] memory kvs) = sI9r.eval2(
sI9rStore,
DEFAULT_STATE_NAMESPACE,
LibNamespace.qualifyNamespace(DEFAULT_STATE_NAMESPACE, address(this)),
dispatch,
LibContext.build(new uint256[][](0), new SignedContextV1[](0))
LibContext.build(new uint256[][](0), new SignedContextV1[](0)),
new uint256[](0)
);
// This can only happen if interpreter is broken.
if (stack.length > 0) {
Expand Down
Loading

0 comments on commit e611e45

Please sign in to comment.