Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ckb-jsonrpc-types #92

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

EthanYuan
Copy link
Contributor

Replace PR #85 with this PR

PR Content

  • Copying and pasting the ckb-jsonrpc-types into this project. We propose that the ckb-sdk-rust should maintain its own jsonrpc-types, facilitating the addition of developer-friendly features.
  • Extensions have been made to the local version of jsonrpc-types to provide deserialization-friendly enhancements for ResponseFormat and BlockResponse.
  • Based on the local version of jsonrpc-types, we've refactored methods in CkbRpcClient that are related to ResponseFormat
    and BlockResponse.

Deprecation Notice

Building on the aforementioned changes, a deprecation notice has been added to the pub trait ResponseFormatGetter, in preparation for its future deprecation in upcoming versions.

pub trait ResponseFormatGetter<V> {
    #[deprecated(note = "This method is being phased out in favor of `ResponseFormat::get_value`.")]
    fn get_value(self) -> Result<V, crate::rpc::RpcError>;
    #[deprecated(note = "This method is being phased out in favor of `ResponseFormat::get_hex`.")]
    fn get_json_bytes(self) -> Result<JsonBytes, crate::rpc::RpcError>;
}

More Details

add methods for ResponseFormat

/// Retrieve the data in JSON format from the ResponseFormat
pub fn get_json(self) -> Result<V, String> {
    match self.inner {
        Either::Left(v) => Ok(v),
        _ => Err("Not in JSON format".to_string()),
    }
}

/// Retrieve the data in molecule serialized hex format from the ResponseFormat
pub fn get_hex(self) -> Result<JsonBytes, String> {
    match self.inner {
        Either::Right(json_bytes) => Ok(json_bytes),
        _ => Err("Not in hex format".to_string()),
    }
}

add method for BlockResponse

/// Get the regular block response.
///
/// Returns `Ok` with the wrapped `ResponseFormat<BlockView>` if the variant is `Regular`,
/// otherwise returns `Err` with an error message.
pub fn get_regular(self) -> Result<ResponseFormat<BlockView>, String> {
    match self {
        BlockResponse::Regular(regular) => Ok(regular),
        _ => Err("Not a Regular variant".to_string()),
    }
}

/// Get the block response with cycles.
///
/// Returns `Ok` with the wrapped `BlockWithCyclesResponse` if the variant is `WithCycles`,
/// otherwise returns `Err` with an error message.
pub fn get_with_cycles(self) -> Result<BlockWithCyclesResponse, String> {
    match self {
        BlockResponse::WithCycles(with_cycles) => Ok(with_cycles),
        _ => Err("Not a WithCycles variant".to_string()),
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant