Skip to content

Commit

Permalink
v4: Adding field to track actual coins deposited (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
dahn510 authored Nov 1, 2023
2 parents 1a860d6 + ac22b69 commit 5380f0a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 62 deletions.
22 changes: 22 additions & 0 deletions app/upgrades/v4/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ func UpdateFileTree(ctx sdk.Context, fk filetreemodulekeeper.Keeper, merkleMap m
}
}

func UpdatePaymentInfo(ctx sdk.Context, sk storagekeeper.Keeper) {
paymentInfo := sk.GetAllStoragePaymentInfo(ctx)
for _, info := range paymentInfo {

planTime := info.End.Sub(info.Start)
millis := planTime.Milliseconds()
seconds := millis / 1000
minutes := seconds / 60
hours := minutes / 60

cost := sk.GetStorageCostKbs(ctx, info.SpaceAvailable, hours)

price := sdk.NewCoin("ujkl", cost)

info.Coins = sdk.NewCoins(price)

sk.SetStoragePaymentInfo(ctx, info)
}
}

func UpdateFiles(ctx sdk.Context, u *Upgrade) map[string][]byte {
fidMerkle := make(map[string][]byte)

Expand Down Expand Up @@ -174,6 +194,8 @@ func (u *Upgrade) Handler() upgradetypes.UpgradeHandler {

UpdateFileTree(ctx, u.fk, fidMerkleMap)

UpdatePaymentInfo(ctx, u.sk) // updating payment info with values at time of upgrade

newVM, err := u.mm.RunMigrations(ctx, u.configurator, fromVM)
if err != nil {
return newVM, err
Expand Down
8 changes: 8 additions & 0 deletions proto/canine_chain/storage/payment_info.proto
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
syntax = "proto3";
package canine_chain.storage;

import "cosmos/base/v1beta1/coin.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/jackalLabs/canine-chain/x/storage/types";

message StoragePaymentInfo {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

google.protobuf.Timestamp start = 1 [ (gogoproto.stdtime) = true, (gogoproto.nullable) = false ];
google.protobuf.Timestamp end = 2 [ (gogoproto.stdtime) = true, (gogoproto.nullable) = false];
int64 spaceAvailable = 3;
int64 spaceUsed = 4;
string address = 5;

repeated cosmos.base.v1beta1.Coin coins = 6
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];
}
8 changes: 7 additions & 1 deletion x/storage/keeper/msg_server_buy_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ func (k msgServer) BuyStorage(goCtx context.Context, msg *types.MsgBuyStorage) (
hours := sdk.NewDec(duration.Milliseconds()).Quo(sdk.NewDec(60 * 60 * 1000))
priceTokens := sdk.NewCoin(denom, k.GetStorageCost(ctx, gbs, hours.TruncateInt().Int64()))

priceTokenList := sdk.NewCoins(priceTokens)

add, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return nil, err
}
err = k.bankkeeper.SendCoinsFromAccountToModule(ctx, add, types.ModuleName, sdk.NewCoins(priceTokens))
err = k.bankkeeper.SendCoinsFromAccountToModule(ctx, add, types.ModuleName, priceTokenList)
if err != nil {
return nil, err
}
Expand All @@ -77,12 +79,15 @@ func (k msgServer) BuyStorage(goCtx context.Context, msg *types.MsgBuyStorage) (
return nil, fmt.Errorf("please use MsgUpgradeStorage if you want to upgrade/downgrade")
}

c := payInfo.Coins.Add(priceTokens)

spi = types.StoragePaymentInfo{
Start: ctx.BlockTime(),
End: ctx.BlockTime().Add(duration),
SpaceAvailable: bytes,
SpaceUsed: payInfo.SpaceUsed,
Address: msg.ForAddress,
Coins: c,
}
} else {
spi = types.StoragePaymentInfo{
Expand All @@ -91,6 +96,7 @@ func (k msgServer) BuyStorage(goCtx context.Context, msg *types.MsgBuyStorage) (
SpaceAvailable: bytes,
SpaceUsed: 0,
Address: msg.ForAddress,
Coins: priceTokenList,
}
}

Expand Down
151 changes: 90 additions & 61 deletions x/storage/types/payment_info.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5380f0a

Please sign in to comment.