-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6beca0
commit 2e0b552
Showing
17 changed files
with
307 additions
and
187 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
packages/bridge-ui/src/api/domain/interfaces/INFTRepository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { NFT } from '$api/domain/models/NFT'; | ||
import type { FetchNftArgs } from '$api/infrastructure/types/common'; | ||
|
||
export interface INFTRepository { | ||
findByAddress({ address, chainId, refresh }: FetchNftArgs): Promise<NFT[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { Address } from 'viem'; | ||
|
||
import type { INFTRepository } from '../interfaces/INFTRepository'; | ||
import type { NFT } from '../models/NFT'; | ||
|
||
export class NFTService { | ||
constructor(private repository: INFTRepository) {} | ||
|
||
async fetchNFTsByAddress({ | ||
address, | ||
chainId, | ||
refresh, | ||
}: { | ||
address: Address; | ||
chainId: number; | ||
refresh: boolean; | ||
}): Promise<NFT[]> { | ||
return await this.repository.findByAddress({ address, chainId, refresh }); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/bridge-ui/src/api/infrastructure/api/MoralisNFTRepository.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import Moralis from 'moralis'; | ||
import { type Address, zeroAddress } from 'viem'; | ||
|
||
import type { INFTRepository } from '$api/domain/interfaces/INFTRepository'; | ||
import type { NFT } from '$api/domain/models/NFT'; | ||
import { mapToNFTFromMoralis } from '$api/infrastructure/mappers/nft/MoralisNFTMapper'; | ||
import type { NFTApiData } from '$api/infrastructure/types/moralis'; | ||
import { moralisApiConfig } from '$config'; | ||
import { MORALIS_API_KEY } from '$env/static/private'; | ||
|
||
import type { FetchNftArgs } from '../types/common'; | ||
|
||
class MoralisNFTRepository implements INFTRepository { | ||
private static instance: MoralisNFTRepository; | ||
|
||
private cursor: string; | ||
private lastFetchedAddress: Address; | ||
private hasFetchedAll: boolean; | ||
|
||
private nfts: NFT[] = []; | ||
|
||
private constructor() { | ||
Moralis.start({ | ||
apiKey: MORALIS_API_KEY, | ||
}).catch(console.error); | ||
|
||
this.cursor = ''; | ||
this.lastFetchedAddress = zeroAddress; | ||
this.hasFetchedAll = false; | ||
} | ||
|
||
public static getInstance(): MoralisNFTRepository { | ||
if (!MoralisNFTRepository.instance) { | ||
MoralisNFTRepository.instance = new MoralisNFTRepository(); | ||
} | ||
return MoralisNFTRepository.instance; | ||
} | ||
|
||
async findByAddress({ address, chainId, refresh = false }: FetchNftArgs): Promise<NFT[]> { | ||
this.lastFetchedAddress = address; | ||
|
||
if (refresh) { | ||
this.reset(); | ||
} | ||
if (this.hasFetchedAll) { | ||
return this.nfts; | ||
} | ||
|
||
try { | ||
const response = await Moralis.EvmApi.nft.getWalletNFTs({ | ||
cursor: this.getCursor(address, refresh), | ||
chain: chainId, | ||
excludeSpam: moralisApiConfig.excludeSpam, | ||
mediaItems: moralisApiConfig.mediaItems, | ||
address: address, | ||
limit: moralisApiConfig.limit, | ||
}); | ||
|
||
this.cursor = response.pagination.cursor || ''; | ||
this.hasFetchedAll = !this.cursor; // If there is no cursor, we have fetched all NFTs | ||
|
||
const mappedData = response.result.map((nft) => mapToNFTFromMoralis(nft as unknown as NFTApiData, chainId)); | ||
this.nfts = [...this.nfts, ...mappedData]; | ||
return this.nfts; | ||
} catch (e) { | ||
console.error('Failed to fetch NFTs from Moralis:', e); | ||
return []; | ||
} | ||
} | ||
|
||
private reset(): void { | ||
this.cursor = ''; | ||
this.hasFetchedAll = false; | ||
this.nfts = []; | ||
} | ||
|
||
private getCursor(address: Address, refresh: boolean): string { | ||
if (this.lastFetchedAddress !== address || refresh) { | ||
return ''; | ||
} | ||
return this.cursor || ''; | ||
} | ||
} | ||
|
||
export default MoralisNFTRepository.getInstance(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { Address } from 'viem'; | ||
|
||
export type FetchNftArgs = { | ||
address: Address; | ||
chainId: number; | ||
refresh: boolean; | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.