Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track eden and edenB #1108

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
586 changes: 569 additions & 17 deletions api/elys/commitment/commitments.pulsar.go

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions proto/elys/commitment/commitments.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ message VestingTokens {
int64 start_block = 6;
int64 vest_started_timestamp = 7;
}

message TotalSupply {
string total_eden_supply = 1 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string total_edenb_supply = 2 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
}
6 changes: 6 additions & 0 deletions x/commitment/keeper/commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@
if err != nil {
return err // never happens
}
prev := k.GetTotalSupply(ctx)
prev.TotalEdenbSupply = prev.TotalEdenbSupply.Sub(claimedRemovalAmount)
k.SetTotalSupply(ctx, prev)

amount = amount.Sub(claimedRemovalAmount)
if amount.IsZero() {
Expand Down Expand Up @@ -189,6 +192,9 @@
if err != nil {
return err
}
prev = k.GetTotalSupply(ctx)
prev.TotalEdenbSupply = prev.TotalEdenbSupply.Sub(amount)
k.SetTotalSupply(ctx, prev)

Check warning on line 197 in x/commitment/keeper/commitments.go

View check run for this annotation

Codecov / codecov/patch

x/commitment/keeper/commitments.go#L195-L197

Added lines #L195 - L197 were not covered by tests

k.SetCommitments(ctx, commitments)

Expand Down
90 changes: 76 additions & 14 deletions x/commitment/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,83 +101,126 @@
return k.bankKeeper.BlockedAddr(addr)
}

func (k Keeper) AddEdenEdenBOnAccount(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) sdk.Coins {
func (k Keeper) AddEdenEdenBOnAccount(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) (sdk.Coins, sdk.Coins) {
commitments := k.GetCommitments(ctx, addr)
var coinsChanged sdk.Coins
if amt.AmountOf(ptypes.Eden).IsPositive() {
coin := sdk.NewCoin(ptypes.Eden, amt.AmountOf(ptypes.Eden))
coinsChanged.Add(coin)
amt = amt.Sub(coin)
commitments.AddClaimed(coin)
}
if amt.AmountOf(ptypes.EdenB).IsPositive() {
coin := sdk.NewCoin(ptypes.EdenB, amt.AmountOf(ptypes.EdenB))
coinsChanged.Add(coin)
amt = amt.Sub(coin)
commitments.AddClaimed(coin)
}

// Save the updated Commitments
k.SetCommitments(ctx, commitments)
return amt

return amt, coinsChanged
}

func (k Keeper) AddEdenEdenBOnModule(ctx sdk.Context, moduleName string, amt sdk.Coins) sdk.Coins {
func (k Keeper) AddEdenEdenBOnModule(ctx sdk.Context, moduleName string, amt sdk.Coins) (sdk.Coins, sdk.Coins) {
addr := authtypes.NewModuleAddress(moduleName)
commitments := k.GetCommitments(ctx, addr)
var coinsChanged sdk.Coins
if amt.AmountOf(ptypes.Eden).IsPositive() {
coin := sdk.NewCoin(ptypes.Eden, amt.AmountOf(ptypes.Eden))
coinsChanged.Add(coin)
amt = amt.Sub(coin)
commitments.AddClaimed(coin)
}
if amt.AmountOf(ptypes.EdenB).IsPositive() {
coin := sdk.NewCoin(ptypes.EdenB, amt.AmountOf(ptypes.EdenB))
coinsChanged.Add(coin)
amt = amt.Sub(coin)
commitments.AddClaimed(coin)
}

// Save the updated Commitments
k.SetCommitments(ctx, commitments)
return amt

return amt, coinsChanged
}

func (k Keeper) SubEdenEdenBOnModule(ctx sdk.Context, moduleName string, amt sdk.Coins) (sdk.Coins, error) {
func (k Keeper) SubEdenEdenBOnModule(ctx sdk.Context, moduleName string, amt sdk.Coins) (sdk.Coins, sdk.Coins, error) {
addr := authtypes.NewModuleAddress(moduleName)
commitments := k.GetCommitments(ctx, addr)
var coinsChanged sdk.Coins
if amt.AmountOf(ptypes.Eden).IsPositive() {
coin := sdk.NewCoin(ptypes.Eden, amt.AmountOf(ptypes.Eden))
coinsChanged.Add(coin)
amt = amt.Sub(coin)
err := commitments.SubClaimed(coin)
if err != nil {
return amt, err
return amt, nil, err

Check warning on line 159 in x/commitment/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/commitment/keeper/keeper.go#L159

Added line #L159 was not covered by tests
}
}
if amt.AmountOf(ptypes.EdenB).IsPositive() {
coin := sdk.NewCoin(ptypes.EdenB, amt.AmountOf(ptypes.EdenB))
coinsChanged.Add(coin)
amt = amt.Sub(coin)
err := commitments.SubClaimed(coin)
if err != nil {
return amt, err
return amt, nil, err

Check warning on line 168 in x/commitment/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/commitment/keeper/keeper.go#L168

Added line #L168 was not covered by tests
}
}

// Save the updated Commitments
k.SetCommitments(ctx, commitments)
return amt, nil
return amt, coinsChanged, nil
}

func (k Keeper) MintCoins(goCtx context.Context, moduleName string, amt sdk.Coins) error {
ctx := sdk.UnwrapSDKContext(goCtx)
amt = k.AddEdenEdenBOnModule(ctx, moduleName, amt)
amt, coinsChanged := k.AddEdenEdenBOnModule(ctx, moduleName, amt)

prev := k.GetTotalSupply(ctx)
prev.TotalEdenSupply = prev.TotalEdenSupply.Add(coinsChanged.AmountOf(ptypes.Eden))
prev.TotalEdenbSupply = prev.TotalEdenbSupply.Add(coinsChanged.AmountOf(ptypes.EdenB))
k.SetTotalSupply(ctx, prev)

// Emit event to track Eden and EdenB mint amount
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMintCoins,
sdk.NewAttribute("module", moduleName),
sdk.NewAttribute("coins", coinsChanged.String()),
),
)

if amt.Empty() {
return nil
}

return k.bankKeeper.MintCoins(ctx, moduleName, amt)
}

func (k Keeper) BurnCoins(goCtx context.Context, moduleName string, amt sdk.Coins) error {
ctx := sdk.UnwrapSDKContext(goCtx)
amt, err := k.SubEdenEdenBOnModule(ctx, moduleName, amt)

amt, coinsChanged, err := k.SubEdenEdenBOnModule(ctx, moduleName, amt)
if err != nil {
return err
}

prev := k.GetTotalSupply(ctx)
prev.TotalEdenSupply = prev.TotalEdenSupply.Sub(coinsChanged.AmountOf(ptypes.Eden))
prev.TotalEdenbSupply = prev.TotalEdenbSupply.Sub(coinsChanged.AmountOf(ptypes.EdenB))
k.SetTotalSupply(ctx, prev)

// Emit event to track Eden and EdenB burn amount
ctx.EventManager().EmitEvent(
avkr003 marked this conversation as resolved.
Show resolved Hide resolved
sdk.NewEvent(
types.EventTypeBurnCoins,
sdk.NewAttribute("module", moduleName),
sdk.NewAttribute("coins", coinsChanged.String()),
),
)

if amt.Empty() {
return nil
}
Expand All @@ -187,25 +230,44 @@

func (k Keeper) SendCoinsFromModuleToModule(goCtx context.Context, senderModule string, recipientModule string, amt sdk.Coins) error {
ctx := sdk.UnwrapSDKContext(goCtx)
_, err := k.SubEdenEdenBOnModule(ctx, senderModule, amt)
_, _, err := k.SubEdenEdenBOnModule(ctx, senderModule, amt)
if err != nil {
return err
}
amt = k.AddEdenEdenBOnModule(ctx, recipientModule, amt)
amt, coinsChanged := k.AddEdenEdenBOnModule(ctx, recipientModule, amt)
// Emit event to track Eden and EdenB send amount
ctx.EventManager().EmitEvent(
avkr003 marked this conversation as resolved.
Show resolved Hide resolved
sdk.NewEvent(
types.EventTypeSendCoins,
sdk.NewAttribute("sender_module", senderModule),
sdk.NewAttribute("recipient_module", recipientModule),
sdk.NewAttribute("coins", coinsChanged.String()),
),
)
if amt.Empty() {
return nil
}

return k.bankKeeper.SendCoinsFromModuleToModule(ctx, senderModule, recipientModule, amt)
}

func (k Keeper) SendCoinsFromModuleToAccount(goCtx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error {
ctx := sdk.UnwrapSDKContext(goCtx)
_, err := k.SubEdenEdenBOnModule(ctx, senderModule, amt)
_, _, err := k.SubEdenEdenBOnModule(ctx, senderModule, amt)
if err != nil {
return err
}

amt = k.AddEdenEdenBOnAccount(ctx, recipientAddr, amt)
amt, coinsChanged := k.AddEdenEdenBOnAccount(ctx, recipientAddr, amt)
// Emit event to track Eden and EdenB send amount
ctx.EventManager().EmitEvent(
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
sdk.NewEvent(
types.EventTypeSendCoins,
sdk.NewAttribute("sender_module", senderModule),
sdk.NewAttribute("recipient_address", recipientAddr.String()),
sdk.NewAttribute("coins", coinsChanged.String()),
),
)
if amt.Empty() {
return nil
}
Expand Down
9 changes: 5 additions & 4 deletions x/commitment/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package keeper_test

import (
sdkmath "cosmossdk.io/math"
"testing"

sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
simapp "github.com/elys-network/elys/app"
Expand Down Expand Up @@ -85,7 +86,7 @@ func TestKeeper_AddEdenEdenBOnAccount(t *testing.T) {
keeper.SetCommitments(ctx, commitments)

// Test AddEdenEdenBOnAccount
_ = keeper.AddEdenEdenBOnAccount(ctx, creator, sdk.NewCoins(sdk.NewCoin(ptypes.Eden, sdkmath.NewInt(50)), sdk.NewCoin(ptypes.EdenB, sdkmath.NewInt(50))))
_, _ = keeper.AddEdenEdenBOnAccount(ctx, creator, sdk.NewCoins(sdk.NewCoin(ptypes.Eden, sdkmath.NewInt(50)), sdk.NewCoin(ptypes.EdenB, sdkmath.NewInt(50))))

// Check the updated commitments
commitments = keeper.GetCommitments(ctx, creator)
Expand Down Expand Up @@ -128,7 +129,7 @@ func TestKeeper_AddEdenEdenBOnModule(t *testing.T) {
keeper.SetCommitments(ctx, commitments)

// Test AddEdenEdenBOnModule
_ = keeper.AddEdenEdenBOnModule(
_, _ = keeper.AddEdenEdenBOnModule(
ctx, moduleName,
sdk.NewCoins(
sdk.NewCoin(ptypes.Eden, sdkmath.NewInt(50)),
Expand Down Expand Up @@ -189,7 +190,7 @@ func TestKeeper_SubEdenEdenBOnModule_InsufficientClaimedTokens(t *testing.T) {
keeper.SetCommitments(ctx, commitments)

// Test SubEdenEdenBOnModule
_, err = keeper.SubEdenEdenBOnModule(
_, _, err = keeper.SubEdenEdenBOnModule(
ctx, moduleName,
sdk.NewCoins(
sdk.NewCoin(ptypes.Eden, sdkmath.NewInt(50)),
Expand Down
4 changes: 4 additions & 0 deletions x/commitment/keeper/msg_server_cancel_vest.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (k msgServer) CancelVest(goCtx context.Context, msg *types.MsgCancelVest) (

// Update the unclaimed tokens amount
commitments.AddClaimed(sdk.NewCoin(ptypes.Eden, msg.Amount))

prev := k.GetTotalSupply(ctx)
prev.TotalEdenSupply = prev.TotalEdenSupply.Add(msg.Amount)
k.SetTotalSupply(ctx, prev)
k.SetCommitments(ctx, commitments)

// Emit blockchain event
Expand Down
7 changes: 7 additions & 0 deletions x/commitment/keeper/msg_server_vest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/elys-network/elys/x/commitment/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
)

// Vest converts user's commitment to vesting - start with unclaimed rewards and if it's not enough deduct from committed bucket
Expand Down Expand Up @@ -56,6 +57,12 @@ func (k Keeper) ProcessTokenVesting(ctx sdk.Context, denom string, amount math.I
})
commitments.VestingTokens = vestingTokens

if denom == ptypes.Eden {
prev := k.GetTotalSupply(ctx)
prev.TotalEdenSupply = prev.TotalEdenSupply.Sub(amount)
k.SetTotalSupply(ctx, prev)
}

// Update the commitments
k.SetCommitments(ctx, commitments)

Expand Down
4 changes: 4 additions & 0 deletions x/commitment/keeper/msg_server_vest_now.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (k msgServer) VestNow(goCtx context.Context, msg *types.MsgVestNow) (*types
return nil, errorsmod.Wrap(sdkerrors.ErrInsufficientFunds, "unable to send withdrawn tokens")
}

prev := k.GetTotalSupply(ctx)
prev.TotalEdenSupply = prev.TotalEdenSupply.Sub(msg.Amount)
k.SetTotalSupply(ctx, prev)

// Update the commitments
k.SetCommitments(ctx, commitments)

Expand Down
24 changes: 0 additions & 24 deletions x/commitment/keeper/params.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/commitment/types"
Expand Down Expand Up @@ -52,26 +51,3 @@ func (k Keeper) GetVestingInfo(ctx sdk.Context, baseDenom string) (*types.Vestin

return nil, 0
}

func (k Keeper) V8_ParamsMigration(ctx sdk.Context) {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, types.CommitmentsKeyPrefix)

defer iterator.Close()

var totalCommited sdk.Coins

for ; iterator.Valid(); iterator.Next() {
var val types.Commitments
k.cdc.MustUnmarshal(iterator.Value(), &val)
for _, token := range val.CommittedTokens {
totalCommited = totalCommited.Add(sdk.NewCoin(token.Denom, token.Amount))
}
}

params := k.GetParams(ctx)
params.TotalCommitted = totalCommited
k.SetParams(ctx, params)

return
}
Loading
Loading