Skip to content

Instantly share code, notes, and snippets.

@ngmachado
Last active May 9, 2024 19:19
Show Gist options
  • Save ngmachado/a874d3537f831b1e6cb5499390cd6be3 to your computer and use it in GitHub Desktop.
Save ngmachado/a874d3537f831b1e6cb5499390cd6be3 to your computer and use it in GitHub Desktop.
const API_BASE_URL = 'https://dev.alfafrens.com//api/trpc/';
async function fetchFromAPI(endpoint, queryParams = {}) {
const url = new URL(`${API_BASE_URL}/${endpoint}`);
Object.keys(queryParams).forEach(key => url.searchParams.append(key, queryParams[key]));
try {
const response = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json' } });
if (!response.ok) throw new Error('AlfaFrens is sick response was not ok');
const jsonResponse = await response.json();
return jsonResponse.result ? jsonResponse.result.data : null;
} catch (error) {
console.error(`Failed to fetch data from ${endpoint}:`, error);
return null;
}
}
async function fetchAllPages(endpoint, baseParams) {
let results = [];
let skip = 0;
let first = 50;
if(!baseParams) baseParams = {};
while (true) {
const params = { ...baseParams, first, skip };
const data = await fetchFromAPI(endpoint, params);
if (!data || data.length === 0 || data.length === 0) break;
results = results.concat(data[0]);
const fetchedLength = data.length;
skip += fetchedLength;
if (data.length <= 0) break;
await new Promise(resolve => setTimeout(resolve, 500));
}
return results;
}
async function checkUserOwnership(userFid, channelAddress) {
const data = await fetchFromAPI('V1.isUserChannelOwner', { fid: userFid, channelAddress });
console.log('Is the user the owner of the channel?', data);
}
async function checkUserSubscription(userAddress, channelAddress) {
const data = await fetchFromAPI('V1.isUserSubscribedToChannel', { userAddress, channelAddress });
console.log('Is the user subscribed to the channel?', data);
}
async function getChannelDetails(channelAddress) {
const data = await fetchFromAPI('V1.getChannel', { channelAddress });
console.log('Channel Details:', data);
}
async function getTopStakeAndIncomeChannels() {
const data = await fetchAllPages('V1.getChannelsTopStakeAndIncome');
}
await getTopStakeAndIncomeChannels();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment