Skip to content

Commit 7dd25f6

Browse files
author
Nick
authored
[por] minor updates for m7 integration (#2466)
* feat: updates for m7 * chore: include into url defs
1 parent 3c70c4f commit 7dd25f6

3 files changed

Lines changed: 89 additions & 6 deletions

File tree

node/pkg/fetcher/utils.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ import (
1313
"github.com/rs/zerolog/log"
1414
)
1515

16-
func FetchSingle(ctx context.Context, definition *Definition) (float64, error) {
17-
rawResult, err := request.Request[interface{}](
16+
func FetchSingle(ctx context.Context, definition *Definition, reqOpts ...request.RequestOption) (float64, error) {
17+
requestOptions := []request.RequestOption{
1818
request.WithEndpoint(*definition.Url),
1919
request.WithHeaders(definition.Headers),
20-
request.WithTimeout(10*time.Second),
21-
)
20+
request.WithTimeout(10 * time.Second),
21+
}
22+
23+
requestOptions = append(requestOptions, reqOpts...)
24+
25+
rawResult, err := request.Request[interface{}](requestOptions...)
2226
if err != nil {
2327
return 0, err
2428
}
29+
2530
return reducer.Reduce(rawResult, definition.Reducers)
2631
}
2732

node/pkg/por/app.go

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ import (
1212
"sync"
1313
"time"
1414

15+
"github.com/rs/zerolog/log"
16+
1517
"bisonai.com/miko/node/pkg/chain/helper"
1618
chainUtils "bisonai.com/miko/node/pkg/chain/utils"
19+
"bisonai.com/miko/node/pkg/common/types"
20+
"bisonai.com/miko/node/pkg/db"
1721
errorSentinel "bisonai.com/miko/node/pkg/error"
1822
"bisonai.com/miko/node/pkg/fetcher"
1923
"bisonai.com/miko/node/pkg/secrets"
2024
"bisonai.com/miko/node/pkg/utils/request"
2125
"bisonai.com/miko/node/pkg/utils/retrier"
22-
"github.com/rs/zerolog/log"
2326
)
2427

2528
const (
@@ -34,10 +37,47 @@ var urls = map[string]urlEntry{
3437
"peg-por": {
3538
"/{CHAIN}/peg-{CHAIN}.por.json",
3639
"/{CHAIN}/peg.por.json",
40+
false,
3741
},
3842
"gp": {
3943
"/{CHAIN}/gp-{CHAIN}.json",
4044
"/{CHAIN}/gp.json",
45+
false,
46+
},
47+
"aapl": {
48+
"/{CHAIN}/aapl-{CHAIN}.json",
49+
"/{CHAIN}/aapl.json",
50+
true,
51+
},
52+
"amzn": {
53+
"/{CHAIN}/amzn-{CHAIN}.json",
54+
"/{CHAIN}/amzn.json",
55+
true,
56+
},
57+
"googl": {
58+
"/{CHAIN}/googl-{CHAIN}.json",
59+
"/{CHAIN}/googl.json",
60+
true,
61+
},
62+
"meta": {
63+
"/{CHAIN}/meta-{CHAIN}.json",
64+
"/{CHAIN}/meta.json",
65+
true,
66+
},
67+
"msft": {
68+
"/{CHAIN}/msft-{CHAIN}.json",
69+
"/{CHAIN}/msft.json",
70+
true,
71+
},
72+
"nvda": {
73+
"/{CHAIN}/nvda-{CHAIN}.json",
74+
"/{CHAIN}/nvda.json",
75+
true,
76+
},
77+
"tsla": {
78+
"/{CHAIN}/tsla-{CHAIN}.json",
79+
"/{CHAIN}/tsla.json",
80+
true,
4181
},
4282
}
4383

@@ -84,6 +124,7 @@ func New(ctx context.Context) (*app, error) {
84124
definition: d,
85125
adapter: ad,
86126
aggregator: ag,
127+
useProxy: u.useProxy,
87128
}
88129

89130
entries[n] = e
@@ -104,9 +145,15 @@ func New(ctx context.Context) (*app, error) {
104145
return nil, err
105146
}
106147

148+
proxies, err := db.QueryRows[types.Proxy](ctx, "SELECT * FROM proxies", nil)
149+
if err != nil {
150+
return nil, err
151+
}
152+
107153
return &app{
108154
entries: entries,
109155
kaiaHelper: chainHelper,
156+
proxies: proxies,
110157
}, nil
111158
}
112159

@@ -183,7 +230,7 @@ func (a *app) startJob(ctx context.Context, entry entry) {
183230
}
184231

185232
func (a *app) execute(ctx context.Context, e entry) error {
186-
v, err := fetcher.FetchSingle(ctx, e.definition)
233+
v, err := fetcher.FetchSingle(ctx, e.definition, a.buildRequestOpts(e)...)
187234
if err != nil {
188235
return err
189236
}
@@ -319,3 +366,26 @@ func (a *app) getRoundId(ctx context.Context, e entry) (uint32, error) {
319366

320367
return RoundID, nil
321368
}
369+
370+
func (a *app) getNextProxy() *string {
371+
a.Lock()
372+
defer a.Unlock()
373+
374+
if len(a.proxies) == 0 {
375+
return nil
376+
}
377+
378+
proxy := a.proxies[a.proxyIdx%len(a.proxies)].GetProxyUrl()
379+
a.proxyIdx++
380+
return &proxy
381+
}
382+
383+
func (a *app) buildRequestOpts(e entry) []request.RequestOption {
384+
opts := []request.RequestOption{}
385+
if e.useProxy {
386+
if p := a.getNextProxy(); p != nil && *p != "" {
387+
opts = append(opts, request.WithProxy(*p))
388+
}
389+
}
390+
return opts
391+
}

node/pkg/por/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package por
33
import (
44
"encoding/json"
55
"math/big"
6+
"sync"
67
"time"
78

89
"bisonai.com/miko/node/pkg/chain/helper"
10+
"bisonai.com/miko/node/pkg/common/types"
911
"bisonai.com/miko/node/pkg/fetcher"
1012
)
1113

@@ -37,12 +39,17 @@ const (
3739
type app struct {
3840
entries map[string]entry
3941
kaiaHelper *helper.ChainHelper
42+
43+
proxies []types.Proxy
44+
proxyIdx int
45+
sync.Mutex
4046
}
4147

4248
type entry struct {
4349
definition *fetcher.Definition
4450
adapter adaptor
4551
aggregator aggregator
52+
useProxy bool
4653
}
4754

4855
type feed struct {
@@ -73,4 +80,5 @@ type lastInfo struct {
7380

7481
type urlEntry struct {
7582
adapterEndpoint, aggregatorEndpoint string
83+
useProxy bool
7684
}

0 commit comments

Comments
 (0)