1
1
from __future__ import annotations
2
2
3
3
from copy import deepcopy
4
+ from dataclasses import dataclass , field
4
5
from typing import Dict , List , Optional , Set , Union
5
6
6
7
from pycardano .address import Address
58
59
)
59
60
60
61
62
+ @dataclass
61
63
class TransactionBuilder :
62
64
"""A class builder that makes it easy to build a transaction.
63
65
@@ -66,32 +68,45 @@ class TransactionBuilder:
66
68
utxo_selectors (Optional[List[UTxOSelector]]): A list of UTxOSelectors that will select input UTxOs.
67
69
"""
68
70
69
- def __init__ (
70
- self , context : ChainContext , utxo_selectors : Optional [List [UTxOSelector ]] = None
71
- ):
72
- self .context = context
73
- self ._inputs = []
74
- self ._excluded_inputs = []
75
- self ._input_addresses = []
76
- self ._outputs = []
77
- self ._fee = 0
78
- self ._ttl = None
79
- self ._validity_start = None
80
- self ._auxiliary_data = None
81
- self ._native_scripts = None
82
- self ._mint = None
83
- self ._required_signers = None
84
- self ._scripts = {}
85
- self ._datums = {}
86
- self ._redeemers = []
87
- self ._inputs_to_redeemers = {}
88
- self ._inputs_to_scripts = {}
89
- self ._collaterals = []
90
-
91
- if utxo_selectors :
92
- self .utxo_selectors = utxo_selectors
93
- else :
94
- self .utxo_selectors = [RandomImproveMultiAsset (), LargestFirstSelector ()]
71
+ context : ChainContext
72
+
73
+ utxo_selectors : List [UTxOSelector ] = field (
74
+ default_factory = lambda : [RandomImproveMultiAsset (), LargestFirstSelector ()]
75
+ )
76
+
77
+ _inputs : List [UTxO ] = field (init = False , default_factory = lambda : [])
78
+
79
+ _excluded_inputs : List [UTxO ] = field (init = False , default_factory = lambda : [])
80
+
81
+ _input_addresses : List [Address ] = field (init = False , default_factory = lambda : [])
82
+
83
+ _outputs : List [TransactionOutput ] = field (init = False , default_factory = lambda : [])
84
+
85
+ _fee : int = field (init = False , default = 0 )
86
+
87
+ _ttl : int = field (init = False , default = None )
88
+
89
+ _validity_start : int = field (init = False , default = None )
90
+
91
+ _auxiliary_data : AuxiliaryData = field (init = False , default = None )
92
+
93
+ _native_scripts : List [NativeScript ] = field (init = False , default = None )
94
+
95
+ _mint : MultiAsset = field (init = False , default = None )
96
+
97
+ _required_signers : List [VerificationKeyHash ] = field (init = False , default = None )
98
+
99
+ _datums : Dict [DatumHash , Datum ] = field (init = False , default_factory = lambda : {})
100
+
101
+ _inputs_to_redeemers : Dict [UTxO , Redeemer ] = field (
102
+ init = False , default_factory = lambda : {}
103
+ )
104
+
105
+ _inputs_to_scripts : Dict [UTxO , bytes ] = field (
106
+ init = False , default_factory = lambda : {}
107
+ )
108
+
109
+ _collaterals : List [UTxO ] = field (init = False , default_factory = lambda : [])
95
110
96
111
def add_input (self , utxo : UTxO ) -> TransactionBuilder :
97
112
"""Add a specific UTxO to transaction's inputs.
@@ -129,9 +144,7 @@ def add_script_input(
129
144
f"Datum hash in transaction output is { utxo .output .datum_hash } , "
130
145
f"but actual datum hash from input datum is { datum .hash ()} ."
131
146
)
132
- self .scripts [plutus_script_hash (script )] = script
133
147
self .datums [datum .hash ()] = datum
134
- self .redeemers .append (redeemer )
135
148
self ._inputs_to_redeemers [utxo ] = redeemer
136
149
self ._inputs_to_scripts [utxo ] = script
137
150
self .inputs .append (utxo )
@@ -252,16 +265,16 @@ def required_signers(self, signers: List[VerificationKeyHash]):
252
265
self ._required_signers = signers
253
266
254
267
@property
255
- def scripts (self ) -> Dict [ ScriptHash , bytes ]:
256
- return self ._scripts
268
+ def scripts (self ) -> List [ bytes ]:
269
+ return list ( set ( self ._inputs_to_scripts . values ()))
257
270
258
271
@property
259
272
def datums (self ) -> Dict [DatumHash , Datum ]:
260
273
return self ._datums
261
274
262
275
@property
263
276
def redeemers (self ) -> List [Redeemer ]:
264
- return self ._redeemers
277
+ return list ( self ._inputs_to_redeemers . values ())
265
278
266
279
@property
267
280
def collaterals (self ) -> List [UTxO ]:
@@ -579,7 +592,7 @@ def build_witness_set(self) -> TransactionWitnessSet:
579
592
"""
580
593
return TransactionWitnessSet (
581
594
native_scripts = self .native_scripts ,
582
- plutus_script = list ( self .scripts . values ()) if self .scripts else None ,
595
+ plutus_script = self .scripts if self .scripts else None ,
583
596
redeemer = self .redeemers if self .redeemers else None ,
584
597
plutus_data = list (self .datums .values ()) if self .datums else None ,
585
598
)
0 commit comments