-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. chainregistry package used for interacting with the Git repo Cosmos chain registry 2. Functionality for pulling the repo and parsing asset lists 3. Use the new package in the following places: * Client initialization setup to get and cache asset mappings * Denom unit conversion method to check for asset list entry and prefer that over database denoms
- Loading branch information
Showing
8 changed files
with
336 additions
and
41 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 |
---|---|---|
|
@@ -14,3 +14,4 @@ vendor/ | |
tax.txt | ||
tax-db-pwd.txt | ||
log.txt | ||
registry |
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,52 @@ | ||
package chainregistry | ||
|
||
type AssetList struct { | ||
ChainName string `json:"chain_name"` | ||
Assets []Asset `json:"assets"` | ||
} | ||
|
||
type Asset struct { | ||
Description string `json:"description"` | ||
Base string `json:"base"` | ||
Symbol string `json:"symbol"` | ||
DenomUnits []AssetDenomUnit `json:"denom_units"` | ||
ChainName string `json:"chain_name,omitempty"` | ||
} | ||
|
||
type AssetDenomUnit struct { | ||
Denom string `json:"denom"` | ||
Exponent uint `json:"exponent"` | ||
} | ||
|
||
var CachedAssetMap = map[string]Asset{} | ||
|
||
func CacheAssetMap(newVal map[string]Asset) { | ||
CachedAssetMap = newVal | ||
} | ||
|
||
func GetCachedAssetEntry(denomBase string) (Asset, bool) { | ||
asset, ok := CachedAssetMap[denomBase] | ||
return asset, ok | ||
} | ||
|
||
func GetBaseDenomUnitForAsset(asset Asset) AssetDenomUnit { | ||
lowestDenomUnit := AssetDenomUnit{Exponent: 0} | ||
for _, denomUnit := range asset.DenomUnits { | ||
if denomUnit.Exponent <= lowestDenomUnit.Exponent { | ||
lowestDenomUnit = denomUnit | ||
} | ||
} | ||
|
||
return lowestDenomUnit | ||
} | ||
|
||
func GetHighestDenomUnitForAsset(asset Asset) AssetDenomUnit { | ||
highestDenomUnit := AssetDenomUnit{Exponent: 0} | ||
for _, denomUnit := range asset.DenomUnits { | ||
if denomUnit.Exponent >= highestDenomUnit.Exponent { | ||
highestDenomUnit = denomUnit | ||
} | ||
} | ||
|
||
return highestDenomUnit | ||
} |
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,106 @@ | ||
package chainregistry | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/DefiantLabs/cosmos-tax-cli/config" | ||
"github.com/go-git/go-git/v5" | ||
) | ||
|
||
const ( | ||
ChainRegistryGitRepo = "https://github.com/cosmos/chain-registry.git" | ||
) | ||
|
||
func UpdateChainRegistryOnDisk(chainRegistryLocation string) error { | ||
_, err := os.Stat(chainRegistryLocation) | ||
if err != nil && !os.IsNotExist(err) { | ||
return err | ||
} | ||
|
||
if os.IsNotExist(err) { | ||
err = os.Mkdir(chainRegistryLocation, 0o777) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// git clone repo | ||
_, err = git.PlainClone(chainRegistryLocation, false, &git.CloneOptions{ | ||
URL: ChainRegistryGitRepo, | ||
}) | ||
|
||
// Check if already cloned | ||
if err != nil && !errors.Is(err, git.ErrRepositoryAlreadyExists) { | ||
return err | ||
} else if errors.Is(err, git.ErrRepositoryAlreadyExists) { | ||
// Pull if already cloned | ||
r, err := git.PlainOpen(chainRegistryLocation) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
w, err := r.Worktree() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = w.Pull(&git.PullOptions{}) | ||
// Ignore up-to-date error | ||
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GetAssetMapOnDisk(chainRegistryLocation string, chainRegBlacklist map[string]bool) (map[string]Asset, error) { | ||
chainRegEntries, err := os.ReadDir(chainRegistryLocation) | ||
if err != nil { | ||
return nil, err | ||
} | ||
assetMap := make(map[string]Asset) | ||
for _, entry := range chainRegEntries { | ||
if entry.IsDir() { | ||
inBlacklist := chainRegBlacklist[entry.Name()] | ||
if !inBlacklist { | ||
path := fmt.Sprintf("%s/%s/assetlist.json", chainRegistryLocation, entry.Name()) | ||
|
||
// check if file exists | ||
_, err := os.Stat(path) | ||
if err != nil && os.IsNotExist(err) { | ||
config.Log.Warnf("Chain registry asset list for %s does not exist. Skipping...", entry.Name()) | ||
continue | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
|
||
// load asset list | ||
jsonFile, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
currAssets := &AssetList{} | ||
err = json.NewDecoder(jsonFile).Decode(currAssets) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, asset := range currAssets.Assets { | ||
asset.ChainName = currAssets.ChainName | ||
if prevEntry, ok := assetMap[asset.Base]; ok { | ||
config.Log.Warnf("Duplicate asset found for %s in %s. Overwriting entry for %s", asset.Base, currAssets.ChainName, prevEntry.ChainName) | ||
} | ||
assetMap[asset.Base] = asset | ||
} | ||
} | ||
} | ||
} | ||
|
||
return assetMap, nil | ||
} |
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.