Skip to content

Commit

Permalink
introduce new big.Int based monetary int
Browse files Browse the repository at this point in the history
  • Loading branch information
altitude committed Aug 5, 2022
1 parent 1308392 commit 55fb8ae
Show file tree
Hide file tree
Showing 20 changed files with 313 additions and 218 deletions.
4 changes: 2 additions & 2 deletions pkg/api/controllers/account_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,11 @@ func TestGetAccount(t *testing.T) {
},
},
Balances: core.AssetsBalances{
"USD": 100,
"USD": core.NewMonetaryInt(100),
},
Volumes: core.AssetsVolumes{
"USD": {
Input: 100,
Input: core.NewMonetaryInt(100),
},
},
}, resp)
Expand Down
14 changes: 7 additions & 7 deletions pkg/api/controllers/balance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestGetBalancesAggregated(t *testing.T) {

resp, ok := internal.DecodeSingleResponse[core.AssetsBalances](t, rsp.Body)
assert.Equal(t, ok, true)
assert.Equal(t, core.AssetsBalances{"USD": 0}, resp)
assert.Equal(t, core.AssetsBalances{"USD": core.NewMonetaryInt(0)}, resp)
})

t.Run("filter by address", func(t *testing.T) {
Expand All @@ -60,7 +60,7 @@ func TestGetBalancesAggregated(t *testing.T) {

resp, ok := internal.DecodeSingleResponse[core.AssetsBalances](t, rsp.Body)
assert.Equal(t, true, ok)
assert.Equal(t, core.AssetsBalances{"USD": -250}, resp)
assert.Equal(t, core.AssetsBalances{"USD": core.NewMonetaryInt(-250)}, resp)
})

t.Run("filter by address no result", func(t *testing.T) {
Expand Down Expand Up @@ -163,9 +163,9 @@ func TestGetBalances(t *testing.T) {

resp := internal.DecodeCursorResponse[core.AccountsBalances](t, rsp.Body)
assert.Equal(t, []core.AccountsBalances{
{"world": core.AssetsBalances{"USD": -250, "EUR": -400, "CAD": -200}},
{"bob": core.AssetsBalances{"USD": 100}},
{"alice": core.AssetsBalances{"USD": 150, "EUR": 400, "CAD": 200}},
{"world": core.AssetsBalances{"USD": core.NewMonetaryInt(-250), "EUR": core.NewMonetaryInt(-400), "CAD": core.NewMonetaryInt(-200)}},
{"bob": core.AssetsBalances{"USD": core.NewMonetaryInt(100)}},
{"alice": core.AssetsBalances{"USD": core.NewMonetaryInt(150), "EUR": core.NewMonetaryInt(400), "CAD": core.NewMonetaryInt(200)}},
}, resp.Data)
})

Expand All @@ -175,7 +175,7 @@ func TestGetBalances(t *testing.T) {

resp := internal.DecodeCursorResponse[core.AccountsBalances](t, rsp.Body)
assert.Equal(t, []core.AccountsBalances{
{"alice": core.AssetsBalances{"USD": 150, "EUR": 400, "CAD": 200}},
{"alice": core.AssetsBalances{"USD": core.NewMonetaryInt(150), "EUR": core.NewMonetaryInt(400), "CAD": core.NewMonetaryInt(200)}},
}, resp.Data)
})

Expand All @@ -185,7 +185,7 @@ func TestGetBalances(t *testing.T) {

resp := internal.DecodeCursorResponse[core.AccountsBalances](t, rsp.Body)
assert.Equal(t, []core.AccountsBalances{
{"world": core.AssetsBalances{"USD": -250, "EUR": -400, "CAD": -200}},
{"world": core.AssetsBalances{"USD": core.NewMonetaryInt(-250), "EUR": core.NewMonetaryInt(-400), "CAD": core.NewMonetaryInt(-200)}},
}, resp.Data)
})

Expand Down
48 changes: 24 additions & 24 deletions pkg/api/controllers/transaction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestPostTransactions(t *testing.T) {
{
Source: "world",
Destination: "central_bank",
Amount: -1000,
Amount: core.NewMonetaryInt(-1000),
Asset: "USB",
},
},
Expand Down Expand Up @@ -316,12 +316,12 @@ func TestGetTransaction(t *testing.T) {
assert.EqualValues(t, core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{
"USD": {
Output: 1000,
Output: core.NewMonetaryInt(1000),
},
},
"central_bank": core.AssetsVolumes{
"USD": {
Input: 1000,
Input: core.NewMonetaryInt(1000),
},
},
}, ret.PostCommitVolumes)
Expand Down Expand Up @@ -761,7 +761,7 @@ func TestTransactionsVolumes(t *testing.T) {
OnStart: func(ctx context.Context) error {

// Single posting - single asset
const worldAliceUSD core.MonetaryInt = 100
worldAliceUSD := core.NewMonetaryInt(100)

rsp := internal.PostTransaction(t, api,
core.TransactionData{
Expand Down Expand Up @@ -798,7 +798,7 @@ func TestTransactionsVolumes(t *testing.T) {
"world": assetsVolumes{
"USD": core.VolumesWithBalance{
Output: worldAliceUSD,
Balance: -worldAliceUSD,
Balance: worldAliceUSD.Neg(),
},
},
}
Expand All @@ -818,7 +818,7 @@ func TestTransactionsVolumes(t *testing.T) {

// Single posting - single asset

const aliceBobUSD core.MonetaryInt = 93
aliceBobUSD := core.NewMonetaryInt(93)

rsp = internal.PostTransaction(t, api,
core.TransactionData{
Expand Down Expand Up @@ -849,8 +849,8 @@ func TestTransactionsVolumes(t *testing.T) {
"alice": assetsVolumes{
"USD": core.VolumesWithBalance{
Input: prevVolAliceUSD.Input,
Output: prevVolAliceUSD.Output + aliceBobUSD,
Balance: prevVolAliceUSD.Input - prevVolAliceUSD.Output - aliceBobUSD,
Output: prevVolAliceUSD.Output.Add(aliceBobUSD),
Balance: prevVolAliceUSD.Input.Sub(prevVolAliceUSD.Output).Sub(aliceBobUSD),
},
},
"bob": assetsVolumes{
Expand All @@ -877,8 +877,8 @@ func TestTransactionsVolumes(t *testing.T) {

// Multi posting - single asset

const worldBobEUR core.MonetaryInt = 156
const bobAliceEUR core.MonetaryInt = 3
worldBobEUR := core.NewMonetaryInt(156)
bobAliceEUR := core.NewMonetaryInt(3)

rsp = internal.PostTransaction(t, api,
core.TransactionData{
Expand Down Expand Up @@ -918,22 +918,22 @@ func TestTransactionsVolumes(t *testing.T) {
"alice": assetsVolumes{
"EUR": core.VolumesWithBalance{
Input: bobAliceEUR,
Output: 0,
Output: core.NewMonetaryInt(0),
Balance: bobAliceEUR,
},
},
"bob": assetsVolumes{
"EUR": core.VolumesWithBalance{
Input: worldBobEUR,
Output: bobAliceEUR,
Balance: worldBobEUR - bobAliceEUR,
Balance: worldBobEUR.Sub(bobAliceEUR),
},
},
"world": assetsVolumes{
"EUR": core.VolumesWithBalance{
Input: 0,
Input: core.NewMonetaryInt(0),
Output: worldBobEUR,
Balance: -worldBobEUR,
Balance: worldBobEUR.Neg(),
},
},
}
Expand All @@ -954,8 +954,8 @@ func TestTransactionsVolumes(t *testing.T) {

// Multi postings - multi assets

const bobAliceUSD core.MonetaryInt = 1
const aliceBobEUR core.MonetaryInt = 2
bobAliceUSD := core.NewMonetaryInt(1)
aliceBobEUR := core.NewMonetaryInt(2)

rsp = internal.PostTransaction(t, api,
core.TransactionData{
Expand Down Expand Up @@ -994,25 +994,25 @@ func TestTransactionsVolumes(t *testing.T) {
"alice": assetsVolumes{
"EUR": core.VolumesWithBalance{
Input: prevVolAliceEUR.Input,
Output: prevVolAliceEUR.Output + aliceBobEUR,
Balance: prevVolAliceEUR.Balance - aliceBobEUR,
Output: prevVolAliceEUR.Output.Add(aliceBobEUR),
Balance: prevVolAliceEUR.Balance.Sub(aliceBobEUR),
},
"USD": core.VolumesWithBalance{
Input: prevVolAliceUSD.Input + bobAliceUSD,
Input: prevVolAliceUSD.Input.Add(bobAliceUSD),
Output: prevVolAliceUSD.Output,
Balance: prevVolAliceUSD.Balance + bobAliceUSD,
Balance: prevVolAliceUSD.Balance.Add(bobAliceUSD),
},
},
"bob": assetsVolumes{
"EUR": core.VolumesWithBalance{
Input: prevVolBobEUR.Input + aliceBobEUR,
Input: prevVolBobEUR.Input.Add(aliceBobEUR),
Output: prevVolBobEUR.Output,
Balance: prevVolBobEUR.Balance + aliceBobEUR,
Balance: prevVolBobEUR.Balance.Add(aliceBobEUR),
},
"USD": core.VolumesWithBalance{
Input: prevVolBobUSD.Input,
Output: prevVolBobUSD.Output + bobAliceUSD,
Balance: prevVolBobUSD.Balance - bobAliceUSD,
Output: prevVolBobUSD.Output.Add(bobAliceUSD),
Balance: prevVolBobUSD.Balance.Sub(bobAliceUSD),
},
},
}
Expand Down
101 changes: 82 additions & 19 deletions pkg/core/monetary.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,102 @@
package core

type MonetaryInt int64
import (
"errors"
"math/big"
)

func (a MonetaryInt) Add(b MonetaryInt) MonetaryInt {
return a + b
type MonetaryInt big.Int

func (a *MonetaryInt) Add(b *MonetaryInt) *MonetaryInt {
if a == nil {
a = NewMonetaryInt(0)
}

if b == nil {
b = NewMonetaryInt(0)
}

return (*MonetaryInt)(big.NewInt(0).Add((*big.Int)(a), (*big.Int)(b)))
}

func (a *MonetaryInt) Sub(b *MonetaryInt) *MonetaryInt {
if a == nil {
a = NewMonetaryInt(0)
}

if b == nil {
b = NewMonetaryInt(0)
}

return (*MonetaryInt)(big.NewInt(0).Sub((*big.Int)(a), (*big.Int)(b)))
}

func (a *MonetaryInt) Neg() *MonetaryInt {
return (*MonetaryInt)(big.NewInt(0).Neg((*big.Int)(a)))
}

func (a *MonetaryInt) Lte(b *MonetaryInt) bool {
return (*big.Int)(a).Cmp((*big.Int)(b)) <= 0
}

func (a *MonetaryInt) Gte(b *MonetaryInt) bool {
return (*big.Int)(a).Cmp((*big.Int)(b)) >= 0
}

func (a *MonetaryInt) Lt(b *MonetaryInt) bool {
return (*big.Int)(a).Cmp((*big.Int)(b)) < 0
}

func (a *MonetaryInt) Ltz() bool {
return (*big.Int)(a).Cmp(big.NewInt(0)) < 0
}

func (a *MonetaryInt) Gt(b *MonetaryInt) bool {
return (*big.Int)(a).Cmp((*big.Int)(b)) > 0
}

func (a *MonetaryInt) Eq(b *MonetaryInt) bool {
return (*big.Int)(a).Cmp((*big.Int)(b)) == 0
}

func (a MonetaryInt) Sub(b MonetaryInt) MonetaryInt {
return a - b
func (a *MonetaryInt) Uint64() uint64 {
return (*big.Int)(a).Uint64()
}

func (a MonetaryInt) Neg() MonetaryInt {
return -a
func (a *MonetaryInt) String() string {
if a == nil {
return "0"
}

return (*big.Int)(a).String()
}

func (a MonetaryInt) Lte(b MonetaryInt) bool {
return a <= b
func (a *MonetaryInt) UnmarshalJSON(b []byte) error {
return (*big.Int)(a).UnmarshalJSON(b)
}

func (a MonetaryInt) Gte(b MonetaryInt) bool {
return a >= b
func (a *MonetaryInt) MarshalJSON() ([]byte, error) {
return (*big.Int)(a).MarshalJSON()
}

func (a MonetaryInt) Lt(b MonetaryInt) bool {
return a < b
func (a *MonetaryInt) MarshalText() ([]byte, error) {
return (*big.Int)(a).MarshalText()
}

func (a MonetaryInt) Gt(b MonetaryInt) bool {
return a > b
func (a *MonetaryInt) UnmarshalText(b []byte) error {
return (*big.Int)(a).UnmarshalText(b)
}

func (a MonetaryInt) Eq(b MonetaryInt) bool {
return a == b
func NewMonetaryInt(i int64) *MonetaryInt {
return (*MonetaryInt)(big.NewInt(i))
}

func NewMonetaryInt(i int64) MonetaryInt {
return MonetaryInt(i)
func ParseMonetaryInt(s string) (*MonetaryInt, error) {
i, ok := big.NewInt(0).SetString(s, 10)

if !ok {
return nil, errors.New("invalid monetary int")
}

return (*MonetaryInt)(i), nil
}
8 changes: 4 additions & 4 deletions pkg/core/posting.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

type Posting struct {
Source string `json:"source"`
Destination string `json:"destination"`
Amount MonetaryInt `json:"amount"`
Asset string `json:"asset"`
Source string `json:"source"`
Destination string `json:"destination"`
Amount *MonetaryInt `json:"amount"`
Asset string `json:"asset"`
}

type Postings []Posting
Expand Down
Loading

0 comments on commit 55fb8ae

Please sign in to comment.