Skip to content

Commit

Permalink
add rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelsoup42 committed Sep 9, 2023
1 parent 0f08d4b commit d950f34
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions src/cardano_account_pandas_dumper/cardano_account_pandas_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,20 @@ def _assets_from_transactions(self, api: BlockFrostApi) -> pd.Series:
)

@staticmethod
def _reward_transaction(api: BlockFrostApi, reward: Namespace) -> Namespace:
def _reward_transaction(
api: BlockFrostApi, reward: Namespace, pools: Mapping[str, Namespace]
) -> Namespace:
result = Namespace()
result.tx_hash = None
result.metadata = Namespace()
result.metadata.message = f"Reward: {reward.type} - {reward.epoch}"
pool_name = (
pools[reward.pool_id].name if reward.pool_id in pools else reward.pool_id
)
result.metadata = [
Namespace(
label="674",
json_metadata=f"Reward: {reward.type} - {pool_name} - {reward.epoch}",
)
]
result.reward_amount = reward.amount
epoch = api.epoch(reward.epoch + 1) # Time is right before start of next epoch.
result.block_time = epoch.start_time
Expand All @@ -122,12 +131,21 @@ def _reward_transaction(api: BlockFrostApi, reward: Namespace) -> Namespace:
return result

def _reward_transactions(self, api: BlockFrostApi) -> pd.Series:
result_list = [
self._reward_transaction(api=api, reward=a_r)
reward_list = [
a_r
for s_a in self.staking_addresses
for a_r in api.account_rewards(s_a, gather_pages=True)
]
return pd.Series(name="Rewards", data=result_list)

pool_result_list = {
pool: api.pool_metadata(pool)
for pool in frozenset([r.pool_id for r in reward_list])
}
reward_result_list = [
self._reward_transaction(api=api, reward=a_r, pools=pool_result_list)
for a_r in reward_list
]
return pd.Series(name="Rewards", data=reward_result_list)


class AccountPandasDumper:
Expand Down Expand Up @@ -327,9 +345,11 @@ def _make_hash_frame(self) -> pd.DataFrame:
tx_hash = pd.DataFrame(
data=[
x.hash
for x in itertools.chain(
self.data.transactions,
self.data.reward_transactions if self.rewards else pd.Series(),
for x in pd.concat(
objs=[
self.data.transactions,
self.data.reward_transactions if self.rewards else pd.Series(),
]
)
],
columns=["hash"],
Expand All @@ -342,9 +362,11 @@ def _make_timestamp_frame(self) -> pd.DataFrame:
data=[
np.datetime64(datetime.datetime.fromtimestamp(x.block_time))
+ (int(x.index) * self.TRANSACTION_OFFSET)
for x in itertools.chain(
self.data.transactions,
self.data.reward_transactions if self.rewards else pd.Series(),
for x in pd.concat(
objs=[
self.data.transactions,
self.data.reward_transactions if self.rewards else pd.Series(),
],
)
],
columns=["timestamp"],
Expand All @@ -358,9 +380,11 @@ def _make_message_frame(self) -> pd.DataFrame:
message = pd.DataFrame(
data=[
self._format_message(x)
for x in itertools.chain(
self.data.transactions,
self.data.reward_transactions if self.rewards else pd.Series(),
for x in pd.concat(
objs=[
self.data.transactions,
self.data.reward_transactions if self.rewards else pd.Series(),
],
)
],
columns=["message"],
Expand All @@ -372,9 +396,11 @@ def _make_balance_frame(self) -> pd.DataFrame:
balance = pd.DataFrame(
data=[
self._transaction_balance(x)
for x in itertools.chain(
self.data.transactions,
self.data.reward_transactions if self.rewards else pd.Series(),
for x in pd.concat(
objs=[
self.data.transactions,
self.data.reward_transactions if self.rewards else pd.Series(),
],
)
],
)
Expand All @@ -383,6 +409,8 @@ def _make_balance_frame(self) -> pd.DataFrame:
return balance

def make_transaction_frame(self) -> pd.DataFrame:
"""Build a transaction spreadsheet."""

# Add total line at the bottom
# total = []
# for column in outputs.columns:
Expand Down

0 comments on commit d950f34

Please sign in to comment.