Demo: ERC1155 holdings

This script first fetches the backing data from the Emblem Vault, gets the live addresses and the target asset name. It then fetches the balances for each live address and checks if the balance contains the expected asset.

const request = require('request');

const tokenId = '14348293833920273912990111611620811208434744638373632813897914200493984998646';
const baseUrl = 'https://v2.emblemvault.io';
const apiKey = 'your_api_key_here'; // Replace with your actual API key

const options = {
    url: `${baseUrl}/allBackingIdsForTokenId/${tokenId}`,
    headers: {
        'x-api-key': apiKey,
    },
};

request(options, (error, response, body) => {
    if (!error && response.statusCode === 200) {
        const parsedBody = JSON.parse(body);
        const liveAddresses = parsedBody.liveAddresses;
        const liveIds = parsedBody.liveIds;
        const targetAssetName = parsedBody.live[0].targetAsset.name;
        console.log('\nAsset Name:', targetAssetName);
        liveAddresses.forEach((address, index) => {
            getTokenBalances(address, balance => {
                console.log('\nVault Token ID:', liveIds[index]);
                console.log('Address:', address);
                console.log('Balance contains the target asset:', balanceContainsExpectedAsset(balance, targetAssetName));
            });
        });
    } else {
        console.log('Error:', error);
        console.log('Status Code:', response.statusCode);
        console.log('Body:', body);
    }
});

function getTokenBalances(address, cb) {
    const options = {
        method: 'GET',
        url: 'https://xchain.io/api/balances/' + address,
        headers: {
            'cache-control': 'no-cache',
            'accept': 'application/json, text/javascript, */*; q=0.01',
        },
    };

    request(options, (error, response) => {
        if (error) throw new Error(error);
        if (response.body && !response.body.includes('<!DOCTYPE html>')) {
            return cb(JSON.parse(response.body));
        } else {
            return cb({});
        }
    });
}

function balanceContainsExpectedAsset(balance, expectedAssetName) {
    const assets = balance.data;
    return assets.some(asset => asset.asset === expectedAssetName);
}

Results:

Asset Name: ZOMBIEPEPES

Vault Token ID: 78617344381428201
Address: 1Gwr7dmaVTJbV2j5zc7UnKQeTwdjzUt95k
Balance contains the target asset: true

Vault Token ID: 29243500604579471
Address: 1BhkPipt2EHczAPQVqKXxJ7J97NDUTbUAJ
Balance contains the target asset: true

Last updated