~singpolyma/jmp-pay

9cd0b87d337ec347ef6bb0b1e50aa9bc3b48a693 — Stephen Paul Weber 9 months ago 3df6bdb
Script to create Redis key of BTC addresses available to the app
2 files changed, 39 insertions(+), 0 deletions(-)

A bin/get_available_addresses
M lib/redis_addresses.rb
A bin/get_available_addresses => bin/get_available_addresses +26 -0
@@ 0,0 1,26 @@
#!/usr/bin/ruby
# frozen_string_literal: true

require "redis"
require "dhall"
require "set"
require_relative "../lib/redis_addresses"
require_relative "../lib/electrum"

config =
	Dhall::Coder
	.new(safe: Dhall::Coder::JSON_LIKE + [Symbol, Proc])
	.load(ARGV[0], transform_keys: :to_sym)

redis = Redis.new
electrum = Electrum.new(**config)

addresses = Set.new(electrum.listaddresses)

RedisBtcAddresses.each_user(redis) do |_, addrs|
	addresses.subtract(addrs)
end

unless addresses.empty?
	redis.sadd("jmp_available_btc_addresses", addresses.to_a)
end

M lib/redis_addresses.rb => lib/redis_addresses.rb +13 -0
@@ 22,3 22,16 @@ def get_addresses_with_users(redis)

	addrs
end

module RedisBtcAddresses
	def self.each_user(redis)
		# I picked 1000 because it made a relatively trivial case take 15 seconds
		#   instead of forever.
		# Basically it's "how long does each command take"
		# The lower it is (default is 10), it will go back and forth to the client a
		#   ton
		redis.scan_each(match: "jmp_customer_btc_addresses-*", count: 1000) do |key|
			yield key, redis.smembers(key)
		end
	end
end