UNPKG

next-tribune-blog

Version:

Automatic blog generator for Next.js from Tribune.sh blockchain articles

131 lines 4.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchArticlesByOwner = fetchArticlesByOwner; exports.fetchAllArticles = fetchAllArticles; const viem_1 = require("viem"); const contract_abi_1 = require("./contract-abi"); // Define Abstract mainnet const abstractMainnet = { id: 2741, name: 'Abstract', network: 'abstract', nativeCurrency: { decimals: 18, name: 'Ethereum', symbol: 'ETH', }, rpcUrls: { default: { http: ['https://abstract-mainnet.g.alchemy.com/v2/HNy2ntUiTXz3ZQpBJq1VKXbVlG11VKTN'] }, public: { http: ['https://abstract-mainnet.g.alchemy.com/v2/HNy2ntUiTXz3ZQpBJq1VKXbVlG11VKTN'] }, }, blockExplorers: { default: { name: 'Abstract Scan', url: 'https://abstractscan.com' }, }, }; // Fixed Tribune contract address const DEFAULT_CONTRACT_ADDRESS = '0xA951D889Dc4c157bad4Dca26BF5b86720D5b33FA'; const DEFAULT_RPC_URL = 'https://abstract-mainnet.g.alchemy.com/v2/HNy2ntUiTXz3ZQpBJq1VKXbVlG11VKTN'; async function fetchArticlesByOwner(ownerAddress, contractAddress = DEFAULT_CONTRACT_ADDRESS, rpcUrl = DEFAULT_RPC_URL) { try { console.log(`🔍 Fetching articles for owner: ${ownerAddress}`); const client = (0, viem_1.createPublicClient)({ chain: abstractMainnet, transport: (0, viem_1.http)(rpcUrl), }); // Get article IDs for the owner const articleIds = await client.readContract({ address: contractAddress, abi: contract_abi_1.contractABI, functionName: 'getArticlesByOwner', args: [ownerAddress], }); console.log(`📄 Found ${articleIds.length} articles`); if (articleIds.length === 0) { return []; } // Fetch each article's details const articles = []; for (const articleId of articleIds) { try { const [header, title, description, body1, body2, body3, body4, body5, owner, timestamp, tokenAddress] = await client.readContract({ address: contractAddress, abi: contract_abi_1.contractABI, functionName: 'getArticle', args: [articleId], }); articles.push({ id: articleId.toString(), header, title, description, body1, body2, body3, body4, body5, owner, timestamp, tokenAddress, }); } catch (error) { console.error(`Error fetching article ${articleId}:`, error); } } return articles; } catch (error) { console.error('Error fetching articles from blockchain:', error); throw error; } } async function fetchAllArticles(contractAddress = DEFAULT_CONTRACT_ADDRESS, rpcUrl = DEFAULT_RPC_URL, limit = 100) { try { console.log('🔍 Fetching all articles from contract'); const client = (0, viem_1.createPublicClient)({ chain: abstractMainnet, transport: (0, viem_1.http)(rpcUrl), }); const [headers, titles, descriptions, owners, timestamps, ids] = await client.readContract({ address: contractAddress, abi: contract_abi_1.contractABI, functionName: 'getArticles', args: [BigInt(0), BigInt(limit)], }); const articles = []; for (let i = 0; i < titles.length; i++) { // Fetch full article content try { const [header, title, description, body1, body2, body3, body4, body5, owner, timestamp, tokenAddress] = await client.readContract({ address: contractAddress, abi: contract_abi_1.contractABI, functionName: 'getArticle', args: [ids[i]], }); articles.push({ id: ids[i].toString(), header, title, description, body1, body2, body3, body4, body5, owner, timestamp, tokenAddress, }); } catch (error) { console.error(`Error fetching article ${ids[i]}:`, error); } } return articles; } catch (error) { console.error('Error fetching articles:', error); throw error; } } //# sourceMappingURL=blockchain.js.map