Skip to content

Commit

Permalink
Merge pull request #192 from alephium/use-nft-collection-metadata-end…
Browse files Browse the repository at this point in the history
…point

Fetch NFT Metadata From Explorer Backend
  • Loading branch information
h0ngcha0 authored Apr 26, 2024
2 parents ae4d014 + cad5a0d commit 22705ab
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 32 deletions.
8 changes: 4 additions & 4 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"main": "index.js",
"license": "MIT",
"devDependencies": {
"@alephium/get-extension-wallet": "^0.24.0",
"@alephium/get-extension-wallet": "^0.39.1",
"@alephium/ledger-app": "^0.2.1",
"@alephium/token-list": "0.0.12",
"@alephium/web3": "^0.24.0",
"@alephium/web3-test": "^0.24.0",
"@alephium/web3-wallet": "^0.24.0",
"@alephium/web3": "^0.39.1",
"@alephium/web3-test": "^0.39.1",
"@alephium/web3-wallet": "^0.39.1",
"@ledgerhq/hw-transport-webusb": "6.27.17",
"@ledgerhq/hw-transport-webhid": "6.27.17",
"@playwright/test": "^1.23.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
import { NFT, CollectionAndNFTMap } from "./alephium-nft.model"
import { NFTCollection } from "./alephium-nft.model"
import { Network } from "../../../shared/network"
import { addressFromContractId, binToHex, contractIdFromAddress, ExplorerProvider, NodeProvider } from "@alephium/web3"
import { fetchImmutable } from "../../../shared/utils/fetchImmutable"
import { ExplorerProvider, NodeProvider } from "@alephium/web3"

export const fetchCollectionAndNfts = async (
nonFungibleTokenIds: string[],
network: Network
): Promise<CollectionAndNFTMap> => {
const explorerProvider = new ExplorerProvider(network.explorerApiUrl)
const nodeProvider = new NodeProvider(network.nodeUrl)

const parentAndTokenIds: CollectionAndNFTMap = {}
for (const tokenId of nonFungibleTokenIds) {
try {
const result = await fetchImmutable(`${tokenId}-parent`, () => explorerProvider.contracts.getContractsContractParent(addressFromContractId(tokenId)))
if (result.parent) {
const parentContractId = binToHex(contractIdFromAddress(result.parent))
const isFollowNFTCollectionStd = await fetchImmutable(`${parentContractId}-std`, () => nodeProvider.guessFollowsNFTCollectionStd(parentContractId))

// Guess if parent implements the NFT collection standard interface
if (isFollowNFTCollectionStd) {
if (parentAndTokenIds[parentContractId]) {
parentAndTokenIds[parentContractId].push(tokenId)
} else {
parentAndTokenIds[parentContractId] = [tokenId]
}
}
}
} catch (e) {
console.error(`Error fetching parent for collection id for NFT ${tokenId}`, e)
const nftMetadataz = await explorerProvider.tokens.postTokensNftMetadata(nonFungibleTokenIds)
for (const nftMetadata of nftMetadataz) {
const tokenId = nftMetadata.id
const collectionId = nftMetadata.collectionId
if (parentAndTokenIds[collectionId]) {
parentAndTokenIds[collectionId].push(tokenId)
} else {
parentAndTokenIds[collectionId] = [tokenId]
}
}
return parentAndTokenIds
Expand Down
58 changes: 52 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
"@alephium/web3" "^0.24.0"
bowser "^2.11.0"

"@alephium/get-extension-wallet@^0.39.1":
version "0.39.1"
resolved "https://registry.yarnpkg.com/@alephium/get-extension-wallet/-/get-extension-wallet-0.39.1.tgz#41a836e3abc1f3e51e09ac068ed81b299b58758d"
integrity sha512-p11TdPzbdozSH7Xk9nV3Z6TSTNesSAmEKrJTH6qEXGRTfmdoX3IrrK6urMBQ89cFt2BacJTw/QuKjwO15ZeadQ==
dependencies:
"@alephium/web3" "^0.39.1"
bowser "^2.11.0"

"@alephium/ledger-app@^0.2.1":
version "0.2.1"
resolved "https://registry.yarnpkg.com/@alephium/ledger-app/-/ledger-app-0.2.1.tgz#703dfcee3d6566aa5c4e48d56603b53bc4db292a"
Expand All @@ -68,13 +76,13 @@
"@alephium/web3" "^0.14.0"
"@alephium/web3-wallet" "^0.14.0"

"@alephium/web3-test@^0.24.0":
version "0.24.0"
resolved "https://registry.yarnpkg.com/@alephium/web3-test/-/web3-test-0.24.0.tgz#bb7f5ec2c468fc7ce93a90f733841acdabcac873"
integrity sha512-UYSU+5spj0dsILbvFXWgB2Gm9ibyZ7u2L1KPuqJ5z4x6ypW/askSkWGBvnt6RDCA3FAqbTMDq80oNwQ1bFNmCw==
"@alephium/web3-test@^0.39.1":
version "0.39.1"
resolved "https://registry.yarnpkg.com/@alephium/web3-test/-/web3-test-0.39.1.tgz#6bd8a6b57abfc56a897d7d83dc570fcc143ba82c"
integrity sha512-Cc8Kuer9rT7LMZDBk3Fx0O+5tnS8SNLP3DOY+aLR4GX3Ou1Zqislgbxz8bNclhZDguP7qfDRn7J3ndQYSSuc5Q==
dependencies:
"@alephium/web3" "^0.24.0"
"@alephium/web3-wallet" "^0.24.0"
"@alephium/web3" "^0.39.1"
"@alephium/web3-wallet" "^0.39.1"

"@alephium/web3-wallet@^0.14.0":
version "0.14.0"
Expand Down Expand Up @@ -104,6 +112,20 @@
elliptic "6.5.4"
fs-extra "10.0.1"

"@alephium/web3-wallet@^0.39.1":
version "0.39.1"
resolved "https://registry.yarnpkg.com/@alephium/web3-wallet/-/web3-wallet-0.39.1.tgz#4cbb029a21cc050069ae2f99a568faafe5ee2f8d"
integrity sha512-sZAF45GuYc/G2L9I+tg76m9rORD0aS2LpUZZ/jwKLz8MecFAGEprvzxdJTXBiGc+n4TzR3C07+DcEYCq4TiqJw==
dependencies:
"@alephium/web3" "^0.39.1"
"@noble/secp256k1" "1.7.1"
"@types/node" "^16.18.23"
bip32 "3.1.0"
bip39 "3.0.4"
buffer "^6.0.3"
elliptic "6.5.4"
fs-extra "10.0.1"

"@alephium/web3@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@alephium/web3/-/web3-0.14.0.tgz#b7b4fbaad8200661acb68e499fca06723d59d400"
Expand Down Expand Up @@ -140,6 +162,25 @@
path-browserify "^1.0.1"
stream-browserify "^3.0.0"

"@alephium/web3@^0.39.1":
version "0.39.1"
resolved "https://registry.yarnpkg.com/@alephium/web3/-/web3-0.39.1.tgz#d26e3f418e61bea631b8f93b9b1d825925dbad5c"
integrity sha512-45F4yrFOkEsOjpDpQY/eg3uhi5kE2yU63CkMiMc6L9cChyn7VaaD6bsugrFTUa2gieXgkAqy4v7y5qhkQOuXHA==
dependencies:
"@noble/secp256k1" "1.7.1"
base-x "4.0.0"
bignumber.js "^9.1.1"
binary-parser "2.2.1"
blakejs "1.2.1"
bn.js "5.2.1"
buffer "^6.0.3"
cross-fetch "^3.1.5"
crypto-browserify "^3.12.0"
elliptic "6.5.4"
eventemitter3 "^4.0.7"
path-browserify "^1.0.1"
stream-browserify "^3.0.0"

"@alloc/quick-lru@^5.2.0":
version "5.2.0"
resolved "https://registry.yarnpkg.com/@alloc/quick-lru/-/quick-lru-5.2.0.tgz#7bf68b20c0a350f936915fcae06f58e32007ce30"
Expand Down Expand Up @@ -8546,6 +8587,11 @@ binary-extensions@^2.0.0:
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==

binary-parser@2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/binary-parser/-/binary-parser-2.2.1.tgz#4edc6da2dc56db73fa5ba450dfe6382ede8294ce"
integrity sha512-5ATpz/uPDgq5GgEDxTB4ouXCde7q2lqAQlSdBRQVl/AJnxmQmhIfyxJx+0MGu//D5rHQifkfGbWWlaysG0o9NA==

bindings@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
Expand Down

0 comments on commit 22705ab

Please sign in to comment.