Skip to content

Commit

Permalink
feat: creating a cache for verified assets (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigobranas authored Sep 25, 2024
1 parent a5db047 commit c30391f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
13 changes: 6 additions & 7 deletions packages/graphql/src/graphql/resolvers/PublicResolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Provider } from 'fuels';
import { env } from '~/config';
import VerifiedAssets from '~/infra/cache/VerifiedAssets';
import AssetDAO from '~/infra/dao/AssetDAO';

type Params = {
Expand All @@ -20,10 +21,8 @@ export class PublicResolver {
const assetDAO = new AssetDAO();
const provider = await Provider.create(env.get('FUEL_PROVIDER'));
const chainId = provider.getChainId();
const response = await fetch(
'https://verified-assets.fuel.network/assets.json',
);
const verifiedAssets = await response.json();
const nonVerifiedAsset = await assetDAO.getByAssetId(_params.assetId);
const verifiedAssets = await VerifiedAssets.getInstance().fetch();
for (const verifiedAsset of verifiedAssets) {
for (const network of verifiedAsset.networks) {
if (network.type === 'fuel') {
Expand All @@ -43,16 +42,16 @@ export class PublicResolver {
const asset = Object.assign(verifiedAsset, {
assetId: _params.assetId,
contractId: network.contractId,
subId: nonVerifiedAsset?.subId,
decimals: network.decimals,
verified: true,
});
return asset;
}
}
}
const asset = await assetDAO.getByAssetId(_params.assetId);
if (!asset) return;
return Object.assign(asset, {
if (!nonVerifiedAsset) return;
return Object.assign(nonVerifiedAsset, {
verified: false,
});
}
Expand Down
31 changes: 31 additions & 0 deletions packages/graphql/src/infra/cache/VerifiedAssets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default class VerifiedAssets {
static instance: VerifiedAssets;
private lastDate?: Date;
private assets: any;

private constructor() {}

async fetch() {
const now = new Date();
if (
!this.lastDate ||
!this.assets ||
now.getTime() - this.lastDate.getTime() > 60000
) {
this.lastDate = new Date();
const response = await fetch(
'https://verified-assets.fuel.network/assets.json',
);
this.assets = await response.json();
return this.assets;
}
return this.assets;
}

static getInstance() {
if (!VerifiedAssets.instance) {
VerifiedAssets.instance = new VerifiedAssets();
}
return VerifiedAssets.instance;
}
}

0 comments on commit c30391f

Please sign in to comment.