-
Notifications
You must be signed in to change notification settings - Fork 179
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
16 changed files
with
313 additions
and
203 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
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,100 @@ | ||
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 NewFixedPrice(price *big.Rat) (*AutoConvertedPrice) { | ||
return &AutoConvertedPrice{current: price} | ||
} | ||
|
||
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 NewFixedPrice(basePrice), nil | ||
} else if strings.ToUpper(currency) == "ETH" { | ||
return NewFixedPrice(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package core | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/livepeer/go-livepeer/eth" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCurrencyToWeiMultiplier(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data eth.PriceData | ||
baseCurrency string | ||
expectedWei *big.Rat | ||
}{ | ||
{ | ||
name: "Base currency is ETH", | ||
data: eth.PriceData{Price: big.NewRat(500, 1)}, // 500 USD per ETH | ||
baseCurrency: "ETH", | ||
expectedWei: big.NewRat(1e18, 500), // (1 / 500 USD/ETH) * 1e18 wei/ETH | ||
}, | ||
{ | ||
name: "Base currency is not ETH", | ||
data: eth.PriceData{Price: big.NewRat(1, 2000)}, // 1/2000 ETH per USD | ||
baseCurrency: "USD", | ||
expectedWei: big.NewRat(5e14, 1), // (1 * 1/2000 ETH/USD) * 1e18 wei/ETH | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := currencyToWeiMultiplier(tt.data, tt.baseCurrency) | ||
assert.Equal(t, 0, tt.expectedWei.Cmp(result)) | ||
}) | ||
} | ||
} |
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.