-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: min/max for int64,uint64,int256,uint256 * feat: std.Address <-> pusers.AddressOrName type conversion * feat: uint256 safe convert * feat: token's method by using grc20reg * feat: TokenIdFrom, to convert type from various to grc721 tokenId
- Loading branch information
Showing
9 changed files
with
629 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package common | ||
|
||
import ( | ||
"std" | ||
|
||
pusers "gno.land/p/demo/users" | ||
"gno.land/r/demo/users" | ||
) | ||
|
||
// AddrToUser converts a type from address to AddressOrName. | ||
// It panics if the address is invalid. | ||
func AddrToUser(addr std.Address) pusers.AddressOrName { | ||
assertValidAddr(addr) | ||
return pusers.AddressOrName(addr) | ||
} | ||
|
||
// UserToAddr converts a type from AddressOrName to address. | ||
// by resolving the user through the users realms. | ||
func UserToAddr(user pusers.AddressOrName) std.Address { | ||
return users.Resolve(user) | ||
} | ||
|
||
// assertValidAddr checks if the given address is valid. | ||
// It panics with a detailed error message if the address is invalid. | ||
func assertValidAddr(addr std.Address) { | ||
if !addr.IsValid() { | ||
panic(addDetailToError(errInvalidAddr, addr.String())) | ||
} | ||
} |
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,115 @@ | ||
package common | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/uassert" | ||
pusers "gno.land/p/demo/users" | ||
) | ||
|
||
func TestAddrToUser(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
addr std.Address | ||
want pusers.AddressOrName | ||
shouldPanic bool | ||
panicMsg string | ||
}{ | ||
{ | ||
name: "valid address", | ||
addr: std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"), | ||
want: pusers.AddressOrName("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"), | ||
}, | ||
{ | ||
name: "empty address", | ||
addr: std.Address(""), | ||
shouldPanic: true, | ||
panicMsg: `[GNOSWAP-COMMON-005] invalid address || `, | ||
}, | ||
{ | ||
name: "invalid address", | ||
addr: std.Address("invalid"), | ||
shouldPanic: true, | ||
panicMsg: `[GNOSWAP-COMMON-005] invalid address || invalid`, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.shouldPanic { | ||
uassert.PanicsWithMessage(t, tt.panicMsg, func() { | ||
AddrToUser(tt.addr) | ||
}) | ||
} else { | ||
got := AddrToUser(tt.addr) | ||
if got != tt.want { | ||
t.Errorf("AddrToUser() = %v, want %v", got, tt.want) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestUserToAddr(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
user pusers.AddressOrName | ||
want std.Address | ||
}{ | ||
{ | ||
name: "address string with user type", | ||
user: pusers.AddressOrName("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"), | ||
want: std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := UserToAddr(tt.user) | ||
if got != tt.want { | ||
t.Errorf("UserToAddr() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAssertValidAddr(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
addr std.Address | ||
shouldPanic bool | ||
panicMsg string | ||
}{ | ||
{ | ||
name: "valid address", | ||
addr: std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"), | ||
}, | ||
{ | ||
name: "empty address", | ||
addr: std.Address(""), | ||
shouldPanic: true, | ||
panicMsg: `[GNOSWAP-COMMON-005] invalid address || `, | ||
}, | ||
{ | ||
name: "invalid address", | ||
addr: std.Address("invalid"), | ||
shouldPanic: true, | ||
panicMsg: `[GNOSWAP-COMMON-005] invalid address || invalid`, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.shouldPanic { | ||
uassert.PanicsWithMessage(t, tt.panicMsg, func() { | ||
assertValidAddr(tt.addr) | ||
}) | ||
} else { | ||
uassert.NotPanics(t, func() { | ||
assertValidAddr(tt.addr) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
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,41 @@ | ||
package common | ||
|
||
import ( | ||
"strconv" | ||
|
||
"gno.land/p/demo/ufmt" | ||
|
||
"gno.land/p/demo/grc/grc721" | ||
) | ||
|
||
// TokenIdFrom converts tokenId to grc721.TokenID type | ||
// NOTE: input parameter tokenId can be string, int, uint64, or grc721.TokenID | ||
// if tokenId is nil or not supported, it will panic | ||
// if input type is not supported, it will panic | ||
// input: tokenId interface{} | ||
// output: grc721.TokenID | ||
func TokenIdFrom(tokenId interface{}) grc721.TokenID { | ||
if tokenId == nil { | ||
panic(addDetailToError( | ||
errInvalidTokenId, | ||
"can not be nil", | ||
)) | ||
} | ||
|
||
switch tokenId.(type) { | ||
case string: | ||
return grc721.TokenID(tokenId.(string)) | ||
case int: | ||
return grc721.TokenID(strconv.Itoa(tokenId.(int))) | ||
case uint64: | ||
return grc721.TokenID(strconv.Itoa(int(tokenId.(uint64)))) | ||
case grc721.TokenID: | ||
return tokenId.(grc721.TokenID) | ||
default: | ||
estimatedType := ufmt.Sprintf("%T", tokenId) | ||
panic(addDetailToError( | ||
errInvalidTokenId, | ||
ufmt.Sprintf("unsupported tokenId type: %s", estimatedType), | ||
)) | ||
} | ||
} |
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,64 @@ | ||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
"gno.land/p/demo/grc/grc721" | ||
) | ||
|
||
func TestTokenIdFrom(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input interface{} | ||
want grc721.TokenID | ||
wantPanic bool | ||
}{ | ||
{ | ||
name: "string input", | ||
input: "123", | ||
want: grc721.TokenID("123"), | ||
}, | ||
{ | ||
name: "int input", | ||
input: 123, | ||
want: grc721.TokenID("123"), | ||
}, | ||
{ | ||
name: "uint64 input", | ||
input: uint64(123), | ||
want: grc721.TokenID("123"), | ||
}, | ||
{ | ||
name: "grc721.TokenID input", | ||
input: grc721.TokenID("123"), | ||
want: grc721.TokenID("123"), | ||
}, | ||
{ | ||
name: "nil input", | ||
input: nil, | ||
wantPanic: true, | ||
}, | ||
{ | ||
name: "unsupported type (byte)", | ||
input: []byte("123"), | ||
wantPanic: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.wantPanic { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Errorf("TokenIdFrom() should have panicked") | ||
} | ||
}() | ||
} | ||
|
||
got := TokenIdFrom(tt.input) | ||
if got != tt.want { | ||
t.Errorf("TokenIdFrom() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.