|
| 1 | +// Copyright 2022 Blockdaemon Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package pyth |
| 16 | + |
| 17 | +import ( |
| 18 | + "sync" |
| 19 | + |
| 20 | + "github.com/gagliardetto/solana-go" |
| 21 | + "github.com/shopspring/decimal" |
| 22 | +) |
| 23 | + |
| 24 | +// PriceEventHandler provides a callback-style interface to Pyth updates. |
| 25 | +type PriceEventHandler struct { |
| 26 | + stream *PriceAccountStream |
| 27 | + |
| 28 | + callbacksLock sync.Mutex // lock over the callbacks map |
| 29 | + regNonce uint64 |
| 30 | + callbacks map[solana.PublicKey]priceCallbacks |
| 31 | +} |
| 32 | + |
| 33 | +// NewPriceEventHandler creates a new event handler over the stream. |
| 34 | +// |
| 35 | +// A stream must not be re-used between event handlers. |
| 36 | +func NewPriceEventHandler(stream *PriceAccountStream) *PriceEventHandler { |
| 37 | + handler := &PriceEventHandler{ |
| 38 | + stream: stream, |
| 39 | + callbacks: make(map[solana.PublicKey]priceCallbacks), |
| 40 | + } |
| 41 | + go handler.consume(stream.Updates()) |
| 42 | + return handler |
| 43 | +} |
| 44 | + |
| 45 | +// Err returns the reason why the underlying price account stream is closed. |
| 46 | +// |
| 47 | +// Will block until the stream has actually closed. |
| 48 | +// Returns nil if closure was expected. |
| 49 | +// |
| 50 | +// After this function returns the event handler will not send any more callbacks. |
| 51 | +// You could use this function as a barrier for any cleanup tasks relating to callbacks. |
| 52 | +func (p *PriceEventHandler) Err() error { |
| 53 | + return p.stream.Err() |
| 54 | +} |
| 55 | + |
| 56 | +// OnPriceChange registers a callback function to be called |
| 57 | +// whenever the aggregate price of the provided price account changes. |
| 58 | +func (p *PriceEventHandler) OnPriceChange(priceKey solana.PublicKey, callback func(PriceUpdate)) CallbackHandle { |
| 59 | + p.callbacksLock.Lock() |
| 60 | + defer p.callbacksLock.Unlock() |
| 61 | + return p.getPriceCallbacks(priceKey).onPrice.register(p, callback) |
| 62 | +} |
| 63 | + |
| 64 | +// OnComponentChange registers a callback function to be called |
| 65 | +// whenever the price component of the given (price account, publisher account) pair changes. |
| 66 | +func (p *PriceEventHandler) OnComponentChange(priceKey solana.PublicKey, publisher solana.PublicKey, callback func(PriceUpdate)) CallbackHandle { |
| 67 | + p.callbacksLock.Lock() |
| 68 | + defer p.callbacksLock.Unlock() |
| 69 | + return p.getComponentCallbacks(priceKey, publisher).register(p, callback) |
| 70 | +} |
| 71 | + |
| 72 | +func (p *PriceEventHandler) getPriceCallbacks(priceKey solana.PublicKey) priceCallbacks { |
| 73 | + // requires lock |
| 74 | + res, ok := p.callbacks[priceKey] |
| 75 | + if !ok { |
| 76 | + res.init() |
| 77 | + p.callbacks[priceKey] = res |
| 78 | + } |
| 79 | + return res |
| 80 | +} |
| 81 | + |
| 82 | +func (p *PriceEventHandler) getComponentCallbacks(priceKey solana.PublicKey, publisherKey solana.PublicKey) callbackMap { |
| 83 | + // requires lock |
| 84 | + price := p.getPriceCallbacks(priceKey) |
| 85 | + res, ok := price.componentCallbacks[publisherKey] |
| 86 | + if !ok { |
| 87 | + res = make(callbackMap) |
| 88 | + price.componentCallbacks[publisherKey] = res |
| 89 | + } |
| 90 | + return res |
| 91 | +} |
| 92 | + |
| 93 | +func (p *PriceEventHandler) consume(updates <-chan PriceAccountUpdate) { |
| 94 | + for update := range updates { |
| 95 | + p.processUpdate(update.Pubkey, update.Price) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func (p *PriceEventHandler) processUpdate(priceKey solana.PublicKey, acc *PriceAccount) { |
| 100 | + p.callbacksLock.Lock() |
| 101 | + defer p.callbacksLock.Unlock() |
| 102 | + |
| 103 | + callbacks := p.callbacks[priceKey] |
| 104 | + for _, onPrice := range callbacks.onPrice { |
| 105 | + onPrice.inform(acc, &acc.Agg) |
| 106 | + } |
| 107 | + for _, comp := range acc.Components { |
| 108 | + if comp.Publisher.IsZero() { |
| 109 | + continue |
| 110 | + } |
| 111 | + compCbs := callbacks.componentCallbacks[comp.Publisher] |
| 112 | + for _, onPrice := range compCbs { |
| 113 | + onPrice.inform(acc, &comp.Latest) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +type priceCallbacks struct { |
| 119 | + onPrice callbackMap |
| 120 | + componentCallbacks map[solana.PublicKey]callbackMap |
| 121 | +} |
| 122 | + |
| 123 | +func (p *priceCallbacks) init() { |
| 124 | + p.onPrice = make(callbackMap) |
| 125 | + p.componentCallbacks = make(map[solana.PublicKey]callbackMap) |
| 126 | +} |
| 127 | + |
| 128 | +type callbackMap map[uint64]*callbackRegistration |
| 129 | + |
| 130 | +func (container callbackMap) register(p *PriceEventHandler, callback func(PriceUpdate)) CallbackHandle { |
| 131 | + // requires lock |
| 132 | + p.regNonce += 1 |
| 133 | + key := p.regNonce |
| 134 | + |
| 135 | + handle := CallbackHandle{ |
| 136 | + handler: p, |
| 137 | + container: container, |
| 138 | + key: key, |
| 139 | + } |
| 140 | + container[key] = &callbackRegistration{ |
| 141 | + handle: handle, |
| 142 | + callback: callback, |
| 143 | + } |
| 144 | + return handle |
| 145 | +} |
| 146 | + |
| 147 | +type callbackRegistration struct { |
| 148 | + previousInfo *PriceInfo |
| 149 | + callback func(PriceUpdate) |
| 150 | + handle CallbackHandle |
| 151 | +} |
| 152 | + |
| 153 | +func (r *callbackRegistration) inform(acc *PriceAccount, newInfo *PriceInfo) { |
| 154 | + if r.previousInfo.HasChanged(newInfo) { |
| 155 | + r.callback(PriceUpdate{ |
| 156 | + Account: acc, |
| 157 | + PreviousInfo: r.previousInfo, |
| 158 | + CurrentInfo: newInfo, |
| 159 | + }) |
| 160 | + } |
| 161 | + r.previousInfo = newInfo |
| 162 | +} |
| 163 | + |
| 164 | +// PriceUpdate is returned to callbacks when an aggregate or component price has been updated. |
| 165 | +type PriceUpdate struct { |
| 166 | + Account *PriceAccount |
| 167 | + PreviousInfo *PriceInfo |
| 168 | + CurrentInfo *PriceInfo |
| 169 | +} |
| 170 | + |
| 171 | +// Previous returns the value of the previously seen price update. |
| 172 | +// |
| 173 | +// If ok is false, the value is invalid. |
| 174 | +func (p PriceUpdate) Previous() (price decimal.Decimal, conf decimal.Decimal, ok bool) { |
| 175 | + if !p.PreviousInfo.IsZero() && p.Account != nil { |
| 176 | + p.PreviousInfo.Value(p.Account.Exponent) |
| 177 | + } |
| 178 | + return |
| 179 | +} |
| 180 | + |
| 181 | +// Current returns the value of the last price update. |
| 182 | +// |
| 183 | +// If ok is false, the value is invalid. |
| 184 | +func (p PriceUpdate) Current() (price decimal.Decimal, conf decimal.Decimal, ok bool) { |
| 185 | + if !p.CurrentInfo.IsZero() && p.Account != nil { |
| 186 | + return p.CurrentInfo.Value(p.Account.Exponent) |
| 187 | + } |
| 188 | + return |
| 189 | +} |
| 190 | + |
| 191 | +// CallbackHandle tracks the lifetime of a callback registration. |
| 192 | +type CallbackHandle struct { |
| 193 | + handler *PriceEventHandler |
| 194 | + container callbackMap |
| 195 | + key uint64 |
| 196 | +} |
| 197 | + |
| 198 | +// Unsubscribe de-registers a callback from the handler. |
| 199 | +// |
| 200 | +// Calling Unsubscribe is optional. |
| 201 | +// The handler calls it automatically when the underlying stream closes. |
| 202 | +func (c CallbackHandle) Unsubscribe() { |
| 203 | + lock := &c.handler.callbacksLock |
| 204 | + lock.Lock() |
| 205 | + defer lock.Unlock() |
| 206 | + |
| 207 | + delete(c.container, c.key) |
| 208 | +} |
0 commit comments