15
15
16
16
from enum import Enum
17
17
from io import BytesIO
18
- from typing import Callable , Dict , List , Optional , Tuple , Union
18
+ from typing import Callable , Dict , Generic , List , Optional , Tuple , TypeVar , Union
19
+
20
+ from typing_extensions import TypeGuard
19
21
20
22
from .argtypes import SignerType
21
23
from .btctools import script
@@ -68,14 +70,17 @@ class ContractInstanceStatus(Enum):
68
70
SPENT = 2 # Already spent
69
71
70
72
71
- class ContractInstance :
73
+ ContractT = TypeVar ('ContractT' , bound = Union [StandardP2TR , StandardAugmentedP2TR ])
74
+
75
+
76
+ class ContractInstance (Generic [ContractT ]):
72
77
"""
73
78
Represents a specific instance of a Contract. It keeps track of:
74
79
- the instance status
75
80
- if augmented, the data embedded in the Contract instance.
76
81
"""
77
82
78
- def __init__ (self , contract : Union [ StandardP2TR , StandardAugmentedP2TR ] ):
83
+ def __init__ (self , contract : ContractT ):
79
84
"""
80
85
Initializes a new ContractInstance with the given contract template.
81
86
@@ -88,7 +93,7 @@ def __init__(self, contract: Union[StandardP2TR, StandardAugmentedP2TR]):
88
93
self .data : Optional [bytes ] = None
89
94
self .data_expanded : Optional [ContractState ] = None # TODO: figure out a good API for this
90
95
91
- self .manager : ContractManager = None
96
+ self .manager : Optional [ ContractManager ] = None
92
97
93
98
self .last_height = 0
94
99
@@ -104,14 +109,14 @@ def __init__(self, contract: Union[StandardP2TR, StandardAugmentedP2TR]):
104
109
# the new instances produced by spending this instance
105
110
self .next : Optional [List [ContractInstance ]] = None
106
111
107
- def is_augm (self ) -> bool :
112
+ def is_augmented (self ) -> TypeGuard [ 'ContractInstance[StandardAugmentedP2TR]' ] :
108
113
"""
109
114
Checks if the Contract contained in this instance is augmented.
110
115
111
116
Returns:
112
117
bool: True if the contract is augmented, False otherwise.
113
118
"""
114
- return isinstance (self .contract , AugmentedP2TR ) or isinstance ( self . contract , StandardAugmentedP2TR )
119
+ return isinstance (self .contract , StandardAugmentedP2TR )
115
120
116
121
def get_tr_info (self ) -> TaprootInfo :
117
122
"""
@@ -123,7 +128,7 @@ def get_tr_info(self) -> TaprootInfo:
123
128
Raises:
124
129
ValueError: If the contract is augmented but no data is set for the instance.
125
130
"""
126
- if not self .is_augm ():
131
+ if not self .is_augmented ():
127
132
return self .contract .get_tr_info ()
128
133
else :
129
134
if self .data is None :
@@ -168,7 +173,7 @@ def decode_wit_stack(self, stack_elems: List[bytes]) -> Tuple[str, dict]:
168
173
Returns:
169
174
Tuple[str, dict]: A tuple containing the name of the clause used and a dictionary of the arguments provided to the clause.
170
175
"""
171
- if self .is_augm ():
176
+ if self .is_augmented ():
172
177
assert self .data is not None
173
178
174
179
return self .contract .decode_wit_stack (self .data , stack_elems )
@@ -292,7 +297,7 @@ def wait_for_outpoint(self, instance: ContractInstance, txid: Optional[str] = No
292
297
"""
293
298
294
299
self ._check_instance (instance , exp_statuses = ContractInstanceStatus .ABSTRACT )
295
- if instance .is_augm ():
300
+ if instance .is_augmented ():
296
301
if instance .data is None :
297
302
raise ValueError ("Data not set in instance" )
298
303
scriptPubKey = instance .contract .get_tr_info (instance .data ).scriptPubKey
@@ -620,7 +625,7 @@ def wait_for_spend(self, instances: Union[ContractInstance, List[ContractInstanc
620
625
self .add_instance (instance )
621
626
return result
622
627
623
- def fund_instance (self , contract : Union [ StandardP2TR , StandardAugmentedP2TR ], amount : int , data : Optional [ContractState ] = None ) -> ContractInstance :
628
+ def fund_instance (self , contract : ContractT , amount : int , data : Optional [ContractState ] = None ) -> ContractInstance [ ContractT ] :
624
629
"""
625
630
Creates a new contract instance from a specified contract template, funds it with a specified amount of satoshis,
626
631
and adds it to the manager.
0 commit comments