forked from xyield/xrpl-go
-
Notifications
You must be signed in to change notification settings - Fork 18
feat(xrpl): add contains flag utility #197
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
GuillemGarciaDev
merged 2 commits into
XRPLF:main
from
kpitapeersyst:xrpl/feat/contains-flag-util
Feb 25, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # flag | ||
|
|
||
| ## Overview | ||
|
|
||
| The `flag` package provides utility functions for working with bitwise flags in XRPL. | ||
|
|
||
| - `Contains`: Checks whether a flag is fully set within a combined flag value. Useful for inspecting transaction or ledger object flags. | ||
|
|
||
| ## Usage | ||
|
|
||
| To import the package, you can use the following code: | ||
|
|
||
| ```go | ||
| import "github.com/Peersyst/xrpl-go/xrpl/flag" | ||
| ``` | ||
|
|
||
| ## API | ||
|
|
||
| ### Contains | ||
|
|
||
| ```go | ||
| func Contains(currentFlag uint32, flag uint32) bool | ||
| ``` | ||
|
|
||
| Returns `true` if all bits of `flag` are set in `currentFlag` (`(currentFlag & flag) == flag`). Returns `false` if any bit of `flag` is missing in `currentFlag`, or if `flag` is `0`. | ||
|
|
||
| :::warning | ||
|
|
||
| The comparison is based on the flag value as a `uint32`. Different contexts may use the same numeric values (e.g. a transaction flag and a ledger-state flag), so a match only indicates the bit is set — not that it belongs to a specific context. Always pair `Contains` with the flag constant that matches the context you are checking. | ||
|
|
||
| ::: | ||
|
|
||
| ### Example | ||
|
|
||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/Peersyst/xrpl-go/xrpl/flag" | ||
| "github.com/Peersyst/xrpl-go/xrpl/transaction" | ||
| ) | ||
|
|
||
| func main() { | ||
| offer := transaction.OfferCreate{ | ||
| BaseTx: transaction.BaseTx{ | ||
| Account: "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe", | ||
| }, | ||
| TakerGets: nil, | ||
| TakerPays: nil, | ||
| } | ||
|
|
||
| // Set the sell flag on the offer | ||
| offer.SetSellFlag() | ||
|
|
||
| // Check whether the sell flag is set | ||
| if flag.Contains(offer.Flags, transaction.TfSell) { | ||
| fmt.Println("Offer is a sell offer") | ||
| } | ||
|
|
||
| // Check whether the fill-or-kill flag is set (it is not) | ||
| if !flag.Contains(offer.Flags, transaction.TfFillOrKill) { | ||
| fmt.Println("Offer is not fill-or-kill") | ||
| } | ||
| } | ||
| ``` |
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
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
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Package flag provides utility functions for working with bitwise flags. | ||
| package flag | ||
|
|
||
| // Contains checks if all bits of flag are present in currentFlag. | ||
| // Returns false if flag is 0. | ||
| // Note: It should be taken into account that the comparison is based on the flag value as a uint32. | ||
| // Different contexts may use same values, and they will return ok as the value matches. | ||
| func Contains(currentFlag, flag uint32) bool { | ||
| return flag != 0 && (currentFlag&flag) == flag | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package flag | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| const ( | ||
| dummyFlagA = 262144 | ||
| dummyFlagB = 131072 | ||
| dummyFlagC = 65536 | ||
| ) | ||
|
|
||
| func TestFlag_Contains(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| currentFlag uint32 | ||
| flag uint32 | ||
| expected bool | ||
| }{ | ||
| { | ||
| name: "pass - same flag", | ||
| currentFlag: dummyFlagA, | ||
| flag: dummyFlagA, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "pass - containing flag", | ||
| currentFlag: dummyFlagA | dummyFlagB, | ||
| flag: dummyFlagA, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "pass - not containing flag", | ||
| currentFlag: dummyFlagA | dummyFlagB, | ||
| flag: dummyFlagC, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "pass - zero flag", | ||
| currentFlag: dummyFlagA, | ||
| flag: 0, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "pass - zero current flag", | ||
| currentFlag: 0, | ||
| flag: 0, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "pass - partial overlap multi-bit flag", | ||
| currentFlag: dummyFlagA, | ||
| flag: dummyFlagA | dummyFlagB, | ||
| expected: false, | ||
| }, | ||
| } | ||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| actual := Contains(tc.currentFlag, tc.flag) | ||
| assert.Equal(t, tc.expected, actual) | ||
| }) | ||
| } | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.