Skip to content

Commit

Permalink
chore: move batch balance util functions to core utils
Browse files Browse the repository at this point in the history
  • Loading branch information
technicallyty committed Mar 31, 2022
1 parent 9ee874a commit 8969e84
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 91 deletions.
95 changes: 4 additions & 91 deletions x/ecocredit/server/basket/take.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"fmt"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1"
ecoApi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/math"
"github.com/regen-network/regen-ledger/x/ecocredit"
baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket"
"github.com/regen-network/regen-ledger/x/ecocredit/server/core"

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down Expand Up @@ -172,21 +171,20 @@ func (k Keeper) addCreditBalance(ctx context.Context, owner sdk.AccAddress, batc
return err
}
if !retire {
if err = k.addAndSaveBalance(ctx, owner, batch.Id, amount); err != nil {
if err = core.AddAndSaveBalance(ctx, k.coreStore.BatchBalanceTable(), owner, batch.Id, amount); err != nil {
return err
}

return sdkCtx.EventManager().EmitTypedEvent(&ecocredit.EventReceive{
Recipient: owner.String(),
BatchDenom: batchDenom,
TradableAmount: amount.String(),
BasketDenom: basketDenom,
})
} else {
if err := k.retireAndSaveBalance(ctx, owner, batch.Id, amount); err != nil {
if err = core.RetireAndSaveBalance(ctx, k.coreStore.BatchBalanceTable(), owner, batch.Id, amount); err != nil {
return err
}
if err := k.retireSupply(ctx, batch.Id, amount); err != nil {
if err = core.RetireSupply(ctx, k.coreStore.BatchSupplyTable(), batch.Id, amount); err != nil {
return err
}
err = sdkCtx.EventManager().EmitTypedEvent(&ecocredit.EventReceive{
Expand All @@ -206,88 +204,3 @@ func (k Keeper) addCreditBalance(ctx context.Context, owner sdk.AccAddress, batc
})
}
}

func (k Keeper) addAndSaveBalance(ctx context.Context, user sdk.AccAddress, batchId uint64, amount math.Dec) error {
userBal, err := k.coreStore.BatchBalanceTable().Get(ctx, user, batchId)
if err != nil {
if ormerrors.IsNotFound(err) {
userBal = &ecoApi.BatchBalance{
Address: user,
BatchId: batchId,
Tradable: "0",
Retired: "0",
Escrowed: "0",
}
} else {
return err
}
}
tradable, err := math.NewDecFromString(userBal.Tradable)
if err != nil {
return err
}
newTradable, err := math.SafeAddBalance(tradable, amount)
if err != nil {
return err
}
userBal.Tradable = newTradable.String()
return k.coreStore.BatchBalanceTable().Save(ctx, userBal)
}

func (k Keeper) retireAndSaveBalance(ctx context.Context, user sdk.AccAddress, batchId uint64, amount math.Dec) error {
userBal, err := k.coreStore.BatchBalanceTable().Get(ctx, user, batchId)
if err != nil {
if ormerrors.IsNotFound(err) {
userBal = &ecoApi.BatchBalance{
Address: user,
BatchId: batchId,
Tradable: "0",
Retired: "0",
Escrowed: "0",
}
} else {
return err
}
}
retired, err := math.NewDecFromString(userBal.Retired)
if err != nil {
return err
}
newRetired, err := math.SafeAddBalance(retired, amount)
if err != nil {
return err
}
userBal.Retired = newRetired.String()
return k.coreStore.BatchBalanceTable().Save(ctx, userBal)
}

func (k Keeper) retireSupply(ctx context.Context, batchId uint64, amount math.Dec) error {
supply, err := k.coreStore.BatchSupplyTable().Get(ctx, batchId)
if err != nil {
return err
}
tradable, err := math.NewDecFromString(supply.TradableAmount)
if err != nil {
return err
}

retired, err := math.NewDecFromString(supply.RetiredAmount)
if err != nil {
return err
}

tradable, err = math.SafeSubBalance(tradable, amount)
if err != nil {
return err
}

retired, err = math.SafeAddBalance(retired, amount)
if err != nil {
return err
}

supply.TradableAmount = tradable.String()
supply.RetiredAmount = retired.String()

return k.coreStore.BatchSupplyTable().Update(ctx, supply)
}
92 changes: 92 additions & 0 deletions x/ecocredit/server/core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package core
import (
"context"

ecoApi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/types/math"

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand All @@ -19,3 +23,91 @@ func (k Keeper) assertClassIssuer(goCtx context.Context, classID uint64, addr sd
}
return nil
}

// AddAndSaveBalance adds 'amt' to the addr's tradable balance.
func AddAndSaveBalance(ctx context.Context, table ecoApi.BatchBalanceTable, addr sdk.AccAddress, batchId uint64, amt math.Dec) error {
bal, err := table.Get(ctx, addr, batchId)
if err != nil {
if ormerrors.IsNotFound(err) {
bal = &ecoApi.BatchBalance{
Address: addr,
BatchId: batchId,
Tradable: "0",
Retired: "0",
Escrowed: "0",
}
} else {
return err
}
}
tradable, err := math.NewDecFromString(bal.Tradable)
if err != nil {
return err
}
newTradable, err := math.SafeAddBalance(tradable, amt)
if err != nil {
return err
}
bal.Tradable = newTradable.String()
return table.Save(ctx, bal)
}

// RetireAndSaveBalance adds 'amt' to the addr's retired balance.
func RetireAndSaveBalance(ctx context.Context, table ecoApi.BatchBalanceTable, addr sdk.AccAddress, batchId uint64, amount math.Dec) error {
bal, err := table.Get(ctx, addr, batchId)
if err != nil {
if ormerrors.IsNotFound(err) {
bal = &ecoApi.BatchBalance{
Address: addr,
BatchId: batchId,
Tradable: "0",
Retired: "0",
Escrowed: "0",
}
} else {
return err
}
}
retired, err := math.NewDecFromString(bal.Retired)
if err != nil {
return err
}
newRetired, err := math.SafeAddBalance(retired, amount)
if err != nil {
return err
}
bal.Retired = newRetired.String()
return table.Save(ctx, bal)
}

// RetireSupply moves `amount` of credits from the supply's tradable amount to its retired amount.
func RetireSupply(ctx context.Context, table ecoApi.BatchSupplyTable, batchId uint64, amount math.Dec) error {
supply, err := table.Get(ctx, batchId)
if err != nil {
return err
}
tradable, err := math.NewDecFromString(supply.TradableAmount)
if err != nil {
return err
}

retired, err := math.NewDecFromString(supply.RetiredAmount)
if err != nil {
return err
}

tradable, err = math.SafeSubBalance(tradable, amount)
if err != nil {
return err
}

retired, err = math.SafeAddBalance(retired, amount)
if err != nil {
return err
}

supply.TradableAmount = tradable.String()
supply.RetiredAmount = retired.String()

return table.Update(ctx, supply)
}

0 comments on commit 8969e84

Please sign in to comment.