Skip to content

Commit

Permalink
feat: better pool id encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu committed Jul 29, 2024
1 parent 6d5e2b0 commit cb890d3
Show file tree
Hide file tree
Showing 6 changed files with 798 additions and 236 deletions.
7 changes: 7 additions & 0 deletions contracts/utils/src/Fetcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.10;
import {PoolId} from "v4-core/types/PoolId.sol";
import {IPoolManager} from "v4-core/interfaces/IPoolManager.sol";
import {Position} from "v4-core/libraries/Position.sol";
import {PoolKey} from "v4-core/types/PoolKey.sol";

// This contract is a compact version of StateLibrary, which can be found below.
// https://github.com/Uniswap/v4-core/blob/799dd2cb980319a8d3b827b6a7aa59a606634553/src/libraries/StateLibrary.sol
Expand All @@ -13,6 +14,12 @@ contract Fetcher {
return keccak256(abi.encodePacked(PoolId.unwrap(poolId), POOLS_SLOT));
}

function toId(PoolKey memory poolKey) external pure returns (PoolId poolId) {
assembly ("memory-safe") {
poolId := keccak256(poolKey, mul(32, 5))
}
}

function getSlot0(IPoolManager manager, PoolId poolId)
external
view
Expand Down
23 changes: 15 additions & 8 deletions src/arbitrageur.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[derive(Debug, Deserialize, Serialize, Clone)]
struct Arbitrageur {
#[derive(Debug, Default, Deserialize, Serialize, Clone)]
pub struct Arbitrageur {
pub base: Base,
pub deployment: Option<Address>,
pub pool: Option<PoolParams>,
Expand Down Expand Up @@ -49,15 +49,22 @@ impl Behavior<Message> for Arbitrageur {
let manager = PoolManager::new(self.deployment.unwrap(), self.base.client.clone().unwrap());
let fetcher = Fetcher::new(self.fetcher.unwrap(), self.base.client.clone().unwrap());

let id = keccak256(&self.pool.clone().unwrap().key.encode());
let fetcher_key = FetcherPoolKey {
currency0: self.pool.clone().unwrap().key.currency0,
currency1: self.pool.clone().unwrap().key.currency0,
fee: self.pool.clone().unwrap().key.fee,
tickSpacing: self.pool.clone().unwrap().key.tickSpacing,
hooks: self.pool.clone().unwrap().key.hooks,
};
let id = fetcher.toId(fetcher_key).call().await?.poolId;

let slot0 = fetcher
.getSlot0(*manager.address(), id)
.send()
.await?
.watch()
let get_slot0_return = fetcher
.getSlot0(manager.address().clone(), id)
.call()
.await?;

println!("price: {:?}", get_slot0_return);

return Ok(ControlFlow::Continue);
}
}
Loading

0 comments on commit cb890d3

Please sign in to comment.