2024-05-28 13:16:15 +00:00
|
|
|
package onramp
|
|
|
|
|
|
|
|
import (
|
2024-08-12 12:53:32 +00:00
|
|
|
"context"
|
2024-05-28 13:16:15 +00:00
|
|
|
"errors"
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/logutils"
|
2024-05-28 13:16:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Manager struct {
|
2024-08-12 12:53:32 +00:00
|
|
|
providers []Provider
|
2024-05-28 13:16:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 12:53:32 +00:00
|
|
|
func NewManager(providers []Provider) *Manager {
|
2024-05-28 13:16:15 +00:00
|
|
|
return &Manager{
|
2024-08-12 12:53:32 +00:00
|
|
|
providers: providers,
|
2024-05-28 13:16:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 12:53:32 +00:00
|
|
|
func (c *Manager) GetProviders(ctx context.Context) ([]CryptoOnRamp, error) {
|
|
|
|
ret := make([]CryptoOnRamp, 0, len(c.providers))
|
|
|
|
for _, provider := range c.providers {
|
|
|
|
cryptoOnRamp, err := provider.GetCryptoOnRamp(ctx)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("failed to get crypto on ramp", zap.String("id", provider.ID()), zap.Error(err))
|
2024-08-12 12:53:32 +00:00
|
|
|
continue
|
2024-05-28 13:16:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 12:53:32 +00:00
|
|
|
ret = append(ret, cryptoOnRamp)
|
2024-05-28 13:16:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 12:53:32 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
2024-05-28 13:16:15 +00:00
|
|
|
|
2024-08-12 12:53:32 +00:00
|
|
|
func (c *Manager) GetURL(ctx context.Context, providerID string, parameters Parameters) (string, error) {
|
|
|
|
for _, provider := range c.providers {
|
|
|
|
if provider.ID() != providerID {
|
|
|
|
continue
|
|
|
|
}
|
2024-05-28 13:16:15 +00:00
|
|
|
|
2024-08-12 12:53:32 +00:00
|
|
|
return provider.GetURL(ctx, parameters)
|
2024-05-28 13:16:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 12:53:32 +00:00
|
|
|
return "", errors.New("provider not found")
|
2024-05-28 13:16:15 +00:00
|
|
|
}
|