Skip to content

Commit

Permalink
add caching and start_block
Browse files Browse the repository at this point in the history
  • Loading branch information
djeck1432 committed Sep 12, 2024
1 parent 470bf79 commit c8acc16
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions backend/src/starknet.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { StarknetIdNavigator } from "starknetid.js";
import {RpcProvider, constants, starknetId} from "starknet";
import {RpcProvider, constants, hash} from "starknet";
import * as dotenv from "dotenv";

dotenv.config();

const NODE_URL: string = process.env.NODE_URL || "";
const CHAIN_ID: string = process.env.NETWORK === 'mainnet'
const START_BLOCK: number = process.env.NETWORK === 'mainnet' ? 720000 : 177000; // Started block number before vesting added
const CHAIN_ID: constants.StarknetChainId = process.env.NETWORK === 'mainnet'
? constants.StarknetChainId.SN_MAIN
: constants.StarknetChainId.SN_SEPOLIA;

Expand All @@ -19,18 +20,25 @@ const starknetIdNavigator = new StarknetIdNavigator(
CHAIN_ID,
);

const cache = new Map<string, any>();

export const getVestingEvents = async (address: string) => {

// Check if the result is already cached
if (cache.has(address)) {
return cache.get(address);
}

try {
const eventFilter = {
from_block: { block_number: 0 },
chunk_size: 1000,
from_block: { block_number: START_BLOCK },
chunk_size: 100,
address: address,
key: ['Vesting'],
keys: [[hash.getSelectorFromName('Vesting')]],
};

const events = await provider.getEvents(eventFilter);

// Check if events are empty and return an empty array
if (!events.events || events.events.length === 0) {
return [];
}
Expand All @@ -39,32 +47,31 @@ export const getVestingEvents = async (address: string) => {

const results = events.events.reduce((acc: any[], event: any) => {
try {
// VEST: {grantee, timestamp, amount}
const grantee = event.data[0]; // Grantee (index 0)
const timestamp = parseInt(event.data[1]); // Timestamp (index 1)
const amount = parseInt(event.data[2]); // Amount (index 2)

// Processing based on the timestamp
if (timestamp < now && amount) {
acc.push({
amount: amount,
timestamp: timestamp,
is_claimable: true
is_claimable: true,
});
} else {
acc.push({
timestamp: timestamp,
amount: amount,
is_claimable: false
is_claimable: false,
});
}
} catch (error) {
// Log the error and continue with the next event
console.error('Error processing event, skipping this event:', error);
}
return acc;
}, []);

// Store the result in the cache
cache.set(address, results);

return results;
} catch (error) {
console.error('Error in getVestingEvents:', error);
Expand Down

0 comments on commit c8acc16

Please sign in to comment.