183
183
GetPrivateKeyFormat ,
184
184
GetPrivateKeyResponse ,
185
185
GetPublicKeysResponse ,
186
+ GetSpendableCoins ,
187
+ GetSpendableCoinsResponse ,
186
188
GetSyncStatusResponse ,
187
189
GetTimestampForHeight ,
188
190
GetTimestampForHeightResponse ,
@@ -1763,43 +1765,26 @@ async def select_coins(
1763
1765
1764
1766
return SelectCoinsResponse (coins = list (selected_coins ))
1765
1767
1766
- async def get_spendable_coins (self , request : dict [str , Any ]) -> EndpointResult :
1768
+ @marshal
1769
+ async def get_spendable_coins (self , request : GetSpendableCoins ) -> GetSpendableCoinsResponse :
1767
1770
if await self .service .wallet_state_manager .synced () is False :
1768
1771
raise ValueError ("Wallet needs to be fully synced before getting all coins" )
1769
1772
1770
- wallet_id = uint32 (request ["wallet_id" ])
1771
- min_coin_amount = uint64 (request .get ("min_coin_amount" , 0 ))
1772
- max_coin_amount : uint64 = uint64 (request .get ("max_coin_amount" , 0 ))
1773
- if max_coin_amount == 0 :
1774
- max_coin_amount = uint64 (self .service .wallet_state_manager .constants .MAX_COIN_AMOUNT )
1775
- excluded_coin_amounts : Optional [list [uint64 ]] = request .get ("excluded_coin_amounts" )
1776
- if excluded_coin_amounts is not None :
1777
- excluded_coin_amounts = [uint64 (a ) for a in excluded_coin_amounts ]
1778
- else :
1779
- excluded_coin_amounts = []
1780
- excluded_coins_input : Optional [dict [str , dict [str , Any ]]] = request .get ("excluded_coins" )
1781
- if excluded_coins_input is not None :
1782
- excluded_coins = [Coin .from_json_dict (json_coin ) for json_coin in excluded_coins_input .values ()]
1783
- else :
1784
- excluded_coins = []
1785
- excluded_coin_ids_input : Optional [list [str ]] = request .get ("excluded_coin_ids" )
1786
- if excluded_coin_ids_input is not None :
1787
- excluded_coin_ids = [bytes32 .from_hexstr (hex_id ) for hex_id in excluded_coin_ids_input ]
1788
- else :
1789
- excluded_coin_ids = []
1790
1773
state_mgr = self .service .wallet_state_manager
1791
- wallet = state_mgr .wallets [wallet_id ]
1774
+ wallet = state_mgr .wallets [request . wallet_id ]
1792
1775
async with state_mgr .lock :
1793
- all_coin_records = await state_mgr .coin_store .get_unspent_coins_for_wallet (wallet_id )
1776
+ all_coin_records = await state_mgr .coin_store .get_unspent_coins_for_wallet (request . wallet_id )
1794
1777
if wallet .type () in {WalletType .CAT , WalletType .CRCAT , WalletType .RCAT }:
1795
1778
assert isinstance (wallet , CATWallet )
1796
1779
spendable_coins : list [WalletCoinRecord ] = await wallet .get_cat_spendable_coins (all_coin_records )
1797
1780
else :
1798
- spendable_coins = list (await state_mgr .get_spendable_coins_for_wallet (wallet_id , all_coin_records ))
1781
+ spendable_coins = list (
1782
+ await state_mgr .get_spendable_coins_for_wallet (request .wallet_id , all_coin_records )
1783
+ )
1799
1784
1800
1785
# Now we get the unconfirmed transactions and manually derive the additions and removals.
1801
1786
unconfirmed_transactions : list [TransactionRecord ] = await state_mgr .tx_store .get_unconfirmed_for_wallet (
1802
- wallet_id
1787
+ request . wallet_id
1803
1788
)
1804
1789
unconfirmed_removal_ids : dict [bytes32 , uint64 ] = {
1805
1790
coin .name (): transaction .created_at_time
@@ -1810,7 +1795,7 @@ async def get_spendable_coins(self, request: dict[str, Any]) -> EndpointResult:
1810
1795
coin
1811
1796
for transaction in unconfirmed_transactions
1812
1797
for coin in transaction .additions
1813
- if await state_mgr .does_coin_belong_to_wallet (coin , wallet_id )
1798
+ if await state_mgr .does_coin_belong_to_wallet (coin , request . wallet_id )
1814
1799
]
1815
1800
valid_spendable_cr : list [CoinRecord ] = []
1816
1801
unconfirmed_removals : list [CoinRecord ] = []
@@ -1820,23 +1805,26 @@ async def get_spendable_coins(self, request: dict[str, Any]) -> EndpointResult:
1820
1805
for coin_record in spendable_coins : # remove all the unconfirmed coins, exclude coins and dust.
1821
1806
if coin_record .name () in unconfirmed_removal_ids :
1822
1807
continue
1823
- if coin_record .coin in excluded_coins :
1824
- continue
1825
- if coin_record .name () in excluded_coin_ids :
1808
+ if request .excluded_coin_ids is not None and coin_record .coin .name () in request .excluded_coin_ids :
1826
1809
continue
1827
- if coin_record .coin .amount < min_coin_amount or coin_record .coin .amount > max_coin_amount :
1810
+ if (request .min_coin_amount is not None and coin_record .coin .amount < request .min_coin_amount ) or (
1811
+ request .max_coin_amount is not None and coin_record .coin .amount > request .max_coin_amount
1812
+ ):
1828
1813
continue
1829
- if coin_record .coin .amount in excluded_coin_amounts :
1814
+ if (
1815
+ request .excluded_coin_amounts is not None
1816
+ and coin_record .coin .amount in request .excluded_coin_amounts
1817
+ ):
1830
1818
continue
1831
1819
c_r = await state_mgr .get_coin_record_by_wallet_record (coin_record )
1832
1820
assert c_r is not None and c_r .coin == coin_record .coin # this should never happen
1833
1821
valid_spendable_cr .append (c_r )
1834
1822
1835
- return {
1836
- " confirmed_records" : [ cr . to_json_dict () for cr in valid_spendable_cr ] ,
1837
- " unconfirmed_removals" : [ cr . to_json_dict () for cr in unconfirmed_removals ] ,
1838
- " unconfirmed_additions" : [ coin . to_json_dict () for coin in unconfirmed_additions ] ,
1839
- }
1823
+ return GetSpendableCoinsResponse (
1824
+ confirmed_records = valid_spendable_cr ,
1825
+ unconfirmed_removals = unconfirmed_removals ,
1826
+ unconfirmed_additions = unconfirmed_additions ,
1827
+ )
1840
1828
1841
1829
async def get_coin_records_by_names (self , request : dict [str , Any ]) -> EndpointResult :
1842
1830
if await self .service .wallet_state_manager .synced () is False :
0 commit comments