Skip to content

Commit

Permalink
Added saga csv. (#365)
Browse files Browse the repository at this point in the history
* add saga blockchain

* add basic unit tests for saga

* saga: added test files and update gitignore to ignore genesis json file

* saga: remove wrong text

---------

Co-authored-by: Nick Plante <nap@zerosum.org>
  • Loading branch information
hodgerpodger and zapnap authored Aug 29, 2024
1 parent 9e1b4ff commit ea0ca57
Show file tree
Hide file tree
Showing 21 changed files with 2,874 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ __pycache__/
# application ignores
env/*
src/staketaxcsv/_reports/*
src/staketaxcsv/tia/genesis.json
src/staketaxcsv/dym/genesis.json
src/staketaxcsv/tia/genesis.json
src/staketaxcsv/saga/genesis.json
sample.env
src/tests/data/lcdv1/_node_info/*

Expand Down
2 changes: 2 additions & 0 deletions src/staketaxcsv/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import staketaxcsv.report_osmo
import staketaxcsv.report_regen
import staketaxcsv.report_rowan
import staketaxcsv.report_saga
import staketaxcsv.report_scrt
import staketaxcsv.report_sol
import staketaxcsv.report_stars
Expand Down Expand Up @@ -65,6 +66,7 @@
co.TICKER_OSMO: staketaxcsv.report_osmo,
co.TICKER_REGEN: staketaxcsv.report_regen,
co.TICKER_ROWAN: staketaxcsv.report_rowan,
co.TICKER_SAGA: staketaxcsv.report_saga,
co.TICKER_SCRT: staketaxcsv.report_scrt,
co.TICKER_SOL: staketaxcsv.report_sol,
co.TICKER_STARS: staketaxcsv.report_stars,
Expand Down
1 change: 1 addition & 0 deletions src/staketaxcsv/common/ibc/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"QCK": "quicksilver",
"REGEN": "regen",
"ROWAN": "sifchain",
"SAGA": "saga",
"SCRT": "secret",
"SEI": "sei",
"SOMM": "sommelier",
Expand Down
4 changes: 2 additions & 2 deletions src/staketaxcsv/common/report_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from staketaxcsv.common.BalExporter import BALANCES_HISTORICAL
from staketaxcsv.settings_csv import (
REPORTS_DIR, TICKER_AKT, TICKER_ALGO, TICKER_ARCH, TICKER_ATOM, TICKER_COSMOSPLUS,
TICKER_EVMOS, TICKER_JUNO, TICKER_LUNA1, TICKER_LUNA2, TICKER_OSMO, TICKER_SOL, TICKER_STRD, TICKER_TIA)
TICKER_EVMOS, TICKER_JUNO, TICKER_LUNA1, TICKER_LUNA2, TICKER_OSMO, TICKER_SAGA, TICKER_SOL, TICKER_STRD, TICKER_TIA)
from staketaxcsv import settings_csv

ALL = "all"
Expand Down Expand Up @@ -95,7 +95,7 @@ def parse_args(ticker):
help="Path to the Koinly NullMap json file",
)
if ticker in [TICKER_AKT, TICKER_ALGO, TICKER_ARCH, TICKER_ATOM, TICKER_EVMOS,
TICKER_JUNO, TICKER_STRD, TICKER_SOL, TICKER_TIA]:
TICKER_JUNO, TICKER_SAGA, TICKER_STRD, TICKER_SOL, TICKER_TIA]:
parser.add_argument(
"--start_date",
type=str,
Expand Down
100 changes: 100 additions & 0 deletions src/staketaxcsv/report_saga.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""
usage: python3 staketaxcsv/report_saga.py <walletaddress> [--format all|cointracking|koinly|..]
Prints transactions and writes CSV(s) to _reports/SAGA*.csv
"""

import logging


import staketaxcsv.saga.processor
from staketaxcsv.saga.config import localconfig
from staketaxcsv.saga.progress import SECONDS_PER_PAGE, ProgressSaga
from staketaxcsv.saga.genesis import genesis_airdrop
from staketaxcsv.common import report_util
from staketaxcsv.common.Cache import Cache
from staketaxcsv.common.Exporter import Exporter
from staketaxcsv.settings_csv import SAGA_NODE, TICKER_SAGA
from staketaxcsv.common.ibc import api_lcd
from staketaxcsv.common.ibc.tx_data import TxDataLcd
from staketaxcsv.common.ibc.decorators import set_ibc_cache


def main():
report_util.main_default(TICKER_SAGA)


def read_options(options):
report_util.read_common_options(localconfig, options)
logging.info("localconfig: %s", localconfig.__dict__)


def _txdata():
max_txs = localconfig.limit
return TxDataLcd(SAGA_NODE, max_txs, limit_per_query=20)


def wallet_exists(wallet_address):
return api_lcd.make_lcd_api(SAGA_NODE).account_exists(wallet_address)


def txone(wallet_address, txid):
elem = _txdata().get_tx(txid)

exporter = Exporter(wallet_address, localconfig, TICKER_SAGA)
txinfo = staketaxcsv.saga.processor.process_tx(wallet_address, elem, exporter)

return exporter


def estimate_duration(wallet_address):
return SECONDS_PER_PAGE * _txdata().get_txs_pages_count(wallet_address)


@set_ibc_cache()
def txhistory(wallet_address):
""" Configure localconfig based on options dictionary. """
progress = ProgressSaga()
exporter = Exporter(wallet_address, localconfig, TICKER_SAGA)
txdata = _txdata()

# Fetch count of transactions to estimate progress more accurately
count_pages = txdata.get_txs_pages_count(wallet_address)
progress.set_estimate(count_pages)

# Fetch transactions
elems = txdata.get_txs_all(wallet_address, progress)

progress.report_message(f"Processing {len(elems)} transactions... ")
staketaxcsv.saga.processor.process_txs(wallet_address, elems, exporter)

return exporter


@set_ibc_cache()
def txhistory(wallet_address):
""" Configure localconfig based on options dictionary. """
progress = ProgressSaga()
exporter = Exporter(wallet_address, localconfig, TICKER_SAGA)
txdata = _txdata()

# Fetch/add genesis airdrop to csv
progress.report_message("Getting genesis airdrop amount...")
genesis_airdrop(wallet_address, exporter)

# Fetch count of transactions to estimate progress more accurately
count_pages = txdata.get_txs_pages_count(wallet_address)
progress.set_estimate(count_pages)

# Fetch transactions
elems = txdata.get_txs_all(wallet_address, progress)

progress.report_message(f"Processing {len(elems)} transactions... ")
staketaxcsv.saga.processor.process_txs(wallet_address, elems, exporter)

return exporter


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()
Empty file.
9 changes: 9 additions & 0 deletions src/staketaxcsv/saga/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from staketaxcsv.common.config import config
from staketaxcsv.settings_csv import MINTSCAN_MAX_TXS


class localconfig(config):

start_date = None
end_date = None
limit = MINTSCAN_MAX_TXS
3 changes: 3 additions & 0 deletions src/staketaxcsv/saga/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MINTSCAN_LABEL_SAGA = "saga"
CURRENCY_SAGA = "SAGA"
EXCHANGE_SAGA = "saga_blockchain"
41 changes: 41 additions & 0 deletions src/staketaxcsv/saga/genesis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json
import logging
import os
import requests

import staketaxcsv.saga.constants as co
from staketaxcsv.saga.make_tx import make_genesis_airdrop_tx

GENESIS_JSON = os.environ.get("STAKETAX_SAGA_GENESIS_JSON", os.path.join(os.path.dirname(__file__), "genesis.json"))
GENESIS_URL = "https://raw.githubusercontent.com/sagaxyz/mainnet/main/genesis/genesis.json"


def genesis_airdrop(wallet_address, exporter):
amount_saga = _genesis_airdrop_saga_amount(wallet_address)
if amount_saga:
row = make_genesis_airdrop_tx(amount_saga, wallet_address)
exporter.ingest_row(row)


def _genesis_airdrop_saga_amount(wallet_address):
data = _get_genesis_data()

balances = data["app_state"]["bank"]["balances"]
for balance in balances:
if balance["address"] == wallet_address:
amount = balance["coins"][0]["amount"]
return float(amount) / 1000000

return 0


def _get_genesis_data():
if not os.path.exists(GENESIS_JSON):
logging.info("Fetching genesis url %s ...", GENESIS_URL)
response = requests.get(GENESIS_URL)
with open(GENESIS_JSON, 'w') as file:
file.write(response.text)

with open(GENESIS_JSON, 'r') as file:
data = json.load(file)
return data
21 changes: 21 additions & 0 deletions src/staketaxcsv/saga/make_tx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from staketaxcsv.common.Exporter import Row
from staketaxcsv.common.ExporterTypes import TX_TYPE_AIRDROP
from staketaxcsv.saga import constants as co


def make_genesis_airdrop_tx(amount_saga, wallet_address):
row = Row(
timestamp="2024-04-08 00:00:00", # timestamp of height 1 (https://www.mintscan.io/saga/block/1)
tx_type=TX_TYPE_AIRDROP,
received_amount=amount_saga,
received_currency=co.CURRENCY_SAGA,
sent_amount="",
sent_currency="",
fee="",
fee_currency="",
exchange=co.EXCHANGE_SAGA,
wallet_address=wallet_address,
txid="saga_genesis_airdrop",
url=""
)
return row
31 changes: 31 additions & 0 deletions src/staketaxcsv/saga/processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import logging

import staketaxcsv.saga.constants as co
import staketaxcsv.common.ibc.handle
import staketaxcsv.common.ibc.processor
from staketaxcsv.saga.config import localconfig
from staketaxcsv.settings_csv import SAGA_NODE


def process_txs(wallet_address, elems, exporter):
for elem in elems:
process_tx(wallet_address, elem, exporter)


def process_tx(wallet_address, elem, exporter):
txinfo = staketaxcsv.common.ibc.processor.txinfo(
wallet_address, elem, co.MINTSCAN_LABEL_SAGA, SAGA_NODE)
txinfo.url = "https://www.mintscan.io/saga/tx/{}".format(txinfo.txid)

if txinfo.is_failed:
staketaxcsv.common.ibc.processor.handle_failed_transaction(exporter, txinfo)
return txinfo

for msginfo in txinfo.msgs:
result = staketaxcsv.common.ibc.processor.handle_message(exporter, txinfo, msginfo, localconfig.debug)
if result:
continue

staketaxcsv.common.ibc.handle.handle_unknown_detect_transfers(exporter, txinfo, msginfo)

return txinfo
13 changes: 13 additions & 0 deletions src/staketaxcsv/saga/progress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from staketaxcsv.saga.config import localconfig
from staketaxcsv.common.progress import Progress

SECONDS_PER_PAGE = 4


class ProgressSaga(Progress):

def __init__(self):
super().__init__(localconfig)

def set_estimate(self, count_pages):
self.add_stage("default", count_pages, SECONDS_PER_PAGE)
2 changes: 2 additions & 0 deletions src/staketaxcsv/settings_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
OSMO_NODE = os.environ.get("STAKETAX_OSMO_NODE", "https://lcd.osmosis.zone")
REGEN_NODE = os.environ.get("STAKETAX_REGEN_NODE", "")
ROWAN_NODE = os.environ.get("STAKETAX_ROWAN_NODE", "")
SAGA_NODE = os.environ.get("STAKETAX_SAGA_NODE", "https://saga-api.polkachu.com")
SCRT_NODE = os.environ.get("STAKETAX_SCRT_NODE", "")
SOL_NODE = os.environ.get("STAKETAX_SOL_NODE", "https://api.mainnet-beta.solana.com")
STARS_NODE = os.environ.get("STAKETAX_STARS_NODE", "")
Expand Down Expand Up @@ -79,6 +80,7 @@
TICKER_OSMO = "OSMO"
TICKER_REGEN = "REGEN"
TICKER_ROWAN = "ROWAN"
TICKER_SAGA = "SAGA"
TICKER_SCRT = "SCRT"
TICKER_SOL = "SOL"
TICKER_STARS = "STARS"
Expand Down
Loading

0 comments on commit ea0ca57

Please sign in to comment.