Ask electrum to notify on new BTC addresses

Stephen Paul Weber created

Otherwise we won't know when someone has paid...

Change summary

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(-)

Detailed changes

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 = ""

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

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

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

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 {