Skip to content

Thoughts on adding deadline wrapper? #22

@Art3miX

Description

@Art3miX

interested to hear thoughts about this wrapper to live in cw-utils.

#[cw_serde]
#[serde(untagged)]
pub enum Deadline {
    Expiration(Expiration),
    Duration(Duration),
}

impl Default for Deadline {
    fn default() -> Self {
        Deadline::Expiration(Expiration::default())
    }
}

impl Deadline {
  pub fn into_expiration(self, block: &BlockInfo) -> Expiration {
    match self {
      Deadline::Expiration(expiration) => expiration,
      Deadline::Duration(duration) => duration.after(block)
    }
  }
}

The main idea here is to export a wrapper that can accept either Expiration or a Duration, and turns it into an Expiration.

The main benefit is that it removes all custom logic that a contract need to handle both cases if a contract wants to accept either one of the options.

Example:

Looking at cw721 ExecuteMsg::Approve:

Approve {
    spender: String,
    token_id: String,
    expires: Option<Expiration>,
},

can be turned into:

Approve {
    spender: String,
    token_id: String,
    expires: Deadline, // or  Option<Deadline>,
},

In contract, we can easily turn deadline into expiration:

let expiration = expires.into_expiration(&env.block)

Or using an optional Deadline

let expiration = expires.unwrap_or_default().into_expiration(&env.block)

Now the user can specify either a specific block/time or some random duration

NOTE:

The #[serde(untagged)] here is to provide a nicer UI, should work nicely given untagged should be supported now and Expiration and Duration enums doesn't have an overlapping values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions