-
Notifications
You must be signed in to change notification settings - Fork 4
/
products.go
65 lines (53 loc) · 1.61 KB
/
products.go
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
package main
import (
"context"
"sync"
"time"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
"go.blockdaemon.com/pyth"
)
type productCacher struct {
symbols map[solana.PublicKey]string
client *pyth.Client
lock sync.RWMutex
}
// discoverProducts lists all products in the Pyth oracle. Creates a symbol cache and product list.
func discoverProducts(ctx context.Context, client *pyth.Client) (*productCacher, []pyth.ProductAccountEntry, error) {
products, err := client.GetAllProductAccounts(ctx, rpc.CommitmentProcessed)
if err != nil {
return nil, nil, err
}
symbols := make(map[solana.PublicKey]string)
for _, p := range products {
symbols[p.Pubkey] = p.Attrs.KVs()["symbol"]
}
return &productCacher{symbols: symbols}, products, nil
}
// getSymbol looks up a symbol name from the cache or fetches it from RPC.
func (p *productCacher) getSymbol(product solana.PublicKey) (string, error) {
sym, ok := p.getSymbolFromCache(product)
if ok {
return sym, nil
}
return p.fetchProduct(product)
}
func (p *productCacher) getSymbolFromCache(product solana.PublicKey) (string, bool) {
p.lock.RLock()
defer p.lock.RUnlock()
sym, ok := p.symbols[product]
return sym, ok
}
func (p *productCacher) fetchProduct(product solana.PublicKey) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
account, err := p.client.GetProductAccount(ctx, product, rpc.CommitmentProcessed)
if err != nil {
return "", err
}
p.lock.Lock()
defer p.lock.Unlock()
sym := account.Attrs.KVs()["symbol"]
p.symbols[product] = sym
return sym, nil
}