Skip to content

Commit a54931d

Browse files
committed
transaction history /value
1 parent d4a96d3 commit a54931d

File tree

12 files changed

+262
-142
lines changed

12 files changed

+262
-142
lines changed

electron-ui/cc_wallet/cc_wallet_renderer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,12 @@ function get_wallet_balance_response(response) {
252252
var confirmed = parseInt(response["confirmed_wallet_balance"])
253253
var unconfirmed = parseInt(response["unconfirmed_wallet_balance"])
254254
var pending = confirmed - unconfirmed
255-
var wallet_id = response["wallet_id"]
256255

256+
var wallet_id = response["wallet_id"]
257+
console.log("wallet_id = " + wallet_id + "confirmed: " + confirmed + "unconfirmed: " + unconfirmed )
257258
chia_confirmed = chia_formatter(confirmed, 'mojo').to('chia').toString()
258259
chia_pending = chia_formatter(pending, 'mojo').to('chia').toString()
260+
chia_pending_abs = chia_formatter(Math.abs(pending), 'mojo').to('chia').toString()
259261

260262
wallet_balance_holder = document.querySelector("#" + "balance_wallet_" + wallet_id )
261263
wallet_pending_holder = document.querySelector("#" + "pending_wallet_" + wallet_id )
@@ -265,7 +267,7 @@ function get_wallet_balance_response(response) {
265267
if (pending > 0) {
266268
pending_textfield.innerHTML = lock + " - " + chia_pending + " CH"
267269
} else {
268-
pending_textfield.innerHTML = lock + " " + chia_pending + " CH"
270+
pending_textfield.innerHTML = lock + " " + chia_pending_abs + " CH"
269271
}
270272
}
271273
if (wallet_balance_holder) {
@@ -275,7 +277,7 @@ function get_wallet_balance_response(response) {
275277
if (pending > 0) {
276278
wallet_pending_holder.innerHTML = lock + " - " + chia_pending + " CH"
277279
} else {
278-
wallet_pending_holder.innerHTML = lock + " " + chia_pending + " CH"
280+
wallet_pending_holder.innerHTML = lock + " " + chia_pending_abs + " CH"
279281
}
280282
}
281283
}

electron-ui/renderer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,12 @@ function get_wallet_balance_response(response) {
323323
var confirmed = parseInt(response["confirmed_wallet_balance"])
324324
var unconfirmed = parseInt(response["unconfirmed_wallet_balance"])
325325
var pending = confirmed - unconfirmed
326-
var wallet_id = response["wallet_id"]
327326

327+
var wallet_id = response["wallet_id"]
328+
console.log("wallet_id = " + wallet_id + "confirmed: " + confirmed + "unconfirmed: " + unconfirmed )
328329
chia_confirmed = chia_formatter(confirmed, 'mojo').to('chia').toString()
329330
chia_pending = chia_formatter(pending, 'mojo').to('chia').toString()
331+
chia_pending_abs = chia_formatter(Math.abs(pending), 'mojo').to('chia').toString()
330332

331333
wallet_balance_holder = document.querySelector("#" + "balance_wallet_" + wallet_id )
332334
wallet_pending_holder = document.querySelector("#" + "pending_wallet_" + wallet_id )
@@ -336,7 +338,7 @@ function get_wallet_balance_response(response) {
336338
if (pending > 0) {
337339
pending_textfield.innerHTML = lock + " - " + chia_pending + " CH"
338340
} else {
339-
pending_textfield.innerHTML = lock + " " + chia_pending + " CH"
341+
pending_textfield.innerHTML = lock + " " + chia_pending_abs + " CH"
340342
}
341343
}
342344
if (wallet_balance_holder) {
@@ -346,7 +348,7 @@ function get_wallet_balance_response(response) {
346348
if (pending > 0) {
347349
wallet_pending_holder.innerHTML = lock + " - " + chia_pending + " CH"
348350
} else {
349-
wallet_pending_holder.innerHTML = lock + " " + chia_pending + " CH"
351+
wallet_pending_holder.innerHTML = lock + " " + chia_pending_abs + " CH"
350352
}
351353
}
352354
}

electron-ui/trade_renderer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ function get_wallet_balance_response(response) {
220220

221221
chia_confirmed = chia_formatter(confirmed, 'mojo').to('chia').toString()
222222
chia_pending = chia_formatter(pending, 'mojo').to('chia').toString()
223+
chia_pending_abs = chia_formatter(Math.abs(pending), 'mojo').to('chia').toString()
223224

224225
wallet_balance_holder = document.querySelector("#" + "balance_wallet_" + wallet_id )
225226
wallet_pending_holder = document.querySelector("#" + "pending_wallet_" + wallet_id )
@@ -231,7 +232,7 @@ function get_wallet_balance_response(response) {
231232
if (pending > 0) {
232233
wallet_pending_holder.innerHTML = lock + " - " + chia_pending + " CH"
233234
} else {
234-
wallet_pending_holder.innerHTML = lock + " " + chia_pending + " CH"
235+
wallet_pending_holder.innerHTML = lock + " " + chia_pending_abs + " CH"
235236
}
236237
}
237238
}

src/types/spend_bundle.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,15 @@ def not_ephemeral_spends(self):
7373
result.append(rem)
7474

7575
return result
76+
77+
def not_ephemeral_additions(self):
78+
all_removals = self.removals()
79+
all_additions = self.additions()
80+
result: List[Coin] = []
81+
82+
for add in all_additions:
83+
if add in all_removals:
84+
continue
85+
result.append(add)
86+
87+
return result

src/wallet/cc_wallet/cc_wallet.py

Lines changed: 114 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import time
23

34
import clvm
45
from typing import Dict, Optional, List, Any, Set
@@ -27,6 +28,7 @@
2728
create_spend_for_ephemeral,
2829
)
2930
from src.wallet.cc_wallet.ccparent import CCParent
31+
from src.wallet.transaction_record import TransactionRecord
3032
from src.wallet.util.json_util import dict_to_json_str
3133
from src.wallet.util.wallet_types import WalletType
3234
from 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(

src/wallet/trade_manager.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import time
12
from pathlib import Path
3+
from secrets import token_bytes
24
from typing import Dict, Optional, Tuple, List, Any
35
import logging
46

@@ -10,13 +12,14 @@
1012
from src.types.program import Program
1113
from src.types.sized_bytes import bytes32
1214
from src.types.spend_bundle import SpendBundle
13-
from src.util.ints import uint32
15+
from src.util.ints import uint32, uint64
1416
from src.wallet.cc_wallet import cc_wallet_puzzles
1517
from src.wallet.cc_wallet.cc_wallet import CCWallet
1618
from src.wallet.cc_wallet.cc_wallet_puzzles import (
1719
create_spend_for_auditor,
1820
create_spend_for_ephemeral,
1921
)
22+
from src.wallet.transaction_record import TransactionRecord
2023
from src.wallet.wallet import Wallet
2124
from src.wallet.wallet_state_manager import WalletStateManager
2225
from clvm_tools import binutils
@@ -476,8 +479,22 @@ async def respond_to_offer(self, file_path: Path) -> bool:
476479
zero_spend_list.append(spend_bundle)
477480
spend_bundle = SpendBundle.aggregate(zero_spend_list)
478481

479-
await self.wallet_state_manager.add_pending_transaction(
480-
spend_bundle, self.wallet_state_manager.main_wallet.wallet_info.id
482+
tx_record = TransactionRecord(
483+
confirmed_at_index=uint32(0),
484+
created_at_time=uint64(int(time.time())),
485+
to_puzzle_hash=token_bytes(),
486+
amount=uint64(0),
487+
fee_amount=uint64(0),
488+
incoming=False,
489+
confirmed=False,
490+
sent=uint32(0),
491+
spend_bundle=spend_bundle,
492+
additions=spend_bundle.additions(),
493+
removals=spend_bundle.removals(),
494+
wallet_id=uint32(1),
495+
sent_to=[],
481496
)
482497

498+
await self.wallet_state_manager.add_pending_transaction(tx_record)
499+
483500
return True

src/wallet/transaction_record.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from src.types.coin import Coin
55
from src.types.spend_bundle import SpendBundle
66
from src.types.sized_bytes import bytes32
7+
from src.util.hash import std_hash
78
from src.util.streamable import Streamable, streamable
89
from src.util.ints import uint32, uint64, uint8
910

@@ -35,4 +36,4 @@ class TransactionRecord(Streamable):
3536
def name(self) -> bytes32:
3637
if self.spend_bundle:
3738
return self.spend_bundle.name()
38-
return self.get_hash()
39+
return std_hash(bytes(self.to_puzzle_hash) + bytes(self.created_at_time) + bytes(self.amount))

0 commit comments

Comments
 (0)