From d799a928330b0a8709084e8ea6a431979949a37e Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Thu, 5 Aug 2021 15:24:10 -0400 Subject: [PATCH] Ask electrum to notify on new BTC addresses Otherwise we won't know when someone has paid... --- config.dhall.sample | 2 ++ lib/customer.rb | 5 ++++- lib/electrum.rb | 4 ++++ test/test_customer.rb | 7 +++++++ test/test_helper.rb | 3 ++- 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/config.dhall.sample b/config.dhall.sample index 370932a..006b65d 100644 --- a/config.dhall.sample +++ b/config.dhall.sample @@ -66,6 +66,8 @@ activation_amount = 15, credit_card_url = \(jid: Text) -> \(customer_id: Text) -> "https://pay.jmp.chat/${jid}/credit_cards?customer_id=${customer_id}", + electrum_notify_url = \(address: Text) -> \(customer_id: Text) -> + "https://pay.jmp.chat/electrum_notify?address=${address}&customer_id=${customer_id}", adr = "", interac = "", payable = "" diff --git a/lib/customer.rb b/lib/customer.rb index cba9192..92642b0 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -91,7 +91,10 @@ class Customer REDIS.spopsadd([ "jmp_available_btc_addresses", "jmp_customer_btc_addresses-#{customer_id}" - ]) + ]).then do |addr| + ELECTRUM.notify(addr, CONFIG[:electrum_notify_url].call(addr, customer_id)) + addr + end end protected def_delegator :@plan, :expires_at diff --git a/lib/electrum.rb b/lib/electrum.rb index b30922c..0f94bf3 100644 --- a/lib/electrum.rb +++ b/lib/electrum.rb @@ -34,6 +34,10 @@ class Electrum rpc_call(:get_tx_status, txid: tx_hash).then { |r| r["result"] } end + def notify(address, url) + rpc_call(:notify, address: address, URL: url).then { |r| r["result"] } + end + class Transaction def initialize(electrum, tx_hash, tx) @electrum = electrum diff --git a/test/test_customer.rb b/test/test_customer.rb index d41f88a..395abf2 100644 --- a/test/test_customer.rb +++ b/test/test_customer.rb @@ -5,6 +5,7 @@ require "customer" Customer::BLATHER = Minitest::Mock.new Customer::BRAINTREE = Minitest::Mock.new +Customer::ELECTRUM = Minitest::Mock.new Customer::REDIS = Minitest::Mock.new Customer::DB = Minitest::Mock.new CustomerPlan::DB = Minitest::Mock.new @@ -239,8 +240,14 @@ class CustomerTest < Minitest::Test EMPromise.resolve("testaddr"), [["jmp_available_btc_addresses", "jmp_customer_btc_addresses-test"]] ) + Customer::ELECTRUM.expect( + :notify, + EMPromise.resolve(nil), + ["testaddr", "http://notify.example.com"] + ) assert_equal "testaddr", Customer.new("test").add_btc_address.sync assert_mock Customer::REDIS + assert_mock Customer::ELECTRUM end em :test_add_btc_address end diff --git a/test/test_helper.rb b/test/test_helper.rb index fb0d673..e70b9fe 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -74,7 +74,8 @@ CONFIG = { USD: "merchant_usd" } }, - credit_card_url: ->(*) { "http://creditcard.example.com" } + credit_card_url: ->(*) { "http://creditcard.example.com" }, + electrum_notify_url: ->(*) { "http://notify.example.com" } }.freeze BLATHER = Class.new { -- 2.34.2