-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
194 lines (156 loc) · 6.25 KB
/
main.py
File metadata and controls
194 lines (156 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import csv
import time
import os
import pandas as pd
import sys
# module path
nifty_module_path = 'utils'
sys.path.append(nifty_module_path)
# module imports
import nifty_scrap
from abc import ABC, abstractmethod
from datetime import datetime, timedelta
from dune_client.types import QueryParameter
from dune_client.client import DuneClient
from dune_client.query import QueryBase
from dotenv import load_dotenv
from nifty_scrap import get_total_sales_volume
# dune setup
load_dotenv()
dune = DuneClient.from_env()
# abstract class for data providers
class DataProvider(ABC):
name: str
@property
@abstractmethod
def name(self) -> str:
return "data-provider"
@abstractmethod
def get_7day_volume(self):
# return 7 day volume as a float
# ...
pass
@abstractmethod
def get_royalties_earned(self):
# return royalties earned as a float
# ...
pass
# concrete classes for marketplaces
class NiftyProvider(DataProvider):
@property
def name(self) -> str:
return "nifty_gateway"
def __init__(self):
self._volume = None # initialise new volume as none
def get_7day_volume(self):
# fetch and store the volume only if it hasn't been retrieved before
if self._volume is None:
self._volume = get_total_sales_volume()
return self._volume
def get_royalties_earned(self):
nifty_royalties = 0.05 * self._volume # Calculate 5% of the sales volume
return f"{nifty_royalties:.2f}"
class OpenseaProvider(DataProvider):
@property
def name(self) -> str:
return "opensea"
def get_7day_volume(self):
response = dune.get_latest_result(1933290)
if response and response.result and response.result.rows:
try:
# Find the 'OpenSea' entry and return its volume
opensea_volume = next(row['volume'] for row in response.result.rows if row['project'] == 'OpenSea')
return f"{opensea_volume:.2f}"
except StopIteration:
# Handle the case where 'OpenSea' is not found
return 0
return 0 # Return 0 if there's no valid data
def get_royalties_earned(self):
response = dune.get_latest_result(2021068)
if response and response.result and response.result.rows:
try:
# Find the 'OpenSea' entry and return its volume
opensea_royalties = next(row['fees_paid'] for row in response.result.rows if row['name'] == 'OpenSea')
return f"{opensea_royalties:.2f}"
except StopIteration:
# Handle the case where 'OpenSea' is not found
return 0
return 0 # Return 0 if there's no valid data
class BlurProvider(DataProvider):
@property
def name(self) -> str:
return "blur"
def get_7day_volume(self):
response = dune.get_latest_result(1933290)
if response and response.result and response.result.rows:
try:
blur_volume = next(row['volume'] for row in response.result.rows if row['project'] == 'Blur')
return f"{blur_volume:.2f}"
except StopIteration:
# Handle the case where 'Blur' is not found
return 0
return 0 # Return 0 if there's no valid data
def get_royalties_earned(self):
response = dune.get_latest_result(2021068)
if response and response.result and response.result.rows:
try:
# Find the 'Blur' entry and return its volume
blur_royalties = next(row['fees_paid'] for row in response.result.rows if row['name'] == 'Blur')
return f"{blur_royalties:.2f}"
except StopIteration:
# Handle the case where 'Blur' is not found
return 0
return 0 # Return 0 if there's no valid data
class MagicEdenProvider(DataProvider):
@property
def name(self) -> str:
return "magic_eden"
def get_7day_volume(self):
response = dune.get_latest_result(1933290)
if response and response.result and response.result.rows:
try:
magic_eden_volume = next(row['volume'] for row in response.result.rows if row['project'] == 'Magic Eden')
return f"{magic_eden_volume:.2f}"
except StopIteration:
# Handle the case where 'Magic Eden' is not found
return 0
return 0 # Return 0 if there's no valid data
def get_royalties_earned(self):
response = dune.get_latest_result(2021068)
if response and response.result and response.result.rows:
try:
# Find the 'Magic Eden' entry and return its volume
magic_eden_royalties = next(row['fees_paid'] for row in response.result.rows if row['name'] == 'Magic Eden')
return f"{magic_eden_royalties:.2f}"
except StopIteration:
# Handle the case where 'Magic Eden' is not found
return 0
return 0 # Return 0 if there's no valid data
# setting up the dataframe
data_providers = [
NiftyProvider(),
OpenseaProvider(),
BlurProvider(),
MagicEdenProvider(),
]
COLUMN_NAMES = ["project", "7_day_volume", "7_day_royalties"]
df = pd.DataFrame(columns=COLUMN_NAMES)
for provider in data_providers:
# Get and format the 7-day volume
volume = float(provider.get_7day_volume()) # Assuming get_7day_volume() returns a string that can be converted to float
formatted_volume = f"{volume:.2f}"
# Get and format the royalties
royalties = float(provider.get_royalties_earned()) # Assuming get_royalties_earned() returns a string that can be converted to float
formatted_royalties = f"{royalties:.2f}"
# Append data to DataFrame using df.loc
df.loc[len(df)] = [provider.name, formatted_volume, formatted_royalties]
print(df)
# date formatting
today = datetime.now()
seven_days_ago = today - timedelta(days=7)
start_date_str = seven_days_ago.strftime("%m%d")
end_date_str = today.strftime("%m%d")
# CSV writing
df.to_csv(f"output/{start_date_str}-{end_date_str}.csv", index=False)
if __name__ == "__main__":
pass