levana_perpswap_cosmos/contracts/position_token/entry.rs
1//! Entrypoint messages for position token proxy
2use std::num::ParseIntError;
3
4use crate::contracts::market::position::PositionId;
5
6use super::{Approval, Metadata};
7use crate::prelude::*;
8use cosmwasm_schema::QueryResponses;
9use cosmwasm_std::{Addr, Binary};
10use cw_utils::Expiration;
11
12/// Instantiate a new position token proxy contract
13#[cw_serde]
14pub struct InstantiateMsg {
15 /// The factory address
16 pub factory: RawAddr,
17 /// Unique market identifier, also used for `symbol` in ContractInfo response
18 pub market_id: MarketId,
19}
20
21/// Execute messages for a position token proxy
22///
23/// Matches the CW721 standard.
24#[cw_serde]
25pub enum ExecuteMsg {
26 /// Transfer is a base message to move a token to another account without triggering actions
27 TransferNft {
28 /// Recipient of the NFT (position)
29 recipient: RawAddr,
30 /// Position ID, represented as a `String` to match the NFT spec
31 token_id: String,
32 },
33 /// Send is a base message to transfer a token to a contract and trigger an action
34 /// on the receiving contract.
35 SendNft {
36 /// Contract to receive the position
37 contract: RawAddr,
38 /// Position ID, represented as a `String` to match the NFT spec
39 token_id: String,
40 /// Message to execute on the contract
41 msg: Binary,
42 },
43 /// Allows operator to transfer / send the token from the owner's account.
44 /// If expiration is set, then this allowance has a time/height limit
45 Approve {
46 /// Address that is allowed to spend the NFT
47 spender: RawAddr,
48 /// Position ID, represented as a `String` to match the NFT spec
49 token_id: String,
50 /// When the approval expires
51 expires: Option<Expiration>,
52 },
53 /// Remove previously granted Approval
54 Revoke {
55 /// Address that is no longer allowed to spend the NFT
56 spender: RawAddr,
57 /// Position ID, represented as a `String` to match the NFT spec
58 token_id: String,
59 },
60 /// Allows operator to transfer / send any token from the owner's account.
61 /// If expiration is set, then this allowance has a time/height limit
62 ApproveAll {
63 /// Address that is allowed to spend all NFTs by the sending wallet
64 operator: RawAddr,
65 /// When the approval expires
66 expires: Option<Expiration>,
67 },
68 /// Remove previously granted ApproveAll permission
69 RevokeAll {
70 /// Address that is no longer allowed to spend all NFTs
71 operator: RawAddr,
72 },
73}
74
75impl ExecuteMsg {
76 /// Get the position ID from this message, if there is one.
77 pub fn get_position_id(&self) -> Result<Option<PositionId>, ParseIntError> {
78 match self {
79 ExecuteMsg::TransferNft {
80 recipient: _,
81 token_id,
82 }
83 | ExecuteMsg::SendNft {
84 contract: _,
85 token_id,
86 msg: _,
87 }
88 | ExecuteMsg::Approve {
89 spender: _,
90 token_id,
91 expires: _,
92 }
93 | ExecuteMsg::Revoke {
94 spender: _,
95 token_id,
96 } => token_id.parse().map(Some),
97 ExecuteMsg::ApproveAll {
98 operator: _,
99 expires: _,
100 }
101 | ExecuteMsg::RevokeAll { operator: _ } => Ok(None),
102 }
103 }
104}
105
106/// Query messages for a position token proxy
107///
108/// Matches the CW721 standard.
109#[cw_serde]
110#[derive(QueryResponses)]
111pub enum QueryMsg {
112 //*************** CW-721 SPEC *********************//
113 /// * returns [OwnerOfResponse]
114 ///
115 /// Return the owner of the given token, error if token does not exist
116 #[returns(OwnerOfResponse)]
117 OwnerOf {
118 /// Position ID, represented as a `String` to match the NFT spec
119 token_id: String,
120 /// unset or false will filter out expired approvals, you must set to true to see them
121 include_expired: Option<bool>,
122 },
123
124 /// * returns [ApprovalResponse]
125 ///
126 /// Return operator that can access all of the owner's tokens.
127 #[returns(ApprovalResponse)]
128 Approval {
129 /// Position ID, represented as a `String` to match the NFT spec
130 token_id: String,
131 /// Spender
132 spender: RawAddr,
133 /// Should we include expired approvals?
134 include_expired: Option<bool>,
135 },
136
137 /// * returns [ApprovalsResponse]
138 ///
139 /// Return approvals that a token has
140 #[returns(ApprovalsResponse)]
141 Approvals {
142 /// Position ID, represented as a `String` to match the NFT spec
143 token_id: String,
144 /// Should we include expired approvals?
145 include_expired: Option<bool>,
146 },
147
148 /// * returns [OperatorsResponse]
149 ///
150 /// List all operators that can access all of the owner's tokens
151 #[returns(OperatorsResponse)]
152 AllOperators {
153 /// Position ID, represented as a `String` to match the NFT spec
154 owner: RawAddr,
155 /// unset or false will filter out expired items, you must set to true to see them
156 include_expired: Option<bool>,
157 /// Last operator seen
158 start_after: Option<String>,
159 /// How many operators to return
160 limit: Option<u32>,
161 },
162
163 /// * returns [NumTokensResponse]
164 ///
165 /// Total number of tokens issued
166 #[returns(NumTokensResponse)]
167 NumTokens {},
168
169 /// * returns [NftContractInfo]
170 ///
171 /// Returns top-level metadata about the contract: `ContractInfoResponse`
172 #[returns(NftContractInfo)]
173 ContractInfo {},
174
175 /// * returns [NftInfoResponse]
176 ///
177 /// Returns metadata for a given token/position
178 /// the format is based on the *ERC721 Metadata JSON Schema*
179 /// but directly from the contract: `NftInfoResponse`
180 #[returns(NftInfoResponse)]
181 NftInfo {
182 /// Position ID, represented as a `String` to match the NFT spec
183 token_id: String,
184 },
185
186 /// * returns [AllNftInfoResponse]
187 ///
188 /// Returns the result of both `NftInfo` and `OwnerOf` as one query as an optimization
189 /// for clients: `AllNftInfo`
190 #[returns(AllNftInfoResponse)]
191 AllNftInfo {
192 /// Position ID, represented as a `String` to match the NFT spec
193 token_id: String,
194 /// unset or false will filter out expired approvals, you must set to true to see them
195 include_expired: Option<bool>,
196 },
197
198 /// * returns [TokensResponse]
199 ///
200 /// Returns all tokens owned by the given address, [] if unset.
201 #[returns(TokensResponse)]
202 Tokens {
203 /// Owner to enumerate over
204 owner: RawAddr,
205 /// Last position ID seen
206 start_after: Option<String>,
207 /// Number of positions to return
208 limit: Option<u32>,
209 },
210
211 /// * returns [TokensResponse]
212 ///
213 /// Requires pagination. Lists all token_ids controlled by the contract.
214 #[returns(TokensResponse)]
215 AllTokens {
216 /// Last position ID seen
217 start_after: Option<String>,
218 /// Number of positions to return
219 limit: Option<u32>,
220 },
221
222 //*************** PROPRIETARY *********************//
223 /// * returns [cw2::ContractVersion]
224 #[returns(cw2::ContractVersion)]
225 Version {},
226}
227
228/// Placeholder migration message
229#[cw_serde]
230pub struct MigrateMsg {}
231
232/// Response for [QueryMsg::OwnerOf]
233#[cw_serde]
234pub struct OwnerOfResponse {
235 /// Owner of the token
236 pub owner: Addr,
237 /// If set this address is approved to transfer/send the token as well
238 pub approvals: Vec<Approval>,
239}
240
241/// Response for [QueryMsg::Approval]
242#[cw_serde]
243pub struct ApprovalResponse {
244 /// Approval information
245 pub approval: Approval,
246}
247
248/// Response for [QueryMsg::Approvals]
249#[cw_serde]
250pub struct ApprovalsResponse {
251 /// Approval information
252 pub approvals: Vec<Approval>,
253}
254
255/// Response for [QueryMsg::AllOperators]
256#[cw_serde]
257pub struct OperatorsResponse {
258 /// Operator approval information
259 pub operators: Vec<Approval>,
260}
261
262/// Response for [QueryMsg::NumTokens]
263#[cw_serde]
264pub struct NumTokensResponse {
265 /// Total number of tokens in the protocol
266 pub count: u64,
267}
268
269/// Response for [QueryMsg::ContractInfo]
270#[cw_serde]
271pub struct NftContractInfo {
272 /// Name of this contract
273 pub name: String,
274 /// Ticker symbol for this contract
275 pub symbol: String,
276}
277
278/// Response for [QueryMsg::NftInfo]
279#[cw_serde]
280pub struct NftInfoResponse {
281 /// You can add any custom metadata here when you extend cw721-base
282 pub extension: Metadata,
283}
284
285/// Response for [QueryMsg::AllNftInfo]
286#[cw_serde]
287pub struct AllNftInfoResponse {
288 /// Who can transfer the token
289 pub access: OwnerOfResponse,
290 /// Data on the token itself,
291 pub info: NftInfoResponse,
292}
293
294/// Response for [QueryMsg::Tokens]
295#[cw_serde]
296pub struct TokensResponse {
297 /// Contains all token_ids in lexicographical ordering
298 /// If there are more than `limit`, use `start_from` in future queries
299 /// to achieve pagination.
300 pub tokens: Vec<String>,
301}