Skip to content

Commit 5a6408e

Browse files
authored
Merge pull request #693 from dkackman/bug-692
no SVG thumbnails on testnet
2 parents 07ce637 + e286aff commit 5a6408e

File tree

6 files changed

+51
-24
lines changed

6 files changed

+51
-24
lines changed

crates/sage-assets/src/nfts/fetch_nft_uri.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct Data {
2121
pub thumbnail: Option<Thumbnail>,
2222
}
2323

24-
pub async fn fetch_uri(uri: String) -> Result<Data, UriError> {
24+
pub async fn fetch_uri(uri: String, testnet: bool) -> Result<Data, UriError> {
2525
let response = reqwest::get(&uri).await?;
2626

2727
let mime_type = match response.headers().get(CONTENT_TYPE) {
@@ -49,7 +49,7 @@ pub async fn fetch_uri(uri: String) -> Result<Data, UriError> {
4949
hasher.update(&blob);
5050
let hash = Bytes32::new(hasher.finalize());
5151

52-
let mut thumbnail = match mintgarden_thumbnail(hash).await {
52+
let mut thumbnail = match mintgarden_thumbnail(hash, testnet).await {
5353
Ok(thumbnail) => thumbnail,
5454
Err(error) => {
5555
debug!("Failed to fetch MintGarden thumbnail for {uri}: {error}");
@@ -91,11 +91,11 @@ pub async fn fetch_uri(uri: String) -> Result<Data, UriError> {
9191
})
9292
}
9393

94-
pub async fn fetch_uris_without_hash(uris: Vec<String>) -> Result<Data, UriError> {
94+
pub async fn fetch_uris_without_hash(uris: Vec<String>, testnet: bool) -> Result<Data, UriError> {
9595
let mut futures = FuturesUnordered::new();
9696

9797
for uri in uris {
98-
futures.push(fetch_uri(uri));
98+
futures.push(fetch_uri(uri, testnet));
9999
}
100100

101101
let mut data = None;
@@ -128,11 +128,11 @@ pub async fn fetch_uris_without_hash(uris: Vec<String>) -> Result<Data, UriError
128128
data.ok_or(UriError::NoUris)
129129
}
130130

131-
pub async fn fetch_uris_with_hash(uris: Vec<String>, hash: Bytes32) -> Option<Data> {
131+
pub async fn fetch_uris_with_hash(uris: Vec<String>, hash: Bytes32, testnet: bool) -> Option<Data> {
132132
let mut futures = FuturesUnordered::new();
133133

134134
for uri in uris {
135-
futures.push(async move { (uri.clone(), fetch_uri(uri).await) });
135+
futures.push(async move { (uri.clone(), fetch_uri(uri, testnet).await) });
136136
}
137137

138138
while let Some((uri, result)) = futures.next().await {
@@ -151,8 +151,14 @@ pub async fn fetch_uris_with_hash(uris: Vec<String>, hash: Bytes32) -> Option<Da
151151
None
152152
}
153153

154-
pub async fn mintgarden_thumbnail(data_hash: Bytes32) -> Result<Option<Thumbnail>, UriError> {
155-
let url = format!("https://assets.mainnet.mintgarden.io/thumbnails/{data_hash}_512.webp");
154+
pub async fn mintgarden_thumbnail(
155+
data_hash: Bytes32,
156+
testnet: bool,
157+
) -> Result<Option<Thumbnail>, UriError> {
158+
let url = format!(
159+
"https://assets.{}.mintgarden.io/thumbnails/{data_hash}_512.webp",
160+
if testnet { "testnet" } else { "mainnet" }
161+
);
156162

157163
let response = reqwest::get(&url).await?;
158164

crates/sage-wallet/src/queues/nft_uri_queue.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@ use tokio::{
1111
use tracing::{debug, info, warn};
1212

1313
use crate::{compute_nft_info, SyncEvent, WalletError};
14-
14+
use chia_wallet_sdk::types::TESTNET11_CONSTANTS;
15+
use sage_config::Network;
1516
#[derive(Debug)]
1617
pub struct NftUriQueue {
1718
db: Database,
1819
sync_sender: mpsc::Sender<SyncEvent>,
20+
network: Network,
1921
}
2022

2123
impl NftUriQueue {
22-
pub fn new(db: Database, sync_sender: mpsc::Sender<SyncEvent>) -> Self {
23-
Self { db, sync_sender }
24+
pub fn new(db: Database, sync_sender: mpsc::Sender<SyncEvent>, network: Network) -> Self {
25+
Self {
26+
db,
27+
sync_sender,
28+
network,
29+
}
2430
}
2531

2632
pub async fn start(self, delay: Duration) -> Result<(), WalletError> {
@@ -40,10 +46,14 @@ impl NftUriQueue {
4046
info!("Processing batch of {} NFT URIs", batch.len());
4147

4248
let mut futures = FuturesUnordered::new();
43-
49+
let testnet = self.network.genesis_challenge == TESTNET11_CONSTANTS.genesis_challenge;
4450
for item in batch {
4551
futures.push(async move {
46-
let result = timeout(Duration::from_secs(15), fetch_uri(item.uri.clone())).await;
52+
let result = timeout(
53+
Duration::from_secs(15),
54+
fetch_uri(item.uri.clone(), testnet),
55+
)
56+
.await;
4757
(item, result)
4858
});
4959
}

crates/sage-wallet/src/sync_manager.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,12 @@ impl SyncManager {
433433

434434
if self.nft_uri_queue_task.is_none() && !self.options.testing {
435435
let task = tokio::spawn(
436-
NftUriQueue::new(wallet.db.clone(), self.event_sender.clone())
437-
.start(self.options.timeouts.nft_uri_delay),
436+
NftUriQueue::new(
437+
wallet.db.clone(),
438+
self.event_sender.clone(),
439+
self.network.clone(),
440+
)
441+
.start(self.options.timeouts.nft_uri_delay),
438442
);
439443
self.nft_uri_queue_task = Some(task);
440444
}

crates/sage/src/endpoints/offers.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use chia::puzzles::nft::NftMetadata;
33
use chia_wallet_sdk::{
44
driver::{decode_offer, encode_offer, DriverError, Offer, SpendContext},
55
signer::AggSigConstants,
6+
types::TESTNET11_CONSTANTS,
67
utils::Address,
78
};
89
use itertools::Itertools;
@@ -304,14 +305,16 @@ impl Sage {
304305
});
305306
}
306307

308+
let testnet = self.network().genesis_challenge == TESTNET11_CONSTANTS.genesis_challenge;
309+
307310
for nft in offer.offered_coins().nfts.values() {
308311
let _info = if let Ok(metadata) = ctx.extract::<NftMetadata>(nft.info.metadata.ptr()) {
309312
let mut confirmation_info = ConfirmationInfo::default();
310313

311314
if let Some(hash) = metadata.data_hash {
312315
if let Ok(Some(data)) = timeout(
313316
Duration::from_secs(10),
314-
fetch_uris_with_hash(metadata.data_uris.clone(), hash),
317+
fetch_uris_with_hash(metadata.data_uris.clone(), hash, testnet),
315318
)
316319
.await
317320
{
@@ -322,7 +325,7 @@ impl Sage {
322325
if let Some(hash) = metadata.metadata_hash {
323326
if let Ok(Some(data)) = timeout(
324327
Duration::from_secs(10),
325-
fetch_uris_with_hash(metadata.metadata_uris.clone(), hash),
328+
fetch_uris_with_hash(metadata.metadata_uris.clone(), hash, testnet),
326329
)
327330
.await
328331
{
@@ -395,7 +398,7 @@ impl Sage {
395398
if let Some(hash) = metadata.data_hash {
396399
if let Ok(Some(data)) = timeout(
397400
Duration::from_secs(10),
398-
fetch_uris_with_hash(metadata.data_uris.clone(), hash),
401+
fetch_uris_with_hash(metadata.data_uris.clone(), hash, testnet),
399402
)
400403
.await
401404
{
@@ -406,7 +409,7 @@ impl Sage {
406409
if let Some(hash) = metadata.metadata_hash {
407410
if let Ok(Some(data)) = timeout(
408411
Duration::from_secs(10),
409-
fetch_uris_with_hash(metadata.metadata_uris.clone(), hash),
412+
fetch_uris_with_hash(metadata.metadata_uris.clone(), hash, testnet),
410413
)
411414
.await
412415
{

crates/sage/src/endpoints/transactions.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use chia::{
66
};
77
use chia_wallet_sdk::{
88
driver::{MetadataUpdate, OptionType},
9+
types::TESTNET11_CONSTANTS,
910
utils::Address,
1011
};
1112
use itertools::Itertools;
@@ -285,6 +286,7 @@ impl Sage {
285286

286287
let mut mints = Vec::with_capacity(req.mints.len());
287288
let mut info = ConfirmationInfo::default();
289+
let testnet = self.network().genesis_challenge == TESTNET11_CONSTANTS.genesis_challenge;
288290

289291
for item in req.mints {
290292
let royalty_puzzle_hash = item
@@ -301,7 +303,7 @@ impl Sage {
301303
} else {
302304
let data = timeout(
303305
Duration::from_secs(10),
304-
fetch_uris_without_hash(item.data_uris.clone()),
306+
fetch_uris_without_hash(item.data_uris.clone(), testnet),
305307
)
306308
.await??;
307309

@@ -318,7 +320,7 @@ impl Sage {
318320
} else {
319321
let metadata = timeout(
320322
Duration::from_secs(10),
321-
fetch_uris_without_hash(item.metadata_uris.clone()),
323+
fetch_uris_without_hash(item.metadata_uris.clone(), testnet),
322324
)
323325
.await??;
324326

@@ -335,7 +337,7 @@ impl Sage {
335337
} else {
336338
let data = timeout(
337339
Duration::from_secs(10),
338-
fetch_uris_without_hash(item.license_uris.clone()),
340+
fetch_uris_without_hash(item.license_uris.clone(), testnet),
339341
)
340342
.await??;
341343

crates/sage/src/utils/cache.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ impl Sage {
8181
}
8282

8383
let info = if let Ok(metadata) = NftMetadata::from_clvm(allocator, nft_metadata) {
84+
let testnet = self.network().genesis_challenge == TESTNET11_CONSTANTS.genesis_challenge;
85+
8486
if let Some(hash) = metadata.data_hash {
8587
if let Entry::Vacant(entry) = confirmation_info.nft_data.entry(hash) {
8688
if let Ok(Some(data)) = timeout(
8789
Duration::from_secs(10),
88-
fetch_uris_with_hash(metadata.data_uris.clone(), hash),
90+
fetch_uris_with_hash(metadata.data_uris.clone(), hash, testnet),
8991
)
9092
.await
9193
{
@@ -98,7 +100,7 @@ impl Sage {
98100
if let Entry::Vacant(entry) = confirmation_info.nft_data.entry(hash) {
99101
if let Ok(Some(data)) = timeout(
100102
Duration::from_secs(10),
101-
fetch_uris_with_hash(metadata.metadata_uris.clone(), hash),
103+
fetch_uris_with_hash(metadata.metadata_uris.clone(), hash, testnet),
102104
)
103105
.await
104106
{

0 commit comments

Comments
 (0)