Skip to content

Commit

Permalink
[IMPROVEMENT] Bid Suggestion Range Options (#26)
Browse files Browse the repository at this point in the history
- Added support for the bid suggestion range options
- Fixed gas price estimation in development enviornment
- Improved Enum data classes
  • Loading branch information
u-hubar authored Jun 19, 2024
2 parents 1e03317 + b8ed8de commit 0e967c6
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 107 deletions.
22 changes: 14 additions & 8 deletions dkg/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
PRIVATE_HISTORICAL_REPOSITORY,
)
from dkg.dataclasses import (
BidSuggestionRange,
KnowledgeAssetContentVisibility,
KnowledgeAssetEnumStates,
NodeResponseDict,
Expand Down Expand Up @@ -230,6 +231,7 @@ def create(
content_asset_storage_address,
public_assertion_id,
DEFAULT_HASH_FUNCTION_ID,
token_amount or BidSuggestionRange.LOW,
)["bidSuggestion"]
)

Expand Down Expand Up @@ -396,6 +398,7 @@ def update(
content_asset_storage_address,
public_assertion_id,
DEFAULT_HASH_FUNCTION_ID,
token_amount or BidSuggestionRange.LOW,
)["bidSuggestion"]
)

Expand Down Expand Up @@ -499,8 +502,8 @@ def burn(self, ual: UAL) -> dict[str, UAL | TxReceipt]:
def get(
self,
ual: UAL,
state: str | HexStr | int = KnowledgeAssetEnumStates.LATEST.value,
content_visibility: str = KnowledgeAssetContentVisibility.ALL.value,
state: str | HexStr | int = KnowledgeAssetEnumStates.LATEST,
content_visibility: str = KnowledgeAssetContentVisibility.ALL,
output_format: Literal["JSON-LD", "N-Quads"] = "JSON-LD",
validate: bool = True,
) -> dict[str, UAL | HexStr | list[JSONLD] | dict[str, str]]:
Expand Down Expand Up @@ -528,10 +531,10 @@ def handle_latest_finalized_state(token_id: int) -> tuple[HexStr, bool]:
is_state_finalized = False

match state:
case KnowledgeAssetEnumStates.LATEST.value:
case KnowledgeAssetEnumStates.LATEST:
public_assertion_id, is_state_finalized = handle_latest_state(token_id)

case KnowledgeAssetEnumStates.LATEST_FINALIZED.value:
case KnowledgeAssetEnumStates.LATEST_FINALIZED:
public_assertion_id, is_state_finalized = handle_latest_finalized_state(
token_id
)
Expand Down Expand Up @@ -592,7 +595,7 @@ def handle_latest_finalized_state(token_id: int) -> tuple[HexStr, bool]:
)

result = {"operation": {}}
if content_visibility != KnowledgeAssetContentVisibility.PRIVATE.value:
if content_visibility != KnowledgeAssetContentVisibility.PRIVATE:
formatted_public_assertion = public_assertion

match output_format:
Expand All @@ -609,7 +612,7 @@ def handle_latest_finalized_state(token_id: int) -> tuple[HexStr, bool]:
f"{output_format} isn't supported!"
)

if content_visibility == KnowledgeAssetContentVisibility.PUBLIC.value:
if content_visibility == KnowledgeAssetContentVisibility.PUBLIC:
result = {
**result,
"asertion": formatted_public_assertion,
Expand All @@ -626,7 +629,7 @@ def handle_latest_finalized_state(token_id: int) -> tuple[HexStr, bool]:
"status": get_public_operation_result["status"],
}

if content_visibility != KnowledgeAssetContentVisibility.PUBLIC.value:
if content_visibility != KnowledgeAssetContentVisibility.PUBLIC:
private_assertion_link_triples = list(
filter(
lambda element: PRIVATE_ASSERTION_PREDICATE in element,
Expand Down Expand Up @@ -702,7 +705,7 @@ def handle_latest_finalized_state(token_id: int) -> tuple[HexStr, bool]:
f"{output_format} isn't supported!"
)

if content_visibility == KnowledgeAssetContentVisibility.PRIVATE:
if content_visibility == KnowledgeAssetContentVisibility:
result = {
**result,
"assertion": formatted_private_assertion,
Expand Down Expand Up @@ -751,6 +754,7 @@ def extend_storing_period(
content_asset_storage_address,
latest_finalized_state,
DEFAULT_HASH_FUNCTION_ID,
token_amount or BidSuggestionRange.LOW,
)["bidSuggestion"]
)

Expand Down Expand Up @@ -806,6 +810,7 @@ def add_tokens(
content_asset_storage_address,
latest_finalized_state,
DEFAULT_HASH_FUNCTION_ID,
token_amount or BidSuggestionRange.LOW,
)["bidSuggestion"]
) - sum(agreement_data.tokensInfo)

Expand Down Expand Up @@ -863,6 +868,7 @@ def add_update_tokens(
content_asset_storage_address,
unfinalized_state,
DEFAULT_HASH_FUNCTION_ID,
token_amount or BidSuggestionRange.LOW,
)["bidSuggestion"]
) - sum(agreement_data.tokensInfo)

Expand Down
24 changes: 16 additions & 8 deletions dkg/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
# specific language governing permissions and limitations
# under the License.

from enum import Enum
from enum import auto, Enum

import pandas as pd

from dkg.types import AutoStrEnum, AutoStrEnumUpperCase


class BlockchainResponseDict(dict):
pass
Expand All @@ -34,12 +36,18 @@ def to_dataframe(self) -> pd.DataFrame:
return pd.DataFrame(self)


class KnowledgeAssetEnumStates(Enum):
LATEST = "LATEST"
LATEST_FINALIZED = "LATEST_FINALIZED"
class BidSuggestionRange(AutoStrEnum):
LOW = auto()
MEDIUM = auto()
HIGH = auto()
ALL = auto()

class KnowledgeAssetEnumStates(AutoStrEnumUpperCase):
LATEST = auto()
LATEST_FINALIZED = auto()


class KnowledgeAssetContentVisibility(Enum):
ALL = "ALL"
PUBLIC = "PUBLIC"
PRIVATE = "PRIVATE"
class KnowledgeAssetContentVisibility(AutoStrEnumUpperCase):
ALL = auto()
PUBLIC = auto()
PRIVATE = auto()
30 changes: 20 additions & 10 deletions dkg/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.

from dkg.constants import DEFAULT_HASH_FUNCTION_ID
from dkg.dataclasses import BidSuggestionRange
from dkg.manager import DefaultRequestManager
from dkg.method import Method
from dkg.module import Module
Expand All @@ -33,19 +34,28 @@ def __init__(self, manager: DefaultRequestManager):
_get_bid_suggestion = Method(NodeRequest.bid_suggestion)

def get_bid_suggestion(
self, public_assertion_id: DataHexStr, size_in_bytes: int, epochs_number: int,
self,
public_assertion_id: DataHexStr,
size_in_bytes: int,
epochs_number: int,
range: BidSuggestionRange = BidSuggestionRange.LOW.value,
) -> int:
content_asset_storage_address = self._get_asset_storage_address(
"ContentAssetStorage"
)

return int(
self._get_bid_suggestion(
self.manager.blockchain_provider.blockchain_id,
epochs_number,
size_in_bytes,
content_asset_storage_address,
public_assertion_id,
DEFAULT_HASH_FUNCTION_ID,
)["bidSuggestion"]
response = self._get_bid_suggestion(
self.manager.blockchain_provider.blockchain_id,
epochs_number,
size_in_bytes,
content_asset_storage_address,
public_assertion_id,
DEFAULT_HASH_FUNCTION_ID,
range,
)

return (
int(response["bidSuggestion"])
if range != BidSuggestionRange.ALL
else response
)
3 changes: 3 additions & 0 deletions dkg/providers/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ def set_account(self, private_key: DataHexStr):
self.w3.eth.default_account = self.account.address

def _get_network_gas_price(self) -> Wei | None:
if self.environment == "development":
return None

blockchain_name, _ = self.blockchain_id.split(":")

default_gas_price = self.w3.to_wei(
Expand Down
1 change: 1 addition & 0 deletions dkg/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .general import AutoStrEnum, AutoStrEnumUpperCase # NOQA: F401
from .blockchain import (ABI, ABIElement, ABIError, ABIEvent, # NOQA: F401
ABIFunction, ABIParameter, AgreementData, Environment)
from .dkg_node import UAL # NOQA: F401
Expand Down
29 changes: 29 additions & 0 deletions dkg/types/general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from enum import Enum

class AutoStrEnum(str, Enum):
@staticmethod
def _generate_next_value_(name: str, start: int, count: int, last_values: list) -> str:
return name.lower()


class AutoStrEnumUpperCase(str, Enum):
@staticmethod
def _generate_next_value_(name: str, start: int, count: int, last_values: list) -> str:
return name
Loading

0 comments on commit 0e967c6

Please sign in to comment.