From 5be518bb184eadc1ccc56efe8b3f3518f0662c98 Mon Sep 17 00:00:00 2001 From: Radosvet M Date: Wed, 18 Oct 2023 14:43:06 +0300 Subject: [PATCH] refactor(primitives/types): move BalanceStatus type to balances module (#251) --- frame/balances/events.go | 69 ++++++++++--------- frame/balances/events_test.go | 6 +- frame/balances/module.go | 5 +- frame/balances/module_test.go | 5 +- .../balances}/types/balance_status.go | 0 .../balances}/types/balance_status_test.go | 0 6 files changed, 44 insertions(+), 41 deletions(-) rename {primitives => frame/balances}/types/balance_status.go (100%) rename {primitives => frame/balances}/types/balance_status_test.go (100%) diff --git a/frame/balances/events.go b/frame/balances/events.go index 934feb6a..f1d4b8ad 100644 --- a/frame/balances/events.go +++ b/frame/balances/events.go @@ -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. @@ -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) @@ -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: diff --git a/frame/balances/events_test.go b/frame/balances/events_test.go index 2d59d64c..13e06ac3 100644 --- a/frame/balances/events_test.go +++ b/frame/balances/events_test.go @@ -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" ) @@ -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) @@ -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, ) } diff --git a/frame/balances/module.go b/frame/balances/module.go index c8370000..f4d5f7e4 100644 --- a/frame/balances/module.go +++ b/frame/balances/module.go @@ -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" ) @@ -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"), })), diff --git a/frame/balances/module_test.go b/frame/balances/module_test.go index 927122da..a9cdfa17 100644 --- a/frame/balances/module_test.go +++ b/frame/balances/module_test.go @@ -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" @@ -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"), })), diff --git a/primitives/types/balance_status.go b/frame/balances/types/balance_status.go similarity index 100% rename from primitives/types/balance_status.go rename to frame/balances/types/balance_status.go diff --git a/primitives/types/balance_status_test.go b/frame/balances/types/balance_status_test.go similarity index 100% rename from primitives/types/balance_status_test.go rename to frame/balances/types/balance_status_test.go