add tow field for the response

This commit is contained in:
sulenn 2021-04-17 12:44:48 +08:00
parent d987c30beb
commit 7f7a4e59cf
6 changed files with 84 additions and 32 deletions

View File

@ -1,8 +1,10 @@
package contract
var (
RepoExisted int64 = -1001 // 在合约中创建项目时,项目已存在
BalanceOverFlow int64 = -1002 // 在合约中创建项目时预分配的用户Token累积值大于项目总Token
RepoExisted int64 = -1001 // 在合约中创建项目时,项目已存在
BalanceOverFlow int64 = -1002 // 在合约中创建项目时预分配的用户Token累积值大于项目总Token
RepoUnexisted int64 = -1003 // 项目不存在
RepoMaxBalanceOverFlow int64 = -1004 // 当前项目总Token 数超过最大 Token 量
ContractAddress string = "0xE46EE87f19049d5705989f49305b136d0732a0Cf"
)

View File

@ -16,6 +16,11 @@ type Repo struct {
CurSupply uint64 `json:"cur_supply"` // 项目当前金额
}
type ReponseRepo struct {
Repo
Response
}
// TransferTokenOption is options when to transfer amount from one person to another
type TransferTokenOption struct {
TokenSymbol string `json:"reponame" binding:"Required"` // 项目名

View File

@ -1,5 +1,7 @@
package structs
import "fmt"
type Response struct {
Status uint16 `json:"status"`
Message string `json:"message"`
@ -8,9 +10,15 @@ type Response struct {
var (
ResRepoSucc = &Response{Status: 0, Message: "create repo successfully!"} // create repo successfully!
ResRepoExisted = &Response{Status: 1, Message: "repo existed!"} // repo existed!
ResRepoBalanceOverFlow = &Response{Status: 1, Message: "username balance overflow when create repo!"} // username balance overflow when create repo!
ResRepoUnsucc = &Response{Status: 2, Message: "create repo unsuccessfully!"} // create repo unsuccessfully!
ResRepoBalanceOverFlow = &Response{Status: 2, Message: "username balance overflow when create repo!"} // username balance overflow when create repo!
ResRepoUnsucc = &Response{Status: 3, Message: "create repo unsuccessfully!"} // create repo unsuccessfully!
ResUserNotExisted = &Response{Status: 100, Message: "user not exist!"}
TypeTransferErr = &Response{Status: 10, Message: "type transfer error!"} // type transfer error!
StringEmpty = &Response{Status: 10, Message: "string empty!"} // string empty!
StringEmpty = &Response{Status: 11, Message: "string empty!"} // string empty!
)
func UnknownErr(err interface{}) *Response {
return &Response{Status: 20, Message: fmt.Sprintf("unknown error, err:%v\n", err)}
}

View File

@ -11,3 +11,13 @@ type UserBalance struct {
}
type UserBalanceList []*UserBalance
type ResponseUserBalanceList struct {
Response
UserBalanceList
}
type ResponseUserBalance struct {
UserBalance
Response
}

View File

@ -51,14 +51,14 @@ func Create(ctx *macaron.Context, opt api.CreateRepoOption, logger *log.Logger)
configs, err := conf.ParseConfigFile("config.toml")
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
config := &configs[0]
client, err := client.Dial(config)
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
@ -66,7 +66,7 @@ func Create(ctx *macaron.Context, opt api.CreateRepoOption, logger *log.Logger)
contractAddress := common.HexToAddress(contract.ContractAddress)
instance, err := opensource.NewOpenSource(contractAddress, client)
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
@ -74,7 +74,7 @@ func Create(ctx *macaron.Context, opt api.CreateRepoOption, logger *log.Logger)
tx, receipt, err := openSourceSession.CreateRepo(tokenName, owner, totalSupply, big.NewInt(curSupply), usernameArr, balanceArr) // call Insert API
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
logger.Printf("tx sent: %s\n", tx.Hash().Hex())
@ -107,14 +107,14 @@ func QueryBasic(ctx *macaron.Context, tokenName string, logger *log.Logger) {
configs, err := conf.ParseConfigFile("config.toml")
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
config := &configs[0]
client, err := client.Dial(config)
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
@ -122,7 +122,7 @@ func QueryBasic(ctx *macaron.Context, tokenName string, logger *log.Logger) {
contractAddress := common.HexToAddress(contract.ContractAddress)
instance, err := opensource.NewOpenSource(contractAddress, client)
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
@ -130,17 +130,23 @@ func QueryBasic(ctx *macaron.Context, tokenName string, logger *log.Logger) {
_, owner, totalSupply, curSupply, err := openSourceSession.SelectRepoBasicInfo(tokenName) // call Insert API
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
fmt.Printf("tokenName: %v, owner: %v, totalSupply: %v, curSupply: %v \n", tokenName, owner, totalSupply, curSupply)
ctx.JSON(http.StatusOK, &structs.Repo{
TokenName: tokenName,
Owner: owner,
TotalSupply: totalSupply.Uint64(),
CurSupply: curSupply.Uint64(),
ctx.JSON(http.StatusOK, &structs.ReponseRepo{
Response: structs.Response{
Status: 0,
Message: "query success!",
},
Repo: structs.Repo{
TokenName: tokenName,
Owner: owner,
TotalSupply: totalSupply.Uint64(),
CurSupply: curSupply.Uint64(),
},
})
}

View File

@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/sulenn/trustie-fisco-bcos/contract"
"github.com/sulenn/trustie-fisco-bcos/contract/opensource"
"github.com/sulenn/trustie-fisco-bcos/modules/structs"
api "github.com/sulenn/trustie-fisco-bcos/modules/structs"
"gitea.com/macaron/macaron"
@ -29,14 +30,14 @@ func SelectUserAllBalance(ctx *macaron.Context, username string, logger *log.Log
}
configs, err := conf.ParseConfigFile("config.toml")
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
config := &configs[0]
client, err := client.Dial(config)
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
@ -44,7 +45,7 @@ func SelectUserAllBalance(ctx *macaron.Context, username string, logger *log.Log
contractAddress := common.HexToAddress(contract.ContractAddress)
instance, err := opensource.NewOpenSource(contractAddress, client)
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
@ -52,7 +53,7 @@ func SelectUserAllBalance(ctx *macaron.Context, username string, logger *log.Log
usernames, tokenNames, balances, err := openSourceSession.SelectUserAllBalance(username) // call select API
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
var userBalancelist api.UserBalanceList
@ -60,7 +61,13 @@ func SelectUserAllBalance(ctx *macaron.Context, username string, logger *log.Log
userBalancelist = append(userBalancelist, &api.UserBalance{User: api.User{Username: usernames[i], TokenName: tokenNames[i]}, Balance: balances[i].Uint64()})
fmt.Printf("username: %v, token_name: %v, balance: %v \n", usernames[i], tokenNames[i], balances[i])
}
ctx.JSON(http.StatusOK, userBalancelist)
ctx.JSON(http.StatusOK, &structs.ResponseUserBalanceList{
UserBalanceList: userBalancelist,
Response: structs.Response{
Status: 0,
Message: "query success!",
},
})
}
// select user balance
@ -71,14 +78,14 @@ func SelectUserBalance(ctx *macaron.Context, username string, tokenName string,
}
configs, err := conf.ParseConfigFile("config.toml")
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
config := &configs[0]
client, err := client.Dial(config)
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
@ -86,7 +93,7 @@ func SelectUserBalance(ctx *macaron.Context, username string, tokenName string,
contractAddress := common.HexToAddress(contract.ContractAddress)
instance, err := opensource.NewOpenSource(contractAddress, client)
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
@ -94,13 +101,27 @@ func SelectUserBalance(ctx *macaron.Context, username string, tokenName string,
usernames, tokenNames, balances, err := openSourceSession.SelectUserBalance(username, tokenName) // call select API
if err != nil {
logger.Panic(err)
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
var userBalancelist api.UserBalanceList
for i := 0; i < len(usernames); i++ {
userBalancelist = append(userBalancelist, &api.UserBalance{User: api.User{Username: usernames[i], TokenName: tokenNames[i]}, Balance: balances[i].Uint64()})
fmt.Printf("username: %v, token_name: %v, balance: %v \n", usernames[i], tokenNames[i], balances[i])
if len(usernames) == 0 {
ctx.JSON(http.StatusOK, api.ResUserNotExisted)
return
}
ctx.JSON(http.StatusOK, userBalancelist)
var userBalance = api.UserBalance{
User: api.User{
Username: usernames[0],
TokenName: tokenNames[0],
},
Balance: balances[0].Uint64(),
}
fmt.Printf("username: %v, token_name: %v, balance: %v \n", usernames[0], tokenNames[0], balances[0])
ctx.JSON(http.StatusOK, &structs.ResponseUserBalance{
UserBalance: userBalance,
Response: structs.Response{
Status: 0,
Message: "query success!",
},
})
}