tron.rb 1.2.1 → 1.2.2
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 +4 -4
- data/lib/tron/abi/constant.rb +16 -0
- data/lib/tron/abi/util.rb +5 -4
- data/lib/tron/key.rb +26 -0
- data/lib/tron/services/transaction.rb +158 -2
- data/lib/tron/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b5cd08e96e3394fc24fb6626f6a36eda41ee19b69c5ae1dc7c80de31cb357464
|
|
4
|
+
data.tar.gz: aed0379a31288c32a74bd7d4aa644c88a5c07899782f853c8a2d0498fe277e01
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8d127adf6c6f7c3477b4627295725454bfe3e59a409f42d18756d7a6e023d4f0b922c8314cede795a1d291644a309c9a00a03236b206dbec279fc59423d03f8f
|
|
7
|
+
data.tar.gz: ed54942d4329923a49af1f9399157cca3d157b857fe13810e86111707bac6ca645ace01753b1db268cc2f68fce1eeb6b8bfcb7fd22d9c26fdd3486022cb87e8b
|
data/lib/tron/abi/constant.rb
CHANGED
|
@@ -6,6 +6,22 @@ module Tron
|
|
|
6
6
|
module Constant
|
|
7
7
|
extend self
|
|
8
8
|
|
|
9
|
+
# Maximum value for uint256 (2^256 - 1)
|
|
10
|
+
# @return [Integer] maximum value for uint256
|
|
11
|
+
UINT_MAX = 2**256 - 1
|
|
12
|
+
|
|
13
|
+
# Minimum value for uint256
|
|
14
|
+
# @return [Integer] minimum value for uint256
|
|
15
|
+
UINT_MIN = 0
|
|
16
|
+
|
|
17
|
+
# Maximum value for int256 (2^255 - 1)
|
|
18
|
+
# @return [Integer] maximum value for int256
|
|
19
|
+
INT_MAX = 2**255 - 1
|
|
20
|
+
|
|
21
|
+
# Minimum value for int256 (-(2^255))
|
|
22
|
+
# @return [Integer] minimum value for int256
|
|
23
|
+
INT_MIN = -(2**255)
|
|
24
|
+
|
|
9
25
|
# Byte zero constant
|
|
10
26
|
# @return [String] binary string containing zero byte
|
|
11
27
|
BYTE_ZERO = "\x00".b
|
data/lib/tron/abi/util.rb
CHANGED
|
@@ -26,14 +26,15 @@ module Tron
|
|
|
26
26
|
((x + 31) / 32).floor * 32
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
# Pads an integer to
|
|
29
|
+
# Pads an integer to a specified number of bytes in binary format
|
|
30
30
|
#
|
|
31
31
|
# @param x [Integer] the integer to pad
|
|
32
|
+
# @param len [Integer] the number of bytes to pad to (default: 32)
|
|
32
33
|
# @return [String] the padded integer as a binary string
|
|
33
|
-
def zpad_int(x)
|
|
34
|
+
def zpad_int(x, len = 32)
|
|
34
35
|
# Ensure x is positive for modulo operation
|
|
35
|
-
x = x % (2 **
|
|
36
|
-
[x.to_s(16).rjust(
|
|
36
|
+
x = x % (2 ** (8 * len)) if x >= 2 ** (8 * len) || x < 0
|
|
37
|
+
[x.to_s(16).rjust(len * 2, '0')].pack('H*')
|
|
37
38
|
end
|
|
38
39
|
|
|
39
40
|
# Pads a string to a specified length with null bytes
|
data/lib/tron/key.rb
CHANGED
|
@@ -146,6 +146,32 @@ module Tron
|
|
|
146
146
|
sign(hashed_message)
|
|
147
147
|
end
|
|
148
148
|
|
|
149
|
+
# Signs a typed data hash using TRON TIP-191 standard
|
|
150
|
+
# This is used for structured data signing (similar to EIP-712 but with TIP-191 prefix)
|
|
151
|
+
# The hash should be the keccak256 of the packed structured data
|
|
152
|
+
#
|
|
153
|
+
# @param hash [String] binary hash (32 bytes) of the structured data
|
|
154
|
+
# @return [String] signature as hexadecimal string with v value adjusted to 27/28
|
|
155
|
+
def sign_typed_data(hash)
|
|
156
|
+
# Add TRON TIP-191 prefix for 32-byte hashes
|
|
157
|
+
# Ref: https://github.com/tronprotocol/tips/blob/master/tip-191.md
|
|
158
|
+
prefix = "\x19Tron Signed Message:\n32"
|
|
159
|
+
prefixed_data = prefix.b + hash
|
|
160
|
+
prefixed_hash = Tron::Utils::Crypto.keccak256(prefixed_data)
|
|
161
|
+
|
|
162
|
+
# Sign the prefixed hash
|
|
163
|
+
context = Secp256k1::Context.new
|
|
164
|
+
compact, recovery_id = context.sign_recoverable(@private_key, prefixed_hash).compact
|
|
165
|
+
signature = compact.bytes
|
|
166
|
+
|
|
167
|
+
# Adjust v value to 27 or 28 (standard Ethereum/TRON signature format)
|
|
168
|
+
# Instead of recovery_id (0 or 1), use 27 + recovery_id
|
|
169
|
+
v = recovery_id + 27
|
|
170
|
+
signature << v
|
|
171
|
+
|
|
172
|
+
Tron::Utils::Crypto.bin_to_hex(signature.pack('c*'))
|
|
173
|
+
end
|
|
174
|
+
|
|
149
175
|
# Verifies a signature against a data blob
|
|
150
176
|
#
|
|
151
177
|
# @param blob [String] the original signed data
|
|
@@ -41,6 +41,144 @@ module Tron
|
|
|
41
41
|
Utils::HTTP.post(endpoint, payload)
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
+
# Gets detailed transaction information by transaction ID (includes error details)
|
|
45
|
+
#
|
|
46
|
+
# @param tx_id [String] the transaction ID (txID)
|
|
47
|
+
# @return [Hash] the detailed transaction information from the blockchain
|
|
48
|
+
def get_transaction_info(tx_id)
|
|
49
|
+
endpoint = "#{@base_url}/wallet/gettransactioninfobyid"
|
|
50
|
+
payload = { value: tx_id }
|
|
51
|
+
|
|
52
|
+
Utils::HTTP.post(endpoint, payload)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Waits for transaction confirmation
|
|
56
|
+
#
|
|
57
|
+
# @param tx_id [String] the transaction ID to wait for
|
|
58
|
+
# @param max_attempts [Integer] maximum number of attempts to check (default: 15)
|
|
59
|
+
# @return [Hash] the transaction information when confirmed
|
|
60
|
+
# @raise [RuntimeError] if transaction is reverted or times out
|
|
61
|
+
def wait_for_transaction(tx_id, max_attempts = 15)
|
|
62
|
+
print "Waiting for transaction: #{tx_id}"
|
|
63
|
+
|
|
64
|
+
max_attempts.times do |i|
|
|
65
|
+
sleep 2
|
|
66
|
+
print '.'
|
|
67
|
+
|
|
68
|
+
begin
|
|
69
|
+
tx_info = get_transaction(tx_id)
|
|
70
|
+
|
|
71
|
+
if tx_info && tx_info['ret'] && tx_info['ret'][0]
|
|
72
|
+
result = tx_info['ret'][0]['contractRet']
|
|
73
|
+
|
|
74
|
+
if result == 'SUCCESS'
|
|
75
|
+
puts "\n✓ Transaction confirmed"
|
|
76
|
+
return tx_info
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
if result == 'REVERT'
|
|
80
|
+
puts "\n❌ Transaction reverted"
|
|
81
|
+
|
|
82
|
+
# Get the actual revert reason from transaction info
|
|
83
|
+
error_message = extract_revert_reason(tx_id)
|
|
84
|
+
|
|
85
|
+
# Raise with just the error message
|
|
86
|
+
raise "Transaction failed: #{error_message}"
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
rescue RuntimeError => e
|
|
90
|
+
# If it's already a formatted error, re-raise it
|
|
91
|
+
raise e if e.message.start_with?('Transaction failed:')
|
|
92
|
+
# Transaction might not be available yet, continue waiting
|
|
93
|
+
next if i < max_attempts - 1
|
|
94
|
+
raise e
|
|
95
|
+
rescue => e
|
|
96
|
+
# Transaction might not be available yet, continue waiting
|
|
97
|
+
next if i < max_attempts - 1
|
|
98
|
+
raise e
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
raise 'Transaction timeout'
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
|
|
107
|
+
# Extracts the revert reason from a failed transaction
|
|
108
|
+
#
|
|
109
|
+
# @param tx_id [String] the transaction ID
|
|
110
|
+
# @return [String] the decoded error message
|
|
111
|
+
def extract_revert_reason(tx_id)
|
|
112
|
+
tx_detail = get_transaction_info(tx_id)
|
|
113
|
+
|
|
114
|
+
# Debug: Print the full transaction info
|
|
115
|
+
if ENV['DEBUG']
|
|
116
|
+
puts "\n🔍 DEBUG - Full transaction info:"
|
|
117
|
+
puts JSON.pretty_generate(tx_detail) rescue puts(tx_detail.inspect)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Try to get error from contractResult (this contains the ABI-encoded revert reason)
|
|
121
|
+
if tx_detail && tx_detail['contractResult'] && !tx_detail['contractResult'].empty?
|
|
122
|
+
contract_result = tx_detail['contractResult'][0]
|
|
123
|
+
puts "🔍 DEBUG - contractResult: #{contract_result}" if ENV['DEBUG']
|
|
124
|
+
|
|
125
|
+
# Check if this is an Error(string) revert (starts with 08c379a0)
|
|
126
|
+
if contract_result && contract_result.start_with?('08c379a0')
|
|
127
|
+
begin
|
|
128
|
+
# ABI encoding for Error(string):
|
|
129
|
+
# - 4 bytes (8 hex chars): function selector (08c379a0)
|
|
130
|
+
# - 32 bytes (64 hex chars): offset to string data
|
|
131
|
+
# - 32 bytes (64 hex chars): length of string
|
|
132
|
+
# - N bytes: actual string data
|
|
133
|
+
|
|
134
|
+
# Skip selector (8 chars) and offset (64 chars)
|
|
135
|
+
data = contract_result[8 + 64..-1]
|
|
136
|
+
|
|
137
|
+
# Read length (next 64 chars)
|
|
138
|
+
length_hex = data[0...64]
|
|
139
|
+
length = length_hex.to_i(16)
|
|
140
|
+
puts "🔍 DEBUG - String length: #{length}" if ENV['DEBUG']
|
|
141
|
+
|
|
142
|
+
# Read string data (next length * 2 hex chars)
|
|
143
|
+
string_hex = data[64, length * 2]
|
|
144
|
+
puts "🔍 DEBUG - String hex: #{string_hex}" if ENV['DEBUG']
|
|
145
|
+
|
|
146
|
+
# Decode hex to UTF-8
|
|
147
|
+
error_message = [string_hex].pack('H*').force_encoding('UTF-8')
|
|
148
|
+
puts "🔍 DEBUG - Decoded error: #{error_message}" if ENV['DEBUG']
|
|
149
|
+
|
|
150
|
+
return error_message unless error_message.empty?
|
|
151
|
+
rescue => e
|
|
152
|
+
puts "🔍 DEBUG - Error decoding contractResult: #{e.message}" if ENV['DEBUG']
|
|
153
|
+
puts "🔍 DEBUG - Backtrace: #{e.backtrace.first(3).join("\n")}" if ENV['DEBUG']
|
|
154
|
+
# If decoding fails, fall through
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Try to get error from resMessage (hex-encoded error string)
|
|
160
|
+
if tx_detail && tx_detail['resMessage']
|
|
161
|
+
error_hex = tx_detail['resMessage']
|
|
162
|
+
puts "🔍 DEBUG - resMessage (hex): #{error_hex}" if ENV['DEBUG']
|
|
163
|
+
|
|
164
|
+
begin
|
|
165
|
+
# Decode hex to UTF-8 string
|
|
166
|
+
error_message = [error_hex].pack('H*').force_encoding('UTF-8')
|
|
167
|
+
# Clean up the message (remove null bytes and control characters)
|
|
168
|
+
error_message = error_message.gsub(/[\x00-\x1f\x7f]/, '').strip
|
|
169
|
+
|
|
170
|
+
puts "🔍 DEBUG - Decoded resMessage: #{error_message}" if ENV['DEBUG']
|
|
171
|
+
return error_message unless error_message.empty?
|
|
172
|
+
rescue => e
|
|
173
|
+
puts "🔍 DEBUG - Error decoding resMessage: #{e.message}" if ENV['DEBUG']
|
|
174
|
+
# If decoding fails, continue to try other methods
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Default to generic revert message
|
|
179
|
+
'REVERT opcode executed'
|
|
180
|
+
end
|
|
181
|
+
|
|
44
182
|
private
|
|
45
183
|
|
|
46
184
|
# Signs a transaction locally using the private key
|
|
@@ -104,8 +242,26 @@ module Tron
|
|
|
104
242
|
|
|
105
243
|
# Check if the transaction was successful
|
|
106
244
|
unless response['result']
|
|
107
|
-
|
|
108
|
-
|
|
245
|
+
error_message = response['Error'] || response['error'] || response['message'] || 'Unknown error'
|
|
246
|
+
|
|
247
|
+
# Build detailed error message
|
|
248
|
+
error_details = ["❌ Transaction broadcast failed: #{error_message}"]
|
|
249
|
+
|
|
250
|
+
# Add code if available
|
|
251
|
+
if response['code']
|
|
252
|
+
error_details << " Error code: #{response['code']}"
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Add transaction ID if available
|
|
256
|
+
if response['txid'] || response['txID']
|
|
257
|
+
tx_id = response['txid'] || response['txID']
|
|
258
|
+
error_details << " Transaction ID: #{tx_id}"
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
# Add full response for debugging
|
|
262
|
+
error_details << " Full response: #{response.inspect}"
|
|
263
|
+
|
|
264
|
+
raise error_details.join("\n")
|
|
109
265
|
end
|
|
110
266
|
|
|
111
267
|
response
|
data/lib/tron/version.rb
CHANGED