Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ python3 pilot_vault_events.py
python3 daily_states_v2.py
python3 daily_points_v2.py
python3 aggregate_daily_points.py
python3 test/main_test.py
```

## find_deployment_blocks.py
Expand Down
8 changes: 6 additions & 2 deletions daily_points_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
POINTS_PER_PILOT_VAULT_TOKEN_FOR_NFT = (142 * 1000) // 100 # 1.42

lp_balances_snapshot = {}
lp_balances_snapshot_start_block = 0

type Points = int

Expand Down Expand Up @@ -110,7 +111,8 @@ def get_points(day_index) -> Dict[str, Points]:
events = block_number_to_events[block_number]
for event in events:
user_state = process_event_above_user_state(event, user_state)
points = give_points_for_user_state(user_state, points)
if block_number > lp_balances_snapshot_start_block:
points = give_points_for_user_state(user_state, points)

validate_end_state(day_index, user_state)
return points
Expand Down Expand Up @@ -139,5 +141,7 @@ def process_points():
)

if __name__ == "__main__":
lp_balances_snapshot = get_user_state("data/lp_balances_snapshot.json", "start_state")
lp_balances_snapshot_data_dir = "data/lp_balances_snapshot.json"
lp_balances_snapshot = get_user_state(lp_balances_snapshot_data_dir, "start_state")
lp_balances_snapshot_start_block = json.load(open(lp_balances_snapshot_data_dir, 'r'))["start_block"]
process_points()
36 changes: 13 additions & 23 deletions find_daily_blocks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/usr/bin/env python3
import json
import argparse
from datetime import datetime, timezone
from web3 import Web3
import os
from utils.get_rpc import get_rpc
from utils.aggregated_w3_request import w3_instances, make_aggregated_call


def get_min_deployment_block():
Expand All @@ -29,11 +27,11 @@ def get_min_deployment_block():
return min_block


def get_block(w3, num, cache):
def get_block(num, cache):
"""Fetch block with simple cache."""
if num in cache:
return cache[num]
blk = w3.eth.get_block(num)
blk = make_aggregated_call(w3_instances, lambda w3: w3.eth.get_block(num))
cache[num] = blk
return blk

Expand All @@ -43,7 +41,7 @@ def get_block_date(block):
return datetime.fromtimestamp(block["timestamp"], tz=timezone.utc).date()


def find_first_block_strictly_after_day(w3, start_block, latest_block, target_day):
def find_first_block_strictly_after_day(start_block, latest_block, target_day):
"""
Binary search for the smallest block number in [start_block, latest_block]
whose UTC date is strictly greater than target_day.
Expand All @@ -55,7 +53,7 @@ def find_first_block_strictly_after_day(w3, start_block, latest_block, target_da

while lo < hi:
mid = (lo + hi) // 2
blk = get_block(w3, mid, cache)
blk = get_block(mid, cache)
blk_day = get_block_date(blk)

if blk_day <= target_day:
Expand All @@ -70,33 +68,25 @@ def find_first_block_strictly_after_day(w3, start_block, latest_block, target_da
return None

# sanity check
blk = get_block(w3, lo, cache)
blk = get_block(lo, cache)
if get_block_date(blk) > target_day:
return lo
return None


def main():
w3 = Web3(Web3.HTTPProvider(get_rpc()))

is_connected = (
w3.is_connected() if hasattr(w3, "is_connected") else w3.isConnected()
)
if not is_connected:
raise RuntimeError("Cannot connect to RPC")

latest_block = w3.eth.block_number
latest_block = make_aggregated_call(w3_instances, lambda w3: w3.eth.block_number)
start_block = get_min_deployment_block()

if start_block > latest_block:
raise ValueError(f"start-block {start_block} is greater than latest block {latest_block}")

# Get starting block and its day
start_blk = w3.eth.get_block(start_block)
start_blk = make_aggregated_call(w3_instances, lambda w3: w3.eth.get_block(start_block))
start_day = get_block_date(start_blk)

# Get latest block and its day
latest_blk = w3.eth.get_block(latest_block)
latest_blk = make_aggregated_call(w3_instances, lambda w3: w3.eth.get_block(latest_block))
latest_day = get_block_date(latest_blk)

print(f"Starting from block {start_block}, day = {start_day}")
Expand All @@ -113,14 +103,14 @@ def main():

# Binary search for first block *after* this day
first_after = find_first_block_strictly_after_day(
w3, current_search_start, latest_block, current_day
current_search_start, latest_block, current_day
)

if first_after is None:
# No next day found yet (we're at the latest day)
# Use latest_block as the last block of current day
last_block_same_day = latest_block
last_blk = get_block(w3, last_block_same_day, cache)
last_blk = get_block(last_block_same_day, cache)

all_boundaries.append({
"day": str(current_day),
Expand All @@ -139,8 +129,8 @@ def main():
break

last_block_same_day = first_after - 1
last_blk = get_block(w3, last_block_same_day, cache)
first_next_blk = get_block(w3, first_after, cache)
last_blk = get_block(last_block_same_day, cache)
first_next_blk = get_block(first_after, cache)
next_day = get_block_date(first_next_blk)

all_boundaries.append({
Expand Down
40 changes: 15 additions & 25 deletions find_deployment_blocks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from web3 import Web3
import json
import sys
import os
from datetime import datetime, timezone
from utils.get_rpc import get_rpc
from utils.aggregated_w3_request import w3_instances, make_aggregated_call
from web3 import Web3

def load_contract_addresses():
"""Load contract addresses from config.json"""
Expand Down Expand Up @@ -35,22 +36,21 @@ def load_contract_addresses():
sys.exit(1)


def has_contract_code(w3, address, block_number):
def has_contract_code(address, block_number):
"""Check if contract has code at a specific block"""
try:
code = w3.eth.get_code(address, block_number)
code = make_aggregated_call(w3_instances, lambda w3: w3.eth.get_code(address, block_number))
return len(code) > 0
except Exception as e:
print(f"Warning: Error checking code at block {block_number}: {e}")
return False


def find_deployment_block(w3, address, start_block=0, end_block=None):
def find_deployment_block(address, start_block=0, end_block=None):
"""
Find the deployment block of a contract using binary search.

Args:
w3: Web3 instance
address: Contract address
start_block: Starting block for search (default: 0)
end_block: Ending block for search (default: latest block)
Expand All @@ -59,12 +59,12 @@ def find_deployment_block(w3, address, start_block=0, end_block=None):
Block number where contract was deployed, or None if not found
"""
if end_block is None:
end_block = w3.eth.block_number
end_block = make_aggregated_call(w3_instances, lambda w3: w3.eth.block_number)

print(f" Searching for deployment block between {start_block} and {end_block}...")

# First, check if contract exists at the end block
if not has_contract_code(w3, address, end_block):
if not has_contract_code(address, end_block):
print(f" Error: Contract has no code at block {end_block}. Contract may not be deployed yet.")
return None

Expand All @@ -76,7 +76,7 @@ def find_deployment_block(w3, address, start_block=0, end_block=None):
while left <= right:
mid = (left + right) // 2

if has_contract_code(w3, address, mid):
if has_contract_code(address, mid):
# Contract exists at this block, search earlier
result = mid
right = mid - 1
Expand All @@ -87,10 +87,10 @@ def find_deployment_block(w3, address, start_block=0, end_block=None):
return result


def get_block_info(w3, block_number):
def get_block_info(block_number):
"""Get block information including timestamp"""
try:
block = w3.eth.get_block(block_number)
block = make_aggregated_call(w3_instances, lambda w3: w3.eth.get_block(block_number))
return {
'block_number': block_number,
'timestamp': block.timestamp,
Expand Down Expand Up @@ -118,18 +118,8 @@ def main():

# Initialize Web3 connection
print("\n2. Connecting to blockchain...")
try:
w3 = Web3(Web3.HTTPProvider(rpc_url))
if not w3.is_connected():
print(f"Error: Could not connect to RPC provider at {rpc_url}")
sys.exit(1)
print(" Connected successfully")
except Exception as e:
print(f"Error connecting to RPC provider: {e}")
sys.exit(1)

# Get latest block
latest_block = w3.eth.block_number
latest_block = make_aggregated_call(w3_instances, lambda w3: w3.eth.block_number)
print(f" Latest block: {latest_block}")

# Find deployment blocks
Expand All @@ -139,9 +129,9 @@ def main():

# Find NFT contract deployment block
print(f"\n NFT Contract ({addresses['nft']}):")
nft_deployment_block = find_deployment_block(w3, addresses['nft'], end_block=latest_block)
nft_deployment_block = find_deployment_block(addresses['nft'], end_block=latest_block)
if nft_deployment_block:
nft_block_info = get_block_info(w3, nft_deployment_block)
nft_block_info = get_block_info(nft_deployment_block)
results['nft'] = {
'address': addresses['nft'],
'deployment_block': nft_deployment_block,
Expand All @@ -159,9 +149,9 @@ def main():

# Find Pilot Vault contract deployment block
print(f"\n Pilot Vault Contract ({addresses['pilot_vault']}):")
vault_deployment_block = find_deployment_block(w3, addresses['pilot_vault'], end_block=latest_block)
vault_deployment_block = find_deployment_block(addresses['pilot_vault'], end_block=latest_block)
if vault_deployment_block:
vault_block_info = get_block_info(w3, vault_deployment_block)
vault_block_info = get_block_info(vault_deployment_block)
results['pilot_vault'] = {
'address': addresses['pilot_vault'],
'deployment_block': vault_deployment_block,
Expand Down
Loading