Skip to content

Commit

Permalink
20 pages of txs
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotBraem committed Nov 18, 2024
1 parent f7594ef commit f2fbbe7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/app/api/[[...slugs]]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
const app = new Elysia({ prefix: "/api", aot: false })
.use(swagger())
.get("/roast/:accountId", async ({ params: { accountId }, set }) => {
// do an accountId check
try {
const cacheKey = accountId;
const now = Date.now();
Expand All @@ -27,7 +28,7 @@ const app = new Elysia({ prefix: "/api", aot: false })
// Analyze account and get summary
console.log("getting account summary...");
const summary = await getAccountSummary(accountId);
roast = summary;
roast = summary; // we could do some roasting here, but just returning directly and gonna tweek the instructions instead
// Store in memory cache
cache.set(cacheKey, {
roast,
Expand Down
24 changes: 17 additions & 7 deletions src/app/lib/fastnear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ export interface Transaction {

export interface AccountActivityResponse {
account_txs: Transaction[];
transactions: any[];
total_txs?: number; // Only present when max_block_height is not provided
txs_count?: number; // Only present when max_block_height is not provided
}

const FASTNEAR_API_SERVER_RS = "https://api.fastnear.com";
Expand Down Expand Up @@ -96,23 +95,31 @@ export async function getAccountActivity(
export async function getAllAccountActivity(
accountId: string,
pages: number = 3,
): Promise<Transaction[]> {
): Promise<AccountActivityResponse> {
try {
// First call without maxBlockHeight to get total transactions
const firstPage = await getAccountActivity(accountId);
let allTransactions = [...firstPage.account_txs];
const totalTransactions = firstPage.txs_count;

if (!firstPage.total_txs || pages <= 1) {
return allTransactions;
if (!totalTransactions || pages <= 1) {
return {
account_txs: allTransactions,
txs_count: totalTransactions
}
}

const transactionsPerPage = 200;
const totalPages = Math.ceil(totalTransactions / transactionsPerPage);
const maxPages = Math.min(pages, totalPages);

// Use the last transaction's block height from each page to fetch the next page
let lastBlockHeight =
allTransactions[allTransactions.length - 1].tx_block_height;

for (
let i = 1;
i < pages && allTransactions.length < firstPage.total_txs;
i < maxPages;
i++
) {
const nextPage = await getAccountActivity(accountId, lastBlockHeight);
Expand All @@ -123,7 +130,10 @@ export async function getAllAccountActivity(
nextPage.account_txs[nextPage.account_txs.length - 1].tx_block_height;
}

return allTransactions;
return {
account_txs: allTransactions,
txs_count: totalTransactions
};
} catch (error) {
console.error("Error fetching all account activity:", error);
throw error;
Expand Down
18 changes: 9 additions & 9 deletions src/app/utils/account-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ function getUniqueItems(items: string[]): string[] {

function processAccountData(
details: FullAccountDetails,
activity: AccountActivityResponse,
allActivity: Transaction[],
allActivity: AccountActivityResponse,
): AccountSummaryData {
// Process balance
const nearBalance = formatNearAmount(details.state.balance);
Expand All @@ -66,12 +65,14 @@ function processAccountData(
balance: token.balance,
}));

const allTransactions = allActivity.account_txs;

// Process unique interactions from transactions
const uniqueInteractions = getUniqueItems(
allActivity.map((tx) => tx.signer_id),
allTransactions.map((tx) => tx.signer_id),
);
// Format recent transactions
const recentTransactions = allActivity.slice(0, 10).map((tx) => ({
const recentTransactions = allTransactions.slice(0, 10).map((tx) => ({
type: "transaction",
timestamp: tx.tx_block_timestamp,
details: `Interaction with ${tx.signer_id}`,
Expand All @@ -90,7 +91,7 @@ function processAccountData(
nftCollections: details.nfts.map((nft) => nft.contract_id),
},
activity: {
totalTransactions: activity.total_txs || 0,
totalTransactions: allActivity.txs_count || 0,
recentTransactions,
uniqueInteractions,
},
Expand Down Expand Up @@ -132,17 +133,16 @@ Please analyze this data and provide:
}

export async function getAccountSummary(accountId: string): Promise<string> {
const MAX_NUM_PAGES = 5; // maximum pages of transaction data to fetch
const MAX_NUM_PAGES = 20; // maximum pages of transaction data to fetch (200/page = 4000 txs)
try {
// Fetch all required data
const [details, activity, allActivity] = await Promise.all([
const [details, allActivity] = await Promise.all([
getFullAccountDetails(accountId),
getAccountActivity(accountId),
getAllAccountActivity(accountId, MAX_NUM_PAGES),
]);

// Process the data into a structured format
const processedData = processAccountData(details, activity, allActivity);
const processedData = processAccountData(details, allActivity);

// Create the prompt for LLM
const prompt = createSummaryPrompt(processedData);
Expand Down

0 comments on commit f2fbbe7

Please sign in to comment.