add lock and unlock amount function
This commit is contained in:
parent
2fb72d312c
commit
c23942f1c8
|
|
@ -20,10 +20,14 @@ var (
|
|||
ResUserAddAmountSucc = &Response{Status: 0, Message: "user adds amount successfully!"}
|
||||
ResUserMinusAmountSucc = &Response{Status: 0, Message: "user minus amount successfully!"}
|
||||
ResUserTransferAmountSucc = &Response{Status: 0, Message: "user transfers amount successfully!"}
|
||||
ResUserLockAmountSucc = &Response{Status: 0, Message: "user locks amount successfully!"}
|
||||
ResUserUnlockAmountSucc = &Response{Status: 0, Message: "user unlocks amount successfully!"}
|
||||
ResUserAddAmountUnsucc = &Response{Status: 101, Message: "user adds amount unsuccessfully!"}
|
||||
ResUserMinusAmountUnsucc = &Response{Status: 101, Message: "user minus amount unsuccessfully!"}
|
||||
ResUserTransferAmountUnsucc = &Response{Status: 101, Message: "user transfers amount unsuccessfully!"}
|
||||
ResUserBalanceNotEnough = &Response{Status: 102, Message: "user's Balance not enough!"}
|
||||
ResUserMinusAmountUnsucc = &Response{Status: 102, Message: "user minus amount unsuccessfully!"}
|
||||
ResUserLockAmountUnsucc = &Response{Status: 103, Message: "user locks amount unsuccessfully!"}
|
||||
ResUserUnlockAmountUnsucc = &Response{Status: 104, Message: "user unlocks amount unsuccessfully!"}
|
||||
ResUserTransferAmountUnsucc = &Response{Status: 105, Message: "user transfers amount unsuccessfully!"}
|
||||
ResUserBalanceNotEnough = &Response{Status: 106, Message: "user's Balance not enough!"}
|
||||
|
||||
TypeTransferErr = &Response{Status: 10, Message: "type transfer error!"} // type transfer error!
|
||||
StringEmpty = &Response{Status: 11, Message: "string empty!"} // string empty!
|
||||
|
|
|
|||
|
|
@ -34,6 +34,10 @@ func HandleAllRoutes(ctx *macaron.Context, opt api.FiscoBcos, logger *log.Logger
|
|||
user.AddUserAmount(ctx, opt.Username, opt.TokenName, opt.Amount, logger)
|
||||
case "minus user balance":
|
||||
user.MinusUserAmount(ctx, opt.Username, opt.TokenName, opt.Amount, logger)
|
||||
case "lock user balance":
|
||||
user.LockUserAmount(ctx, opt.Username, opt.TokenName, opt.Amount, logger)
|
||||
case "unlock user balance":
|
||||
user.UnlockUserAmount(ctx, opt.Username, opt.TokenName, opt.Amount, logger)
|
||||
case "transfer amount":
|
||||
user.TransferAmount(ctx, opt.Payer, opt.Payee, opt.TokenName, opt.Amount, logger)
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -241,6 +241,116 @@ func MinusUserAmount(ctx *macaron.Context, username string, tokenName string, am
|
|||
ctx.JSON(http.StatusOK, structs.ResUserMinusAmountUnsucc)
|
||||
}
|
||||
|
||||
func LockUserAmount(ctx *macaron.Context, username string, tokenName string, amount uint64, logger *log.Logger) {
|
||||
if username == "" || tokenName == "" {
|
||||
ctx.JSON(http.StatusOK, api.StringEmpty)
|
||||
return
|
||||
}
|
||||
configs, err := conf.ParseConfigFile("config.toml")
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
config := &configs[0]
|
||||
|
||||
client, err := client.Dial(config)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
// load the contract
|
||||
contractAddress := common.HexToAddress(contract.ContractAddress)
|
||||
instance, err := opensource.NewOpenSource(contractAddress, client)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
openSourceSession := &opensource.OpenSourceSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}
|
||||
tx, receipt, err := openSourceSession.TransferUserBalance(username, "temporary-account", tokenName, big.NewInt(int64(amount)))
|
||||
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
logger.Printf("tx sent: %s\n", tx.Hash().Hex())
|
||||
code, err := parseOutput(opensource.OpenSourceABI, "transferUserBalance", receipt)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
if code.Int64() > 0 {
|
||||
logger.Printf("inserted lines: %v\n", code)
|
||||
ctx.JSON(http.StatusOK, structs.ResUserLockAmountSucc)
|
||||
return
|
||||
} else if code.Int64() == contract.RepoUnexisted {
|
||||
logger.Printf("error code: %v, message: %v\n", code, structs.ResRepoNotExisted)
|
||||
ctx.JSON(http.StatusOK, structs.ResRepoNotExisted)
|
||||
return
|
||||
} else if code.Int64() == contract.UserBblanceNotEnough {
|
||||
logger.Printf("error code: %v, message: %v\n", code, structs.ResUserBalanceNotEnough)
|
||||
ctx.JSON(http.StatusOK, structs.ResUserBalanceNotEnough)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, structs.ResUserLockAmountUnsucc)
|
||||
}
|
||||
|
||||
func UnlockUserAmount(ctx *macaron.Context, username string, tokenName string, amount uint64, logger *log.Logger) {
|
||||
if username == "" || tokenName == "" {
|
||||
ctx.JSON(http.StatusOK, api.StringEmpty)
|
||||
return
|
||||
}
|
||||
configs, err := conf.ParseConfigFile("config.toml")
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
config := &configs[0]
|
||||
|
||||
client, err := client.Dial(config)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
// load the contract
|
||||
contractAddress := common.HexToAddress(contract.ContractAddress)
|
||||
instance, err := opensource.NewOpenSource(contractAddress, client)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
openSourceSession := &opensource.OpenSourceSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}
|
||||
tx, receipt, err := openSourceSession.TransferUserBalance("temporary-account", username, tokenName, big.NewInt(int64(amount)))
|
||||
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
logger.Printf("tx sent: %s\n", tx.Hash().Hex())
|
||||
code, err := parseOutput(opensource.OpenSourceABI, "transferUserBalance", receipt)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
if code.Int64() > 0 {
|
||||
logger.Printf("inserted lines: %v\n", code)
|
||||
ctx.JSON(http.StatusOK, structs.ResUserUnlockAmountSucc)
|
||||
return
|
||||
} else if code.Int64() == contract.RepoUnexisted {
|
||||
logger.Printf("error code: %v, message: %v\n", code, structs.ResRepoNotExisted)
|
||||
ctx.JSON(http.StatusOK, structs.ResRepoNotExisted)
|
||||
return
|
||||
} else if code.Int64() == contract.UserBblanceNotEnough {
|
||||
logger.Printf("error code: %v, message: %v\n", code, structs.ResUserBalanceNotEnough)
|
||||
ctx.JSON(http.StatusOK, structs.ResUserBalanceNotEnough)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, structs.ResUserUnlockAmountUnsucc)
|
||||
}
|
||||
|
||||
func TransferAmount(ctx *macaron.Context, payer string, payee string, tokenName string, amount uint64, logger *log.Logger) {
|
||||
if payer == "" || payee == "" || tokenName == "" {
|
||||
ctx.JSON(http.StatusOK, api.StringEmpty)
|
||||
|
|
|
|||
Loading…
Reference in New Issue