Skip to content

Commit 42d709d

Browse files
committedMar 30, 2023
feat(storage): implements storage object logic fs URI
1 parent c5517ca commit 42d709d

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
 

‎packages/storage/src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![forbid(unsafe_code)]
2+
#![deny(
3+
warnings,
4+
rust_2018_idioms,
5+
trivial_casts,
6+
trivial_numeric_casts,
7+
unused_lifetimes,
8+
unused_import_braces,
9+
unused_qualifications
10+
)]
11+
12+
mod object;
13+
14+
pub use object::ObjectRef;

‎packages/storage/src/object.rs

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use cw_storage::msg::QueryMsg;
2+
use cw_storage::msg::QueryMsg::ObjectData;
3+
use logic_bindings::error::CosmwasmUriError;
4+
use logic_bindings::uri::CosmwasmUri;
5+
use serde::{Deserialize, Serialize};
6+
7+
const CONTRACT_NAME: &'static str = "cw-storage";
8+
9+
/// Represents a reference to an Object stored in the `cw-storage` contract.
10+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
11+
pub struct ObjectRef {
12+
/// The object id in the `cw-storage` contract.
13+
pub object_id: String,
14+
15+
/// The `cw-storage` contract address on which the object is stored.
16+
pub storage_address: String,
17+
}
18+
19+
impl TryFrom<CosmwasmUri> for ObjectRef {
20+
type Error = CosmwasmUriError;
21+
22+
fn try_from(value: CosmwasmUri) -> Result<Self, Self::Error> {
23+
let address = value.contract_address.clone();
24+
value
25+
.into_query::<QueryMsg>()
26+
.and_then(|query: QueryMsg| match query {
27+
ObjectData { id: object_id } => Ok(ObjectRef {
28+
storage_address: address,
29+
object_id,
30+
}),
31+
_ => Err(CosmwasmUriError::Malformed(
32+
"wrong query content".to_string(),
33+
)),
34+
})
35+
}
36+
}
37+
38+
impl TryFrom<ObjectRef> for CosmwasmUri {
39+
type Error = CosmwasmUriError;
40+
41+
fn try_from(value: ObjectRef) -> Result<Self, Self::Error> {
42+
CosmwasmUri::try_new(
43+
Some(CONTRACT_NAME.to_string()),
44+
value.storage_address,
45+
&ObjectData {
46+
id: value.object_id,
47+
},
48+
)
49+
}
50+
}
51+
52+
#[cfg(test)]
53+
mod tests {
54+
use super::*;
55+
56+
#[test]
57+
fn uri_to_object() {
58+
let cases = vec![
59+
(
60+
CosmwasmUri {
61+
contract_name: None,
62+
contract_address: "okp41ffzp0xmjhwkltuxcvccl0z9tyfuu7txp5ke0tpkcjpzuq9fcj3pqrteqt3".to_string(),
63+
raw_query: "{\"object_data\":{\"id\":\"4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05\"}}".to_string(),
64+
},
65+
Ok(ObjectRef {
66+
object_id: "4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05".to_string(),
67+
storage_address: "okp41ffzp0xmjhwkltuxcvccl0z9tyfuu7txp5ke0tpkcjpzuq9fcj3pqrteqt3".to_string(),
68+
}),
69+
),
70+
(
71+
CosmwasmUri {
72+
contract_name: None,
73+
contract_address: "address".to_string(),
74+
raw_query: "{\"object\":{\"id\":\"myid\"}}".to_string(),
75+
},
76+
Err(CosmwasmUriError::Malformed(
77+
"wrong query content".to_string(),
78+
)),
79+
),
80+
];
81+
82+
for case in cases {
83+
let res = ObjectRef::try_from(case.0);
84+
assert_eq!(res, case.1);
85+
}
86+
}
87+
88+
#[test]
89+
fn object_ro_uri() {
90+
let res = CosmwasmUri::try_from(ObjectRef {
91+
object_id: "4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05"
92+
.to_string(),
93+
storage_address: "okp41ffzp0xmjhwkltuxcvccl0z9tyfuu7txp5ke0tpkcjpzuq9fcj3pqrteqt3"
94+
.to_string(),
95+
});
96+
97+
assert!(res.is_ok());
98+
assert_eq!(res.unwrap().to_string(), "cosmwasm:cw-storage:okp41ffzp0xmjhwkltuxcvccl0z9tyfuu7txp5ke0tpkcjpzuq9fcj3pqrteqt3?query=%7B%22object_data%22%3A%7B%22id%22%3A%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D");
99+
}
100+
}

0 commit comments

Comments
 (0)
Please sign in to comment.