-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters