-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: create asset registry * refactor: move denoms to separate file * refactor: use AssetRegistry instead of hard-coded constants * chore: update changelog * refactor(denom): move denoms to separate package * refactor(asset_registry): Use `set[string]` instead of slice `[]string`, since it covers both use cases (#1159) * refactor(asset_registry): Use StringSet instead of []slice, since it covers both use cases * feat: add generic sets (#1160) * feat(set): use a generic set instead of StringSet * chore: update changelog Co-authored-by: Kevin Yang <5478483+k-yang@users.noreply.github.com> * refactor: remove redundant Denom name * refactor: AssetRegistry in asset package * refactor: rename AssetRegistry to Registry Co-authored-by: Unique Divine <51418232+Unique-Divine@users.noreply.github.com>
- Loading branch information
1 parent
fc2e00c
commit ab96d89
Showing
80 changed files
with
1,680 additions
and
1,424 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package asset | ||
|
||
import ( | ||
"github.com/NibiruChain/nibiru/x/common" | ||
"github.com/NibiruChain/nibiru/x/common/denoms" | ||
"github.com/NibiruChain/nibiru/x/common/set" | ||
) | ||
|
||
type registry map[string]set.Set[string] | ||
|
||
var Registry registry | ||
|
||
func init() { | ||
// map of base asset to supported quote assets | ||
// quote assets are usually stables | ||
Registry = map[string]set.Set[string]{ | ||
denoms.BTC: set.New(denoms.USDC, denoms.NUSD, denoms.USD, denoms.USDT), | ||
denoms.ETH: set.New(denoms.USDC, denoms.NUSD, denoms.USD, denoms.USDT), | ||
denoms.NIBI: set.New(denoms.USDC, denoms.NUSD, denoms.USD, denoms.USDT), | ||
denoms.ATOM: set.New(denoms.USDC, denoms.NUSD, denoms.USD, denoms.USDT), | ||
denoms.OSMO: set.New(denoms.USDC, denoms.NUSD, denoms.USD, denoms.USDT), | ||
denoms.AVAX: set.New(denoms.USDC, denoms.NUSD, denoms.USD, denoms.USDT), | ||
denoms.SOL: set.New(denoms.USDC, denoms.NUSD, denoms.USD, denoms.USDT), | ||
denoms.BNB: set.New(denoms.USDC, denoms.NUSD, denoms.USD, denoms.USDT), | ||
denoms.ADA: set.New(denoms.USDC, denoms.NUSD, denoms.USD, denoms.USDT), | ||
denoms.NUSD: set.New(denoms.USD, denoms.USDC), | ||
denoms.USDC: set.New(denoms.USD, denoms.NUSD), | ||
denoms.USDT: set.New(denoms.USD, denoms.NUSD, denoms.USDC), | ||
} | ||
} | ||
|
||
func (r registry) Pair(base string, quote string) common.AssetPair { | ||
for q := range r[base] { | ||
if q == quote { | ||
return common.NewAssetPair(string(base), string(quote)) | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// Returns all supported base denoms | ||
func (r registry) BaseDenoms() set.Set[string] { | ||
baseSet := make(set.Set[string]) | ||
for d := range r { | ||
baseSet.Add(d) | ||
} | ||
return baseSet | ||
} | ||
|
||
// Returns all supported quote denoms | ||
func (r registry) QuoteDenoms() set.Set[string] { | ||
quoteSet := make(set.Set[string]) | ||
for base := range r { | ||
for q := range r[base] { | ||
quoteSet.Add(q) | ||
} | ||
} | ||
return quoteSet | ||
} | ||
|
||
// Checks if the provided denom is a supported base denom | ||
func (r registry) IsSupportedBaseDenom(denom string) bool { | ||
_, ok := r[denom] | ||
return ok | ||
} | ||
|
||
// Checks if the provided denom is a supported quote denom | ||
func (r registry) IsSupportedQuoteDenom(denom string) bool { | ||
return r.QuoteDenoms().Has(denom) | ||
} | ||
|
||
// Checks if the provided denom is a supported denom | ||
func (r registry) IsSupportedDenom(denom string) bool { | ||
return r.IsSupportedBaseDenom(string(denom)) || r.IsSupportedQuoteDenom(string(denom)) | ||
} | ||
|
||
// Checks if the provided base and quote denoms are a supported pair | ||
func (r registry) IsSupportedPair(base string, quote string) bool { | ||
return r.IsSupportedBaseDenom(base) && r.IsSupportedQuoteDenom(quote) | ||
} |
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,82 @@ | ||
package asset | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/NibiruChain/nibiru/x/common" | ||
"github.com/NibiruChain/nibiru/x/common/denoms" | ||
) | ||
|
||
func TestIsSupportedPair(t *testing.T) { | ||
for base := range Registry { | ||
for quote := range Registry[base] { | ||
require.Truef(t, Registry.IsSupportedPair(base, quote), "%s:%s should be supported", base, quote) | ||
} | ||
} | ||
|
||
t.Log("test an unsupported pair") | ||
require.False(t, Registry.IsSupportedPair(denoms.ATOM, denoms.OSMO)) | ||
} | ||
|
||
func TestPair(t *testing.T) { | ||
for base := range Registry { | ||
for quote := range Registry[base] { | ||
require.Equal(t, common.NewAssetPair(base, quote), Registry.Pair(base, quote)) | ||
} | ||
} | ||
|
||
t.Log("test an unsupported pair") | ||
require.Equal(t, common.AssetPair(""), Registry.Pair(denoms.ATOM, denoms.OSMO)) | ||
|
||
t.Log("test an unsupported base asset") | ||
require.Equal(t, common.AssetPair(""), Registry.Pair("unsuported_denom", denoms.USDC)) | ||
|
||
t.Log("test an unsupported quote asset") | ||
require.Equal(t, common.AssetPair(""), Registry.Pair(denoms.ATOM, "unsupported_denom")) | ||
} | ||
|
||
func TestBaseDenoms(t *testing.T) { | ||
for base := range Registry { | ||
require.Contains(t, Registry.BaseDenoms(), base) | ||
} | ||
} | ||
|
||
func TestIsSupportedBaseDenom(t *testing.T) { | ||
for base := range Registry { | ||
require.True(t, Registry.IsSupportedBaseDenom(base)) | ||
} | ||
require.False(t, Registry.IsSupportedBaseDenom("unsupported_denom")) | ||
} | ||
|
||
func TestQuoteDenoms(t *testing.T) { | ||
for base := range Registry { | ||
for quote := range Registry[base] { | ||
require.True(t, Registry.QuoteDenoms().Has(quote)) | ||
} | ||
} | ||
} | ||
|
||
func TestIsSupportedQuoteDenom(t *testing.T) { | ||
for base := range Registry { | ||
for quote := range Registry[base] { | ||
require.True(t, Registry.IsSupportedQuoteDenom(quote)) | ||
} | ||
} | ||
|
||
require.False(t, Registry.IsSupportedQuoteDenom("unsupported_denom")) | ||
} | ||
|
||
func TestIsSupportedDenom(t *testing.T) { | ||
for base := range Registry.BaseDenoms() { | ||
require.True(t, Registry.IsSupportedDenom(base)) | ||
} | ||
|
||
for quote := range Registry.QuoteDenoms() { | ||
require.True(t, Registry.IsSupportedDenom(quote)) | ||
} | ||
|
||
t.Log("test an unsupported denom") | ||
require.False(t, Registry.IsSupportedDenom("unsupported_denom")) | ||
} |
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,20 @@ | ||
package denoms | ||
|
||
const ( // stablecoins | ||
USDC = "uusdc" | ||
NUSD = "unusd" | ||
USD = "uusd" | ||
USDT = "uusdt" | ||
) | ||
|
||
const ( // volatile assets | ||
NIBI = "unibi" | ||
BTC = "ubtc" | ||
ETH = "ueth" | ||
ATOM = "uatom" | ||
OSMO = "uosmo" | ||
AVAX = "uavax" | ||
SOL = "usol" | ||
BNB = "ubnb" | ||
ADA = "uada" | ||
) |
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.