andromedaprotocol / ado-database

Repository for submission and review of Andromeda Digital Objects
0 stars 3 forks source link

Cw721 timelock #9

Open mdjakovic0920 opened 3 weeks ago

mdjakovic0920 commented 3 weeks ago

Contact Information

Summary: With the CW721 Timelock, you can lock an NFT (CW721) with a contract for a certain amount of time (currently between one day & one year). Once the timelock has expired, anyone can call the claim function to send the NFT to the defined recipient. Each locked NFT has a specific lock ID comprising the CW721 contract address concatenated with the token_id.

This is the solution of https://github.com/andromedaprotocol/ado-database/issues/7

Messages

Instantiation (What is specified and stored at instantiation)

pub struct InstantiateMsg {
    pub kernel_address: String,
    pub owner: Option<String>,
    pub authorized_token_addresses: Option<Vec<AndrAddr>>,
}

authorized_token_addresses: An optional vector of addresses that are authorized to interact with the contract. If not specified, any address can interact.

Execute Messages (What are the messages that can be executed)

  1. ReceiveNft: Handles the receipt of an NFT and locks it for a specified duration.
ReceiveNft(Cw721ReceiveMsg),

Cw721ReceiveMsg: The message received when an NFT is sent to this contract. This message includes the sender, the token ID, and a message for further handling.

  1. ClaimNft: Allows the recipient to claim the NFT once the lock period has expired.
ClaimNft {
    cw721_contract: AndrAddr,
    token_id: String,
},

cw721_contract: The address of the CW721 contract. token_id: The ID of the token to be claimed.

Query Messages (What are the messages that can be queried, what does each return)

  1. UnlockTime: Returns the unlock time for a specified NFT.
    UnlockTime {
    cw721_contract: AndrAddr,
    token_id: String,
    },

cw721_contract: The address of the CW721 contract. token_id: The ID of the token.

Returns:

pub struct UnlockTimeResponse {
    pub unlock_time: u64,
}

unlock_time: The time at which the NFT can be claimed.

  1. NftDetails: Returns the details of a locked NFT including the unlock time and the recipient address.
    NftDetails {
    cw721_contract: AndrAddr,
    token_id: String,
    },

    cw721_contract: The address of the CW721 contract. token_id: The ID of the token.

Returns:

pub struct NftDetailsResponse {
    pub unlock_time: u64,
    pub recipient: Addr,
}

unlock_time: The time at which the NFT can be claimed. recipient: The address of the recipient who can claim the NFT after the unlock time.

State

The contract maintains the following state:

pub struct TimelockInfo {
    pub unlock_time: MillisecondsExpiration,
    pub recipient: Addr,
}

pub const TIMELOCKS: Map<&str, TimelockInfo> = Map::new("timelocks");

TimelockInfo: Structure holding the unlock time and the recipient address for each locked NFT. TIMELOCKS: A mapping from token IDs to their respective TimelockInfo.

This state ensures that each NFT has its own lock period and designated recipient.

Summary by CodeRabbit

coderabbitai[bot] commented 3 weeks ago

[!NOTE] Currently processing new changes in this PR. This may take a few minutes, please wait...

Commits Files that changed from the base of the PR and between 5dc7e606016509086c8fb975c3044a4c4d9840bd and e86b8db844c7cd03bdb23438d5052fa960e6888e.
Files selected for processing (3) * cw721-timelock/src/contract.rs (1 hunks) * cw721-timelock/src/state.rs (1 hunks) * cw721-timelock/src/testing/tests.rs (1 hunks)
 ________________________________________________________________________________________________________________________________________
< Use assertions to prevent the impossible. Assertions validate your assumptions. Use them to protect your code from an uncertain world. >
 ----------------------------------------------------------------------------------------------------------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).

Walkthrough

The updates introduce a CW721 Timelock functionality to lock and claim NFTs within smart contracts. Key modifications include new configuration files for Cargo and CI/CD, added dependencies, message and state logic for NFT timelocks, and extensive test coverage for various scenarios.

Changes

Files/Groups Change Summary
.cargo/config Introduced command aliases for build and test operations for convenience.
.circleci/config.yml, .github/workflows Added CircleCI and GitHub Actions configurations for building, testing, and releasing Docker images and WASM artifacts.
.editorconfig, .gitignore, LICENSE, NOTICE Added configuration, ignore rules, and license/notice files for better project management.
Cargo.toml Updated with new dependencies, features, and configurations for the Rust project.
README.md Added documentation outlining CW721 Timelock functionality and usage.
examples/schema.rs Introduced schema generation for contract messages.
src/contract.rs, src/lib.rs, src/msg.rs, src/state.rs Added core logic for contract instantiation, execution, state management, and message handling.
src/testing Added test cases covering various scenarios such as instantiation, timelocks, claims, and NFT detail queries.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Contract
    participant State
    participant CW721Contract

    User->>+Contract: Instantiate contract with authorized addresses
    Contract->>State: Initialize state

    User->>+Contract: Receive NFT and lock it
    Contract->>State: Save timelock details

    User->>+Contract: Claim NFT after lock expires
    Contract->>State: Verify lock expiration
    Contract->>+CW721Contract: Transfer NFT to user
    CW721Contract->>-User: NFT received

    User->>+Contract: Query NFT details
    Contract->>State: Retrieve timelock info
    Contract->>-User: Respond with NFT details and unlock time

In fields of code, we hop with glee, Unraveling secrets, bit by bit, Unlocking NFTs, let time run free, Claim your treasures, if they fit! With tests so robust and CI so neat, Our journey's complete, oh what a feat!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
joemonem commented 1 day ago

Looks good to me, would be nice to have it tried on testnet. With each step documented along its tx.