Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: open oracle settlement functionality and test #123

Open
wants to merge 19 commits into
base: develop
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
appnope==0.1.3 ; python_version >= "3.9" and python_version < "3.11" and platform_system == "Darwin" or python_version >= "3.9" and python_version < "3.11" and sys_platform == "darwin"
appnope==0.1.3 ; python_version >= "3.9" and python_version < "3.11" and (platform_system == "Darwin" or sys_platform == "darwin")
asttokens==2.2.1 ; python_version >= "3.9" and python_version < "3.11"
attrs==22.2.0 ; python_version >= "3.9" and python_version < "3.11"
backcall==0.2.0 ; python_version >= "3.9" and python_version < "3.11"
Expand All @@ -7,7 +7,7 @@ certifi==2022.12.7 ; python_version >= "3.9" and python_version < "3.11"
cffi==1.15.1 ; python_version >= "3.9" and python_version < "3.11"
charset-normalizer==3.1.0 ; python_version >= "3.9" and python_version < "3.11"
click==8.1.3 ; python_version >= "3.9" and python_version < "3.11"
colorama==0.4.6 ; python_version >= "3.9" and python_version < "3.11" and sys_platform == "win32" or python_version >= "3.9" and python_version < "3.11" and platform_system == "Windows"
colorama==0.4.6 ; python_version >= "3.9" and python_version < "3.11" and (sys_platform == "win32" or platform_system == "Windows")
comm==0.1.3 ; python_version >= "3.9" and python_version < "3.11"
contourpy==1.0.7 ; python_version >= "3.9" and python_version < "3.11"
cycler==0.11.0 ; python_version >= "3.9" and python_version < "3.11"
Expand Down
4 changes: 2 additions & 2 deletions requirements-learning.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
absl-py==1.4.0 ; python_version >= "3.9" and python_version < "3.11"
appnope==0.1.3 ; python_version >= "3.9" and python_version < "3.11" and platform_system == "Darwin" or python_version >= "3.9" and python_version < "3.11" and sys_platform == "darwin"
appnope==0.1.3 ; python_version >= "3.9" and python_version < "3.11" and (platform_system == "Darwin" or sys_platform == "darwin")
asttokens==2.2.1 ; python_version >= "3.9" and python_version < "3.11"
attrs==22.2.0 ; python_version >= "3.9" and python_version < "3.11"
backcall==0.2.0 ; python_version >= "3.9" and python_version < "3.11"
Expand All @@ -10,7 +10,7 @@ cffi==1.15.1 ; python_version >= "3.9" and python_version < "3.11"
charset-normalizer==3.1.0 ; python_version >= "3.9" and python_version < "3.11"
click==8.1.3 ; python_version >= "3.9" and python_version < "3.11"
cloudpickle==2.2.1 ; python_version >= "3.9" and python_version < "3.11"
colorama==0.4.6 ; python_version >= "3.9" and python_version < "3.11" and platform_system == "Windows" or python_version >= "3.9" and python_version < "3.11" and sys_platform == "win32"
colorama==0.4.6 ; python_version >= "3.9" and python_version < "3.11" and (platform_system == "Windows" or sys_platform == "win32")
comm==0.1.3 ; python_version >= "3.9" and python_version < "3.11"
contourpy==1.0.7 ; python_version >= "3.9" and python_version < "3.11"
cycler==0.11.0 ; python_version >= "3.9" and python_version < "3.11"
Expand Down
106 changes: 106 additions & 0 deletions tests/integration/test_openoracle_settlement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import pytest

from tests.integration.utils.fixtures import (
build_basic_market,
vega_service,
MM_WALLET,
AUCTION1 as TRADER_1_WALLET,
AUCTION2 as TRADER_2_WALLET,
TERMINATE_WALLET,
)
from vega_sim.null_service import VegaServiceNull
import vega_sim.api.governance as vega_gov
import random

wallets = [MM_WALLET, TRADER_1_WALLET, TRADER_2_WALLET]

PAYLOAD = """{"timestamp":"1660832460","messages":["0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062fe4acc00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000005788505200000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034254430000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062fe4acc00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000006f3c41e00000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034554480000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062fe4acc00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000001c697000000000000000000000000000000000000000000000000000000000000000067072696365730000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000358545a0000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062cedf1c00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000f41780000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034441490000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062fe4acc00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000877f800000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035245500000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062fe4acc00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000005572c0000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035a52580000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062cf049c00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000005a7b20000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034241540000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062fe4acc00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000001a3ad70000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034b4e430000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062fe4acc00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000007c0bdc0000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044c494e4b00000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062fe4acc00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000388c518000000000000000000000000000000000000000000000000000000000000000670726963657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004434f4d5000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062fe4acc00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000007bac28000000000000000000000000000000000000000000000000000000000000000670726963657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003554e490000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062fe4acc00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000001e23a0000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034752540000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000062fe4acc00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000035cd70000000000000000000000000000000000000000000000000000000000000000670726963657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003534e580000000000000000000000000000000000000000000000000000000000"],"signatures":["0x77a4cf03c91662f718714c5baeea91aa1e1f53c767ad057a34844bc93e8d56a33d1060d743182927afdbcad83fb1179fdbf5efbfbacf901e2c06fc1decc02fb8000000000000000000000000000000000000000000000000000000000000001c","0x286fdef55afae6a3bf74e380be7dc6d9e18dced4679d0ff22b38e7ca71a799cabe301a0ae7df6ccbe56a7657e84c037e2e8c3ec05a08c0a07cc8e1873fe88fc1000000000000000000000000000000000000000000000000000000000000001b","0x911a7ac54e1decd7907aad4a2f030840760a34bd4634466c6b17bac7b5867d6ece0946f194b08be9f479d6aa6a2a0c17120ad5f605b88e45a2766ca8f30a7597000000000000000000000000000000000000000000000000000000000000001b","0xad99f6564beb896c16f31145b5b3f176c7ac9ac1bba315d42ef252c460b7a0a9cd75d5e25e7d05d0616d4341a1f93c46168c487addf04414d7ab1d7b44ea4887000000000000000000000000000000000000000000000000000000000000001c","0x4b9858bc1430ad00bbafef428854c18b66d1eeee66b331612e9516e019017dffe635774395a2ec600d02a216539a90cc63e0ace63a3f17ba0300461acd56f4aa000000000000000000000000000000000000000000000000000000000000001c","0x721a7c5a1a6a2c98a43353f4914518aab1ea87037b36fb2f690c02cea93af88aa04032743b1542cf6989661139e87c82100369b32ef265cee92a0b23a493e8da000000000000000000000000000000000000000000000000000000000000001b","0x882bc7b0c91e58ca72e5689789463fad2772de1cd4b58c3e3cde43c4a3519fb3dd4e15256dffa71504048afa784bd288570aa98a7a660c54ef147256b302281e000000000000000000000000000000000000000000000000000000000000001c","0xf83fa6321f6535e4c37036f791d7154acdba98a6b57d911339af90e03bfef3104ea49fb5cb20050b6ee4d1c60a2c362a11c8538728b1b311d2316ceef822ea8b000000000000000000000000000000000000000000000000000000000000001b","0x894fc98bf0623bd6520df18b15cfa3e1e10569138f3b4cd516a2a53fc1185f3bdbfcff1ce1b746042e575195eb7556fb155b7b03a2ea72745fd2463d9c3e1b97000000000000000000000000000000000000000000000000000000000000001b","0x22208b890dc371a4d54ba9a3a4dbff0c56f86f025bfeddf6e8ca06ce8926bc3959c751a4e54f2a325960eda48cd0149038e13d104dc294b04e605a2ad48d1bd1000000000000000000000000000000000000000000000000000000000000001c","0xb716ebaf5733a272c7c7a48ab4d99caac0d691631c4b2b7c7017667e881ec7fbc467a4f5c0ca9cf85efd04a2203849c79d63555f217e7dc336db109971fd4f4d000000000000000000000000000000000000000000000000000000000000001c","0xed228d05e0bed656f481143a39f469d1a3f9f1673c51065f69b99201c22f8905ec4e90b5a344589d8037c7f1ae822eee741f5dbeabfdb7aa135d36c0f32bc4e8000000000000000000000000000000000000000000000000000000000000001c","0x854a3b0277e9f2f1fbc2ed57caff803bc91014d28e261d97262a723e8e608ae96ff868a38f1250185f6101c97c205b0dfabc32c8882fc71e99f4b1495eaf1866000000000000000000000000000000000000000000000000000000000000001c"],"prices":{"BTC":"23496.82","ETH":"1866.22","XTZ":"1.862","DAI":"0.9998","REP":"8.88","ZRX":"0.349996","BAT":"0.37061","KNC":"1.7189999999999999","LINK":"8.1295","COMP":"59.295","UNI":"8.105","GRT":"0.12345","SNX":"3.526"}}"""


@pytest.mark.integration
def test_settlement_with_openoracle(vega_service: VegaServiceNull):
vega = vega_service

participants_initial_deposit = 1e6

trade_price = random.randint(1850, 1870)
bid_ask_offset = random.randint(1, 11)

settlement_price = 1866.22
trade_size = random.randint(1, 3)

tt_oracle = vega_gov.vega_timestamp_termination_oracle(
timestamp=1660826549, pub_key="0xfCEAdAFab14d46e20144F48824d0C09B1a03F2BC"
)
price_oracle = vega_gov.openoracle_oracle(
pub_key="0xfCEAdAFab14d46e20144F48824d0C09B1a03F2BC",
instrument_name="ETH",
min_price=1,
min_timestamp=1660828749,
)

build_basic_market(
vega,
mint_amount=participants_initial_deposit,
initial_commitment=0.5 * participants_initial_deposit,
initial_price=trade_price,
initial_volume=trade_size,
initial_spread=bid_ask_offset * 2,
market_decimals=2,
settlement_price_decimals=6,
oracle_spec_for_settlement_price=price_oracle,
oracle_spec_for_trading_termination=tt_oracle,
)

pnl_long = trade_size * (settlement_price - trade_price)

tdai_id = vega.find_asset_id(symbol="tDAI")
market_id = vega.all_markets()[0].id

vega.wait_fn(1)
vega.wait_for_datanode_sync()

vega.forward("2022-08-18T17:29:10Z")
vega.wait_for_core_catchup()

vega.settle_market_openoracle(
settlement_wallet=TERMINATE_WALLET.name,
openoracle_data=PAYLOAD,
)
vega.wait_fn(10)

# On Jenkins this large forwarding can chew
# up the machine. Give it some time to catch up
for _ in range(3):
try:
vega.wait_for_total_catchup()
break
except:
continue

# check bond and margin for all
for wallet in wallets:
general, margin, bond = vega.party_account(
wallet_name=wallet.name, asset_id=tdai_id, market_id=market_id
)
assert margin == 0
assert bond == 0

# LP general
general, margin, bond = vega.party_account(
wallet_name=MM_WALLET.name, asset_id=tdai_id, market_id=market_id
)
assert general == participants_initial_deposit

# Trader 1 who went long
general, margin, bond = vega.party_account(
wallet_name=TRADER_1_WALLET.name, asset_id=tdai_id, market_id=market_id
)
assert general == (participants_initial_deposit + pnl_long)

# Trader 2 who went short
general, margin, bond = vega.party_account(
wallet_name=TRADER_2_WALLET.name, asset_id=tdai_id, market_id=market_id
)

assert general == (participants_initial_deposit - pnl_long)
13 changes: 13 additions & 0 deletions tests/integration/utils/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from collections import namedtuple
from typing import Optional

import pytest
from vega_sim.null_service import VegaServiceNull
import vega_sim.proto.vega.data_source_pb2 as data_source_protos

WalletConfig = namedtuple("WalletConfig", ["name", "passphrase"])

Expand Down Expand Up @@ -34,6 +36,14 @@ def build_basic_market(
initial_volume: float = 1,
initial_spread: float = 0.1,
initial_commitment: float = 100,
market_decimals: int = 5,
settlement_price_decimals: Optional[int] = None,
oracle_spec_for_settlement_price: Optional[
data_source_protos.DataSourceDefinition
] = None,
oracle_spec_for_trading_termination: Optional[
data_source_protos.DataSourceDefinition
] = None,
):
vega.wait_for_total_catchup()
for wallet in WALLETS:
Expand Down Expand Up @@ -73,6 +83,9 @@ def build_basic_market(
settlement_asset_id=asset_id,
termination_key=TERMINATE_WALLET.name,
market_decimals=5,
oracle_spec_for_settlement_price=oracle_spec_for_settlement_price,
oracle_spec_for_trading_termination=oracle_spec_for_trading_termination,
settlement_price_decimals=settlement_price_decimals,
)

market_id = vega.all_markets()[0].id
Expand Down
Loading