Skip to content

Commit

Permalink
addressed reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
parv0888 committed Aug 7, 2023
1 parent 1db46ee commit b433cdd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 40 deletions.
49 changes: 10 additions & 39 deletions concordium-cis2/src/cis2_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,7 @@ const TRANSFER_ENTRYPOINT_NAME: EntrypointName = EntrypointName::new_unchecked("
const UPDATE_OPERATOR_ENTRYPOINT_NAME: EntrypointName =
EntrypointName::new_unchecked("updateOperator");

/// Wrapper for `Cis2Error<T>`. This allows for various traits to be
/// implemented.
#[derive(Debug)]
pub struct Cis2ErrorWrapper<T>(Cis2Error<T>);

impl<T> From<Cis2Error<T>> for Cis2ErrorWrapper<T> {
fn from(e: Cis2Error<T>) -> Self { Cis2ErrorWrapper(e) }
}

impl<T> AsRef<Cis2Error<T>> for Cis2ErrorWrapper<T> {
fn as_ref(&self) -> &Cis2Error<T> { &self.0 }
}

impl<T: Serial> Serial for Cis2ErrorWrapper<T> {
fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { self.0.serial(out) }
}

impl<T: Deserial> Deserial for Cis2ErrorWrapper<T> {
fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
let tag = source.read_u8()?;
match tag {
0 => Ok(Cis2ErrorWrapper(Cis2Error::InvalidTokenId)),
1 => Ok(Cis2ErrorWrapper(Cis2Error::InsufficientFunds)),
2 => Ok(Cis2ErrorWrapper(Cis2Error::Unauthorized)),
3 => {
let t = T::deserial(source)?;
Ok(Cis2ErrorWrapper(Cis2Error::Custom(t)))
}
_ => bail!(ParseError {}),
}
}
}
pub type InvokeContractError<T> = CallContractError<Cis2ErrorWrapper<T>>;
pub type InvokeContractError<T> = CallContractError<Cis2Error<T>>;

/// Errors which can be returned by the `Cis2Client`.
#[derive(Debug)]
Expand All @@ -70,7 +38,7 @@ impl<T: Serial> Serial for Cis2ClientError<T> {
fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
match self {
Cis2ClientError::InvokeContractError(e) => {
out.write_u8(3)?;
out.write_u8(2)?;
match e {
CallContractError::AmountTooLarge => out.write_u8(0),
CallContractError::MissingAccount => out.write_u8(1),
Expand Down Expand Up @@ -121,7 +89,7 @@ impl<T: Read, R: Deserial> TryFrom<CallContractError<T>> for Cis2ClientError<R>
} => Ok(Cis2ClientError::InvokeContractError(InvokeContractError::LogicReject {
reason,
return_value: {
let cis2_error = Cis2ErrorWrapper::<R>::deserial(&mut return_value);
let cis2_error = Cis2Error::<R>::deserial(&mut return_value);
match cis2_error {
Ok(cis2_error) => cis2_error,
Err(_) => bail!(Cis2ClientError::ParseResult),
Expand Down Expand Up @@ -294,8 +262,9 @@ impl Cis2Client {

/// Calls the `transfer` entrypoint of the CIS2 contract to transfer the
/// given amount of tokens from the given owner to the given receiver.
/// If the transfer is successful, it returns `Ok(())`, else it returns an
/// `Err`.
/// If the transfer is successful and the state is modified, it returns
/// `Ok(true)`, else it returns `Ok(false)`. If there is an error, it
/// returns `Err`.
///
/// # Examples
/// ```rust
Expand Down Expand Up @@ -332,8 +301,10 @@ impl Cis2Client {
}

/// Calls the `updateOperator` of the CIS2 contract.
/// If the update is successful, it returns `Ok(())`, else it returns an
/// `Err`. # Examples
/// If the update is successful and the state is modified, it returns `Ok(true)`, else it returns `Ok(false)`.
/// If there is an error, it returns `Err`.
///
/// # Examples
/// ```rust
/// use concordium_cis2::{cis2_client::*, *};
/// use concordium_std::{test_infrastructure::*, *};
Expand Down
2 changes: 1 addition & 1 deletion concordium-cis2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ impl<T: IsTokenId, A: IsTokenAmount> schema::SchemaType for Cis2Event<T, A> {
}

/// The different errors the contract can produce.
#[derive(Debug, PartialEq, Eq, SchemaType, Serial)]
#[derive(Debug, PartialEq, Eq, SchemaType, Serial, Deserial)]
pub enum Cis2Error<R> {
/// Invalid token id (Error code: -42000001).
InvalidTokenId,
Expand Down

0 comments on commit b433cdd

Please sign in to comment.