Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/sage-api/endpoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"set_network_override": true,
"get_networks": false,
"get_network": false,
"remove_cat": true,
"resync_cat": true,
"update_cat": true,
"update_did": true,
"update_nft": true,
Expand Down
3 changes: 1 addition & 2 deletions crates/sage-database/src/tables/assets.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use chia::protocol::{Bytes32, Program};
use sqlx::{query, Row, SqliteConnection, SqliteExecutor};
use std::sync::LazyLock;

use crate::{Convert, Database, DatabaseError, DatabaseTx, Result};

pub static XCH_ASSET_HASH: LazyLock<Bytes32> = LazyLock::new(|| Bytes32::default());
pub static XCH_ASSET_HASH: Bytes32 = Bytes32::new([0; 32]);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AssetKind {
Expand Down
85 changes: 60 additions & 25 deletions crates/sage-database/src/tables/offers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async fn offer_assets(
is_visible: row.is_visible,
created_height: row.created_height.map(|h| h as u32),
icon_url: row.icon_url,
kind: kind,
kind,
name: row.name,
},
amount: row.amount.convert()?,
Expand Down Expand Up @@ -191,19 +191,32 @@ async fn offer_xch_assets(

async fn insert_offer(conn: impl SqliteExecutor<'_>, offer: OfferRow) -> Result<()> {
let offer_id_ref = offer.offer_id.as_ref();

let expiration_height: Option<i64> = offer.expiration_height.map(Into::into);
let expiration_timestamp: Option<i64> = offer
.expiration_timestamp
.map(TryInto::try_into)
.transpose()?;
let inserted_timestamp: i64 = offer.inserted_timestamp.try_into()?;

sqlx::query(
"INSERT INTO offers (hash, encoded_offer, fee, status, expiration_height, expiration_timestamp, inserted_timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?)",
"
INSERT INTO offers (
hash, encoded_offer, fee, status,
expiration_height, expiration_timestamp, inserted_timestamp
)
VALUES (?, ?, ?, ?, ?, ?, ?)
",
)
.bind(offer_id_ref)
.bind(offer.encoded_offer)
.bind(offer.fee as i64)
.bind(offer.status as u8)
.bind(offer.expiration_height.map(|h| h as i64))
.bind(offer.expiration_timestamp.map(|t| t as i64))
.bind(offer.inserted_timestamp as i64)
.execute(conn)
.await?;
.bind(offer_id_ref)
.bind(offer.encoded_offer)
.bind(i64::try_from(offer.fee)?)
.bind(offer.status as u8)
.bind(expiration_height)
.bind(expiration_timestamp)
.bind(inserted_timestamp)
.execute(conn)
.await?;
Ok(())
}

Expand All @@ -217,17 +230,28 @@ async fn insert_offer_asset(
) -> Result<()> {
let offer_id_ref = offer_id.as_ref();
let asset_id_ref = asset_id.as_ref();

let amount: i64 = amount.try_into()?;
let royalty: i64 = royalty.try_into()?;

sqlx::query(
"INSERT INTO offer_assets (offer_id, asset_id, amount, royalty, is_requested)
VALUES ((SELECT id FROM offers WHERE hash = ?), (SELECT id FROM assets WHERE hash = ?), ?, ?, ?)",
"
INSERT INTO offer_assets (offer_id, asset_id, amount, royalty, is_requested)
VALUES (
(SELECT id FROM offers WHERE hash = ?),
(SELECT id FROM assets WHERE hash = ?),
?, ?, ?
)
",
)
.bind(offer_id_ref)
.bind(asset_id_ref)
.bind(amount as i64)
.bind(royalty as i64)
.bind(is_requested)
.execute(conn)
.await?;
.bind(offer_id_ref)
.bind(asset_id_ref)
.bind(amount)
.bind(royalty)
.bind(is_requested)
.execute(conn)
.await?;

Ok(())
}

Expand All @@ -239,16 +263,26 @@ async fn insert_offer_xch(
is_requested: bool,
) -> Result<()> {
let offer_id_ref = offer_hash.as_ref();

let xch: i64 = xch.try_into()?;
let royalty: i64 = royalty.try_into()?;

sqlx::query(
"INSERT INTO offer_assets (offer_id, asset_id, amount, royalty, is_requested)
VALUES ((SELECT id FROM offers WHERE hash = ?), 0, ?, ?, ?)",
"
INSERT INTO offer_assets (offer_id, asset_id, amount, royalty, is_requested)
VALUES (
(SELECT id FROM offers WHERE hash = ?),
0, ?, ?, ?
)
",
)
.bind(offer_id_ref)
.bind(xch as i64)
.bind(royalty as i64)
.bind(xch)
.bind(royalty)
.bind(is_requested)
.execute(conn)
.await?;

Ok(())
}

Expand All @@ -267,6 +301,7 @@ async fn insert_offered_coin(
.bind(coin_hash_ref)
.execute(conn)
.await?;

Ok(())
}

Expand Down