swaps: cache pairs to file
This commit is contained in:
@@ -148,8 +148,8 @@ class SwapManager(Logger):
|
|||||||
self.normal_fee = 0
|
self.normal_fee = 0
|
||||||
self.lockup_fee = 0
|
self.lockup_fee = 0
|
||||||
self.percentage = 0
|
self.percentage = 0
|
||||||
self.min_amount = 0
|
self._min_amount = None
|
||||||
self._max_amount = 0
|
self._max_amount = None
|
||||||
self.wallet = wallet
|
self.wallet = wallet
|
||||||
self.lnworker = lnworker
|
self.lnworker = lnworker
|
||||||
|
|
||||||
@@ -170,6 +170,8 @@ class SwapManager(Logger):
|
|||||||
self.api_url = API_URL_TESTNET
|
self.api_url = API_URL_TESTNET
|
||||||
else:
|
else:
|
||||||
self.api_url = API_URL_REGTEST
|
self.api_url = API_URL_REGTEST
|
||||||
|
# init default min & max
|
||||||
|
self.init_min_max_values()
|
||||||
|
|
||||||
def start_network(self, *, network: 'Network', lnwatcher: 'LNWalletWatcher'):
|
def start_network(self, *, network: 'Network', lnwatcher: 'LNWalletWatcher'):
|
||||||
assert network
|
assert network
|
||||||
@@ -491,19 +493,40 @@ class SwapManager(Logger):
|
|||||||
raise SwapServerError() from e
|
raise SwapServerError() from e
|
||||||
# we assume server response is well-formed; otherwise let an exception propagate to the crash reporter
|
# we assume server response is well-formed; otherwise let an exception propagate to the crash reporter
|
||||||
pairs = json.loads(response)
|
pairs = json.loads(response)
|
||||||
|
# cache data to disk
|
||||||
|
with open(self.pairs_filename(), 'w', encoding='utf-8') as f:
|
||||||
|
f.write(json.dumps(pairs))
|
||||||
fees = pairs['pairs']['BTC/BTC']['fees']
|
fees = pairs['pairs']['BTC/BTC']['fees']
|
||||||
self.percentage = fees['percentage']
|
self.percentage = fees['percentage']
|
||||||
self.normal_fee = fees['minerFees']['baseAsset']['normal']
|
self.normal_fee = fees['minerFees']['baseAsset']['normal']
|
||||||
self.lockup_fee = fees['minerFees']['baseAsset']['reverse']['lockup']
|
self.lockup_fee = fees['minerFees']['baseAsset']['reverse']['lockup']
|
||||||
limits = pairs['pairs']['BTC/BTC']['limits']
|
limits = pairs['pairs']['BTC/BTC']['limits']
|
||||||
self.min_amount = limits['minimal']
|
self._min_amount = limits['minimal']
|
||||||
self._max_amount = limits['maximal']
|
self._max_amount = limits['maximal']
|
||||||
|
|
||||||
|
def pairs_filename(self):
|
||||||
|
return os.path.join(self.wallet.config.path, 'swap_pairs')
|
||||||
|
|
||||||
|
def init_min_max_values(self):
|
||||||
|
# use default values if we never requested pairs
|
||||||
|
try:
|
||||||
|
with open(self.pairs_filename(), 'r', encoding='utf-8') as f:
|
||||||
|
pairs = json.loads(f.read())
|
||||||
|
limits = pairs['pairs']['BTC/BTC']['limits']
|
||||||
|
self._min_amount = limits['minimal']
|
||||||
|
self._max_amount = limits['maximal']
|
||||||
|
except:
|
||||||
|
self._min_amount = 10000
|
||||||
|
self._max_amount = 10000000
|
||||||
|
|
||||||
def get_max_amount(self):
|
def get_max_amount(self):
|
||||||
return self._max_amount
|
return self._max_amount
|
||||||
|
|
||||||
|
def get_min_amount(self):
|
||||||
|
return self._min_amount
|
||||||
|
|
||||||
def check_invoice_amount(self, x):
|
def check_invoice_amount(self, x):
|
||||||
return x >= self.min_amount and x <= self._max_amount
|
return x >= self.get_min_amount() and x <= self.get_max_amount()
|
||||||
|
|
||||||
def _get_recv_amount(self, send_amount: Optional[int], *, is_reverse: bool) -> Optional[int]:
|
def _get_recv_amount(self, send_amount: Optional[int], *, is_reverse: bool) -> Optional[int]:
|
||||||
"""For a given swap direction and amount we send, returns how much we will receive.
|
"""For a given swap direction and amount we send, returns how much we will receive.
|
||||||
@@ -646,5 +669,5 @@ class SwapManager(Logger):
|
|||||||
max_recv_amt_ln = int(self.num_sats_can_receive())
|
max_recv_amt_ln = int(self.num_sats_can_receive())
|
||||||
max_amt_ln = int(min(max_swap_amt_ln, max_recv_amt_ln))
|
max_amt_ln = int(min(max_swap_amt_ln, max_recv_amt_ln))
|
||||||
max_amt_oc = self.get_send_amount(max_amt_ln, is_reverse=False) or 0
|
max_amt_oc = self.get_send_amount(max_amt_ln, is_reverse=False) or 0
|
||||||
min_amt_oc = self.get_send_amount(self.min_amount, is_reverse=False) or 0
|
min_amt_oc = self.get_send_amount(self.get_min_amount(), is_reverse=False) or 0
|
||||||
return max_amt_oc if max_amt_oc >= min_amt_oc else None
|
return max_amt_oc if max_amt_oc >= min_amt_oc else None
|
||||||
|
|||||||
Reference in New Issue
Block a user