Skip to content

Commit 86dc481

Browse files
committed
More goverance related classes
1 parent 47c880d commit 86dc481

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

integration-test/test/test_governance.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ def test_governance_action_and_voting(self):
8585
time.sleep(5)
8686

8787
# Step 2: Create and submit parameter change action
88-
info_proposal = InfoAction()
88+
param_update = ProtocolParamUpdate(
89+
max_block_body_size=75536,
90+
max_transaction_size=26384,
91+
)
92+
93+
parameter_change_action = ParameterChangeAction(None, param_update, None)
8994

9095
# Create transaction for parameter change
9196
builder = TransactionBuilder(self.chain_context)
@@ -97,9 +102,9 @@ def test_governance_action_and_voting(self):
97102
builder.add_proposal(
98103
100000000000,
99104
bytes(reward_account),
100-
info_proposal,
105+
parameter_change_action,
101106
Anchor(
102-
url="https://test-info.com",
107+
url="https://test-param-update.com",
103108
data_hash=AnchorDataHash(bytes.fromhex("0" * 64)),
104109
),
105110
)

pycardano/plutus.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,8 @@ class RedeemerTag(CBORSerializable, Enum):
948948
MINT = 1
949949
CERTIFICATE = 2
950950
WITHDRAWAL = 3
951+
VOTING = 4
952+
PROPOSING = 5
951953

952954
def to_primitive(self) -> int:
953955
return self.value

pycardano/serialization.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,15 @@
4040
logger.warning("Failed to remove semantic decoder for CBOR tag 258", e)
4141
pass
4242

43-
from cbor2 import CBOREncoder, CBORSimpleValue, CBORTag, dumps, loads, undefined
44-
from frozendict import frozendict
43+
from cbor2 import (
44+
CBOREncoder,
45+
CBORSimpleValue,
46+
CBORTag,
47+
FrozenDict,
48+
dumps,
49+
loads,
50+
undefined,
51+
)
4552
from frozenlist import FrozenList
4653
from pprintpp import pformat
4754

@@ -126,7 +133,7 @@ class RawCBOR:
126133
set,
127134
Fraction,
128135
frozenset,
129-
frozendict,
136+
FrozenDict,
130137
FrozenList,
131138
IndefiniteFrozenList,
132139
ByteString,
@@ -154,7 +161,7 @@ class RawCBOR:
154161
CBORTag,
155162
set,
156163
frozenset,
157-
frozendict,
164+
FrozenDict,
158165
Fraction,
159166
FrozenList,
160167
IndefiniteFrozenList,
@@ -205,7 +212,7 @@ def default_encoder(
205212
RawCBOR,
206213
FrozenList,
207214
IndefiniteFrozenList,
208-
frozendict,
215+
FrozenDict,
209216
),
210217
), (
211218
f"Type of input value is not CBORSerializable, " f"got {type(value)} instead."
@@ -231,7 +238,7 @@ def default_encoder(
231238
encoder.write(value.cbor)
232239
elif isinstance(value, FrozenList):
233240
encoder.encode(list(value))
234-
elif isinstance(value, frozendict):
241+
elif isinstance(value, FrozenDict):
235242
encoder.encode(dict(value))
236243
else:
237244
encoder.encode(value.to_validated_primitive())
@@ -296,7 +303,7 @@ def _dfs(value, freeze=False):
296303
for k, v in value.items():
297304
_dict[_dfs(k, freeze=True)] = _dfs(v, freeze)
298305
if freeze:
299-
return frozendict(_dict)
306+
return FrozenDict(_dict)
300307
return _dict
301308
elif isinstance(value, set):
302309
_set = set(_dfs(v, freeze=True) for v in value)
@@ -348,7 +355,7 @@ def _check_recursive(value, type_hint):
348355
return _check_recursive(value, type_hint.__args__[0])
349356
elif origin is Union:
350357
return any(_check_recursive(value, arg) for arg in type_hint.__args__)
351-
elif origin is Dict or isinstance(value, (dict, frozendict)):
358+
elif origin is Dict or isinstance(value, (dict, FrozenDict)):
352359
key_type, value_type = type_hint.__args__
353360
return all(
354361
_check_recursive(k, key_type) and _check_recursive(v, value_type)
@@ -814,8 +821,8 @@ def to_shallow_primitive(self) -> Primitive:
814821
return primitives
815822

816823
@classmethod
817-
@limit_primitive_type(dict)
818-
def from_primitive(cls: Type[MapBase], values: dict) -> MapBase:
824+
@limit_primitive_type(dict, FrozenDict)
825+
def from_primitive(cls: Type[MapBase], values: Union[dict, FrozenDict]) -> MapBase:
819826
"""Restore a primitive value to its original class type.
820827
821828
Args:
@@ -1038,10 +1045,14 @@ def from_primitive(
10381045
value.value = [type_arg.from_primitive(v) for v in value.value]
10391046
return cls(value.value, use_tag=True)
10401047

1048+
use_tag = isinstance(value, set)
1049+
10411050
if isinstance(value, (list, tuple, set)):
10421051
if isclass(type_arg) and issubclass(type_arg, CBORSerializable):
10431052
value = [type_arg.from_primitive(v) for v in value]
1044-
return cls(list(value), use_tag=False)
1053+
1054+
# If the value is a set, we know it is coming from a CBORTag (#6.258)
1055+
return cls(list(value), use_tag=use_tag)
10451056

10461057
raise ValueError(f"Cannot deserialize {value} to {cls}")
10471058

test/pycardano/test_transaction.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from dataclasses import dataclass
2+
from fractions import Fraction
23
from test.pycardano.util import check_two_way_cbor
34

45
import pytest
56
from typeguard import TypeCheckError
67

8+
from pycardano import ParameterChangeAction
79
from pycardano.address import Address
810
from pycardano.exception import InvalidDataException, InvalidOperationException
911
from pycardano.hash import SCRIPT_HASH_SIZE, ScriptHash, TransactionId
@@ -617,3 +619,17 @@ def test_add_empty_pop():
617619
)
618620
assert len(nft_output.multi_asset) == 1
619621
assert len(nft_output.multi_asset[ScriptHash(policy)]) == 1
622+
623+
624+
def test_decode_param_update_proposal_tx():
625+
# The proposal of decreasing treasury tax from 20% to 10% on mainnet
626+
# https://cardanoscan.io/transaction/941502b0aa104c850d197923259444d2b57cab7af18b63143775465aaacc84f5
627+
tx_cbor_hex = """84a700d90102868258202f980a7d47a6195c975c266335211afd3b9cabb5db5165e6e6d9cb18418415ab008258202f980a7d47a6195c975c266335211afd3b9cabb5db5165e6e6d9cb18418415ab018258202f980a7d47a6195c975c266335211afd3b9cabb5db5165e6e6d9cb18418415ab028258202f980a7d47a6195c975c266335211afd3b9cabb5db5165e6e6d9cb18418415ab0382582040aba0069d0dce7f801a9d16c26d469ec8ce16e1eb68379ae2774e5d28f33d5b008258206ba686304126196267200c6502df4b42af898ad2fb1621561fdb0a457fd8b68b000dd90102818258202f980a7d47a6195c975c266335211afd3b9cabb5db5165e6e6d9cb18418415ab040181825839013c55ef61a7fac4c7f94dc65052586f31dd659acddffc69f13d2c4364646c9e5f7484e8aeceba94566b73b8b50394eb6bfb54f67ac5885d591ab25dc1bf021a0004ee04031a08d0f5dc0b58204a080e29d89a598d6a3c000c9f15f4ab74a10ffdaa320f256fc7f69b75ff8a5914d9010281841b000000174876e800581de1646c9e5f7484e8aeceba94566b73b8b50394eb6bfb54f67ac5885d598400825820b2a591ac219ce6dcca5847e0248015209c7cb0436aa6bd6863d0c1f152a60bc500a10bd81e82010a581cfa24fb305126805cf2164c161d852a0e7330cf988f1fe558cf7d4a64827835697066733a2f2f516d634b51676763706f757568414176555947447a6f4b674d77625a536b57716945654536633637534a336b457158209b2438f0032a0c24ed62d12d6bdb79b47e2bd0c4d2dd4f4936c055ead7109cafa300d90102818258205d58313597871a1823742d172d738fcd1fee4800ba41859db790f981d4dae74e584089b07924734e5b9d813b43638c3e2e6f4ac1e473e454d2d5b404b7bee939d8b5046b6a5c4ba0b51096d5538feb933e802a5944442b046ef11b2381ffce70f70e07d90102815908545908510101003232323232323232323232323232323232323232323232323232323232323232323232323232323232259323255333573466e1d20000011180098111bab357426ae88d55cf00104554ccd5cd19b87480100044600422c6aae74004dd51aba1357446ae88d55cf1baa3255333573466e1d200a35573a002226ae84d5d11aab9e00111637546ae84d5d11aba235573c6ea800642b26006003149a2c8a4c301f801c0052000c00e0070018016006901e4070c00e003000c00d20d00fc000c0003003800a4005801c00e003002c00d20c09a0c80e1801c006001801a4101b5881380018000600700148013003801c006005801a410100078001801c006001801a4101001f8001800060070014801b0038018096007001800600690404002600060001801c0052008c00e006025801c006001801a41209d8001800060070014802b003801c006005801a410112f501c3003800c00300348202b7881300030000c00e00290066007003800c00b003482032ad7b806038403060070014803b00380180960003003800a4021801c00e003002c00d20f40380e1801c006001801a41403f800100a0c00e0029009600f0030078040c00e002900a600f003800c00b003301a483403e01a600700180060066034904801e00060001801c0052016c01e00600f801c006001801980c2402900e30000c00e002901060070030128060c00e00290116007003800c00b003483c0ba03860070018006006906432e00040283003800a40498003003800a404d802c00e00f003800c00b003301a480cb0003003800c003003301a4802b00030001801c01e0070018016006603490605c0160006007001800600660349048276000600030000c00e0029014600b003801c00c04b003800c00300348203a2489b00030001801c00e006025801c006001801a4101b11dc2df80018000c0003003800a4055802c00e007003012c00e003000c00d2080b8b872c000c0006007003801809600700180060069040607e4155016000600030000c00e00290166007003012c00e003000c00d2080c001c000c0003003800a405d801c00e003002c00d20c80180e1801c006001801a412007800100a0c00e00290186007003013c0006007001480cb005801801e006003801800e00600500403003800a4069802c00c00f003001c00c007003803c00e003002c00c05300333023480692028c0004014c00c00b003003c00c00f003003c00e00f003800c00b00301480590052008003003800a406d801c00e003002c00d2000c00d2006c00060070018006006900a600060001801c0052038c00e007001801600690006006901260003003800c003003483281300020141801c005203ac00e006027801c006001801a403d800180006007001480f3003801804e00700180060069040404af3c4e302600060001801c005203ec00e006013801c006001801a4101416f0fd20b80018000600700148103003801c006005801a403501c3003800c0030034812b00030000c00e0029021600f003800c00a01ac00e003000c00ccc08d20d00f4800b00030000c0000000000803c00c016008401e006009801c006001801807e0060298000c000401e006007801c0060018018074020c000400e00f003800c00b003010c000802180020070018006006019801805e0003000400600580180760060138000800c00b00330134805200c400e00300080330004006005801a4001801a410112f58000801c00600901260008019806a40118002007001800600690404a75ee01e00060008018046000801801e000300c4832004c025201430094800a0030028052003002c00d2002c000300648010c0092002300748028c0312000300b48018c0292012300948008c0212066801a40018000c0192008300a2233335573e00250002801994004d55ce800cd55cf0008d5d08014c00cd5d10011263009222532900389800a4d2219002912c80344c01526910c80148964cc04cdd68010034564cc03801400626601800e0071801226601800e01518010096400a3000910c008600444002600244004a664600200244246466004460044460040064600444600200646a660080080066a00600224446600644b20051800484ccc02600244666ae68cdc3801000c00200500a91199ab9a33710004003000801488ccd5cd19b89002001800400a44666ae68cdc4801000c00a00122333573466e20008006005000912a999ab9a3371200400222002220052255333573466e2400800444008440040026eb400a42660080026eb000a4264666015001229002914801c8954ccd5cd19b8700400211333573466e1c00c006001002118011229002914801c88cc044cdc100200099b82002003245200522900391199ab9a3371066e08010004cdc1001001c002004403245200522900391199ab9a3371266e08010004cdc1001001c00a00048a400a45200722333573466e20cdc100200099b820020038014000912c99807001000c40062004912c99807001000c400a2002001199919ab9a357466ae880048cc028dd69aba1003375a6ae84008d5d1000934000dd60010a40064666ae68d5d1800c0020052225933006003357420031330050023574400318010600a444aa666ae68cdc3a400000222c22aa666ae68cdc4000a4000226600666e05200000233702900000088994004cdc2001800ccdc20010008cc010008004c01088954ccd5cd19b87480000044400844cc00c004cdc300100091119803112c800c60012219002911919806912c800c4c02401a442b26600a004019130040018c008002590028c804c8888888800d1900991111111002a244b267201722222222008001000c600518000001112a999ab9a3370e004002230001155333573466e240080044600823002229002914801c88ccd5cd19b893370400800266e0800800e00100208c8c0040048c0088cc00800800505a182050082a0821a0007c6d41a06a71df2f5f6"""
628+
tx = Transaction.from_cbor(tx_cbor_hex)
629+
assert len(tx.transaction_body.proposal_procedures) == 1
630+
assert isinstance(
631+
tx.transaction_body.proposal_procedures[0].gov_action, ParameterChangeAction
632+
)
633+
assert tx.transaction_body.proposal_procedures[
634+
0
635+
].gov_action.protocol_param_update.treasury_growth_rate == Fraction(1, 10)

0 commit comments

Comments
 (0)