11import logging
2+ import time
23
34import clvm
45from typing import Dict , Optional , List , Any , Set
2728 create_spend_for_ephemeral ,
2829)
2930from src .wallet .cc_wallet .ccparent import CCParent
31+ from src .wallet .transaction_record import TransactionRecord
3032from src .wallet .util .json_util import dict_to_json_str
3133from src .wallet .util .wallet_types import WalletType
3234from src .wallet .wallet import Wallet
@@ -75,14 +77,62 @@ async def create_new_cc(
7577 "CC Wallet" , WalletType .COLOURED_COIN , info_as_string
7678 )
7779 if self .wallet_info is None :
78- raise
80+ raise ValueError ( "Internal Error" )
7981
8082 spend_bundle = await self .generate_new_coloured_coin (amount )
8183 if spend_bundle is None :
82- raise
84+ raise ValueError ( "Internal Error, unable to generate new coloured coin" )
8385
8486 await self .wallet_state_manager .add_new_wallet (self , self .wallet_info .id )
85- await self .standard_wallet .push_transaction (spend_bundle )
87+
88+ # Change and actual coloured coin
89+ non_ephemeral_spends : List [Coin ] = spend_bundle .not_ephemeral_additions ()
90+ cc_coin = None
91+ puzzle_store = self .wallet_state_manager .puzzle_store
92+
93+ for c in non_ephemeral_spends :
94+ info = await puzzle_store .wallet_info_for_puzzle_hash (c .puzzle_hash )
95+ if info is None :
96+ raise ValueError ("Internal Error" )
97+ id , wallet_type = info
98+ if id == self .wallet_info .id :
99+ cc_coin = c
100+
101+ if cc_coin is None :
102+ raise ValueError ("Internal Error, unable to generate new coloured coin" )
103+
104+ regular_record = TransactionRecord (
105+ confirmed_at_index = uint32 (0 ),
106+ created_at_time = uint64 (int (time .time ())),
107+ to_puzzle_hash = cc_coin .puzzle_hash ,
108+ amount = uint64 (cc_coin .amount ),
109+ fee_amount = uint64 (0 ),
110+ incoming = False ,
111+ confirmed = False ,
112+ sent = uint32 (0 ),
113+ spend_bundle = spend_bundle ,
114+ additions = spend_bundle .additions (),
115+ removals = spend_bundle .removals (),
116+ wallet_id = self .wallet_state_manager .main_wallet .wallet_info .id ,
117+ sent_to = [],
118+ )
119+ cc_record = TransactionRecord (
120+ confirmed_at_index = uint32 (0 ),
121+ created_at_time = uint64 (int (time .time ())),
122+ to_puzzle_hash = cc_coin .puzzle_hash ,
123+ amount = uint64 (cc_coin .amount ),
124+ fee_amount = uint64 (0 ),
125+ incoming = True ,
126+ confirmed = False ,
127+ sent = uint32 (10 ),
128+ spend_bundle = None ,
129+ additions = spend_bundle .additions (),
130+ removals = spend_bundle .removals (),
131+ wallet_id = self .wallet_info .id ,
132+ sent_to = [],
133+ )
134+ await self .standard_wallet .push_transaction (regular_record )
135+ await self .standard_wallet .push_transaction (cc_record )
86136 return self
87137
88138 @staticmethod
@@ -154,20 +204,18 @@ async def get_confirmed_balance(self) -> uint64:
154204
155205 async def get_unconfirmed_balance (self ) -> uint64 :
156206 confirmed = await self .get_confirmed_balance ()
157- unconfirmed_tx = await self .wallet_state_manager .tx_store .get_unconfirmed_for_wallet (
207+ unconfirmed_tx : List [ TransactionRecord ] = await self .wallet_state_manager .tx_store .get_unconfirmed_for_wallet (
158208 self .wallet_info .id
159209 )
160210 addition_amount = 0
161211 removal_amount = 0
162212
163213 for record in unconfirmed_tx :
164- for coin in record .additions :
165- if await self .wallet_state_manager .puzzle_store .puzzle_hash_exists (
166- coin .puzzle_hash
167- ):
168- addition_amount += coin .amount
169- for coin in record .removals :
170- removal_amount += coin .amount
214+ if record .incoming :
215+ addition_amount += record .amount
216+ else :
217+ removal_amount += record .amount
218+
171219 result = confirmed - removal_amount + addition_amount
172220
173221 self .log .info (f"Unconfirmed balance for cc wallet is { result } " )
@@ -426,16 +474,16 @@ async def generate_zero_val_coin(
426474 cc_puzzle = cc_wallet_puzzles .cc_make_puzzle (cc_inner , self .cc_info .my_core )
427475 cc_puzzle_hash = cc_puzzle .get_tree_hash ()
428476
429- spend_bundle = await self .standard_wallet .generate_signed_transaction (
477+ tx = await self .standard_wallet .generate_signed_transaction (
430478 uint64 (0 ), cc_puzzle_hash , uint64 (0 ), origin_id , coins
431479 )
432480 self .log .warning (f"cc_puzzle_hash is { cc_puzzle_hash } " )
433481 eve_coin = Coin (origin_id , cc_puzzle_hash , uint64 (0 ))
434- if spend_bundle is None :
482+ if tx is None or tx . spend_bundle is None :
435483 return None
436484
437485 eve_spend = cc_generate_eve_spend (eve_coin , cc_puzzle )
438- full_spend = SpendBundle .aggregate ([spend_bundle , eve_spend ])
486+ full_spend = SpendBundle .aggregate ([tx . spend_bundle , eve_spend ])
439487
440488 future_parent = CCParent (eve_coin .parent_coin_info , cc_inner , eve_coin .amount )
441489 eve_parent = CCParent (
@@ -446,7 +494,39 @@ async def generate_zero_val_coin(
446494 await self .add_parent (eve_coin .parent_coin_info , eve_parent )
447495
448496 if send :
449- await self .standard_wallet .push_transaction (full_spend )
497+ regular_record = TransactionRecord (
498+ confirmed_at_index = uint32 (0 ),
499+ created_at_time = uint64 (int (time .time ())),
500+ to_puzzle_hash = cc_puzzle_hash ,
501+ amount = uint64 (0 ),
502+ fee_amount = uint64 (0 ),
503+ incoming = False ,
504+ confirmed = False ,
505+ sent = uint32 (10 ),
506+ spend_bundle = full_spend ,
507+ additions = full_spend .additions (),
508+ removals = full_spend .removals (),
509+ wallet_id = uint32 (1 ),
510+ sent_to = [],
511+ )
512+ cc_record = TransactionRecord (
513+ confirmed_at_index = uint32 (0 ),
514+ created_at_time = uint64 (int (time .time ())),
515+ to_puzzle_hash = cc_puzzle_hash ,
516+ amount = uint64 (0 ),
517+ fee_amount = uint64 (0 ),
518+ incoming = True ,
519+ confirmed = False ,
520+ sent = uint32 (0 ),
521+ spend_bundle = full_spend ,
522+ additions = full_spend .additions (),
523+ removals = full_spend .removals (),
524+ wallet_id = self .wallet_info .id ,
525+ sent_to = [],
526+ )
527+ await self .wallet_state_manager .add_transaction (regular_record )
528+ await self .wallet_state_manager .add_pending_transaction (cc_record )
529+
450530 return full_spend
451531
452532 async def get_cc_spendable_coins (self ) -> List [WalletCoinRecord ]:
@@ -676,8 +756,23 @@ async def cc_spend(
676756 aggsig = BLSSignature .aggregate (sigs )
677757 spend_bundle = SpendBundle (list_of_solutions , aggsig )
678758
759+ tx_record = TransactionRecord (
760+ confirmed_at_index = uint32 (0 ),
761+ created_at_time = uint64 (int (time .time ())),
762+ to_puzzle_hash = to_address ,
763+ amount = uint64 (amount ),
764+ fee_amount = uint64 (0 ),
765+ incoming = False ,
766+ confirmed = False ,
767+ sent = uint32 (0 ),
768+ spend_bundle = spend_bundle ,
769+ additions = spend_bundle .additions (),
770+ removals = spend_bundle .removals (),
771+ wallet_id = self .wallet_info .id ,
772+ sent_to = [],
773+ )
679774 await self .wallet_state_manager .add_pending_transaction (
680- spend_bundle , self . wallet_info . id
775+ tx_record
681776 )
682777
683778 return spend_bundle
@@ -725,17 +820,17 @@ async def generate_new_coloured_coin(self, amount: uint64) -> Optional[SpendBund
725820 cc_puzzle = cc_wallet_puzzles .cc_make_puzzle (cc_inner , cc_core )
726821 cc_puzzle_hash = cc_puzzle .get_tree_hash ()
727822
728- spend_bundle = await self .standard_wallet .generate_signed_transaction (
823+ tx_record : Optional [ TransactionRecord ] = await self .standard_wallet .generate_signed_transaction (
729824 amount , cc_puzzle_hash , uint64 (0 ), origin_id , coins
730825 )
731826 self .log .warning (f"cc_puzzle_hash is { cc_puzzle_hash } " )
732827 eve_coin = Coin (origin_id , cc_puzzle_hash , amount )
733- if spend_bundle is None :
828+ if tx_record is None or tx_record . spend_bundle is None :
734829 return None
735830
736831 eve_spend = cc_generate_eve_spend (eve_coin , cc_puzzle )
737832
738- full_spend = SpendBundle .aggregate ([spend_bundle , eve_spend ])
833+ full_spend = SpendBundle .aggregate ([tx_record . spend_bundle , eve_spend ])
739834 return full_spend
740835
741836 async def create_spend_bundle_relative_amount (
0 commit comments