Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Link public on-chain wallets in the demo family (#4618)

Artem Danilov28175807fe46
diff --git a/app/models/demo/generator.rb b/app/models/demo/generator.rb
index 97cef19b0b..41c70fed3e 100644
--- a/app/models/demo/generator.rb
+++ b/app/models/demo/generator.rb
@@ -5,6 +5,21 @@ class Demo::Generator
# reference Demo::Generator::DEMO_STRIPE_SUBSCRIPTION_ID keep working.
DEMO_STRIPE_SUBSCRIPTION_ID = Subscription::DEMO_STRIPE_ID
+ # Organic Maps' published donation addresses (organicmaps.app/donate): real,
+ # public and modest. Every sync re-reads a wallet from its chain, so a made-up
+ # address would drop to zero on the demo user's first sync.
+ ONCHAIN_DEMO_WALLETS = [
+ { chain: "bitcoin", address: "bc1qxnn9j9z5xywqz5sdqxaq327vw0x22nhsxc67ld", tokens: [] },
+ { chain: "ethereum", address: "0xC36A638DD2274BDe6097DFD62B3EB5Aa8A67Ec54", tokens: [
+ { symbol: "USDC", name: "USD Coin", decimals: 6, contract: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" },
+ { symbol: "USDT", name: "Tether USD", decimals: 6, contract: "0xdAC17F958D2ee523a2206206994597C13D831ec7" }
+ ] },
+ { chain: "solana", address: "9Fcm6Gw5PsivSCcpxCh9GVfj9jcPgQ8Q5mZJhsEuRHJV", tokens: [
+ { symbol: "USDC", name: "USD Coin", decimals: 6, contract: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" },
+ { symbol: "USDT", name: "Tether USD", decimals: 6, contract: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB" }
+ ] }
+ ].freeze
+
# @param seed [Integer, String, nil] Seed value used to initialise the internal PRNG. If nil, the ENV variable DEMO_DATA_SEED will
# be honoured and default to a random seed when not present.
#
@@ -110,6 +125,11 @@ class Demo::Generator
puts "🎯 Seeding goals..."
generate_goals!(family)
+ # Only the demo family: every family with these wallets reads the same
+ # addresses from the public explorers on each sync.
+ puts "πŸ‘› Linking public on-chain wallets..."
+ link_onchain_demo_wallets!(family)
+
puts "πŸ”‘ Creating monitoring API key..."
create_monitoring_api_key!(family)
@@ -1051,6 +1071,24 @@ class Demo::Generator
end
end
+ # Linked the way a user links them, from an empty snapshot instead of a chain
+ # read, so generating stays offline. The item sync the linker queues fills in
+ # the real balances, history and prices.
+ #
+ # One transaction: jobs are enqueued after commit, so the sync the first link
+ # queues starts with every wallet linked rather than with the first alone.
+ def link_onchain_demo_wallets!(family)
+ item = family.onchain_wallet_item!
+ item.transaction do
+ ONCHAIN_DEMO_WALLETS.each do |wallet|
+ chain = Onchain::Chains.find!(wallet[:chain])
+ assets = [ chain.native_asset(quantity: 0) ] + wallet[:tokens].map { |token| chain.token_asset(**token, quantity: 0) }
+ OnchainWalletItem::WalletLinker.new(item, chain: chain.key, address: Onchain::Chains.canonical_address(chain.key, wallet[:address]))
+ .link(snapshot: Onchain::Snapshot.new(assets: assets, movements: []), selected_keys: assets.map { |asset| OnchainWalletItem::TokenReview.key_for(asset) })
+ end
+ end
+ end
+
# ---------------------------------------------------------------------------
# Deterministic helper methods
# ---------------------------------------------------------------------------
diff --git a/test/models/demo/generator_test.rb b/test/models/demo/generator_test.rb
index d66972f72e..08857518ae 100644
--- a/test/models/demo/generator_test.rb
+++ b/test/models/demo/generator_test.rb
@@ -28,6 +28,25 @@ class Demo::GeneratorTest < ActiveSupport::TestCase
end
end
+ test "default demo data links the public on-chain wallets without reading a chain" do
+ # WebMock refuses any request, so this also proves generation stays offline.
+ User.find_by!(email: "[email protected]").update!(email: "[email protected]")
+ generator = Demo::Generator.new(seed: 42)
+ %i[create_realistic_categories! create_realistic_accounts! create_realistic_transactions!
+ generate_budget_auto_fill! generate_goals! create_monitoring_api_key!].each do |step|
+ generator.stubs(step)
+ end
+
+ generator.generate_default_data!(skip_clear: true)
+
+ item = User.find_by!(email: "[email protected]").family.onchain_wallet_item
+ expected = Demo::Generator::ONCHAIN_DEMO_WALLETS.map { |wallet| [ wallet[:chain], Onchain::Chains.canonical_address(wallet[:chain], wallet[:address]) ] }
+ assert_equal expected.sort, item.wallet_keys.sort
+ assert_equal %w[BTC ETH SOL USDC USDC USDT USDT], item.onchain_wallet_accounts.pluck(:symbol).sort
+ assert item.onchain_wallet_accounts.all? { |row| row.account&.accountable_type == "Crypto" }
+ assert_equal 1, item.syncs.count, "the item sync fills in the real balances"
+ end
+
test "production sample reset includes Wallet in the normal accounts and transactions phases" do
Rails.stubs(:env).returns(ActiveSupport::EnvironmentInquirer.new("production"))
generator = Demo::Generator.new(seed: 42)