Skip to content

Commit

Permalink
wip on e2
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Nov 21, 2023
1 parent fe3fa90 commit 19b42b0
Show file tree
Hide file tree
Showing 33 changed files with 466 additions and 348 deletions.
6 changes: 3 additions & 3 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 @@ -28,3 +25,6 @@
[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 lib/rain.factory
2 changes: 1 addition & 1 deletion lib/rain.interpreter
56 changes: 35 additions & 21 deletions src/abstract/OrderBookV3ArbOrderTaker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ import {
DeployerDiscoverableMetaV3ConstructionConfig,
LibMeta
} from "lib/rain.interpreter/src/abstract/DeployerDiscoverableMetaV3.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";
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 "lib/rain.factory/src/interface/ICloneableV2.sol";
import {EncodedDispatch, LibEncodedDispatch} from "lib/rain.interpreter/src/lib/caller/LibEncodedDispatch.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 +36,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 @@ -56,7 +62,7 @@ abstract contract OrderBookV3ArbOrderTaker is

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

constructor(bytes32 metaHash, DeployerDiscoverableMetaV3ConstructionConfig memory config)
Expand Down Expand Up @@ -103,18 +109,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 +151,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,
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
41 changes: 26 additions & 15 deletions src/abstract/OrderBookV3FlashBorrower.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ import {LibEncodedDispatch, EncodedDispatch} from "lib/rain.interpreter/src/lib/
import {LibContext} from "lib/rain.interpreter/src/lib/caller/LibContext.sol";
import {LibBytecode} from "lib/rain.interpreter/src/lib/bytecode/LibBytecode.sol";
import {ON_FLASH_LOAN_CALLBACK_SUCCESS} from "../interface/ierc3156/IERC3156FlashBorrower.sol";
import {EvaluableConfigV2} from "rain.interpreter/src/lib/caller/LibEvaluable.sol";
import {IOrderBookV3, TakeOrdersConfigV2, NoOrders} from "../interface/unstable/IOrderBookV3.sol";
import {ICloneableV2, ICLONEABLE_V2_SUCCESS} from "lib/rain.factory/src/interface/ICloneableV2.sol";
import {
IInterpreterV2, SourceIndex, DEFAULT_STATE_NAMESPACE
IInterpreterV2, SourceIndexV2, DEFAULT_STATE_NAMESPACE
} from "lib/rain.interpreter/src/interface/unstable/IInterpreterV2.sol";
import {IERC3156FlashBorrower} from "../interface/ierc3156/IERC3156FlashBorrower.sol";
import {IInterpreterStoreV1} from "lib/rain.interpreter/src/interface/IInterpreterStoreV1.sol";
import {BadLender, MinimumOutput, NonZeroBeforeArbStack, Initializing} from "./OrderBookV3ArbCommon.sol";
import {SignedContextV1} from "rain.interpreter/src/interface/IInterpreterCallerV2.sol";
import {EvaluableConfigV3, SignedContextV1} from "rain.interpreter/src/interface/IInterpreterCallerV2.sol";

/// Thrown when the initiator is not the order book.
/// @param badInitiator The untrusted initiator of the flash loan.
Expand All @@ -37,24 +36,29 @@ error FlashLoanFailed();
/// Thrown when the swap fails.
error SwapFailed();

/// Thrown when "Before arb" expects inputs.
error NonZeroBeforeArbInputs();

/// Config for `OrderBookV3FlashBorrower` to initialize.
/// @param orderBook The `IOrderBookV3` contract to arb against.
/// @param evaluableConfig The config to eval for access control to arb.
/// @param implementationData Arbitrary bytes to pass to the implementation in
/// the `beforeInitialize` hook.
struct OrderBookV3FlashBorrowerConfigV2 {
address orderBook;
EvaluableConfigV2 evaluableConfig;
EvaluableConfigV3 evaluableConfig;
bytes implementationData;
}

/// @dev "Before arb" is evaluated before the flash loan is taken. Ostensibly
/// allows for some kind of access control to the arb.
SourceIndex constant BEFORE_ARB_SOURCE_INDEX = SourceIndex.wrap(0);
SourceIndexV2 constant BEFORE_ARB_SOURCE_INDEX = SourceIndexV2.wrap(0);
/// @dev "Before arb" has no inputs.
uint256 constant BEFORE_ARB_MIN_INPUTS = 0;
/// @dev "Before arb" has no outputs.
uint256 constant BEFORE_ARB_MIN_OUTPUTS = 0;
/// @dev "Before arb" has no outputs.
uint16 constant BEFORE_ARB_MAX_OUTPUTS = 0;
uint256 constant BEFORE_ARB_MAX_OUTPUTS = 0;

/// @title OrderBookV3FlashBorrower
/// @notice Abstract contract that liq-source specifialized contracts can inherit
Expand Down Expand Up @@ -108,7 +112,7 @@ abstract contract OrderBookV3FlashBorrower is
/// The encoded dispatch that will run for access control to `arb`.
EncodedDispatch public sI9rDispatch;
/// The interpreter that will eval access control to `arb`.
IInterpreterV1 public sI9r;
IInterpreterV2 public sI9r;
/// The associated store for the interpreter.
IInterpreterStoreV1 public sI9rStore;

Expand Down Expand Up @@ -156,18 +160,24 @@ abstract contract OrderBookV3FlashBorrower 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 the 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);
// There can't be any inputs because we don't pass any in.
uint256 inputs;
assembly ("memory-safe") {
inputs := and(mload(add(io, 1)), 0xFF)
}
if (inputs > 0) {
revert NonZeroBeforeArbInputs();
}
sI9rDispatch = LibEncodedDispatch.encode2(expression, BEFORE_ARB_SOURCE_INDEX, BEFORE_ARB_MAX_OUTPUTS);
}

return ICLONEABLE_V2_SUCCESS;
Expand Down Expand Up @@ -278,11 +288,12 @@ abstract contract OrderBookV3FlashBorrower 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,
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 the interpreter is broken.
if (stack.length > 0) {
Expand Down
Loading

0 comments on commit 19b42b0

Please sign in to comment.