Skip to content

Commit

Permalink
New methods for validation package (#17)
Browse files Browse the repository at this point in the history
* Add func for GET HTTP request

* Add GetHTTPResponseCode to pkg

* Add methods for getting external asset info

* Make separate jobs for CI
  • Loading branch information
unanoc authored Nov 29, 2021
1 parent f012f83 commit f5259e9
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 9 deletions.
24 changes: 18 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,41 @@ name: CI

on:
push:
branches: [ main ]
branches: [ master ]
pull_request:
branches: [ main ]
branches: [ master ]

jobs:
test:
name: Unit
name: Unit test
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.17
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
id: go

- name: Check out code
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
- name: Unit Test
run: make test

check:
name: Linter
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
id: go

- name: Check out code
uses: actions/checkout@v2

- name: Lint
run: make lint
run: make lint
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTK
github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
45 changes: 45 additions & 0 deletions pkg/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package pkg

import (
"encoding/json"
"io/ioutil"
"net/http"

"github.com/pkg/errors"
)

// nolint: noctx
func GetHTTPResponse(url string, v interface{}) error {
res, err := http.Get(url)
if err != nil {
return errors.Wrap(err, "failed to make GET request")
}
defer res.Body.Close()

if res.StatusCode != 200 {
return errors.New("failed to obtain json")
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return errors.Wrap(err, "failed to read bytes from body")
}

err = json.Unmarshal(body, v)
if err != nil {
return errors.Wrap(err, "failed to unmarshal json")
}

return nil
}

// nolint: noctx
func GetHTTPResponseCode(url string) (int, error) {
res, err := http.Get(url)
if err != nil {
return 0, errors.Wrap(err, "failed to make GET request")
}
defer res.Body.Close()

return res.StatusCode, nil
}
8 changes: 8 additions & 0 deletions pkg/validation/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,11 @@ func ValidateAddress(address string, prefix string, length int) error {

return nil
}

func IsEthereumAddress(addr string) bool {
if len(addr) == 40 || len(addr) == 42 && strings.HasPrefix(addr, "0x") {
return true
}

return false
}
42 changes: 42 additions & 0 deletions pkg/validation/info/external.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package info

import (
"fmt"
"strings"

"github.com/trustwallet/assets-go-libs/pkg"
)

type ExternalTokenInfo struct {
Symbol string `json:"symbol"`
Decimals int `json:"decimals"`
HoldersCount int `json:"holdersCount"`
}

func GetExternalTokenInfo(tokenID, tokentType string) (*ExternalTokenInfo, error) {
switch strings.ToLower(tokentType) {
case "erc20":
return GetTokenInfoForERC20(tokenID)
case "bep20":
return GetTokenInfoForBEP20(tokenID)
}

return nil, nil
}

func GetTokenInfoForERC20(tokenID string) (*ExternalTokenInfo, error) {
url := fmt.Sprintf("https://api.ethplorer.io/getTokenInfo/%s?apiKey=freekey", tokenID)

var result ExternalTokenInfo
err := pkg.GetHTTPResponse(url, &result)
if err != nil {
return nil, err
}

return &result, nil
}

// TODO: Implement it.
func GetTokenInfoForBEP20(tokenID string) (*ExternalTokenInfo, error) {
return nil, nil
}
4 changes: 1 addition & 3 deletions pkg/validation/info/fields_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,7 @@ func ValidateExplorer(explorer, name string, chain coin.Coin, addr string) error
}

if matchCount == 0 {
err := fmt.Errorf("invalid value for explorer field, %s insted of %s", explorerActual, explorerExpected)

return err
return fmt.Errorf("invalid value for explorer field, %s insted of %s", explorerActual, explorerExpected)
}
}

Expand Down

0 comments on commit f5259e9

Please sign in to comment.