Skip to content

Commit

Permalink
view: correctly store denom metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
hdevalence committed Aug 11, 2024
1 parent 671b576 commit c39ddc3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
33 changes: 12 additions & 21 deletions crates/view/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,14 +823,10 @@ impl Storage {

spawn_blocking(move || {
pool.get()?
.prepare_cached("SELECT * FROM assets")?
.prepare_cached("SELECT metadata FROM assets")?
.query_and_then([], |row| {
let _asset_id: Vec<u8> = row.get("asset_id")?;
let denom: String = row.get("denom")?;

let denom_metadata = asset::REGISTRY
.parse_denom(&denom)
.ok_or_else(|| anyhow::anyhow!("invalid denomination {}", denom))?;
let metadata_json = row.get::<_, String>("metadata")?;
let denom_metadata = serde_json::from_str(&metadata_json)?;

anyhow::Ok(denom_metadata)
})?
Expand All @@ -846,13 +842,10 @@ impl Storage {

spawn_blocking(move || {
pool.get()?
.prepare_cached("SELECT * FROM assets WHERE asset_id = ?1")?
.prepare_cached("SELECT metadata FROM assets WHERE asset_id = ?1")?
.query_and_then([id], |row| {
let _asset_id: Vec<u8> = row.get("asset_id")?;
let denom: String = row.get("denom")?;
let denom_metadata = asset::REGISTRY
.parse_denom(&denom)
.ok_or_else(|| anyhow::anyhow!("invalid denomination {}", denom))?;
let metadata_json = row.get::<_, String>("metadata")?;
let denom_metadata = serde_json::from_str(&metadata_json)?;
anyhow::Ok(denom_metadata)
})?
.next()
Expand All @@ -870,13 +863,10 @@ impl Storage {

spawn_blocking(move || {
pool.get()?
.prepare_cached("SELECT * FROM assets WHERE denom LIKE ?1 ESCAPE '\\'")?
.prepare_cached("SELECT metadata FROM assets WHERE denom LIKE ?1 ESCAPE '\\'")?
.query_and_then([pattern], |row| {
let _asset_id: Vec<u8> = row.get("asset_id")?;
let denom: String = row.get("denom")?;
let denom_metadata = asset::REGISTRY
.parse_denom(&denom)
.ok_or_else(|| anyhow::anyhow!("invalid denomination {}", denom))?;
let metadata_json = row.get::<_, String>("metadata")?;
let denom_metadata = serde_json::from_str(&metadata_json)?;
anyhow::Ok(denom_metadata)
})?
.collect()
Expand Down Expand Up @@ -1066,14 +1056,15 @@ impl Storage {

let asset_id = asset.id().to_bytes().to_vec();
let denom = asset.base_denom().denom;
let metadata_json = serde_json::to_string(&asset)?;

let pool = self.pool.clone();

spawn_blocking(move || {
pool.get()?
.execute(
"INSERT OR IGNORE INTO assets (asset_id, denom) VALUES (?1, ?2)",
(asset_id, denom),
"INSERT OR REPLACE INTO assets (asset_id, denom, metadata) VALUES (?1, ?2, ?3)",
(asset_id, denom, metadata_json),
)
.map_err(anyhow::Error::from)
})
Expand Down
3 changes: 2 additions & 1 deletion crates/view/src/storage/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ CREATE TABLE sync_height (height BIGINT NOT NULL);
-- used for storing a cache of known assets
CREATE TABLE assets (
asset_id BLOB PRIMARY KEY NOT NULL,
denom TEXT NOT NULL
denom TEXT NOT NULL,
metadata TEXT NOT NULL
);

-- the shape information about the sct
Expand Down

0 comments on commit c39ddc3

Please sign in to comment.