chore_: remove exchanges module
This commit is contained in:
parent
11cf42bedd
commit
e85a0808f7
|
@ -1,12 +0,0 @@
|
|||
.PHONY: regen fetch test
|
||||
|
||||
regen:
|
||||
go run cmd/main.go --from-json-file=exchanges.json
|
||||
gofmt -w exchanges.go
|
||||
|
||||
fetch:
|
||||
go run cmd/main.go
|
||||
gofmt -w exchanges.go
|
||||
|
||||
test:
|
||||
go test -v -cover ./...
|
|
@ -1,89 +0,0 @@
|
|||
package exchanges
|
||||
|
||||
/*--------------------------------+
|
||||
| Code generated by exchanges |
|
||||
| DO NOT EDIT |
|
||||
+--------------------------------*/
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
type Exchange struct {
|
||||
code string
|
||||
name string
|
||||
symbol string
|
||||
logo string
|
||||
addresses []common.Address
|
||||
}
|
||||
|
||||
func (e *Exchange) Code() string { return e.code }
|
||||
|
||||
func (e *Exchange) Name() string { return e.name }
|
||||
|
||||
func (e *Exchange) Symbol() string { return e.symbol }
|
||||
|
||||
func (e *Exchange) Logo() string { return e.logo }
|
||||
|
||||
func (e *Exchange) Addresses() []common.Address { return e.addresses }
|
||||
|
||||
// Get returns an exchange struct if the provided
|
||||
// code is contained within the valid codes. Otherwise
|
||||
// an error will be returned
|
||||
func GetCentralizedExchangeWithCode(code string) (*Exchange, error) {
|
||||
if Valid(code) {
|
||||
val, ok := centralizedExchangesByCode[code]
|
||||
if ok {
|
||||
return val, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("exchange: could not find exchange with code: %q", code)
|
||||
}
|
||||
|
||||
// Get returns an exchange struct which owns the given
|
||||
// address. If the address does not belong to any exchange,
|
||||
// nil will be returned
|
||||
func GetCentralizedExchangeWithAddress(address common.Address) (*Exchange) {
|
||||
return centralizedExchangesByAddress[address.String()]
|
||||
}
|
||||
|
||||
// Valid checks if a provided code is contained
|
||||
// inside the provided ValidCodes slice
|
||||
func Valid(code string) bool {
|
||||
for _, c := range ValidCodes {
|
||||
if c == code {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Following are all the structs containing exchange data
|
||||
var (
|
||||
{{ range $k, $v := . -}}
|
||||
// {{$v.Code}} Exchange struct
|
||||
{{toVariableName $v.Code}} = Exchange{ code: "{{$v.Code}}", name: "{{$v.Name}}", symbol: "{{$v.Symbol}}", logo: "{{$v.Logo}}", addresses: []common.Address{ {{addressesJoin $v.Addresses}} } }
|
||||
{{ end }}
|
||||
)
|
||||
|
||||
var centralizedExchangesByCode = map[string]*Exchange{
|
||||
{{ range $k, $v := . -}}
|
||||
"{{$v.Code}}": &{{toVariableName $v.Code}},
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
var centralizedExchangesByAddress = map[string]*Exchange{
|
||||
{{ range $k, $v := . -}}
|
||||
{{ range $i, $a := $v.Addresses}}
|
||||
"{{$a}}": &{{toVariableName $v.Code}},
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
var ValidCodes = []string{
|
||||
{{ range $k, $v := . -}}
|
||||
"{{$v.Code}}",
|
||||
{{ end }}
|
||||
}
|
|
@ -1,351 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/status-im/status-go/exchanges/cmd/scaffold"
|
||||
)
|
||||
|
||||
const (
|
||||
templateFile = "cmd/exchanges.txt" // must include the cmd prefix because this code is called from the Makefile
|
||||
outputGoFile = "exchanges.go"
|
||||
outputJsonFile = "exchanges.json"
|
||||
|
||||
baseUrl = "https://sapi.coincarp.com/api"
|
||||
walletUrl = baseUrl + "/v1/market/walletscreen/coin/wallet"
|
||||
walletAddressUrl = baseUrl + "/v1/market/walletscreen/coin/walletaddress"
|
||||
iconBaseUrl = "https://s1.coincarp.com"
|
||||
|
||||
ethereumCode = "ethereum"
|
||||
mainnetChainType = ""
|
||||
initialPageSize = 30
|
||||
|
||||
maxRetries = 10
|
||||
|
||||
requestWaitTime = 1000 * time.Millisecond
|
||||
requestTimeout = 5 * time.Second
|
||||
|
||||
zeroAddress = "0x0000000000000000000000000000000000000000"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.SetFlags(log.Lshortfile | log.LstdFlags)
|
||||
|
||||
var fromJsonFile string
|
||||
|
||||
flag.StringVar(&fromJsonFile, "from-json-file", "", "Path to JSON file to use instead of remote source")
|
||||
flag.Parse()
|
||||
|
||||
var exchangesData []exchangeData
|
||||
if fromJsonFile == "" {
|
||||
log.Println("Fetching from external service...")
|
||||
exchangesData = getExchangesData()
|
||||
} else {
|
||||
log.Println("Fetching from JSON file...")
|
||||
exchangesData = loadExchangesDataFromJson(fromJsonFile)
|
||||
}
|
||||
|
||||
log.Println("Generating files...")
|
||||
for _, gen := range generators {
|
||||
gen(exchangesData)
|
||||
}
|
||||
}
|
||||
|
||||
func doRequest(url string) ([]byte, error) {
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:96.0) Gecko/20100101 Firefox/96.0")
|
||||
|
||||
// Ensure wait time between requests
|
||||
time.Sleep(requestWaitTime)
|
||||
|
||||
client := http.Client{
|
||||
Timeout: requestTimeout,
|
||||
}
|
||||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
statusCode := res.StatusCode
|
||||
if statusCode != http.StatusOK {
|
||||
err := fmt.Errorf("unsuccessful request: %s - %d %s", url, statusCode, http.StatusText(statusCode))
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var dataInfo scaffold.DataInfo
|
||||
if err = json.Unmarshal(b, &dataInfo); err != nil {
|
||||
fmt.Println("unmarshall error: ", url)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if dataInfo.Code != http.StatusOK {
|
||||
err := fmt.Errorf("inconsistent response: %s - %d %s", url, dataInfo.Code, dataInfo.Msg)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func getExchangesData() []exchangeData {
|
||||
log.Println("Fetching exchanges list...")
|
||||
exchanges, err := getLatestExchangeList()
|
||||
if err != nil {
|
||||
log.Fatalf("could not get list of exchanges: %v", err)
|
||||
}
|
||||
|
||||
exchangesData := make([]exchangeData, 0, 128)
|
||||
for _, exchange := range exchanges {
|
||||
log.Println("Fetching address list for exchange:", exchange.Name)
|
||||
addresses, err := getLatestExchangeAddresses(exchange.Code)
|
||||
if err != nil {
|
||||
log.Fatalf("could not get list of addresses: %v", err)
|
||||
}
|
||||
exchangeData := buildExchangeData(exchange, addresses)
|
||||
exchangesData = append(exchangesData, exchangeData)
|
||||
}
|
||||
|
||||
if len(exchangesData) == 0 {
|
||||
log.Fatalf("could not build exchanges list")
|
||||
}
|
||||
|
||||
return exchangesData
|
||||
}
|
||||
|
||||
func loadExchangesDataFromJson(filePath string) []exchangeData {
|
||||
file, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var data []exchangeData
|
||||
err = json.Unmarshal(file, &data)
|
||||
if err != nil {
|
||||
log.Fatalf("cannot unmarshal data: %v", err)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func getLatestExchangeList() ([]*scaffold.Exchange, error) {
|
||||
page := 1
|
||||
pageSize := initialPageSize
|
||||
retries := 0
|
||||
exchanges := make([]*scaffold.Exchange, 0, 128)
|
||||
|
||||
for {
|
||||
queryParams := url.Values{
|
||||
"code": {ethereumCode},
|
||||
"chainType": {mainnetChainType},
|
||||
"page": {strconv.Itoa(page)},
|
||||
"pageSize": {strconv.Itoa(pageSize)},
|
||||
"isexchange": {"false"},
|
||||
"lang": {"en-US"},
|
||||
}
|
||||
|
||||
url := walletUrl + "?" + queryParams.Encode()
|
||||
|
||||
b, err := doRequest(url)
|
||||
if err != nil {
|
||||
fmt.Println("request error:", err)
|
||||
if retries < maxRetries {
|
||||
page = 1
|
||||
pageSize++
|
||||
retries++
|
||||
exchanges = nil
|
||||
fmt.Println("retry", retries)
|
||||
continue
|
||||
}
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var data scaffold.ExchangesData
|
||||
if err = json.Unmarshal(b, &data); err != nil {
|
||||
fmt.Println("unmarshall error: ", url)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
exchanges = append(exchanges, data.Entries.List...)
|
||||
|
||||
if page >= data.Entries.TotalPages {
|
||||
break
|
||||
}
|
||||
|
||||
page++
|
||||
}
|
||||
|
||||
return exchanges, nil
|
||||
}
|
||||
|
||||
func getLatestExchangeAddresses(exchangeCode string) ([]*scaffold.ExchangeAddress, error) {
|
||||
page := 1
|
||||
pageSize := initialPageSize
|
||||
retries := 0
|
||||
addresses := make([]*scaffold.ExchangeAddress, 0, 128)
|
||||
|
||||
for {
|
||||
queryParams := url.Values{
|
||||
"code": {ethereumCode},
|
||||
"exchangecode": {exchangeCode},
|
||||
"chainType": {mainnetChainType},
|
||||
"page": {strconv.Itoa(page)},
|
||||
"pageSize": {strconv.Itoa(pageSize)},
|
||||
"lang": {"en-US"},
|
||||
}
|
||||
|
||||
url := walletAddressUrl + "?" + queryParams.Encode()
|
||||
|
||||
b, err := doRequest(url)
|
||||
if err != nil {
|
||||
fmt.Println("request error:", err)
|
||||
if retries < maxRetries {
|
||||
page = 1
|
||||
pageSize++
|
||||
retries++
|
||||
addresses = nil
|
||||
fmt.Println("retry", retries)
|
||||
continue
|
||||
}
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var data scaffold.ExchangeAddressesData
|
||||
if err = json.Unmarshal(b, &data); err != nil {
|
||||
fmt.Println("unmarshall error: ", url)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
addresses = append(addresses, data.Entries.List...)
|
||||
|
||||
if page >= data.Entries.TotalPages {
|
||||
break
|
||||
}
|
||||
|
||||
page++
|
||||
}
|
||||
|
||||
return addresses, nil
|
||||
}
|
||||
|
||||
type exchangeData struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Symbol string `json:"symbol"`
|
||||
Logo string `json:"logo"`
|
||||
Addresses []common.Address `json:"addresses"`
|
||||
}
|
||||
|
||||
func buildExchangeData(exchange *scaffold.Exchange, addresses []*scaffold.ExchangeAddress) exchangeData {
|
||||
data := exchangeData{
|
||||
Code: exchange.Code,
|
||||
Name: exchange.Name,
|
||||
Symbol: exchange.Symbol,
|
||||
Logo: iconBaseUrl + exchange.Logo,
|
||||
Addresses: []common.Address{},
|
||||
}
|
||||
|
||||
for _, exchangeAddress := range addresses {
|
||||
address := common.HexToAddress(exchangeAddress.Address)
|
||||
if address.Hex() == zeroAddress {
|
||||
continue
|
||||
}
|
||||
data.Addresses = append(data.Addresses, address)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
type generatorFunc func(exchangesData []exchangeData)
|
||||
|
||||
var generators = []generatorFunc{
|
||||
generateJsonFile,
|
||||
generateGoPackage,
|
||||
}
|
||||
|
||||
func generateJsonFile(exchangesData []exchangeData) {
|
||||
file, err := json.MarshalIndent(exchangesData, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("cannot marshal data: %v", err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(outputJsonFile, file, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func toVariableName(input string) string {
|
||||
return "exchange_" + strings.ToLower(strings.Replace(input, "-", "_", -1))
|
||||
}
|
||||
|
||||
func addressesJoin(addresses []common.Address) string {
|
||||
list := make([]string, 0, len(addresses))
|
||||
|
||||
for _, address := range addresses {
|
||||
list = append(list, "common.HexToAddress(\""+address.String()+"\")")
|
||||
}
|
||||
|
||||
return strings.Join(list, ", ")
|
||||
}
|
||||
|
||||
func generateGoPackage(exchangesData []exchangeData) {
|
||||
tpl, err := ioutil.ReadFile(templateFile)
|
||||
if err != nil {
|
||||
log.Fatalf("cannot open template file: %v", err)
|
||||
}
|
||||
|
||||
funcMap := template.FuncMap{
|
||||
"toVariableName": toVariableName,
|
||||
"addressesJoin": addressesJoin,
|
||||
}
|
||||
|
||||
t := template.Must(template.New("go").Funcs(funcMap).Parse(string(tpl)))
|
||||
buf := new(bytes.Buffer)
|
||||
err = t.Execute(buf, exchangesData)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
formatted, err := format.Source(buf.Bytes())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
buf = bytes.NewBuffer(formatted)
|
||||
|
||||
to, err := os.Create(outputGoFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer to.Close()
|
||||
|
||||
_, err = io.Copy(to, buf)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
package scaffold
|
||||
|
||||
type DataInfo struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
type EntriesInfo struct {
|
||||
TotalCount int `json:"total_count"`
|
||||
TotalPages int `json:"total_pages"`
|
||||
Page int `json:"page"`
|
||||
}
|
||||
|
||||
type Exchange struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Symbol string `json:"symbol"`
|
||||
Logo string `json:"logo"`
|
||||
}
|
||||
|
||||
type ExchangesEntries struct {
|
||||
EntriesInfo
|
||||
List []*Exchange `json:"list"`
|
||||
}
|
||||
|
||||
type ExchangesData struct {
|
||||
DataInfo
|
||||
Entries *ExchangesEntries `json:"data"`
|
||||
}
|
||||
|
||||
type ExchangeAddress struct {
|
||||
Address string `json:"address"`
|
||||
Flag string `json:"flag"`
|
||||
}
|
||||
|
||||
type ExchangesAddressesEntries struct {
|
||||
EntriesInfo
|
||||
List []*ExchangeAddress `json:"list"`
|
||||
}
|
||||
|
||||
type ExchangeAddressesData struct {
|
||||
DataInfo
|
||||
Entries *ExchangesAddressesEntries `json:"data"`
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -1,44 +0,0 @@
|
|||
package exchanges
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
func TestNullAddress(t *testing.T) {
|
||||
address := common.HexToAddress("0x0")
|
||||
exchange := GetCentralizedExchangeWithAddress(address)
|
||||
|
||||
require.Empty(t, exchange)
|
||||
}
|
||||
|
||||
func TestBinanceWithCode(t *testing.T) {
|
||||
exchange, err := GetCentralizedExchangeWithCode("binance")
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, exchange)
|
||||
require.Equal(t, exchange.Name(), "Binance")
|
||||
}
|
||||
|
||||
func TestBinanceWithAddress(t *testing.T) {
|
||||
// Address "Binance 3"
|
||||
// https://etherscan.io/address/0x564286362092d8e7936f0549571a803b203aaced
|
||||
address := common.HexToAddress("0x564286362092D8e7936f0549571a803B203aAceD")
|
||||
exchange := GetCentralizedExchangeWithAddress(address)
|
||||
|
||||
require.NotEmpty(t, exchange)
|
||||
require.Equal(t, exchange.Name(), "Binance")
|
||||
}
|
||||
|
||||
func TestKrakenWithAddress(t *testing.T) {
|
||||
// Address "Kraken 4"
|
||||
// https://etherscan.io/address/0x267be1c1d684f78cb4f6a176c4911b741e4ffdc0
|
||||
address := common.HexToAddress("0x267be1C1D684F78cb4F6a176C4911b741E4Ffdc0")
|
||||
exchange := GetCentralizedExchangeWithAddress(address)
|
||||
|
||||
require.NotEmpty(t, exchange)
|
||||
require.Equal(t, exchange.Name(), "Kraken")
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
module github.com/status-im/status-go/exchanges
|
||||
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/ethereum/go-ethereum v1.10.26
|
||||
github.com/stretchr/testify v1.8.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
golang.org/x/crypto v0.1.0 // indirect
|
||||
golang.org/x/sys v0.6.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
|
@ -1,21 +0,0 @@
|
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/ethereum/go-ethereum v1.10.26 h1:i/7d9RBBwiXCEuyduBQzJw/mKmnvzsN14jqBmytw72s=
|
||||
github.com/ethereum/go-ethereum v1.10.26/go.mod h1:EYFyF19u3ezGLD4RqOkLq+ZCXzYbLoNDdZlMt7kyKFg=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
|
||||
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
|
||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
Loading…
Reference in New Issue