You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

56 lines
1.3 KiB

package main
import (
"encoding/json"
"net/http"
"github.com/bvinc/go-sqlite-lite/sqlite3"
"git.sporestack.com/SporeStack/settlers"
)
func deposit(depositOnlyToken string, amount uint64, db *sqlite3.Conn) (userErr, err error) {
// Add to a regular depositOnlyToken with its deposit only depositOnlyToken.
if userErr = settlers.ValidateToken(depositOnlyToken); userErr != nil {
return
}
// Check if account is enabled.
userErr, err = depositOnlyTokenEnabled(depositOnlyToken, db)
if userErr != nil || err != nil {
return
}
// sqlite3 only supports signed integers
var signedAmount int64
signedAmount = int64(amount)
err = db.Exec("UPDATE balances SET balance = balance + ? WHERE combined_deposit_only_token = ?", signedAmount, depositOnlyToken)
if err != nil {
return
}
return
}
func httpDeposit(w http.ResponseWriter, r *http.Request, db *sqlite3.Conn) {
defer r.Body.Close()
var request settlers.DepositRequest
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
userErr := decoder.Decode(&request)
if userErr != nil {
httpHandle400(w, userErr)
return
}
userErr, err := deposit(request.CombinedDepositOnlyToken, request.Amount, db)
if userErr != nil {
httpHandle400(w, userErr)
return
}
if err != nil {
httpHandle500(w, err)
return
}
return
}