Skip to content

Commit

Permalink
refactor(primitives/types): move BalanceStatus type to balances module (
Browse files Browse the repository at this point in the history
  • Loading branch information
radkomih authored Oct 18, 2023
1 parent e84b7be commit 5be518b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 41 deletions.
69 changes: 35 additions & 34 deletions frame/balances/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"bytes"

sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/frame/balances/types"
"github.com/LimeChain/gosemble/primitives/log"
"github.com/LimeChain/gosemble/primitives/types"
primitives "github.com/LimeChain/gosemble/primitives/types"
)

// Balances module events.
Expand All @@ -27,47 +28,47 @@ const (
errInvalidEventType = "invalid balances.Event type"
)

func newEventEndowed(moduleIndex sc.U8, account types.PublicKey, freeBalance types.Balance) types.Event {
return types.NewEvent(moduleIndex, EventEndowed, account, freeBalance)
func newEventEndowed(moduleIndex sc.U8, account primitives.PublicKey, freeBalance primitives.Balance) primitives.Event {
return primitives.NewEvent(moduleIndex, EventEndowed, account, freeBalance)
}

func newEventDustLost(moduleIndex sc.U8, account types.PublicKey, amount types.Balance) types.Event {
return types.NewEvent(moduleIndex, EventDustLost, account, amount)
func newEventDustLost(moduleIndex sc.U8, account primitives.PublicKey, amount primitives.Balance) primitives.Event {
return primitives.NewEvent(moduleIndex, EventDustLost, account, amount)
}

func newEventTransfer(moduleIndex sc.U8, from types.PublicKey, to types.PublicKey, amount types.Balance) types.Event {
return types.NewEvent(moduleIndex, EventTransfer, from, to, amount)
func newEventTransfer(moduleIndex sc.U8, from primitives.PublicKey, to primitives.PublicKey, amount primitives.Balance) primitives.Event {
return primitives.NewEvent(moduleIndex, EventTransfer, from, to, amount)
}

func newEventBalanceSet(moduleIndex sc.U8, account types.PublicKey, free types.Balance, reserved types.Balance) types.Event {
return types.NewEvent(moduleIndex, EventBalanceSet, account, free, reserved)
func newEventBalanceSet(moduleIndex sc.U8, account primitives.PublicKey, free primitives.Balance, reserved primitives.Balance) primitives.Event {
return primitives.NewEvent(moduleIndex, EventBalanceSet, account, free, reserved)
}

func newEventReserved(moduleIndex sc.U8, account types.PublicKey, amount types.Balance) types.Event {
return types.NewEvent(moduleIndex, EventReserved, account, amount)
func newEventReserved(moduleIndex sc.U8, account primitives.PublicKey, amount primitives.Balance) primitives.Event {
return primitives.NewEvent(moduleIndex, EventReserved, account, amount)
}

func newEventUnreserved(moduleIndex sc.U8, account types.PublicKey, amount types.Balance) types.Event {
return types.NewEvent(moduleIndex, EventUnreserved, account, amount)
func newEventUnreserved(moduleIndex sc.U8, account primitives.PublicKey, amount primitives.Balance) primitives.Event {
return primitives.NewEvent(moduleIndex, EventUnreserved, account, amount)
}

func newEventReserveRepatriated(moduleIndex sc.U8, from types.PublicKey, to types.PublicKey, amount types.Balance, destinationStatus types.BalanceStatus) types.Event {
return types.NewEvent(moduleIndex, EventReserveRepatriated, from, to, amount, destinationStatus)
func newEventReserveRepatriated(moduleIndex sc.U8, from primitives.PublicKey, to primitives.PublicKey, amount primitives.Balance, destinationStatus types.BalanceStatus) primitives.Event {
return primitives.NewEvent(moduleIndex, EventReserveRepatriated, from, to, amount, destinationStatus)
}

func newEventDeposit(moduleIndex sc.U8, account types.PublicKey, amount types.Balance) types.Event {
return types.NewEvent(moduleIndex, EventDeposit, account, amount)
func newEventDeposit(moduleIndex sc.U8, account primitives.PublicKey, amount primitives.Balance) primitives.Event {
return primitives.NewEvent(moduleIndex, EventDeposit, account, amount)
}

func newEventWithdraw(moduleIndex sc.U8, account types.PublicKey, amount types.Balance) types.Event {
return types.NewEvent(moduleIndex, EventWithdraw, account, amount)
func newEventWithdraw(moduleIndex sc.U8, account primitives.PublicKey, amount primitives.Balance) primitives.Event {
return primitives.NewEvent(moduleIndex, EventWithdraw, account, amount)
}

func newEventSlashed(moduleIndex sc.U8, account types.PublicKey, amount types.Balance) types.Event {
return types.NewEvent(moduleIndex, EventSlashed, account, amount)
func newEventSlashed(moduleIndex sc.U8, account primitives.PublicKey, amount primitives.Balance) primitives.Event {
return primitives.NewEvent(moduleIndex, EventSlashed, account, amount)
}

func DecodeEvent(moduleIndex sc.U8, buffer *bytes.Buffer) types.Event {
func DecodeEvent(moduleIndex sc.U8, buffer *bytes.Buffer) primitives.Event {
decodedModuleIndex := sc.DecodeU8(buffer)
if decodedModuleIndex != moduleIndex {
log.Critical(errInvalidEventModule)
Expand All @@ -77,47 +78,47 @@ func DecodeEvent(moduleIndex sc.U8, buffer *bytes.Buffer) types.Event {

switch b {
case EventEndowed:
account := types.DecodePublicKey(buffer)
account := primitives.DecodePublicKey(buffer)
freeBalance := sc.DecodeU128(buffer)
return newEventEndowed(moduleIndex, account, freeBalance)
case EventDustLost:
account := types.DecodePublicKey(buffer)
account := primitives.DecodePublicKey(buffer)
amount := sc.DecodeU128(buffer)
return newEventDustLost(moduleIndex, account, amount)
case EventTransfer:
from := types.DecodePublicKey(buffer)
to := types.DecodePublicKey(buffer)
from := primitives.DecodePublicKey(buffer)
to := primitives.DecodePublicKey(buffer)
amount := sc.DecodeU128(buffer)
return newEventTransfer(moduleIndex, from, to, amount)
case EventBalanceSet:
account := types.DecodePublicKey(buffer)
account := primitives.DecodePublicKey(buffer)
free := sc.DecodeU128(buffer)
reserved := sc.DecodeU128(buffer)
return newEventBalanceSet(moduleIndex, account, free, reserved)
case EventReserved:
account := types.DecodePublicKey(buffer)
account := primitives.DecodePublicKey(buffer)
amount := sc.DecodeU128(buffer)
return newEventReserved(moduleIndex, account, amount)
case EventUnreserved:
account := types.DecodePublicKey(buffer)
account := primitives.DecodePublicKey(buffer)
amount := sc.DecodeU128(buffer)
return newEventUnreserved(moduleIndex, account, amount)
case EventReserveRepatriated:
from := types.DecodePublicKey(buffer)
to := types.DecodePublicKey(buffer)
from := primitives.DecodePublicKey(buffer)
to := primitives.DecodePublicKey(buffer)
amount := sc.DecodeU128(buffer)
destinationStatus := types.DecodeBalanceStatus(buffer)
return newEventReserveRepatriated(moduleIndex, from, to, amount, destinationStatus)
case EventDeposit:
account := types.DecodePublicKey(buffer)
account := primitives.DecodePublicKey(buffer)
amount := sc.DecodeU128(buffer)
return newEventDeposit(moduleIndex, account, amount)
case EventWithdraw:
account := types.DecodePublicKey(buffer)
account := primitives.DecodePublicKey(buffer)
amount := sc.DecodeU128(buffer)
return newEventWithdraw(moduleIndex, account, amount)
case EventSlashed:
account := types.DecodePublicKey(buffer)
account := primitives.DecodePublicKey(buffer)
amount := sc.DecodeU128(buffer)
return newEventSlashed(moduleIndex, account, amount)
default:
Expand Down
6 changes: 3 additions & 3 deletions frame/balances/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

sc "github.com/LimeChain/goscale"
primitives "github.com/LimeChain/gosemble/primitives/types"
"github.com/LimeChain/gosemble/frame/balances/types"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -108,7 +108,7 @@ func Test_Balances_DecodeEvent_ReserveRepatriated(t *testing.T) {
buffer.Write(fromAddress.AsAddress32().Bytes())
buffer.Write(toAddress.AsAddress32().Bytes())
buffer.Write(targetValue.Bytes())
buffer.Write(primitives.BalanceStatusFree.Bytes())
buffer.Write(types.BalanceStatusFree.Bytes())

result := DecodeEvent(moduleId, buffer)

Expand All @@ -118,7 +118,7 @@ func Test_Balances_DecodeEvent_ReserveRepatriated(t *testing.T) {
EventReserveRepatriated,
fromAddress.AsAddress32().FixedSequence,
toAddress.AsAddress32().FixedSequence,
targetValue, primitives.BalanceStatusFree),
targetValue, types.BalanceStatusFree),
result,
)
}
Expand Down
5 changes: 3 additions & 2 deletions frame/balances/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/constants"
"github.com/LimeChain/gosemble/constants/metadata"
"github.com/LimeChain/gosemble/frame/balances/types"
"github.com/LimeChain/gosemble/hooks"
primitives "github.com/LimeChain/gosemble/primitives/types"
)
Expand Down Expand Up @@ -438,12 +439,12 @@ func (m Module) metadataTypes() sc.Sequence[primitives.MetadataType] {
primitives.NewMetadataDefinitionVariant(
"Free",
sc.Sequence[primitives.MetadataTypeDefinitionField]{},
primitives.BalanceStatusFree,
types.BalanceStatusFree,
"BalanceStatus.Free"),
primitives.NewMetadataDefinitionVariant(
"Reserved",
sc.Sequence[primitives.MetadataTypeDefinitionField]{},
primitives.BalanceStatusReserved,
types.BalanceStatusReserved,
"BalanceStatus.Reserved"),
})),

Expand Down
5 changes: 3 additions & 2 deletions frame/balances/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/constants/metadata"
"github.com/LimeChain/gosemble/frame/balances/types"
"github.com/LimeChain/gosemble/mocks"
primitives "github.com/LimeChain/gosemble/primitives/types"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -604,12 +605,12 @@ func Test_Module_Metadata(t *testing.T) {
primitives.NewMetadataDefinitionVariant(
"Free",
sc.Sequence[primitives.MetadataTypeDefinitionField]{},
primitives.BalanceStatusFree,
types.BalanceStatusFree,
"BalanceStatus.Free"),
primitives.NewMetadataDefinitionVariant(
"Reserved",
sc.Sequence[primitives.MetadataTypeDefinitionField]{},
primitives.BalanceStatusReserved,
types.BalanceStatusReserved,
"BalanceStatus.Reserved"),
})),

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 5be518b

Please sign in to comment.