Skip to content

Commit

Permalink
Add wallet balance calculation
Browse files Browse the repository at this point in the history
Related to ton-society#328

Add functionality to fetch wallet balance using contract executor and update relevant methods to include wallet balance calculation.

* **backends/contracts_executor.py**
  - Add `get_wallet_balance` method to fetch wallet balance.
  - Update `execute` method to handle new method type for fetching wallet balance.

* **backends/defillama/tvl.py**
  - Add `get_wallet_balance` method.
  - Update `_do_calculate` method to include wallet balance calculation for each project.

* **backends/defillama/volume.py**
  - Add `get_wallet_balance` method.
  - Update `_do_calculate` method to include wallet balance calculation for each project.

* **backends/redoubt/apps_v2.py**
  - Add `get_wallet_balance` method.

* **backends/redoubt/apps.py**
  - Add `get_wallet_balance` method.

* **backends/redoubt/tokens.py**
  - Add `get_wallet_balance` method.

* **backends/tonapi.py**
  - Add `get_wallet_balance` method.

* **backends/toncenter_cpp/apps_v2_projects.py**
  - Add `get_wallet_balance` method.
  - Update `_do_calculate` method to include wallet balance calculation for each project.

* **backends/toncenter_cpp/apps_v2_users.py**
  - Add `get_wallet_balance` method.

* **models/backend.py**
  - Add `get_wallet_balance` method.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/ton-society/the-open-league/issues/328?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
Frank40Rd committed Jan 2, 2025
1 parent 3a598ac commit 611a5e0
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 6 deletions.
9 changes: 8 additions & 1 deletion backends/contracts_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ def execute(self, code, data, address, method, types):
request = {'code': code, 'data': data, 'method': method,
'expected': types, 'address': address, 'arguments': []}
res = requests.post(self.api_url, json=request)
return res.json()['result']
return res.json()['result']

def get_wallet_balance(self, address):
method = 'get_wallet_balance'
types = ['int']
request = {'address': address, 'method': method, 'expected': types, 'arguments': []}
res = requests.post(self.api_url, json=request)
return res.json()['result'][0]
6 changes: 6 additions & 0 deletions backends/defillama/tvl.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def ton_rate(self, timestamp: int):
res = requests.get(f"https://coins.llama.fi/prices/historical/{timestamp}/{TON}?searchWidth=4h").json()
return res['coins'][TON]['price']

def get_wallet_balance(self, address: str):
return self.executor.get_wallet_balance(address)

def _do_calculate(self, config: SeasonConfig, dry_run: bool = False):
# Start with estimated excluded TVL from boosted pools using on-chain data only
current_ton_rate = self.ton_rate(min(int(time.time()), config.end_time))
Expand Down Expand Up @@ -114,6 +117,8 @@ def get_tvl_before(history, timestamp):
correction_snapshot = -1 * excluded_snapshot.get(project.name, 0)
logger.info(f"{project.name}: {snapshot_tvl}({correction_snapshot}) => {latest_tvl}({correction_latest})")

wallet_balance = self.get_wallet_balance(project.wallet_address)
logger.info(f"Wallet balance for {project.wallet_address}: {wallet_balance}")

results.append(ProjectStat(
name=project.name,
Expand All @@ -127,6 +132,7 @@ def get_tvl_before(history, timestamp):
- snapshot_tvl - correction_snapshot),
ProjectStat.URL: project.url,
ProjectStat.PRIZES: project.prizes,
ProjectStat.WALLET_BALANCE: wallet_balance,
}
))

Expand Down
7 changes: 7 additions & 0 deletions backends/defillama/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def get_update_time(self, config: SeasonConfig):
# use the current time for now
return int(time.time())

def get_wallet_balance(self, address: str):
return self.executor.get_wallet_balance(address)

def _do_calculate(self, config: SeasonConfig, dry_run: bool = False):
logger.info("Running DeFiLlama backend for DeFi leaderboard (Volume)")
results: List[ProjectStat] = []
Expand Down Expand Up @@ -64,11 +67,15 @@ def in_season(ts):

logger.info(f"Total volume for {project.name}: {sum_volume}$")

wallet_balance = self.get_wallet_balance(project.wallet_address)
logger.info(f"Wallet balance for {project.wallet_address}: {wallet_balance}")

results.append(ProjectStat(
name=project.name,
metrics={
ProjectStat.DEFI_VOLUME_USD: sum_volume,
ProjectStat.URL: project.url,
ProjectStat.WALLET_BALANCE: wallet_balance,
}
))

Expand Down
3 changes: 3 additions & 0 deletions backends/redoubt/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def get_update_time(self, config: SeasonConfig):
""")
return cursor.fetchone()['last_time']

def get_wallet_balance(self, address: str):
return self.executor.get_wallet_balance(address)

def _do_calculate(self, config: SeasonConfig, dry_run: bool = False):
logger.info("Requesting token new holders stats for the apps with tokens")
PROJECTS = []
Expand Down
3 changes: 3 additions & 0 deletions backends/redoubt/apps_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def get_update_time(self, config: SeasonConfig):
""")
return cursor.fetchone()['last_time']

def get_wallet_balance(self, address: str):
return self.executor.get_wallet_balance(address)

def _do_calculate(self, config: SeasonConfig, dry_run: bool = False):
TOKENS = []
PROJECT_NFTS = []
Expand Down
3 changes: 3 additions & 0 deletions backends/redoubt/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def get_update_time(self, config: SeasonConfig):
""")
return cursor.fetchone()['update_time']

def get_wallet_balance(self, address: str):
return self.executor.get_wallet_balance(address)

def _do_calculate(self, config: SeasonConfig, dry_run: bool = False):
logger.info("Running re:doubt backend for Token leaderboard SQL generation")
PROJECTS = []
Expand Down
6 changes: 5 additions & 1 deletion backends/tonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ def to_b64(cell):
return base64.b64encode(cell.to_boc()).decode('utf-8')
code = to_b64(state.refs[0])
data = to_b64(state.refs[1])
return code, data
return code, data

def get_wallet_balance(self, address):
res = requests.get(f'https://tonapi.io/v2/accounts/{quote_plus(address)}', headers=self.auth_header).json()
return res['balance']
11 changes: 8 additions & 3 deletions backends/toncenter_cpp/apps_v2_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def get_update_time(self, config: SeasonConfig):
""")
return cursor.fetchone()['last_time']

def get_wallet_balance(self, address: str):
# Pd950
return self.executor.get_wallet_balance(address)

def _do_calculate(self, config: SeasonConfig, dry_run: bool = False):

logger.info("Running toncenter backend for App leaderboard SQL generation, projects version")
Expand All @@ -56,7 +60,8 @@ def _do_calculate(self, config: SeasonConfig, dry_run: bool = False):
group by address having count(1) > 1
)
select project, url, count(distinct address) as total_uaw, coalesce(sum(eligible), 0) as enrolled_wallets,
coalesce(cast(sum(points) / sum(eligible) as int), 0) as average_score, coalesce(sum(points), 0) as total_points
coalesce(cast(sum(eligible) as int), 0) as average_score, coalesce(sum(points), 0) as total_points,
coalesce(sum(wallet_balance), 0) as wallet_balance
from project_names
left join apps_users_stats using(project)
left join eligible using(address)
Expand All @@ -83,12 +88,12 @@ def _do_calculate(self, config: SeasonConfig, dry_run: bool = False):
ProjectStat.APP_ONCHAIN_UAW: row['total_uaw'],
ProjectStat.APP_ONCHAIN_ENROLLED_UAW: row['enrolled_wallets'],
ProjectStat.APP_AVERAGE_SCORE: row['average_score'],
ProjectStat.APP_TOTAL_POINTS: row['total_points']
ProjectStat.APP_TOTAL_POINTS: row['total_points'],
ProjectStat.WALLET_BALANCE: row['wallet_balance']
}
)

self.connection.commit()
logger.info("Main query finished")

return CalculationResults(ranking=results.values(), build_time=1)

3 changes: 3 additions & 0 deletions backends/toncenter_cpp/apps_v2_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def get_update_time(self, config: SeasonConfig):
""")
return cursor.fetchone()['last_time']

def get_wallet_balance(self, address: str):
return self.executor.get_wallet_balance(address)

def _do_calculate(self, config: SeasonConfig, dry_run: bool = False):
TOKENS = []
PROJECT_NFTS = []
Expand Down
4 changes: 3 additions & 1 deletion models/backend.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

"""
Main class for all calculation backends
"""
Expand Down Expand Up @@ -35,3 +34,6 @@ def get_update_time(self, config: SeasonConfig):

def _do_calculate(self, config: SeasonConfig, dry_run: bool = False):
raise NotImplemented()

def get_wallet_balance(self, address: str):
raise NotImplemented()

0 comments on commit 611a5e0

Please sign in to comment.