Skip to content
Merged
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
17 changes: 6 additions & 11 deletions hathor/indexes/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,10 @@ def handle_contract_execution(self, tx: BaseTransaction) -> None:
Update indexes according to a Nano Contract execution.
Must be called only once for each time a contract is executed.
"""
from hathor.conf.settings import HATHOR_TOKEN_UID
from hathor.nanocontracts.runner.types import (
NCIndexUpdateRecord,
SyscallCreateContractRecord,
SyscallUpdateTokensRecord,
SyscallUpdateTokenRecord,
UpdateAuthoritiesRecord,
)
from hathor.nanocontracts.types import ContractId
Expand Down Expand Up @@ -260,7 +259,7 @@ def handle_contract_execution(self, tx: BaseTransaction) -> None:
if self.blueprint_history:
self.blueprint_history.add_single_key(blueprint_id, tx)

case SyscallUpdateTokensRecord():
case SyscallUpdateTokenRecord():
# Minted/melted tokens are added/removed to/from the tokens index,
# and the respective destroyed/created HTR too.
if self.tokens:
Expand All @@ -280,8 +279,7 @@ def handle_contract_execution(self, tx: BaseTransaction) -> None:
version=record.token_version
)

self.tokens.add_to_total(record.token_uid, record.token_amount)
self.tokens.add_to_total(HATHOR_TOKEN_UID, record.htr_amount)
self.tokens.add_to_total(record.token_uid, record.amount)

case UpdateAuthoritiesRecord():
if self.tokens:
Expand All @@ -295,11 +293,10 @@ def handle_contract_unexecution(self, tx: BaseTransaction) -> None:
Update indexes according to a Nano Contract unexecution, which happens when a reorg unconfirms a nano tx.
Must be called only once for each time a contract is unexecuted.
"""
from hathor.conf.settings import HATHOR_TOKEN_UID
from hathor.nanocontracts.runner.types import (
NCIndexUpdateRecord,
SyscallCreateContractRecord,
SyscallUpdateTokensRecord,
SyscallUpdateTokenRecord,
UpdateAuthoritiesRecord,
)
from hathor.nanocontracts.types import NC_INITIALIZE_METHOD, ContractId
Expand Down Expand Up @@ -341,11 +338,9 @@ def handle_contract_unexecution(self, tx: BaseTransaction) -> None:
if self.blueprint_history:
self.blueprint_history.remove_single_key(blueprint_id, tx)

case SyscallUpdateTokensRecord():
# Undo the tokens update.
case SyscallUpdateTokenRecord():
if self.tokens:
self.tokens.add_to_total(record.token_uid, -record.token_amount)
self.tokens.add_to_total(HATHOR_TOKEN_UID, -record.htr_amount)
self.tokens.add_to_total(record.token_uid, -record.amount)

from hathor.nanocontracts.runner.types import IndexUpdateRecordType
if record.type is IndexUpdateRecordType.CREATE_TOKEN:
Expand Down
27 changes: 21 additions & 6 deletions hathor/nanocontracts/blueprint_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from typing import TYPE_CHECKING, Any, Collection, Optional, Sequence, TypeAlias, final

from hathor.conf.settings import HATHOR_TOKEN_UID
from hathor.nanocontracts.storage import NCContractStorage
from hathor.nanocontracts.types import Amount, BlueprintId, ContractId, NCAction, TokenUid

Expand Down Expand Up @@ -218,14 +219,26 @@ def revoke_authorities(self, token_uid: TokenUid, *, revoke_mint: bool, revoke_m
self.__runner.syscall_revoke_authorities(token_uid=token_uid, revoke_mint=revoke_mint, revoke_melt=revoke_melt)

@final
def mint_tokens(self, token_uid: TokenUid, amount: int) -> None:
def mint_tokens(
self,
token_uid: TokenUid,
amount: int,
*,
fee_payment_token: TokenUid = TokenUid(HATHOR_TOKEN_UID)
) -> None:
"""Mint tokens and add them to the balance of this nano contract."""
self.__runner.syscall_mint_tokens(token_uid=token_uid, amount=amount)
self.__runner.syscall_mint_tokens(token_uid=token_uid, amount=amount, fee_payment_token=fee_payment_token)

@final
def melt_tokens(self, token_uid: TokenUid, amount: int) -> None:
def melt_tokens(
self,
token_uid: TokenUid,
amount: int,
*,
fee_payment_token: TokenUid = TokenUid(HATHOR_TOKEN_UID)
) -> None:
"""Melt tokens by removing them from the balance of this nano contract."""
self.__runner.syscall_melt_tokens(token_uid=token_uid, amount=amount)
self.__runner.syscall_melt_tokens(token_uid=token_uid, amount=amount, fee_payment_token=fee_payment_token)

@final
def create_contract(
Expand All @@ -250,9 +263,9 @@ def create_deposit_token(
token_name: str,
token_symbol: str,
amount: int,
*,
mint_authority: bool = True,
melt_authority: bool = True,
*,
salt: bytes = b'',
) -> TokenUid:
"""Create a new deposit-based token."""
Expand All @@ -271,10 +284,11 @@ def create_fee_token(
token_name: str,
token_symbol: str,
amount: int,
*,
mint_authority: bool = True,
melt_authority: bool = True,
*,
salt: bytes = b'',
fee_payment_token: TokenUid = TokenUid(HATHOR_TOKEN_UID)
) -> TokenUid:
"""Create a new fee-based token."""
return self.__runner.syscall_create_child_fee_token(
Expand All @@ -284,6 +298,7 @@ def create_fee_token(
amount=amount,
mint_authority=mint_authority,
melt_authority=melt_authority,
fee_payment_token=fee_payment_token
)

@final
Expand Down
4 changes: 4 additions & 0 deletions hathor/nanocontracts/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class NCInvalidMethodCall(NCFail, NCTxValidationError):
"""Raised when a contract calls another contract's invalid method."""


class NCInvalidFeePaymentToken(NCFail):
"""Raised when a payment token is invalid."""


class NCInvalidInitializeMethodCall(NCFail):
"""Raised when a contract calls another contract's initialize method."""

Expand Down
13 changes: 11 additions & 2 deletions hathor/nanocontracts/nc_types/dataclass_nc_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@
if TYPE_CHECKING:
from _typeshed import DataclassInstance

from hathor.nanocontracts.nc_types import TypeToNCTypeMap

D = TypeVar('D', bound='DataclassInstance')


def make_dataclass_nc_type(class_: type[D]) -> DataclassNCType[D]:
def make_dataclass_nc_type(
class_: type[D],
*,
extra_nc_types_map: TypeToNCTypeMap | None = None,
) -> DataclassNCType[D]:
""" Helper function to build a NCType for the given dataclass.
"""
from hathor.nanocontracts.nc_types import DEFAULT_TYPE_ALIAS_MAP, RETURN_TYPE_TO_NC_TYPE_MAP
type_map = NCType.TypeMap(DEFAULT_TYPE_ALIAS_MAP, RETURN_TYPE_TO_NC_TYPE_MAP)
alias_map = DEFAULT_TYPE_ALIAS_MAP
extras = extra_nc_types_map or {}
nc_types_map = {**RETURN_TYPE_TO_NC_TYPE_MAP, **extras}
type_map = NCType.TypeMap(alias_map, nc_types_map)
return DataclassNCType._from_type(class_, type_map=type_map)


Expand Down
5 changes: 5 additions & 0 deletions hathor/nanocontracts/nc_types/sized_int_nc_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,8 @@ class Int32NCType(_SizedIntNCType):
class Uint32NCType(_SizedIntNCType):
_signed = False
_byte_size = 4 # 4-bytes -> 32-bits


class Uint8NCType(_SizedIntNCType):
_signed = False
_byte_size = 1
Loading
Loading