commit 1cb10633b6b4a49c8e8549d55b19e8eb4398a8ff
parent 5b40e5607ba3b947905de0698993e18f76c9ec3e
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Wed, 13 Dec 2023 05:16:43 -0500
withdraw xmr
Diffstat:
4 files changed, 54 insertions(+), 1 deletion(-)
diff --git a/pkg/database/tablePokerXmrTransactions.go b/pkg/database/tablePokerXmrTransactions.go
@@ -41,6 +41,11 @@ func (d *DkfDB) GetOrCreatePokerXmrTransaction(userID UserID, transfer *wallet.T
return
}
+func (d *DkfDB) CreatePokerXmrTransaction(userID UserID, transfer *wallet.ResponseTransfer) (out PokerXmrTransaction, err error) {
+ err = d.db.Create(&PokerXmrTransaction{UserID: userID, Amount: int64(transfer.Amount), IsIn: false}).Error
+ return
+}
+
func (t *PokerXmrTransaction) Save(db *DkfDB) error {
return db.db.Save(t).Error
}
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -753,6 +753,10 @@ func BHCHandler(c echo.Context) error {
return c.Render(http.StatusOK, "bhc", data)
}
+const pokerHouseFee = 0.07
+const pokerHouseXmrAddressLive = "83pJEonAgps6F61RSk6SAmFi4xmq4zVYmaDTk8eFGLE49dm1ZE2bLVaWqumHLRGU8gewV6wGffmhrV3RN35TXy8CK9UhWMa"
+const pokerHouseXmrAddressStag = "5B3jkfaD5Bu7NqyzuDqBjTgdCPKSfiqgmAznEhwxunLLQ1u9NzmeRvbHtNhyBf5xQV7fL6S7TEXNrgNSv3e4wdtA32cRyKc"
+
func PokerHomeHandler(c echo.Context) error {
db := c.Get("database").(*database.DkfDB)
authUser := c.Get("authUser").(*database.User)
@@ -776,5 +780,38 @@ func PokerHomeHandler(c echo.Context) error {
b, _ := authUser.GetImage()
data.Img = getImgStr(b)
}
+
+ if c.Request().Method == http.MethodPost {
+ withdrawChips := utils.DoParseInt(c.Request().PostFormValue("withdraw_amount"))
+ withdrawAddress := c.Request().PostFormValue("withdraw_address")
+ userChips := authUser.GetChips(true)
+ chips := utils.Clamp(withdrawChips, 100, userChips)
+ chips = utils.MinInt(chips, userChips)
+ if chips < 100 { // not enough for withdraw
+ return c.Redirect(http.StatusFound, "/poker")
+ }
+ authUser.IncrChips(-chips, true)
+ authUser.DoSave(db)
+
+ fee := int(pokerHouseFee * float64(chips))
+ chips -= fee
+
+ res, err := config.Xmr().Transfer(&wallet1.RequestTransfer{
+ Destinations: []*wallet1.Destination{
+ {Address: withdrawAddress,
+ Amount: uint64(chips) * 100_000_000},
+ {Address: pokerHouseXmrAddressStag,
+ Amount: uint64(fee) * 100_000_000}}})
+ if err != nil {
+ logrus.Error(err)
+ return c.Redirect(http.StatusFound, "/poker")
+ }
+ if _, err := db.CreatePokerXmrTransaction(authUser.ID, res); err != nil {
+ logrus.Error(err)
+ return c.Redirect(http.StatusFound, "/poker")
+ }
+ return c.Redirect(http.StatusFound, "/poker")
+ }
+
return c.Render(http.StatusOK, "poker", data)
}
diff --git a/pkg/web/public/views/pages/poker.gohtml b/pkg/web/public/views/pages/poker.gohtml
@@ -14,6 +14,14 @@
<div class="mb-3">
Balance: {{ .Data.Balance }} chips
</div>
+ <div class="mb-3">
+ <form method="post">
+ <input type="hidden" name="csrf" value="{{ .CSRF }}" />
+ <input type="text" name="withdraw_address" placeholder="address" class="form-control" style="width: 400px;" />
+ <input type="number" name="withdraw_amount" placeholder="amount" class="form-control" style="width: 150px;" />
+ <button class="btn btn-primary">Withdraw</button>
+ </form>
+ </div>
<h3>Tables</h3>
<table class="table table-novpadding table-sm table-hover table-striped">
<tr>
@@ -36,13 +44,15 @@
<th>Amount XMR</th>
<th>Confirmations</th>
<th>Processed</th>
+ <th>In/Out</th>
<th>Created at</th>
</tr>
{{ range .Data.Transactions }}
<tr>
<td>{{ .Amount | divide1T }}</td>
- <td>{{ .Confirmations }}</td>
+ <td>{{ if .IsIn }}{{ .Confirmations }}{{ else }}-{{ end }}</td>
<td>{{ .Processed | fmtBool }}</td>
+ <td>{{ if .IsIn }}IN{{ else }}OUT{{ end }}</td>
<td>{{ .CreatedAt.Format "Jan 02, 2006 15:04:05" }}</td>
</tr>
{{ else }}
diff --git a/pkg/web/web.go b/pkg/web/web.go
@@ -99,6 +99,7 @@ func getMainServer(db *database.DkfDB, i18nBundle *i18n.Bundle, renderer *tmp.Te
authGroup.GET("/donate", handlers.DonateHandler)
authGroup.GET("/shop", handlers.ShopHandler)
authGroup.GET("/poker", handlers.PokerHomeHandler)
+ authGroup.POST("/poker", handlers.PokerHomeHandler, middlewares.AuthRateLimitMiddleware(time.Second, 1))
authGroup.GET("/poker/:roomID", poker.PokerHandler)
authGroup.GET("/poker/:roomID/logs", poker.PokerLogsHandler)
authGroup.GET("/poker/:roomID/check", poker.PokerCheckHandler)