def on_openchannel(openchannel2, plugin, **kwargs): # We mirror what the peer does, wrt to funding amount ... amount = Millisatoshi(openchannel2['their_funding']) locktime = openchannel2['locktime'] if amount > plugin.max_fund: plugin.log("amount adjusted from {} to {}".format( amount, plugin.max_fund)) amount = plugin.max_fund if amount == 0: plugin.log("accepter_max_funding set to zero") return {'result': 'continue'} # ...unless they send us totally unacceptable feerates. feerate = find_feerate(openchannel2['funding_feerate_best'], openchannel2['funding_feerate_min'], openchannel2['funding_feerate_max'], openchannel2['feerate_our_min'], openchannel2['feerate_our_max']) # Their feerate range is out of bounds, we're not going to # participate. if not feerate: plugin.log("Declining to fund, no feerate found.") return {'result': 'continue'} funding = plugin.rpc.fundpsbt( int(amount.to_satoshi()), '{}perkw'.format(feerate), 0, # because we're the accepter!! reserve=True, locktime=locktime, minconf=0, min_witness_weight=110) psbt_obj = psbt_from_base64(funding['psbt']) excess = Millisatoshi(funding['excess_msat']) change_cost = Millisatoshi(124 * feerate) dust_limit = Millisatoshi(253 * 1000) if excess > (dust_limit + change_cost): addr = plugin.rpc.newaddr()['bech32'] change = excess - change_cost output = tx_output_init(change.to_whole_satoshi(), get_script(addr)) psbt_add_output_at(psbt_obj, 0, 0, output) psbt = psbt_to_base64(psbt_obj, 0) add_inflight(plugin, openchannel2['id'], openchannel2['channel_id'], psbt) plugin.log("contributing {} at feerate {}".format(amount, feerate)) return { 'result': 'continue', 'psbt': psbt, 'accepter_funding_msat': amount, 'funding_feerate': feerate }
def test_floats(): # test parsing amounts from floating number strings amount = Millisatoshi("0.01btc") assert amount.to_satoshi() == 10**6 amount = Millisatoshi("1.01btc") assert amount.to_satoshi() == 10**8 + 10**6 amount = Millisatoshi("0.1sat") assert int(amount) == 100 amount = Millisatoshi("0.01sat") assert int(amount) == 10 amount = Millisatoshi("1.1sat") assert int(amount) == 1100 # test floating point arithmetic amount = Millisatoshi("1000msat") * 0.1 assert int(amount) == 100 amount = Millisatoshi("100msat") * 0.1 assert int(amount) == 10 amount = Millisatoshi("10msat") * 0.1 assert int(amount) == 1
def test_floats(): # test parsing amounts from floating number strings amount = Millisatoshi("0.01btc") assert amount.to_satoshi() == 10**6 amount = Millisatoshi("1.01btc") assert amount.to_satoshi() == 10**8 + 10**6 amount = Millisatoshi("0.1sat") assert int(amount) == 100 amount = Millisatoshi("0.01sat") assert int(amount) == 10 amount = Millisatoshi("1.1sat") assert int(amount) == 1100 # test floating point arithmetic amount = Millisatoshi("1000msat") * 0.1 assert int(amount) == 100 # sub millisatoshi are not a concept yet with pytest.raises(ValueError, match='Millisatoshi must be a whole number'): amount = Millisatoshi("0.000000000001btc") with pytest.raises(ValueError, match='Millisatoshi must be a whole number'): amount = Millisatoshi("0.0001sat") with pytest.raises(ValueError, match='Millisatoshi must be a whole number'): amount = Millisatoshi("0.1msat")
def on_openchannel(openchannel2, plugin, **kwargs): # We mirror what the peer does, wrt to funding amount ... amount = Millisatoshi(openchannel2['their_funding']) locktime = openchannel2['locktime'] if amount > plugin.max_fund: plugin.log("amount adjusted from {} to {}".format( amount, plugin.max_fund)) amount = plugin.max_fund if amount == 0: plugin.log("accepter_max_funding set to zero") return {'result': 'continue'} # ...unless they send us totally unacceptable feerates. feerate = find_feerate(openchannel2['funding_feerate_best'], openchannel2['funding_feerate_min'], openchannel2['funding_feerate_max'], openchannel2['feerate_our_min'], openchannel2['feerate_our_max']) # Their feerate range is out of bounds, we're not going to # participate. if not feerate: plugin.log("Declining to fund, no feerate found.") return {'result': 'continue'} funding = plugin.rpc.fundpsbt( int(amount.to_satoshi()), '{}perkw'.format(feerate), 0, # because we're the accepter!! reserve=True, locktime=locktime, minconf=0, min_witness_weight=110, excess_as_change=True) add_inflight(plugin, openchannel2['id'], openchannel2['channel_id'], funding['psbt']) plugin.log("contributing {} at feerate {}".format(amount, feerate)) return { 'result': 'continue', 'psbt': funding['psbt'], 'accepter_funding_msat': amount, 'funding_feerate': feerate }