Skip to content

Commit

Permalink
finish Bridge endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Landon Gingerich authored and Landon Gingerich committed Sep 12, 2023
1 parent 6e90367 commit fae68f3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 43 deletions.
127 changes: 87 additions & 40 deletions src/defillama_py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ def get_pools(self):
return results

# --- TVL --- #
# all tvl endpoints manually checked. seem good, need to write tests still

def get_all_protocols_current_tvl(
self, raw: bool = True
Expand Down Expand Up @@ -408,6 +407,7 @@ def get_all_chains_current_tvl(

# /fetch/signature
# /fetch/contract/{chain}/{address}


# --- Bridges --- #

Expand Down Expand Up @@ -436,6 +436,7 @@ def get_all_bridge_volume(
else:
return pd.DataFrame(response["bridges"])


def get_bridge_volume(
self, ids: List[str], raw: bool = True
) -> Union[Dict, pd.DataFrame]:
Expand Down Expand Up @@ -519,6 +520,7 @@ def get_bridge_volume(
else:
return pd.concat(dfs, ignore_index=True)


def get_chain_bridge_volume(
self, chains: List[str], params: Dict = None, raw: bool = True
) -> Union[Dict, pd.DataFrame]:
Expand Down Expand Up @@ -578,45 +580,6 @@ def get_chain_bridge_volume(
]
return self._clean_chain_name(df)

# still need to handle raw=False
def get_chain_bridge_day_volume(
self, timestamp: int, chains: List[str], params: Dict = None, raw: bool = True
) -> Union[Dict, pd.DataFrame]:
"""Get a 24hr token and address volume breakdown for a bridge.
Endpoint: /bridgedaystats/{timestamp}/{chain}
Parameters:
- timestamp (int, required): Unix timestamp. Data returned will be for the 24hr
period starting at 00:00 UTC that the timestamp lands in.
- chains (str or List[str], required): chain slug(s) — you can get these from
get_chains().
- params (Dict, optional): Dictionary containing optional API parameters.
- id (int): Id's of the desired bridge(s).
- raw (bool, optional): If True, returns raw data. If False, returns a
transformed DataFrame.
Defaults to True.
Returns:
- Dict or DataFrame: Raw data from the API or a transformed DataFrame.
"""

if isinstance(chains, str):
chains = [chains]

if raw:
if len(chains) == 1:
return self._get(
"BRIDGES", endpoint=f"/bridgedaystats/{timestamp}/{chains[0]}"
)

results = {}
for chain in chains:
results[chain] = self._get(
"BRIDGES", endpoint=f"/bridgedaystats/{timestamp}/{chains}"
)
return results
# doesn't work for multiple chains

def get_bridge_day_stats(
self,
Expand All @@ -641,6 +604,52 @@ def get_bridge_day_stats(
Returns:
- Dict or DataFrame: Raw data from the API or a transformed DataFrame.
"""
if isinstance(chains, str):
chains = [chains]

if raw:
results = {}
for chain in chains:
endpoint = f"/bridgedaystats/{timestamp}/{chain}"
results[chain] = self._get("BRIDGES", endpoint=endpoint, params=params)
return results

else:
results = []
for chain in chains:
data = self._get("BRIDGES", endpoint=f"/bridgedaystats/{timestamp}/{chain}", params=params)

for token, details in data.get("totalTokensDeposited", {}).items():
details["date"] = data["date"]
details["chain"] = chain
details["token"] = token
details["type"] = "totalTokensDeposited"
results.append(details)

for token, details in data.get("totalTokensWithdrawn", {}).items():
details["date"] = data["date"]
details["chain"] = chain
details["token"] = token
details["type"] = "totalTokensWithdrawn"
results.append(details)

for token, details in data.get("totalAddressDeposited", {}).items():
details["date"] = data["date"]
details["chain"] = chain
details["token"] = token
details["type"] = "totalAddressDeposited"
results.append(details)

for token, details in data.get("totalAddressWithdrawn", {}).items():
details["date"] = data["date"]
details["chain"] = chain
details["token"] = token
details["type"] = "totalAddressWithdrawn"
results.append(details)

df = pd.DataFrame(results)
return df


def get_bridge_transactions(self, id: int, params: Dict = None, raw: bool = True):
"""Get all transactions for a bridge within a date range.
Expand All @@ -665,6 +674,44 @@ def get_bridge_transactions(self, id: int, params: Dict = None, raw: bool = True
Returns:
- Dict or DataFrame: Raw data from the API or a transformed DataFrame.
"""
if isinstance(id, int):
id = [id]

if raw:
if len(id) == 1:
return self._get("BRIDGES", endpoint=f"/transactions/{id[0]}", params=params)

results = {}
for bridge_id in id:
results[bridge_id] = self._get("BRIDGES", endpoint=f"/transactions/{bridge_id}", params=params)
return results

else:
results = []

for bridge_id in id:
transactions = self._get("BRIDGES", endpoint=f"/transactions/{bridge_id}", params=params)

for entry in transactions:
results.append(
{
"tx_hash": entry.get("tx_hash"),
"timestamp": entry.get("ts"),
"tx_block": entry.get("tx_block"),
"tx_from": entry.get("tx_from"),
"tx_to": entry.get("tx_to"),
"token": entry.get("token"),
"amount": entry.get("amount"),
"chain": entry.get("chain"),
"bridge_name": entry.get("bridge_name"),
"usd_value": entry.get("usd_value"),
"sourceChain": entry.get("sourceChain")
}
)

df = pd.DataFrame(results)
return df


# --- Volumes --- #

Expand Down
8 changes: 5 additions & 3 deletions src/defillama_py/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@
# },
# )


# ttps://bridges.llama.fi/bridgedaystats/1694347200/ethereum?id=5
# df = obj.get_bridges(params={"includeChains": False}, raw=False)
# df = obj.get_bridge_transactions(id)
df = obj.get_chain_bridge_volume('Ethereum', raw=False)
# df = obj.get_bridge_day_stats(1694347200, ['ethereum'], raw=True)
# df = obj.get_bridge_transactions([1, 2], raw=False)

# df = obj.get_bridge_volume(["1", "2"], raw=True)
df = obj.get_chain_bridge_volume(["ethereum", "arbitrum"], raw=False)
# df = obj.get_chain_bridge_volume(["ethereum", "arbitrum"], raw=False)
print(df)
# print(str(df)[:1000])
# df.to_csv('test.csv')

0 comments on commit fae68f3

Please sign in to comment.