Skip to content

Commit

Permalink
feat: add vdpr api methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cdummett committed Sep 29, 2023
1 parent 95f3b4c commit a809aa5
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
2 changes: 2 additions & 0 deletions tests/integration/test_trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1660,10 +1660,12 @@ def test_volume_discount_program(vega_service_with_market: VegaServiceNull):
non_discounted_trades = vega.get_trades(
market_id=market_id, order_id=non_discounted_order_id
)
assert vega.get_current_volume_discount_program() is not None
assert non_discounted_trades[0].seller_fee.maker_fee_volume_discount == 0
assert non_discounted_trades[0].seller_fee.liquidity_fee_volume_discount == 0
assert non_discounted_trades[0].seller_fee.infrastructure_fee_volume_discount == 0
next_epoch(vega=vega)
assert vega.get_volume_discount_stats(key_name=PARTY_B.name) is not None
vega.submit_order(
trading_key=PARTY_A.name,
market_id=market_id,
Expand Down
83 changes: 82 additions & 1 deletion vega_sim/api/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ class ReferralSetReferee:
at_epoch: int


@dataclass(frozen=True)
class VolumeDiscountStats:
at_epoch: int
party_id: str
discount_factor: float
running_volume: float


@dataclass(frozen=True)
class BenefitTier:
minimum_running_notional_taker_volume: float
Expand All @@ -301,6 +309,22 @@ class ReferralProgram:
ended_at: int


@dataclass(frozen=True)
class VolumeBenefitTier:
minimum_running_notional_taker_volume: float
volume_discount_factor: float


@dataclass(frozen=True)
class VolumeDiscountProgram:
version: int
id: str
benefit_tiers: List[VolumeBenefitTier]
end_of_program_timestamp: str
window_length: int
ended_at: int


def _ledger_entry_from_proto(
ledger_entry,
asset_decimals: int,
Expand Down Expand Up @@ -820,6 +844,40 @@ def _referral_program_from_proto(referral_program) -> ReferralProgram:
)


def _volume_benefit_tier_from_proto(volume_benefit_tier) -> VolumeBenefitTier:
return VolumeBenefitTier(
minimum_running_notional_taker_volume=float(
volume_benefit_tier.minimum_running_notional_taker_volume
),
volume_discount_factor=float(volume_benefit_tier.volume_discount_factor),
)


def _volume_discount_program_from_proto(
volume_discount_program,
) -> VolumeDiscountProgram:
return VolumeDiscountProgram(
version=int(volume_discount_program.version),
id=str(volume_discount_program.id),
benefit_tiers=[
_volume_benefit_tier_from_proto(volume_benefit_tier)
for volume_benefit_tier in volume_discount_program.benefit_tiers
],
end_of_program_timestamp=int(volume_discount_program.end_of_program_timestamp),
window_length=int(volume_discount_program.window_length),
ended_at=int(volume_discount_program.ended_at),
)


def _volume_discount_stats_from_proto(volume_discount_stats) -> VolumeDiscountStats:
return VolumeDiscountStats(
at_epoch=int(volume_discount_stats.at_epoch),
party_id=str(volume_discount_stats.party_id),
discount_factor=float(volume_discount_stats.discount_factor),
running_volume=float(volume_discount_stats.running_volume),
)


def list_accounts(
data_client: vac.VegaTradingDataClientV2,
pub_key: Optional[str] = None,
Expand Down Expand Up @@ -1860,6 +1918,29 @@ def list_referral_set_referees(
return referral_set_referees


def get_current_referral_program(data_client: vac.trading_data_grpc_v2):
def get_current_referral_program(
data_client: vac.trading_data_grpc_v2,
) -> ReferralProgram:
response = data_raw.get_current_referral_program(data_client=data_client)
return _referral_program_from_proto(referral_program=response)


def get_current_volume_discount_program(
data_client: vac.trading_data_grpc_v2,
) -> VolumeDiscountProgram:
response = data_raw.get_current_volume_discount_program(data_client=data_client)
return _volume_discount_program_from_proto(volume_discount_program=response)


def get_volume_discount_stats(
data_client: vac.trading_data_grpc_v2,
at_epoch: Optional[int] = None,
party_id: Optional[str] = None,
) -> List[VolumeDiscountStats]:
response = data_raw.get_volume_discount_stats(
data_client=data_client, at_epoch=at_epoch, party_id=party_id
)
return [
_volume_discount_stats_from_proto(volume_discount_stats=volume_discount_stats)
for volume_discount_stats in response
]
27 changes: 27 additions & 0 deletions vega_sim/api/data_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,3 +733,30 @@ def get_current_referral_program(data_client: vac.trading_data_grpc_v2):
return data_client.GetCurrentReferralProgram(
data_node_protos_v2.trading_data.GetCurrentReferralProgramRequest()
).current_referral_program


@_retry(3)
def get_current_volume_discount_program(
data_client: vac.trading_data_grpc_v2.TradingDataServiceStub,
):
return data_client.GetCurrentVolumeDiscountProgram(
data_node_protos_v2.trading_data.GetCurrentVolumeDiscountProgramRequest()
).current_volume_discount_program


@_retry(3)
def get_volume_discount_stats(
data_client: vac.trading_data_grpc_v2,
at_epoch: Optional[int] = None,
party_id: Optional[str] = None,
) -> List[data_node_protos_v2.trading_data.ReferralSet]:
base_request = data_node_protos_v2.trading_data.GetVolumeDiscountStatsRequest()
if at_epoch is not None:
setattr(base_request, "at_epoch", at_epoch)
if party_id is not None:
setattr(base_request, "party_id", party_id)
return unroll_v2_pagination(
base_request=base_request,
request_func=lambda x: data_client.GetVolumeDiscountStats(x).stats,
extraction_func=lambda res: [i.node for i in res.edges],
)
19 changes: 19 additions & 0 deletions vega_sim/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2807,3 +2807,22 @@ def update_volume_discount_program(
self.wait_fn(int(time_to_enactment / self.seconds_per_block) + 1)
self.wait_for_thread_catchup()
return proposal_id

def get_current_volume_discount_program(
self,
) -> data.VolumeDiscountProgram:
return data.get_current_volume_discount_program(
data_client=self.trading_data_client_v2
)

def get_volume_discount_stats(
self,
at_epoch: Optional[str] = None,
key_name: Optional[str] = None,
wallet_name: Optional[str] = None,
) -> List[data.VolumeDiscountStats]:
return data.get_volume_discount_stats(
data_client=self.trading_data_client_v2,
at_epoch=at_epoch,
party_id=self.wallet.public_key(name=key_name, wallet_name=wallet_name),
)

0 comments on commit a809aa5

Please sign in to comment.