Skip to content

Commit

Permalink
from_key
Browse files Browse the repository at this point in the history
  • Loading branch information
akremstudy committed Jul 24, 2023
2 parents d1ccd89 + 15e8825 commit 2815b2c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/telliot_feeds/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.11dev0"
__version__ = "0.1.11"
15 changes: 8 additions & 7 deletions src/telliot_feeds/cli/commands/settle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from click.core import Context
from telliot_core.cli.utils import async_run

from telliot_feeds.cli.utils import CustomHexBytes
from telliot_feeds.cli.utils import get_accounts_from_name
from telliot_feeds.cli.utils import reporter_cli_core
from telliot_feeds.cli.utils import valid_diva_chain
Expand All @@ -26,7 +27,7 @@ def diva() -> None:
"pool_id",
help="pool ID for Diva Protocol",
nargs=1,
type=str,
type=CustomHexBytes,
required=True,
)
@click.option(
Expand Down Expand Up @@ -54,16 +55,16 @@ def diva() -> None:
async def settle(
ctx: Context,
account_str: str,
pool_id: str,
pool_id: CustomHexBytes,
password: str,
legacy_gas_price: int = 100,
legacy_gas_price: int,
) -> None:
"""Settle a derivative pool in DIVA Protocol."""
ctx.obj["ACCOUNT_NAME"] = account_str
accounts = get_accounts_from_name(account_str)
if not accounts:
return

ctx.obj["CHAIN_ID"] = accounts[0].chains[0]
try:
if not password:
password = getpass.getpass(f"Enter password for {account_str} keyfile: ")
Expand All @@ -85,8 +86,8 @@ async def settle(
oracle = DivaOracleTellorContract(core.endpoint, account)
oracle.connect()

status = await oracle.set_final_reference_value(pool_id=pool_id, legacy_gas_price=legacy_gas_price)
status = await oracle.set_final_reference_value(pool_id=pool_id.hex(), legacy_gas_price=legacy_gas_price)
if status is not None and status.ok:
click.echo(f"Pool {pool_id} settled.")
click.echo(f"Pool {pool_id.hex()} settled.")
else:
click.echo(f"Unable to settle Pool {pool_id}.")
click.echo(f"Unable to settle Pool {pool_id.hex()}.")
12 changes: 11 additions & 1 deletion src/telliot_feeds/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from typing import Any
from typing import Callable
from typing import Dict
from typing import cast
from typing import get_args
from typing import get_type_hints
from typing import Optional
from typing import Type
from typing import Union

import click
Expand All @@ -14,6 +16,7 @@
from dotenv import load_dotenv
from eth_typing import ChecksumAddress
from eth_utils import to_checksum_address
from hexbytes import HexBytes
from simple_term_menu import TerminalMenu
from telliot_core.apps.core import TelliotCore
from telliot_core.cli.utils import cli_core
Expand Down Expand Up @@ -98,7 +101,7 @@ def reporter_cli_core(ctx: click.Context) -> TelliotCore:
core = cli_core(ctx)

# Ensure chain id compatible with flashbots relay
if ctx.obj.get("SIGNATURE_ACCOUNT_NAME") is not None:
if ctx.obj.get("SIGNATURE_ACCOUNT_NAME", None) is not None:
# Only supports mainnet
assert core.config.main.chain_id in (1, 5)

Expand Down Expand Up @@ -475,3 +478,10 @@ async def call_oracle(
except ValueError as e:
if "no gas strategy selected" in str(e):
click.echo("Can't set gas fees automatically. Please specify gas fees manually.")
class CustomHexBytes(HexBytes):
"""Wrapper around HexBytes that doesn't accept int or bool"""

def __new__(cls: Type[bytes], val: Union[bytearray, bytes, str]) -> "CustomHexBytes":
if isinstance(val, (int, bool)):
raise ValueError("Invalid value")
return cast(CustomHexBytes, super().__new__(cls, val))
2 changes: 1 addition & 1 deletion tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_report_options_available():
def test_cmd_settle():
"""Test CLI settle DIVA pool command"""
runner = CliRunner()
result = runner.invoke(cli_main, ["--test-config", "settle", "--pool-id", "a;lsdkfj;ak"])
result = runner.invoke(cli_main, ["--test-config", "settle", "--pool-id", 1])

expected = "Invalid value"

Expand Down
17 changes: 17 additions & 0 deletions tests/cli/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

from telliot_feeds.cli.utils import build_query
from telliot_feeds.cli.utils import call_oracle
from hexbytes import HexBytes

from telliot_feeds.cli.utils import CustomHexBytes
from telliot_feeds.queries.abi_query import AbiQuery
from telliot_feeds.queries.price.spot_price import SpotPrice
from telliot_feeds.reporters.stake import Stake
Expand Down Expand Up @@ -80,3 +83,17 @@ def __init__(self):
user_inputs=user_inputs,
)
assert "withdrawStake transaction succeeded" in caplog.text
def test_custom_hexbytes_wrapper():
"""Test custom hexbytes wrapper."""
# test when 0x is present and not present
for value in (CustomHexBytes("0x1234"), CustomHexBytes("1234")):
value = CustomHexBytes("0x1234")
assert value.hex() == "0x1234"
assert isinstance(value, CustomHexBytes)
assert isinstance(value, HexBytes)
assert isinstance(value, bytes)
with pytest.raises(ValueError):
CustomHexBytes(1)
CustomHexBytes(1.1)
CustomHexBytes(True)
CustomHexBytes(False)

0 comments on commit 2815b2c

Please sign in to comment.