kopia lustrzana https://github.com/bugout-dev/moonstream
Resource migration to checksum address
rodzic
3120f39c59
commit
1dde5040a4
|
@ -5,6 +5,8 @@ import argparse
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from . import subscription_types
|
from . import subscription_types
|
||||||
|
from .migrations.checksum_address import checksum_all_subscription_addresses
|
||||||
|
from ..web3_provider import yield_web3_provider
|
||||||
from ..settings import (
|
from ..settings import (
|
||||||
BUGOUT_BROOD_URL,
|
BUGOUT_BROOD_URL,
|
||||||
BUGOUT_SPIRE_URL,
|
BUGOUT_SPIRE_URL,
|
||||||
|
@ -22,6 +24,12 @@ def parse_boolean_arg(raw_arg: Optional[str]) -> Optional[bool]:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def migrations_run(args: argparse.Namespace) -> None:
|
||||||
|
web3_session = yield_web3_provider()
|
||||||
|
if args.id == 1:
|
||||||
|
checksum_all_subscription_addresses(web3_session)
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
cli_description = f"""Moonstream Admin CLI
|
cli_description = f"""Moonstream Admin CLI
|
||||||
|
|
||||||
|
@ -212,6 +220,21 @@ This CLI is configured to work with the following API URLs:
|
||||||
func=subscription_types.cli_ensure_canonical_subscription_types
|
func=subscription_types.cli_ensure_canonical_subscription_types
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser_migrations = subcommands.add_parser(
|
||||||
|
"migrations", description="Manage database, resource and etc migrations"
|
||||||
|
)
|
||||||
|
parser_migrations.set_defaults(func=lambda _: parser_migrations.print_help())
|
||||||
|
subcommands_migrations = parser_migrations.add_subparsers(
|
||||||
|
description="Migration commands"
|
||||||
|
)
|
||||||
|
parser_migrations_run = subcommands_migrations.add_parser(
|
||||||
|
"run", description="Run migration"
|
||||||
|
)
|
||||||
|
parser_migrations_run.add_argument(
|
||||||
|
"-i", "--id", required=True, type=int, help="Provide migration ID"
|
||||||
|
)
|
||||||
|
parser_migrations_run.set_defaults(func=migrations_run)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
args.func(args)
|
args.func(args)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
"""
|
||||||
|
Convert all addresses in user subscriptions to checksum address.
|
||||||
|
"""
|
||||||
|
from bugout.data import BugoutResources
|
||||||
|
from bugout.exceptions import BugoutResponseException
|
||||||
|
from web3 import Web3
|
||||||
|
|
||||||
|
from ...settings import BUGOUT_REQUEST_TIMEOUT_SECONDS, MOONSTREAM_ADMIN_ACCESS_TOKEN
|
||||||
|
from ...settings import bugout_client as bc
|
||||||
|
|
||||||
|
|
||||||
|
def checksum_all_subscription_addresses(web3: Web3) -> None:
|
||||||
|
resources: BugoutResources = bc.list_resources(
|
||||||
|
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
||||||
|
params={"type": "subscription"},
|
||||||
|
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS,
|
||||||
|
)
|
||||||
|
for resource in resources.resources:
|
||||||
|
resource_data = resource.resource_data
|
||||||
|
try:
|
||||||
|
address = resource_data["address"]
|
||||||
|
resource_data["address"] = web3.toChecksumAddress(address)
|
||||||
|
updated_resource = bc.update_resource(
|
||||||
|
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
||||||
|
resource_id=resource.id,
|
||||||
|
resource_data={"update": resource_data},
|
||||||
|
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS,
|
||||||
|
)
|
||||||
|
print(f"Resource id: {updated_resource.id} updated")
|
||||||
|
except ValueError as e:
|
||||||
|
print(
|
||||||
|
f"Not valid checksum address: {address}, probably "
|
||||||
|
"txpool or whalewatch subscription"
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
except BugoutResponseException as e:
|
||||||
|
print(f"Bugout error: {e.status_code} with details: {e.detail}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Unexpected error: {repr(e)}")
|
||||||
|
continue
|
Ładowanie…
Reference in New Issue