tron.rb 1.2.3 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be709cc97a1f54ec26a0046e814ad9a0b6a2b080e940b5b9d5edfd5dfcb337b3
4
- data.tar.gz: 2a0e026e4dad643ee21f725173b27ed4f53df3be3102b7a79860ed74d279cbf4
3
+ metadata.gz: ce451efce40a6ad863faba401277f320310875a692adc22fbecda0ad5f0e217c
4
+ data.tar.gz: 4a78a4d10e09c20c485394bb25e6ba1f20c61b67d46e7d8d6b3b855c86321d60
5
5
  SHA512:
6
- metadata.gz: f7ed78165a2f0657e80d810f4653393ddfe93ef4598b601595733c9b61a628b3c291840eb8f177c794d3c0a4d1d05ea567d9196475702dc550d1b331f7869bfc
7
- data.tar.gz: 98137d8eaa648c76939beb477d0ea8a5c512fba6e7698465ee70fbeb52d7f559caceafde60c9c0a5617b720b5252ec9588259f4994981f792e635b21befa0791
6
+ metadata.gz: 68fc9765d78659f10991cec77e2fb2ddbf3c82f313b2e9194afa54f4b1184ca90a5789c7db0e39e45eea4732df366137137a6d80b328ce49958299eb8861c74d
7
+ data.tar.gz: 9f7fc8306018da351129803ba8bd3bd9ee893374a45071e40bd8a7c9fc17b8016e55ca162f7f334cb57f69d24b7ae912a7abd7d098a97438b945d030288664e2
data/lib/tron/client.rb CHANGED
@@ -150,20 +150,29 @@ module Tron
150
150
 
151
151
  # Step 3: Process TRC20 tokens
152
152
  wallet_data[:trc20_tokens].each do |token|
153
+ # Handle tokens from TronGrid API that have raw_amount instead of balance
154
+ if token[:raw_amount] && !token[:balance]
155
+ # Skip tokens without metadata (caller must look them up)
156
+ next unless token[:decimals]
157
+
158
+ token[:balance] = token[:raw_amount] / (10.0 ** token[:decimals])
159
+ end
160
+
153
161
  next if token[:balance] <= 0 && !include_zero_balances
154
162
 
155
163
  # Get USD price for this token
156
- price_usd = price_service.get_token_price_usd(token[:symbol].downcase)
164
+ price_usd = price_service.get_token_price_usd(token[:symbol]&.downcase || '')
157
165
  usd_value = price_usd ? (token[:balance] * price_usd) : nil
158
166
 
159
167
  tokens << {
160
- symbol: token[:symbol],
161
- name: token[:name],
168
+ symbol: token[:symbol] || 'UNKNOWN',
169
+ name: token[:name] || 'Unknown Token',
162
170
  token_balance: token[:balance],
163
171
  decimals: token[:decimals],
164
172
  address: token[:address],
165
173
  price_usd: price_usd,
166
- usd_value: usd_value
174
+ usd_value: usd_value,
175
+ raw_amount: token[:raw_amount] # Include for caller reference
167
176
  }
168
177
  end
169
178
 
@@ -92,6 +92,28 @@ module Tron
92
92
  # Rate limit before API call
93
93
  @rate_limiter.execute_request
94
94
 
95
+ # Use TronGrid API for Shasta (Tronscan is blocked by Cloudflare)
96
+ # Use Tronscan API for mainnet and nile (better token metadata)
97
+ if @config.network == :shasta
98
+ result = get_trc20_from_trongrid(address)
99
+ else
100
+ result = get_trc20_from_tronscan(address, strict)
101
+ end
102
+
103
+ # Cache the result
104
+ @cache.set(cache_key, result) if @config.cache_enabled
105
+
106
+ result
107
+ end
108
+
109
+ private
110
+
111
+ # Gets TRC20 tokens from Tronscan API
112
+ #
113
+ # @param address [String] the TRON address
114
+ # @param strict [Boolean] whether to enable strict validation
115
+ # @return [Array<Hash>] array of token information
116
+ def get_trc20_from_tronscan(address, strict)
95
117
  url = "#{@config.tronscan_base_url}/api/account/wallet?address=#{address}&asset_type=1"
96
118
  headers = tronscan_headers
97
119
 
@@ -102,7 +124,7 @@ module Tron
102
124
  raise "Missing 'data' field in TRC20 response" unless response.key?('data')
103
125
  raise "Invalid 'data' format in TRC20 response" unless response['data'].is_a?(Array)
104
126
 
105
- result = response['data'].select { |token| token['token_type'] == 20 && token['balance'].to_f > 0 }
127
+ response['data'].select { |token| token['token_type'] == 20 && token['balance'].to_f > 0 }
106
128
  .map do |token|
107
129
  validate_token_data!(token)
108
130
  {
@@ -113,11 +135,48 @@ module Tron
113
135
  address: token['token_id']
114
136
  }
115
137
  end
138
+ end
116
139
 
117
- # Cache the result
118
- @cache.set(cache_key, result) if @config.cache_enabled
140
+ # Gets TRC20 tokens from TronGrid API (used for Shasta)
141
+ # Returns raw token data with amounts, caller must look up token details from database
142
+ #
143
+ # @param address [String] the TRON address
144
+ # @return [Array<Hash>] array of token information with raw amounts
145
+ def get_trc20_from_trongrid(address)
146
+ url = "#{@config.base_url}/v1/accounts/#{address}"
147
+ headers = api_headers
119
148
 
120
- result
149
+ response = Utils::HTTP.get(url, headers)
150
+
151
+ # Validate response structure
152
+ raise "Unexpected API response format" unless response.is_a?(Hash)
153
+ raise "Missing 'data' field in response" unless response.key?('data')
154
+ raise "Invalid 'data' format in response" unless response['data'].is_a?(Array)
155
+
156
+ # Handle empty data array (new/inactive accounts)
157
+ return [] if response['data'].empty?
158
+
159
+ account_data = response['data'].first
160
+ raise "Invalid account data format" unless account_data.is_a?(Hash)
161
+
162
+ # Parse TRC20 tokens from account data
163
+ trc20_data = account_data.fetch('trc20', [])
164
+ return [] unless trc20_data.is_a?(Array)
165
+
166
+ trc20_data.flat_map do |token_hash|
167
+ # Each token_hash is like: {"TKnF3Ugr8FFPMLZyvLBJR4Di2fEH42amw3"=>"989550000000"}
168
+ token_hash.map do |token_address, raw_amount|
169
+ # Return raw data, let caller convert based on actual token decimals
170
+ {
171
+ symbol: nil, # TronGrid doesn't provide symbol/name/decimals
172
+ name: nil, # Caller must look these up from database
173
+ balance: nil, # Will be calculated by caller using correct decimals
174
+ raw_amount: raw_amount.to_i, # Raw amount from API
175
+ decimals: nil, # Caller must look up from database
176
+ address: token_address
177
+ }
178
+ end
179
+ end.select { |token| token[:raw_amount] > 0 }
121
180
  end
122
181
 
123
182
  # Gets cache statistics for the balance service
@@ -0,0 +1,34 @@
1
+ # lib/tron/utils/chain_id.rb
2
+ module Tron
3
+ module Utils
4
+ module ChainId
5
+ # Chain ID constants for different TRON networks
6
+ MAINNET = 0x2b6653dc # 728126428 decimal
7
+ NILE = 0xcd8690dc # 3448148188 decimal
8
+
9
+ # Get the chain ID for a given network
10
+ #
11
+ # @param network [Symbol] the network symbol (:mainnet, :nile)
12
+ # @return [Integer] the chain ID for the network
13
+ # @raise [ArgumentError] if the network is unknown
14
+ def self.for_network(network)
15
+ case network
16
+ when :mainnet
17
+ MAINNET
18
+ when :nile
19
+ NILE
20
+ else
21
+ raise ArgumentError, "Unknown network: #{network}. Supported networks: :mainnet, :nile"
22
+ end
23
+ end
24
+
25
+ # Get the chain ID from a client instance
26
+ #
27
+ # @param client [Tron::Client] the client instance
28
+ # @return [Integer] the chain ID for the client's network
29
+ def self.from_client(client)
30
+ for_network(client.configuration.network)
31
+ end
32
+ end
33
+ end
34
+ end
data/lib/tron/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  # lib/tron/version.rb
4
4
  module Tron
5
- VERSION = "1.2.3".freeze
5
+ VERSION = "1.2.4".freeze
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tron.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bolo Michelin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-11-04 00:00:00.000000000 Z
11
+ date: 2025-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base58-alphabets
@@ -160,6 +160,7 @@ files:
160
160
  - lib/tron/signature.rb
161
161
  - lib/tron/utils/address.rb
162
162
  - lib/tron/utils/cache.rb
163
+ - lib/tron/utils/chain_id.rb
163
164
  - lib/tron/utils/crypto.rb
164
165
  - lib/tron/utils/http.rb
165
166
  - lib/tron/utils/rate_limiter.rb