Skip to content

Commit

Permalink
Add handling of messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dev10 committed Aug 12, 2019
1 parent 4aa1f06 commit 81ab452
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 8 deletions.
77 changes: 77 additions & 0 deletions x/assetmanagement/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package assetmanagement

import (
"fmt"
"github.com/dev10/fantom-asset-management/x/assetmanagement/rand"
"github.com/dev10/fantom-asset-management/x/assetmanagement/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// NewHandler returns a handler for "assetmanagement" type messages.
func NewHandler(keeper Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case types.MsgIssueToken:
return handleMsgIssueToken(ctx, keeper, msg)
case types.MsgMintCoins:
return handleMsgMintCoins(ctx, keeper, msg)
case types.MsgBurnCoins:
return handleMsgBurnCoins(ctx, keeper, msg)
case types.MsgFreezeCoins:
return handleMsgFreezeCoins(ctx, keeper, msg)
case types.MsgUnfreezeCoins:
return handleMsgUnfreezeCoins(ctx, keeper, msg)
default:
errMsg := fmt.Sprintf("Unrecognized assetmanagement Msg type: %v", msg.Type())
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}

// handle message to issue token
func handleMsgIssueToken(ctx sdk.Context, keeper Keeper, msg types.MsgIssueToken) sdk.Result {
var newRandomSymbol = rand.GenerateNewSymbol(msg.Symbol)
token, err := types.NewToken(msg.Name, newRandomSymbol, msg.Symbol, msg.TotalSupply, msg.SourceAddress, msg.Mintable)
if err != nil {
return sdk.ErrUnknownRequest(fmt.Sprintf("failed to create new token: %s", err)).Result()
}

// keeper.coinKeeper.SetCoins(ctx, msg.SourceAddress, msg.TotalSupply)

err = keeper.SetToken(ctx, newRandomSymbol, token)
if err != nil {
return sdk.ErrInternal(fmt.Sprintf("failed to store new token: '%s'", err)).Result()
}
return sdk.Result{} // Todo: return new symbol name?
}

// handle message to mint coins
func handleMsgMintCoins(ctx sdk.Context, keeper Keeper, msg types.MsgMintCoins) sdk.Result {
owner, err := keeper.GetOwner(ctx, msg.Symbol)
if err != nil {
return sdk.ErrUnknownAddress(
fmt.Sprintf("Could not find the owner for the symbol '%s'", msg.Symbol)).Result()
}
if !msg.Owner.Equals(owner) { // Checks if the msg sender is the same as the current owner
return sdk.ErrUnauthorized("Incorrect Owner").Result() // If not, throw an error
}

amountDec, err := sdk.NewDecFromStr(msg.Amount)
if err != nil {
return sdk.ErrUnknownRequest(fmt.Sprintf("failed to create decimal from Amount string: %s", err)).Result()
}

// Fix: can't add Dec amount of coins because Cosmos sdk doesn't have AddDecCoins function
coins, err := keeper.coinKeeper.AddCoins(ctx, owner,
sdk.NewCoins(sdk.NewInt64Coin(msg.Symbol, amountDec.TruncateInt64())))
if err != nil {
return sdk.ErrInternal(fmt.Sprintf("failed to mint coins: '%s'", err)).Result()
}

err = keeper.SetTotalSupply(ctx, msg.Symbol, coins)
if err != nil {
return sdk.ErrInternal(fmt.Sprintf("failed to set total supply when minting coins: '%s'", err)).Result()
}
return sdk.Result{}
}
6 changes: 3 additions & 3 deletions x/assetmanagement/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (k Keeper) SetOwner(ctx sdk.Context, symbol string, owner sdk.AccAddress) e
}

// GetTotalSupply - gets the current total supply of a symbol
func (k Keeper) GetTotalSupply(ctx sdk.Context, symbol string) (sdk.Coins, error) {
func (k Keeper) GetTotalSupply(ctx sdk.Context, symbol string) (sdk.DecCoins, error) {
token, err := k.GetToken(ctx, symbol)
if err == nil {
return token.TotalSupply, nil
Expand All @@ -118,10 +118,10 @@ func (k Keeper) GetTotalSupply(ctx sdk.Context, symbol string) (sdk.Coins, error
}

// SetTotalSupply - sets the current total supply of a symbol
func (k Keeper) SetTotalSupply(ctx sdk.Context, symbol string, price sdk.Coins) error {
func (k Keeper) SetTotalSupply(ctx sdk.Context, symbol string, totalSupply sdk.DecCoins) error {
token, err := k.GetToken(ctx, symbol)
if err == nil {
token.TotalSupply = price
token.TotalSupply = totalSupply
return k.SetToken(ctx, symbol, token)
}
return fmt.Errorf("failed to set total supply for symbol '%s' because: %s", symbol, err)
Expand Down
29 changes: 29 additions & 0 deletions x/assetmanagement/rand/random_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package rand

import (
"fmt"
"math/rand"
"time"
)

const source_charset = "0123456789" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

// Make sure it is pseudo-random by using a new seed on startup
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))

func stringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}

func randomString(length int) string {
return stringWithCharset(length, source_charset)
}

func GenerateNewSymbol(original string) string {
return fmt.Sprintf("%s-%s", original, randomString(3))
}
18 changes: 13 additions & 5 deletions x/assetmanagement/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@ type Token struct {
Name string `json:"name"` // token name eg Fantom Chain Token
Symbol string `json:"symbol"` // unique token trade symbol eg FTM-000
OriginalSymbol string `json:"original_symbol"` // token symbol eg FTM
TotalSupply sdk.Coins `json:"total_supply"` // Total token supply
TotalSupply sdk.DecCoins `json:"total_supply"` // Total token supply
Mintable bool `json:"mintable"`
}

// NewToken returns a new token
func NewToken(name, symbol, originalSymbol string, totalSupply int64) Token {
return Token{
func NewToken(name, symbol, originalSymbol string, totalSupply string, owner sdk.AccAddress, mintable bool) (*Token, error) {
decTotalSupply, err := sdk.NewDecFromStr(totalSupply)
if err != nil {
return nil, fmt.Errorf("unable to create decimal from total supply string: '%s'", err)
}

return &Token{
Name: name,
Symbol: symbol,
OriginalSymbol: originalSymbol,
TotalSupply: sdk.Coins{sdk.NewInt64Coin(symbol, totalSupply)},
}
TotalSupply: sdk.DecCoins{sdk.NewDecCoinFromDec(symbol, decTotalSupply)},
Owner: owner,
Mintable: mintable,
}, nil

}

// String implements fmt.Stringer
Expand Down

0 comments on commit 81ab452

Please sign in to comment.