Skip to content

Commit

Permalink
[0.1.0-beta.6] Feature/gnosis mainnet support (#24)
Browse files Browse the repository at this point in the history
- Added default proximity-score functions pair ids
- Reworked gas price estimation
- Added support for Gnosis
- Reworked operation results returns
  • Loading branch information
u-hubar authored Mar 15, 2024
2 parents e0fd904 + fbbd7da commit d253339
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 129 deletions.
128 changes: 81 additions & 47 deletions dkg/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.

import json
import math
import re
from typing import Literal, Type
Expand All @@ -23,28 +24,46 @@
from web3 import Web3
from web3.constants import ADDRESS_ZERO, HASH_ZERO
from web3.exceptions import ContractLogicError

from dkg.constants import (DEFAULT_HASH_FUNCTION_ID, DEFAULT_SCORE_FUNCTION_ID,
PRIVATE_ASSERTION_PREDICATE,
PRIVATE_CURRENT_REPOSITORY,
PRIVATE_HISTORICAL_REPOSITORY)
from dkg.dataclasses import (KnowledgeAssetContentVisibility,
KnowledgeAssetEnumStates, NodeResponseDict)
from dkg.exceptions import (DatasetOutputFormatNotSupported,
InvalidKnowledgeAsset, InvalidStateOption,
InvalidTokenAmount, MissingKnowledgeAssetState,
OperationNotFinished)
from web3.types import TxReceipt

from dkg.constants import (
DEFAULT_HASH_FUNCTION_ID,
DEFAULT_PROXIMITY_SCORE_FUNCTIONS_PAIR_IDS,
PRIVATE_ASSERTION_PREDICATE,
PRIVATE_CURRENT_REPOSITORY,
PRIVATE_HISTORICAL_REPOSITORY,
)
from dkg.dataclasses import (
KnowledgeAssetContentVisibility,
KnowledgeAssetEnumStates,
NodeResponseDict,
)
from dkg.exceptions import (
DatasetOutputFormatNotSupported,
InvalidKnowledgeAsset,
InvalidStateOption,
InvalidTokenAmount,
MissingKnowledgeAssetState,
OperationNotFinished,
)
from dkg.manager import DefaultRequestManager
from dkg.method import Method
from dkg.module import Module
from dkg.types import JSONLD, UAL, Address, AgreementData, HexStr, Wei
from dkg.utils.blockchain_request import BlockchainRequest
from dkg.utils.decorators import retry
from dkg.utils.merkle import MerkleTree, hash_assertion_with_indexes
from dkg.utils.metadata import (generate_agreement_id,
generate_assertion_metadata, generate_keyword)
from dkg.utils.node_request import (NodeRequest, StoreTypes,
validate_operation_status)
from dkg.utils.metadata import (
generate_agreement_id,
generate_assertion_metadata,
generate_keyword,
)
from dkg.utils.node_request import (
NodeRequest,
OperationStatus,
StoreTypes,
validate_operation_status,
)
from dkg.utils.rdf import format_content, normalize_dataset
from dkg.utils.ual import format_ual, parse_ual

Expand Down Expand Up @@ -188,7 +207,7 @@ def create(
token_amount: Wei | None = None,
immutable: bool = False,
content_type: Literal["JSON-LD", "N-Quads"] = "JSON-LD",
) -> dict[str, HexStr | dict[str, str]]:
) -> dict[str, UAL | HexStr | dict[str, dict[str, str] | TxReceipt]]:
blockchain_id = self.manager.blockchain_provider.blockchain_id
assertions = format_content(content, content_type)

Expand Down Expand Up @@ -218,16 +237,20 @@ def create(
if is_allowance_increased := current_allowance < token_amount:
self.increase_allowance(token_amount)

result = {"publicAssertionId": public_assertion_id, "operation": {}}

try:
receipt = self._create(
receipt: TxReceipt = self._create(
{
"assertionId": Web3.to_bytes(hexstr=public_assertion_id),
"size": public_assertion_metadata["size"],
"triplesNumber": public_assertion_metadata["triples_number"],
"chunksNumber": public_assertion_metadata["chunks_number"],
"tokenAmount": token_amount,
"epochsNumber": epochs_number,
"scoreFunctionId": DEFAULT_SCORE_FUNCTION_ID,
"scoreFunctionId": DEFAULT_PROXIMITY_SCORE_FUNCTIONS_PAIR_IDS[
self.manager.blockchain_provider.environment
][blockchain_id],
"immutable_": immutable,
}
)
Expand All @@ -243,6 +266,11 @@ def create(
)
token_id = events[0].args["tokenId"]

result["UAL"] = format_ual(
blockchain_id, content_asset_storage_address, token_id
)
result["operation"]["mintKnowledgeAsset"] = json.loads(Web3.to_json(receipt))

assertions_list = [
{
"blockchain": blockchain_id,
Expand All @@ -269,9 +297,6 @@ def create(
}
)

operation_id = self._local_store(assertions_list)["operationId"]
self.get_operation_result(operation_id, "local-store")

operation_id = self._publish(
public_assertion_id,
assertions["public"],
Expand All @@ -282,25 +307,32 @@ def create(
)["operationId"]
operation_result = self.get_operation_result(operation_id, "publish")

return {
"UAL": format_ual(blockchain_id, content_asset_storage_address, token_id),
"publicAssertionId": public_assertion_id,
"operation": {
result["operation"]["publish"] = {
"operationId": operation_id,
"status": operation_result["status"],
}

if operation_result["status"] == OperationStatus.COMPLETED:
operation_id = self._local_store(assertions_list)["operationId"]
operation_result = self.get_operation_result(operation_id, "local-store")

result["operation"]["localStore"] = {
"operationId": operation_id,
"status": operation_result["status"],
},
}
}

return result

_transfer = Method(BlockchainRequest.transfer_asset)

def transfer(
self,
ual: UAL,
new_owner: Address,
) -> dict[str, UAL | Address | dict[str, str]]:
) -> dict[str, UAL | Address | TxReceipt]:
token_id = parse_ual(ual)["token_id"]

self._transfer(
receipt: TxReceipt = self._transfer(
self.manager.blockchain_provider.account,
new_owner,
token_id,
Expand All @@ -309,7 +341,7 @@ def transfer(
return {
"UAL": ual,
"owner": new_owner,
"operation": {"status": "COMPLETED"},
"operation": json.loads(Web3.to_json(receipt)),
}

_update = Method(NodeRequest.update)
Expand All @@ -325,7 +357,7 @@ def update(
content: dict[Literal["public", "private"], JSONLD],
token_amount: Wei | None = None,
content_type: Literal["JSON-LD", "N-Quads"] = "JSON-LD",
) -> dict[str, HexStr | dict[str, str]]:
) -> dict[str, UAL | HexStr | dict[str, str]]:
parsed_ual = parse_ual(ual)
blockchain_id, content_asset_storage_address, token_id = (
parsed_ual["blockchain"],
Expand Down Expand Up @@ -438,24 +470,24 @@ def update(

_cancel_update = Method(BlockchainRequest.cancel_asset_state_update)

def cancel_update(self, ual: UAL) -> dict[str, UAL | dict[str, str]]:
def cancel_update(self, ual: UAL) -> dict[str, UAL | TxReceipt]:
token_id = parse_ual(ual)["token_id"]

self._cancel_update(token_id)
receipt: TxReceipt = self._cancel_update(token_id)

return {
"UAL": ual,
"operation": {"status": "COMPLETED"},
"operation": json.loads(Web3.to_json(receipt)),
}

_burn_asset = Method(BlockchainRequest.burn_asset)

def burn(self, ual: UAL) -> dict[str, UAL | dict[str, str]]:
def burn(self, ual: UAL) -> dict[str, UAL | TxReceipt]:
token_id = parse_ual(ual)["token_id"]

self._burn_asset(token_id)
receipt: TxReceipt = self._burn_asset(token_id)

return {"UAL": ual, "operation": {"status": "COMPLETED"}}
return {"UAL": ual, "operation": json.loads(Web3.to_json(receipt))}

_get_assertion_ids = Method(BlockchainRequest.get_assertion_ids)
_get_latest_assertion_id = Method(BlockchainRequest.get_latest_assertion_id)
Expand All @@ -471,7 +503,7 @@ def get(
content_visibility: str = KnowledgeAssetContentVisibility.ALL.value,
output_format: Literal["JSON-LD", "N-Quads"] = "JSON-LD",
validate: bool = True,
) -> dict[str, HexStr | list[JSONLD] | dict[str, str]]:
) -> dict[str, UAL | HexStr | list[JSONLD] | dict[str, str]]:
state = (
state.upper()
if (isinstance(state, str) and not re.match(r"^0x[a-fA-F0-9]{64}$", state))
Expand Down Expand Up @@ -697,7 +729,7 @@ def extend_storing_period(
ual: UAL,
additional_epochs: int,
token_amount: Wei | None = None,
) -> dict[str, UAL | dict[str, str]]:
) -> dict[str, UAL | TxReceipt]:
parsed_ual = parse_ual(ual)
blockchain_id, content_asset_storage_address, token_id = (
parsed_ual["blockchain"],
Expand All @@ -722,11 +754,13 @@ def extend_storing_period(
)["bidSuggestion"]
)

self._extend_storing_period(token_id, additional_epochs, token_amount)
receipt: TxReceipt = self._extend_storing_period(
token_id, additional_epochs, token_amount
)

return {
"UAL": ual,
"operation": {"status": "COMPLETED"},
"operation": json.loads(Web3.to_json(receipt)),
}

_get_assertion_size = Method(BlockchainRequest.get_assertion_size)
Expand All @@ -736,7 +770,7 @@ def add_tokens(
self,
ual: UAL,
token_amount: Wei | None = None,
) -> dict[str, UAL | dict[str, str]]:
) -> dict[str, UAL | TxReceipt]:
parsed_ual = parse_ual(ual)
blockchain_id, content_asset_storage_address, token_id = (
parsed_ual["blockchain"],
Expand Down Expand Up @@ -782,11 +816,11 @@ def add_tokens(
"more tokens!"
)

self._add_tokens(token_id, token_amount)
receipt: TxReceipt = self._add_tokens(token_id, token_amount)

return {
"UAL": ual,
"operation": {"status": "COMPLETED"},
"operation": json.loads(Web3.to_json(receipt)),
}

_add_update_tokens = Method(BlockchainRequest.increase_asset_update_token_amount)
Expand All @@ -795,7 +829,7 @@ def add_update_tokens(
self,
ual: UAL,
token_amount: Wei | None = None,
) -> dict[str, UAL | dict[str, str]]:
) -> dict[str, UAL | TxReceipt]:
parsed_ual = parse_ual(ual)
blockchain_id, content_asset_storage_address, token_id = (
parsed_ual["blockchain"],
Expand Down Expand Up @@ -839,11 +873,11 @@ def add_update_tokens(
"more update tokens!"
)

self._add_update_tokens(token_id, token_amount)
receipt: TxReceipt = self._add_update_tokens(token_id, token_amount)

return {
"UAL": ual,
"operation": {"status": "COMPLETED"},
"operation": json.loads(Web3.to_json(receipt)),
}

def get_owner(self, ual: UAL) -> Address:
Expand Down
33 changes: 31 additions & 2 deletions dkg/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,42 @@
"hub": "0x5fA7916c48Fe6D5F1738d12Ad234b78c90B4cAdA",
"rpc": "https://astrosat-parachain-rpc.origin-trail.network",
},
"gnosis:100": {
"hub": "0xbEF14fc04F870c2dD65c13Df4faB6ba01A9c746b",
"rpc": "https://rpc.gnosischain.com/",
"gas_price_oracle": [
"https://api.gnosisscan.io/api?module=proxy&action=eth_gasPrice",
"https://blockscout.com/xdai/mainnet/api/v1/gas-price-oracle",
],
}
},
}

DEFAULT_GAS_PRICE_GWEI = 100
DEFAULT_GAS_PRICE_GWEI = {
"gnosis": 20,
"otp": 1,
}

DEFAULT_HASH_FUNCTION_ID = 1
DEFAULT_SCORE_FUNCTION_ID = 1
DEFAULT_PROXIMITY_SCORE_FUNCTIONS_PAIR_IDS = {
"development": {
"hardhat1:31337": 1,
"hardhat2:31337": 2,
"otp:2043": 1
},
"devnet": {
"otp:2160": 1,
"gnosis:10200": 2,
},
"testnet": {
"otp:20430": 1,
"gnosis:10200": 2,
},
"mainnet": {
"otp:2043": 1,
"gnosis:100": 2,
},
}

PRIVATE_HISTORICAL_REPOSITORY = "privateHistory"
PRIVATE_CURRENT_REPOSITORY = "privateCurrent"
Loading

0 comments on commit d253339

Please sign in to comment.