---
# System prepended metadata

title: Back-Testing Transaction Policies with Cryo & Analysis of Preventable DEX Slippage

---

# Back-Testing Transaction Policies with Cryo & Analysis of Preventable DEX Slippage

Misconfigured transactions, user errors and malicious transaction requests are responsible for a large amount of preventable losses in crypto. Automated transaction policies at the wallet or RPC layer are one approach to mitigating these losses. In this article, we present a library for decoding transaction payloads to detect max slippage of DEX transactions on Uni V2, V3, and Universal Router, a policy implemented as RPC middleware, and a method for using Cryo with Python to back test the effectiveness and calculate preventable slippage of previously executed trades


## What Is A Transaction Policy (and why)?

Smart contracts define the permissible actions of a protocol; however, permissable actions are very different from optimal actions. This gap leads to scenarios where decentralized application users are vulnerable to exploitation due to transaction misconfigurations.

This isn't a flaw of smart contracts, but it highlights a gap in the traditional Web3 stack of application, wallet, contracts, and neutral RPCs. Many user protection features are ill-suited for the smart contract layer as they would be overly restrictive and break the open, composable nature of DeFi. And while applications and wallets can help educate and guide users towards safe transactions, they can benefit from an additional external validation of the user's intended actions.

We introduce transaction policies as an RPC middleware to perform the validation and act as a check on transactions coming from wallets. This middleware is supported by a robust policy framework which interprets complex transactions, adds critical context, and takes action to protect users from mistakes or malicious activity.

![tPGj-5ixJ-87dW31ator6cW4lVJjdDp-e2TJMNR2MO4](https://hackmd.io/_uploads/S1m8tD4Da.png)

By including opt-in rules on the RPC layer we can preserve the neutrality of underlying protocols while enabling protections chosen by the user.

## Slippage Detection Transaction Policy

In the Shield3 framework, policy modules are constructed using [Banyan](https://github.com/0xShield3/banyan), a policy language forked from [AWS Cedar](https://docs.cedarpolicy.com/) and tailored for Ethereum transactions. These policies define the logic to decode, validate, and route transactions safely.

For example, the policy detailed below is designed to determine the potential risks and outcomes of broadcasting a swap transaction with the Uniswap Universal Router. It considers factors like maximum allowable slippage, triggering additional confirmations or halting transactions if needed. These policies can also be configured to reroute transactions to private pools to mitigate public mempool manipulation risks.

```jsonld=
@name("Allow Uniswap Trades With Low Slippage")
@message("Allow Uniswap trades with slippage under threshold")
@dependency("shared_code_snippets:detect_slippage.py")
permit(
  principal,
  action,
  resource
) when {
  true
};

@name("Allow Uniswap Trades With Medium Slippage With MFA")
@action("MFA")
@message("Require MFA for Uniswap trades with slippage over threshold")
@dependency("shared_code_snippets:detect_slippage.py")
permit(
  principal,
  action == Action::"0x3593564c",
  resource
) when {
  (((principal has "groups") && ((principal["groups"]).contains(Group::"slippage_threshold"))) && (((context["transaction"])["codeSnippetResults"]).contains(CodeSnippet::"detect_slippage.py"))) && ((Group::"slippage_threshold"["slippage_threshold_mfa"]) < (CodeSnippet::"detect_slippage.py"["value"]))
};

@name("Block Uniswap Trades With High Slippage")
@message("Block Uniswap trades with slippage over high threshold")
@dependency("shared_code_snippets:detect_slippage.py")
forbid(
  principal,
  action == Action::"0x3593564c",
  resource
) when {
  (((principal has "groups") && ((principal["groups"]).contains(Group::"slippage_threshold"))) && (((context["transaction"])["codeSnippetResults"]).contains(CodeSnippet::"detect_slippage.py"))) && ((Group::"slippage_threshold"["slippage_threshold_block"]) < (CodeSnippet::"detect_slippage.py"["value"]))
};
```

The code snippet dependency invokes the following code using the transaction input data:

```python=
from web3 import Web3
from contextooor.uniswap import uniswap
from eth_utils import encode_hex

parsed_tx = decode(context['tx'])
w3 = Web3(Web3.HTTPProvider('https://reth.shield3.com/rpc'))
max_slippage = uniswap(w3=w3).getSlippage(to_address="0xE592427A0AEce92De3Edee1F18E0157C05861564", input_data=parsed_tx['data'], parsed_tx['value'])
max_slippage_int = round(max_slippage * 100)
result = {'value': max_slippage_int}
```


### Validation Example

This [transaction](https://etherscan.io/tx/0x11ee1c1fd6cb5e7b0b7716082cacb461dc6f2bfa2f3ed119205822d1dc04ba55) is a an example of the worst case scenario in transaction misconfiguration. Looking at the decoded input data we can see the minimum amount out for the swap is 0, allowing an [MEV bot to temporarily manipulate the pools](https://etherscan.io/txs?block=18604963&ps=100&p=3) surounding this transaction to extract a profit, leaving the trader with nearly 0 DAI in exchange for their 45,000 USDT.

| #  | Name                     | Type    | Data                                      |
|----|--------------------------|---------|-------------------------------------------|
| 0  | params.tokenIn           | address | 0xdAC17F958D2ee523a2206206994597C13D831ec7 |
| 0  | params.tokenOut          | address | 0x6B175474E89094C44Da98b954EedeAC495271d0F |
| 0  | params.fee               | uint24  | 100                                       |
| 0  | params.recipient         | address | 0xd2f1680555a45253ac192146E43f66b5e2020eee |
| 0  | params.deadline          | uint256 | 3400772756                                |
| 0  | params.amountIn          | uint256 | 45000000000                               |
| 0  | params.amountOutMinimum  | uint256 | 0                                         |
| 0  | params.sqrtPriceLimitX96 | uint160 | 0                                         |

![Screenshot 2023-12-22 at 20.27.26](https://hackmd.io/_uploads/r1k4_37wp.png)

![Screenshot 2023-12-22 at 20.23.14](https://hackmd.io/_uploads/H1n7OnQwa.png)

On a network fork taken at the block number of this transaction, we can simulate broadcasting this transaction to an RPC URL with the policy middleware enabled and we receive the following message:

```json 
{
	"jsonrpc": "2.0",
	"error": {
    	"code": "-9999982",
    	"message": "Blocked transaction due to policy violation - Block Uniswap trades with slippage over high threshold"
	},
	"id": 1
}

```

With this result the transaction would not have been broadcast and would have saved the trader from the $45k USD loss.


## Evaluating Policy Efficacy

In the above scenario it is trivial to detect the preventable loss of the transaction if the user's wallet had been connected to a protected RPC. However, to assess the efficacy and potential impact of this policy, we can analyze historical trade data from the Uniswap router contracts. By identifying all transactions where traders incurred suboptimal pricing and simulating these transactions with the policy engine, we can learn how widespread swap transaction misconfiguration is, and what could be prevented in the future.

### Gathering Historical Data

With Cryo we can gather historical logs for transactions with event signatures matching swaps on the Uniswap routers, and then use the Uniswap pool addresses to validate the results.

*Note this analysis uses a fork of Cryo available [here](https://github.com/davidthegardens/cryo.git) which adds additional support for new cryo datasets in the Python package*

#### 1. Environment Configuration

```Shell
# Configure Virtual Env
python3.11 -m venv env
source env/bin/activate
pip install maturin

#Using a forked version
git clone https://github.com/davidthegardens/cryo.git
cd cryo/crates/python
maturin build --release
pip install --force-reinstall <OUTPUT_OF_MATURIN_BUILD>.whl
pip install contextooor
```

#### 2. Data Collection

**Range:**

This sample focuses on the first week of December '23 (start block=18687851 dec1 and end block=18737828 dec7)

**Contracts:** 
* Uniswap Universal Router - 0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD
* V2 router - 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
* V3 router - 0xE592427A0AEce92De3Edee1F18E0157C05861564 

All Uniswap logs can be fetched using the example queries at [uniswap.sh](https://github.com/paradigmxyz/cryo/blob/main/examples/uniswap.sh).


```Python=
df_universal_router= await cryo.async_collect('transactions',blocks=bl_u,rpc=my_rpc,to_address=["0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD"],hex=True)
df_universal_router=df_universal_router.filter(pl.col.input.str.slice(0,10).is_in(universal_methods))
df_v2_router= await cryo.async_collect('transactions',blocks=bl_2,rpc=my_rpc,to_address=["0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"],hex=True)
df_v2_router=df_v2_router.filter(pl.col.input.str.slice(0,10).is_in(v2_methods))
df_v3_router= await cryo.async_collect('transactions',blocks=bl_3,rpc=my_rpc,to_address=["0xE592427A0AEce92De3Edee1F18E0157C05861564"],hex=True)
df_v3_router=df_v3_router.filter(pl.col.input.str.slice(0,10).is_in(v3_methods))
all_routers=pl.concat([df_universal_router,df_v2_router,df_v3_router]).filter(pl.col.success==True)
```

#### 3. Load dataframes

```python=
v2_pools=pl.read_parquet("v2_pools/*")
v3_pools=pl.read_parquet("v3_pools/*")

v2_logs=pl.scan_parquet("v2_logs/*").filter((pl.col.block_number>=18687851) & (pl.col.block_number<=18737828)).collect(streaming=True)
v3_logs=pl.scan_parquet("v3_logs/*").filter((pl.col.block_number>=18687851) & (pl.col.block_number<=18737828)).collect(streaming=True)

```

#### 4. Transform and prepare logs for analysis

```python=
v2_logs=v2_logs.rename({'address':"event__pair"})
    .join(v2_pools,on="event__pair",how="left")
    .filter(pl.col.event__pair.is_in([bytes.fromhex("3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad"),bytes.fromhex("7a250d5630b4cf539739df2c5dacb4c659f2488d"),bytes.fromhex("e592427a0aece92de3edee1f18e0157c05861564")]).not_())
    .with_columns(
    amount_out=pl.when(pl.col.event__amount0In_f64!=0)
        .then(pl.col.event__amount0In_f64.abs())
        .otherwise(pl.col.event__amount1In_f64.abs()),
    amount_in=pl.when(pl.col.event__amount0In_f64!=0)
        .then(pl.col.event__amount1In_f64.abs())
        .otherwise(pl.col.event__amount0In_f64.abs()),
    denomination=pl.when(pl.col.event__amount0Out_f64==0)
        .then(pl.col.event__token0)
        .otherwise(pl.col.event__token1),
    destination=pl.when(pl.col.event__amount0Out_f64==0)
        .then(pl.col.event__token1)
        .otherwise(pl.col.event__token0),
    executed_rate=((pl.col.event__amount0Out_f64+pl.col.event__amount1Out_f64))/(pl.col.event__amount0In_f64+pl.col.event__amount1In_f64))

v3_logs=v3_logs.rename({'address':"event__pool"})
    .join(v2_pools,on="event__pool",how="left")
    .filter(pl.col.event__pair.is_in([bytes.fromhex("3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad"),bytes.fromhex("7a250d5630b4cf539739df2c5dacb4c659f2488d"),bytes.fromhex("e592427a0aece92de3edee1f18e0157c05861564")]).not_())
    .with_columns(
    amount_out=
        pl.when(pl.col.event__amount1_f64<0)
        .then(pl.col.event__amount0_f64)
        .otherwise(pl.col.event__amount1_f64),
    amount_in=
        pl.when(pl.col.event__amount1_f64<0)
        .then(pl.col.event__amount1_f64)
        .otherwise(pl.col.event__amount0_f64),
    denomination=
        pl.when(pl.col.event__amount1_f64<0)
        .then(pl.col.event__token1)
        .otherwise(pl.col.event__token0),
    destination=
        pl.when(pl.col.event__amount1_f64<0)
        .then(pl.col.event__token0)
        .otherwise(pl.col.event__token1),
    executed_rate=
        pl.when(pl.col.event__amount0_f64<0)
        .then(abs(pl.col.event__amount0_f64/pl.col.event__amount1_f64))
        .otherwise(
            pl.when(pl.col.event__amount1_f64==0)
            .then(float('inf')*pl.col.event__amount0_f64*pl.col.event__amount1_f64)
            .otherwise(abs(pl.col.event__amount1_f64/pl.col.event__amount0_f64))))
```

#### 5. Get the reserves of each pair at the correct block

*NOTE: Fetching historic reserve data was a significant bottle neck. This took over 16 hours to run. Another approach could be to store the amounts of the reserves rather than calculate the actual rate. This would be more computationally efficient, and save time by filtering out low liquidity pools.*

```python=
from web3 import Web3
w3=Web3(Web3.HTTPProvider(my_rpc))

def v3_actual(pair_addr,block):
    try:
        pair_addr=w3.to_checksum_address(pair_addr)
        slot0=w3.eth.contract(pair_addr,abi="""[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"int24","name":"tickLower","type":"int24"},{"indexed":true,"internalType":"int24","name":"tickUpper","type":"int24"},{"indexed":false,"internalType":"uint128","name":"amount","type":"uint128"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":true,"internalType":"int24","name":"tickLower","type":"int24"},{"indexed":true,"internalType":"int24","name":"tickUpper","type":"int24"},{"indexed":false,"internalType":"uint128","name":"amount0","type":"uint128"},{"indexed":false,"internalType":"uint128","name":"amount1","type":"uint128"}],"name":"Collect","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint128","name":"amount0","type":"uint128"},{"indexed":false,"internalType":"uint128","name":"amount1","type":"uint128"}],"name":"CollectProtocol","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"paid0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"paid1","type":"uint256"}],"name":"Flash","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint16","name":"observationCardinalityNextOld","type":"uint16"},{"indexed":false,"internalType":"uint16","name":"observationCardinalityNextNew","type":"uint16"}],"name":"IncreaseObservationCardinalityNext","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint160","name":"sqrtPriceX96","type":"uint160"},{"indexed":false,"internalType":"int24","name":"tick","type":"int24"}],"name":"Initialize","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"int24","name":"tickLower","type":"int24"},{"indexed":true,"internalType":"int24","name":"tickUpper","type":"int24"},{"indexed":false,"internalType":"uint128","name":"amount","type":"uint128"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint8","name":"feeProtocol0Old","type":"uint8"},{"indexed":false,"internalType":"uint8","name":"feeProtocol1Old","type":"uint8"},{"indexed":false,"internalType":"uint8","name":"feeProtocol0New","type":"uint8"},{"indexed":false,"internalType":"uint8","name":"feeProtocol1New","type":"uint8"}],"name":"SetFeeProtocol","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"int256","name":"amount0","type":"int256"},{"indexed":false,"internalType":"int256","name":"amount1","type":"int256"},{"indexed":false,"internalType":"uint160","name":"sqrtPriceX96","type":"uint160"},{"indexed":false,"internalType":"uint128","name":"liquidity","type":"uint128"},{"indexed":false,"internalType":"int24","name":"tick","type":"int24"}],"name":"Swap","type":"event"},{"inputs":[{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"},{"internalType":"uint128","name":"amount","type":"uint128"}],"name":"burn","outputs":[{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"},{"internalType":"uint128","name":"amount0Requested","type":"uint128"},{"internalType":"uint128","name":"amount1Requested","type":"uint128"}],"name":"collect","outputs":[{"internalType":"uint128","name":"amount0","type":"uint128"},{"internalType":"uint128","name":"amount1","type":"uint128"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint128","name":"amount0Requested","type":"uint128"},{"internalType":"uint128","name":"amount1Requested","type":"uint128"}],"name":"collectProtocol","outputs":[{"internalType":"uint128","name":"amount0","type":"uint128"},{"internalType":"uint128","name":"amount1","type":"uint128"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"fee","outputs":[{"internalType":"uint24","name":"","type":"uint24"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeGrowthGlobal0X128","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeGrowthGlobal1X128","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"flash","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint16","name":"observationCardinalityNext","type":"uint16"}],"name":"increaseObservationCardinalityNext","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint160","name":"sqrtPriceX96","type":"uint160"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"liquidity","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxLiquidityPerTick","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"},{"internalType":"uint128","name":"amount","type":"uint128"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"mint","outputs":[{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"observations","outputs":[{"internalType":"uint32","name":"blockTimestamp","type":"uint32"},{"internalType":"int56","name":"tickCumulative","type":"int56"},{"internalType":"uint160","name":"secondsPerLiquidityCumulativeX128","type":"uint160"},{"internalType":"bool","name":"initialized","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint32[]","name":"secondsAgos","type":"uint32[]"}],"name":"observe","outputs":[{"internalType":"int56[]","name":"tickCumulatives","type":"int56[]"},{"internalType":"uint160[]","name":"secondsPerLiquidityCumulativeX128s","type":"uint160[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"positions","outputs":[{"internalType":"uint128","name":"liquidity","type":"uint128"},{"internalType":"uint256","name":"feeGrowthInside0LastX128","type":"uint256"},{"internalType":"uint256","name":"feeGrowthInside1LastX128","type":"uint256"},{"internalType":"uint128","name":"tokensOwed0","type":"uint128"},{"internalType":"uint128","name":"tokensOwed1","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"protocolFees","outputs":[{"internalType":"uint128","name":"token0","type":"uint128"},{"internalType":"uint128","name":"token1","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint8","name":"feeProtocol0","type":"uint8"},{"internalType":"uint8","name":"feeProtocol1","type":"uint8"}],"name":"setFeeProtocol","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"slot0","outputs":[{"internalType":"uint160","name":"sqrtPriceX96","type":"uint160"},{"internalType":"int24","name":"tick","type":"int24"},{"internalType":"uint16","name":"observationIndex","type":"uint16"},{"internalType":"uint16","name":"observationCardinality","type":"uint16"},{"internalType":"uint16","name":"observationCardinalityNext","type":"uint16"},{"internalType":"uint8","name":"feeProtocol","type":"uint8"},{"internalType":"bool","name":"unlocked","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"}],"name":"snapshotCumulativesInside","outputs":[{"internalType":"int56","name":"tickCumulativeInside","type":"int56"},{"internalType":"uint160","name":"secondsPerLiquidityInsideX128","type":"uint160"},{"internalType":"uint32","name":"secondsInside","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"bool","name":"zeroForOne","type":"bool"},{"internalType":"int256","name":"amountSpecified","type":"int256"},{"internalType":"uint160","name":"sqrtPriceLimitX96","type":"uint160"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"swap","outputs":[{"internalType":"int256","name":"amount0","type":"int256"},{"internalType":"int256","name":"amount1","type":"int256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"int16","name":"","type":"int16"}],"name":"tickBitmap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"tickSpacing","outputs":[{"internalType":"int24","name":"","type":"int24"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"int24","name":"","type":"int24"}],"name":"ticks","outputs":[{"internalType":"uint128","name":"liquidityGross","type":"uint128"},{"internalType":"int128","name":"liquidityNet","type":"int128"},{"internalType":"uint256","name":"feeGrowthOutside0X128","type":"uint256"},{"internalType":"uint256","name":"feeGrowthOutside1X128","type":"uint256"},{"internalType":"int56","name":"tickCumulativeOutside","type":"int56"},{"internalType":"uint160","name":"secondsPerLiquidityOutsideX128","type":"uint160"},{"internalType":"uint32","name":"secondsOutside","type":"uint32"},{"internalType":"bool","name":"initialized","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token0","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token1","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}]""").functions.slot0()
        slot0_b0=slot0.call(block_identifier=block)[0]
        rate0=(slot0_b0/2**96)**2
        return rate0
    except Exception:
        return None

def v2_actual(pair_addr,block):
    try:
        pair_addr=w3.to_checksum_address(pair_addr)
        reserves=w3.eth.contract(pair_addr,abi="""[{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"},{"indexed":true,"internalType":"address","name":"to","type":"address"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount0In","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1In","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount0Out","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1Out","type":"uint256"},{"indexed":true,"internalType":"address","name":"to","type":"address"}],"name":"Swap","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint112","name":"reserve0","type":"uint112"},{"indexed":false,"internalType":"uint112","name":"reserve1","type":"uint112"}],"name":"Sync","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"constant":true,"inputs":[],"name":"DOMAIN_SEPARATOR","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"MINIMUM_LIQUIDITY","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"PERMIT_TYPEHASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"burn","outputs":[{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getReserves","outputs":[{"internalType":"uint112","name":"_reserve0","type":"uint112"},{"internalType":"uint112","name":"_reserve1","type":"uint112"},{"internalType":"uint32","name":"_blockTimestampLast","type":"uint32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_token0","type":"address"},{"internalType":"address","name":"_token1","type":"address"}],"name":"initialize","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"kLast","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"mint","outputs":[{"internalType":"uint256","name":"liquidity","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"nonces","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"permit","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"price0CumulativeLast","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"price1CumulativeLast","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"skim","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount0Out","type":"uint256"},{"internalType":"uint256","name":"amount1Out","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"swap","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"sync","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"token0","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"token1","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]""").functions.getReserves()
        reserves_b0=reserves.call(block_identifier=block)
        return reserves_b0[1]/reserves_b0[0]
    except Exception:
        return None

#map the function and adjust for the direction of the swap
v2_logs=v2_logs.with_columns(actual_rate=pl.struct("block_number","event__pair").map_elements(lambda x: v2_actual(x["event__pair"],x["block_number"]),return_dtype=pl.Float64))
    .with_columns(actual_rate=pl.when(pl.col.denomination==pl.col.event__token0).then(pl.col.actual_rate**-1).otherwise(pl.col.actual_rate))
    
#write to storage to avoid endless regrets
v2_logs.write_parquet("v2_with_actuals.parquet")

#same thing for v3
v3_logs=v3_logs.with_columns(actual_rate=pl.struct("block_number","event__pool").map_elements(lambda x: v3_actual(x["event__pool"],x["block_number"]),return_dtype=pl.Float64))
    .with_columns(actual_rate=pl.when(pl.col.denomination==pl.col.event__token0).then(pl.col.actual_rate**-1).otherwise(pl.col.actual_rate))
    
v3_logs.write_parquet("v3_with_actuals.parquet")
```

#### 6. Slippage Calculation

```python=
v3_logs=v3_logs.select(['block_number','transaction_hash','amount_out','destination','executed_rate','actual_rate','event__pool']).rename({'event__pool':'pair'})
v2_logs=v2_logs.select(['block_number','transaction_hash','amount_out','denomination','executed_rate','actual_rate','event__pair']).rename({'event__pair':'pair'})
all_swaps=pl.concat([v3_logs,v2_logs])

all_swaps=all_swaps
    .drop_nulls()
    .sort("executed_rate",descending=True,nulls_last=True)
    .fill_nan(-1)
    .filter(pl.col.executed_rate<=pl.col.actual_rate)
    .with_columns(
        denomination=pl.concat_str(pl.lit("0x"),pl.col.denomination.bin.encode('hex'),
        slippage=1-(pl.col.executed_rate/pl.col.actual_rate),
        loss=(pl.col.actual_rate-pl.col.executed_rate)*pl.col.amount_out)

```

#### 7. Join with price data

*NOTE: In this step we filter the dataset for the highest liquidity pools and calculate the USD value of the transaction slippage*

```python=
import requests
tokens=all_swaps.select(pl.col.denomination).to_series().unique().to_list()
def get_usd_value(tokens):
    cstokens_list=[]
    cstokens=""
    for i,token in enumerate(tokens):
        if i%100 ==0 and i !=0:
            cstokens_list.append(cstokens)
            cstokens=""
        cstokens=cstokens+",ethereum:"+token
    
    if len(tokens)%150!=0:
        cstokens_list.append(cstokens)
    
    returned={}
    for cstokens in cstokens_list: 
        value=requests.get(f"https://coins.llama.fi/prices/current/{cstokens}").json()
        returned.update(value['coins'])
    
    tokens=[]
    symbol=[]
    price=[]
    conf=[]
    for token in returned.keys():
        tokens.append(token.lstrip("ethereum:"))
        data=returned[token]
        symbol.append(data['symbol'])
        price.append(data['price']/10**data['decimals'])
        conf.append(data['confidence'])
    
    return pl.DataFrame({'denomination':tokens,'price':price,'symbol':symbol,'confidence':conf})

high_liquidity=['a43fe16908251ee70ef74718545e4fe6c5ccec9f','06da0fd433c1a5d7a4faa01111c044910a184553','09d1d767edf8fa23a64c51fa559e0688e526812f','11b815efb8f581194ae79006d24e0d814b7697f6','3416cf6c708da44db2624d63ea0aaef7113527c6','4585fe77225b41b697c938b018e2ac67ac5a20c0','4e68ccd3e89f51c3074ca5072bbac773960dfa36','b4e16d0168e52d35cacd2c6185b44281ec28c9dc','0d4a11d5eeaac28ec3f61d100daf4d40471f1852','88e6a0c2ddd26feeb64f039a2c41296fcb3f5640']

usd_val=get_usd_value(tokens)

slippagewithusd=all_swaps.join(usd_val,how='left',on='denomination')
    .with_columns(
            pair=pl.col.pair.bin.encode('hex'),
            loss_usd=pl.col.price*pl.col.loss,
            transaction_hash=pl.col.transaction_hash.bin.encode('hex'))
        .filter(pl.col.pair.is_in(high_liquidity))
            .sort(by='loss_usd',descending=True,nulls_last=True)
            .head(2000)

```

#### 8. Collect transaction data for filtered swaps

```python=
blox=slippagewithusd.select(pl.col.block_number.cast(pl.Utf8)).to_series().to_list()
methods=universal_methods
methods.extend(v2_methods)
methods.extend(v3_methods)
df_router= await cryo.async_collect('transactions',blocks=blox,rpc=my_rpc,to_address=["0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD","0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D","0xE592427A0AEce92De3Edee1F18E0157C05861564"],hex=True)
df_router=df_router.filter(pl.col.input.str.slice(0,10).is_in(methods))
with_tx_data=slippagewithusd.with_columns(transaction_hash=pl.concat_str(pl.lit("0x"),pl.col.transaction_hash)).join(df_router,on='transaction_hash',how='left').drop_nulls()
```

#### 9. Apply the policy retroactively

```python=
from web3 import Web3
w3=Web3(Web3.HTTPProvider(my_rpc))


def get_policy_slippage(block,to_address,value,input_data):
    try:
        result=uniswap(w3=w3,block=block).getSlippage(to_address=to_address,input_data=input_data,value=value)
    except:
        result={'error':'oof'}
    if 'success' in result.keys():
        return result['success']
    else:
        return None

all_routers=with_tx_data
    .with_columns(
        policy_slippage=pl.struct('block_number','to_address','value_f64','input')                              
        .map_elements(lambda 
        x:get_policy_slippage(x['block_number'],x['to_address'],x['value_f64'],x['input']),return_dtype=pl.Float64))
        .drop_nulls("policy_slippage")

```

#### 10. Identify transactions that would have violated the policy

```python=
policy_threshold=0.05
prevented_transactions=all_routers.filter(pl.col.policy_slippage>=policy_threshold)
```

### Methodology for Analysis

The [Contextooor](https://github.com/davidthegardens/contextooor) Library provides utilites and code snippets to the policy engine execution step.

In order to analyze the maximum potential slippage given an encoded transaction payload, the library performs the following steps:

1.  De-serialize the transation
2.  Identify the router version & ABI based on the contract address
3.  Decode the trade path
4.  Using the token addresses in the trade path, call the correct factory to get the pool address
5.  On the pool contract, call getReserves or slot0 to derive the optimal rate
6.  Take the inverse of the optimal rate if token addresses are not ordered
7.  Aggregate the rates together to provide the least amount of slippages possible. Sometimes paths will diverge, and often times paths perform similar swaps concurrently. All paths with the same start token are aggregated by weighted average (weighted by amount out)
8.  Compare the path's optimal rate to the specified amount out and amount in found in the input data.


### Proposed Future Enhancements

The library and analysis method are currently tailored from the Uniswap router contracts. In the near future the policy & libraries will be updated to support additional AMMs and swap types.

## Conclusion

This article outlines the role of policy frameworks in optimizing user interactions and preventing exploits of misconfigured transactions, a well as techniques for testing policies against historical datasets of transactions using Cryo.

### Further Reading and Resources

* Banyan Middleware Library: https://github.com/0xShield3/banyan
* Contextooor: https://github.com/davidthegardens/contextooor
* Automating Compliance & Security with Shield3: https://medium.com/block-science/automating-compliance-and-security-with-shield3-3174f6be3fcc