-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from InjectiveLabs/feat/load_official_tokens_…
…list feat/load_official_tokens_list
- Loading branch information
Showing
10 changed files
with
218 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package core | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type TokenMetadata struct { | ||
Address string `json:"address"` | ||
IsNative bool `json:"isNative"` | ||
TokenVerification string `json:"tokenVerification"` | ||
Decimals int32 `json:"decimals"` | ||
CoinGeckoId string `json:"coinGeckoId"` | ||
Name string `json:"name"` | ||
Symbol string `json:"symbol"` | ||
Logo string `json:"logo"` | ||
Creator string `json:"creator"` | ||
Denom string `json:"denom"` | ||
TokenType string `json:"tokenType"` | ||
ExternalLogo string `json:"externalLogo"` | ||
} | ||
|
||
func (tm TokenMetadata) GetName() string { | ||
return tm.Name | ||
} | ||
|
||
func (tm TokenMetadata) GetAddress() string { | ||
return tm.Address | ||
} | ||
|
||
func (tm TokenMetadata) GetSymbol() string { | ||
return tm.Symbol | ||
} | ||
|
||
func (tm TokenMetadata) GetLogo() string { | ||
return tm.Logo | ||
} | ||
|
||
func (tm TokenMetadata) GetDecimals() int32 { | ||
return tm.Decimals | ||
} | ||
|
||
func (tm TokenMetadata) GetUpdatedAt() int64 { | ||
return -1 | ||
} | ||
|
||
// LoadTokens loads tokens from the given file URL | ||
func LoadTokens(tokensFileUrl string) ([]TokenMetadata, error) { | ||
var tokensMetadata []TokenMetadata | ||
response, err := http.Get(tokensFileUrl) | ||
if err != nil { | ||
return tokensMetadata, err | ||
} | ||
if 400 <= response.StatusCode { | ||
return tokensMetadata, fmt.Errorf("failed to load tokens from %s: %s", tokensFileUrl, response.Status) | ||
} | ||
defer response.Body.Close() | ||
|
||
decoder := json.NewDecoder(response.Body) | ||
err = decoder.Decode(&tokensMetadata) | ||
if err != nil { | ||
return make([]TokenMetadata, 0), err | ||
} | ||
|
||
return tokensMetadata, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package core | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestLoadTokensFromUrl(t *testing.T) { | ||
tokensMetadata := make([]TokenMetadata, 2) | ||
tokensMetadata = append(tokensMetadata, TokenMetadata{ | ||
Address: "", | ||
IsNative: true, | ||
Decimals: 9, | ||
Symbol: "SOL", | ||
Name: "Solana", | ||
Logo: "https://imagedelivery.net/DYKOWp0iCc0sIkF-2e4dNw/2aa4deed-fa31-4d1a-ba0a-d698b84f3800/public", | ||
Creator: "inj15jeczm4mqwtc9lk4c0cyynndud32mqd4m9xnmu", | ||
CoinGeckoId: "solana", | ||
Denom: "", | ||
TokenType: "spl", | ||
TokenVerification: "verified", | ||
ExternalLogo: "solana.png", | ||
}, | ||
) | ||
tokensMetadata = append(tokensMetadata, TokenMetadata{ | ||
Address: "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270", | ||
IsNative: false, | ||
Decimals: 18, | ||
Symbol: "WMATIC", | ||
Name: "Wrapped Matic", | ||
Logo: "https://imagedelivery.net/DYKOWp0iCc0sIkF-2e4dNw/0d061e1e-a746-4b19-1399-8187b8bb1700/public", | ||
Creator: "inj169ed97mcnf8ay6rgvskn95n6tyt46uwvy5qgs0", | ||
CoinGeckoId: "wmatic", | ||
Denom: "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270", | ||
TokenType: "evm", | ||
TokenVerification: "verified", | ||
ExternalLogo: "polygon.png", | ||
}, | ||
) | ||
|
||
metadataString, err := json.Marshal(tokensMetadata) | ||
assert.NilError(t, err) | ||
|
||
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write(metadataString) | ||
})) | ||
defer httpServer.Close() | ||
|
||
loadedTokens, err := LoadTokens(httpServer.URL) | ||
assert.NilError(t, err) | ||
|
||
assert.Equal(t, len(loadedTokens), len(tokensMetadata)) | ||
|
||
for i, metadata := range tokensMetadata { | ||
assert.Equal(t, loadedTokens[i], metadata) | ||
} | ||
} | ||
|
||
func TestLoadTokensFromUrlReturnsNoTokensWhenRequestErrorHappens(t *testing.T) { | ||
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNotFound) | ||
})) | ||
defer httpServer.Close() | ||
|
||
loadedTokens, err := LoadTokens(httpServer.URL) | ||
assert.Error(t, err, fmt.Sprintf("failed to load tokens from %s: %v %s", httpServer.URL, http.StatusNotFound, http.StatusText(http.StatusNotFound))) | ||
assert.Equal(t, len(loadedTokens), 0) | ||
} |
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.