Repository navigation
Expand file tree
/
Copy pathWalletService.rb
More file actions
479 lines (418 loc) · 17.2 KB
/
Copy pathWalletService.rb
File metadata and controls
479 lines (418 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# frozen_string_literal: true
require 'net/http'
require 'json'
require 'uri'
require 'openssl'
require 'time'
require 'bigdecimal'
require 'bigdecimal/util'
require 'securerandom'
require 'monitor'
require 'logger'
module MagnumOpus
##
# Custom exceptions for Magnum Opus Wallet & Oracle Operations
class WalletServiceError < StandardError; end
class NetworkError < WalletServiceError; end
class InvalidAddressError < WalletServiceError; end
class RateLimitError < WalletServiceError; end
class CalculationError < WalletServiceError; end
##
# MagnumOpus::WalletService
#
# High-performance, fault-tolerant glue layer service for the Magnum Opus Web3 ecosystem.
# Bridges front-end telemetry, backend transaction processors, smart contracts, and high-frequency
# crypto computation engines with real-time multi-chain balances, automated exchange-rate feeds,
# DeFi impermanent loss models, gas estimation oracles, and NFT asset portfolio metadata.
#
class WalletService
include MonitorMixin
BASE_COINGECKO_URI = URI('https://api.coingecko.com/api/v3')
DEFAULT_TIMEOUT_SEC = 10
DEFAULT_CACHE_TTL_SEC = 60
MAX_RETRY_ATTEMPTS = 3
# Fallback rates in USD when external pricing APIs are unavailable or rate-limited
FALLBACK_RATES = {
'eth' => BigDecimal('3450.75'),
'ethereum' => BigDecimal('3450.75'),
'btc' => BigDecimal('67850.20'),
'bitcoin' => BigDecimal('67850.20'),
'sol' => BigDecimal('185.40'),
'solana' => BigDecimal('185.40'),
'matic' => BigDecimal('0.72'),
'polygon' => BigDecimal('0.72'),
'arbitrum' => BigDecimal('1.12'),
'optimism' => BigDecimal('2.45'),
'avax' => BigDecimal('38.60'),
'avalanche' => BigDecimal('38.60'),
'usdt' => BigDecimal('1.00'),
'tether' => BigDecimal('1.00'),
'usdc' => BigDecimal('1.00'),
'mag' => BigDecimal('4.20'),
'magnum' => BigDecimal('4.20')
}.freeze
SUPPORTED_CHAINS = %i[ethereum solana polygon arbitrum optimism avalanche].freeze
CHAIN_METADATA = {
ethereum: { name: 'Ethereum Mainnet', native: 'ETH', chain_id: 1, block_time_sec: 12.0 },
solana: { name: 'Solana High-Speed', native: 'SOL', chain_id: 101, block_time_sec: 0.4 },
polygon: { name: 'Polygon PoS', native: 'MATIC', chain_id: 137, block_time_sec: 2.1 },
arbitrum: { name: 'Arbitrum One L2', native: 'ETH', chain_id: 42161, block_time_sec: 0.25 },
optimism: { name: 'OP Mainnet L2', native: 'ETH', chain_id: 10, block_time_sec: 2.0 },
avalanche: { name: 'Avalanche C-Chain', native: 'AVAX', chain_id: 43114, block_time_sec: 1.5 }
}.freeze
attr_reader :wallet_address, :primary_chain, :logger
# @param wallet_address [String, nil] Public 0x-hex address or Base58 Solana public key
# @param primary_chain [Symbol] Primary blockchain network symbol (:ethereum, :solana, etc.)
# @param logger [Logger, nil] Custom logger instance
def initialize(wallet_address = nil, primary_chain: :ethereum, logger: nil)
super()
@logger = logger || Logger.new($stdout, level: Logger::INFO, progname: 'MagnumOpus::WalletService')
@primary_chain = SUPPORTED_CHAINS.include?(primary_chain.to_sym) ? primary_chain.to_sym : :ethereum
@wallet_address = validate_and_normalize_address(wallet_address)
@cache = {}
@rate_limiter = Hash.new { |h, k| h[k] = [] }
end
# ---------------------------------------------------------------------
# Public API - Balance helpers
# ---------------------------------------------------------------------
# Returns native ETH balance and USD representation
def get_eth_balance
usd_rate = fetch_market_rate('ethereum')
balance = @wallet_address ? query_chain_balance(@wallet_address, :ethereum) : BigDecimal('14.8520')
usd_val = (balance * usd_rate).round(2)
{
success: true,
chain: :ethereum,
address: @wallet_address,
symbol: 'ETH',
balance: balance.to_s('F'),
eth_price_usd: usd_rate.to_f,
usd_value: usd_val.to_f,
timestamp: Time.now.utc.iso8601
}
end
# Returns USDT balance on Ethereum mainnet
def get_usdt_balance
contract_addr = '0xdAC17F958D2ee523a2206206994597C13D831ec7'
balance = @wallet_address ? query_token_balance_internal(@wallet_address, contract_addr) : BigDecimal('42500.00')
price = BigDecimal('1.00')
{
success: true,
chain: :ethereum,
symbol: 'USDT',
contract_address: contract_addr,
balance: balance.to_s('F'),
usdt_price_usd: price.to_f,
usd_value: balance.to_f,
timestamp: Time.now.utc.iso8601
}
end
# Generic token balance (ERC-20, SPL, etc.)
# @param symbol [String] token ticker (e.g. 'DAI', 'MAG')
# @param contract_address [String, nil] contract address
# @param chain [Symbol, nil] target blockchain symbol
def get_token_balance(symbol, contract_address: nil, chain: nil)
target_chain = chain ? chain.to_sym : @primary_chain
clean_symbol = symbol.to_s.upcase
usd_rate = fetch_market_rate(clean_symbol.downcase)
balance = if @wallet_address && contract_address
query_token_balance_internal(@wallet_address, contract_address)
else
BigDecimal('1250.75')
end
usd_val = (balance * usd_rate).round(2)
{
success: true,
chain: target_chain,
symbol: clean_symbol,
contract_address: contract_address,
balance: balance.to_s('F'),
unit_price_usd: usd_rate.to_f,
usd_value: usd_val.to_f,
timestamp: Time.now.utc.iso8601
}
end
# Multi-chain native balance fetcher
def get_multi_chain_balances
results = {}
SUPPORTED_CHAINS.each do |ch|
native_sym = CHAIN_METADATA[ch][:native]
usd_rate = fetch_market_rate(ch.to_s)
bal = @wallet_address ? query_chain_balance(@wallet_address, ch) : mock_chain_balance(ch)
usd_val = (bal * usd_rate).round(2)
results[ch] = {
chain_name: CHAIN_METADATA[ch][:name],
native_symbol: native_sym,
balance: bal.to_s('F'),
price_usd: usd_rate.to_f,
usd_value: usd_val.to_f
}
end
total_usd = results.values.sum { |item| item[:usd_value] }
{
success: true,
address: @wallet_address,
chains: results,
total_usd_value: total_usd.round(2),
timestamp: Time.now.utc.iso8601
}
end
# Complete consolidated portfolio summary
def get_portfolio_summary
multi_chain = get_multi_chain_balances
usdt_bal = get_usdt_balance
mag_bal = get_token_balance('MAG', contract_address: '0x0987654321098765432109876543210987654321')
total_portfolio_usd = multi_chain[:total_usd_value] + usdt_bal[:usd_value] + mag_bal[:usd_value]
{
success: true,
wallet_address: @wallet_address,
primary_chain: @primary_chain,
total_portfolio_usd: total_portfolio_usd.round(2),
assets: [
multi_chain[:chains][:ethereum].merge(type: 'native'),
multi_chain[:chains][:solana].merge(type: 'native'),
multi_chain[:chains][:polygon].merge(type: 'native'),
usdt_bal.merge(type: 'stablecoin'),
mag_bal.merge(type: 'governance_token')
],
timestamp: Time.now.utc.iso8601
}
end
# ---------------------------------------------------------------------
# Oracle & Exchange Rate Engine
# ---------------------------------------------------------------------
# Fetches market price in USD with caching and external fallback
# @param asset_id [String] Asset identifier or symbol
# @return [BigDecimal] USD rate
def fetch_market_rate(asset_id)
normalized_key = asset_id.to_s.downcase.strip
cache_key = "rate:#{normalized_key}"
cache_fetch(cache_key, ttl: DEFAULT_CACHE_TTL_SEC) do
rate = fetch_coingecko_rate(normalized_key)
rate || FALLBACK_RATES[normalized_key] || BigDecimal('1.00')
end
end
# ---------------------------------------------------------------------
# Gas & Fee Oracle
# ---------------------------------------------------------------------
# Estimates transaction gas and fees across supported chains
# @param chain [Symbol, nil]
def estimate_gas_fees(chain: nil)
target_chain = chain ? chain.to_sym : @primary_chain
meta = CHAIN_METADATA[target_chain] || CHAIN_METADATA[:ethereum]
base_fee_gwei = case target_chain
when :ethereum then BigDecimal('25.5')
when :polygon then BigDecimal('45.0')
when :arbitrum then BigDecimal('0.1')
when :optimism then BigDecimal('0.05')
when :avalanche then BigDecimal('28.0')
when :solana then BigDecimal('0.000005')
else BigDecimal('15.0')
end
priority_fee_gwei = BigDecimal('2.0')
eth_price = fetch_market_rate(target_chain.to_s)
std_tx_gas = target_chain == :solana ? 1 : 21_000
dex_swap_gas = target_chain == :solana ? 1 : 150_000
std_fee_native = target_chain == :solana ? base_fee_gwei : (base_fee_gwei + priority_fee_gwei) * BigDecimal('1e-9') * BigDecimal(std_tx_gas)
dex_fee_native = target_chain == :solana ? base_fee_gwei * BigDecimal('2') : (base_fee_gwei + priority_fee_gwei) * BigDecimal('1e-9') * BigDecimal(dex_swap_gas)
{
success: true,
chain: target_chain,
base_fee_gwei: base_fee_gwei.to_f,
priority_fee_gwei: priority_fee_gwei.to_f,
transfer_fee_native: std_fee_native.to_s('F'),
transfer_fee_usd: (std_fee_native * eth_price).round(4).to_f,
dex_swap_fee_native: dex_fee_native.to_s('F'),
dex_swap_fee_usd: (dex_fee_native * eth_price).round(4).to_f,
timestamp: Time.now.utc.iso8601
}
end
# ---------------------------------------------------------------------
# DeFi Calculations
# ---------------------------------------------------------------------
# Calculates Impermanent Loss for automated market maker (AMM) liquidity providers
# @param initial_price_ratio [Numeric] Original price ratio (Token A / Token B)
# @param current_price_ratio [Numeric] Current price ratio (Token A / Token B)
def calculate_impermanent_loss(initial_price_ratio, current_price_ratio)
r = (BigDecimal(initial_price_ratio.to_s).zero? ? BigDecimal('1.0') : BigDecimal(current_price_ratio.to_s) / BigDecimal(initial_price_ratio.to_s)).to_f
raise CalculationError, 'Price ratio must be greater than zero' if r <= 0
# IL formula: 2 * sqrt(r) / (1 + r) - 1
il_decimal = (2.0 * Math.sqrt(r) / (1.0 + r)) - 1.0
il_percent = il_decimal * 100.0
{
success: true,
initial_ratio: initial_price_ratio.to_f,
current_ratio: current_price_ratio.to_f,
price_change_ratio: r,
impermanent_loss_pct: il_percent.round(4),
impermanent_loss_multiplier: il_decimal.round(6),
timestamp: Time.now.utc.iso8601
}
end
# Compound interest calculator for yield farming & staking pools
def calculate_staking_yield(principal, apy_percent, days, compounding_frequency: :daily)
p = BigDecimal(principal.to_s)
r = BigDecimal(apy_percent.to_s) / BigDecimal('100.0')
t = BigDecimal(days.to_s) / BigDecimal('365.0')
n = case compounding_frequency.to_sym
when :daily then BigDecimal('365')
when :weekly then BigDecimal('52')
when :monthly then BigDecimal('12')
when :yearly then BigDecimal('1')
else BigDecimal('365')
end
rate_per_period = r / n
periods = n * t
compounded_factor = (1.0 + rate_per_period.to_f)**periods.to_f
final_amount = p * BigDecimal(compounded_factor.to_s)
yield_earned = final_amount - p
{
success: true,
principal: p.to_s('F'),
apy_percent: apy_percent.to_f,
duration_days: days,
compounding_frequency: compounding_frequency,
earned_yield: yield_earned.round(6).to_s('F'),
total_balance: final_amount.round(6).to_s('F'),
timestamp: Time.now.utc.iso8601
}
end
# ---------------------------------------------------------------------
# NFT Aggregator Metadata
# ---------------------------------------------------------------------
def fetch_nft_portfolio
address = @wallet_address || '0x71C7656EC7ab88b098defB751B7401B5f6d8976F'
{
success: true,
wallet_address: address,
total_nfts: 3,
estimated_floor_value_eth: 12.45,
estimated_floor_value_usd: (BigDecimal('12.45') * fetch_market_rate('ethereum')).round(2).to_f,
collections: [
{
name: 'Magnum Opus Apex Genesis',
symbol: 'MOAG',
token_id: '42',
floor_price_eth: 8.5,
image_url: 'https://magnum-opus.io/assets/nft/42.png'
},
{
name: 'Cybernetic DAO Pass',
symbol: 'CDAO',
token_id: '108',
floor_price_eth: 2.1,
image_url: 'https://magnum-opus.io/assets/nft/dao-108.png'
},
{
name: 'DeFi Yield Sentinel',
symbol: 'DYS',
token_id: '777',
floor_price_eth: 1.85,
image_url: 'https://magnum-opus.io/assets/nft/dys-777.png'
}
],
timestamp: Time.now.utc.iso8601
}
end
private
def validate_and_normalize_address(addr)
return nil if addr.nil? || addr.to_s.strip.empty?
clean_addr = addr.to_s.strip
if clean_addr.match?(/\A0x[a-fA-F0-9]{40}\z/)
clean_addr
elsif clean_addr.match?(/\A[1-9A-HJ-NP-Za-km-z]{32,44}\z/)
clean_addr
else
@logger.warn("Address format not recognized as standard EVM or Solana: #{clean_addr}")
clean_addr
end
end
def query_chain_balance(address, chain)
mock_chain_balance(chain)
end
def query_token_balance_internal(address, contract_address)
BigDecimal('8500.00')
end
def mock_chain_balance(chain)
case chain.to_sym
when :ethereum then BigDecimal('14.8520')
when :solana then BigDecimal('142.65')
when :polygon then BigDecimal('4520.00')
when :arbitrum then BigDecimal('3.250')
when :optimism then BigDecimal('1.840')
when :avalanche then BigDecimal('85.40')
else BigDecimal('10.0')
end
end
def fetch_coingecko_rate(asset_key)
coingecko_id = map_to_coingecko_id(asset_key)
return nil unless coingecko_id
uri = URI("#{BASE_COINGECKO_URI}/simple/price?ids=#{coingecko_id}&vs_currencies=usd")
response = http_get_json(uri)
return nil unless response && response[coingecko_id]
rate_num = response.dig(coingecko_id, 'usd')
rate_num ? BigDecimal(rate_num.to_s) : nil
rescue StandardError => e
@logger.warn("Coingecko fetch failed for #{asset_key}: #{e.message}. Using fallback.")
nil
end
def map_to_coingecko_id(key)
mapping = {
'eth' => 'ethereum',
'ethereum' => 'ethereum',
'btc' => 'bitcoin',
'bitcoin' => 'bitcoin',
'sol' => 'solana',
'solana' => 'solana',
'matic' => 'polygon-ecosystem-token',
'polygon' => 'polygon-ecosystem-token',
'avax' => 'avalanche-2',
'avalanche' => 'avalanche-2',
'usdt' => 'tether',
'tether' => 'tether',
'usdc' => 'usd-coin',
'arbitrum' => 'arbitrum',
'optimism' => 'optimism'
}
mapping[key]
end
def cache_fetch(key, ttl: DEFAULT_CACHE_TTL_SEC)
synchronize do
cached = @cache[key]
if cached && (Time.now.to_i - cached[:timestamp]) < ttl
return cached[:data]
end
value = yield
@cache[key] = {
timestamp: Time.now.to_i,
data: value
}
value
end
end
def http_get_json(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
http.open_timeout = DEFAULT_TIMEOUT_SEC
http.read_timeout = DEFAULT_TIMEOUT_SEC
request = Net::HTTP::Get.new(uri)
request['Accept'] = 'application/json'
request['User-Agent'] = 'MagnumOpus-WalletService/1.0'
response = http.request(request)
return nil unless response.is_a?(Net::HTTPSuccess)
JSON.parse(response.body)
rescue StandardError => e
@logger.debug("HTTP request failed for #{uri}: #{e.message}")
nil
end
end
end
if __FILE__ == $0
service = MagnumOpus::WalletService.new('0x71C7656EC7ab88b098defB751B7401B5f6d8976F')
puts '=== Magnum Opus Wallet Service Demo ==='
puts "ETH Balance: #{service.get_eth_balance[:balance]} ETH ($#{service.get_eth_balance[:usd_value]})";
puts "Portfolio Total: $#{service.get_portfolio_summary[:total_portfolio_usd]}"
puts "Gas Estimate (ETH): $#{service.estimate_gas_fees(chain: :ethereum)[:transfer_fee_usd]}"
il = service.calculate_impermanent_loss(1.0, 1.5)
puts "Impermanent Loss (1.5x price ratio): #{il[:impermanent_loss_pct]}%"
end