Game Asset Tracker: Monitoring Game Assets
1.1 Game Asset Tracker Overview
1.2 Code for Asset Tracking
1.2.1 Basic Setup for Game Asset Tracker
const Web3 = require('web3');
const web3 = new Web3('wss://mainnet.infura.io/ws/v3/YOUR_INFURA_PROJECT_ID');
// User's wallet address to track assets
const walletAddress = '0xUserWalletAddressHere';
1.2.2 Tracking NFT (ERC-721) Assets
const assetContractAddress = '0xAssetContractAddressHere';
const assetABI = [
{
"constant": true,
"inputs": [{"name": "_owner", "type": "address"}],
"name": "balanceOf",
"outputs": [{"name": "balance", "type": "uint256"}],
"type": "function"
},
{
"constant": true,
"inputs": [{"name": "_tokenId", "type": "uint256"}],
"name": "ownerOf",
"outputs": [{"name": "owner", "type": "address"}],
"type": "function"
}
];
const assetContract = new web3.eth.Contract(assetABI, assetContractAddress);
// Function to retrieve NFT ownership status
async function checkNFTOwnership(tokenId) {
try {
const owner = await assetContract.methods.ownerOf(tokenId).call();
console.log(`NFT Token ID: ${tokenId} is owned by ${owner}`);
} catch (error) {
console.error("Error retrieving NFT ownership:", error);
}
}
1.2.3 Tracking Fungible Tokens (ERC-20) for Game Currencies
const tokenContractAddress = '0xTokenContractAddressHere';
const tokenABI = [
{
"constant": true,
"inputs": [{"name": "_owner", "type": "address"}],
"name": "balanceOf",
"outputs": [{"name": "balance", "type": "uint256"}],
"type": "function"
}
];
const tokenContract = new web3.eth.Contract(tokenABI, tokenContractAddress);
// Function to check the balance of a fungible token
async function checkTokenBalance() {
try {
const balance = await tokenContract.methods.balanceOf(walletAddress).call();
console.log(`Token Balance: ${web3.utils.fromWei(balance, 'ether')} Tokens`);
} catch (error) {
console.error("Error fetching token balance:", error);
}
}
1.2.4 Real-Time Asset Update with WebSocket
web3.eth.subscribe('newBlockHeaders', (error, block) => {
if (error) console.error("Error on subscription:", error);
else {
console.log("New block received. Checking asset status...");
checkNFTOwnership(1234); // Check NFT asset
checkTokenBalance(); // Check ERC-20 token balance
}
});
1.3 API Integration
1.3.1 GET /assets/{asset_id}
curl -X GET "https://api.capsurelabs.com/assets/1234" \
-H "Authorization: Bearer <token>"
1.3.2 GET /wallets/{wallet_id}/assets
curl -X GET "https://api.capsurelabs.com/wallets/0xYourWalletAddress/assets" \
-H "Authorization: Bearer <token>"
1.3.3 WebSocket /assets/subscribe
{
"type": "subscribe",
"walletId": "0xYourWalletAddress",
"assetId": "1234"
}
Last updated