Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: src-7 src-9 #1653

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/brown-badgers-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

Fetch and display NFT data directly from indexer.
5 changes: 5 additions & 0 deletions .changeset/light-cooks-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": minor
---

Support SRC-7, 9 nft asset data.
5 changes: 5 additions & 0 deletions .changeset/rotten-grapes-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

Fixes intermittent long loading state on the wallet
58 changes: 13 additions & 45 deletions packages/app/src/systems/Asset/cache/AssetsCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type Endpoint = {
};

export class AssetsCache {
private cache: { [chainId: number]: { [assetId: string]: Asset } };
private static instance: AssetsCache;
private endpoints: Endpoint[] = [
{
Expand All @@ -21,12 +20,12 @@ export class AssetsCache {
url: 'https://explorer-indexer-testnet.fuel.network',
},
];
private storage: IndexedAssetsDB;

private constructor() {
this.cache = {};
this.storage = new IndexedAssetsDB();
}
asset = {
name: '',
symbol: '',
metadata: {},
};

private getIndexerEndpoint(chainId: number) {
return this.endpoints.find(
Expand Down Expand Up @@ -61,50 +60,34 @@ export class AssetsCache {
}
const endpoint = this.getIndexerEndpoint(chainId);
if (!endpoint) return;
// try to get from memory cache first
this.cache[chainId] = this.cache[chainId] || {};
const assetFromCache = this.cache[chainId][assetId];
if (assetFromCache?.name) {
return assetFromCache;
}

// get from indexed db if not in memory
const assetFromDb = await this.storage.getItem(`${chainId}/${assetId}`);
if (assetFromDb?.name) {
this.cache[chainId][assetId] = assetFromDb;
return assetFromDb;
}

const assetFromIndexer = await this.fetchAssetFromIndexer(
endpoint.url,
assetId
);

console.log('asd assetFromIndexer', assetFromIndexer);
if (!assetFromIndexer) return;

const { isNFT, ...rest } = assetFromIndexer;
const asset = {
...assetFromIndexer,
isNft: false,
...rest,
isNft: !!isNFT,
};

if (assetFromIndexer.contractId) {
const nftData = await fetchNftData({
assetId,
contractId: assetFromIndexer.contractId,
provider,
});
Object.assign(asset, nftData);
}).catch(() => undefined);
if (nftData?.name) {
Object.assign(asset, nftData);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need this as the assetFromIndexer will have the isNft already


this.cache[chainId][assetId] = asset;
this.storage.setItem(`${chainId}/${assetId}`, asset);
return asset;
}
asset = {
name: '',
symbol: '',
metadata: {},
};

static getInstance() {
if (!AssetsCache.instance) {
Expand All @@ -113,18 +96,3 @@ export class AssetsCache {
return AssetsCache.instance;
}
}

class IndexedAssetsDB {
async getItem(key: string) {
return db.transaction('r', db.indexedAssets, async () => {
const asset = await db.indexedAssets.get({ key });
return asset;
});
}

async setItem(key: string, data: AssetData) {
await db.transaction('rw', db.indexedAssets, async () => {
await db.indexedAssets.put({ key, ...data });
});
}
}
7 changes: 1 addition & 6 deletions packages/app/src/systems/Asset/utils/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ export const fetchNftData = async ({
])
.dryRun();

const [total_supply, decimals, name, symbol] = result.value;
const [_total_supply, _decimals, name, symbol] = result.value;

return {
/*
according to sway standards this is how you recognize an NFT:
https://docs.fuel.network/docs/sway-standards/src-20-native-asset/#non-fungible-asset-restrictions
*/
isNft: total_supply.toNumber() === 1 && !decimals,
name: name as string,
symbol: symbol as string,
};
Expand Down
Loading