DEPRECATED - See Anima Reputation V2 for the latest information.
Anima Reputation SBT System Overview
This guide explains how to interact with the Anima Reputation Soulbound Token (SBT) system using Solidity functions to retrieve token metadata and query specific attributes.
Smart Contract Interaction
The Anima SBT smart contract allows you to retrieve metadata associated with a user's token, which contains information such as the reputation level and the last verification timestamp. Below are the key functions for interacting with the contract:
1. getMasterToken(address account)
This function retrieves the SBT token ID associated with a specific address.
function getMasterToken(address account) public view returns (uint256)
- Input: Ethereum address (
account
) - Output: Token ID (
uint256
) linked to the user's account
2. tokenURI(uint256 tokenId)
This function retrieves the metadata URL associated with a specific token ID.
function tokenURI(uint256 tokenId) public view virtual returns (string memory)
- Input: Token ID (
uint256
) - Output: Metadata URL (
string
) containing the SBT's attributes and other data.
Querying the Metadata
To retrieve the metadata for a specific user, you can combine the two functions as follows:
string memory metadata_url = tokenURI(getMasterToken(user_address));
This will return the URL of the metadata JSON file for the SBT associated with the provided user address.
Example of Metadata Structure
The metadata returned is in JSON format and includes various attributes related to the user's reputation.
{
"name": "Reputation Level #1",
"image": "https://api2.aleph.im/api/v0/storage/raw/9fb396401fc8fecc1ce180c1f302acb768859dc605990569ce854cc1017e630f",
"attributes": [
{
"value": 1,
"trait_type": "Reputation Level"
},
{
"value": 1709223352,
"trait_type": "Last Verification"
}
],
"external_url": "https://reputation.anima.io"
}
Key Metadata Fields:
- name: The name of the reputation level (e.g., "Reputation Level #1").
- image: URL of the image representing the reputation.
- attributes:
Reputation Level
: Numeric value representing the user's current reputation level.Last Verification
: Timestamp of the user's last verification event.- external_url: Link to the Anima Reputation SBT service.
Smart Contract Addresses by Chain
The Anima Reputation SBT smart contract is deployed on different blockchains. Below are the addresses for each supported chain:
- Polygon:
0xd0Deb392D834C3733a9a18d9066642f8B12567e1
- Linea:
0x59C512b57900d6FFaf080674d99A194729401f6F
- Base:
0xAd5B23B4bC9c8C5416eE7Fe29eB5FDbE2f0E0b66
- BNB:
0x7fCaF6DFd3eeb8a268565C1A87EB4B77D1eaA660
- Scroll:
0xEF508D3B60BF7009E3a1a00EED30eb459E8Cf511
Example: Querying Metadata in JavaScript with ethers.js
Below is an example of how to query the metadata from the Anima Reputation SBT smart contract using ethers.js
, followed by fetching the metadata JSON via an HTTP call.
Step 1: Set Up ethers.js
First, install the required dependencies if you haven't done so:
npm install ethers
Step 2: Query the Smart Contract
Here's a JavaScript code example to retrieve the metadata URL and then fetch the metadata using fetch
:
const { ethers } = require('ethers');
const fetch = require('node-fetch');
// Set up the provider (e.g., Infura, Alchemy, or any other Ethereum RPC provider)
const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
// Smart contract ABI (only the relevant functions)
const contractABI = [
'function getMasterToken(address account) view returns (uint256)',
'function tokenURI(uint256 tokenId) view returns (string)'
];
// Replace with the contract address on the specific chain you're querying (e.g., Linea)
const contractAddress = '0x59C512b57900d6FFaf080674d99A194729401f6F';
// Initialize the contract
const contract = new ethers.Contract(contractAddress, contractABI, provider);
// User's wallet address
const userAddress = '0xYourUserAddress';
async function getMetadata() {
try {
// Step 1: Get the token ID for the user
const tokenId = await contract.getMasterToken(userAddress);
console.log(`Token ID: ${tokenId.toString()}`);
// Step 2: Get the metadata URL for the token ID
const metadataURL = await contract.tokenURI(tokenId);
console.log(`Metadata URL: ${metadataURL}`);
// Step 3: Fetch the metadata JSON from the URL
const response = await fetch(metadataURL);
const metadata = await response.json();
// Step 4: Log the fetched metadata
console.log('Fetched Metadata:', metadata);
} catch (error) {
console.error('Error fetching metadata:', error);
}
}
// Call the function to fetch metadata
getMetadata();
Explanation:
- Set up the provider: Connect to the Ethereum network using a provider like Infura or Alchemy.
- Smart contract interaction:
- Call
getMasterToken
to retrieve the token ID for a user's address. - Call
tokenURI
to get the metadata URL for that token ID.
- Fetch metadata: Once you have the metadata URL, use
fetch
to retrieve and parse the metadata JSON file. - Log the metadata: The fetched JSON data will include attributes like the reputation level and last verification timestamp.
How It Works: Step-by-Step
-
Retrieve the Token ID: Call the
getMasterToken
function with the user's address to retrieve the token ID associated with their account. -
Fetch Metadata: Once you have the token ID, use the
tokenURI
function to get the metadata URL, which contains all the relevant information about the user's reputation and verification status. -
Query Attributes: Parse the JSON metadata file returned from the URL to extract attributes such as the user's current reputation level and the timestamp of their last verification.
Summary:
This example demonstrates how to use ethers.js
to query the Anima SBT smart contract for metadata and then fetch the metadata using an HTTP request. You can easily customize the code to fit your use case, such as querying different blockchain networks and processing different metadata attributes.