mirror of
https://github.com/AthanorLabs/atomic-swap.git
synced 2026-01-09 14:18:03 -05:00
implement suggested exchange rate (#262)
Pulls chainlink's ETH and XMR price feeds to create suggested exchange rates. Co-authored-by: Dmitry Holodov <dimalinux@protonmail.com>
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/cockroachdb/apd/v3"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
)
|
||||
|
||||
// PiconeroAmount represents some amount of piconero (the smallest denomination of monero)
|
||||
|
||||
@@ -2,6 +2,7 @@ package coins
|
||||
|
||||
import (
|
||||
"github.com/cockroachdb/apd/v3"
|
||||
logging "github.com/ipfs/go-log"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -19,6 +20,8 @@ const (
|
||||
var (
|
||||
// DecimalCtx is the apd context used for math operations on our coins
|
||||
decimalCtx = apd.BaseContext.WithPrecision(MaxCoinPrecision)
|
||||
|
||||
log = logging.Logger("coins")
|
||||
)
|
||||
|
||||
// DecimalCtx clones and returns the apd.Context we use for coin math operations.
|
||||
|
||||
@@ -3,9 +3,14 @@ package coins
|
||||
import (
|
||||
"testing"
|
||||
|
||||
logging "github.com/ipfs/go-log"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func init() {
|
||||
logging.SetLogLevel("coins", "debug")
|
||||
}
|
||||
|
||||
func TestDecimalCtx(t *testing.T) {
|
||||
c := DecimalCtx()
|
||||
assert.Equal(t, c.Precision, uint32(MaxCoinPrecision))
|
||||
|
||||
@@ -9,6 +9,21 @@ import (
|
||||
// ie. an ExchangeRate of 0.1 means that the node considers 1 ETH = 10 XMR.
|
||||
type ExchangeRate apd.Decimal
|
||||
|
||||
// CalcExchangeRate computes and returns an exchange rate using ETH and XRM prices. The
|
||||
// price can be relative to USD, bitcoin or something else, but both values should be
|
||||
// relative to the same alternate currency.
|
||||
func CalcExchangeRate(xmrPrice *apd.Decimal, ethPrice *apd.Decimal) (*ExchangeRate, error) {
|
||||
rate := new(apd.Decimal)
|
||||
_, err := decimalCtx.Quo(rate, xmrPrice, ethPrice)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = roundToDecimalPlace(rate, rate, MaxExchangeRateDecimals); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ToExchangeRate(rate), nil
|
||||
}
|
||||
|
||||
// ToExchangeRate casts an *apd.Decimal to *ExchangeRate
|
||||
func ToExchangeRate(rate *apd.Decimal) *ExchangeRate {
|
||||
return (*ExchangeRate)(rate)
|
||||
|
||||
@@ -61,3 +61,18 @@ func TestExchangeRate_String(t *testing.T) {
|
||||
rate := ToExchangeRate(apd.New(3, -4)) // 0.0003
|
||||
assert.Equal(t, "0.0003", rate.String())
|
||||
}
|
||||
|
||||
func TestCalcExchangeRate(t *testing.T) {
|
||||
xmrPrice := StrToDecimal("200")
|
||||
ethPrice := StrToDecimal("300")
|
||||
rate, err := CalcExchangeRate(xmrPrice, ethPrice)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "0.666667", rate.String())
|
||||
}
|
||||
|
||||
func TestCalcExchangeRate_fail(t *testing.T) {
|
||||
xmrPrice := StrToDecimal("1.0")
|
||||
ethPrice := StrToDecimal("0") // create a division by zero error
|
||||
_, err := CalcExchangeRate(xmrPrice, ethPrice)
|
||||
require.ErrorContains(t, err, "division by zero")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user