Skip to content

Commit a908067

Browse files
committed
Implement structured parameter handling and calldata encoding in Erc20FixedDenominationParser
- Added `structured_params` method to convert operation parameters into a hash format for downstream services. - Introduced `encode_calldata` method to encode parameters into the ABI tuple required by the manager contract. - Updated `ProtocolParser` to utilize the new methods for improved parameter handling and encoding.
1 parent 13c1e7e commit a908067

File tree

2 files changed

+53
-63
lines changed

2 files changed

+53
-63
lines changed

app/models/erc20_fixed_denomination_parser.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,42 @@ def self.extract(content_uri)
4242
# No match - return default
4343
DEFAULT_PARAMS
4444
end
45+
46+
# Returns a hash representation of params that downstream services expect.
47+
def self.structured_params(params)
48+
op, _protocol, tick, val1, val2, val3 = params
49+
50+
case op
51+
when 'deploy'.b
52+
{
53+
op: op,
54+
tick: tick,
55+
max: val1,
56+
lim: val2,
57+
amt: 0
58+
}
59+
when 'mint'.b
60+
{
61+
op: op,
62+
tick: tick,
63+
id: val1,
64+
amt: val3
65+
}
66+
else
67+
nil
68+
end
69+
end
70+
71+
# Encodes params into the ABI tuple required by the manager contract.
72+
def self.encode_calldata(params)
73+
op, _protocol, tick, val1, val2, val3 = params
74+
75+
if op == 'deploy'.b
76+
Eth::Abi.encode(['(string,uint256,uint256)'], [[tick.b, val1, val2]])
77+
elsif op == 'mint'.b
78+
Eth::Abi.encode(['(string,uint256,uint256)'], [[tick.b, val1, val3]])
79+
else
80+
''.b
81+
end
82+
end
4583
end

app/models/protocol_parser.rb

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,28 @@ def self.try_token_parser(content_uri)
2929
params = Erc20FixedDenominationParser.extract(content_uri)
3030

3131
# Check if extraction succeeded (returns non-default params)
32-
if params != TOKEN_DEFAULT_PARAMS
33-
{
34-
type: :erc20_fixed_denomination,
35-
protocol: 'erc-20-fixed-denomination',
36-
operation: params[0], # 'deploy' or 'mint'
37-
params: params,
38-
encoded_params: encode_token_params(params)
39-
}
40-
else
41-
nil
42-
end
32+
return nil if params == TOKEN_DEFAULT_PARAMS
33+
34+
{
35+
type: :erc20_fixed_denomination,
36+
protocol: 'erc-20-fixed-denomination',
37+
operation: params[0], # 'deploy' or 'mint'
38+
params: params,
39+
encoded_params: Erc20FixedDenominationParser.structured_params(params)
40+
}
4341
end
4442

4543
def self.try_collections_parser(content_uri, ethscription_id: nil)
4644
# Erc721EthscriptionsCollectionParser returns [''.b, ''.b, ''.b] if no match
47-
protocol, operation, encoded_data = Erc721EthscriptionsCollectionParser.extract(
45+
params = Erc721EthscriptionsCollectionParser.extract(
4846
content_uri,
4947
ethscription_id: ethscription_id
5048
)
5149

50+
return nil if params == COLLECTIONS_DEFAULT_PARAMS
51+
52+
protocol, operation, encoded_data = params
53+
5254
# Check if extraction succeeded
5355
if protocol != ''.b && operation != ''.b
5456
{
@@ -63,34 +65,6 @@ def self.try_collections_parser(content_uri, ethscription_id: nil)
6365
end
6466
end
6567

66-
def self.encode_token_params(params)
67-
# Convert token params to format expected by contracts
68-
# params format: [op, protocol, tick, val1, val2, val3]
69-
op, _protocol, tick, val1, val2, val3 = params
70-
71-
case op
72-
when 'deploy'.b
73-
# For deploy: tick, max, lim
74-
{
75-
op: op,
76-
tick: tick,
77-
max: val1,
78-
lim: val2,
79-
amt: 0
80-
}
81-
when 'mint'.b
82-
# For mint: tick, id, amt
83-
{
84-
op: op,
85-
tick: tick,
86-
id: val1,
87-
amt: val3
88-
}
89-
else
90-
nil
91-
end
92-
end
93-
9468
# Get protocol data formatted for L2 calldata
9569
# Returns [protocol, operation, encoded_data] for contract consumption
9670
def self.for_calldata(content_uri, ethscription_id: nil)
@@ -104,7 +78,7 @@ def self.for_calldata(content_uri, ethscription_id: nil)
10478
protocol = result[:protocol].b
10579
operation = result[:operation]
10680
# For tokens, encode the params properly
107-
encoded_data = encode_token_data(result[:params])
81+
encoded_data = Erc20FixedDenominationParser.encode_calldata(result[:params])
10882
[protocol, operation, encoded_data]
10983
elsif result[:type] == :erc721_ethscriptions_collection
11084
# Collections protocol - already has encoded data
@@ -115,26 +89,4 @@ def self.for_calldata(content_uri, ethscription_id: nil)
11589
end
11690
end
11791

118-
# Encode token params as bytes for contract consumption
119-
def self.encode_token_data(params)
120-
# params format: [op, protocol, tick, val1, val2, val3]
121-
op, _protocol, tick, val1, val2, val3 = params
122-
123-
# Encode based on operation type (operation is passed separately now)
124-
# Use tuple encoding for struct compatibility with contracts
125-
# IMPORTANT: Field order must match ERC20FixedDenominationManager's struct definitions!
126-
if op == 'deploy'.b
127-
# DeployOperation struct: tick, maxSupply, mintAmount
128-
# Our params: tick, max (val1), lim (val2)
129-
# So: tick, maxSupply=val1, mintAmount=val2
130-
Eth::Abi.encode(['(string,uint256,uint256)'], [[tick.b, val1, val2]])
131-
elsif op == 'mint'.b
132-
# MintOperation struct: tick, id, amount
133-
# Our params: tick, id (val1), amt (val3)
134-
# So: tick, id=val1, amount=val3
135-
Eth::Abi.encode(['(string,uint256,uint256)'], [[tick.b, val1, val3]])
136-
else
137-
''.b
138-
end
139-
end
14092
end

0 commit comments

Comments
 (0)