Skip to content

Commit

Permalink
Add python bindings for SCAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
bbyalcinkaya committed Sep 12, 2024
1 parent 2e384bb commit a67a521
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/komet/kast/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def sc_bytes(b: bytes) -> KInner:
return KApply('SCVal:Bytes', [token(b)])


def sc_address(address: KInner) -> KInner:
return KApply('SCVal:Address', [address])


def sc_vec(l: Iterable[KInner]) -> KInner:
return KApply('SCVal:Vec', list_of(l))

Expand Down
43 changes: 43 additions & 0 deletions src/komet/scval.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from pyk.prelude.utils import token

from .kast.syntax import (
account_id,
contract_id,
sc_address,
sc_bool,
sc_bytes,
sc_i32,
Expand Down Expand Up @@ -120,6 +123,30 @@ def to_kast(self) -> KInner:
return sc_bytes(self.val)


@dataclass(frozen=True)
class AccountId:
val: bytes

def to_kast(self) -> KInner:
return account_id(self.val)


@dataclass(frozen=True)
class ContractId:
val: bytes

def to_kast(self) -> KInner:
return contract_id(self.val)


@dataclass(frozen=True)
class SCAddress(SCValue):
val: AccountId | ContractId

def to_kast(self) -> KInner:
return sc_address(self.val.to_kast())


@dataclass(frozen=True)
class SCVec(SCValue):
val: tuple[SCValue]
Expand Down Expand Up @@ -150,6 +177,7 @@ def to_kast(self) -> KInner:
'u256': 'SCU256Type',
'symbol': 'SCSymbolType',
'bytes': 'SCBytesType',
'address': 'SCAddressType',
'vec': 'SCVecType',
'map': 'SCMapType',
}
Expand Down Expand Up @@ -330,6 +358,21 @@ def as_var(cls, name: str) -> tuple[KInner, tuple[KInner, ...]]:
return KApply('SCVal:Bytes', [KVariable(name, KSort('Bytes'))]), ()


@dataclass
class SCAddressType(SCMonomorphicType):
def strategy(self) -> strategies.SearchStrategy:
def target(p: bool, val: bytes) -> SCAddress:
if p:
return SCAddress(ContractId(val))
return SCAddress(AccountId(val))

return strategies.builds(target, strategies.booleans(), strategies.binary(min_size=32, max_size=32))

@classmethod
def as_var(cls, name: str) -> tuple[KInner, tuple[KInner, ...]]:
return KApply('SCVal:Address', [KVariable(name, KSort('Address'))]), ()


@dataclass
class SCVecType(SCType):
element: SCType
Expand Down

0 comments on commit a67a521

Please sign in to comment.