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-08-12 12:53:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
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 {
|
|
|
|
log.Error("failed to get crypto on ramp", "id", provider.ID(), "error", err)
|
|
|
|
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
|
|
|
}
|