-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd,core,server: Support dynamic updates to price in USD
- Loading branch information
Showing
6 changed files
with
214 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
|
||
"github.com/livepeer/go-livepeer/eth" | ||
"github.com/livepeer/go-livepeer/eth/watchers" | ||
) | ||
|
||
// PriceFeedWatcher is a global instance of a PriceFeedWatcher. It must be | ||
// initialized before creating an AutoConvertedPrice instance. | ||
var PriceFeedWatcher *watchers.PriceFeedWatcher | ||
|
||
// Number of wei in 1 ETH | ||
var weiPerETH = big.NewRat(1e18, 1) | ||
|
||
type AutoConvertedPrice struct { | ||
cancelSubscription func() | ||
onUpdate func(*big.Rat) | ||
basePrice *big.Rat | ||
|
||
current *big.Rat | ||
} | ||
|
||
func NewAutoConvertedPrice(currency string, basePrice *big.Rat, onUpdate func(*big.Rat)) (*AutoConvertedPrice, error) { | ||
if PriceFeedWatcher == nil { | ||
return nil, fmt.Errorf("PriceFeedWatcher is not initialized") | ||
} | ||
|
||
if strings.ToLower(currency) == "wei" { | ||
return &AutoConvertedPrice{current: basePrice}, nil | ||
} else if strings.ToUpper(currency) == "ETH" { | ||
return &AutoConvertedPrice{current: new(big.Rat).Mul(basePrice, weiPerETH)}, nil | ||
} | ||
|
||
base, quote := PriceFeedWatcher.Currencies() | ||
if base != "ETH" && quote != "ETH" { | ||
return nil, fmt.Errorf("price feed does not have ETH as a currency (%v/%v)", base, quote) | ||
} | ||
if base != currency && quote != currency { | ||
return nil, fmt.Errorf("price feed does not have %v as a currency (%v/%v)", currency, base, quote) | ||
} | ||
|
||
currencyPrice, err := PriceFeedWatcher.Current() | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting current price data: %v", err) | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
price := &AutoConvertedPrice{ | ||
cancelSubscription: cancel, | ||
onUpdate: onUpdate, | ||
basePrice: basePrice, | ||
current: new(big.Rat).Mul(basePrice, currencyToWeiMultiplier(currencyPrice, base)), | ||
} | ||
go price.watch(ctx) | ||
onUpdate(price.current) | ||
|
||
return price, nil | ||
} | ||
|
||
func (a *AutoConvertedPrice) Value() *big.Rat { | ||
return a.current | ||
} | ||
|
||
func (a *AutoConvertedPrice) Stop() { | ||
if a.cancelSubscription != nil { | ||
a.cancelSubscription() | ||
} | ||
} | ||
|
||
func (a *AutoConvertedPrice) watch(ctx context.Context) { | ||
base, _ := PriceFeedWatcher.Currencies() | ||
priceUpdated := make(chan eth.PriceData, 1) | ||
PriceFeedWatcher.Subscribe(ctx, priceUpdated) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case currencyPrice := <-priceUpdated: | ||
a.current = new(big.Rat).Mul(a.basePrice, currencyToWeiMultiplier(currencyPrice, base)) | ||
a.onUpdate(a.current) | ||
} | ||
} | ||
} | ||
|
||
func currencyToWeiMultiplier(data eth.PriceData, baseCurrency string) *big.Rat { | ||
ethMultipler := data.Price | ||
if baseCurrency == "ETH" { | ||
// Invert the multiplier if the quote is in the form ETH / X | ||
ethMultipler = new(big.Rat).Inv(ethMultipler) | ||
} | ||
return new(big.Rat).Mul(ethMultipler, weiPerETH) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.