-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfactory.go
More file actions
81 lines (71 loc) · 2.16 KB
/
factory.go
File metadata and controls
81 lines (71 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package gateway
import (
"encoding/json"
"fmt"
"github.com/KiraCore/sai-interx-manager/logger"
"go.uber.org/zap"
"time"
saiService "github.com/KiraCore/sai-service/service"
"github.com/spf13/cast"
"github.com/KiraCore/sai-interx-manager/types"
)
type GatewayFactory struct {
context *saiService.Context
storage types.Storage
}
func NewGatewayFactory(context *saiService.Context, storage types.Storage) *GatewayFactory {
return &GatewayFactory{
context: context,
storage: storage,
}
}
func (f *GatewayFactory) CreateGateway(gatewayType string) (types.Gateway, error) {
switch gatewayType {
case "ethereum":
return NewEthereumGateway(
f.context,
cast.ToStringMapString(f.context.GetConfig("ethereum.nodes", map[string]string{})),
f.storage,
cast.ToInt(f.context.GetConfig("ethereum.retries", 1)),
time.Duration(cast.ToInt64(f.context.GetConfig("ethereum.retry_delay", 10))),
cast.ToInt(f.context.GetConfig("ethereum.rate_limit", 10)),
)
case "cosmos":
var cosmosConfig types.CosmosConfig
configBytes, err := json.Marshal(f.context.GetConfig("cosmos", cosmosConfig))
if err != nil {
logger.Logger.Error("Invalid cosmos configuration format")
return nil, err
}
err = json.Unmarshal(configBytes, &cosmosConfig)
if err != nil {
logger.Logger.Error("Invalid cosmos configuration format")
return nil, err
}
return NewCosmosGateway(
f.context,
f.storage,
cosmosConfig,
)
case "bitcoin":
return NewBitcoinGateway(
f.context,
cast.ToString(f.context.GetConfig("bitcoin.url", "")),
cast.ToInt(f.context.GetConfig("bitcoin.retries", 1)),
time.Duration(cast.ToInt64(f.context.GetConfig("bitcoin.retry_delay", 10))),
cast.ToInt(f.context.GetConfig("bitcoin.rate_limit", 10)),
)
case "storage":
return NewStorageGateway(
f.context,
f.storage,
cast.ToInt(f.context.GetConfig("storage.retries", 1)),
time.Duration(cast.ToInt64(f.context.GetConfig("storage.retry_delay", 10))),
cast.ToInt(f.context.GetConfig("storage.rate_limit", 10)),
)
default:
err := fmt.Errorf("unknown gateway type: %s", gatewayType)
logger.Logger.Error("GatewayFactory - CreateGateway", zap.Error(err))
return nil, err
}
}