Skip to content

Commit

Permalink
MUL-1763: add get_accounts_by_key, fix account price calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
vovapi committed Jul 30, 2018
1 parent 98f0bf7 commit 5478000
Showing 1 changed file with 52 additions and 14 deletions.
66 changes: 52 additions & 14 deletions client/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ import (
btcpb "github.com/Multy-io/Multy-back/node-streamer/btc"
ethpb "github.com/Multy-io/Multy-back/node-streamer/eth"

"fmt"
"github.com/Multy-io/Multy-back/eos"
"github.com/Multy-io/Multy-back/eos"
"github.com/btcsuite/btcd/rpcclient"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)

const (
Expand Down Expand Up @@ -140,6 +138,7 @@ func SetRestHandlers(
v1.POST("/account/create", restClient.accountCreate)
v1.GET("/account/check", restClient.accountCheck)
v1.GET("/account/price", restClient.accountPrice)
v1.GET("/account/get_by_key", restClient.accountGetByKey)
}
return restClient, nil
}
Expand All @@ -151,11 +150,7 @@ func initMiddlewareJWT(restClient *RestClient) {
Timeout: time.Hour,
MaxRefresh: time.Hour,
Authenticator: func(userId, deviceId, pushToken string, deviceType int, c *gin.Context) (store.User, bool) {
query := bson.M{"userID": userId}

user := store.User{}

err := restClient.userStore.FindUser(query, &user)
user, err := restClient.userStore.GetUserByID(userId)

if err != nil || len(user.UserID) == 0 {
return user, false
Expand Down Expand Up @@ -215,7 +210,7 @@ type DisplayWallet struct {
}

type WalletExtended struct {
CuurencyID int `bson:"chain"` //cuurencyID
CurrencyID int `bson:"chain"` //currencyID
WalletIndex int `bson:"walletIndex"` //walletIndex
Addresses []AddressEx `bson:"addresses"`
}
Expand All @@ -236,7 +231,7 @@ func getToken(c *gin.Context) (string, error) {
}

func (restClient *RestClient) createCustomWallet(wp WalletParams, token string, c *gin.Context) error {
user, err := restClient.getUser(token)
user, err := restClient.userStore.GetUserByToken(token)
if err != nil {
restClient.log.Errorf("createCustomWallet: %s\t[addr=%s]", err.Error(), c.Request.RemoteAddr)
err = errors.New(msgErrUserNotFound)
Expand Down Expand Up @@ -2445,10 +2440,53 @@ func (server *RestClient) accountCreate(ctx *gin.Context) {
func (server *RestClient) accountPrice(ctx *gin.Context) {
type resources struct {
RAM uint64 `json:"ram"`
CPU uint64 `json:"cpu"`
NET uint64 `json:"net"`
CPU float64 `json:"cpu"`
NET float64 `json:"net"`
}
var res resources
decodeBody(ctx, &res)

err := decodeBody(ctx, &res)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": msgErrRequestBodyError,
})
return
}
price, err := server.EOS.Client.GetRAMPrice(ctx, &eospb.Empty{})
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"message": http.StatusText(http.StatusInternalServerError),
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"price": (price.Price * float64(res.RAM)) + res.CPU + res.NET,
})
return
}
func (server *RestClient) accountGetByKey(ctx *gin.Context) {
var key eospb.PublicKey
err := decodeBody(ctx, &key)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": msgErrRequestBodyError,
})
return
}
accounts, err := server.EOS.Client.GetKeyAccounts(ctx, &key)
if err != nil {
server.log.Errorf("eos get_key_accounts: %s", err)
ctx.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"message": http.StatusText(http.StatusInternalServerError),
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"account_names": accounts.AccountNames,
})
}

0 comments on commit 5478000

Please sign in to comment.