-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Michael Tsitrin <michael@dymension.xyz>
- Loading branch information
Showing
27 changed files
with
353 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,37 @@ | ||
package denom | ||
|
||
import ( | ||
"strings" | ||
|
||
transferTypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" | ||
) | ||
|
||
// ValidateIBCDenom validates that the given denomination is a valid fungible token representation (i.e 'ibc/{hash}') | ||
// per ADR 001 https://github.com/cosmos/ibc-go/blob/main/docs/architecture/adr-001-coin-source-tracing.md. | ||
// If the denom is valid, return its hash-string part. Inspired by | ||
// https://github.com/cosmos/ibc-go/blob/5d7655684554e4f577be9573ef94ef4ad6c82667/modules/apps/transfer/types/denom.go#L190. | ||
func ValidateIBCDenom(denom string) (string, bool) { | ||
denomSplit := strings.SplitN(denom, "/", 2) | ||
|
||
if len(denomSplit) == 2 && denomSplit[0] == transferTypes.DenomPrefix && strings.TrimSpace(denomSplit[1]) != "" { | ||
return denomSplit[1], true | ||
} | ||
|
||
return "", false | ||
} | ||
|
||
// SourcePortChanFromTracePath extracts source port and channel from the provided IBC denom trace path. | ||
// References: | ||
// - https://github.com/cosmos/ibc-go/blob/main/docs/architecture/adr-001-coin-source-tracing.md | ||
// - https://github.com/cosmos/relayer/issues/288 | ||
func SourcePortChanFromTracePath(tracePath string) (sourcePort, sourceChannel string, validTrace bool) { | ||
sp := strings.Split(tracePath, "/") | ||
if len(sp) < 2 { | ||
return "", "", false | ||
} | ||
sourcePort, sourceChannel = sp[len(sp)-2], sp[len(sp)-1] | ||
if sourcePort == "" || sourceChannel == "" { | ||
return "", "", false | ||
} | ||
return sourcePort, sourceChannel, true | ||
} |
This file contains 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,40 @@ | ||
package denom_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/dymensionxyz/dymension/v3/utils/denom" | ||
) | ||
|
||
func TestSourcePortChanFromTracePath(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
trace string | ||
expValid bool | ||
expPort string | ||
expChan string | ||
}{ | ||
{"invalid: empty trace", "", false, "", ""}, | ||
{"invalid: only port", "transfer", false, "", ""}, | ||
{"invalid: only port with '/'", "transfer/", false, "", ""}, | ||
{"invalid: only channel with '/'", "/channel-1", false, "", ""}, | ||
{"invalid: only '/'", "/", false, "", ""}, | ||
{"invalid: double '/'", "transfer//channel-1", false, "", ""}, | ||
{"valid trace", "transfer/channel-1", true, "transfer", "channel-1"}, | ||
{"valid trace with multiple port/channel pairs", "transfer/channel-1/transfer/channel-2", true, "transfer", "channel-2"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
port, channel, valid := denom.SourcePortChanFromTracePath(tc.trace) | ||
|
||
require.Equal(t, tc.expValid, valid) | ||
if tc.expValid { | ||
require.Equal(t, tc.expPort, port) | ||
require.Equal(t, tc.expChan, channel) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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.