Skip to content

Commit

Permalink
Merge pull request #113 from valory-xyz/develop
Browse files Browse the repository at this point in the history
Release v0.9.6
  • Loading branch information
DavidMinarsch authored Nov 19, 2023
2 parents 6d7da00 + 70c8d66 commit 9d6ef8b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 23 deletions.
20 changes: 12 additions & 8 deletions run_service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,21 @@ try_read_storage() {
# ------------------

set -e # Exit script on first error

# Initialize repo and version variables
org_name="valory-xyz"
directory="trader"
service_repo=https://github.com/$org_name/$directory.git
# This is a tested version that works well.
# Feel free to replace this with a different version of the repo, but be careful as there might be breaking changes
service_version="v0.9.6"

echo ""
echo "---------------"
echo " Trader runner "
echo "---------------"
echo ""
echo "This script will assist you in setting up and running the Trader service (https://github.com/valory-xyz/trader)."
echo "This script will assist you in setting up and running the Trader service ($service_repo)."
echo ""

# Check the command-line arguments
Expand Down Expand Up @@ -533,19 +542,14 @@ echo "RPC checks passed."
echo ""

# clone repo
directory="trader"
# This is a tested version that works well.
# Feel free to replace this with a different version of the repo, but be careful as there might be breaking changes
service_version="v0.9.6"
service_repo=https://github.com/valory-xyz/$directory.git
if [ -d $directory ]
then
echo "Detected an existing $directory repo. Using this one..."
echo "Detected an existing $directory directory. Using this one..."
echo "Please stop and manually delete the $directory repo if you updated the service's version ($service_version)!"
echo "You can run the following command, or continue with the pre-existing version of the service:"
echo "rm -r $directory"
else
echo "Cloning the $directory repo..."
echo "Cloning the $directory repo from $org_name GitHub..."
git clone --depth 1 --branch $service_version $service_repo
fi

Expand Down
93 changes: 78 additions & 15 deletions trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,50 @@ def argparse(s: str) -> "MarketAttribute":
STATS_TABLE_ROWS = list(MarketAttribute)


def _read_file(file_path):
with open(file_path, 'r') as file:
return file.readline().strip()


def _get_balance(address, rpc_url):
"""Get the native xDAI balance of an address in wei."""
headers = {
'Content-Type': 'application/json'
}
data = {
'jsonrpc': '2.0',
'method': 'eth_getBalance',
'params': [address, 'latest'],
'id': 1
}
response = requests.post(rpc_url, headers=headers, json=data)
return response.json().get('result')


def _get_token_balance(gnosis_address, token_contract_address, rpc_url):
"""Get the token balance of an address in wei."""
function_selector = '70a08231' # function selector for balanceOf(address)
padded_address = gnosis_address.replace('0x', '').rjust(64, '0') # remove '0x' and pad the address to 32 bytes
data = function_selector + padded_address

payload = {
"jsonrpc": "2.0",
"method": "eth_call",
"params": [
{
"to": token_contract_address,
"data": data
},
"latest"
],
"id": 1
}
response = requests.post(rpc_url, json=payload)
result = response.json().get('result', '0x0')
balance_wei = int(result, 16) # convert hex to int
return balance_wei


def _parse_args() -> Any:
"""Parse the creator positional argument."""
parser = ArgumentParser(description="Get trades on Omen for a Safe address.")
Expand Down Expand Up @@ -311,12 +355,18 @@ def _query_conditional_tokens_gc_subgraph(creator: str) -> dict[str, Any]:
return all_results


def wei_to_dai(wei: int) -> str:
"""Converts and formats Wei to DAI."""
dai = wei / 10**18
formatted_dai = "{:.2f}".format(dai)
return f"{formatted_dai} DAI"
def wei_to_unit(wei: int) -> float:
"""Converts wei to currency unit."""
return wei / 10**18


def wei_to_xdai(wei: int) -> str:
"""Converts and formats wei to xDAI."""
return "{:.2f} xDAI".format(wei_to_unit(wei))

def wei_to_wxdai(wei: int) -> str:
"""Converts and formats wei to WxDAI."""
return "{:.2f} WxDAI".format(wei_to_unit(wei))

def _is_redeemed(user_json: dict[str, Any], fpmmTrade: dict[str, Any]) -> bool:
user_positions = user_json["data"]["user"]["userPositions"]
Expand Down Expand Up @@ -413,7 +463,7 @@ def _format_table(table: dict[Any, dict[Any, Any]]) -> str:
f"{MarketAttribute.INVESTMENT:<{column_width}}"
+ "".join(
[
f"{wei_to_dai(table[MarketAttribute.INVESTMENT][c]):>{column_width}}"
f"{wei_to_xdai(table[MarketAttribute.INVESTMENT][c]):>{column_width}}"
for c in STATS_TABLE_COLS
]
)
Expand All @@ -423,7 +473,7 @@ def _format_table(table: dict[Any, dict[Any, Any]]) -> str:
f"{MarketAttribute.FEES:<{column_width}}"
+ "".join(
[
f"{wei_to_dai(table[MarketAttribute.FEES][c]):>{column_width}}"
f"{wei_to_xdai(table[MarketAttribute.FEES][c]):>{column_width}}"
for c in STATS_TABLE_COLS
]
)
Expand All @@ -433,7 +483,7 @@ def _format_table(table: dict[Any, dict[Any, Any]]) -> str:
f"{MarketAttribute.EARNINGS:<{column_width}}"
+ "".join(
[
f"{wei_to_dai(table[MarketAttribute.EARNINGS][c]):>{column_width}}"
f"{wei_to_xdai(table[MarketAttribute.EARNINGS][c]):>{column_width}}"
for c in STATS_TABLE_COLS
]
)
Expand All @@ -443,7 +493,7 @@ def _format_table(table: dict[Any, dict[Any, Any]]) -> str:
f"{MarketAttribute.NET_EARNINGS:<{column_width}}"
+ "".join(
[
f"{wei_to_dai(table[MarketAttribute.NET_EARNINGS][c]):>{column_width}}"
f"{wei_to_xdai(table[MarketAttribute.NET_EARNINGS][c]):>{column_width}}"
for c in STATS_TABLE_COLS
]
)
Expand All @@ -453,7 +503,7 @@ def _format_table(table: dict[Any, dict[Any, Any]]) -> str:
f"{MarketAttribute.REDEMPTIONS:<{column_width}}"
+ "".join(
[
f"{wei_to_dai(table[MarketAttribute.REDEMPTIONS][c]):>{column_width}}"
f"{wei_to_xdai(table[MarketAttribute.REDEMPTIONS][c]):>{column_width}}"
for c in STATS_TABLE_COLS
]
)
Expand All @@ -463,7 +513,7 @@ def _format_table(table: dict[Any, dict[Any, Any]]) -> str:
f"{MarketAttribute.ROI:<{column_width}}"
+ "".join(
[
f"{table[MarketAttribute.ROI][c]*100.0:>{column_width-4}.2f} % "
f"{table[MarketAttribute.ROI][c]*100.0:>{column_width-5}.2f} % "
for c in STATS_TABLE_COLS
]
)
Expand Down Expand Up @@ -528,8 +578,8 @@ def parse_user( # pylint: disable=too-many-locals,too-many-statements
statistics_table[MarketAttribute.FEES][market_status] += fee_amount

output += f" Market status: {market_status}\n"
output += f" Bought: {wei_to_dai(collateral_amount)} for {wei_to_dai(outcomes_tokens_traded)} {fpmm['outcomes'][outcome_index]!r} tokens\n"
output += f" Fee: {wei_to_dai(fee_amount)}\n"
output += f" Bought: {wei_to_xdai(collateral_amount)} for {wei_to_xdai(outcomes_tokens_traded)} {fpmm['outcomes'][outcome_index]!r} tokens\n"
output += f" Fee: {wei_to_xdai(fee_amount)}\n"
output += f" Your answer: {fpmm['outcomes'][outcome_index]!r}\n"

if market_status == MarketState.FINALIZING:
Expand All @@ -556,11 +606,11 @@ def parse_user( # pylint: disable=too-many-locals,too-many-statements
if is_invalid:
earnings = collateral_amount
output += " Final answer: Market has been declared invalid.\n"
output += f" Earnings: {wei_to_dai(earnings)}\n"
output += f" Earnings: {wei_to_xdai(earnings)}\n"
elif outcome_index == current_answer:
earnings = outcomes_tokens_traded
output += f" Final answer: {fpmm['outcomes'][current_answer]!r} - Congrats! The trade was for the winner answer.\n"
output += f" Earnings: {wei_to_dai(earnings)}\n"
output += f" Earnings: {wei_to_xdai(earnings)}\n"
redeemed = _is_redeemed(user_json, fpmmTrade)
output += f" Redeemed: {redeemed}\n"
statistics_table[MarketAttribute.WINNER_TRADES][market_status] += 1
Expand Down Expand Up @@ -591,6 +641,19 @@ def parse_user( # pylint: disable=too-many-locals,too-many-statements
output += "--------------------------\n"
output += "\n"

# Read rpc and get safe address balance
rpc_url_path = f"../.trader_runner/rpc.txt"
rpc_url = _read_file(rpc_url_path)
safe_address = user_args.creator
safe_address_balance = _get_balance(safe_address, rpc_url)

output += f"Safe address: {safe_address}\n"
output += f"Address balance: {wei_to_xdai(int(safe_address_balance, 16))}\n"

wxdai_contract_address = "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d"
wxdai_balance = _get_token_balance(safe_address, wxdai_contract_address, rpc_url)
output += f"Token balance: {wei_to_wxdai(wxdai_balance)}\n\n"

_compute_totals(statistics_table)
output += _format_table(statistics_table)

Expand Down

0 comments on commit 9d6ef8b

Please sign in to comment.