Skip to content

Commit

Permalink
remove dependency and add stakin stakeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Stumble authored and xiaoxiaff committed Sep 16, 2018
1 parent b5096a7 commit 9061dd9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
17 changes: 17 additions & 0 deletions x/reputation/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package reputation

import (
"fmt"
"github.com/lino-network/lino/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// ErrAccountNotFound - error when account is not found
func ErrAccountNotFound(author types.AccountKey) sdk.Error {
return types.NewError(types.CodeAccountNotFound, fmt.Sprintf("account %v is not found", author))
}

// ErrPostNotFound - error when post is not found
func ErrPostNotFound(permlink types.Permlink) sdk.Error {
return types.NewError(types.CodePostNotFound, fmt.Sprintf("post %v doesn't exist", permlink))
}
38 changes: 28 additions & 10 deletions x/reputation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"github.com/lino-network/lino/param"
"github.com/lino-network/lino/types"

acc "github.com/lino-network/lino/x/account"
post "github.com/lino-network/lino/x/post"
model "github.com/lino-network/lino/x/reputation/internal"
"math/big"
)

type ReputationManager struct {
Expand Down Expand Up @@ -36,14 +35,14 @@ func (rep ReputationManager) getHandler(ctx sdk.Context) (model.Reputation, sdk.

func (rep ReputationManager) checkUsername(uid model.Uid) sdk.Error {
if len(uid) == 0 {
return acc.ErrAccountNotFound("")
return ErrAccountNotFound("")
}
return nil
}

func (rep ReputationManager) checkPost(pid model.Pid) sdk.Error {
if len(pid) == 0 {
return post.ErrPostNotFound("")
return ErrPostNotFound("")
}
return nil
}
Expand All @@ -59,7 +58,7 @@ func (rep ReputationManager) basicCheck(uid model.Uid, pid model.Pid) sdk.Error

// It's caller's responsibility that parameters are all correct, although we do have some checks.
func (rep ReputationManager) DonateAt(ctx sdk.Context,
username types.AccountKey, post types.Permlink, stake types.Coin) (types.Coin, sdk.Error) {
username types.AccountKey, post types.Permlink, coinDay types.Coin) (types.Coin, sdk.Error) {
handler, err := rep.getHandler(ctx)
if err != nil {
return types.NewCoinFromInt64(0), err
Expand All @@ -72,7 +71,7 @@ func (rep ReputationManager) DonateAt(ctx sdk.Context,
return types.NewCoinFromInt64(0), err
}

dp := handler.DonateAt(uid, pid, stake.Amount.BigInt())
dp := handler.DonateAt(uid, pid, coinDay.Amount.BigInt())
return types.NewCoinFromBigInt(dp), nil
}

Expand All @@ -93,20 +92,39 @@ func (rep ReputationManager) ReportAt(ctx sdk.Context,
return types.NewCoinFromBigInt(sumRep), nil
}

func (rep ReputationManager) IncFreeScore(ctx sdk.Context,
username types.AccountKey, score types.Coin) sdk.Error {
func (rep ReputationManager) calcFreeScore(amount types.Coin) *big.Int {
score := amount.Amount.BigInt()
score.Mul(score, big.NewInt(15))
score.Div(score, big.NewInt(10000)) // 0.15% freescore if you lock down.
return score
}

func (rep ReputationManager) OnStakeIn(ctx sdk.Context,
username types.AccountKey, amount types.Coin) {
incAmount := rep.calcFreeScore(amount)
rep.incFreeScore(ctx, username, incAmount)
}

func (rep ReputationManager) OnStakeOut(ctx sdk.Context,
username types.AccountKey, amount types.Coin) {
incAmount := rep.calcFreeScore(amount)
incAmount.Neg(incAmount)
rep.incFreeScore(ctx, username, incAmount)
}

func (rep ReputationManager) incFreeScore(ctx sdk.Context,
username types.AccountKey, score *big.Int) sdk.Error {
handler, err := rep.getHandler(ctx)
if err != nil {
return err
}

uid := string(username)
err = rep.checkUsername(uid)
if err != nil {
return err
}

handler.IncFreeScore(uid, score.Amount.BigInt())
handler.IncFreeScore(uid, score)
return nil
}

Expand Down

0 comments on commit 9061dd9

Please sign in to comment.