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

Thoughts on adding deadline wrapper? #22

Open
Art3miX opened this issue Jan 7, 2024 · 0 comments
Open

Thoughts on adding deadline wrapper? #22

Art3miX opened this issue Jan 7, 2024 · 0 comments

Comments

@Art3miX
Copy link

Art3miX commented Jan 7, 2024

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.

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

No branches or pull requests

1 participant