-
Notifications
You must be signed in to change notification settings - Fork 25
Refactor Protocol Parsing and Introduce Word Domains Support #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,83 +1,61 @@ | ||
| # Extracts fixed-denomination ERC-20 parameters from strict JSON inscriptions. | ||
| class Erc20FixedDenominationParser | ||
| # Constants | ||
| DEFAULT_PARAMS = [''.b, ''.b, ''.b, 0, 0, 0].freeze | ||
| DEFAULT_PARAMS = [''.b, ''.b, ''.b].freeze | ||
| UINT256_MAX = 2**256 - 1 | ||
|
|
||
| PROTOCOL = 'erc-20-fixed-denomination'.b | ||
|
|
||
| # Exact regex patterns for valid formats | ||
| # Protocol must be "erc-20" (legacy inscription) or the canonical identifier | ||
| # Tick must be lowercase letters/numbers, max 28 chars | ||
| # Numbers must be positive decimals without leading zeros | ||
| PROTOCOL_PATTERN = '(?:erc-20|erc-20-fixed-denomination)' | ||
| DEPLOY_REGEX = /\Adata:,\{"p":"#{PROTOCOL_PATTERN}","op":"deploy","tick":"([a-z0-9]{1,28})","max":"(0|[1-9][0-9]*)","lim":"(0|[1-9][0-9]*)"\}\z/ | ||
| MINT_REGEX = /\Adata:,\{"p":"#{PROTOCOL_PATTERN}","op":"mint","tick":"([a-z0-9]{1,28})","id":"(0|[1-9][0-9]*)","amt":"(0|[1-9][0-9]*)"\}\z/ | ||
| DEPLOY_REGEX = /\A\{"p":"#{PROTOCOL_PATTERN}","op":"deploy","tick":"([a-z0-9]{1,28})","max":"(0|[1-9][0-9]*)","lim":"(0|[1-9][0-9]*)"\}\z/ | ||
| MINT_REGEX = /\A\{"p":"#{PROTOCOL_PATTERN}","op":"mint","tick":"([a-z0-9]{1,28})","id":"(0|[1-9][0-9]*)","amt":"(0|[1-9][0-9]*)"\}\z/ | ||
|
|
||
| # Validate and encode protocol params | ||
| # Unified interface - accepts all possible parameters, uses what it needs | ||
| def self.validate_and_encode(decoded_content:, operation:, params:, source:, ethscription_id: nil, **_extras) | ||
| new.validate_and_encode( | ||
| decoded_content: decoded_content, | ||
| operation: operation, | ||
| params: params, | ||
| source: source | ||
| ) | ||
| end | ||
|
|
||
| def self.extract(content_uri) | ||
| return DEFAULT_PARAMS unless content_uri.is_a?(String) | ||
| def validate_and_encode(decoded_content:, operation:, params:, source:) | ||
| # Only support JSON source - no header parameters for ERC-20 | ||
| return DEFAULT_PARAMS unless source == :json | ||
| return DEFAULT_PARAMS unless decoded_content.is_a?(String) | ||
|
|
||
| # Try deploy format first | ||
| if match = DEPLOY_REGEX.match(content_uri) | ||
| if match = DEPLOY_REGEX.match(decoded_content) | ||
| tick = match[1] # Group 1: tick | ||
| max = match[2].to_i # Group 2: max | ||
| lim = match[3].to_i # Group 3: lim | ||
|
|
||
| # Validate uint256 bounds | ||
| return DEFAULT_PARAMS if max > UINT256_MAX || lim > UINT256_MAX | ||
|
|
||
| return ['deploy'.b, 'erc-20-fixed-denomination'.b, tick.b, max, lim, 0] | ||
| encoded = Eth::Abi.encode(['(string,uint256,uint256)'], [[tick.b, max, lim]]) | ||
| return [PROTOCOL, 'deploy'.b, encoded.b] | ||
| end | ||
|
|
||
| # Try mint format | ||
| if match = MINT_REGEX.match(content_uri) | ||
| if match = MINT_REGEX.match(decoded_content) | ||
| tick = match[1] # Group 1: tick | ||
| id = match[2].to_i # Group 2: id | ||
| amt = match[3].to_i # Group 3: amt | ||
|
|
||
| # Validate uint256 bounds | ||
| return DEFAULT_PARAMS if id > UINT256_MAX || amt > UINT256_MAX | ||
|
|
||
| return ['mint'.b, 'erc-20-fixed-denomination'.b, tick.b, id, 0, amt] | ||
| encoded = Eth::Abi.encode(['(string,uint256,uint256)'], [[tick.b, id, amt]]) | ||
| return [PROTOCOL, 'mint'.b, encoded.b] | ||
| end | ||
|
|
||
| # No match - return default | ||
| DEFAULT_PARAMS | ||
| end | ||
|
|
||
| # Returns a hash representation of params that downstream services expect. | ||
| def self.structured_params(params) | ||
| op, _protocol, tick, val1, val2, val3 = params | ||
|
|
||
| case op | ||
| when 'deploy'.b | ||
| { | ||
| op: op, | ||
| tick: tick, | ||
| max: val1, | ||
| lim: val2, | ||
| amt: 0 | ||
| } | ||
| when 'mint'.b | ||
| { | ||
| op: op, | ||
| tick: tick, | ||
| id: val1, | ||
| amt: val3 | ||
| } | ||
| else | ||
| nil | ||
| end | ||
| end | ||
|
|
||
| # Encodes params into the ABI tuple required by the manager contract. | ||
| def self.encode_calldata(params) | ||
| op, _protocol, tick, val1, val2, val3 = params | ||
|
|
||
| if op == 'deploy'.b | ||
| Eth::Abi.encode(['(string,uint256,uint256)'], [[tick.b, val1, val2]]) | ||
| elsif op == 'mint'.b | ||
| Eth::Abi.encode(['(string,uint256,uint256)'], [[tick.b, val1, val3]]) | ||
| else | ||
| ''.b | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The content hash computation is duplicated in three places: lines 156-157, lines 219-220, and used again in the
build_item_objectmethod. Consider extracting this into a private helper method likecompute_content_hash(decoded_content)to reduce duplication and ensure consistency.