Skip to content
Open
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
18 changes: 18 additions & 0 deletions constants/midas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from web3 import Web3

from utils.web3_utils import w3
import json

PAGINATION_SIZE = 2000

MIDAS_MWILDUSD_ADDRESS = Web3.to_checksum_address("0x605A84861EE603e385b01B9048BEa6A86118DB0a")
MIDAS_MWILDUSD_START_BLOCK = 23441782
ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"

with open("abi/ERC20_abi.json") as f:
ERC20_ABI = json.load(f)

MIDAS_MWILDUSD_CONTRACT = w3.eth.contract(
address=MIDAS_MWILDUSD_ADDRESS,
abi=ERC20_ABI,
)
2 changes: 2 additions & 0 deletions constants/summary_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class SummaryColumn(Enum):
SummaryColumnType.ETHENA_PTS,
)

MIDAS_MWILDUSD_PTS = ("midas_mwildusd_pts", SummaryColumnType.ETHENA_PTS)

THRUSTER_POOL_PTS = ("thruster_pool_pts", SummaryColumnType.ETHENA_PTS)

def __init__(self, column_name: str, col_type: SummaryColumnType):
Expand Down
3 changes: 3 additions & 0 deletions integrations/integration_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,9 @@ class IntegrationID(Enum):
Token.USDE,
)

# Midas
MIDAS_MWILDUSD = ("midas_mwildusd", "Midas mWildUSD", Token.USDE)

def __init__(self, column_name: str, description: str, token: Token = Token.USDE):
self.column_name = column_name
self.description = description
Expand Down
110 changes: 110 additions & 0 deletions integrations/midas_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from copy import deepcopy
import logging

from typing import Dict, List, Optional

from constants.midas import MIDAS_MWILDUSD_CONTRACT, MIDAS_MWILDUSD_START_BLOCK, PAGINATION_SIZE, ZERO_ADDRESS
from constants.summary_columns import SummaryColumn
from eth_typing import ChecksumAddress

from constants.chains import Chain
from integrations.integration_ids import IntegrationID
from integrations.cached_balances_integration import CachedBalancesIntegration
from utils.web3_utils import fetch_events_logs_with_retry


class MidasIntegration(CachedBalancesIntegration):
def __init__(
self,
integration_id: IntegrationID,
start_block: int,
chain: Chain = Chain.ETHEREUM,
summary_cols: Optional[List[SummaryColumn]] = None,
reward_multiplier: int = 1,
):
super().__init__(
integration_id,
start_block,
chain,
summary_cols,
reward_multiplier,
)

def get_block_balances(
self, cached_data: Dict[int, Dict[ChecksumAddress, float]], blocks: List[int]
) -> Dict[int, Dict[ChecksumAddress, float]]:
logging.info("Getting block data for Midas")
new_block_data: Dict[int, Dict[ChecksumAddress, float]] = {}
if not blocks:
logging.error("No blocks provided to Midas get_block_balances")
return new_block_data
sorted_blocks = sorted(blocks)
cache_copy: Dict[int, Dict[ChecksumAddress, float]] = deepcopy(cached_data)
for block in sorted_blocks:
# find the closest prev block in the data
# list keys parsed as ints and in descending order
sorted_existing_blocks = sorted(
cache_copy,
reverse=True,
)
# loop through the sorted blocks and find the closest previous block
prev_block = self.start_block
start = prev_block
bals = {}
for existing_block in sorted_existing_blocks:
if existing_block < block:
prev_block = existing_block
start = existing_block + 1
bals = deepcopy(cache_copy[prev_block])
break
# parse transfer events since and update bals
while start <= block:
to_block = min(start + PAGINATION_SIZE, block)
transfers = fetch_events_logs_with_retry(
"Token transfers Midas",
MIDAS_MWILDUSD_CONTRACT.events.Transfer(),
start,
to_block,
)
for transfer in transfers:
userTo = transfer["args"]["to"]
if userTo not in bals:
bals[userTo] = 0
bals[userTo] += round(transfer["args"]["value"] / 10**18, 4)

userFrom = transfer["args"]["from"]
if userFrom == ZERO_ADDRESS:
continue
if userFrom not in bals:
bals[userFrom] = 0
bals[userFrom] -= round(transfer["args"]["value"] / 10**18, 4)
start = to_block + 1
new_block_data[block] = bals
cache_copy[block] = bals
return new_block_data


if __name__ == "__main__":
midas_integration = MidasIntegration(
integration_id=IntegrationID.MIDAS_MWILDUSD,
start_block=MIDAS_MWILDUSD_START_BLOCK,
summary_cols=[SummaryColumn.MIDAS_MWILDUSD_PTS],
reward_multiplier=20,
)

# Without cached data
without_cached_data_output = midas_integration.get_block_balances(
cached_data={}, blocks=[23441782,23442447]
)

print("=" * 120)
print("Run without cached data", without_cached_data_output)
print("=" * 120, "\n" * 5)

# With cached data, using the previous output so there is no need
# to fetch the previous blocks again
with_cached_data_output = midas_integration.get_block_balances(
cached_data=without_cached_data_output, blocks=[23441782,23442447]
)
print("Run with cached data", with_cached_data_output)
print("=" * 120)