-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_token_uri.js
More file actions
35 lines (29 loc) · 1.13 KB
/
get_token_uri.js
File metadata and controls
35 lines (29 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { ethers } from 'ethers';
// ERC721 contract ABI for tokenURI function
const abi = [
"function tokenURI(uint256 tokenId) view returns (string)"
];
const contractAddress = '0x3d6670aC0A881Dcc742c17D687F5dfE05Af81cff';
const tokenId = 2;
// Use Base Sepolia RPC endpoint
const provider = new ethers.JsonRpcProvider('https://sepolia.base.org');
async function getTokenURI() {
try {
const contract = new ethers.Contract(contractAddress, abi, provider);
const tokenURI = await contract.tokenURI(tokenId);
console.log('Token URI for token 2:', tokenURI);
// If it's an IPFS URI, we might need to resolve it
if (tokenURI.startsWith('ipfs://')) {
console.log('IPFS URI detected, you may need to resolve it to https://ipfs.io/ipfs/...');
} else if (tokenURI.startsWith('http')) {
console.log('Fetching metadata from:', tokenURI);
// Try to fetch the metadata
const response = await fetch(tokenURI);
const metadata = await response.json();
console.log('Metadata:', JSON.stringify(metadata, null, 2));
}
} catch (error) {
console.error('Error:', error.message);
}
}
getTokenURI();