Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion run_service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ export OMEN_CREATORS='["0x89c5cc945dd550BcFfb72Fe42BfF002429F46Fec"]'
export BET_THRESHOLD=100000000000000000
export TRADING_STRATEGY=kelly_criterion
export PROMPT_TEMPLATE="Please take over the role of a Data Scientist to evaluate the given question. With the given question \"@{question}\" and the \`yes\` option represented by \`@{yes}\` and the \`no\` option represented by \`@{no}\`, what are the respective probabilities of \`p_yes\` and \`p_no\` occurring?"
export IRRELEVANT_TOOLS='["claude-prediction-online", "openai-gpt-3.5-turbo-instruct", "prediction-online-summarized-info", "prediction-online-sum-url-content", "prediction-online", "openai-text-davinci-002", "openai-text-davinci-003", "openai-gpt-3.5-turbo", "openai-gpt-4", "stabilityai-stable-diffusion-v1-5", "stabilityai-stable-diffusion-xl-beta-v2-2-2", "stabilityai-stable-diffusion-512-v2-1", "stabilityai-stable-diffusion-768-v2-1", "deepmind-optimization-strong", "deepmind-optimization", "claude-prediction-offline"]'
export IRRELEVANT_TOOLS='["prediction-offline-sme", "openai-gpt-3.5-turbo-instruct", "prediction-online-summarized-info", "prediction-online-sum-url-content", "prediction-online", "openai-text-davinci-002", "openai-text-davinci-003", "openai-gpt-3.5-turbo", "openai-gpt-4", "stabilityai-stable-diffusion-v1-5", "stabilityai-stable-diffusion-xl-beta-v2-2-2", "stabilityai-stable-diffusion-512-v2-1", "stabilityai-stable-diffusion-768-v2-1", "deepmind-optimization-strong", "deepmind-optimization", "claude-prediction-offline"]'

service_dir="trader_service"
build_dir="abci_build"
Expand Down
147 changes: 96 additions & 51 deletions scripts/staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 Valory AG
# Copyright 2023-2024 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,23 +39,24 @@
get_service_info,
get_stake_txs,
get_unstake_txs,
is_service_staked,
is_service_evicted,
is_service_staked,
send_tx_and_wait_for_receipt,
)


EVEREST_STAKING_CONTRACT_ADDRESS = "0x5add592ce0a1B5DceCebB5Dcac086Cd9F9e3eA5C"


def format_duration(duration_seconds: int) -> str:
def _format_duration(duration_seconds: int) -> str:
days, remainder = divmod(duration_seconds, 86400)
hours, remainder = divmod(remainder, 3600)
minutes, _ = divmod(remainder, 60)
formatted_duration = f"{days}D {hours}h {minutes}m"
return formatted_duration


def unstake_everest(
def _unstake_everest(
ledger_api: EthereumApi, service_id: int, owner_crypto: EthereumCrypto
) -> None:
print("Checking if service is staked on Everest...")
Expand Down Expand Up @@ -87,21 +88,60 @@ def unstake_everest(
def _check_unstaking_availability(
now: float,
ts_start: float,
minimum_staking_duration: float,
minimum_staking_duration: int,
available_rewards: float,
staking_program: str
staking_program: str,
) -> None:
if (now - ts_start) < minimum_staking_duration and available_rewards > 0:
print(
f"WARNING: Your service has been staked on {staking_program} for {format_duration(int(now - ts_start))}."
f"WARNING: Your service has been staked on {staking_program} for {_format_duration(int(now - ts_start))}."
)
print(
f"You cannot unstake your service from {staking_program} until it has been staked for at least {format_duration(minimum_staking_duration)}."
f"You cannot unstake your service from {staking_program} until it has been staked for at least {_format_duration(minimum_staking_duration)}."
)
print("Terminating script.")
sys.exit(1)


def _try_stake_service(
ledger_api: EthereumApi,
service_id: int,
owner_crypto: EthereumCrypto,
service_registry_address: str,
staking_contract_address: str,
) -> None:
if get_available_staking_slots(ledger_api, staking_contract_address) > 0:
print(
f"Service {service_id} is not staked on {staking_program}. Checking for available rewards..."
)
available_rewards = get_available_rewards(ledger_api, staking_contract_address)
if available_rewards == 0:
# no rewards available, do nothing
print(f"No rewards available. Service {service_id} cannot be staked.")
sys.exit(0)

print(
f"Rewards available: {available_rewards/10**18:.2f} OLAS. Staking service {service_id}..."
)
stake_txs = get_stake_txs(
ledger_api,
service_id,
service_registry_address,
staking_contract_address,
)
for tx in stake_txs:
send_tx_and_wait_for_receipt(ledger_api, owner_crypto, tx)

print(f"Service {service_id} staked successfully on {staking_program}.")
sys.exit(0)
else:
print(
f"All staking slots for contract {staking_contract_address} are taken. Service {service_id} cannot be staked."
)
print("The script will finish.")
sys.exit(1)


if __name__ == "__main__":
try:
staking_program = "Alpine"
Expand Down Expand Up @@ -144,18 +184,15 @@ def _check_unstaking_availability(
private_key_path=args.owner_private_key_path, password=args.password
)

unstake_everest(ledger_api, args.service_id, owner_crypto)

_unstake_everest(ledger_api, args.service_id, owner_crypto)

# Collect information
next_ts = get_next_checkpoint_ts(ledger_api, args.staking_contract_address)
ts_start = get_service_info(
ledger_api, args.service_id, args.staking_contract_address
)[3]

liveness_period = get_liveness_period(
ledger_api, args.staking_contract_address
)
liveness_period = get_liveness_period(ledger_api, args.staking_contract_address)
last_ts = next_ts - liveness_period
now = time.time()

Expand All @@ -178,11 +215,17 @@ def _check_unstaking_availability(
ledger_api, args.service_id, args.staking_contract_address
):
print(
f"WARNING: Your service has been evicted from the {staking_program} staking program due to inactivity."
f"WARNING: Service {args.service_id} has been evicted from the {staking_program} staking program due to inactivity."
)
input("Press Enter to continue...")

_check_unstaking_availability(now, ts_start, minimum_staking_duration, available_rewards, staking_program)
_check_unstaking_availability(
now,
ts_start,
minimum_staking_duration,
available_rewards,
staking_program,
)

if now < next_ts:
formatted_last_ts = datetime.utcfromtimestamp(last_ts).strftime(
Expand All @@ -204,7 +247,7 @@ def _check_unstaking_availability(
)

user_input = input(
f"Do you want to continue unstaking from {staking_program}? (yes/no)\n"
f"Do you want to continue unstaking service {args.service_id} from {staking_program}? (yes/no)\n"
).lower()
print()

Expand All @@ -218,7 +261,9 @@ def _check_unstaking_availability(
)
for tx in unstake_txs:
send_tx_and_wait_for_receipt(ledger_api, owner_crypto, tx)
print(f"Successfully unstaked from {staking_program}.")
print(
f"Successfully unstaked service {args.service_id} from {staking_program}."
)
sys.exit(0)

if is_service_staked(
Expand All @@ -228,18 +273,34 @@ def _check_unstaking_availability(
ledger_api, args.service_id, args.staking_contract_address
):
print(
f"Your service has been evicted from the {staking_program} staking program due to inactivity. Unstaking..."
f"Service {args.service_id} has been evicted from the {staking_program} staking program due to inactivity. Unstaking..."
)

_check_unstaking_availability(now, ts_start, minimum_staking_duration, available_rewards, staking_program)
_check_unstaking_availability(
now,
ts_start,
minimum_staking_duration,
available_rewards,
staking_program,
)

unstake_txs = get_unstake_txs(
ledger_api, args.service_id, args.staking_contract_address
)
for tx in unstake_txs:
send_tx_and_wait_for_receipt(ledger_api, owner_crypto, tx)

print(f"Successfully unstaked from {staking_program}.")
print(
f"Successfully unstaked service {args.service_id} from {staking_program}."
)

_try_stake_service(
ledger_api=ledger_api,
service_id=args.service_id,
owner_crypto=owner_crypto,
service_registry_address=args.service_registry_address,
staking_contract_address=args.staking_contract_address,
)
sys.exit(0)

print(
Expand All @@ -250,49 +311,33 @@ def _check_unstaking_availability(
ledger_api, args.staking_contract_address
)
if available_rewards == 0:
print("No rewards available. Unstaking...")
print(
f"No rewards available. Unstaking service {args.service_id} from {staking_program}..."
)
unstake_txs = get_unstake_txs(
ledger_api, args.service_id, args.staking_contract_address
)
for tx in unstake_txs:
send_tx_and_wait_for_receipt(ledger_api, owner_crypto, tx)

print(f"Successfully unstaked from {staking_program}.")
sys.exit(0)

print("There are rewards available. The service should remain staked.")
sys.exit(0)
elif get_available_staking_slots(ledger_api, args.staking_contract_address) > 0:
print(
f"Service {args.service_id} is not staked on {staking_program}. Checking for available rewards..."
)
available_rewards = get_available_rewards(
ledger_api, args.staking_contract_address
)
if available_rewards == 0:
# no rewards available, do nothing
print("No rewards available. The service cannot be staked.")
print(
f"Successfully unstaked service {args.service_id} from {staking_program}."
)
sys.exit(0)

print(
f"Rewards available: {available_rewards/10**18:.2f} OLAS. Staking the service..."
)
stake_txs = get_stake_txs(
ledger_api,
args.service_id,
args.service_registry_address,
args.staking_contract_address,
f"There are rewards available. The service {args.service_id} should remain staked."
)
for tx in stake_txs:
send_tx_and_wait_for_receipt(ledger_api, owner_crypto, tx)

print(f"Service {args.service_id} staked successfully on {staking_program}.")
sys.exit(0)
else:
print(
f"All staking slots for contract {args.staking_contract_address} are taken. Your service cannot be staked."
_try_stake_service(
ledger_api=ledger_api,
service_id=args.service_id,
owner_crypto=owner_crypto,
service_registry_address=args.service_registry_address,
staking_contract_address=args.staking_contract_address,
)
print("The script will finish.")
sys.exit(1)

except Exception as e: # pylint: disable=broad-except
print(f"An error occurred while executing {Path(__file__).name}: {str(e)}")
traceback.print_exc()
Expand Down