feat(wallet)_: added supported chains and recurrent purchase url to onramp providers

This commit is contained in:
Dario Gabriel Lipicar 2024-05-28 10:16:15 -03:00 committed by dlipicar
parent 69514629f0
commit 717c7df690
5 changed files with 176 additions and 155 deletions

View File

@ -25,6 +25,7 @@ import (
wcommon "github.com/status-im/status-go/services/wallet/common" wcommon "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/currency" "github.com/status-im/status-go/services/wallet/currency"
"github.com/status-im/status-go/services/wallet/history" "github.com/status-im/status-go/services/wallet/history"
"github.com/status-im/status-go/services/wallet/onramp"
"github.com/status-im/status-go/services/wallet/router" "github.com/status-im/status-go/services/wallet/router"
"github.com/status-im/status-go/services/wallet/thirdparty" "github.com/status-im/status-go/services/wallet/thirdparty"
"github.com/status-im/status-go/services/wallet/token" "github.com/status-im/status-go/services/wallet/token"
@ -304,7 +305,7 @@ func (api *API) WatchTransactionByChainID(ctx context.Context, chainID uint64, t
} }
} }
func (api *API) GetCryptoOnRamps(ctx context.Context) ([]CryptoOnRamp, error) { func (api *API) GetCryptoOnRamps(ctx context.Context) ([]onramp.CryptoOnRamp, error) {
return api.s.cryptoOnRampManager.Get() return api.s.cryptoOnRampManager.Get()
} }

View File

@ -0,0 +1,121 @@
package onramp
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
)
type DataSourceType int
const (
DataSourceHTTP DataSourceType = iota + 1
DataSourceStatic
)
type CryptoOnRamp struct {
Name string `json:"name"`
Description string `json:"description"`
Fees string `json:"fees"`
LogoURL string `json:"logoUrl"`
SiteURL string `json:"siteUrl"`
RecurrentSiteURL string `json:"recurrentSiteUrl"`
Hostname string `json:"hostname"`
Params map[string]string `json:"params"` // TODO implement params in JSON and parsing status-mobile
SupportedChainIDs []uint64 `json:"supportedChainIds"`
}
type Options struct {
DataSource string
DataSourceType DataSourceType
}
type Manager struct {
options *Options
ramps []CryptoOnRamp
lastCalled time.Time
}
func NewManager(options *Options) *Manager {
return &Manager{
options: options,
}
}
func (c *Manager) Get() ([]CryptoOnRamp, error) {
var ramps []CryptoOnRamp
var err error
switch c.options.DataSourceType {
case DataSourceHTTP:
if !c.hasCacheExpired(time.Now()) {
return c.ramps, nil
}
ramps, err = c.getFromHTTPDataSource()
c.lastCalled = time.Now()
case DataSourceStatic:
ramps, err = c.getFromStaticDataSource()
default:
return nil, fmt.Errorf("unsupported Manager.DataSourceType '%d'", c.options.DataSourceType)
}
if err != nil {
return nil, err
}
c.ramps = ramps
return c.ramps, nil
}
func (c *Manager) hasCacheExpired(t time.Time) bool {
// If lastCalled + 1 hour is before the given time, then 1 hour hasn't passed yet
return c.lastCalled.Add(time.Hour).Before(t)
}
func (c *Manager) getFromHTTPDataSource() ([]CryptoOnRamp, error) {
if c.options.DataSource == "" {
return nil, errors.New("data source is not set for Manager")
}
sgc := http.Client{
Timeout: time.Second * 5,
}
req, err := http.NewRequest(http.MethodGet, c.options.DataSource, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "status-go")
res, err := sgc.Do(req)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
fmt.Println(string(body))
var ramps []CryptoOnRamp
err = json.Unmarshal(body, &ramps)
if err != nil {
return nil, err
}
return ramps, nil
}
func (c *Manager) getFromStaticDataSource() ([]CryptoOnRamp, error) {
return getOnRampProviders(), nil
}

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
package wallet package onramp
import ( import (
"net/http" "net/http"
@ -10,18 +10,18 @@ import (
) )
const ( const (
path = "../../_assets/tests/" path = "../../../_assets/tests/"
) )
func TestCryptoOnRamps_Get(t *testing.T) { func TestCryptoOnRamps_Get(t *testing.T) {
s := httptest.NewServer(http.FileServer(http.Dir(path))) s := httptest.NewServer(http.FileServer(http.Dir(path)))
defer s.Close() defer s.Close()
cs := []*CryptoOnRampManager{ cs := []*Manager{
{options: &CryptoOnRampOptions{dataSourceType: DataSourceStatic}}, {options: &Options{DataSourceType: DataSourceStatic}},
{options: &CryptoOnRampOptions{ {options: &Options{
dataSourceType: DataSourceHTTP, DataSourceType: DataSourceHTTP,
dataSource: s.URL + "/ramps.json", DataSource: s.URL + "/ramps.json",
}}, }},
} }
@ -38,9 +38,9 @@ func TestCryptoOnRampManager_hasCacheExpired(t *testing.T) {
s := httptest.NewServer(http.FileServer(http.Dir(path))) s := httptest.NewServer(http.FileServer(http.Dir(path)))
defer s.Close() defer s.Close()
corm := NewCryptoOnRampManager(&CryptoOnRampOptions{ corm := NewManager(&Options{
dataSourceType: DataSourceHTTP, DataSourceType: DataSourceHTTP,
dataSource: s.URL + "/ramps.json", DataSource: s.URL + "/ramps.json",
}) })
nt := time.Time{}.Add(30 * time.Minute) nt := time.Time{}.Add(30 * time.Minute)

View File

@ -27,6 +27,7 @@ import (
"github.com/status-im/status-go/services/wallet/currency" "github.com/status-im/status-go/services/wallet/currency"
"github.com/status-im/status-go/services/wallet/history" "github.com/status-im/status-go/services/wallet/history"
"github.com/status-im/status-go/services/wallet/market" "github.com/status-im/status-go/services/wallet/market"
"github.com/status-im/status-go/services/wallet/onramp"
"github.com/status-im/status-go/services/wallet/thirdparty" "github.com/status-im/status-go/services/wallet/thirdparty"
"github.com/status-im/status-go/services/wallet/thirdparty/alchemy" "github.com/status-im/status-go/services/wallet/thirdparty/alchemy"
"github.com/status-im/status-go/services/wallet/thirdparty/coingecko" "github.com/status-im/status-go/services/wallet/thirdparty/coingecko"
@ -60,8 +61,8 @@ func NewService(
feed *event.Feed, feed *event.Feed,
mediaServer *server.MediaServer, mediaServer *server.MediaServer,
) *Service { ) *Service {
cryptoOnRampManager := NewCryptoOnRampManager(&CryptoOnRampOptions{ cryptoOnRampManager := onramp.NewManager(&onramp.Options{
dataSourceType: DataSourceStatic, DataSourceType: onramp.DataSourceStatic,
}) })
signals := &walletevent.SignalsTransmitter{ signals := &walletevent.SignalsTransmitter{
@ -210,7 +211,7 @@ type Service struct {
communityManager *community.Manager communityManager *community.Manager
transactionManager *transfer.TransactionManager transactionManager *transfer.TransactionManager
pendingTxManager *transactions.PendingTxTracker pendingTxManager *transactions.PendingTxTracker
cryptoOnRampManager *CryptoOnRampManager cryptoOnRampManager *onramp.Manager
transferController *transfer.Controller transferController *transfer.Controller
marketManager *market.Manager marketManager *market.Manager
started bool started bool