Skip to content

Commit

Permalink
feat: integrate referral program into market-sim and fuzz tests (#514)
Browse files Browse the repository at this point in the history
* chore: bump vega version

* feat: integrate referral api

* feat: add referral api tests

* chore: update genesis

* feat: add referral program agents

* feat: integrate referrals into fuzz test

* chore: address review feedback
  • Loading branch information
cdummett authored and daniel1302 committed Oct 5, 2023
1 parent c436ef8 commit af66dbc
Show file tree
Hide file tree
Showing 22 changed files with 2,072 additions and 1,044 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VEGA_SIM_VEGA_TAG="cd631fba15cae959e7a13b9dbd39ece359d83fc0"
VEGA_SIM_VEGA_TAG="086658a26128ce92a0a5aadfcec8dce484b20f3d"
VEGA_SIM_CONSOLE_TAG=develop
VEGA_DEFAULT_KEY_NAME='Key 1'
VEGA_SIM_NETWORKS_INTERNAL_TAG=main
Expand Down
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pipeline {
disableConcurrentBuilds(abortPrevious: true)
}
parameters {
string( name: 'VEGA_VERSION', defaultValue: 'cd631fba15cae959e7a13b9dbd39ece359d83fc0',
string( name: 'VEGA_VERSION', defaultValue: '086658a26128ce92a0a5aadfcec8dce484b20f3d',
description: 'Git branch, tag or hash of the vegaprotocol/vega repository')
string( name: 'VEGACAPSULE_VERSION', defaultValue: 'main',
description: 'Git branch, tag or hash of the vegaprotocol/vegacapsule repository')
Expand Down
93 changes: 93 additions & 0 deletions tests/integration/test_trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,3 +1523,96 @@ def test_estimated_liquidation_price_AC001003(vega_service: VegaServiceNull):
)
assert closeout_price <= estimate_liquidation_price_2.best_case.open_volume_only
assert closeout_price >= estimate_liquidation_price_2.worst_case.open_volume_only


@pytest.mark.integration
def test_referral_sets(vega_service_with_market: VegaServiceNull):
vega = vega_service_with_market

create_and_faucet_wallet(vega=vega, wallet=PARTY_A)
vega.wait_for_total_catchup()
create_and_faucet_wallet(vega=vega, wallet=PARTY_B)
vega.wait_for_total_catchup()
referrer_id = vega.wallet.public_key(name=PARTY_A.name)
referee_id = vega.wallet.public_key(name=PARTY_B.name)

vega.create_referral_set(key_name=PARTY_A.name)
vega.wait_fn(1)
vega.wait_for_total_catchup()

referral_set_id = list(vega.list_referral_sets().keys())[0]
vega.apply_referral_code(key_name=PARTY_B.name, id=referral_set_id)
vega.wait_fn(1)
vega.wait_for_total_catchup()

# Check we can request a referral set by referral set id
referral_set = vega.list_referral_sets(referral_set_id=referral_set_id)
assert referral_set[referral_set_id].id == referral_set_id
assert referral_set[referral_set_id].referrer == referrer_id

# Check we can request a referral set by referrer
referral_set = vega.list_referral_sets(referrer=referrer_id)
assert referral_set[referral_set_id].id == referral_set_id
assert referral_set[referral_set_id].referrer == referrer_id

# Check we can request a referral set by referee
referral_set = vega.list_referral_sets(referee=referee_id)
assert referral_set[referral_set_id].id == referral_set_id
assert referral_set[referral_set_id].referrer == referrer_id

# Check we can request all referral set referees
referees = vega.list_referral_set_referees()
assert referees[referral_set_id][referee_id].referee == referee_id
assert referees[referral_set_id][referee_id].referral_set_id == referral_set_id

# Check we can request referral set referees by referral set id
referees = vega.list_referral_set_referees(referral_set_id=referral_set_id)
assert referees[referral_set_id][referee_id].referee == referee_id
assert referees[referral_set_id][referee_id].referral_set_id == referral_set_id

# Check we can request referral set referees by referrer id
referees = vega.list_referral_set_referees(referrer=referrer_id)
assert referees[referral_set_id][referee_id].referee == referee_id
assert referees[referral_set_id][referee_id].referral_set_id == referral_set_id

# Check we can request referral set referees by referee id
referees = vega.list_referral_set_referees(referee=referee_id)
assert referees[referral_set_id][referee_id].referee == referee_id
assert referees[referral_set_id][referee_id].referral_set_id == referral_set_id


@pytest.mark.integration
def test_referral_program(vega_service_with_market: VegaServiceNull):
vega = vega_service_with_market
vega.update_referral_program(
proposal_key=MM_WALLET.name,
benefit_tiers=[
{
"minimum_running_notional_taker_volume": 10000,
"minimum_epochs": 1,
"referral_reward_factor": 0.01,
"referral_discount_factor": 0.01,
},
{
"minimum_running_notional_taker_volume": 20000,
"minimum_epochs": 2,
"referral_reward_factor": 0.02,
"referral_discount_factor": 0.02,
},
{
"minimum_running_notional_taker_volume": 30000,
"minimum_epochs": 3,
"referral_reward_factor": 0.03,
"referral_discount_factor": 0.03,
},
],
staking_tiers=[
{"minimum_staked_tokens": 100, "referral_reward_multiplier": 1},
{"minimum_staked_tokens": 1000, "referral_reward_multiplier": 2},
{"minimum_staked_tokens": 10000, "referral_reward_multiplier": 2},
],
window_length=1,
)
next_epoch(vega=vega)
referral_program = vega.get_current_referral_program()
assert referral_program is not None
91 changes: 91 additions & 0 deletions tests/vega_sim/api/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
MarginEstimate,
LiquidationEstimate,
LiquidationPrice,
ReferralSet,
ReferralSetReferee,
get_asset_decimals,
find_asset_id,
get_trades,
Expand All @@ -33,6 +35,8 @@
list_transfers,
list_ledger_entries,
estimate_position,
list_referral_sets,
list_referral_set_referees,
)
from vega_sim.grpc.client import (
VegaTradingDataClientV2,
Expand Down Expand Up @@ -857,3 +861,90 @@ def EstimatePosition(self, request, context):

assert margin == expected_margin
assert liquidation == expected_liquidation


def test_list_referral_sets(trading_data_v2_servicer_and_port):
def ListReferralSets(self, request, context):
return data_node_protos_v2.trading_data.ListReferralSetsResponse(
referral_sets=data_node_protos_v2.trading_data.ReferralSetConnection(
page_info=data_node_protos_v2.trading_data.PageInfo(
has_next_page=False,
has_previous_page=False,
start_cursor="",
end_cursor="",
),
edges=[
data_node_protos_v2.trading_data.ReferralSetEdge(
cursor="cursor",
node=data_node_protos_v2.trading_data.ReferralSet(
id=request.referral_set_id,
referrer=request.referrer,
created_at=123456789,
updated_at=123456789,
),
),
],
)
)

server, port, mock_servicer = trading_data_v2_servicer_and_port
mock_servicer.ListReferralSets = ListReferralSets

add_TradingDataServiceServicer_v2_to_server(mock_servicer(), server)

data_client = VegaTradingDataClientV2(f"localhost:{port}")

assert list_referral_sets(data_client=data_client, referral_set_id="id") == {
"id": ReferralSet(
id="id", referrer="", created_at=123456789, updated_at=123456789
)
}
assert list_referral_sets(data_client=data_client, referral_set_id="id") == {
"id": ReferralSet(
id="id", referrer="", created_at=123456789, updated_at=123456789
)
}


def test_list_referral_set_referees(trading_data_v2_servicer_and_port):
def ListReferralSetReferees(self, request, context):
return data_node_protos_v2.trading_data.ListReferralSetRefereesResponse(
referral_set_referees=data_node_protos_v2.trading_data.ReferralSetRefereeConnection(
page_info=data_node_protos_v2.trading_data.PageInfo(
has_next_page=False,
has_previous_page=False,
start_cursor="",
end_cursor="",
),
edges=[
data_node_protos_v2.trading_data.ReferralSetRefereeEdge(
cursor="cursor",
node=data_node_protos_v2.trading_data.ReferralSetReferee(
referral_set_id=request.referral_set_id,
referee=request.referee,
joined_at=123456789,
at_epoch=1,
),
),
],
)
)

server, port, mock_servicer = trading_data_v2_servicer_and_port
mock_servicer.ListReferralSetReferees = ListReferralSetReferees

add_TradingDataServiceServicer_v2_to_server(mock_servicer(), server)

data_client = VegaTradingDataClientV2(f"localhost:{port}")
assert list_referral_set_referees(
data_client=data_client, referral_set_id="id"
) == {
"id": {
"": ReferralSetReferee(
referral_set_id="id",
referee="",
joined_at=123456789,
at_epoch=1,
)
}
}
100 changes: 100 additions & 0 deletions tests/vega_sim/api/test_data_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
list_transfers,
list_ledger_entries,
estimate_position,
list_referral_sets,
list_referral_set_referees,
)

from vega_sim.proto.data_node.api.v2.trading_data_pb2_grpc import (
Expand Down Expand Up @@ -942,3 +944,101 @@ def EstimatePosition(self, request, context):
)
assert margin == expected_margin
assert liquidation == expected_liquidation


def test_list_referral_sets(trading_data_v2_servicer_and_port):
def ListReferralSets(self, request, context):
return data_node_protos_v2.trading_data.ListReferralSetsResponse(
referral_sets=data_node_protos_v2.trading_data.ReferralSetConnection(
page_info=data_node_protos_v2.trading_data.PageInfo(
has_next_page=False,
has_previous_page=False,
start_cursor="",
end_cursor="",
),
edges=[
data_node_protos_v2.trading_data.ReferralSetEdge(
cursor="cursor",
node=data_node_protos_v2.trading_data.ReferralSet(
id=request.referral_set_id,
referrer=request.referrer,
created_at=123456789,
updated_at=123456789,
),
),
],
)
)

server, port, mock_servicer = trading_data_v2_servicer_and_port
mock_servicer.ListReferralSets = ListReferralSets

add_TradingDataServiceServicer_v2_to_server(mock_servicer(), server)

data_client = VegaTradingDataClientV2(f"localhost:{port}")
assert list_referral_sets(data_client=data_client, referral_set_id="id") == [
data_node_protos_v2.trading_data.ReferralSet(
id="id",
referrer="",
created_at=123456789,
updated_at=123456789,
)
]
assert list_referral_sets(data_client=data_client, referrer="referrer") == [
data_node_protos_v2.trading_data.ReferralSet(
id="",
referrer="referrer",
created_at=123456789,
updated_at=123456789,
)
]


def test_list_referral_set_referees(trading_data_v2_servicer_and_port):
def ListReferralSetReferees(self, request, context):
return data_node_protos_v2.trading_data.ListReferralSetRefereesResponse(
referral_set_referees=data_node_protos_v2.trading_data.ReferralSetRefereeConnection(
page_info=data_node_protos_v2.trading_data.PageInfo(
has_next_page=False,
has_previous_page=False,
start_cursor="",
end_cursor="",
),
edges=[
data_node_protos_v2.trading_data.ReferralSetRefereeEdge(
cursor="cursor",
node=data_node_protos_v2.trading_data.ReferralSetReferee(
referral_set_id=request.referral_set_id,
referee=request.referee,
joined_at=123456789,
at_epoch=1,
),
),
],
)
)

server, port, mock_servicer = trading_data_v2_servicer_and_port
mock_servicer.ListReferralSetReferees = ListReferralSetReferees

add_TradingDataServiceServicer_v2_to_server(mock_servicer(), server)

data_client = VegaTradingDataClientV2(f"localhost:{port}")
assert list_referral_set_referees(
data_client=data_client, referral_set_id="id"
) == [
data_node_protos_v2.trading_data.ReferralSetReferee(
referral_set_id="id",
referee="",
joined_at=123456789,
at_epoch=1,
)
]
assert list_referral_set_referees(data_client=data_client, referee="referee") == [
data_node_protos_v2.trading_data.ReferralSetReferee(
referral_set_id="",
referee="referee",
joined_at=123456789,
at_epoch=1,
)
]
Loading

0 comments on commit af66dbc

Please sign in to comment.