// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
library BitLib {
// - if x is a nonzero uint64:
// return number of zeroes in x that do not have a 1 to their right
// - otherwise:
// return 64
function ctz64(uint x) internal pure returns (uint256 c) {
unchecked {
assembly ("memory-safe") {
// clean
x:= and(x,0xffffffffffffffff)
// 7th bit
c:= shl(6,iszero(x))
// isolate lsb
x := and(x, add(not(x), 1))
// 6th bit
c := or(c,shl(5, lt(0xffffffff, x)))
// debruijn lookup
c := or(c, byte(shr(251, mul(shr(c, x), shl(224, 0x077cb531))),
0x00011c021d0e18031e16140f191104081f1b0d17151310071a0c12060b050a09))
}
}
}
// Function fls is MIT License. Copyright (c) 2022 Solady.
/// @dev find last set.
/// Returns the index of the most significant bit of `x`,
/// counting from the least significant bit position.
/// If `x` is zero, returns 256.
/// Equivalent to `log2(x)`, but without reverting for the zero case.
function fls(uint256 x) internal pure returns (uint256 r) {
assembly ("memory-safe") {
r := shl(8, iszero(x))
r := or(r, shl(7, lt(0xffffffffffffffffffffffffffffffff, x)))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
// For the remaining 32 bits, use a De Bruijn lookup.
x := shr(r, x)
x := or(x, shr(1, x))
x := or(x, shr(2, x))
x := or(x, shr(4, x))
x := or(x, shr(8, x))
x := or(x, shr(16, x))
// forgefmt: disable-next-item
r := or(r, byte(shr(251, mul(x, shl(224, 0x07c4acdd))),
0x0009010a0d15021d0b0e10121619031e080c141c0f111807131b17061a05041f))
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
/* The constants below are written as literals to optimize gas. For all the relevant constants here, the non-literal expression that computes them is checked in `Constants.t.sol`. */
uint constant ONE = 1;
uint constant ONES = type(uint).max;
uint constant TOPBIT = 0x8000000000000000000000000000000000000000000000000000000000000000;
uint constant NOT_TOPBIT = 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
/* **sizes must match field sizes in `structs.ts` where relevant** */
/* Number of bits used to represent a tick */
uint constant TICK_BITS = 21;
/* Number of bits used to represent an offer id */
uint constant OFFER_BITS = 32;
/* Maximum possible size of a field -- protects against wrong code changes. Constraint given by `BitLib.ctz64`. */
uint constant MAX_FIELD_SIZE = 64;
/* `X_SIZE_BITS` is 1+log2 of the size of `X`, where size is the number of elements it holds.In a field, an element is a bit; in a leaf, an element is a pair of offer ids. For `LEVEL`s and `ROOT`, the value must be exact, so only power-of-2 sizes are allowed. */
uint constant LEAF_SIZE_BITS = 2;
uint constant LEVEL_SIZE_BITS = 6;
uint constant ROOT_SIZE_BITS = 1;
/* `X_SIZE` is `2**X_SIZE_BITS` */
int constant LEAF_SIZE = 4;
int constant LEVEL_SIZE = 64;
int constant ROOT_SIZE = 2;
/* `X_SIZE_MASK` is `0...01...1` where the number of 1s is `X_SIZE_BITS` */
uint constant LEAF_SIZE_MASK = 0x3;
uint constant LEVEL_SIZE_MASK = 0x3f;
uint constant ROOT_SIZE_MASK = 0x1;
/* `0...01...1` with `OFFER_BITS` 1s at the end */
uint constant OFFER_MASK = 0xffffffff;
/* Same as `ROOT_SIZE` */
int constant NUM_LEVEL1 = 2;
/* Same as `NUM_LEVEL1 * LEVEL_SIZE` */
int constant NUM_LEVEL2 = 128;
/* Same as `NUM_LEVEL2 * LEVEL_SIZE` */
int constant NUM_LEVEL3 = 8192;
/* Same as `NUM_LEVEL3 * LEVEL_SIZE` */
int constant NUM_LEAFS = 524288;
/* Same as `NUM_LEAFS * LEAF` */
int constant NUM_BINS = 2097152;
/* min and max bins are defined like min and max int. */
int constant MIN_BIN = -1048576;
int constant MAX_BIN = 1048575;
/* The tick range is the largest such that the mantissa of `1.0001^MAX_TICK` fits on 128 bits (and thus can be multiplied by volumes). */
int constant MIN_TICK = -887272;
int constant MAX_TICK = 887272;
/* These are reference values for what the function `tickFromRatio` function will return, not the most possible accurate values for the min and max tick. */
uint constant MIN_RATIO_MANTISSA = 170153974464283981435225617938057077692;
int constant MIN_RATIO_EXP = 255;
uint constant MAX_RATIO_MANTISSA = 340256786836388094050805785052946541084;
int constant MAX_RATIO_EXP = 0;
/* `MANTISSA_BITS` is the number of bits used in the mantissa of normalized floats that represent ratios. 128 means we can multiply all allowed volumes by the mantissa and not overflow. */
uint constant MANTISSA_BITS = 128;
uint constant MANTISSA_BITS_MINUS_ONE = 127;
/*
With `|tick|<=887272` and normalized mantissas on 128 bits, the maximum possible mantissa is `340282295208261841796968287475569060645`, so the maximum safe volume before overflow is `NO_OVERFLOW_AMOUNT = 340282438633630198193436196978374475856` (slightly above `2**128`).
For ease of use, we could pick a simpler, slightly smaller max safe volume: `(1<<128)-1`.
However the `*ByVolume` functions get a price by (abstractly) performing `outboundAmount/inboundAmount`. If we limit all volumes to `NO_OVERFLOW_AMOUNT` but aren't more restrictive than that, since `type(uint128).max > 1.0001**MAX_TICK`, we will get ratios that are outside the price boundaries.
We thus pick a uniform, easy to remember constraint on volumes that works everywhere: `(1<<127)-1`
*/
uint constant MAX_SAFE_VOLUME = 170141183460469231731687303715884105727;
/* When a market order consumes offers, the implementation uses recursion consumes additional EVM stack space at each new offer. To avoid reverting due to stack overflow, Mangrove keeps a counter and stops the market order when it reaches a maximum recursion depth. `INITIAL_MAX_RECURSION_DEPTH` is the maximum recursion depth given at deployment time.
See `maxRecursionDepth` in `structs.ts`
Without optimizer enabled it fails above 79. With optimizer and 200 runs it fails above 80. Set default a bit lower to be safe. */
uint constant INITIAL_MAX_RECURSION_DEPTH = 75;
/* When a market order consumes offers, it may use gas on offers that fail to deliver. To avoid reverts after a string of failing offers that consumes more gas than is available in a block, Mangrove stops a market order after it has gone through failing offers such that their cumulative `gasreq` is greater than the global `maxGasreqForFailingOffers` parameter. At deployment, `maxGasreqForFailingOffers` is set to:
```
INITIAL_MAX_GASREQ_FOR_FAILING_OFFERS_MULTIPLIER * gasmax
``` */
uint constant INITIAL_MAX_GASREQ_FOR_FAILING_OFFERS_MULTIPLIER = 3;
/* Those two constants are used in `TickLib.ratioFromTick` to convert a log base 1.0001 to a log base 2. */
uint constant LOG_BP_SHIFT = 235;
uint constant LOG_BP_2X235 = 382733217082594961806491056566382061424140926068392360945012727618364717537;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import {Field} from "@mgv/lib/core/TickTreeLib.sol";
import {ONES} from "@mgv/lib/core/Constants.sol";
import {BitLib} from "@mgv/lib/core/BitLib.sol";
/*
The density of a semibook is the number of outbound tokens per gas required. An offer must a respect a semibook's density.
Density can be < 1.
The density of a semibook is stored as a 9 bits float. For convenience, governance functions read density as a 96.32 fixed point number. The functions below give conversion utilities between the two formats
As a guideline, fixed-point densities should be uints and should use hungarian notation (for instance `uint density96X32`). Floating-point densities should use the Density user-defined type.
The float <-> fixed conversion is format agnostic but the expectation is that fixed points are 96x32, and floats are 2-bit mantissa, 7bit exponent with bias 32.
The encoding is nonstandard so the code can be simpler.
There are no subnormal floats in this encoding, `[exp][mantissa]` means:
```
if exp is 0 or 1: 0bmantissa * 2^-32
otherwise: 0b1.mantissa * 2^(exp-32)
```
so the small values have some holes:
```
coeff exponent available | coeff exponent available
--------------------------------------------------------------
0b0.00 | 0b1.10 -31
0b1.00 -32 | 0b1.11 -31 no
0b1.01 -32 no | 0b1.00 -30
0b1.10 -32 no | 0b1.01 -30
0b1.11 -32 no | 0b1.10 -30
0b1.00 -31 | 0b1.11 -30
0b1.01 -31 no | 0b1.00 -29
```
*/
type Density is uint;
using DensityLib for Density global;
library DensityLib {
/* Numbers in this file assume that density is 9 bits in structs.ts */
uint constant BITS = 9; // must match structs.ts
uint constant MANTISSA_BITS = 2;
uint constant SUBNORMAL_LIMIT = ~(ONES << (MANTISSA_BITS+1));
uint constant MANTISSA_MASK = ~(ONES << MANTISSA_BITS);
uint constant MASK = ~(ONES << BITS);
uint constant MANTISSA_INTEGER = 1 << MANTISSA_BITS;
uint constant EXPONENT_BITS = BITS - MANTISSA_BITS;
function eq(Density a, Density b) internal pure returns (bool) { unchecked {
return Density.unwrap(a) == Density.unwrap(b);
}}
/* Check the size of a fixed-point formatted density */
function checkDensity96X32(uint density96X32) internal pure returns (bool) { unchecked {
return density96X32 < (1<<(96+32));
}}
/* fixed-point -> float conversion */
/* Warning: no bit cleaning (expected to be done by Local's code), no checking that the input is on 128 bits. */
/* floats with `[exp=1]` are not in the image of fromFixed. They are considered noncanonical. */
function from96X32(uint density96X32) internal pure returns (Density) { unchecked {
if (density96X32 <= MANTISSA_MASK) {
return Density.wrap(density96X32);
}
// invariant: `exp >= 2` (so not 0)
uint exp = BitLib.fls(density96X32);
return make(density96X32 >> (exp-MANTISSA_BITS),exp);
}}
/* float -> fixed-point conversion */
function to96X32(Density density) internal pure returns (uint) { unchecked {
/* also accepts floats not generated by fixedToFloat, i.e. with exp=1 */
if (Density.unwrap(density) <= SUBNORMAL_LIMIT) {
return Density.unwrap(density) & MANTISSA_MASK;
}
/* assumes exp is on the right number of bits */
// invariant: `exp >= 2`
uint shift = (Density.unwrap(density) >> MANTISSA_BITS) - MANTISSA_BITS;
return ((Density.unwrap(density) & MANTISSA_MASK) | MANTISSA_INTEGER) << shift;
}}
function mantissa(Density density) internal pure returns (uint) { unchecked {
return Density.unwrap(density) & MANTISSA_MASK;
}}
function exponent(Density density) internal pure returns (uint) { unchecked {
return Density.unwrap(density) >> MANTISSA_BITS;
}}
/* Make a float from a mantissa and an exponent. May make a noncanonical float. */
/* Warning: no checks */
function make(uint _mantissa, uint _exponent) internal pure returns (Density) { unchecked {
return Density.wrap((_exponent << MANTISSA_BITS) | (_mantissa & MANTISSA_MASK));
}}
/* None of the functions below will overflow if m is 96bit wide.
Density being a 96.32 number is useful because:
- Most of its range is representable with the 9-bits float format chosen
- It does not overflow when multiplied with a 96bit number, which is the size chosen to represent token amounts in Mangrove.
- Densities below `2^-32` need `> 4e9` gasreq to force gives > 0, which is not realistic
*/
/* Multiply the density with m, rounded towards zero. */
/* May overflow if `|m|>9` */
function multiply(Density density, uint m) internal pure returns (uint) { unchecked {
return (m * density.to96X32())>>32;
}}
/* Multiply the density with m, rounded towards +infinity. */
/* May overflow if `|m|>96` */
function multiplyUp(Density density, uint m) internal pure returns (uint) { unchecked {
uint part = m * density.to96X32();
return (part >> 32) + (part%(2<<32) == 0 ? 0 : 1);
}}
/* Convenience function: get a fixed-point density from the given parameters. Computes the price of gas in outbound tokens (base units), then multiplies by cover_factor. */
/* Warning: you must multiply input usd prices by 100 */
/* not supposed to be gas optimized */
function paramsTo96X32(
uint outbound_decimals,
uint gasprice_in_Mwei,
uint eth_in_centiusd,
uint outbound_display_in_centiusd,
uint cover_factor
) internal pure returns (uint) {
// Do not use unchecked here
require(uint8(outbound_decimals) == outbound_decimals,"DensityLib/fixedFromParams1/decimals/wrong");
uint num = cover_factor * gasprice_in_Mwei * (10**outbound_decimals) * eth_in_centiusd;
// use * instead of << to trigger overflow check
return (num * (1 << 32)) / (outbound_display_in_centiusd * 1e12);
}
/* Version with token in Mwei instead of usd */
function paramsTo96X32(
uint outbound_decimals,
uint gasprice_in_Mwei,
uint outbound_display_in_Mwei,
uint cover_factor
) internal pure returns (uint) {
/* **Do not** use unchecked here. */
require(uint8(outbound_decimals) == outbound_decimals,"DensityLib/fixedFromParams2/decimals/wrong");
uint num = cover_factor * gasprice_in_Mwei * (10**outbound_decimals);
/* use `*` instead of `<<` to trigger overflow check */
return (num * (1 << 32)) / outbound_display_in_Mwei;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
/* ************************************************** *
GENERATED FILE. DO NOT EDIT.
* ************************************************** */
/* since you can't convert bool to uint in an expression without conditionals,
* we add a file-level function and rely on compiler optimization
*/
function uint_of_bool(bool b) pure returns (uint u) {
assembly ("memory-safe") { u := b }
}
import "@mgv/lib/core/Constants.sol";
struct GlobalUnpacked {
address monitor;
bool useOracle;
bool notify;
uint gasprice;
uint gasmax;
bool dead;
uint maxRecursionDepth;
uint maxGasreqForFailingOffers;
}
//some type safety for each struct
type Global is uint;
using GlobalLib for Global global;
////////////// ADDITIONAL DEFINITIONS, IF ANY ////////////////
////////////// END OF ADDITIONAL DEFINITIONS /////////////////
library GlobalLib {
// number of bits in each field
uint constant monitor_bits = 160;
uint constant useOracle_bits = 1;
uint constant notify_bits = 1;
uint constant gasprice_bits = 26;
uint constant gasmax_bits = 24;
uint constant dead_bits = 1;
uint constant maxRecursionDepth_bits = 8;
uint constant maxGasreqForFailingOffers_bits = 32;
// number of bits before each field
uint constant monitor_before = 0 + 0;
uint constant useOracle_before = monitor_before + monitor_bits;
uint constant notify_before = useOracle_before + useOracle_bits;
uint constant gasprice_before = notify_before + notify_bits;
uint constant gasmax_before = gasprice_before + gasprice_bits;
uint constant dead_before = gasmax_before + gasmax_bits;
uint constant maxRecursionDepth_before = dead_before + dead_bits;
uint constant maxGasreqForFailingOffers_before = maxRecursionDepth_before + maxRecursionDepth_bits;
// focus-mask: 1s at field location, 0s elsewhere
uint constant monitor_mask_inv = (ONES << 256 - monitor_bits) >> monitor_before;
uint constant useOracle_mask_inv = (ONES << 256 - useOracle_bits) >> useOracle_before;
uint constant notify_mask_inv = (ONES << 256 - notify_bits) >> notify_before;
uint constant gasprice_mask_inv = (ONES << 256 - gasprice_bits) >> gasprice_before;
uint constant gasmax_mask_inv = (ONES << 256 - gasmax_bits) >> gasmax_before;
uint constant dead_mask_inv = (ONES << 256 - dead_bits) >> dead_before;
uint constant maxRecursionDepth_mask_inv = (ONES << 256 - maxRecursionDepth_bits) >> maxRecursionDepth_before;
uint constant maxGasreqForFailingOffers_mask_inv = (ONES << 256 - maxGasreqForFailingOffers_bits) >> maxGasreqForFailingOffers_before;
// cleanup-mask: 0s at field location, 1s elsewhere
uint constant monitor_mask = ~monitor_mask_inv;
uint constant useOracle_mask = ~useOracle_mask_inv;
uint constant notify_mask = ~notify_mask_inv;
uint constant gasprice_mask = ~gasprice_mask_inv;
uint constant gasmax_mask = ~gasmax_mask_inv;
uint constant dead_mask = ~dead_mask_inv;
uint constant maxRecursionDepth_mask = ~maxRecursionDepth_mask_inv;
uint constant maxGasreqForFailingOffers_mask = ~maxGasreqForFailingOffers_mask_inv;
// cast-mask: 0s followed by |field| trailing 1s
uint constant monitor_cast_mask = ~(ONES << monitor_bits);
uint constant useOracle_cast_mask = ~(ONES << useOracle_bits);
uint constant notify_cast_mask = ~(ONES << notify_bits);
uint constant gasprice_cast_mask = ~(ONES << gasprice_bits);
uint constant gasmax_cast_mask = ~(ONES << gasmax_bits);
uint constant dead_cast_mask = ~(ONES << dead_bits);
uint constant maxRecursionDepth_cast_mask = ~(ONES << maxRecursionDepth_bits);
uint constant maxGasreqForFailingOffers_cast_mask = ~(ONES << maxGasreqForFailingOffers_bits);
// size-related error message
string constant monitor_size_error = "mgv/config/monitor/160bits";
string constant useOracle_size_error = "mgv/config/useOracle/1bits";
string constant notify_size_error = "mgv/config/notify/1bits";
string constant gasprice_size_error = "mgv/config/gasprice/26bits";
string constant gasmax_size_error = "mgv/config/gasmax/24bits";
string constant dead_size_error = "mgv/config/dead/1bits";
string constant maxRecursionDepth_size_error = "mgv/config/maxRecursionDepth/8bits";
string constant maxGasreqForFailingOffers_size_error = "mgv/config/maxGasreqForFailingOffers/32bits";
// from packed to in-memory struct
function to_struct(Global __packed) internal pure returns (GlobalUnpacked memory __s) { unchecked {
__s.monitor = address(uint160(uint(Global.unwrap(__packed) << monitor_before) >> (256 - monitor_bits)));
__s.useOracle = ((Global.unwrap(__packed) & useOracle_mask_inv) > 0);
__s.notify = ((Global.unwrap(__packed) & notify_mask_inv) > 0);
__s.gasprice = uint(Global.unwrap(__packed) << gasprice_before) >> (256 - gasprice_bits);
__s.gasmax = uint(Global.unwrap(__packed) << gasmax_before) >> (256 - gasmax_bits);
__s.dead = ((Global.unwrap(__packed) & dead_mask_inv) > 0);
__s.maxRecursionDepth = uint(Global.unwrap(__packed) << maxRecursionDepth_before) >> (256 - maxRecursionDepth_bits);
__s.maxGasreqForFailingOffers = uint(Global.unwrap(__packed) << maxGasreqForFailingOffers_before) >> (256 - maxGasreqForFailingOffers_bits);
}}
// equality checking
function eq(Global __packed1, Global __packed2) internal pure returns (bool) { unchecked {
return Global.unwrap(__packed1) == Global.unwrap(__packed2);
}}
// from packed to a tuple
function unpack(Global __packed) internal pure returns (address __monitor, bool __useOracle, bool __notify, uint __gasprice, uint __gasmax, bool __dead, uint __maxRecursionDepth, uint __maxGasreqForFailingOffers) { unchecked {
__monitor = address(uint160(uint(Global.unwrap(__packed) << monitor_before) >> (256 - monitor_bits)));
__useOracle = ((Global.unwrap(__packed) & useOracle_mask_inv) > 0);
__notify = ((Global.unwrap(__packed) & notify_mask_inv) > 0);
__gasprice = uint(Global.unwrap(__packed) << gasprice_before) >> (256 - gasprice_bits);
__gasmax = uint(Global.unwrap(__packed) << gasmax_before) >> (256 - gasmax_bits);
__dead = ((Global.unwrap(__packed) & dead_mask_inv) > 0);
__maxRecursionDepth = uint(Global.unwrap(__packed) << maxRecursionDepth_before) >> (256 - maxRecursionDepth_bits);
__maxGasreqForFailingOffers = uint(Global.unwrap(__packed) << maxGasreqForFailingOffers_before) >> (256 - maxGasreqForFailingOffers_bits);
}}
// getters
function monitor(Global __packed) internal pure returns(address) { unchecked {
return address(uint160(uint(Global.unwrap(__packed) << monitor_before) >> (256 - monitor_bits)));
}}
// setters
function monitor(Global __packed,address val) internal pure returns(Global) { unchecked {
return Global.wrap((Global.unwrap(__packed) & monitor_mask) | (uint(uint160(val)) << (256 - monitor_bits)) >> monitor_before);
}}
function useOracle(Global __packed) internal pure returns(bool) { unchecked {
return ((Global.unwrap(__packed) & useOracle_mask_inv) > 0);
}}
// setters
function useOracle(Global __packed,bool val) internal pure returns(Global) { unchecked {
return Global.wrap((Global.unwrap(__packed) & useOracle_mask) | (uint_of_bool(val) << (256 - useOracle_bits)) >> useOracle_before);
}}
function notify(Global __packed) internal pure returns(bool) { unchecked {
return ((Global.unwrap(__packed) & notify_mask_inv) > 0);
}}
// setters
function notify(Global __packed,bool val) internal pure returns(Global) { unchecked {
return Global.wrap((Global.unwrap(__packed) & notify_mask) | (uint_of_bool(val) << (256 - notify_bits)) >> notify_before);
}}
function gasprice(Global __packed) internal pure returns(uint) { unchecked {
return uint(Global.unwrap(__packed) << gasprice_before) >> (256 - gasprice_bits);
}}
// setters
function gasprice(Global __packed,uint val) internal pure returns(Global) { unchecked {
return Global.wrap((Global.unwrap(__packed) & gasprice_mask) | (val << (256 - gasprice_bits)) >> gasprice_before);
}}
function gasmax(Global __packed) internal pure returns(uint) { unchecked {
return uint(Global.unwrap(__packed) << gasmax_before) >> (256 - gasmax_bits);
}}
// setters
function gasmax(Global __packed,uint val) internal pure returns(Global) { unchecked {
return Global.wrap((Global.unwrap(__packed) & gasmax_mask) | (val << (256 - gasmax_bits)) >> gasmax_before);
}}
function dead(Global __packed) internal pure returns(bool) { unchecked {
return ((Global.unwrap(__packed) & dead_mask_inv) > 0);
}}
// setters
function dead(Global __packed,bool val) internal pure returns(Global) { unchecked {
return Global.wrap((Global.unwrap(__packed) & dead_mask) | (uint_of_bool(val) << (256 - dead_bits)) >> dead_before);
}}
function maxRecursionDepth(Global __packed) internal pure returns(uint) { unchecked {
return uint(Global.unwrap(__packed) << maxRecursionDepth_before) >> (256 - maxRecursionDepth_bits);
}}
// setters
function maxRecursionDepth(Global __packed,uint val) internal pure returns(Global) { unchecked {
return Global.wrap((Global.unwrap(__packed) & maxRecursionDepth_mask) | (val << (256 - maxRecursionDepth_bits)) >> maxRecursionDepth_before);
}}
function maxGasreqForFailingOffers(Global __packed) internal pure returns(uint) { unchecked {
return uint(Global.unwrap(__packed) << maxGasreqForFailingOffers_before) >> (256 - maxGasreqForFailingOffers_bits);
}}
// setters
function maxGasreqForFailingOffers(Global __packed,uint val) internal pure returns(Global) { unchecked {
return Global.wrap((Global.unwrap(__packed) & maxGasreqForFailingOffers_mask) | (val << (256 - maxGasreqForFailingOffers_bits)) >> maxGasreqForFailingOffers_before);
}}
// from in-memory struct to packed
function t_of_struct(GlobalUnpacked memory __s) internal pure returns (Global) { unchecked {
return pack(__s.monitor, __s.useOracle, __s.notify, __s.gasprice, __s.gasmax, __s.dead, __s.maxRecursionDepth, __s.maxGasreqForFailingOffers);
}}
// from arguments to packed
function pack(address __monitor, bool __useOracle, bool __notify, uint __gasprice, uint __gasmax, bool __dead, uint __maxRecursionDepth, uint __maxGasreqForFailingOffers) internal pure returns (Global) { unchecked {
uint __packed;
__packed |= (uint(uint160(__monitor)) << (256 - monitor_bits)) >> monitor_before;
__packed |= (uint_of_bool(__useOracle) << (256 - useOracle_bits)) >> useOracle_before;
__packed |= (uint_of_bool(__notify) << (256 - notify_bits)) >> notify_before;
__packed |= (__gasprice << (256 - gasprice_bits)) >> gasprice_before;
__packed |= (__gasmax << (256 - gasmax_bits)) >> gasmax_before;
__packed |= (uint_of_bool(__dead) << (256 - dead_bits)) >> dead_before;
__packed |= (__maxRecursionDepth << (256 - maxRecursionDepth_bits)) >> maxRecursionDepth_before;
__packed |= (__maxGasreqForFailingOffers << (256 - maxGasreqForFailingOffers_bits)) >> maxGasreqForFailingOffers_before;
return Global.wrap(__packed);
}}
// input checking
function monitor_check(address __monitor) internal pure returns (bool) { unchecked {
return (uint(uint160(__monitor)) & monitor_cast_mask) == uint(uint160(__monitor));
}}
function useOracle_check(bool __useOracle) internal pure returns (bool) { unchecked {
return (uint_of_bool(__useOracle) & useOracle_cast_mask) == uint_of_bool(__useOracle);
}}
function notify_check(bool __notify) internal pure returns (bool) { unchecked {
return (uint_of_bool(__notify) & notify_cast_mask) == uint_of_bool(__notify);
}}
function gasprice_check(uint __gasprice) internal pure returns (bool) { unchecked {
return (__gasprice & gasprice_cast_mask) == __gasprice;
}}
function gasmax_check(uint __gasmax) internal pure returns (bool) { unchecked {
return (__gasmax & gasmax_cast_mask) == __gasmax;
}}
function dead_check(bool __dead) internal pure returns (bool) { unchecked {
return (uint_of_bool(__dead) & dead_cast_mask) == uint_of_bool(__dead);
}}
function maxRecursionDepth_check(uint __maxRecursionDepth) internal pure returns (bool) { unchecked {
return (__maxRecursionDepth & maxRecursionDepth_cast_mask) == __maxRecursionDepth;
}}
function maxGasreqForFailingOffers_check(uint __maxGasreqForFailingOffers) internal pure returns (bool) { unchecked {
return (__maxGasreqForFailingOffers & maxGasreqForFailingOffers_cast_mask) == __maxGasreqForFailingOffers;
}}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.2;
interface IERC20 {
function totalSupply() external view returns (uint);
function balanceOf(address account) external view returns (uint);
function transfer(address recipient, uint amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint amount) external returns (bool);
function transferFrom(address sender, address recipient, uint amount) external returns (bool);
function symbol() external view returns (string memory);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
function decimals() external view returns (uint8);
function name() external view returns (string memory);
}
// SPDX-License-Identifier: MIT
// This file must be kept up-to-date with the actual Mangrove interface.
pragma solidity >=0.7.0 <0.9.0;
import "@mgv/src/core/MgvLib.sol";
///@title Interface for the Mangrove contract.
interface IMangrove is HasMgvEvents {
// # Permit functions
///@notice See {IERC20Permit-DOMAIN_SEPARATOR}.
///@return the domain separator.
function DOMAIN_SEPARATOR() external view returns (bytes32);
///@notice See {IERC20Permit-PERMIT_TYPEHASH}.
///@return The permit type hash.
function PERMIT_TYPEHASH() external pure returns (bytes32);
///@notice Approves the spender to spend the amount of tokens on behalf of the caller.
///@param outbound_tkn The address of the (maker) outbound token.
///@param inbound_tkn The address of the (maker) inbound token.
///@param spender The address of the spender.
///@param value The amount of tokens to approve.
///@return true If the approval succeeded; always true.
function approve(address outbound_tkn, address inbound_tkn, address spender, uint value) external returns (bool);
///@notice Returns the allowance of the spender to spend tokens on behalf of the owner.
///@param outbound_tkn The address of the (maker) outbound token.
///@param inbound_tkn The address of the (maker) inbound token.
///@param owner The address of the owner.
///@param spender The address of the spender.
///@return amount The amount of tokens the spender is allowed to spend on behalf of the owner.
function allowance(address outbound_tkn, address inbound_tkn, address owner, address spender)
external
view
returns (uint amount);
///@notice Adapted from [Uniswap v2 contract](https://github.com/Uniswap/uniswap-v2-core/blob/55ae25109b7918565867e5c39f1e84b7edd19b2a/contracts/UniswapV2ERC20.sol#L81)
///@param outbound_tkn The address of the (maker) outbound token.
///@param inbound_tkn The address of the (maker) inbound token.
///@param owner The address of the owner.
///@param spender The address of the spender.
///@param value The amount of tokens to approve.
///@param deadline The deadline after which the permit is no longer valid.
///@param v The signature v parameter.
///@param r The signature r parameter.
///@param s The signature s parameter.
function permit(
address outbound_tkn,
address inbound_tkn,
address owner,
address spender,
uint value,
uint deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
///@notice See {IERC20Permit-nonces}.
///@param owner The address of the owner.
///@return nonce The current nonce of the owner.
function nonces(address owner) external view returns (uint nonce);
// # Taker functions
///@notice Performs a market order on a specified offer list taking offers up to a limit price.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param maxTick Must be `>= MIN_TICK` and `<= MAX_TICK`. The log of the limit price the taker is ready to pay (meaning: the log base 1.0001 of the ratio of inbound tokens over outbound tokens).
///@param fillVolume Must be `<= MAX_SAFE_VOLUME`. If `fillWants` is true, the amount of `olKey.outbound_tkn` the taker wants to buy; otherwise, the amount of `olKey.inbound_tkn` the taker wants to sell.
///@param fillWants If true, the matching engine tries to get the taker all they want; otherwise, the matching engine tries to sell all that the taker gives. In both cases subject to the price limit.
///@return takerGot The amount of `olKey.outbound_tkn` the taker got.
///@return takerGave The amount of `olKey.inbound_tkn` the taker gave.
///@return bounty The amount of native token the taker got as a bounty due to failing offers (in wei).
///@return feePaid The amount of `olKey.outbound_tkn` the taker paid as a fee to Mangrove.
///@dev The market order stops when there are no more offers at or below `maxTick`, when the end of the book has been reached, or:
///@dev - If `fillWants` is true, the market order stops when `fillVolume` units of `olKey.outbound_tkn` have been obtained. To buy a specific volume of `olKey.outbound_tkn` at any price, set `fillWants` to true, set `fillVolume` to the volume you want to buy, and set `maxTick` to the `MAX_TICK` constant.
///@dev - If `fillWants` is false, the market order stops when `fillVolume` units of `olKey.inbound_tkn` have been paid. To sell a specific volume of `olKey.inbound_tkn` at any price, set `fillWants` to false, set `fillVolume` to the volume you want to sell, and set `maxTick` to the `MAX_TICK` constant.
function marketOrderByTick(OLKey memory olKey, Tick maxTick, uint fillVolume, bool fillWants)
external
returns (uint takerGot, uint takerGave, uint bounty, uint feePaid);
///@notice Performs a market order on a specified offer list taking offers up to a limit price, while allowing to specify a custom `maxGasreqForFailingOffers`.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param maxTick Must be `>= MIN_TICK` and `<= MAX_TICK`. The log of the limit price the taker is ready to pay (meaning: the log base 1.0001 of the ratio of inbound tokens over outbound tokens).
///@param fillVolume Must be `<= MAX_SAFE_VOLUME`. If `fillWants` is true, the amount of `olKey.outbound_tkn` the taker wants to buy; otherwise, the amount of `olKey.inbound_tkn` the taker wants to sell.
///@param fillWants If true, the matching engine tries to get the taker all they want; otherwise, the matching engine tries to sell all that the taker gives. In both cases subject to the price limit.
///@param maxGasreqForFailingOffers The maximum allowed gas required for failing offers (in wei).
///@return takerGot The amount of `olKey.outbound_tkn` the taker got.
///@return takerGave The amount of `olKey.inbound_tkn` the taker gave.
///@return bounty The amount of native token the taker got as a bounty due to failing offers (in wei).
///@return feePaid The amount of `olKey.outbound_tkn` the taker paid as a fee to Mangrove.
///@dev Mangrove stops a market order after it has gone through failing offers such that their cumulative `gasreq` is greater than the global `maxGasreqForFailingOffers` parameter. This function can be used by the taker to override the default `maxGasreqForFailingOffers` parameter.
function marketOrderByTickCustom(
OLKey memory olKey,
Tick maxTick,
uint fillVolume,
bool fillWants,
uint maxGasreqForFailingOffers
) external returns (uint takerGot, uint takerGave, uint bounty, uint feePaid);
///@notice Performs a market order on a specified offer list taking offers up to a limit price defined by a ratio `inbound_tkn/outbound_tkn` of volumes.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param takerWants Must be `<= MAX_SAFE_VOLUME`. The amount the taker wants. This is used along with `takerGives` to derive a max price (`maxTick`) which is the lowest allowed tick in the offer list such that `log_1.0001(takerGives/takerWants) <= maxTick`.
///@param takerGives Must be `<= MAX_SAFE_VOLUME`. The amount the taker gives. This is used along with `takerWants` to derive a max price (`maxTick`) which is the lowest allowed tick in the offer list such that `log_1.0001(takerGives/takerWants) <= maxTick`.
///@param fillWants If true, the matching engine tries to get the taker all they want; otherwise, the matching engine tries to sell all that the taker gives. In both cases subject to the price limit.
///@return takerGot The amount of `olKey.outbound_tkn` the taker got.
///@return takerGave The amount of `olKey.inbound_tkn` the taker gave.
///@return bounty The amount of native token the taker got as a bounty due to failing offers (in wei).
///@return feePaid The amount of `olKey.outbound_tkn` the taker paid as a fee to Mangrove.
///@dev This function is just a wrapper for `marketOrderByTick`, see that function for details.
///@dev When deriving the tick, then `takerWants = 0` has a special meaning and the tick for the highest possible ratio between wants and gives will be used,
///@dev and if `takerGives = 0` and `takerWants != 0`, then the tick for the lowest possible ratio will be used.
function marketOrderByVolume(OLKey memory olKey, uint takerWants, uint takerGives, bool fillWants)
external
returns (uint takerGot, uint takerGave, uint bounty, uint feePaid);
///@notice Performs a market order on a specified offer list taking offers up to a limit price for a specified taker.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param maxTick Must be `>= MIN_TICK` and `<= MAX_TICK`. The log of the limit price the taker is ready to pay (meaning: the log base 1.0001 of the ratio of inbound tokens over outbound tokens).
///@param fillVolume Must be `<= MAX_SAFE_VOLUME`. If `fillWants` is true, the amount of `olKey.outbound_tkn` the taker wants to buy; otherwise, the amount of `olKey.inbound_tkn` the taker wants to sell.
///@param fillWants If true, the matching engine tries to get the taker all they want; otherwise, the matching engine tries to sell all that the taker gives. In both cases subject to the price limit.
///@param taker The taker from which amounts will be transferred from and to. If the `msg.sender`'s allowance for the given `olKey.outbound_tkn`,`olKey.inbound_tkn` is strictly less than the total amount eventually spent by `taker`, the call will fail.
///@return takerGot The amount of `olKey.outbound_tkn` the taker got.
///@return takerGave The amount of `olKey.inbound_tkn` the taker gave.
///@return bounty The amount of native token the taker got as a bounty due to failing offers (in wei).
///@return feePaid The amount of `olKey.outbound_tkn` the taker paid as a fee to Mangrove.
///@dev The `bounty` will be sent to `msg.sender` but transfers will be for `taker`. Requires prior permission.
///@dev See also `marketOrderByTick`.
function marketOrderForByTick(OLKey memory olKey, Tick maxTick, uint fillVolume, bool fillWants, address taker)
external
returns (uint takerGot, uint takerGave, uint bounty, uint feePaid);
///@notice Performs a market order on a specified offer list taking offers up to a limit price defined by a ratio `inbound_tkn/outbound_tkn` of volumes for a specified taker.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param takerWants Must be `<= MAX_SAFE_VOLUME`. The amount the taker wants. This is used along with `takerGives` to derive a max price (`maxTick`) which is the lowest allowed tick in the offer list such that `log_1.0001(takerGives/takerWants) <= maxTick`.
///@param takerGives Must be `<= MAX_SAFE_VOLUME`. The amount the taker gives. This is used along with `takerGives` to derive a max price (`maxTick`) which is the lowest allowed tick in the offer list such that `log_1.0001(takerGives/takerWants) <= maxTick`.
///@param fillWants If true, the matching engine tries to get the taker all they want; otherwise, the matching engine tries to sell all that the taker gives. In both cases subject to the price limit.
///@param taker The taker from which amounts will be transferred from and to the. If the `msg.sender`'s allowance for the given `olKey.outbound_tkn`,`olKey.inbound_tkn` are strictly less than the total amount eventually spent by `taker`, the call will fail.
///@return takerGot The amount of `olKey.outbound_tkn` the taker got.
///@return takerGave The amount of `olKey.inbound_tkn` the taker gave.
///@return bounty The amount of native token the taker got as a bounty due to failing offers (in wei).
///@return feePaid The amount of native token the taker paid as a fee (in wei of `olKey.outbound_tkn`).
///@dev The `bounty` will be send to `msg.sender` but transfers will be for `taker`. Requires prior permission.
///@dev See also `marketOrderByVolume`.
function marketOrderForByVolume(OLKey memory olKey, uint takerWants, uint takerGives, bool fillWants, address taker)
external
returns (uint takerGot, uint takerGave, uint bounty, uint feePaid);
// # Cleaning functions
/* # Cleaning */
///@notice Cleans multiple offers, i.e. executes them and removes them from the book if they fail, transferring the failure penalty as bounty to the caller.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param targets The offers to clean, identified by their (`offerId, tick, gasreq, takerWants`) that will make them fail.
///@param taker The taker used for transfers (should be able to deliver token amounts).
///@return successes The number of successfully cleaned offers.
///@return bounty The total bounty received by the caller.
///@dev If an offer succeeds, the execution of that offer is reverted, it stays in the book, and no bounty is paid; The `cleanByImpersonation` function itself will not revert.
///@dev Note that Mangrove won't attempt to execute an offer if the values in a target don't match its offer. To distinguish between a non-executed clean and a failed clean (due to the offer itself not failing), you must inspect the log (see `MgvLib.sol`) or check the received bounty.
///@dev Any `taker` can be impersonated when cleaning because:
///@dev - The function reverts if the offer succeeds, reverting any token transfers.
///@dev - After a `clean` where the offer has failed, all ERC20 token transfers have also been reverted -- but the sender will still have received the bounty of the failing offers. */
function cleanByImpersonation(OLKey memory olKey, MgvLib.CleanTarget[] calldata targets, address taker)
external
returns (uint successes, uint bounty);
// # Maker functions
///@notice Adds funds to Mangrove for the caller (the maker) to use for provisioning offers.
receive() external payable;
///@notice Adds funds to Mangrove for the caller (the maker) to use for provisioning offers.
function fund() external payable;
///@notice Adds funds to Mangrove for the maker to use for provisioning offers.
///@param maker The maker to add funds for.
function fund(address maker) external payable;
///@notice Withdraws the caller's (the maker's) free native tokens (funds for provisioning offers not locked by an offer) by transferring them to the caller.
///@param amount The amount to withdraw.
///@return noRevert Whether the transfer succeeded.
function withdraw(uint amount) external returns (bool noRevert);
///@notice Gets the maker's free balance of native tokens (funds for provisioning offers not locked by an offer).
///@param maker The maker to get the balance for.
///@return balance The maker's free balance of native tokens (funds for provisioning offers not locked by an offer).
function balanceOf(address maker) external view returns (uint balance);
///@notice Creates a new offer on Mangrove, where the caller is the maker. The maker can implement the `IMaker` interface to be called during offer execution.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param tick Must be `>= MIN_TICK` and `<= MAX_TICK`. The `tick` induces a price which is `1.0001^tick`. The actual tick of the offer will be the smallest tick `offerTick > tick` that satisfies `offerTick % tickSpacing == 0`.
///@param gives Must be `<= MAX_SAFE_VOLUME`. The amount of `olKey.outbound_tkn` the maker gives.
///@param gasreq The amount of gas required to execute the offer logic in the maker's `IMaker` implementation. This will limit the gas available, and the offer will fail if it spends more.
///@param gasprice The maximum gas price the maker is willing to pay a penalty for due to failing execution.
///@return offerId The id of the offer on Mangrove. Can be used to retract or update the offer (even to reuse a taken offer).
///@dev The gasreq and gasprice are used to derive the provision which will be used to pay a penalty if the offer fails.
///@dev This function is payable to enable delivery of the provision along with the offer creation.
function newOfferByTick(OLKey memory olKey, Tick tick, uint gives, uint gasreq, uint gasprice)
external
payable
returns (uint offerId);
///@notice Creates a new offer on Mangrove, where the caller is the maker. The maker can implement the `IMaker` interface to be called during offer execution.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param wants Must be less than MAX_SAFE_VOLUME. The amount of `olKey.inbound_tkn` the maker wants. This is used along with `gives` to derive a tick (price). which is the lowest allowed tick in the offer list such that `log_1.0001(takerGives/takerWants) <= maxTick`.
///@param gives Must be less than MAX_SAFE_VOLUME. The amount of `olKey.outbound_tkn` the maker gives. This is used along with `wants` to derive a tick (price). which is the lowest allowed tick in the offer list such that `log_1.0001(takerGives/takerWants) <= maxTick`. Must be less than MAX_SAFE_VOLUME.
///@param gasreq The amount of gas required to execute the offer logic in the maker's `IMaker` implementation. This will limit the gas available, and the offer will fail if it spends more.
///@param gasprice The maximum gas price the maker is willing to pay a penalty for due to failing execution.
///@return offerId The id of the offer on Mangrove. Can be used to retract or update the offer (even to reuse a taken offer).
///@dev This function is just a wrapper for `newOfferByTick`, see that function for details.
function newOfferByVolume(OLKey memory olKey, uint wants, uint gives, uint gasreq, uint gasprice)
external
payable
returns (uint offerId);
///@notice Updates an existing offer on Mangrove, where the caller is the maker.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param tick Must be `>= MIN_TICK` and `<= MAX_TICK`. The `tick` induces a price which is `1.0001^tick`. The actual tick of the offer will be the smallest tick `offerTick > tick` that satisfies `offerTick % tickSpacing == 0`.
///@param gives The amount of `olKey.outbound_tkn` the maker gives. Must be less than MAX_SAFE_VOLUME.
///@param gasreq The amount of gas required to execute the offer logic in the maker's `IMaker` implementation.
///@param gasprice The maximum gas price the maker is willing to pay a penalty for due to failing execution.
///@param offerId The id of the offer on Mangrove.
///@dev See `newOfferByTick` for additional details.
function updateOfferByTick(OLKey memory olKey, Tick tick, uint gives, uint gasreq, uint gasprice, uint offerId)
external
payable;
///@notice Updates an existing, owned offer on Mangrove, where the caller is the maker.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param wants The amount of `olKey.inbound_tkn` the maker wants. This is used along with `gives` to derive a tick (price). which is the lowest allowed tick in the offer list such that `log_1.0001(takerGives/takerWants) <= maxTick`.
///@param gives The amount of `olKey.outbound_tkn` the maker gives. This is used along with `wants` to derive a tick (price). which is the lowest allowed tick in the offer list such that `log_1.0001(takerGives/takerWants) <= maxTick`. Must be less than MAX_SAFE_VOLUME.
///@param gasreq The amount of gas required to execute the offer logic in the maker's `IMaker` implementation.
///@param gasprice The maximum gas price the maker is willing to pay a penalty for due to failing execution.
///@param offerId The id of the offer on Mangrove.
///@dev This function is just a wrapper for `updateOfferByTick`, see that function for details.
function updateOfferByVolume(OLKey memory olKey, uint wants, uint gives, uint gasreq, uint gasprice, uint offerId)
external
payable;
///@notice Retracts an offer from Mangrove, where the caller is the maker.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param offerId The id of the offer on Mangrove.
///@param deprovision Whether to deprovision the offer, i.e, return the provision to the maker's balance on Mangrove.
///@return provision The amount of native token deprovisioned for the offer (in wei).
///@dev `withdraw` can be used to withdraw the funds after deprovisioning.
///@dev Leaving funds provisioned can be used to save gas if offer is later updated.
function retractOffer(OLKey memory olKey, uint offerId, bool deprovision) external returns (uint provision);
// # Global config view functions
///@notice Gets the global configuration for Mangrove.
///@return _global The global configuration for Mangrove.
function global() external view returns (Global _global);
// # Offer list view functions
///@notice Gets the local configuration for a specific offer list.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@return _local The local configuration for the offer list.
function local(OLKey memory olKey) external view returns (Local _local);
///@notice Gets the global configuration for Mangrove and local the configuration for a specific offer list.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@return _global The global configuration for Mangrove.
///@return _local The local configuration for the offer list.
function config(OLKey memory olKey) external view returns (Global _global, Local _local);
///@notice Determines whether the reentrancy lock is in effect for the offer list.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@return true If locked; otherwise, false.
///@dev The lock protects modifying or inspecting the offer list while an order is in progress.
function locked(OLKey memory olKey) external view returns (bool);
///@notice Gets the `offerId` of the best offer in the offer list.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@return offerId The `offerId` of the best offer on the offer list.
function best(OLKey memory olKey) external view returns (uint offerId);
///@notice Gets the offer list key with the given hash (if the offer list key has been activated at least once).
///@param olKeyHash The hash of the offer list key.
///@return olKey The olKey.
function olKeys(bytes32 olKeyHash) external view returns (OLKey memory olKey);
// # Offer view functions
///@notice Gets an offer in packed format.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param offerId The `offerId` of the offer on the offer list.
///@return offer The offer in packed format.
function offers(OLKey memory olKey, uint offerId) external view returns (Offer offer);
///@notice Gets an offer's details in packed format.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param offerId The `offerId` of the offer on the offer list.
///@return offerDetail The offer details in packed format.
function offerDetails(OLKey memory olKey, uint offerId) external view returns (OfferDetail offerDetail);
///@notice Gets both an offer and its details in packed format.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param offerId The `offerId` of the offer on the offer list.
///@return offer The offer in packed format.
///@return offerDetail The offer details in packed format.
function offerData(OLKey memory olKey, uint offerId) external view returns (Offer offer, OfferDetail offerDetail);
// # Governance functions
///@notice Gets the governance address.
///@return The governance address.
function governance() external view returns (address);
///@notice Activates an offer list.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param fee In basis points, of `olKey.outbound_tkn` given to the taker. This fee is sent to Mangrove. Fee is capped to ~2.5%.
///@param density96X32 The density of the offer list used to define a minimum offer volume. See `setDensity96X32`.
///@param offer_gasbase The gasbase of the offer list used to define a minimum provision necessary for offers. See `setGasbase`.
///@dev If the flipped offer list is active then the offer lists are expected to have the same `tickSpacing`.
function activate(OLKey memory olKey, uint fee, uint density96X32, uint offer_gasbase) external;
///@notice Deactivates an offer list.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
function deactivate(OLKey memory olKey) external;
///@notice Kills the Mangrove instance. A dead instance cannot have offers executed or funds received, but offers can be retracted and funds can be withdrawn.
function kill() external;
///@notice Sets the density.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param density96X32 Is given as a 96.32 fixed point number. It will be stored as a 9-bit float and be approximated towards 0. The maximum error is 20%. See `DensityLib` for more information.
///@dev Useless if `global.useOracle != 0` and oracle returns a valid density.
function setDensity96X32(OLKey memory olKey, uint density96X32) external;
///@notice Sets the fee for the offer list.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param fee In basis points, of `olKey.outbound_tkn` given to the taker. This fee is sent to Mangrove. Fee is capped to ~2.5%.
function setFee(OLKey memory olKey, uint fee) external;
///@notice Sets the gasbase for the offer list.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param offer_gasbase The gasbase of the offer list used to define a minimum provision necessary for offers. Represents the gas overhead used by processing the offer inside Mangrove + the overhead of initiating an entire order. Stored in thousands in a maximum of 9 bits.
function setGasbase(OLKey memory olKey, uint offer_gasbase) external;
///@notice Sets the gasmax for Mangrove, the maximum amount of gas an offer can require to execute.
///@param gasmax The maximum amount of gas required to execute an offer. Must fit in 24 bits.
function setGasmax(uint gasmax) external;
///@notice Sets the maximum number of times a market order can recursively execute offers. This is a protection against stack overflows.
///@param maxRecursionDepth The maximum number of times a market order can recursively execute offers.
function setMaxRecursionDepth(uint maxRecursionDepth) external;
///@notice Sets the maximum cumulative `gasreq` for failing offers during a market order before doing a partial fill.
///@param maxGasreqForFailingOffers The maximum cumulative `gasreq` for failing offers during a market order before doing a partial fill. 32 bits.
function setMaxGasreqForFailingOffers(uint maxGasreqForFailingOffers) external;
///@notice Sets the gasprice (in Mwei, 26 bits).
///@param gasprice The gasprice (in Mwei, 26 bits).
function setGasprice(uint gasprice) external;
///@notice Sets a new governance address.
///@param governanceAddress The new governance address.
function setGovernance(address governanceAddress) external;
///@notice Sets the monitor/oracle. The `monitor/oracle` can provide real-time values for `gasprice` and `density` to Mangrove. It can also receive liquidity event notifications.
///@param monitor The new monitor/oracle address.
function setMonitor(address monitor) external;
///@notice Sets whether Mangrove notifies the Monitor when and offer is taken.
///@param notify Whether Mangrove notifies the Monitor when and offer is taken.
function setNotify(bool notify) external;
///@notice Sets whether Mangrove uses the monitor as oracle for `gasprice` and `density` values.
///@param useOracle Whether Mangrove uses the monitor as oracle for `gasprice` and `density` values.
function setUseOracle(bool useOracle) external;
///@notice Transfer ERC20 tokens to governance.
///@param tokenAddress The address of the ERC20 token.
///@param value The amount of tokens to transfer.
function withdrawERC20(address tokenAddress, uint value) external;
// # Tick tree view functions
///@notice Gets a leaf.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param index The index.
///@return The leaf.
function leafs(OLKey memory olKey, int index) external view returns (Leaf);
///@notice Gets a level 3 field.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param index The index.
///@return The field.
function level3s(OLKey memory olKey, int index) external view returns (Field);
///@notice Gets a level 2 field.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param index The index.
///@return The field.
function level2s(OLKey memory olKey, int index) external view returns (Field);
///@notice Gets a level 1 field.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param index The index.
///@return Yhe field.
function level1s(OLKey memory olKey, int index) external view returns (Field);
///@notice Gets the root from local.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@return The root.
function root(OLKey memory olKey) external view returns (Field);
// # Internal functions
///@notice Internal function used to flashloan tokens from taker to maker, for maker to source the promised liquidity.
///@param sor Data about an order-offer match.
///@param taker The taker.
///@return gasused The amount of gas used for `makerExecute`.
///@return makerData The data returned by `makerExecute`.
///@dev Not to be called externally - only external to be able to revert.
function flashloan(MgvLib.SingleOrder memory sor, address taker) external returns (uint gasused, bytes32 makerData);
///@notice Internal function used to clean failing offers.
///@param olKey The offer list key given by (maker) `outbound_tkn`, (maker) `inbound_tkn`, and `tickSpacing`.
///@param offerId The id of the offer on Mangrove.
///@param tick Must be `>= MIN_TICK` and `<= MAX_TICK`. The `tick` induces a price which is `1.0001^tick`. The actual tick of the offer will be the smallest tick `offerTick > tick` that satisfies `offerTick % tickSpacing == 0`.
///@param gasreq The gas required for the offer.
///@param takerWants Must be `<= MAX_SAFE_VOLUME`. The amount of `olKey.outbound_tkn` the taker wants.
///@param taker The taker used for transfers (should be able to deliver token amounts).
///@return bounty The bounty paid.
///@dev Not to be called externally - only external to be able to revert.
function internalCleanByImpersonation(
OLKey memory olKey,
uint offerId,
Tick tick,
uint gasreq,
uint takerWants,
address taker
) external returns (uint bounty);
///@notice Fall back function (forwards calls to `MgvAppendix`).
///@param callData The call data.
///@return The result.
fallback(bytes calldata callData) external returns (bytes memory);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
/* ************************************************** *
GENERATED FILE. DO NOT EDIT.
* ************************************************** */
/* since you can't convert bool to uint in an expression without conditionals,
* we add a file-level function and rely on compiler optimization
*/
function uint_of_bool(bool b) pure returns (uint u) {
assembly ("memory-safe") { u := b }
}
import "@mgv/lib/core/Constants.sol";
struct LocalUnpacked {
bool active;
uint fee;
Density density;
uint binPosInLeaf;
Field level3;
Field level2;
Field level1;
Field root;
uint kilo_offer_gasbase;
bool lock;
uint last;
}
//some type safety for each struct
type Local is uint;
using LocalLib for Local global;
////////////// ADDITIONAL DEFINITIONS, IF ANY ////////////////
import {Density, DensityLib} from "@mgv/lib/core/DensityLib.sol";
import {Bin,TickTreeLib,Field} from "@mgv/lib/core/TickTreeLib.sol";
/* Globally enable global.method(...) */
import {LocalExtra,LocalUnpackedExtra} from "@mgv/lib/core/LocalExtra.sol";
using LocalExtra for Local global;
using LocalUnpackedExtra for LocalUnpacked global;
////////////// END OF ADDITIONAL DEFINITIONS /////////////////
library LocalLib {
// number of bits in each field
uint constant active_bits = 1;
uint constant fee_bits = 8;
uint constant density_bits = 9;
uint constant binPosInLeaf_bits = 2;
uint constant level3_bits = 64;
uint constant level2_bits = 64;
uint constant level1_bits = 64;
uint constant root_bits = 2;
uint constant kilo_offer_gasbase_bits = 9;
uint constant lock_bits = 1;
uint constant last_bits = 32;
// number of bits before each field
uint constant active_before = 0 + 0;
uint constant fee_before = active_before + active_bits;
uint constant density_before = fee_before + fee_bits;
uint constant binPosInLeaf_before = density_before + density_bits;
uint constant level3_before = binPosInLeaf_before + binPosInLeaf_bits;
uint constant level2_before = level3_before + level3_bits;
uint constant level1_before = level2_before + level2_bits;
uint constant root_before = level1_before + level1_bits;
uint constant kilo_offer_gasbase_before = root_before + root_bits;
uint constant lock_before = kilo_offer_gasbase_before + kilo_offer_gasbase_bits;
uint constant last_before = lock_before + lock_bits;
// focus-mask: 1s at field location, 0s elsewhere
uint constant active_mask_inv = (ONES << 256 - active_bits) >> active_before;
uint constant fee_mask_inv = (ONES << 256 - fee_bits) >> fee_before;
uint constant density_mask_inv = (ONES << 256 - density_bits) >> density_before;
uint constant binPosInLeaf_mask_inv = (ONES << 256 - binPosInLeaf_bits) >> binPosInLeaf_before;
uint constant level3_mask_inv = (ONES << 256 - level3_bits) >> level3_before;
uint constant level2_mask_inv = (ONES << 256 - level2_bits) >> level2_before;
uint constant level1_mask_inv = (ONES << 256 - level1_bits) >> level1_before;
uint constant root_mask_inv = (ONES << 256 - root_bits) >> root_before;
uint constant kilo_offer_gasbase_mask_inv = (ONES << 256 - kilo_offer_gasbase_bits) >> kilo_offer_gasbase_before;
uint constant lock_mask_inv = (ONES << 256 - lock_bits) >> lock_before;
uint constant last_mask_inv = (ONES << 256 - last_bits) >> last_before;
// cleanup-mask: 0s at field location, 1s elsewhere
uint constant active_mask = ~active_mask_inv;
uint constant fee_mask = ~fee_mask_inv;
uint constant density_mask = ~density_mask_inv;
uint constant binPosInLeaf_mask = ~binPosInLeaf_mask_inv;
uint constant level3_mask = ~level3_mask_inv;
uint constant level2_mask = ~level2_mask_inv;
uint constant level1_mask = ~level1_mask_inv;
uint constant root_mask = ~root_mask_inv;
uint constant kilo_offer_gasbase_mask = ~kilo_offer_gasbase_mask_inv;
uint constant lock_mask = ~lock_mask_inv;
uint constant last_mask = ~last_mask_inv;
// cast-mask: 0s followed by |field| trailing 1s
uint constant active_cast_mask = ~(ONES << active_bits);
uint constant fee_cast_mask = ~(ONES << fee_bits);
uint constant density_cast_mask = ~(ONES << density_bits);
uint constant binPosInLeaf_cast_mask = ~(ONES << binPosInLeaf_bits);
uint constant level3_cast_mask = ~(ONES << level3_bits);
uint constant level2_cast_mask = ~(ONES << level2_bits);
uint constant level1_cast_mask = ~(ONES << level1_bits);
uint constant root_cast_mask = ~(ONES << root_bits);
uint constant kilo_offer_gasbase_cast_mask = ~(ONES << kilo_offer_gasbase_bits);
uint constant lock_cast_mask = ~(ONES << lock_bits);
uint constant last_cast_mask = ~(ONES << last_bits);
// size-related error message
string constant active_size_error = "mgv/config/active/1bits";
string constant fee_size_error = "mgv/config/fee/8bits";
string constant density_size_error = "mgv/config/density/9bits";
string constant binPosInLeaf_size_error = "mgv/config/binPosInLeaf/2bits";
string constant level3_size_error = "mgv/config/level3/64bits";
string constant level2_size_error = "mgv/config/level2/64bits";
string constant level1_size_error = "mgv/config/level1/64bits";
string constant root_size_error = "mgv/config/root/2bits";
string constant kilo_offer_gasbase_size_error = "mgv/config/kilo_offer_gasbase/9bits";
string constant lock_size_error = "mgv/config/lock/1bits";
string constant last_size_error = "mgv/config/last/32bits";
// from packed to in-memory struct
function to_struct(Local __packed) internal pure returns (LocalUnpacked memory __s) { unchecked {
__s.active = ((Local.unwrap(__packed) & active_mask_inv) > 0);
__s.fee = uint(Local.unwrap(__packed) << fee_before) >> (256 - fee_bits);
__s.density = Density.wrap(uint(Local.unwrap(__packed) << density_before) >> (256 - density_bits));
__s.binPosInLeaf = uint(Local.unwrap(__packed) << binPosInLeaf_before) >> (256 - binPosInLeaf_bits);
__s.level3 = Field.wrap(uint(Local.unwrap(__packed) << level3_before) >> (256 - level3_bits));
__s.level2 = Field.wrap(uint(Local.unwrap(__packed) << level2_before) >> (256 - level2_bits));
__s.level1 = Field.wrap(uint(Local.unwrap(__packed) << level1_before) >> (256 - level1_bits));
__s.root = Field.wrap(uint(Local.unwrap(__packed) << root_before) >> (256 - root_bits));
__s.kilo_offer_gasbase = uint(Local.unwrap(__packed) << kilo_offer_gasbase_before) >> (256 - kilo_offer_gasbase_bits);
__s.lock = ((Local.unwrap(__packed) & lock_mask_inv) > 0);
__s.last = uint(Local.unwrap(__packed) << last_before) >> (256 - last_bits);
}}
// equality checking
function eq(Local __packed1, Local __packed2) internal pure returns (bool) { unchecked {
return Local.unwrap(__packed1) == Local.unwrap(__packed2);
}}
// from packed to a tuple
function unpack(Local __packed) internal pure returns (bool __active, uint __fee, Density __density, uint __binPosInLeaf, Field __level3, Field __level2, Field __level1, Field __root, uint __kilo_offer_gasbase, bool __lock, uint __last) { unchecked {
__active = ((Local.unwrap(__packed) & active_mask_inv) > 0);
__fee = uint(Local.unwrap(__packed) << fee_before) >> (256 - fee_bits);
__density = Density.wrap(uint(Local.unwrap(__packed) << density_before) >> (256 - density_bits));
__binPosInLeaf = uint(Local.unwrap(__packed) << binPosInLeaf_before) >> (256 - binPosInLeaf_bits);
__level3 = Field.wrap(uint(Local.unwrap(__packed) << level3_before) >> (256 - level3_bits));
__level2 = Field.wrap(uint(Local.unwrap(__packed) << level2_before) >> (256 - level2_bits));
__level1 = Field.wrap(uint(Local.unwrap(__packed) << level1_before) >> (256 - level1_bits));
__root = Field.wrap(uint(Local.unwrap(__packed) << root_before) >> (256 - root_bits));
__kilo_offer_gasbase = uint(Local.unwrap(__packed) << kilo_offer_gasbase_before) >> (256 - kilo_offer_gasbase_bits);
__lock = ((Local.unwrap(__packed) & lock_mask_inv) > 0);
__last = uint(Local.unwrap(__packed) << last_before) >> (256 - last_bits);
}}
// getters
function active(Local __packed) internal pure returns(bool) { unchecked {
return ((Local.unwrap(__packed) & active_mask_inv) > 0);
}}
// setters
function active(Local __packed,bool val) internal pure returns(Local) { unchecked {
return Local.wrap((Local.unwrap(__packed) & active_mask) | (uint_of_bool(val) << (256 - active_bits)) >> active_before);
}}
function fee(Local __packed) internal pure returns(uint) { unchecked {
return uint(Local.unwrap(__packed) << fee_before) >> (256 - fee_bits);
}}
// setters
function fee(Local __packed,uint val) internal pure returns(Local) { unchecked {
return Local.wrap((Local.unwrap(__packed) & fee_mask) | (val << (256 - fee_bits)) >> fee_before);
}}
function density(Local __packed) internal pure returns(Density) { unchecked {
return Density.wrap(uint(Local.unwrap(__packed) << density_before) >> (256 - density_bits));
}}
// setters
function density(Local __packed,Density val) internal pure returns(Local) { unchecked {
return Local.wrap((Local.unwrap(__packed) & density_mask) | (Density.unwrap(val) << (256 - density_bits)) >> density_before);
}}
function binPosInLeaf(Local __packed) internal pure returns(uint) { unchecked {
return uint(Local.unwrap(__packed) << binPosInLeaf_before) >> (256 - binPosInLeaf_bits);
}}
// setters
function binPosInLeaf(Local __packed,uint val) internal pure returns(Local) { unchecked {
return Local.wrap((Local.unwrap(__packed) & binPosInLeaf_mask) | (val << (256 - binPosInLeaf_bits)) >> binPosInLeaf_before);
}}
function level3(Local __packed) internal pure returns(Field) { unchecked {
return Field.wrap(uint(Local.unwrap(__packed) << level3_before) >> (256 - level3_bits));
}}
// setters
function level3(Local __packed,Field val) internal pure returns(Local) { unchecked {
return Local.wrap((Local.unwrap(__packed) & level3_mask) | (Field.unwrap(val) << (256 - level3_bits)) >> level3_before);
}}
function level2(Local __packed) internal pure returns(Field) { unchecked {
return Field.wrap(uint(Local.unwrap(__packed) << level2_before) >> (256 - level2_bits));
}}
// setters
function level2(Local __packed,Field val) internal pure returns(Local) { unchecked {
return Local.wrap((Local.unwrap(__packed) & level2_mask) | (Field.unwrap(val) << (256 - level2_bits)) >> level2_before);
}}
function level1(Local __packed) internal pure returns(Field) { unchecked {
return Field.wrap(uint(Local.unwrap(__packed) << level1_before) >> (256 - level1_bits));
}}
// setters
function level1(Local __packed,Field val) internal pure returns(Local) { unchecked {
return Local.wrap((Local.unwrap(__packed) & level1_mask) | (Field.unwrap(val) << (256 - level1_bits)) >> level1_before);
}}
function root(Local __packed) internal pure returns(Field) { unchecked {
return Field.wrap(uint(Local.unwrap(__packed) << root_before) >> (256 - root_bits));
}}
// setters
function root(Local __packed,Field val) internal pure returns(Local) { unchecked {
return Local.wrap((Local.unwrap(__packed) & root_mask) | (Field.unwrap(val) << (256 - root_bits)) >> root_before);
}}
function kilo_offer_gasbase(Local __packed) internal pure returns(uint) { unchecked {
return uint(Local.unwrap(__packed) << kilo_offer_gasbase_before) >> (256 - kilo_offer_gasbase_bits);
}}
// setters
function kilo_offer_gasbase(Local __packed,uint val) internal pure returns(Local) { unchecked {
return Local.wrap((Local.unwrap(__packed) & kilo_offer_gasbase_mask) | (val << (256 - kilo_offer_gasbase_bits)) >> kilo_offer_gasbase_before);
}}
function lock(Local __packed) internal pure returns(bool) { unchecked {
return ((Local.unwrap(__packed) & lock_mask_inv) > 0);
}}
// setters
function lock(Local __packed,bool val) internal pure returns(Local) { unchecked {
return Local.wrap((Local.unwrap(__packed) & lock_mask) | (uint_of_bool(val) << (256 - lock_bits)) >> lock_before);
}}
function last(Local __packed) internal pure returns(uint) { unchecked {
return uint(Local.unwrap(__packed) << last_before) >> (256 - last_bits);
}}
// setters
function last(Local __packed,uint val) internal pure returns(Local) { unchecked {
return Local.wrap((Local.unwrap(__packed) & last_mask) | (val << (256 - last_bits)) >> last_before);
}}
// from in-memory struct to packed
function t_of_struct(LocalUnpacked memory __s) internal pure returns (Local) { unchecked {
return pack(__s.active, __s.fee, __s.density, __s.binPosInLeaf, __s.level3, __s.level2, __s.level1, __s.root, __s.kilo_offer_gasbase, __s.lock, __s.last);
}}
// from arguments to packed
function pack(bool __active, uint __fee, Density __density, uint __binPosInLeaf, Field __level3, Field __level2, Field __level1, Field __root, uint __kilo_offer_gasbase, bool __lock, uint __last) internal pure returns (Local) { unchecked {
uint __packed;
__packed |= (uint_of_bool(__active) << (256 - active_bits)) >> active_before;
__packed |= (__fee << (256 - fee_bits)) >> fee_before;
__packed |= (Density.unwrap(__density) << (256 - density_bits)) >> density_before;
__packed |= (__binPosInLeaf << (256 - binPosInLeaf_bits)) >> binPosInLeaf_before;
__packed |= (Field.unwrap(__level3) << (256 - level3_bits)) >> level3_before;
__packed |= (Field.unwrap(__level2) << (256 - level2_bits)) >> level2_before;
__packed |= (Field.unwrap(__level1) << (256 - level1_bits)) >> level1_before;
__packed |= (Field.unwrap(__root) << (256 - root_bits)) >> root_before;
__packed |= (__kilo_offer_gasbase << (256 - kilo_offer_gasbase_bits)) >> kilo_offer_gasbase_before;
__packed |= (uint_of_bool(__lock) << (256 - lock_bits)) >> lock_before;
__packed |= (__last << (256 - last_bits)) >> last_before;
return Local.wrap(__packed);
}}
// input checking
function active_check(bool __active) internal pure returns (bool) { unchecked {
return (uint_of_bool(__active) & active_cast_mask) == uint_of_bool(__active);
}}
function fee_check(uint __fee) internal pure returns (bool) { unchecked {
return (__fee & fee_cast_mask) == __fee;
}}
function density_check(Density __density) internal pure returns (bool) { unchecked {
return (Density.unwrap(__density) & density_cast_mask) == Density.unwrap(__density);
}}
function binPosInLeaf_check(uint __binPosInLeaf) internal pure returns (bool) { unchecked {
return (__binPosInLeaf & binPosInLeaf_cast_mask) == __binPosInLeaf;
}}
function level3_check(Field __level3) internal pure returns (bool) { unchecked {
return (Field.unwrap(__level3) & level3_cast_mask) == Field.unwrap(__level3);
}}
function level2_check(Field __level2) internal pure returns (bool) { unchecked {
return (Field.unwrap(__level2) & level2_cast_mask) == Field.unwrap(__level2);
}}
function level1_check(Field __level1) internal pure returns (bool) { unchecked {
return (Field.unwrap(__level1) & level1_cast_mask) == Field.unwrap(__level1);
}}
function root_check(Field __root) internal pure returns (bool) { unchecked {
return (Field.unwrap(__root) & root_cast_mask) == Field.unwrap(__root);
}}
function kilo_offer_gasbase_check(uint __kilo_offer_gasbase) internal pure returns (bool) { unchecked {
return (__kilo_offer_gasbase & kilo_offer_gasbase_cast_mask) == __kilo_offer_gasbase;
}}
function lock_check(bool __lock) internal pure returns (bool) { unchecked {
return (uint_of_bool(__lock) & lock_cast_mask) == uint_of_bool(__lock);
}}
function last_check(uint __last) internal pure returns (bool) { unchecked {
return (__last & last_cast_mask) == __last;
}}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import {Bin,TickTreeLib,Field} from "@mgv/lib/core/TickTreeLib.sol";
import {Density, DensityLib} from "@mgv/lib/core/DensityLib.sol";
import {Local,LocalUnpacked,LocalLib} from "@mgv/src/preprocessed/Local.post.sol";
// cleanup-mask: 0s at location of fields to hide from maker, 1s elsewhere
uint constant HIDE_FIELDS_FROM_MAKER_MASK = ~(LocalLib.binPosInLeaf_mask_inv | LocalLib.level3_mask_inv | LocalLib.level2_mask_inv | LocalLib.level1_mask_inv | LocalLib.root_mask_inv | LocalLib.last_mask_inv);
/* Extra functions for local */
library LocalExtra {
/* Sets the density in fixed-point 96X32 format (it is stored in floating-point, see `DensityLib` for more information). */
function densityFrom96X32(Local local, uint density96X32) internal pure returns (Local) { unchecked {
return local.density(DensityLib.from96X32(density96X32));
}}
/* Returns the gasbase in gas (it is stored in kilogas) */
function offer_gasbase(Local local) internal pure returns (uint) { unchecked {
return local.kilo_offer_gasbase() * 1e3;
}}
/* Sets the gasbase in gas (it is stored in kilogas) */
function offer_gasbase(Local local,uint val) internal pure returns (Local) { unchecked {
return local.kilo_offer_gasbase(val/1e3);
}}
/* Returns the bin that contains the best offer in \`local\`'s offer list */
function bestBin(Local local) internal pure returns (Bin) { unchecked {
return TickTreeLib.bestBinFromLocal(local);
}}
/* Erases field that give information about the current structure of the offer list. */
function clearFieldsForMaker(Local local) internal pure returns (Local) { unchecked {
return Local.wrap(
Local.unwrap(local)
& HIDE_FIELDS_FROM_MAKER_MASK);
}}
}
/* Extra functions for the struct version of local */
library LocalUnpackedExtra {
/* Sets the density in fixed-point 96X32 format (it is stored in floating-point, see `DensityLib` for more information). */
function densityFrom96X32(LocalUnpacked memory local, uint density96X32) internal pure { unchecked {
local.density = DensityLib.from96X32(density96X32);
}}
/* Returns the gasbase in gas (it is stored in kilogas) */
function offer_gasbase(LocalUnpacked memory local) internal pure returns (uint) { unchecked {
return local.kilo_offer_gasbase * 1e3;
}}
/* Sets the gasbase in gas (it is stored in kilogas) */
function offer_gasbase(LocalUnpacked memory local,uint val) internal pure { unchecked {
local.kilo_offer_gasbase = val/1e3;
}}
/* Returns the bin that contains the best offer in \`local\`'s offer list */
function bestBin(LocalUnpacked memory local) internal pure returns (Bin) { unchecked {
return TickTreeLib.bestBinFromBranch(local.binPosInLeaf,local.level3,local.level2,local.level1,local.root);
}}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IMangrove} from "@mgv/src/IMangrove.sol";
import {MgvReader} from "@mgv/src/periphery/MgvReader.sol";
import {OLKey, Offer, OfferDetail, Tick, MgvLib} from "@mgv/src/core/MgvLib.sol";
import {IERC20} from "@mgv/lib/IERC20.sol";
contract MangroveCleanerV2 {
IMangrove public immutable mangrove;
MgvReader public immutable mgvReader;
struct State {
address taker;
uint96 maxOfferPerCall;
}
State public state;
constructor(
IMangrove _mangrove,
MgvReader _mgvReader,
address _taker,
uint96 _maxOfferPerCall
) {
mgvReader = _mgvReader;
mangrove = _mangrove;
state.taker = _taker;
state.maxOfferPerCall = _maxOfferPerCall;
}
function _getCleanTarget(uint256 offerId, uint256 balanceGives, Offer offer, OfferDetail detail)
internal
pure
returns (MgvLib.CleanTarget memory target)
{
target.offerId = offerId;
target.tick = offer.tick();
target.gasreq = detail.gasreq();
target.takerWants = target.tick.outboundFromInbound(balanceGives);
}
struct CleanHeap {
uint256[] offerIds;
Offer[] offers;
OfferDetail[] details;
MgvLib.CleanTarget[] targets;
uint256[] bounties;
uint256 length;
MgvLib.CleanTarget[] target;
uint256 currentInsert;
}
function _generateHeap(uint256 size) internal pure returns (CleanHeap memory heap) {
heap.targets = new MgvLib.CleanTarget[](size);
}
function cleanByMarket(OLKey memory olKey, bool _withdraw) public returns (MgvLib.CleanTarget[] memory successes) {
uint256 maxOfferPerCall = state.maxOfferPerCall;
address taker = state.taker;
CleanHeap memory heap = _generateHeap(state.maxOfferPerCall);
(, heap.offerIds, heap.offers, heap.details) = mgvReader.packedOfferList(olKey, 0, maxOfferPerCall);
heap.targets = new MgvLib.CleanTarget[](heap.offerIds.length);
heap.bounties = new uint256[](heap.offerIds.length);
uint256 inboundBalance = IERC20(olKey.inbound_tkn).balanceOf(taker);
for (uint256 i = 0; i < heap.offerIds.length; i++) {
heap.targets[i] = _getCleanTarget(heap.offerIds[i], inboundBalance, heap.offers[i], heap.details[i]);
MgvLib.CleanTarget[] memory target = new MgvLib.CleanTarget[](1);
target[0] = heap.targets[i];
(, heap.bounties[i]) = mangrove.cleanByImpersonation(olKey, target, taker);
if (heap.bounties[i] > 0) {
heap.length++;
}
}
successes = new MgvLib.CleanTarget[](heap.length);
for (uint256 i = 0; i < heap.bounties.length; i++) {
if (heap.bounties[i] > 0) {
successes[heap.currentInsert] = heap.targets[i];
heap.currentInsert++;
}
}
if (_withdraw) {
withdraw();
}
}
function cleanMarkets(OLKey[] memory olKeys, bool _withdraw) public returns (MgvLib.CleanTarget[][] memory successes) {
successes = new MgvLib.CleanTarget[][](olKeys.length);
for (uint256 i = 0; i < olKeys.length; i++) {
successes[i] = cleanByMarket(olKeys[i], false);
}
if (_withdraw) {
withdraw();
}
}
function efficientClean(OLKey[] calldata olKeys, MgvLib.CleanTarget[][] calldata targets, bool _withdraw)
public
returns (uint256 successes, uint256 totalBounty)
{
address taker = state.taker;
for (uint256 i = 0; i < olKeys.length; i++) {
(uint256 _s, uint256 _b) = mangrove.cleanByImpersonation(olKeys[i], targets[i], taker);
successes += _s;
totalBounty += _b;
}
if (_withdraw) {
withdraw();
}
}
function setTaker(address _taker) public {
require(msg.sender == state.taker, "MangroveCleaner: FORBIDDEN");
state.taker = _taker;
}
function setMaxOfferPerCall(uint96 _maxOfferPerCall) public {
require(msg.sender == state.taker, "MangroveCleaner: FORBIDDEN");
state.maxOfferPerCall = _maxOfferPerCall;
}
function setState(State calldata _state) public {
require(msg.sender == state.taker, "MangroveCleaner: FORBIDDEN");
state = _state;
}
function withdraw() public {
uint256 balance = address(this).balance;
if (balance > 0) {
payable(state.taker).transfer(balance);
}
}
receive() external payable {}
struct EfficientMarketClean {
bytes32 olKeyHash;
uint256[] offerIds;
}
function efficientCalldataSingleClean(EfficientMarketClean memory market, bool _withdraw)
public
returns (uint256 successes, uint256 totalBounty)
{
OLKey memory olKey = mangrove.olKeys(market.olKeyHash);
uint256 balance = IERC20(olKey.inbound_tkn).balanceOf(state.taker);
MgvLib.CleanTarget[] memory targets = new MgvLib.CleanTarget[](market.offerIds.length);
for (uint256 i = 0; i < market.offerIds.length; i++) {
(Offer offer, OfferDetail detail) = mangrove.offerData(olKey, market.offerIds[i]);
targets[i] = _getCleanTarget(market.offerIds[i], balance, offer, detail);
}
(successes, totalBounty) = mangrove.cleanByImpersonation(olKey, targets, state.taker);
if (_withdraw) {
withdraw();
}
}
function efficientCalldataClean(EfficientMarketClean[] memory markets)
public
returns (uint256 successes, uint256 totalBounty)
{
for (uint256 i = 0; i < markets.length; i++) {
(uint256 _s, uint256 _b) = efficientCalldataSingleClean(markets[i], false);
successes += _s;
totalBounty += _b;
}
withdraw();
}
}
// SPDX-License-Identifier: MIT
/* `MgvLib` contains data structures returned by external calls to Mangrove and the interfaces it uses for its own external calls. */
pragma solidity ^0.8.10;
import "@mgv/src/preprocessed/Structs.post.sol";
import {IERC20} from "@mgv/lib/IERC20.sol";
import {Density, DensityLib} from "@mgv/lib/core/DensityLib.sol";
import "@mgv/lib/core/TickTreeLib.sol";
import "@mgv/lib/core/TickLib.sol";
/* `OLKey` (for "OfferListKey") contains the information that characterizes an offer list:
* `outbound_tkn`, token that goes from the maker to the taker (to remember the direction, imagine the token as going out of the Mangrove offer, towards the taker)
* `inbound_tkn`, token that goes from the taker to the maker (to remember the direction, imagine the token as going into Mangrove from the taker)
* `tickSpacing`, how many ticks should be jumped between available price points. More volatile outbound/inbound pairs should have a larger `tickSpacing`. */
struct OLKey {
address outbound_tkn;
address inbound_tkn;
uint tickSpacing;
}
/* Globally enable `olKey.method(...)` */
using OLLib for OLKey global;
library OLLib {
/* The id of an `OLKey` should be `keccak256(abi.encode(olKey))`
To save gas, `id()` directly hashes the memory (which matches the ABI encoding; there is a fuzz test on that). If the memory layout changes, this function must be updated. */
function hash(OLKey memory olKey) internal pure returns (bytes32 _id) {
assembly ("memory-safe") {
_id := keccak256(olKey, 96)
}
}
/* Creates a flipped copy of the `olKey` with same `tickSpacing`. */
function flipped(OLKey memory olKey) internal pure returns (OLKey memory) {
return OLKey(olKey.inbound_tkn, olKey.outbound_tkn, olKey.tickSpacing);
}
/* Convert `tick` to bin according to `olKey.tickSpacing` */
function nearestBin(OLKey memory olKey, Tick _tick) internal pure returns (Bin) {
return _tick.nearestBin(olKey.tickSpacing);
}
/* Convert `bin` to `tick` according to `olKey.tickSpacing` */
function tick(OLKey memory olKey, Bin bin) internal pure returns (Tick) {
return bin.tick(olKey.tickSpacing);
}
}
/* # Structs
The structs defined in `structs.js` have their counterpart as solidity structs that are easy to manipulate for outside contracts / callers of view functions. */
library MgvLib {
/*
Some miscellaneous data types useful to `Mangrove` and external contracts */
//+clear+
/* `SingleOrder` holds data about an order-offer match in a struct. Used by `marketOrder` (and some of its nested functions) to avoid stack too deep errors. It is used in market order and cleaning. */
struct SingleOrder {
OLKey olKey;
uint offerId;
/* The `offer` given to the maker will be cleaned of `prev`/`next` pointers. */
Offer offer;
/* `takerWants`/`takerGives` mutate over execution. Initially the `wants`/`gives` from the taker's pov, then actual `wants`/`gives` adjusted by offer's price and volume. */
uint takerWants;
uint takerGives;
/* `offerDetail` is only populated when necessary. */
OfferDetail offerDetail;
Global global;
/* The `local` given to the maker will be cleaned of tick tree information. */
Local local;
}
/* <a id="MgvLib/OrderResult"></a> `OrderResult` holds additional data for the maker and is given to them _after_ they fulfilled an offer. It gives them their own returned data from the previous call and an `mgvData` specifying whether Mangrove encountered an error. */
struct OrderResult {
/* `makerData` holds a message that was either returned by the maker or passed as revert message at the end of the trade execution*/
bytes32 makerData;
/* `mgvData` is an [internal Mangrove status code](#MgvOfferTaking/statusCodes) code. */
bytes32 mgvData;
}
/* `CleanTarget` holds data about an offer that should be cleaned, i.e. made to fail by executing it with the specified volume. It is used in `MgvOfferTaking.cleanByImpersonation`. */
struct CleanTarget {
uint offerId;
Tick tick;
uint gasreq;
uint takerWants;
}
}
/* # Events
The events emitted are listed here: */
interface HasMgvEvents {
/*
Events in solidity is a hard thing to do in a optimal way. If you look at it as a purely gas efficient issue, you want to emit as few events as possible and with as few fields as possible. But as events also has to be usable for an off chain user, then doing this is not always the best solution.
We tried to list the main forces that drive which events and data to emit:
1. Use as little gas as possible.
2. An indexer should be able to keep track of the state of Mangrove.
3. Doing RPC calls directly, should be able to find offers and other information based on offer list, maker, taker, offer id, etc.
These forces are in conflict and it is impossible to find a solution that is optimal for all three.
The following events are therefore an attempt to balance the trade-offs.
*/
/* ### Mangrove Creation
Emitted at the creation of the new Mangrove contract */
event NewMgv();
/* ### Mangrove adds or removes wei from `maker`'s account */
/*
* Credit event occurs when an offer is removed from Mangrove or when the `fund` function is called
This is emitted when a user's account on Mangrove is credited with some native funds, to be used as provision for offers.
It emits the `maker`'s address and the `amount` credited
The `maker` address is indexed so that we can filter on it when doing RPC calls.
These are the scenarios where it can happen:
- Fund Mangrove directly
- Fund Mangrove when posting an offer
- When updating an offer
- Funding Mangrove or
- the updated offer needs less provision, than it already has. Meaning the user gets credited the difference.
- When retracting an offer and deprovisioning it. Meaning the user gets credited the provision that was locked by the offer.
- When an offer fails. The remaining provision gets credited back to the maker
A challenge for an indexer is to know how much provision each offer locks. With the current events, an indexer is going to have to know the liveness, gasreq and gasprice of the offer. If the offer is not live, then also know if it has been deprovisioned. And know what the gasbase of the offer list was when the offer was posted. With this information an indexer can calculate the exact provision locked by the offer.
The indexer also cannot deduce what scenario the credit event happened. E.g., we don't know if the credit event happened because an offer failed or because the user simply funded Mangrove.
*/
event Credit(address indexed maker, uint amount);
/*
* Debit event occurs when an offer is posted or when the `withdraw` function is called
This is emitted when a user's account on Mangrove is debited with some native funds.
It emits the `maker`'s address and the `amount` debited.
The `maker` address is indexed so that we can filter on it when doing RPC calls.
These are the scenarios where it can happen:
- Withdraw funds from Mangrove directly
- When posting an offer. The user gets debited the provision that the offer locks.
- When updating an offer and it requires more provision that it already has. Meaning the user gets debited the difference.
Same challenges as for Credit
*/
event Debit(address indexed maker, uint amount);
/* ### Mangrove reconfiguration */
/*
This event is emitted when an offer list is activated or deactivated. Meaning one half of a market is opened.
It emits the `olKeyHash` and the boolean `value`. By emitting this, an indexer will be able to keep track of what offer lists are active and what their hash is.
The `olKeyHash` and both token addresses are indexed, so that we can filter on it when doing RPC calls.
*/
event SetActive(
bytes32 indexed olKeyHash, address indexed outbound_tkn, address indexed inbound_tkn, uint tickSpacing, bool value
);
/*
This event is emitted when the fee of an offer list is changed.
It emits the `olKeyHash` and the `value`. By emitting this, an indexer will be able to keep track of what fee each offer list has.
The `olKeyHash` is indexed, so that we can filter on it when doing RPC calls.
*/
event SetFee(bytes32 indexed olKeyHash, uint value);
/*
This event is emitted when the gasbase of an offer list is changed.
It emits the `olKeyHash` and the `offer_gasbase`. By emitting this, an indexer will be able to keep track of what gasbase each offer list has.
The `olKeyHash` is indexed, so that we can filter on it when doing RPC calls.
*/
event SetGasbase(bytes32 indexed olKeyHash, uint offer_gasbase);
/*
This event is emitted when the governance of Mangrove is set.
It emits the `governance` address. By emitting this, an indexer will be able to keep track of what governance address Mangrove has.
No fields are indexed as there is no need for RPC calls to filter on this.
*/
event SetGovernance(address value);
/*
This event is emitted when the monitor address of Mangrove is set. Be aware that the address for Monitor is also the address for the oracle.
It emits the `monitor` / `oracle` address. By emitting this, an indexer will be able to keep track of what monitor/oracle address Mangrove use.
No fields are indexed as there is no need for RPC calls to filter on this.
*/
event SetMonitor(address value);
/*
This event is emitted when the configuration for whether to use an oracle or not is set.
It emits the `useOracle`, which is a boolean value, that controls whether or not Mangrove reads its `gasprice` and `density` from an oracle or uses its own local values. By emitting this, an indexer will be able to keep track of if Mangrove is using an oracle.
No fields are indexed as there is no need for RPC calls to filter on this.
*/
event SetUseOracle(bool value);
/*
This event is emitted when the configuration for notify on Mangrove is set.
It emits a boolean value, to tell whether or not notify is active. By emitting this, an indexer will be able to keep track of whether or not Mangrove notifies the Monitor/Oracle when and offer is taken, either successfully or not.
No fields are indexed as there is no need for RPC calls to filter on this.
*/
event SetNotify(bool value);
/*
This event is emitted when the gasmax of Mangrove is set.
It emits the `gasmax`. By emitting this, an indexer will be able to keep track of what gasmax Mangrove has. Read more about Mangroves gasmax on [docs.mangrove.exchange](https://docs.mangrove.exchange)
No fields are indexed as there is no need for RPC calls to filter on this.
*/
event SetGasmax(uint value);
/*
This event is emitted when the density of an offer list is changed.
It emits the `olKeyHash` and the `density`. By emitting this, an indexer will be able to keep track of what density each offer list has.
The `olKeyHash` is indexed, so that we can filter on it when doing RPC calls.
*/
event SetDensity96X32(bytes32 indexed olKeyHash, uint value);
/*
This event is emitted when the max recursion depth of Mangrove is set.
It emits the max depth `value`. By emitting this, an indexer will be able to keep track of what max recursion depth Mangrove has. Read more about Mangroves max recursion depth on [docs.mangrove.exchange](https://docs.mangrove.exchange)
*/
event SetMaxRecursionDepth(uint value);
/*
This event is emitted when the max gasreq for failing offers of Mangrove is set.
It emits the max gasreq for failing offers `value`. By emitting this, an indexer will be able to keep track of what max gasreq for failing offers Mangrove has. Read more about Mangroves max gasreq for failing offers on [docs.mangrove.exchange](https://docs.mangrove.exchange)
*/
event SetMaxGasreqForFailingOffers(uint value);
/*
This event is emitted when the gasprice of Mangrove is set.
It emits the `gasprice`. By emitting this, an indexer will be able to keep track of what gasprice Mangrove has. Read more about Mangroves gasprice on [docs.mangrove.exchange](https://docs.mangrove.exchange)
No fields are indexed as there is no need for RPC calls to filter on this.
*/
event SetGasprice(uint value);
/* ### Clean order execution */
/*
This event is emitted when a user tries to clean offers on Mangrove, using the build in clean functionality.
It emits the `olKeyHash`, the `taker` address and `offersToBeCleaned`, which is the number of offers that should be cleaned. By emitting this event, an indexer can save what `offer list` the user is trying to clean and what `taker` is being used.
This way it can keep a context for the following events being emitted (Just like `OrderStart`). The `offersToBeCleaned` is emitted so that an indexer can keep track of how many offers the user tried to clean.
Combining this with the amount of `OfferFail` events emitted, then an indexer can know how many offers the user actually managed to clean. This could be used for analytics.
The fields `olKeyHash` and `taker` are indexed, so that we can filter on them when doing RPC calls.
*/
event CleanStart(bytes32 indexed olKeyHash, address indexed taker, uint offersToBeCleaned);
/*
This event is emitted when a Clean operation is completed.
It does not emit any fields. This event is only needed in order to know that the clean operation is completed. This way an indexer can know exactly in what context we are in.
It could emit the total bounty received, but in order to know who got the bounty, an indexer would still be needed or we would have to emit the taker address as well, in order for an RPC call to find the data.
But an indexer would still be able to find this info, by collecting all the previous events. So we do not emit the bounty.
*/
event CleanComplete();
/* ### Market order execution */
/*
This event is emitted when a market order is started on Mangrove.
It emits the `olKeyHash`, the `taker`, the `maxTick`, `fillVolume` and `fillWants`.
The fields `olKeyHash` and `taker` are indexed, so that we can filter on them when doing RPC calls.
By emitting this an indexer can keep track of what context the current market order is in.
E.g. if a user starts a market order and one of the offers taken also starts a market order, then we can in an indexer have a stack of started market orders and thereby know exactly what offer list the order is running on and the taker.
By emitting `maxTick`, `fillVolume` and `fillWants`, we can now also know how much of the market order was filled and if it matches the price given. See OrderComplete for more.
*/
event OrderStart(bytes32 indexed olKeyHash, address indexed taker, Tick maxTick, uint fillVolume, bool fillWants);
/*
This event is emitted when a market order is finished.
It emits `olKeyHash`, the `taker` and the total `fee paid`. We need this event, in order to know that a market order is completed. This way an indexer can know exactly in what context we are in.
The total `fee paid` for that market order, is needed, as we do not emit this any other places. The fields `olKeyHash` and `taker` is not needed for an indexer, but they are emitted and indexed in order for RPC calls to filter and find the fee.
*/
event OrderComplete(bytes32 indexed olKeyHash, address indexed taker, uint fee);
/* ### Offer execution */
/*
This event is emitted when an offer is successfully taken. Meaning both maker and taker has gotten their funds.
It emits the `olKeyHash`, `taker` address, the `offerId` and the `takerWants` and `takerGives` that the offer was taken at.
Just as for `OfferFail`, the `olKeyHash` and `taker` are not needed for an indexer to work, but are needed for RPC calls.
`olKeyHash` `taker` and `id` are indexed so that we can filter on them when doing RPC calls. As `maker` can be a strategy and not the actual owner, then we chose not to emit it here and to mark the field `id` indexed,
the strat should emit the relation between maker and offerId. This way you can still by RPC calls find the relevant offer successes
So they are emitted and indexed for that reason. Just like `OfferFail` this event is emitted during posthook end, so it is emitted in reverse order compared to what order they are taken.
This event could be emitted at offer execution, but for consistency we emit it at posthook end.
If the posthook of the offer fails. Then we emit `OfferSuccessWithPosthookData` instead of just `OfferSuccess`. This event has one extra field, which is the reason for the posthook failure.
By emitting the posthook data, an indexer can keep track of the reason posthook fails, this could for example be used for analytics.
By emitting `offerId`, `wants` and `gives`, an indexer can keep track of whether the offer was partially or fully taken, by looking at the what the offer was posted at.
*/
event OfferSuccess(
bytes32 indexed olKeyHash, address indexed taker, uint indexed id, uint takerWants, uint takerGives
);
event OfferSuccessWithPosthookData(
bytes32 indexed olKeyHash,
address indexed taker,
uint indexed id,
uint takerWants,
uint takerGives,
bytes32 posthookData
);
/*
This event is emitted when an offer fails, because of a maker error.
It emits `olKeyHash`, `taker`, the `offerId`, the offers `takerWants`, `takerGives`, `penalty` and the `reason` for failure.
`olKeyHash` and `taker` are all fields that we do not need, in order for an indexer to work, as an indexer will be able the get that info from the former `OrderStart` and `OfferWrite` events.
But in order for RPC call to filter on this, we need to emit them.
`olKeyHash` `taker` and `id` are indexed so that we can filter on them when doing RPC calls. As `maker` can be a strategy and not the actual owner, then we chose to not emit it here and to mark the field `id` indexed,
the strat should emit the relation between maker and offerId. This way you can still by RPC calls find the relevant offer successes
If the posthook of the offer fails. Then we emit `OfferFailWithPosthookData` instead of just `OfferFail`.
This event has one extra field, which is the reason for the posthook failure. By emitting the posthook data, an indexer can keep track of the reason posthook fails, this could for example be used for analytics.
This event is emitted during posthook end, we wait to emit this event to the end, because we need the information of `penalty`, which is only available at the end of the posthook.
This means that `OfferFail` events are emitted in reverse order, compared to what order they are taken. This is due to the way we handle posthooks. The same goes for `OfferSuccess`.
By emitting this event, an indexer can keep track of, if an offer failed and thereby if the offer is live.
By emitting the `takerWants` and `takerGives` that the offer was taken with, then an indexer can keep track of these amounts, which could be useful for e.g. strategy manager, to know if their offers fail at a certain amount.
*/
event OfferFail(
bytes32 indexed olKeyHash,
address indexed taker,
uint indexed id,
uint takerWants,
uint takerGives,
uint penalty,
// `mgvData` may only be `"mgv/makerRevert"`, `"mgv/makerTransferFail"` or `"mgv/makerReceiveFail"`
bytes32 mgvData
);
event OfferFailWithPosthookData(
bytes32 indexed olKeyHash,
address indexed taker,
uint indexed id,
uint takerWants,
uint takerGives,
uint penalty,
// `mgvData` may only be `"mgv/makerRevert"`, `"mgv/makerTransferFail"` or `"mgv/makerReceiveFail"`
bytes32 mgvData,
bytes32 posthookData
);
/*
### After `permit` and `approve`
This is emitted when a user permits another address to use a certain amount of its funds to do market orders, or when a user revokes another address to use a certain amount of its funds.
Approvals are based on the pair of outbound and inbound token. Be aware that it is not offer list bases, as an offer list also holds the tick spacing.
We emit `outbound` token, `inbound` token, `owner`, msg.sender (`spender`), `value`. Where `owner` is the one who owns the funds, `spender` is the one who is allowed to use the funds and `value` is the amount of funds that is allowed to be used.
Outbound, inbound and owner is indexed, this way one can filter on these fields when doing RPC calls. If we could somehow combine outbound and inbound into one field, then we could also index the spender.
*/
event Approval(
address indexed outbound_tkn, address indexed inbound_tkn, address indexed owner, address spender, uint value
);
/* ### Mangrove closure */
event Kill();
/* ### An offer was created or updated.
This event is emitted when an offer is posted on Mangrove.
It emits the `olKeyHash`, the `maker` address, the `tick`, the `gives`, the `gasprice`, `gasreq` and the offers `id`.
By emitting the `olKeyHash` and `id`, an indexer will be able to keep track of each offer, because offer list and id together create a unique id for the offer. By emitting the `maker` address, we are able to keep track of who has posted what offer. The `tick` and `gives`, enables an indexer to know exactly how much an offer is willing to give and at what price, this could for example be used to calculate a return. The `gasprice` and `gasreq`, enables an indexer to calculate how much provision is locked by the offer, see `Credit` for more information.
The fields `olKeyHash` and `maker` are indexed, so that we can filter on them when doing RPC calls.
*/
event OfferWrite(
bytes32 indexed olKeyHash, address indexed maker, int tick, uint gives, uint gasprice, uint gasreq, uint id
);
/*
This event is emitted when a user retracts his offer.
It emits the `olKeyHash`, the `maker` address, `offerId` and whether or not the user chose to `deprovision` the offer.
By emitting this event an indexer knows whether or not an offer is live. And whether or not an offer is deprovisioned. This is important because we need to know this, when we try to calculate how much an offer locks in provision. See the description of `Credit` for more info.
The `maker` is not needed for an indexer to work, but is needed for RPC calls, so it is emitted and indexed for that reason. The `olKeyHash` is only indexed because it is needed for RPC calls.
*/
event OfferRetract(bytes32 indexed olKeyHash, address indexed maker, uint id, bool deprovision);
}
/* # IMaker interface */
interface IMaker {
/* Called upon offer execution.
- If the call throws, Mangrove will not try to transfer funds and the first 32 bytes of revert reason are passed to `makerPosthook` as `makerData`
- If the call returns normally, `returnData` is passed to `makerPosthook` as `makerData` and Mangrove will attempt to transfer the funds.
*/
function makerExecute(MgvLib.SingleOrder calldata order) external returns (bytes32);
/* Called after all offers of an order have been executed. Posthook of the last executed order is called first and full reentrancy into Mangrove is enabled at this time. `order` recalls key arguments of the order that was processed and `result` recalls important information for updating the current offer. (see [above](#MgvLib/OrderResult))*/
function makerPosthook(MgvLib.SingleOrder calldata order, MgvLib.OrderResult calldata result) external;
}
/* # Monitor interface
If enabled, the monitor receives notification after each offer execution and is read for each offer list's `gasprice` and `density`. */
interface IMgvMonitor {
function notifySuccess(MgvLib.SingleOrder calldata sor, address taker) external;
function notifyFail(MgvLib.SingleOrder calldata sor, address taker) external;
function read(OLKey memory olKey) external view returns (uint gasprice, Density density);
}
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;
import "@mgv/src/core/MgvLib.sol";
import {IMangrove} from "@mgv/src/IMangrove.sol";
struct VolumeData {
uint totalGot;
uint totalGave;
uint totalGasreq;
}
struct Market {
address tkn0;
address tkn1;
uint tickSpacing;
}
/// @notice Config of a market. Assumes a context where `tkn0` and `tkn1` are defined. `config01` is the local config of the `tkn0/tkn1` offer list. `config10` is the config of the `tkn1/tkn0` offer list.
struct MarketConfig {
LocalUnpacked config01;
LocalUnpacked config10;
}
/// @notice We choose a canonical orientation for all markets based on the numerical values of their token addresses. That way we can uniquely identify a market with two addresses given in any order + a tickSpacing.
/// @return address the lowest of the given arguments (numerically)
/// @return address the highest of the given arguments (numerically)
function order(address tkn0, address tkn1) pure returns (address, address) {
return uint160(tkn0) < uint160(tkn1) ? (tkn0, tkn1) : (tkn1, tkn0);
}
// canonically order the tokens of a Market
// modifies in-place
function order(Market memory market) pure {
(market.tkn0, market.tkn1) = order(market.tkn0, market.tkn1);
}
// flip tkn0/tkn1 of a market. Useful before conversion to OLKey
// creates a copy
function flipped(Market memory market) pure returns (Market memory) {
return Market(market.tkn1, market.tkn0, market.tickSpacing);
}
// convert Market to OLKey
// creates a copy
function toOLKey(Market memory market) pure returns (OLKey memory) {
return OLKey(market.tkn0, market.tkn1, market.tickSpacing);
}
contract MgvReader {
struct MarketOrder {
OLKey olKey;
Tick maxTick;
uint initialFillVolume;
uint totalGot;
uint totalGave;
uint totalGasreq;
uint currentFillVolume;
uint currentWants;
uint currentGives;
bool fillWants;
uint offerId;
Offer offer;
OfferDetail offerDetail;
Local local;
VolumeData[] volumeData;
uint numOffers;
bool accumulate;
uint maxRecursionDepth;
}
/**
* @notice Open markets tracking (below) provides information about which markets on Mangrove are open. Anyone can update a market status by calling `updateMarket`.
* @notice The array of structs `_openMarkets` is the array of all currently open markets (up to a delay in calling `updateMarkets`). A market is a triplet of tokens `(tkn0,tkn1,tickSpacing)`. The which token is 0 which token is 1 is non-meaningful but canonical (see `order`).
* @notice In this contract, 'markets' are defined by non-oriented offer lists. Usually markets come with a base/quote orientation. Please keep that in mind.
* @notice A market {tkn0,tkn1} is open if either the tkn0/tkn1 offer list is active or the tkn1/tkn0 offer list is active.
*/
Market[] internal _openMarkets;
/// @notice Markets can be added or removed from `_openMarkets` array. To remove a market, we must remember its position in the array. The `marketPositions` mapping does that. The mapping goes outbound => inbound => tickSpacing => positions.
mapping(address => mapping(address => mapping(uint => uint))) internal marketPositions;
IMangrove public immutable MGV;
constructor(address mgv) {
MGV = IMangrove(payable(mgv));
}
// # Config view functions
/* Returns the configuration in an ABI-compatible struct. Should not be called internally, would be a huge memory copying waste. Use `config` instead. */
function configInfo(OLKey memory olKey)
external
view
returns (GlobalUnpacked memory _global, LocalUnpacked memory _local)
{
unchecked {
(Global __global, Local __local) = MGV.config(olKey);
_global = __global.to_struct();
_local = __local.to_struct();
}
}
function globalUnpacked() public view returns (GlobalUnpacked memory) {
return MGV.global().to_struct();
}
function localUnpacked(OLKey memory olKey) public view returns (LocalUnpacked memory) {
return MGV.local(olKey).to_struct();
}
// # Offer view functions
/* Returns information about an offer in ABI-compatible structs. Do not use internally, would be a huge memory-copying waste. Use `offer lists[outbound_tkn][inbound_tkn].offers` and `offer lists[outbound_tkn][inbound_tkn].offerDetails` instead. */
function offerInfo(OLKey memory olKey, uint offerId)
public
view
returns (OfferUnpacked memory offer, OfferDetailUnpacked memory offerDetail)
{
unchecked {
(Offer _offer, OfferDetail _offerDetail) = MGV.offerData(olKey, offerId);
offer = _offer.to_struct();
offerDetail = _offerDetail.to_struct();
}
}
// # Offer list view functions
/* Convenience function for checking whether a offer list is empty. There is no offer with id 0, so if the id of the offer list's best offer is 0, it means the offer list is empty. */
function isEmptyOB(OLKey memory olKey) external view returns (bool) {
return MGV.best(olKey) == 0;
}
/*
* Returns two uints.
*
* `startId` is the id of the best live offer with id equal or greater than
* `fromId`, 0 if there is no such offer.
*
* `length` is 0 if `startId == 0`. Other it is the number of live offers as good or worse than the offer with
* id `startId`.
*/
function offerListEndPoints(OLKey memory olKey, uint fromId, uint maxOffers)
public
view
returns (uint startId, uint length)
{
unchecked {
if (fromId == 0) {
startId = MGV.best(olKey);
} else {
startId = MGV.offers(olKey, fromId).isLive() ? fromId : 0;
}
uint currentId = startId;
while (currentId != 0 && length < maxOffers) {
currentId = nextOfferId(olKey, MGV.offers(olKey, currentId));
length = length + 1;
}
return (startId, length);
}
}
struct OfferListArgs {
OLKey olKey;
uint fromId;
uint maxOffers;
}
// Returns the orderbook for the outbound_tkn/inbound_tkn/tickSpacing offer list in packed form. First number is id of next offer (0 is we're done). First array is ids, second is offers (as bytes32), third is offerDetails (as bytes32). Array will be of size `min(# of offers in out/in list, maxOffers)`.
function packedOfferList(OLKey memory olKey, uint fromId, uint maxOffers)
public
view
returns (uint, uint[] memory, Offer[] memory, OfferDetail[] memory)
{
unchecked {
OfferListArgs memory olh = OfferListArgs(olKey, fromId, maxOffers);
(uint currentId, uint length) = offerListEndPoints(olh.olKey, olh.fromId, olh.maxOffers);
uint[] memory offerIds = new uint[](length);
Offer[] memory offers = new Offer[](length);
OfferDetail[] memory details = new OfferDetail[](length);
uint i = 0;
while (currentId != 0 && i < length) {
offerIds[i] = currentId;
(offers[i], details[i]) = MGV.offerData(olKey, currentId);
currentId = nextOfferId(olKey, offers[i]);
i = i + 1;
}
return (currentId, offerIds, offers, details);
}
}
// Returns the orderbook for the outbound_tkn/inbound_tkn/tickSpacing offer list in unpacked form. First number is id of next offer (0 if we're done). First array is ids, second is offers (as structs), third is offerDetails (as structs). Array will be of size `min(# of offers in out/in list, maxOffers)`.
function offerList(OLKey memory olKey, uint fromId, uint maxOffers)
public
view
returns (uint, uint[] memory, OfferUnpacked[] memory, OfferDetailUnpacked[] memory)
{
unchecked {
OfferListArgs memory olh = OfferListArgs(olKey, fromId, maxOffers);
(uint currentId, uint length) = offerListEndPoints(olh.olKey, olh.fromId, olh.maxOffers);
uint[] memory offerIds = new uint[](length);
OfferUnpacked[] memory offers = new OfferUnpacked[](length);
OfferDetailUnpacked[] memory details = new OfferDetailUnpacked[](length);
uint i = 0;
while (currentId != 0 && i < length) {
offerIds[i] = currentId;
(offers[i], details[i]) = offerInfo(olKey, currentId);
currentId = nextOfferIdById(olKey, currentId);
i = i + 1;
}
return (currentId, offerIds, offers, details);
}
}
/* Next/Prev offers */
/// @notice Get the offer after a given offer, given its id
function nextOfferIdById(OLKey memory olKey, uint offerId) public view returns (uint) {
return nextOfferId(olKey, MGV.offers(olKey, offerId));
}
/// @notice Get the offer after a given offer
function nextOfferId(OLKey memory olKey, Offer offer) public view returns (uint) {
// WARNING
// If the offer is not actually recorded in the offer list, results will be meaningless.
// if (offer.gives() == 0) {
// revert("Offer is not live, prev/next meaningless.");
// }
Bin offerBin = offer.bin(olKey.tickSpacing);
uint nextId = offer.next();
if (nextId == 0) {
int index = offerBin.leafIndex();
Leaf leaf = MGV.leafs(olKey, index);
leaf = leaf.eraseBelow(offerBin);
if (leaf.isEmpty()) {
index = offerBin.level3Index();
Field field = MGV.level3s(olKey, index);
field = field.eraseBelowInLevel3(offerBin);
if (field.isEmpty()) {
index = offerBin.level2Index();
field = MGV.level2s(olKey, index);
field = field.eraseBelowInLevel2(offerBin);
if (field.isEmpty()) {
index = offerBin.level1Index();
field = MGV.level1s(olKey, index);
field = field.eraseBelowInLevel1(offerBin);
if (field.isEmpty()) {
field = MGV.root(olKey);
field = field.eraseBelowInRoot(offerBin);
if (field.isEmpty()) {
return 0;
}
index = field.firstLevel1Index();
field = MGV.level1s(olKey, index);
}
index = field.firstLevel2Index(index);
field = MGV.level2s(olKey, index);
}
index = field.firstLevel3Index(index);
field = MGV.level3s(olKey, index);
}
leaf = MGV.leafs(olKey, field.firstLeafIndex(index));
}
nextId = leaf.bestOfferId();
}
return nextId;
}
/// @notice Get the offer before a given offer, given its id
function prevOfferIdById(OLKey memory olKey, uint offerId) public view returns (uint) {
return prevOfferId(olKey, MGV.offers(olKey, offerId));
}
/// @notice Get the offer before a given offer
function prevOfferId(OLKey memory olKey, Offer offer) public view returns (uint offerId) {
// WARNING
// If the offer is not actually recorded in the offer list, results will be meaningless.
// if (offer.gives() == 0) {
// revert("Offer is not live, prev/next meaningless.");
// }
Bin offerBin = offer.bin(olKey.tickSpacing);
uint prevId = offer.prev();
if (prevId == 0) {
int index = offerBin.leafIndex();
Leaf leaf = MGV.leafs(olKey, index);
leaf = leaf.eraseAbove(offerBin);
if (leaf.isEmpty()) {
index = offerBin.level3Index();
Field field = MGV.level3s(olKey, index);
field = field.eraseAboveInLevel3(offerBin);
if (field.isEmpty()) {
index = offerBin.level2Index();
field = MGV.level2s(olKey, index);
field = field.eraseAboveInLevel2(offerBin);
if (field.isEmpty()) {
index = offerBin.level1Index();
field = MGV.level1s(olKey, index);
field = field.eraseAboveInLevel1(offerBin);
if (field.isEmpty()) {
field = MGV.root(olKey);
field = field.eraseAboveInRoot(offerBin);
if (field.isEmpty()) {
return 0;
}
index = field.lastLevel1Index();
field = MGV.level1s(olKey, index);
}
index = field.lastLevel2Index(index);
field = MGV.level2s(olKey, index);
}
index = field.lastLevel3Index(index);
field = MGV.level3s(olKey, index);
}
leaf = MGV.leafs(olKey, field.lastLeafIndex(index));
}
prevId = leaf.bestOfferId();
}
return prevId;
}
// # Offer list utility functions
/* Returns the minimum outbound_tkn volume to give on the outbound_tkn/inbound_tkn offer list for an offer that requires gasreq gas. */
function minVolume(OLKey memory olKey, uint gasreq) public view returns (uint) {
Local _local = MGV.local(olKey);
return _local.density().multiplyUp(gasreq + _local.offer_gasbase());
}
/* Returns the provision necessary to post an offer on the outbound_tkn/inbound_tkn offer list. You can set gasprice=0 or use the overload to use Mangrove's internal gasprice estimate. */
function getProvision(OLKey memory olKey, uint ofr_gasreq, uint ofr_gasprice) public view returns (uint) {
unchecked {
(Global _global, Local _local) = MGV.config(olKey);
uint gp;
uint global_gasprice = _global.gasprice();
if (global_gasprice > ofr_gasprice) {
gp = global_gasprice;
} else {
gp = ofr_gasprice;
}
return (ofr_gasreq + _local.offer_gasbase()) * gp * 1e6;
}
}
/* Sugar for getProvision(olKey, ofr_gasreq, 0) */
function getProvisionWithDefaultGasPrice(OLKey memory olKey, uint ofr_gasreq) public view returns (uint) {
unchecked {
return getProvision(olKey, ofr_gasreq, 0);
}
}
/* Returns the fee that would be extracted from the given volume of `outbound_tkn` on Mangrove's `outbound_tkn`/`inbound_tkn` offer list. */
function getFee(OLKey memory olKey, uint outVolume) external view returns (uint) {
(, Local _local) = MGV.config(olKey);
return ((outVolume * _local.fee()) / 10000);
}
/* Returns the given amount of `outbound_tkn` minus the fee on Mangrove's `outbound_tkn`/`inbound_tkn` offer list. */
function minusFee(OLKey memory olKey, uint outVolume) external view returns (uint) {
(, Local _local) = MGV.config(olKey);
return (outVolume * (10_000 - _local.fee())) / 10000;
}
// # Simulation functions
// These are a cheaper way to approximate the results of a market order than by actually executing it and reverting.
/* `simulateMarketOrderBy*`, `simulateInternalMarketOrder`, and `simulateExecute` all together simulate a market order on Mangrove and return the cumulative `totalGot`, `totalGave`, and `totalGasreq` for each offer traversed. We assume offer execution is successful and uses exactly its `gasreq`.
We do not account for gasbase.
* Calling this from an EOA will give you an estimate of the volumes you will receive, but you may as well `eth_call` Mangrove.
* Calling this from a contract will let the contract choose what to do after receiving a response.
* If `!accumulate`, only return the total cumulative volume.
*/
function simulateMarketOrderByVolume(OLKey memory olKey, uint takerWants, uint takerGives, bool fillWants)
external
view
returns (VolumeData[] memory)
{
return simulateMarketOrderByVolume(olKey, takerWants, takerGives, fillWants, true);
}
function simulateMarketOrderByVolume(
OLKey memory olKey,
uint takerWants,
uint takerGives,
bool fillWants,
bool accumulate
) public view returns (VolumeData[] memory) {
uint fillVolume = fillWants ? takerWants : takerGives;
Tick maxTick = TickLib.tickFromVolumes(takerGives, takerWants);
return simulateMarketOrderByTick(olKey, maxTick, fillVolume, fillWants, accumulate);
}
function simulateMarketOrderByTick(OLKey memory olKey, Tick maxTick, uint fillVolume, bool fillWants)
public
view
returns (VolumeData[] memory)
{
return simulateMarketOrderByTick(olKey, maxTick, fillVolume, fillWants, true);
}
function simulateMarketOrderByTick(OLKey memory olKey, Tick maxTick, uint fillVolume, bool fillWants, bool accumulate)
public
view
returns (VolumeData[] memory)
{
MarketOrder memory mr;
mr.olKey = olKey;
Global _global;
(_global, mr.local) = MGV.config(olKey);
mr.offerId = MGV.best(olKey);
mr.offer = MGV.offers(olKey, mr.offerId);
mr.maxTick = maxTick;
mr.currentFillVolume = fillVolume;
mr.initialFillVolume = fillVolume;
mr.fillWants = fillWants;
mr.accumulate = accumulate;
mr.maxRecursionDepth = _global.maxRecursionDepth();
simulateInternalMarketOrder(mr);
return mr.volumeData;
}
function simulateInternalMarketOrder(MarketOrder memory mr) internal view {
unchecked {
if (
mr.currentFillVolume > 0 && Tick.unwrap(mr.offer.tick()) <= Tick.unwrap(mr.maxTick) && mr.offerId > 0
&& mr.maxRecursionDepth > 0
) {
uint currentIndex = mr.numOffers;
mr.offerDetail = MGV.offerDetails(mr.olKey, mr.offerId);
mr.maxRecursionDepth--;
simulateExecute(mr);
uint totalGot = mr.totalGot;
uint totalGave = mr.totalGave;
uint totalGasreq = mr.totalGasreq;
mr.numOffers++;
mr.currentFillVolume -= mr.fillWants ? mr.currentWants : mr.currentGives;
mr.offerId = nextOfferId(mr.olKey, mr.offer);
mr.offer = MGV.offers(mr.olKey, mr.offerId);
simulateInternalMarketOrder(mr);
if (mr.accumulate || currentIndex == 0) {
uint concreteFee = (mr.totalGot * mr.local.fee()) / 10_000;
mr.volumeData[currentIndex] =
VolumeData({totalGot: totalGot - concreteFee, totalGave: totalGave, totalGasreq: totalGasreq});
}
} else {
if (mr.accumulate) {
mr.volumeData = new VolumeData[](mr.numOffers);
} else {
mr.volumeData = new VolumeData[](1);
}
}
}
}
function simulateExecute(MarketOrder memory mr) internal pure {
unchecked {
{
// caching
uint fillVolume = mr.currentFillVolume;
uint offerGives = mr.offer.gives();
uint offerWants = mr.offer.wants();
if ((mr.fillWants && offerGives < fillVolume) || (!mr.fillWants && offerWants < fillVolume)) {
mr.currentWants = offerGives;
mr.currentGives = offerWants;
} else {
if (mr.fillWants) {
mr.currentGives = mr.offer.tick().inboundFromOutboundUp(fillVolume);
mr.currentWants = fillVolume;
} else {
// offerWants = 0 is forbidden at offer writing
mr.currentWants = mr.offer.tick().outboundFromInbound(fillVolume);
mr.currentGives = fillVolume;
}
}
}
// flashloan would normally be called here
// if success branch of original mangrove code, assumed to be true
mr.totalGot += mr.currentWants;
mr.totalGave += mr.currentGives;
mr.totalGasreq += mr.offerDetail.gasreq();
/* end if success branch **/
}
}
// # Open markets tracking
/// @return uint The number of open markets.
function numOpenMarkets() external view returns (uint) {
return _openMarkets.length;
}
/// @return markets all open markets
/// @return configs the configs of each markets
/// @notice If the ith market is [tkn0,tkn1], then the ith config will be a MarketConfig where config01 is the config for the tkn0/tkn1 offer list, and config10 is the config for the tkn1/tkn0 offer list.
function openMarkets() external view returns (Market[] memory, MarketConfig[] memory) {
return openMarkets(0, _openMarkets.length, true);
}
/// @notice List open markets, and optionally skip querying Mangrove for all the market configurations.
/// @param withConfig if false, the second return value will be the empty array.
/// @return Market[] all open markets
/// @return MarketConfig[] corresponding configs, or the empty array if withConfig is false.
function openMarkets(bool withConfig) external view returns (Market[] memory, MarketConfig[] memory) {
return openMarkets(0, _openMarkets.length, withConfig);
}
/// @notice Get a slice of open markets, with accompanying market configs
/// @return markets The following slice of _openMarkets: [from..min(_openMarkets.length,from+maxLen)]
/// @return configs corresponding configs
/// @dev throws if `from > _openMarkets.length`
function openMarkets(uint from, uint maxLen)
external
view
returns (Market[] memory markets, MarketConfig[] memory configs)
{
return openMarkets(from, maxLen, true);
}
/// @notice Get a slice of open markets, with accompanying market configs or not.
/// @param withConfig if false, the second return value will be the empty array.
/// @return markets The following slice of _openMarkets: [from..min(_openMarkets.length,from+maxLen)]
/// @return configs corresponding configs, or the empty array if withConfig is false.
/// @dev if there is a delay in updating a market, it is possible that an 'open market' (according to this contract) is not in fact open and that config01.active and config10.active are both false.
/// @dev throws if `from > _openMarkets.length`
function openMarkets(uint from, uint maxLen, bool withConfig)
public
view
returns (Market[] memory markets, MarketConfig[] memory configs)
{
uint numMarkets = _openMarkets.length;
if (from + maxLen > numMarkets) {
maxLen = numMarkets - from;
}
markets = new Market[](maxLen);
configs = new MarketConfig[](withConfig ? maxLen : 0);
unchecked {
for (uint i = 0; i < maxLen; ++i) {
address tkn0 = _openMarkets[from + i].tkn0;
address tkn1 = _openMarkets[from + i].tkn1;
uint tickSpacing = _openMarkets[from + i].tickSpacing;
// copy
markets[i] = Market({tkn0: tkn0, tkn1: tkn1, tickSpacing: tickSpacing});
if (withConfig) {
configs[i].config01 = localUnpacked(toOLKey(markets[i]));
configs[i].config10 = localUnpacked(toOLKey(flipped(markets[i])));
}
}
}
}
/// @param market the market
/// @return bool Whether the {tkn0,tkn1} market is open.
/// @dev May not reflect the true state of the market on Mangrove if `updateMarket` was not called recently enough.
function isMarketOpen(Market memory market) external view returns (bool) {
(market.tkn0, market.tkn1) = order(market.tkn0, market.tkn1);
return marketPositions[market.tkn0][market.tkn1][market.tickSpacing] > 0;
}
/// @notice return the configuration for the given market
/// @param market the market
/// @return config The market configuration. config01 and config10 follow the order given in arguments, not the canonical order
/// @dev This function queries Mangrove so all the returned info is up-to-date.
function marketConfig(Market memory market) external view returns (MarketConfig memory config) {
config.config01 = localUnpacked(toOLKey(market));
config.config10 = localUnpacked(toOLKey(flipped(market)));
}
/// @notice Permissionless update of _openMarkets array.
/// @notice Will consider a market open iff either the offer lists tkn0/tkn1 or tkn1/tkn0 are open on Mangrove.
function updateMarket(Market memory market) external {
(market.tkn0, market.tkn1) = order(market.tkn0, market.tkn1);
bool openOnMangrove = MGV.local(toOLKey(market)).active() || MGV.local(toOLKey(flipped(market))).active();
uint position = marketPositions[market.tkn0][market.tkn1][market.tickSpacing];
if (openOnMangrove && position == 0) {
_openMarkets.push(market);
marketPositions[market.tkn0][market.tkn1][market.tickSpacing] = _openMarkets.length;
} else if (!openOnMangrove && position > 0) {
uint numMarkets = _openMarkets.length;
if (numMarkets > 1) {
// avoid array holes
Market memory lastMarket = _openMarkets[numMarkets - 1];
_openMarkets[position - 1] = lastMarket;
marketPositions[lastMarket.tkn0][lastMarket.tkn1][lastMarket.tickSpacing] = position;
}
_openMarkets.pop();
marketPositions[market.tkn0][market.tkn1][market.tickSpacing] = 0;
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
/* ************************************************** *
GENERATED FILE. DO NOT EDIT.
* ************************************************** */
/* since you can't convert bool to uint in an expression without conditionals,
* we add a file-level function and rely on compiler optimization
*/
function uint_of_bool(bool b) pure returns (uint u) {
assembly ("memory-safe") { u := b }
}
import "@mgv/lib/core/Constants.sol";
struct OfferUnpacked {
uint prev;
uint next;
Tick tick;
uint gives;
}
//some type safety for each struct
type Offer is uint;
using OfferLib for Offer global;
////////////// ADDITIONAL DEFINITIONS, IF ANY ////////////////
import {Bin} from "@mgv/lib/core/TickTreeLib.sol";
import {Tick} from "@mgv/lib/core/TickLib.sol";
import {OfferExtra,OfferUnpackedExtra} from "@mgv/lib/core/OfferExtra.sol";
using OfferExtra for Offer global;
using OfferUnpackedExtra for OfferUnpacked global;
////////////// END OF ADDITIONAL DEFINITIONS /////////////////
library OfferLib {
// number of bits in each field
uint constant prev_bits = 32;
uint constant next_bits = 32;
uint constant tick_bits = 21;
uint constant gives_bits = 127;
// number of bits before each field
uint constant prev_before = 0 + 0;
uint constant next_before = prev_before + prev_bits;
uint constant tick_before = next_before + next_bits;
uint constant gives_before = tick_before + tick_bits;
// focus-mask: 1s at field location, 0s elsewhere
uint constant prev_mask_inv = (ONES << 256 - prev_bits) >> prev_before;
uint constant next_mask_inv = (ONES << 256 - next_bits) >> next_before;
uint constant tick_mask_inv = (ONES << 256 - tick_bits) >> tick_before;
uint constant gives_mask_inv = (ONES << 256 - gives_bits) >> gives_before;
// cleanup-mask: 0s at field location, 1s elsewhere
uint constant prev_mask = ~prev_mask_inv;
uint constant next_mask = ~next_mask_inv;
uint constant tick_mask = ~tick_mask_inv;
uint constant gives_mask = ~gives_mask_inv;
// cast-mask: 0s followed by |field| trailing 1s
uint constant prev_cast_mask = ~(ONES << prev_bits);
uint constant next_cast_mask = ~(ONES << next_bits);
uint constant tick_cast_mask = ~(ONES << tick_bits);
uint constant gives_cast_mask = ~(ONES << gives_bits);
// size-related error message
string constant prev_size_error = "mgv/config/prev/32bits";
string constant next_size_error = "mgv/config/next/32bits";
string constant tick_size_error = "mgv/config/tick/21bits";
string constant gives_size_error = "mgv/config/gives/127bits";
// from packed to in-memory struct
function to_struct(Offer __packed) internal pure returns (OfferUnpacked memory __s) { unchecked {
__s.prev = uint(Offer.unwrap(__packed) << prev_before) >> (256 - prev_bits);
__s.next = uint(Offer.unwrap(__packed) << next_before) >> (256 - next_bits);
__s.tick = Tick.wrap(int(int(Offer.unwrap(__packed) << tick_before) >> (256 - tick_bits)));
__s.gives = uint(Offer.unwrap(__packed) << gives_before) >> (256 - gives_bits);
}}
// equality checking
function eq(Offer __packed1, Offer __packed2) internal pure returns (bool) { unchecked {
return Offer.unwrap(__packed1) == Offer.unwrap(__packed2);
}}
// from packed to a tuple
function unpack(Offer __packed) internal pure returns (uint __prev, uint __next, Tick __tick, uint __gives) { unchecked {
__prev = uint(Offer.unwrap(__packed) << prev_before) >> (256 - prev_bits);
__next = uint(Offer.unwrap(__packed) << next_before) >> (256 - next_bits);
__tick = Tick.wrap(int(int(Offer.unwrap(__packed) << tick_before) >> (256 - tick_bits)));
__gives = uint(Offer.unwrap(__packed) << gives_before) >> (256 - gives_bits);
}}
// getters
function prev(Offer __packed) internal pure returns(uint) { unchecked {
return uint(Offer.unwrap(__packed) << prev_before) >> (256 - prev_bits);
}}
// setters
function prev(Offer __packed,uint val) internal pure returns(Offer) { unchecked {
return Offer.wrap((Offer.unwrap(__packed) & prev_mask) | (val << (256 - prev_bits)) >> prev_before);
}}
function next(Offer __packed) internal pure returns(uint) { unchecked {
return uint(Offer.unwrap(__packed) << next_before) >> (256 - next_bits);
}}
// setters
function next(Offer __packed,uint val) internal pure returns(Offer) { unchecked {
return Offer.wrap((Offer.unwrap(__packed) & next_mask) | (val << (256 - next_bits)) >> next_before);
}}
function tick(Offer __packed) internal pure returns(Tick) { unchecked {
return Tick.wrap(int(int(Offer.unwrap(__packed) << tick_before) >> (256 - tick_bits)));
}}
// setters
function tick(Offer __packed,Tick val) internal pure returns(Offer) { unchecked {
return Offer.wrap((Offer.unwrap(__packed) & tick_mask) | (uint(Tick.unwrap(val)) << (256 - tick_bits)) >> tick_before);
}}
function gives(Offer __packed) internal pure returns(uint) { unchecked {
return uint(Offer.unwrap(__packed) << gives_before) >> (256 - gives_bits);
}}
// setters
function gives(Offer __packed,uint val) internal pure returns(Offer) { unchecked {
return Offer.wrap((Offer.unwrap(__packed) & gives_mask) | (val << (256 - gives_bits)) >> gives_before);
}}
// from in-memory struct to packed
function t_of_struct(OfferUnpacked memory __s) internal pure returns (Offer) { unchecked {
return pack(__s.prev, __s.next, __s.tick, __s.gives);
}}
// from arguments to packed
function pack(uint __prev, uint __next, Tick __tick, uint __gives) internal pure returns (Offer) { unchecked {
uint __packed;
__packed |= (__prev << (256 - prev_bits)) >> prev_before;
__packed |= (__next << (256 - next_bits)) >> next_before;
__packed |= (uint(Tick.unwrap(__tick)) << (256 - tick_bits)) >> tick_before;
__packed |= (__gives << (256 - gives_bits)) >> gives_before;
return Offer.wrap(__packed);
}}
// input checking
function prev_check(uint __prev) internal pure returns (bool) { unchecked {
return (__prev & prev_cast_mask) == __prev;
}}
function next_check(uint __next) internal pure returns (bool) { unchecked {
return (__next & next_cast_mask) == __next;
}}
function tick_check(Tick __tick) internal pure returns (bool) { unchecked {
return (uint(Tick.unwrap(__tick)) & tick_cast_mask) == uint(Tick.unwrap(__tick));
}}
function gives_check(uint __gives) internal pure returns (bool) { unchecked {
return (__gives & gives_cast_mask) == __gives;
}}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
/* ************************************************** *
GENERATED FILE. DO NOT EDIT.
* ************************************************** */
/* since you can't convert bool to uint in an expression without conditionals,
* we add a file-level function and rely on compiler optimization
*/
function uint_of_bool(bool b) pure returns (uint u) {
assembly ("memory-safe") { u := b }
}
import "@mgv/lib/core/Constants.sol";
struct OfferDetailUnpacked {
address maker;
uint gasreq;
uint kilo_offer_gasbase;
uint gasprice;
}
//some type safety for each struct
type OfferDetail is uint;
using OfferDetailLib for OfferDetail global;
////////////// ADDITIONAL DEFINITIONS, IF ANY ////////////////
import {OfferDetailExtra,OfferDetailUnpackedExtra} from "@mgv/lib/core/OfferDetailExtra.sol";
using OfferDetailExtra for OfferDetail global;
using OfferDetailUnpackedExtra for OfferDetailUnpacked global;
////////////// END OF ADDITIONAL DEFINITIONS /////////////////
library OfferDetailLib {
// number of bits in each field
uint constant maker_bits = 160;
uint constant gasreq_bits = 24;
uint constant kilo_offer_gasbase_bits = 9;
uint constant gasprice_bits = 26;
// number of bits before each field
uint constant maker_before = 0 + 0;
uint constant gasreq_before = maker_before + maker_bits;
uint constant kilo_offer_gasbase_before = gasreq_before + gasreq_bits;
uint constant gasprice_before = kilo_offer_gasbase_before + kilo_offer_gasbase_bits;
// focus-mask: 1s at field location, 0s elsewhere
uint constant maker_mask_inv = (ONES << 256 - maker_bits) >> maker_before;
uint constant gasreq_mask_inv = (ONES << 256 - gasreq_bits) >> gasreq_before;
uint constant kilo_offer_gasbase_mask_inv = (ONES << 256 - kilo_offer_gasbase_bits) >> kilo_offer_gasbase_before;
uint constant gasprice_mask_inv = (ONES << 256 - gasprice_bits) >> gasprice_before;
// cleanup-mask: 0s at field location, 1s elsewhere
uint constant maker_mask = ~maker_mask_inv;
uint constant gasreq_mask = ~gasreq_mask_inv;
uint constant kilo_offer_gasbase_mask = ~kilo_offer_gasbase_mask_inv;
uint constant gasprice_mask = ~gasprice_mask_inv;
// cast-mask: 0s followed by |field| trailing 1s
uint constant maker_cast_mask = ~(ONES << maker_bits);
uint constant gasreq_cast_mask = ~(ONES << gasreq_bits);
uint constant kilo_offer_gasbase_cast_mask = ~(ONES << kilo_offer_gasbase_bits);
uint constant gasprice_cast_mask = ~(ONES << gasprice_bits);
// size-related error message
string constant maker_size_error = "mgv/config/maker/160bits";
string constant gasreq_size_error = "mgv/config/gasreq/24bits";
string constant kilo_offer_gasbase_size_error = "mgv/config/kilo_offer_gasbase/9bits";
string constant gasprice_size_error = "mgv/config/gasprice/26bits";
// from packed to in-memory struct
function to_struct(OfferDetail __packed) internal pure returns (OfferDetailUnpacked memory __s) { unchecked {
__s.maker = address(uint160(uint(OfferDetail.unwrap(__packed) << maker_before) >> (256 - maker_bits)));
__s.gasreq = uint(OfferDetail.unwrap(__packed) << gasreq_before) >> (256 - gasreq_bits);
__s.kilo_offer_gasbase = uint(OfferDetail.unwrap(__packed) << kilo_offer_gasbase_before) >> (256 - kilo_offer_gasbase_bits);
__s.gasprice = uint(OfferDetail.unwrap(__packed) << gasprice_before) >> (256 - gasprice_bits);
}}
// equality checking
function eq(OfferDetail __packed1, OfferDetail __packed2) internal pure returns (bool) { unchecked {
return OfferDetail.unwrap(__packed1) == OfferDetail.unwrap(__packed2);
}}
// from packed to a tuple
function unpack(OfferDetail __packed) internal pure returns (address __maker, uint __gasreq, uint __kilo_offer_gasbase, uint __gasprice) { unchecked {
__maker = address(uint160(uint(OfferDetail.unwrap(__packed) << maker_before) >> (256 - maker_bits)));
__gasreq = uint(OfferDetail.unwrap(__packed) << gasreq_before) >> (256 - gasreq_bits);
__kilo_offer_gasbase = uint(OfferDetail.unwrap(__packed) << kilo_offer_gasbase_before) >> (256 - kilo_offer_gasbase_bits);
__gasprice = uint(OfferDetail.unwrap(__packed) << gasprice_before) >> (256 - gasprice_bits);
}}
// getters
function maker(OfferDetail __packed) internal pure returns(address) { unchecked {
return address(uint160(uint(OfferDetail.unwrap(__packed) << maker_before) >> (256 - maker_bits)));
}}
// setters
function maker(OfferDetail __packed,address val) internal pure returns(OfferDetail) { unchecked {
return OfferDetail.wrap((OfferDetail.unwrap(__packed) & maker_mask) | (uint(uint160(val)) << (256 - maker_bits)) >> maker_before);
}}
function gasreq(OfferDetail __packed) internal pure returns(uint) { unchecked {
return uint(OfferDetail.unwrap(__packed) << gasreq_before) >> (256 - gasreq_bits);
}}
// setters
function gasreq(OfferDetail __packed,uint val) internal pure returns(OfferDetail) { unchecked {
return OfferDetail.wrap((OfferDetail.unwrap(__packed) & gasreq_mask) | (val << (256 - gasreq_bits)) >> gasreq_before);
}}
function kilo_offer_gasbase(OfferDetail __packed) internal pure returns(uint) { unchecked {
return uint(OfferDetail.unwrap(__packed) << kilo_offer_gasbase_before) >> (256 - kilo_offer_gasbase_bits);
}}
// setters
function kilo_offer_gasbase(OfferDetail __packed,uint val) internal pure returns(OfferDetail) { unchecked {
return OfferDetail.wrap((OfferDetail.unwrap(__packed) & kilo_offer_gasbase_mask) | (val << (256 - kilo_offer_gasbase_bits)) >> kilo_offer_gasbase_before);
}}
function gasprice(OfferDetail __packed) internal pure returns(uint) { unchecked {
return uint(OfferDetail.unwrap(__packed) << gasprice_before) >> (256 - gasprice_bits);
}}
// setters
function gasprice(OfferDetail __packed,uint val) internal pure returns(OfferDetail) { unchecked {
return OfferDetail.wrap((OfferDetail.unwrap(__packed) & gasprice_mask) | (val << (256 - gasprice_bits)) >> gasprice_before);
}}
// from in-memory struct to packed
function t_of_struct(OfferDetailUnpacked memory __s) internal pure returns (OfferDetail) { unchecked {
return pack(__s.maker, __s.gasreq, __s.kilo_offer_gasbase, __s.gasprice);
}}
// from arguments to packed
function pack(address __maker, uint __gasreq, uint __kilo_offer_gasbase, uint __gasprice) internal pure returns (OfferDetail) { unchecked {
uint __packed;
__packed |= (uint(uint160(__maker)) << (256 - maker_bits)) >> maker_before;
__packed |= (__gasreq << (256 - gasreq_bits)) >> gasreq_before;
__packed |= (__kilo_offer_gasbase << (256 - kilo_offer_gasbase_bits)) >> kilo_offer_gasbase_before;
__packed |= (__gasprice << (256 - gasprice_bits)) >> gasprice_before;
return OfferDetail.wrap(__packed);
}}
// input checking
function maker_check(address __maker) internal pure returns (bool) { unchecked {
return (uint(uint160(__maker)) & maker_cast_mask) == uint(uint160(__maker));
}}
function gasreq_check(uint __gasreq) internal pure returns (bool) { unchecked {
return (__gasreq & gasreq_cast_mask) == __gasreq;
}}
function kilo_offer_gasbase_check(uint __kilo_offer_gasbase) internal pure returns (bool) { unchecked {
return (__kilo_offer_gasbase & kilo_offer_gasbase_cast_mask) == __kilo_offer_gasbase;
}}
function gasprice_check(uint __gasprice) internal pure returns (bool) { unchecked {
return (__gasprice & gasprice_cast_mask) == __gasprice;
}}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import {OfferDetail,OfferDetailUnpacked} from "@mgv/src/preprocessed/OfferDetail.post.sol";
/* Extra functions for the offer details */
library OfferDetailExtra {
function offer_gasbase(OfferDetail offerDetail) internal pure returns (uint) { unchecked {
return offerDetail.kilo_offer_gasbase() * 1e3;
}}
function offer_gasbase(OfferDetail offerDetail,uint val) internal pure returns (OfferDetail) { unchecked {
return offerDetail.kilo_offer_gasbase(val/1e3);
}}
}
/* Extra functions for the struct version of the offer details */
library OfferDetailUnpackedExtra {
function offer_gasbase(OfferDetailUnpacked memory offerDetail) internal pure returns (uint) { unchecked {
return offerDetail.kilo_offer_gasbase * 1e3;
}}
function offer_gasbase(OfferDetailUnpacked memory offerDetail,uint val) internal pure { unchecked {
offerDetail.kilo_offer_gasbase = val/1e3;
}}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "@mgv/lib/core/TickTreeLib.sol";
import "@mgv/lib/core/TickLib.sol";
import {Offer,OfferUnpacked,OfferLib} from "@mgv/src/preprocessed/Offer.post.sol";
/* cleanup-mask: 0s at location of fields to hide from maker, 1s elsewhere */
uint constant HIDE_FIELDS_FROM_MAKER_MASK = ~(OfferLib.prev_mask_inv | OfferLib.next_mask_inv);
/* Extra functions for the offer */
library OfferExtra {
/* Compute wants from tick and gives */
function wants(Offer offer) internal pure returns (uint) {
return offer.tick().inboundFromOutboundUp(offer.gives());
}
/* Sugar to test offer liveness */
function isLive(Offer offer) internal pure returns (bool resp) {
uint gives = offer.gives();
assembly ("memory-safe") {
resp := iszero(iszero(gives))
}
}
/* Get the bin where the offer is stored given an offer list that has `tickSpacing` */
function bin(Offer offer, uint tickSpacing) internal pure returns (Bin) {
return offer.tick().nearestBin(tickSpacing);
}
/* Removes prev/next pointers from an offer before sending it to the maker. Ensures that the maker has no information about the state of the book when it gets called. */
function clearFieldsForMaker(Offer offer) internal pure returns (Offer) {
unchecked {
return Offer.wrap(
Offer.unwrap(offer)
& HIDE_FIELDS_FROM_MAKER_MASK);
}
}
}
/* Extra functions for the struct version of the offer */
library OfferUnpackedExtra {
/* Compute wants from tick and gives */
function wants(OfferUnpacked memory offer) internal pure returns (uint) {
return offer.tick.inboundFromOutboundUp(offer.gives);
}
/* Sugar to test offer liveness */
function isLive(OfferUnpacked memory offer) internal pure returns (bool resp) {
uint gives = offer.gives;
assembly ("memory-safe") {
resp := iszero(iszero(gives))
}
}
/* Removes prev/next pointers from an offer before sending it to the maker; Ensures that the maker has no information about the state of the book when it gets called. */
function bin(OfferUnpacked memory offer, uint tickSpacing) internal pure returns (Bin) {
return offer.tick.nearestBin(tickSpacing);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
/* ************************************************** *
GENERATED FILE. DO NOT EDIT.
* ************************************************** */
import {Offer, OfferUnpacked, OfferLib} from "./Offer.post.sol";
import {OfferDetail, OfferDetailUnpacked, OfferDetailLib} from "./OfferDetail.post.sol";
import {Global, GlobalUnpacked, GlobalLib} from "./Global.post.sol";
import {Local, LocalUnpacked, LocalLib} from "./Local.post.sol";
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import {Bin} from "@mgv/lib/core/TickTreeLib.sol";
import "@mgv/lib/core/BitLib.sol";
import "@mgv/lib/core/Constants.sol";
/* This file is inspired by Uniswap's approach to ticks, with the following notable changes:
- directly compute ticks base 1.0001 (not base `sqrt(1.0001)`)
- directly compute ratios (not `sqrt(ratio)`) (simpler code elsewhere when dealing with actual ratios and logs of ratios)
- ratios are floating-point numbers, not fixed-point numbers (increases precision when computing amounts)
*/
/* # TickLib
The `TickLib` file contains tick math-related code and utilities for manipulating ticks. It also holds functions related to ratios, which are represented as (mantissa,exponent) pairs. */
/* Globally enable `tick.method(...)` */
type Tick is int;
using TickLib for Tick global;
library TickLib {
function inRange(Tick tick) internal pure returns (bool) {
return Tick.unwrap(tick) >= MIN_TICK && Tick.unwrap(tick) <= MAX_TICK;
}
function eq(Tick tick1, Tick tick2) internal pure returns (bool) {
unchecked {
return Tick.unwrap(tick1) == Tick.unwrap(tick2);
}
}
/* Returns the nearest, higher bin to the given `tick` at the given `tickSpacing`
We do not force ticks to fit the tickSpacing (aka `tick%tickSpacing==0`). Ratios are rounded up that the maker is always paid at least what they asked for
*/
function nearestBin(Tick tick, uint tickSpacing) internal pure returns (Bin bin) {
unchecked {
// By default division rounds towards 0. Since `smod` is signed we get the sign of `tick` and `tick%tickSpacing` in a single instruction.
assembly("memory-safe") {
bin := sdiv(tick,tickSpacing)
bin := add(bin,sgt(smod(tick,tickSpacing),0))
}
}
}
/* ## Conversion functions */
/* ### (inbound,tick) → outbound
`inboundFromOutbound[Up]` converts an outbound amount (i.e. an `offer.gives` or a `takerWants`), to an inbound amount, following the price induced by `tick`. There's a rounding-up and a rounding-down variant.
`outboundAmt` should not exceed 127 bits.
*/
function inboundFromOutbound(Tick tick, uint outboundAmt) internal pure returns (uint) {
(uint sig, uint exp) = ratioFromTick(tick);
return (sig * outboundAmt) >> exp;
}
function inboundFromOutboundUp(Tick tick, uint outboundAmt) internal pure returns (uint) {
unchecked {
(uint sig, uint exp) = ratioFromTick(tick);
return divExpUp(sig*outboundAmt,exp);
}
}
/* ### (outbound,tick) → inbound */
/* `outboundFromInbound[Up]` converts an inbound amount (i.e. an `offer.wants` or a `takerGives`), to an outbound amount, following the price induced by `tick`. There's a rounding-up and a rounding-down variant.
`inboundAmt` should not exceed 127 bits.
*/
function outboundFromInbound(Tick tick, uint inboundAmt) internal pure returns (uint) {
(uint sig, uint exp) = ratioFromTick(Tick.wrap(-Tick.unwrap(tick)));
return (sig * inboundAmt) >> exp;
}
function outboundFromInboundUp(Tick tick, uint inboundAmt) internal pure returns (uint) {
unchecked {
(uint sig, uint exp) = ratioFromTick(Tick.wrap(-Tick.unwrap(tick)));
return divExpUp(sig*inboundAmt,exp);
}
}
/* ## Ratio representation
Ratios are represented as a (mantissa,exponent) pair which represents the number `mantissa * 2**-exponent`.
The exponent is negated so that, for ratios in the accepted range, the exponent is `>= 0`. This simplifies the code.
Floats are normalized so that the mantissa uses exactly 128 bits. It enables easy comparison between floats and ensures they can be multiplied by amounts without overflow.
The accepted ratio range is between `ratioFromTick(MIN_TICK)` and `ratioFromTick(MAX_TICK)` (inclusive).
*/
/* ### (inbound,outbound) → ratio */
/* `ratioFromVolumes` converts a pair of (inbound,outbound) volumes to a floating-point, normalized ratio. It rounds down.
* `outboundAmt = 0` has a special meaning and the highest possible price will be returned.
* `inboundAmt = 0` has a special meaning if `outboundAmt != 0` and the lowest possible price will be returned.
*/
function ratioFromVolumes(uint inboundAmt, uint outboundAmt) internal pure returns (uint mantissa, uint exp) {
unchecked {
require(inboundAmt <= MAX_SAFE_VOLUME, "mgv/ratioFromVol/inbound/tooBig");
require(outboundAmt <= MAX_SAFE_VOLUME, "mgv/ratioFromVol/outbound/tooBig");
if (outboundAmt == 0) {
return (MAX_RATIO_MANTISSA,uint(MAX_RATIO_EXP));
} else if (inboundAmt == 0) {
return (MIN_RATIO_MANTISSA,uint(MIN_RATIO_EXP));
}
uint ratio = (inboundAmt << MANTISSA_BITS) / outboundAmt;
uint log2 = BitLib.fls(ratio);
require(ratio != 0,"mgv/ratioFromVolumes/zeroRatio");
if (log2 > MANTISSA_BITS_MINUS_ONE) {
uint diff = log2 - MANTISSA_BITS_MINUS_ONE;
return (ratio >> diff, MANTISSA_BITS - diff);
} else {
uint diff = MANTISSA_BITS_MINUS_ONE - log2;
return (ratio << diff, MANTISSA_BITS + diff);
}
}
}
/* ### (inbound,outbound) → tick */
function tickFromVolumes(uint inboundAmt, uint outboundAmt) internal pure returns (Tick tick) {
(uint man, uint exp) = ratioFromVolumes(inboundAmt, outboundAmt);
return tickFromNormalizedRatio(man,exp);
}
/* ### ratio → tick */
/* Does not require a normalized ratio. */
function tickFromRatio(uint mantissa, int exp) internal pure returns (Tick) {
uint normalized_exp;
(mantissa, normalized_exp) = normalizeRatio(mantissa, exp);
return tickFromNormalizedRatio(mantissa,normalized_exp);
}
/* ### low-level ratio → tick */
/* Given `ratio`, return greatest tick `t` such that `ratioFromTick(t) <= ratio`.
* Input ratio must be within the maximum and minimum ratios returned by the available ticks.
* Does _not_ expected a normalized float.
The function works as follows:
* Approximate log2(ratio) to the 13th fractional digit.
* Following <a href="https://hackmd.io/@mangrovedao/HJvl21zla">https://hackmd.io/@mangrovedao/HJvl21zla</a>, obtain `tickLow` and `tickHigh` such that $\log_{1.0001}(ratio)$ is between them
* Return the highest one that yields a ratio below the input ratio.
*/
function tickFromNormalizedRatio(uint mantissa, uint exp) internal pure returns (Tick tick) {
if (floatLt(mantissa, exp, MIN_RATIO_MANTISSA, uint(MIN_RATIO_EXP))) {
revert("mgv/tickFromRatio/tooLow");
}
if (floatLt(MAX_RATIO_MANTISSA, uint(MAX_RATIO_EXP), mantissa, exp)) {
revert("mgv/tickFromRatio/tooHigh");
}
int log2ratio = int(MANTISSA_BITS_MINUS_ONE) - int(exp) << 64;
uint mpow = mantissa >> MANTISSA_BITS_MINUS_ONE - 127; // give 129 bits of room left
/* How the fractional digits of the log are computed:
* for a given `n` compute $n^2$.
* If $\lfloor\log_2(n^2)\rfloor = 2\lfloor\log_2(n)\rfloor$ then the fractional part of $\log_2(n^2)$ was $< 0.5$ (first digit is 0).
* If $\lfloor\log_2(n^2)\rfloor = 1 + 2\lfloor\log_2(n)\rfloor$ then the fractional part of $\log_2(n^2)$ was $\geq 0.5$ (first digit is 1).
* Apply starting with `n=mpow` repeatedly by keeping `n` on 127 bits through right-shifts (has no impact on high fractional bits).
*/
assembly ("memory-safe") {
// 13 bits of precision
mpow := shr(127, mul(mpow, mpow))
let highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(63, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(62, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(61, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(60, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(59, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(58, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(57, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(56, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(55, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(54, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(53, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(52, highbit))
mpow := shr(highbit, mpow)
mpow := shr(127, mul(mpow, mpow))
highbit := shr(128, mpow)
log2ratio := or(log2ratio, shl(51, highbit))
}
// Convert log base 2 to log base 1.0001 (multiply by `log2(1.0001)^-1 << 64`), since log2ratio is x64 this yields a x128 number.
int log_bp_ratio = log2ratio * 127869479499801913173571;
// tickLow is approx - maximum error
int tickLow = int((log_bp_ratio - 1701496478404567508395759362389778998) >> 128);
// tickHigh is approx + minimum error
int tickHigh = int((log_bp_ratio + 289637967442836606107396900709005211253) >> 128);
(uint mantissaHigh, uint expHigh) = ratioFromTick(Tick.wrap(tickHigh));
bool ratioHighGt = floatLt(mantissa, exp, mantissaHigh, expHigh);
if (tickLow == tickHigh || ratioHighGt) {
tick = Tick.wrap(tickLow);
} else {
tick = Tick.wrap(tickHigh);
}
}
/* ### tick → ratio conversion function */
/* Returns a normalized (man,exp) ratio floating-point number. The mantissa is on 128 bits to avoid overflow when mulitplying with token amounts. The exponent has no bias. for easy comparison. */
function ratioFromTick(Tick tick) internal pure returns (uint man, uint exp) {
unchecked {
(man, exp) = nonNormalizedRatioFromTick(tick);
int shiftedTick = Tick.unwrap(tick) << LOG_BP_SHIFT;
int log2ratio;
// floor log2 of ratio towards negative infinity
assembly ("memory-safe") {
log2ratio := sdiv(shiftedTick,LOG_BP_2X235)
log2ratio := sub(log2ratio,slt(smod(shiftedTick,LOG_BP_2X235),0))
}
int diff = log2ratio+int(exp)-int(MANTISSA_BITS_MINUS_ONE);
if (diff > 0) {
// For |tick| <= 887272, this drops at most 5 bits of precision
man = man >> uint(diff);
} else {
man = man << uint(-diff);
}
// For |tick| << 887272, log2ratio <= 127
exp = uint(int(MANTISSA_BITS_MINUS_ONE)-log2ratio);
}
}
/* ### low-level tick → ratio conversion */
/* Compute 1.0001^tick and returns it as a (mantissa,exponent) pair. Works by checking each set bit of `|tick|` multiplying by `1.0001^(-2**i)<<128` if the ith bit of tick is set. Since we inspect the absolute value of `tick`, `-1048576` is not a valid tick. If the tick is positive this computes `1.0001^-tick`, and we take the inverse at the end. For maximum precision some powers of 1.0001 are shifted until they occupy 128 bits. The `extra_shift` is recorded and added to the exponent.
Since the resulting mantissa is left-shifted by 128 bits, if tick was positive, we divide `2**256` by the mantissa to get the 128-bit left-shifted inverse of the mantissa.
The error (relative to 1.0001^tick) may be negative or positive.
*/
function nonNormalizedRatioFromTick(Tick tick) internal pure returns (uint man, uint exp) {
uint absTick = Tick.unwrap(tick) < 0 ? uint(-Tick.unwrap(tick)) : uint(Tick.unwrap(tick));
require(absTick <= uint(MAX_TICK), "mgv/absTick/outOfBounds");
int extra_shift;
if (absTick & 0x1 != 0) {
man = 0xfff97272373d413259a46990580e2139;
} else {
man = 0x100000000000000000000000000000000;
}
if (absTick & 0x2 != 0) {
man = (man * 0xfff2e50f5f656932ef12357cf3c7fdcb) >> 128;
}
if (absTick & 0x4 != 0) {
man = (man * 0xffe5caca7e10e4e61c3624eaa0941ccf) >> 128;
}
if (absTick & 0x8 != 0) {
man = (man * 0xffcb9843d60f6159c9db58835c926643) >> 128;
}
if (absTick & 0x10 != 0) {
man = (man * 0xff973b41fa98c081472e6896dfb254bf) >> 128;
}
if (absTick & 0x20 != 0) {
man = (man * 0xff2ea16466c96a3843ec78b326b52860) >> 128;
}
if (absTick & 0x40 != 0) {
man = (man * 0xfe5dee046a99a2a811c461f1969c3052) >> 128;
}
if (absTick & 0x80 != 0) {
man = (man * 0xfcbe86c7900a88aedcffc83b479aa3a3) >> 128;
}
if (absTick & 0x100 != 0) {
man = (man * 0xf987a7253ac413176f2b074cf7815e53) >> 128;
}
if (absTick & 0x200 != 0) {
man = (man * 0xf3392b0822b70005940c7a398e4b70f2) >> 128;
}
if (absTick & 0x400 != 0) {
man = (man * 0xe7159475a2c29b7443b29c7fa6e889d8) >> 128;
}
if (absTick & 0x800 != 0) {
man = (man * 0xd097f3bdfd2022b8845ad8f792aa5825) >> 128;
}
if (absTick & 0x1000 != 0) {
man = (man * 0xa9f746462d870fdf8a65dc1f90e061e4) >> 128;
}
if (absTick & 0x2000 != 0) {
man = (man * 0xe1b0d342ada5437121767bec575e65ed) >> 128;
extra_shift += 1;
}
if (absTick & 0x4000 != 0) {
man = (man * 0xc6f84d7e5f423f66048c541550bf3e96) >> 128;
extra_shift += 2;
}
if (absTick & 0x8000 != 0) {
man = (man * 0x9aa508b5b7a84e1c677de54f3e99bc8f) >> 128;
extra_shift += 4;
}
if (absTick & 0x10000 != 0) {
man = (man * 0xbad5f1bdb70232cd33865244bdcc089c) >> 128;
extra_shift += 9;
}
if (absTick & 0x20000 != 0) {
man = (man * 0x885b9613d7e87aa498106fb7fa5edd37) >> 128;
extra_shift += 18;
}
if (absTick & 0x40000 != 0) {
man = (man * 0x9142e0723efb884889d1f447715afacd) >> 128;
extra_shift += 37;
}
if (absTick & 0x80000 != 0) {
man = (man * 0xa4d9a773d61316918f140bd96e8e6814) >> 128;
extra_shift += 75;
}
if (Tick.unwrap(tick) > 0) {
/* We use [Remco Bloemen's trick](https://xn--2-umb.com/17/512-bit-division/#divide-2-256-by-a-given-number) to divide `2**256` by `man`: */
assembly("memory-safe") {
man := add(div(sub(0, man), man), 1)
}
extra_shift = -extra_shift;
}
exp = uint(128 + extra_shift);
}
/* Shift mantissa so it occupies exactly `MANTISSA_BITS` and adjust `exp` in consequence.
A float is normalized when its mantissa occupies exactly 128 bits. All in-range normalized floats have `exp >= 0`, so we can use a `uint` for exponents everywhere we expect a normalized float.
When a non-normalized float is expected/used, `exp` can be negative since there is no constraint on the size of the mantissa.
*/
function normalizeRatio(uint mantissa, int exp) internal pure returns (uint, uint) {
require(mantissa != 0,"mgv/normalizeRatio/mantissaIs0");
uint log2ratio = BitLib.fls(mantissa);
int shift = int(MANTISSA_BITS_MINUS_ONE) - int(log2ratio);
if (shift < 0) {
mantissa = mantissa >> uint(-shift);
} else {
mantissa = mantissa << uint(shift);
}
exp = exp + shift;
if (exp < 0) {
revert("mgv/normalizeRatio/lowExp");
}
return (mantissa,uint(exp));
}
/* Return `a/(2**e)` rounded up */
function divExpUp(uint a, uint e) internal pure returns (uint) {
unchecked {
uint rem;
/*
Let mask be `(1<<e)-1`, `rem` is 1 if `a & mask > 0`, and 0 otherwise.
Explanation:
* if a is 0 then `rem` must be 0. `0 & mask` is 0.
* else if `e > 255` then `0 < a < 2^e`, so `rem` must be 1. `(1<<e)-1` is `type(uint).max`, so `a & mask is a > 0`.
* else `a & mask` is `a % 2**e`
*/
assembly("memory-safe") {
rem := gt(and(a,sub(shl(e,1),1)),0)
}
return (a>>e) + rem;
}
}
/* Floats are normalized to 128 bits to ensure no overflow when multiplying with amounts, and for easier comparisons. Normalized in-range floats have `exp>=0`. */
function floatLt(uint mantissa_a, uint exp_a, uint mantissa_b, uint exp_b) internal pure returns (bool) {
/* Exponents are negated (so that exponents of ratios within the accepted range as >= 0, which simplifies the code), which explains the direction of the `exp_a > exp_b` comparison. */
return (exp_a > exp_b || (exp_a == exp_b && mantissa_a < mantissa_b));
}
}
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.17;
import "@mgv/lib/core/Constants.sol";
import {Tick} from "@mgv/lib/core/TickLib.sol";
import {BitLib} from "@mgv/lib/core/BitLib.sol";
import {console2 as csf} from "@mgv/forge-std/console2.sol";
import {Local} from "@mgv/src/preprocessed/Local.post.sol";
/* # Libraries for tick tree manipulation
Offers in Mangrove are structured in a tree so that offer insertion, removal, and update can happen in constant time.
The tree has the following structure: nodes at height 0, 1, 2 and 3 are bit fields (type `Field`) and nodes at height 4 (leaves) are arrays of pairs of offers (type `Leaf`).
- The root node is a 2-bit `Field` and has 2 child nodes.
- The nodes below are called `level1` nodes and are 64-bit `Field`s, with 64 child nodes each.
- The nodes below are called `level2` nodes and are 64-bit `Field`s with 64 child nodes each.
- The nodes below are called `level3` nodes and are 64-bit `Field`s with 64 child nodes each.
- The nodes below are called `leaf` nodes and are arrays of pairs of offers. Each pair of offers represents the first and last offer of a bin.
- Bins are linked lists of offers.
For each field, the i-th bit (starting from least significant) is set if there is at least one bin holding offers below the i-th child of the field. */
/* Globally enable `leaf.method(...)` */
type Leaf is uint;
using LeafLib for Leaf global;
/* Each `Leaf` holds information about 4 bins as follows: `[firstId,lastId] [firstId,lastId] [firstId,lastId] [firstId,lastId]`. For each bin `firstId` is used by `marketOrder` to start consuming offers in the bin (each offer contains a pointer to the next offer in the bin, until `lastId` is reacher). `lastId` is used when inserting offers in the bin: the newly inserted offer replaces the last offer.
Each `leaf` has an `index`: it is the number of leaves before it.
*/
library LeafLib {
Leaf constant EMPTY = Leaf.wrap(uint(0));
/* Checks if a leaf is dirty or not (see below for more on dirty leaves). */
function dirty(Leaf leaf) internal pure returns (DirtyLeaf) {
unchecked {
assembly ("memory-safe") {
leaf := or(iszero(leaf),leaf)
}
return DirtyLeaf.wrap(Leaf.unwrap(leaf));
}
}
function eq(Leaf leaf1, Leaf leaf2) internal pure returns (bool) {
unchecked {
return Leaf.unwrap(leaf1) == Leaf.unwrap(leaf2);
}
}
function isEmpty(Leaf leaf) internal pure returns (bool) {
unchecked {
return Leaf.unwrap(leaf) == Leaf.unwrap(EMPTY);
}
}
/* `bool -> int` cast */
function uint_of_bool(bool b) internal pure returns (uint u) {
unchecked {
assembly ("memory-safe") {
u := b
}
}
}
/* Set either tick's first (at pos*size) or last (at pos*size + 1) */
function setPosFirstOrLast(Leaf leaf, uint pos, uint id, bool last) internal pure returns (Leaf) {
unchecked {
uint before = OFFER_BITS * ((pos * 2) + uint_of_bool(last));
uint cleanupMask = ~(OFFER_MASK << (256 - OFFER_BITS - before));
uint shiftedId = id << (256 - OFFER_BITS - before);
uint newLeaf = Leaf.unwrap(leaf) & cleanupMask | shiftedId;
return Leaf.wrap(newLeaf);
}
}
/* Assume `leaf` is the leaf of `bin`. Set the first offer of `bin` in leaf to `id`. */
function setBinFirst(Leaf leaf, Bin bin, uint id) internal pure returns (Leaf) {
unchecked {
uint posInLeaf = bin.posInLeaf();
return setPosFirstOrLast(leaf, posInLeaf, id, false);
}
}
/* Assume `leaf` is the leaf of `bin`. Set the last offer of `bin` in leaf to `id`. */
function setBinLast(Leaf leaf, Bin bin, uint id) internal pure returns (Leaf) {
unchecked {
uint posInLeaf = bin.posInLeaf();
return setPosFirstOrLast(leaf, posInLeaf, id, true);
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Assumes `leaf` is the leaf of `bin`. Erase contents of bins in `leaf` to (not including) `bin`. */
function eraseBelow(Leaf leaf, Bin bin) internal pure returns (Leaf) {
unchecked {
uint mask = ONES >> ((bin.posInLeaf() + 1) * OFFER_BITS * 2);
return Leaf.wrap(Leaf.unwrap(leaf) & mask);
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Assumes `leaf` is the leaf of `bin`. Erase contents of bins in `leaf` from (not including) `bin`. */
function eraseAbove(Leaf leaf, Bin bin) internal pure returns (Leaf) {
unchecked {
uint mask = ~(ONES >> (bin.posInLeaf() * OFFER_BITS * 2));
return Leaf.wrap(Leaf.unwrap(leaf) & mask);
}
}
// _This function is not used by Mangrove but is part of the library._
/* Return the id of the first offer in `bin`, assuming `bin`'s leaf is `leaf`. */
function firstOfBin(Leaf leaf, Bin bin) internal pure returns (uint) {
unchecked {
return firstOfPos(leaf, bin.posInLeaf());
}
}
/* Return the id of the last offer in `bin`, assuming `bin`'s leaf is `leaf`. */
function lastOfBin(Leaf leaf, Bin bin) internal pure returns (uint) {
unchecked {
return lastOfPos(leaf, bin.posInLeaf());
}
}
/* Return first offer of pair in position `pos` */
function firstOfPos(Leaf leaf, uint pos) internal pure returns (uint) {
unchecked {
uint raw = Leaf.unwrap(leaf);
return uint(raw << (pos * OFFER_BITS * 2) >> (256 - OFFER_BITS));
}
}
/* Return second offer of pair in position `pos` */
function lastOfPos(Leaf leaf, uint pos) internal pure returns (uint) {
unchecked {
uint raw = Leaf.unwrap(leaf);
return uint(raw << (OFFER_BITS * ((pos * 2) + 1)) >> (256 - OFFER_BITS));
}
}
/* Return the position of the first pair of `leaf` (0, 1, 2 or 3) that has a nonzero offer.
Explanation:
Note that unlike in fields, have their low bin on the most significant bits.
`pos` is initially 1 if `leaf` has some nonzero bit in its MSB half, 0 otherwise. Then `pos` is `A | B`, where `A` is `iszero(pos)<<1`, so `A` is 0 if leaf has some nonzero bit in its MSB half, 2 otherwise. And `B` is 0 if `leaf >> (pos << 7)` has some nonzero bit in its most significant 192 bits, 1 otherwise.
*/
function bestNonEmptyBinPos(Leaf leaf) internal pure returns (uint pos) {
assembly("memory-safe") {
pos := gt(leaf,0xffffffffffffffffffffffffffffffff)
pos := or(shl(1,iszero(pos)),iszero(gt(shr(shl(7,pos),leaf),0xffffffffffffffff)))
}
}
/* Return the offer id of the first offer of the first non-empty pair in `leaf`. */
function bestOfferId(Leaf leaf) internal pure returns (uint offerId) {
unchecked {
return firstOfPos(leaf,bestNonEmptyBinPos(leaf));
}
}
}
/* Bins are numbered from MIN_BIN to MAX_BIN (inclusive). Each bin contains the offers at a given price. For a given `tickSpacing`, bins represent the following prices (centered on the central bin):
```
...
1.0001^-(tickSpacing*2)
1.0001^-(tickSpacing*1)
1.0001
1.0001^(tickSpacing*1)
1.0001^(tickSpacing*2)
...
```
There are 4 bins per leaf, `4 * 64` bins per level3, etc. The leaf of a bin is the leaf that holds its first/last offer id. The level3 of a bin is the level3 field above its leaf; the level2 of a bin is the level2 field above its level3, etc. */
/* Globally enable `bin.method(...)` */
type Bin is int;
using TickTreeLib for Bin global;
library TickTreeLib {
function eq(Bin bin1, Bin bin2) internal pure returns (bool) {
unchecked {
return Bin.unwrap(bin1) == Bin.unwrap(bin2);
}
}
function inRange(Bin bin) internal pure returns (bool) {
unchecked {
return Bin.unwrap(bin) >= MIN_BIN && Bin.unwrap(bin) <= MAX_BIN;
}
}
// _This function is not used by Mangrove but is part of the library for convenience._
/* Not optimized for gas. Returns the bin induced by the branch formed by the arguments. Returned value will make no sense if any of the `Field` arguments are empty. */
function bestBinFromBranch(uint binPosInLeaf,Field level3, Field level2, Field level1, Field root) internal pure returns (Bin) {
unchecked {
Local local;
local = local.binPosInLeaf(binPosInLeaf).level3(level3).level2(level2).level1(level1).root(root);
return bestBinFromLocal(local);
}
}
/* Returns tick held by the `bin`, given a `tickSpacing`. */
function tick(Bin bin, uint tickSpacing) internal pure returns (Tick) {
return Tick.wrap(Bin.unwrap(bin) * int(tickSpacing));
}
/* Returns the bin induced by the branch held in `local`. Returned value will make no sense if any of the fields of `local` are empty. */
function bestBinFromLocal(Local local) internal pure returns (Bin) {
unchecked {
uint ubin = local.binPosInLeaf() |
((BitLib.ctz64(Field.unwrap(local.level3())) |
(BitLib.ctz64(Field.unwrap(local.level2())) |
(BitLib.ctz64(Field.unwrap(local.level1())) |
uint(
(int(BitLib.ctz64(Field.unwrap(local.root())))-ROOT_SIZE/2) << LEVEL_SIZE_BITS))
<< LEVEL_SIZE_BITS)
<< LEVEL_SIZE_BITS)
<< LEAF_SIZE_BITS);
return Bin.wrap(int(ubin));
}
}
/* Returns the index of the leaf that holds `bin` */
function leafIndex(Bin bin) internal pure returns (int) {
unchecked {
return Bin.unwrap(bin) >> LEAF_SIZE_BITS;
}
}
/* Returns the position of `bin` in its leaf. */
function posInLeaf(Bin bin) internal pure returns (uint) {
unchecked {
return uint(Bin.unwrap(bin)) & LEAF_SIZE_MASK;
}
}
/* Returns the index of `bin`'s level3 */
function level3Index(Bin bin) internal pure returns (int) {
unchecked {
return Bin.unwrap(bin) >> (LEAF_SIZE_BITS + LEVEL_SIZE_BITS);
}
}
/* Returns the position of `bin`'s leaf in `bin`'s level3 */
function posInLevel3(Bin bin) internal pure returns (uint) {
unchecked {
return uint(bin.leafIndex()) & LEVEL_SIZE_MASK;
}
}
/* Returns the index of `bin`'s level2 */
function level2Index(Bin bin) internal pure returns (int) {
unchecked {
return Bin.unwrap(bin) >> (LEAF_SIZE_BITS + 2* LEVEL_SIZE_BITS);
}
}
/* Returns the position of `bin`'s level3 in `bin`'s level2 */
function posInLevel2(Bin bin) internal pure returns (uint) {
unchecked {
return uint(bin.level3Index()) & LEVEL_SIZE_MASK;
}
}
/* Returns the index of `bin`'s level1 */
function level1Index(Bin bin) internal pure returns (int) {
unchecked {
return Bin.unwrap(bin) >> (LEAF_SIZE_BITS + 3 * LEVEL_SIZE_BITS);
}
}
/* Returns the position of `bin`'s level2 in `bin`'s level1 */
function posInLevel1(Bin bin) internal pure returns (uint) {
unchecked {
return uint(bin.level2Index()) & LEVEL_SIZE_MASK;
}
}
/* Returns the position of `bin`'s level1 in root */
function posInRoot(Bin bin) internal pure returns (uint) {
unchecked {
return uint(bin.level1Index() + ROOT_SIZE / 2);
}
}
/* `<`, typed for bin */
function strictlyBetter(Bin bin1, Bin bin2) internal pure returns (bool) {
unchecked {
return Bin.unwrap(bin1) < Bin.unwrap(bin2);
}
}
/* `<=`, typed for bin */
function better(Bin bin1, Bin bin2) internal pure returns (bool) {
unchecked {
return Bin.unwrap(bin1) <= Bin.unwrap(bin2);
}
}
}
/* Globally enable `field.method(...)` */
type Field is uint;
using FieldLib for Field global;
/* Fields are bit fields. Each bit position of a field corresponds to a child of the node in the tick tree. The i-th bit of a field is set iff its child is the parent of at least one non-emtpy field.
Using bit fields market orders can locate the next bin containing an offer in constant time: once a leaf has been emptied, one must at most walk up along the branch of the current bin, up to the first 1 in a field, then go down to the of the tree, then go down the corresponding child to the nonempty bin found.
*/
/* In fields, positions are counted from the least significant bits */
library FieldLib {
Field constant EMPTY = Field.wrap(uint(0));
/* Checks if a field is dirty or not (see below for more on dirty fields). */
function dirty(Field field) internal pure returns (DirtyField) {
unchecked {
assembly ("memory-safe") {
field := or(TOPBIT,field)
}
return DirtyField.wrap(Field.unwrap(field));
}
}
function eq(Field field1, Field field2) internal pure returns (bool) {
unchecked {
return Field.unwrap(field1) == Field.unwrap(field2);
}
}
function isEmpty(Field field) internal pure returns (bool) {
unchecked {
return Field.unwrap(field) == Field.unwrap(EMPTY);
}
}
/* Flip the bit at the position of `bin`'s leaf */
function flipBitAtLevel3(Field level3, Bin bin) internal pure returns (Field) {
unchecked {
uint pos = bin.posInLevel3();
level3 = Field.wrap(Field.unwrap(level3) ^ (1 << pos));
return level3;
}
}
/* Flip the bit at the position of `bin`'s level3 */
function flipBitAtLevel2(Field level2, Bin bin) internal pure returns (Field) {
unchecked {
uint pos = bin.posInLevel2();
level2 = Field.wrap(Field.unwrap(level2) ^ (1 << pos));
return level2;
}
}
/* Flip the bit at the position of `bin`'s level2 */
function flipBitAtLevel1(Field level1, Bin bin) internal pure returns (Field) {
unchecked {
uint pos = bin.posInLevel1();
level1 = Field.wrap(Field.unwrap(level1) ^ (1 << pos));
return level1;
}
}
/* Flip the bit at the position of `bin`'s level1 */
function flipBitAtRoot(Field root, Bin bin) internal pure returns (Field) {
unchecked {
uint pos = bin.posInRoot();
root = Field.wrap(Field.unwrap(root) ^ (1 << pos));
return root;
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Assumes `field` is the level3 of `bin`. Erase contents of field up to (not including) bin. */
function eraseBelowInLevel3(Field field, Bin bin) internal pure returns (Field) {
unchecked {
uint mask = ONES << (bin.posInLevel3() + 1);
return Field.wrap(Field.unwrap(field) & mask);
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Assumes `field` is the level3 of `bin`. Erase contents of field from (not including) bin. */
function eraseAboveInLevel3(Field field, Bin bin) internal pure returns (Field) {
unchecked {
uint mask = ~(ONES << bin.posInLevel3());
return Field.wrap(Field.unwrap(field) & mask);
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Assumes `field` is the level2 of `bin`. Erase contents of field up to (not including) bin. */
function eraseBelowInLevel2(Field field, Bin bin) internal pure returns (Field) {
unchecked {
uint mask = ONES << (bin.posInLevel2() + 1);
return Field.wrap(Field.unwrap(field) & mask);
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Assumes `field` is the level2 of `bin`. Erase contents of field from (not including) bin. */
function eraseAboveInLevel2(Field field, Bin bin) internal pure returns (Field) {
unchecked {
uint mask = ~(ONES << bin.posInLevel2());
return Field.wrap(Field.unwrap(field) & mask);
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Assumes `field` is the level1 of `bin`. Erase contents of field up to (not including) bin. */
function eraseBelowInLevel1(Field field, Bin bin) internal pure returns (Field) {
unchecked {
uint mask = ONES << (bin.posInLevel1() + 1);
return Field.wrap(Field.unwrap(field) & mask);
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Assumes `field` is the level1 of `bin`. Erase contents of field from (not including) bin. */
function eraseAboveInLevel1(Field field, Bin bin) internal pure returns (Field) {
unchecked {
uint mask = ~(ONES << bin.posInLevel1());
return Field.wrap(Field.unwrap(field) & mask);
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Assumes `field` is the root of `bin`. Erase contents of field up to (not including) bin. */
function eraseBelowInRoot(Field field, Bin bin) internal pure returns (Field) {
unchecked {
uint mask = ONES << (bin.posInRoot() + 1);
return Field.wrap(Field.unwrap(field) & mask);
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Assumes `field` is the root of `bin`. Erase contents of field from (not including) bin. */
function eraseAboveInRoot(Field field, Bin bin) internal pure returns (Field) {
unchecked {
uint mask = ~(ONES << bin.posInRoot());
return Field.wrap(Field.unwrap(field) & mask);
}
}
/* Returns first nonzero position of `field`. Will return 64 if field is empty */
function firstOnePosition(Field field) internal pure returns (uint) {
unchecked {
return BitLib.ctz64(Field.unwrap(field));
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Returns last nonzero position of `field`. Will return 256 if field is empty */
function lastOnePosition(Field field) internal pure returns (uint) {
unchecked {
return BitLib.fls(Field.unwrap(field));
}
}
/* Return index of the first nonempty level1 below `root` (`root` should not be empty) */
function firstLevel1Index(Field root) internal pure returns (int) {
unchecked {
return int(root.firstOnePosition()) - ROOT_SIZE / 2;
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Return index of the last nonempty level1 below `root` (root should not be empty) */
function lastLevel1Index(Field root) internal pure returns (int) {
unchecked {
return int(root.lastOnePosition()) - ROOT_SIZE / 2;
}
}
/* Return index of the first nonempty level2 below `level1` assuming its index is `level1Index` (`level1` should not be empty). */
function firstLevel2Index(Field level1, int level1Index) internal pure returns (int) {
unchecked {
return level1Index * LEVEL_SIZE + int(level1.firstOnePosition());
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Return index of the last nonempty level2 below `level1` assuming its index is `level1Index` (`level1` should not be empty). */
function lastLevel2Index(Field level1, int level1Index) internal pure returns (int) {
unchecked {
return level1Index * LEVEL_SIZE + int(level1.lastOnePosition());
}
}
/* Return index of the first nonempty level3 below `level2` assuming its index is `level2Index` (`level2` should not be empty). */
function firstLevel3Index(Field level2, int level2Index) internal pure returns (int) {
unchecked {
return level2Index * LEVEL_SIZE + int(level2.firstOnePosition());
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Return index of the last nonempty level3 below `level2` assuming its index is `level2Index` (`level2` should not be empty). */
function lastLevel3Index(Field level2, int level2Index) internal pure returns (int) {
unchecked {
return level2Index * LEVEL_SIZE + int(level2.lastOnePosition());
}
}
/* Return index of the first nonempty leaf below `level3` assuming its index is `level3Index` (`level3` should not be empty). */
function firstLeafIndex(Field level3, int level3Index) internal pure returns (int) {
unchecked {
return level3Index * LEVEL_SIZE + int(level3.firstOnePosition());
}
}
// _This function is not used by Mangrove but is useful for MgvReader._
/* Return index of the last nonempty leaf below `level3` assuming its index is `level3Index` (`level3` should not be empty). */
function lastLeafIndex(Field level3, int level3Index) internal pure returns (int) {
unchecked {
return level3Index * LEVEL_SIZE + int(level3.lastOnePosition());
}
}
}
/* ## Clean/Dirty Fields and Leaves
To save gas, leaves and fields at never zeroed out but written with a dirty bit. This is especially helpful when the price oscillates quickly between two nodes.
Leaves don't have 'available bits' so an empty leaf is encoded as 1. This is not a valid leaf because for every offer pair in a leaf, either both are 0 (the bin is empty) or both are nonzero (they are the same if the bin has a single offer).
*/
/* Globally enable `dirtyLeaf.method(...)` */
type DirtyLeaf is uint;
using DirtyLeafLib for DirtyLeaf global;
library DirtyLeafLib {
/* Return 0 if leaf is 1, leaf otherwise */
function clean(DirtyLeaf leaf) internal pure returns (Leaf) {
unchecked {
assembly ("memory-safe") {
leaf := xor(eq(leaf,ONE),leaf)
}
return Leaf.wrap(DirtyLeaf.unwrap(leaf));
}
}
function isDirty(DirtyLeaf leaf) internal pure returns (bool) {
unchecked {
return DirtyLeaf.unwrap(leaf) == ONE;
}
}
function eq(DirtyLeaf leaf1, DirtyLeaf leaf2) internal pure returns (bool) {
unchecked {
return DirtyLeaf.unwrap(leaf1) == DirtyLeaf.unwrap(leaf2);
}
}
}
/* We use `TOPBIT` as the optimized in-storage value since 1 is a valid Field value */
/* For fields, it's simpler, since they do not use 64 bits we store the word with top bit at 1 and everything else at 0 to mark emptiness. */
/* Globally enable `dirtyField.method(...)` */
type DirtyField is uint;
using DirtyFieldLib for DirtyField global;
library DirtyFieldLib {
DirtyField constant DIRTY_EMPTY = DirtyField.wrap(TOPBIT);
DirtyField constant CLEAN_EMPTY = DirtyField.wrap(0);
/* Return clean field with topbit set to 0 */
function clean(DirtyField field) internal pure returns (Field) {
unchecked {
assembly ("memory-safe") {
field := and(NOT_TOPBIT,field)
}
return Field.wrap(DirtyField.unwrap(field));
}
}
function isDirty(DirtyField field) internal pure returns (bool) {
unchecked {
return DirtyField.unwrap(field) & TOPBIT == TOPBIT;
}
}
function eq(DirtyField leaf1, DirtyField leaf2) internal pure returns (bool) {
unchecked {
return DirtyField.unwrap(leaf1) == DirtyField.unwrap(leaf2);
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
/// @dev The original console.sol uses `int` and `uint` for computing function selectors, but it should
/// use `int256` and `uint256`. This modified version fixes that. This version is recommended
/// over `console.sol` if you don't need compatibility with Hardhat as the logs will show up in
/// forge stack traces. If you do need compatibility with Hardhat, you must use `console.sol`.
/// Reference: https://github.com/NomicFoundation/hardhat/issues/2178
library console2 {
address constant CONSOLE_ADDRESS = address(0x000000000000000000636F6e736F6c652e6c6f67);
function _castLogPayloadViewToPure(
function(bytes memory) internal view fnIn
) internal pure returns (function(bytes memory) internal pure fnOut) {
assembly {
fnOut := fnIn
}
}
function _sendLogPayload(bytes memory payload) internal pure {
_castLogPayloadViewToPure(_sendLogPayloadView)(payload);
}
function _sendLogPayloadView(bytes memory payload) private view {
uint256 payloadLength = payload.length;
address consoleAddress = CONSOLE_ADDRESS;
/// @solidity memory-safe-assembly
assembly {
let payloadStart := add(payload, 32)
let r := staticcall(gas(), consoleAddress, payloadStart, payloadLength, 0, 0)
}
}
function log() internal pure {
_sendLogPayload(abi.encodeWithSignature("log()"));
}
function logInt(int256 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(int256)", p0));
}
function logUint(uint256 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256)", p0));
}
function logString(string memory p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string)", p0));
}
function logBool(bool p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool)", p0));
}
function logAddress(address p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address)", p0));
}
function logBytes(bytes memory p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes)", p0));
}
function logBytes1(bytes1 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes1)", p0));
}
function logBytes2(bytes2 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes2)", p0));
}
function logBytes3(bytes3 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes3)", p0));
}
function logBytes4(bytes4 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes4)", p0));
}
function logBytes5(bytes5 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes5)", p0));
}
function logBytes6(bytes6 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes6)", p0));
}
function logBytes7(bytes7 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes7)", p0));
}
function logBytes8(bytes8 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes8)", p0));
}
function logBytes9(bytes9 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes9)", p0));
}
function logBytes10(bytes10 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes10)", p0));
}
function logBytes11(bytes11 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes11)", p0));
}
function logBytes12(bytes12 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes12)", p0));
}
function logBytes13(bytes13 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes13)", p0));
}
function logBytes14(bytes14 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes14)", p0));
}
function logBytes15(bytes15 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes15)", p0));
}
function logBytes16(bytes16 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes16)", p0));
}
function logBytes17(bytes17 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes17)", p0));
}
function logBytes18(bytes18 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes18)", p0));
}
function logBytes19(bytes19 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes19)", p0));
}
function logBytes20(bytes20 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes20)", p0));
}
function logBytes21(bytes21 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes21)", p0));
}
function logBytes22(bytes22 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes22)", p0));
}
function logBytes23(bytes23 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes23)", p0));
}
function logBytes24(bytes24 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes24)", p0));
}
function logBytes25(bytes25 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes25)", p0));
}
function logBytes26(bytes26 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes26)", p0));
}
function logBytes27(bytes27 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes27)", p0));
}
function logBytes28(bytes28 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes28)", p0));
}
function logBytes29(bytes29 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes29)", p0));
}
function logBytes30(bytes30 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes30)", p0));
}
function logBytes31(bytes31 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes31)", p0));
}
function logBytes32(bytes32 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bytes32)", p0));
}
function log(uint256 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256)", p0));
}
function log(int256 p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(int256)", p0));
}
function log(string memory p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string)", p0));
}
function log(bool p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool)", p0));
}
function log(address p0) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address)", p0));
}
function log(uint256 p0, uint256 p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256)", p0, p1));
}
function log(uint256 p0, string memory p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string)", p0, p1));
}
function log(uint256 p0, bool p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool)", p0, p1));
}
function log(uint256 p0, address p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address)", p0, p1));
}
function log(string memory p0, uint256 p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256)", p0, p1));
}
function log(string memory p0, int256 p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,int256)", p0, p1));
}
function log(string memory p0, string memory p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string)", p0, p1));
}
function log(string memory p0, bool p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool)", p0, p1));
}
function log(string memory p0, address p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address)", p0, p1));
}
function log(bool p0, uint256 p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256)", p0, p1));
}
function log(bool p0, string memory p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string)", p0, p1));
}
function log(bool p0, bool p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool)", p0, p1));
}
function log(bool p0, address p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address)", p0, p1));
}
function log(address p0, uint256 p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256)", p0, p1));
}
function log(address p0, string memory p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string)", p0, p1));
}
function log(address p0, bool p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool)", p0, p1));
}
function log(address p0, address p1) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address)", p0, p1));
}
function log(uint256 p0, uint256 p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,uint256)", p0, p1, p2));
}
function log(uint256 p0, uint256 p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,string)", p0, p1, p2));
}
function log(uint256 p0, uint256 p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,bool)", p0, p1, p2));
}
function log(uint256 p0, uint256 p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,address)", p0, p1, p2));
}
function log(uint256 p0, string memory p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,uint256)", p0, p1, p2));
}
function log(uint256 p0, string memory p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,string)", p0, p1, p2));
}
function log(uint256 p0, string memory p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,bool)", p0, p1, p2));
}
function log(uint256 p0, string memory p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,address)", p0, p1, p2));
}
function log(uint256 p0, bool p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,uint256)", p0, p1, p2));
}
function log(uint256 p0, bool p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,string)", p0, p1, p2));
}
function log(uint256 p0, bool p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,bool)", p0, p1, p2));
}
function log(uint256 p0, bool p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,address)", p0, p1, p2));
}
function log(uint256 p0, address p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,uint256)", p0, p1, p2));
}
function log(uint256 p0, address p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,string)", p0, p1, p2));
}
function log(uint256 p0, address p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,bool)", p0, p1, p2));
}
function log(uint256 p0, address p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,address)", p0, p1, p2));
}
function log(string memory p0, uint256 p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,uint256)", p0, p1, p2));
}
function log(string memory p0, uint256 p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,string)", p0, p1, p2));
}
function log(string memory p0, uint256 p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,bool)", p0, p1, p2));
}
function log(string memory p0, uint256 p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,address)", p0, p1, p2));
}
function log(string memory p0, string memory p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,uint256)", p0, p1, p2));
}
function log(string memory p0, string memory p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,string)", p0, p1, p2));
}
function log(string memory p0, string memory p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,bool)", p0, p1, p2));
}
function log(string memory p0, string memory p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,address)", p0, p1, p2));
}
function log(string memory p0, bool p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,uint256)", p0, p1, p2));
}
function log(string memory p0, bool p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,string)", p0, p1, p2));
}
function log(string memory p0, bool p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,bool)", p0, p1, p2));
}
function log(string memory p0, bool p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,address)", p0, p1, p2));
}
function log(string memory p0, address p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,uint256)", p0, p1, p2));
}
function log(string memory p0, address p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,string)", p0, p1, p2));
}
function log(string memory p0, address p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,bool)", p0, p1, p2));
}
function log(string memory p0, address p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,address)", p0, p1, p2));
}
function log(bool p0, uint256 p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,uint256)", p0, p1, p2));
}
function log(bool p0, uint256 p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,string)", p0, p1, p2));
}
function log(bool p0, uint256 p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,bool)", p0, p1, p2));
}
function log(bool p0, uint256 p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,address)", p0, p1, p2));
}
function log(bool p0, string memory p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,uint256)", p0, p1, p2));
}
function log(bool p0, string memory p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,string)", p0, p1, p2));
}
function log(bool p0, string memory p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,bool)", p0, p1, p2));
}
function log(bool p0, string memory p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,address)", p0, p1, p2));
}
function log(bool p0, bool p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,uint256)", p0, p1, p2));
}
function log(bool p0, bool p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,string)", p0, p1, p2));
}
function log(bool p0, bool p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,bool)", p0, p1, p2));
}
function log(bool p0, bool p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,address)", p0, p1, p2));
}
function log(bool p0, address p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,uint256)", p0, p1, p2));
}
function log(bool p0, address p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,string)", p0, p1, p2));
}
function log(bool p0, address p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,bool)", p0, p1, p2));
}
function log(bool p0, address p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,address)", p0, p1, p2));
}
function log(address p0, uint256 p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,uint256)", p0, p1, p2));
}
function log(address p0, uint256 p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,string)", p0, p1, p2));
}
function log(address p0, uint256 p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,bool)", p0, p1, p2));
}
function log(address p0, uint256 p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,address)", p0, p1, p2));
}
function log(address p0, string memory p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,uint256)", p0, p1, p2));
}
function log(address p0, string memory p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,string)", p0, p1, p2));
}
function log(address p0, string memory p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,bool)", p0, p1, p2));
}
function log(address p0, string memory p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,address)", p0, p1, p2));
}
function log(address p0, bool p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,uint256)", p0, p1, p2));
}
function log(address p0, bool p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,string)", p0, p1, p2));
}
function log(address p0, bool p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,bool)", p0, p1, p2));
}
function log(address p0, bool p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,address)", p0, p1, p2));
}
function log(address p0, address p1, uint256 p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,uint256)", p0, p1, p2));
}
function log(address p0, address p1, string memory p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,string)", p0, p1, p2));
}
function log(address p0, address p1, bool p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,bool)", p0, p1, p2));
}
function log(address p0, address p1, address p2) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,address)", p0, p1, p2));
}
function log(uint256 p0, uint256 p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,uint256,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,uint256,string)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,uint256,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,uint256,address)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,string,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,string,string)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,string,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,string,address)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,bool,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,bool,string)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,bool,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,bool,address)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,address,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,address,string)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,address,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, uint256 p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,address,address)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,uint256,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,uint256,string)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,uint256,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,uint256,address)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,string,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,string,string)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,string,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,string,address)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,bool,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,bool,string)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,bool,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,bool,address)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,address,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,address,string)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,address,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, string memory p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,string,address,address)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,uint256,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,uint256,string)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,uint256,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,uint256,address)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,string,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,string,string)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,string,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,string,address)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,bool,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,bool,string)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,bool,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,bool,address)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,address,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,address,string)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,address,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, bool p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,bool,address,address)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,uint256,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,uint256,string)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,uint256,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,uint256,address)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,string,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,string,string)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,string,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,string,address)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,bool,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,bool,string)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,bool,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,bool,address)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,address,uint256)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,address,string)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,address,bool)", p0, p1, p2, p3));
}
function log(uint256 p0, address p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(uint256,address,address,address)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,uint256,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,uint256,string)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,uint256,bool)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,uint256,address)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,string,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,string,string)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,string,bool)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,string,address)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,bool,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,bool,string)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,bool,bool)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,bool,address)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,address,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,address,string)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,address,bool)", p0, p1, p2, p3));
}
function log(string memory p0, uint256 p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,uint256,address,address)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,uint256,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,uint256,string)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,uint256,bool)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,uint256,address)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,string,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,string,string)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,string,bool)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,string,address)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,bool,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,bool,string)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,bool,bool)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,bool,address)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,address,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,address,string)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,address,bool)", p0, p1, p2, p3));
}
function log(string memory p0, string memory p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,string,address,address)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,uint256,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,uint256,string)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,uint256,bool)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,uint256,address)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,string,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,string,string)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,string,bool)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,string,address)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,bool,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,bool,string)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,bool,bool)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,bool,address)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,address,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,address,string)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,address,bool)", p0, p1, p2, p3));
}
function log(string memory p0, bool p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,bool,address,address)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,uint256,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,uint256,string)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,uint256,bool)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,uint256,address)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,string,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,string,string)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,string,bool)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,string,address)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,bool,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,bool,string)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,bool,bool)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,bool,address)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,address,uint256)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,address,string)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,address,bool)", p0, p1, p2, p3));
}
function log(string memory p0, address p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(string,address,address,address)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,uint256,uint256)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,uint256,string)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,uint256,bool)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,uint256,address)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,string,uint256)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,string,string)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,string,bool)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,string,address)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,bool,uint256)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,bool,string)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,bool,bool)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,bool,address)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,address,uint256)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,address,string)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,address,bool)", p0, p1, p2, p3));
}
function log(bool p0, uint256 p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,uint256,address,address)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,uint256,uint256)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,uint256,string)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,uint256,bool)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,uint256,address)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,string,uint256)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,string,string)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,string,bool)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,string,address)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,bool,uint256)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,bool,string)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,bool,bool)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,bool,address)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,address,uint256)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,address,string)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,address,bool)", p0, p1, p2, p3));
}
function log(bool p0, string memory p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,string,address,address)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,uint256,uint256)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,uint256,string)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,uint256,bool)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,uint256,address)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,string,uint256)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,string,string)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,string,bool)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,string,address)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,bool,uint256)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,bool,string)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,bool,bool)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,bool,address)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,address,uint256)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,address,string)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,address,bool)", p0, p1, p2, p3));
}
function log(bool p0, bool p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,bool,address,address)", p0, p1, p2, p3));
}
function log(bool p0, address p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,uint256,uint256)", p0, p1, p2, p3));
}
function log(bool p0, address p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,uint256,string)", p0, p1, p2, p3));
}
function log(bool p0, address p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,uint256,bool)", p0, p1, p2, p3));
}
function log(bool p0, address p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,uint256,address)", p0, p1, p2, p3));
}
function log(bool p0, address p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,string,uint256)", p0, p1, p2, p3));
}
function log(bool p0, address p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,string,string)", p0, p1, p2, p3));
}
function log(bool p0, address p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,string,bool)", p0, p1, p2, p3));
}
function log(bool p0, address p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,string,address)", p0, p1, p2, p3));
}
function log(bool p0, address p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,bool,uint256)", p0, p1, p2, p3));
}
function log(bool p0, address p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,bool,string)", p0, p1, p2, p3));
}
function log(bool p0, address p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,bool,bool)", p0, p1, p2, p3));
}
function log(bool p0, address p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,bool,address)", p0, p1, p2, p3));
}
function log(bool p0, address p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,address,uint256)", p0, p1, p2, p3));
}
function log(bool p0, address p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,address,string)", p0, p1, p2, p3));
}
function log(bool p0, address p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,address,bool)", p0, p1, p2, p3));
}
function log(bool p0, address p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(bool,address,address,address)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,uint256,uint256)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,uint256,string)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,uint256,bool)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,uint256,address)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,string,uint256)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,string,string)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,string,bool)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,string,address)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,bool,uint256)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,bool,string)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,bool,bool)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,bool,address)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,address,uint256)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,address,string)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,address,bool)", p0, p1, p2, p3));
}
function log(address p0, uint256 p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,uint256,address,address)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,uint256,uint256)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,uint256,string)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,uint256,bool)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,uint256,address)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,string,uint256)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,string,string)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,string,bool)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,string,address)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,bool,uint256)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,bool,string)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,bool,bool)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,bool,address)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,address,uint256)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,address,string)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,address,bool)", p0, p1, p2, p3));
}
function log(address p0, string memory p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,string,address,address)", p0, p1, p2, p3));
}
function log(address p0, bool p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,uint256,uint256)", p0, p1, p2, p3));
}
function log(address p0, bool p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,uint256,string)", p0, p1, p2, p3));
}
function log(address p0, bool p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,uint256,bool)", p0, p1, p2, p3));
}
function log(address p0, bool p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,uint256,address)", p0, p1, p2, p3));
}
function log(address p0, bool p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,string,uint256)", p0, p1, p2, p3));
}
function log(address p0, bool p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,string,string)", p0, p1, p2, p3));
}
function log(address p0, bool p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,string,bool)", p0, p1, p2, p3));
}
function log(address p0, bool p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,string,address)", p0, p1, p2, p3));
}
function log(address p0, bool p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,bool,uint256)", p0, p1, p2, p3));
}
function log(address p0, bool p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,bool,string)", p0, p1, p2, p3));
}
function log(address p0, bool p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,bool,bool)", p0, p1, p2, p3));
}
function log(address p0, bool p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,bool,address)", p0, p1, p2, p3));
}
function log(address p0, bool p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,address,uint256)", p0, p1, p2, p3));
}
function log(address p0, bool p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,address,string)", p0, p1, p2, p3));
}
function log(address p0, bool p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,address,bool)", p0, p1, p2, p3));
}
function log(address p0, bool p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,bool,address,address)", p0, p1, p2, p3));
}
function log(address p0, address p1, uint256 p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,uint256,uint256)", p0, p1, p2, p3));
}
function log(address p0, address p1, uint256 p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,uint256,string)", p0, p1, p2, p3));
}
function log(address p0, address p1, uint256 p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,uint256,bool)", p0, p1, p2, p3));
}
function log(address p0, address p1, uint256 p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,uint256,address)", p0, p1, p2, p3));
}
function log(address p0, address p1, string memory p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,string,uint256)", p0, p1, p2, p3));
}
function log(address p0, address p1, string memory p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,string,string)", p0, p1, p2, p3));
}
function log(address p0, address p1, string memory p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,string,bool)", p0, p1, p2, p3));
}
function log(address p0, address p1, string memory p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,string,address)", p0, p1, p2, p3));
}
function log(address p0, address p1, bool p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,bool,uint256)", p0, p1, p2, p3));
}
function log(address p0, address p1, bool p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,bool,string)", p0, p1, p2, p3));
}
function log(address p0, address p1, bool p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,bool,bool)", p0, p1, p2, p3));
}
function log(address p0, address p1, bool p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,bool,address)", p0, p1, p2, p3));
}
function log(address p0, address p1, address p2, uint256 p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,address,uint256)", p0, p1, p2, p3));
}
function log(address p0, address p1, address p2, string memory p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,address,string)", p0, p1, p2, p3));
}
function log(address p0, address p1, address p2, bool p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,address,bool)", p0, p1, p2, p3));
}
function log(address p0, address p1, address p2, address p3) internal pure {
_sendLogPayload(abi.encodeWithSignature("log(address,address,address,address)", p0, p1, p2, p3));
}
}
{
"compilationTarget": {
"contracts/MangroveCleanerV2.sol": "MangroveCleanerV2"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": [
":@mgv/forge-std/=lib/forge-std/src/",
":@mgv/lib/=lib/mangrove-core/lib/",
":@mgv/script/=lib/mangrove-core/script/",
":@mgv/src/=lib/mangrove-core/src/",
":@mgv/test/=lib/mangrove-core/test/",
":core/=lib/mangrove-core/lib/core/",
":forge-std/=lib/forge-std/src/",
":mangrove-core/=lib/mangrove-core/src/",
":preprocessed/=lib/mangrove-core/lib/preprocessed/"
]
}
[{"inputs":[{"internalType":"contract IMangrove","name":"_mangrove","type":"address"},{"internalType":"contract MgvReader","name":"_mgvReader","type":"address"},{"internalType":"address","name":"_taker","type":"address"},{"internalType":"uint96","name":"_maxOfferPerCall","type":"uint96"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"components":[{"internalType":"address","name":"outbound_tkn","type":"address"},{"internalType":"address","name":"inbound_tkn","type":"address"},{"internalType":"uint256","name":"tickSpacing","type":"uint256"}],"internalType":"struct OLKey","name":"olKey","type":"tuple"},{"internalType":"bool","name":"_withdraw","type":"bool"}],"name":"cleanByMarket","outputs":[{"components":[{"internalType":"uint256","name":"offerId","type":"uint256"},{"internalType":"Tick","name":"tick","type":"int256"},{"internalType":"uint256","name":"gasreq","type":"uint256"},{"internalType":"uint256","name":"takerWants","type":"uint256"}],"internalType":"struct MgvLib.CleanTarget[]","name":"successes","type":"tuple[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"outbound_tkn","type":"address"},{"internalType":"address","name":"inbound_tkn","type":"address"},{"internalType":"uint256","name":"tickSpacing","type":"uint256"}],"internalType":"struct OLKey[]","name":"olKeys","type":"tuple[]"},{"internalType":"bool","name":"_withdraw","type":"bool"}],"name":"cleanMarkets","outputs":[{"components":[{"internalType":"uint256","name":"offerId","type":"uint256"},{"internalType":"Tick","name":"tick","type":"int256"},{"internalType":"uint256","name":"gasreq","type":"uint256"},{"internalType":"uint256","name":"takerWants","type":"uint256"}],"internalType":"struct MgvLib.CleanTarget[][]","name":"successes","type":"tuple[][]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"bytes32","name":"olKeyHash","type":"bytes32"},{"internalType":"uint256[]","name":"offerIds","type":"uint256[]"}],"internalType":"struct MangroveCleanerV2.EfficientMarketClean[]","name":"markets","type":"tuple[]"}],"name":"efficientCalldataClean","outputs":[{"internalType":"uint256","name":"successes","type":"uint256"},{"internalType":"uint256","name":"totalBounty","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"bytes32","name":"olKeyHash","type":"bytes32"},{"internalType":"uint256[]","name":"offerIds","type":"uint256[]"}],"internalType":"struct MangroveCleanerV2.EfficientMarketClean","name":"market","type":"tuple"},{"internalType":"bool","name":"_withdraw","type":"bool"}],"name":"efficientCalldataSingleClean","outputs":[{"internalType":"uint256","name":"successes","type":"uint256"},{"internalType":"uint256","name":"totalBounty","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"outbound_tkn","type":"address"},{"internalType":"address","name":"inbound_tkn","type":"address"},{"internalType":"uint256","name":"tickSpacing","type":"uint256"}],"internalType":"struct OLKey[]","name":"olKeys","type":"tuple[]"},{"components":[{"internalType":"uint256","name":"offerId","type":"uint256"},{"internalType":"Tick","name":"tick","type":"int256"},{"internalType":"uint256","name":"gasreq","type":"uint256"},{"internalType":"uint256","name":"takerWants","type":"uint256"}],"internalType":"struct MgvLib.CleanTarget[][]","name":"targets","type":"tuple[][]"},{"internalType":"bool","name":"_withdraw","type":"bool"}],"name":"efficientClean","outputs":[{"internalType":"uint256","name":"successes","type":"uint256"},{"internalType":"uint256","name":"totalBounty","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"mangrove","outputs":[{"internalType":"contract IMangrove","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"mgvReader","outputs":[{"internalType":"contract MgvReader","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint96","name":"_maxOfferPerCall","type":"uint96"}],"name":"setMaxOfferPerCall","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"taker","type":"address"},{"internalType":"uint96","name":"maxOfferPerCall","type":"uint96"}],"internalType":"struct MangroveCleanerV2.State","name":"_state","type":"tuple"}],"name":"setState","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_taker","type":"address"}],"name":"setTaker","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"state","outputs":[{"internalType":"address","name":"taker","type":"address"},{"internalType":"uint96","name":"maxOfferPerCall","type":"uint96"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]