0

I am trying to retrieve the slot0 data from a UniswapV3 liquidity pool. This is pretty straightforward on Golang, but giving me major problems in Rust. Like Swapping tokens is problematic for me in Golang but easy in Rust. I am hoping somebody can point me in the right direction so I can actually build something :-) :

struct SlotZeroData {
sqrtPriceX96: U256,
tick: U256,
observationIndex: U256,
observationCardinality: U256,
observationCardinalityNext: U256,
feeProtocol: U256,
unlocked: bool,}

impl Tokenize for SlotZeroData {
fn into_tokens(self) -> Vec<Token> {
    let mut data = Vec::new();
    data.push(Token::FixedBytes(self.sqrtPriceX96.to_bytes().to_vec()));
    data.push(Token::Int(self.tick));
    data.push(Token::Uint(self.observationIndex.into()));
    data.push(Token::Uint(self.observationCardinality.into()));
    data.push(Token::Uint(self.observationCardinalityNext.into()));
    data.push(Token::Uint(self.feeProtocol.into()));
    data.push(Token::Bool(self.unlocked));
    data
}

impl Tokenize for SlotZeroData {
fn tokenize(&self) -> Vec<Token> {
    let mut data = Vec::new();
    data.push(Token::FixedBytes(self.sqrtPriceX96.to_bytes().to_vec()));
    data.push(Token::Int(self.tick));
    data.push(Token::Uint(self.observationIndex.into()));
    data.push(Token::Uint(self.observationCardinality.into()));
    data.push(Token::Uint(self.observationCardinalityNext.into()));
    data.push(Token::Uint(self.feeProtocol.into()));
    data.push(Token::Bool(self.unlocked));
    data
}

impl Detokenize for SlotZeroData {
fn from_tokens(tokens: Vec<Token>) -> Result<Self, Error> {
    if let [
    Token::FixedBytes(sqrt_price),
    Token::Int(tick),
    Token::Uint(obs_index),
    Token::Uint(obs_card),
    Token::Uint(obs_card_next),
    Token::Uint(fee_protocol),
    Token::Bool(unlocked),
    ] = tokens.as_slice()
    {
        Ok(Self {
            sqrtPriceX96: U256::from_big_endian(&sqrt_price),
            tick: tick.clone(),
            observationIndex: obs_index.clone().as_u32() as u16,
            observationCardinality: obs_card.clone().as_u32() as u16,
            observationCardinalityNext: obs_card_next.clone().as_u32() as u16,
            feeProtocol: fee_protocol.clone().as_u32() as u8,
            unlocked: unlocked.clone(),
        })
    } else {
        Err(Error::from("Invalid number of tokens for SlotZeroData"))
    }
}

I feel it should not be this hard to query some data. What am I doing wrong here? I am using the web3 and ethabi crate.

let slot_0: SlotZeroData = pool_contract
        .query("slot0", (), None, Options::default(), None)
        .await
        .unwrap();

This is basically what I am trying to do.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Please properly format your code, and also please don't type out the code on SO but copy a [mre] that you've tried and confirmed to produce the expected error. And then it would be great if you could provide the error message or describe better what's "wrong" with the code as it seems to be fine apart from the obvious missing `}` which I do believe are copying errors. – cafce25 Jul 20 '23 at 04:45

0 Answers0