Skip to content

Commit 4c88dc4

Browse files
committed
tweaks
1 parent 4b7a1f0 commit 4c88dc4

File tree

17 files changed

+178
-272
lines changed

17 files changed

+178
-272
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ require (
99
github.com/gin-contrib/pprof v1.5.3
1010
github.com/gin-gonic/gin v1.10.1
1111
github.com/juju/ratelimit v1.0.2
12-
github.com/mattn/go-colorable v0.1.13
13-
github.com/mattn/go-isatty v0.0.20
1412
github.com/pressly/goose/v3 v3.24.3
1513
github.com/prometheus/client_golang v1.14.0
1614
github.com/stretchr/testify v1.10.0
@@ -48,6 +46,7 @@ require (
4846
github.com/json-iterator/go v1.1.12 // indirect
4947
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
5048
github.com/leodido/go-urn v1.4.0 // indirect
49+
github.com/mattn/go-isatty v0.0.20 // indirect
5150
github.com/mattn/go-sqlite3 v1.14.22 // indirect
5251
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
5352
github.com/mfridman/interpolate v0.0.2 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
8888
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
8989
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
9090
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
91-
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
92-
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
93-
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
9491
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
9592
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
9693
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
@@ -161,7 +158,6 @@ golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
161158
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
162159
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
163160
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
164-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
165161
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
166162
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
167163
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=

internal/config/config.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313

1414
// Config represents the configuration for the paymaster.
1515
type Config struct {
16-
APIKey string `json:"api_key"`
17-
PaymasterAddressV7 common.Address `json:"paymaster_address_v7"`
18-
RateLimiterQPS int64 `json:"rate_limiter_qps"`
19-
ChainID int64 `json:"chain_id"`
20-
SignerPrivateKey string `json:"signer_private_key"`
21-
USDTAddress common.Address `json:"usdt_address"`
22-
EthereumRPCURLs []string `json:"ethereum_rpc_urls"`
23-
DBConfig *database.Config `json:"db_config"`
16+
APIKey string `json:"api_key"`
17+
PaymasterAddressV7 common.Address `json:"paymaster_address_v7"`
18+
RateLimiterQPS int64 `json:"rate_limiter_qps"`
19+
ChainID int64 `json:"chain_id"`
20+
SignerPrivateKey string `json:"signer_private_key"`
21+
USDTAddress common.Address `json:"usdt_address"`
22+
EthereumRPCURLs []string `json:"ethereum_rpc_urls"`
23+
DBConfig database.Config `json:"db_config"`
2424
}
2525

2626
// NewConfig return an unmarshalled config instance.

internal/controller/admin.go

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"math/big"
8-
"strconv"
98

109
"github.com/ethereum/go-ethereum/log"
1110
"github.com/gin-gonic/gin"
@@ -19,15 +18,13 @@ import (
1918
// AdminController handles admin API requests
2019
type AdminController struct {
2120
cfg *config.Config
22-
db *gorm.DB
2321
policyOrm *orm.Policy
2422
}
2523

2624
// NewAdminController creates a new AdminController
2725
func NewAdminController(cfg *config.Config, db *gorm.DB) *AdminController {
2826
return &AdminController{
2927
cfg: cfg,
30-
db: db,
3128
policyOrm: orm.NewPolicy(db),
3229
}
3330
}
@@ -63,7 +60,7 @@ func (ac *AdminController) handleListPolicies(c *gin.Context, req types.Paymaste
6360
var result []types.PolicyResponse
6461
for _, policy := range policies {
6562
result = append(result, types.PolicyResponse{
66-
PolicyID: strconv.FormatInt(policy.PolicyID, 10),
63+
PolicyID: policy.PolicyID,
6764
PolicyName: policy.PolicyName,
6865
Limits: policy.Limits,
6966
CreatedAt: policy.CreatedAt.Format("2006-01-02T15:04:05Z"),
@@ -78,25 +75,26 @@ func (ac *AdminController) handleListPolicies(c *gin.Context, req types.Paymaste
7875
// handleGetPolicyByID retrieves a specific policy by ID
7976
func (ac *AdminController) handleGetPolicyByID(c *gin.Context, req types.PaymasterJSONRPCRequest, apiKey string) {
8077
var params struct {
81-
PolicyID string `json:"policy_id"`
78+
PolicyID *int64 `json:"policy_id"`
8279
}
8380

8481
if err := json.Unmarshal(req.Params, &params); err != nil {
8582
types.SendError(c, req.ID, types.InvalidParamsCode, "Invalid params structure")
8683
return
8784
}
8885

89-
if params.PolicyID == "" {
86+
if params.PolicyID == nil {
9087
types.SendError(c, req.ID, types.InvalidParamsCode, "policy_id is required")
9188
return
9289
}
9390

94-
policyID, err := strconv.ParseInt(params.PolicyID, 10, 64)
95-
if err != nil {
96-
types.SendError(c, req.ID, types.InvalidParamsCode, "Invalid policy_id format")
91+
if *params.PolicyID < 0 {
92+
types.SendError(c, req.ID, types.InvalidParamsCode, "policy_id must be positive")
9793
return
9894
}
9995

96+
policyID := *params.PolicyID
97+
10098
policy, err := ac.policyOrm.GetByAPIKeyAndPolicyID(c.Request.Context(), apiKey, policyID)
10199
if err != nil {
102100
if err == gorm.ErrRecordNotFound {
@@ -109,7 +107,7 @@ func (ac *AdminController) handleGetPolicyByID(c *gin.Context, req types.Paymast
109107
}
110108

111109
result := types.PolicyResponse{
112-
PolicyID: strconv.FormatInt(policy.PolicyID, 10),
110+
PolicyID: policy.PolicyID,
113111
PolicyName: policy.PolicyName,
114112
Limits: policy.Limits,
115113
CreatedAt: policy.CreatedAt.Format("2006-01-02T15:04:05Z"),
@@ -123,7 +121,7 @@ func (ac *AdminController) handleGetPolicyByID(c *gin.Context, req types.Paymast
123121
// handleCreatePolicy creates a new policy
124122
func (ac *AdminController) handleCreatePolicy(c *gin.Context, req types.PaymasterJSONRPCRequest, apiKey string) {
125123
var params struct {
126-
PolicyID string `json:"policy_id"`
124+
PolicyID *int64 `json:"policy_id"`
127125
PolicyName string `json:"policy_name"`
128126
Limits orm.PolicyLimits `json:"limits"`
129127
}
@@ -133,24 +131,25 @@ func (ac *AdminController) handleCreatePolicy(c *gin.Context, req types.Paymaste
133131
return
134132
}
135133

136-
if params.PolicyID == "" {
134+
if params.PolicyID == nil {
137135
types.SendError(c, req.ID, types.InvalidParamsCode, "policy_id is required")
138136
return
139137
}
140138

141-
if params.PolicyName == "" {
142-
types.SendError(c, req.ID, types.InvalidParamsCode, "policy_name is required")
139+
if *params.PolicyID < 0 {
140+
types.SendError(c, req.ID, types.InvalidParamsCode, "policy_id must be positive")
143141
return
144142
}
145143

146-
policyID, err := strconv.ParseInt(params.PolicyID, 10, 64)
147-
if err != nil {
148-
types.SendError(c, req.ID, types.InvalidParamsCode, "Invalid policy_id format")
144+
policyID := *params.PolicyID
145+
146+
if params.PolicyName == "" {
147+
types.SendError(c, req.ID, types.InvalidParamsCode, "policy_name is required")
149148
return
150149
}
151150

152151
// Validate limits
153-
if err = ac.validatePolicyLimits(&params.Limits); err != nil {
152+
if err := ac.validatePolicyLimits(&params.Limits); err != nil {
154153
types.SendError(c, req.ID, types.PolicyValidationErrorCode, "Invalid limits: "+err.Error())
155154
return
156155
}
@@ -162,8 +161,7 @@ func (ac *AdminController) handleCreatePolicy(c *gin.Context, req types.Paymaste
162161
Limits: params.Limits,
163162
}
164163

165-
err = ac.policyOrm.Create(c.Request.Context(), newPolicy)
166-
if err != nil {
164+
if err := ac.policyOrm.Create(c.Request.Context(), newPolicy); err != nil {
167165
log.Error("Failed to create policy", "error", err, "apiKey", apiKey)
168166
types.SendError(c, req.ID, types.InternalServerError, "Failed to create policy")
169167
return
@@ -181,7 +179,7 @@ func (ac *AdminController) handleCreatePolicy(c *gin.Context, req types.Paymaste
181179
// handleUpdatePolicy updates an existing policy
182180
func (ac *AdminController) handleUpdatePolicy(c *gin.Context, req types.PaymasterJSONRPCRequest, apiKey string) {
183181
var params struct {
184-
PolicyID string `json:"policy_id"`
182+
PolicyID *int64 `json:"policy_id"`
185183
PolicyName *string `json:"policy_name,omitempty"`
186184
Limits *orm.PolicyLimits `json:"limits,omitempty"`
187185
}
@@ -191,28 +189,28 @@ func (ac *AdminController) handleUpdatePolicy(c *gin.Context, req types.Paymaste
191189
return
192190
}
193191

194-
if params.PolicyID == "" {
192+
if params.PolicyID == nil {
195193
types.SendError(c, req.ID, types.InvalidParamsCode, "policy_id is required")
196194
return
197195
}
198196

199-
policyID, err := strconv.ParseInt(params.PolicyID, 10, 64)
200-
if err != nil {
201-
types.SendError(c, req.ID, types.InvalidParamsCode, "Invalid policy_id format")
197+
if *params.PolicyID < 0 {
198+
types.SendError(c, req.ID, types.InvalidParamsCode, "policy_id must be positive")
202199
return
203200
}
204201

202+
policyID := *params.PolicyID
203+
205204
// Validate limits if provided
206205
if params.Limits != nil {
207-
if err = ac.validatePolicyLimits(params.Limits); err != nil {
206+
if err := ac.validatePolicyLimits(params.Limits); err != nil {
208207
types.SendError(c, req.ID, types.PolicyValidationErrorCode, "Invalid limits: "+err.Error())
209208
return
210209
}
211210
}
212211

213212
// Check if policy exists
214-
_, err = ac.policyOrm.GetByAPIKeyAndPolicyID(c.Request.Context(), apiKey, policyID)
215-
if err != nil {
213+
if _, err := ac.policyOrm.GetByAPIKeyAndPolicyID(c.Request.Context(), apiKey, policyID); err != nil {
216214
if err == gorm.ErrRecordNotFound {
217215
types.SendError(c, req.ID, types.PolicyNotFoundCode, "Policy not found")
218216
return
@@ -229,7 +227,7 @@ func (ac *AdminController) handleUpdatePolicy(c *gin.Context, req types.Paymaste
229227
}
230228
if params.Limits != nil {
231229
var limitsJSON []byte
232-
limitsJSON, err = json.Marshal(*params.Limits)
230+
limitsJSON, err := json.Marshal(*params.Limits)
233231
if err != nil {
234232
types.SendError(c, req.ID, types.InternalErrorCode, "Failed to serialize limits")
235233
return
@@ -243,8 +241,7 @@ func (ac *AdminController) handleUpdatePolicy(c *gin.Context, req types.Paymaste
243241
}
244242

245243
// Perform update
246-
err = ac.policyOrm.Update(c.Request.Context(), apiKey, policyID, updates)
247-
if err != nil {
244+
if err := ac.policyOrm.Update(c.Request.Context(), apiKey, policyID, updates); err != nil {
248245
log.Error("Failed to update policy", "error", err, "apiKey", apiKey, "policyID", policyID)
249246
types.SendError(c, req.ID, types.InternalServerError, "Failed to update policy")
250247
return
@@ -262,28 +259,28 @@ func (ac *AdminController) handleUpdatePolicy(c *gin.Context, req types.Paymaste
262259
// handleDeletePolicy deletes a policy
263260
func (ac *AdminController) handleDeletePolicy(c *gin.Context, req types.PaymasterJSONRPCRequest, apiKey string) {
264261
var params struct {
265-
PolicyID string `json:"policy_id"`
262+
PolicyID *int64 `json:"policy_id"`
266263
}
267264

268265
if err := json.Unmarshal(req.Params, &params); err != nil {
269266
types.SendError(c, req.ID, types.InvalidParamsCode, "Invalid params structure")
270267
return
271268
}
272269

273-
if params.PolicyID == "" {
270+
if params.PolicyID == nil {
274271
types.SendError(c, req.ID, types.InvalidParamsCode, "policy_id is required")
275272
return
276273
}
277274

278-
policyID, err := strconv.ParseInt(params.PolicyID, 10, 64)
279-
if err != nil {
280-
types.SendError(c, req.ID, types.InvalidParamsCode, "Invalid policy_id format")
275+
if *params.PolicyID < 0 {
276+
types.SendError(c, req.ID, types.InvalidParamsCode, "policy_id must be positive")
281277
return
282278
}
283279

280+
policyID := *params.PolicyID
281+
284282
// Check if policy exists
285-
_, err = ac.policyOrm.GetByAPIKeyAndPolicyID(c.Request.Context(), apiKey, policyID)
286-
if err != nil {
283+
if _, err := ac.policyOrm.GetByAPIKeyAndPolicyID(c.Request.Context(), apiKey, policyID); err != nil {
287284
if err == gorm.ErrRecordNotFound {
288285
types.SendError(c, req.ID, types.PolicyNotFoundCode, "Policy not found")
289286
return
@@ -294,8 +291,7 @@ func (ac *AdminController) handleDeletePolicy(c *gin.Context, req types.Paymaste
294291
}
295292

296293
// Perform deletion (soft delete)
297-
err = ac.policyOrm.Delete(c.Request.Context(), apiKey, policyID)
298-
if err != nil {
294+
if err := ac.policyOrm.Delete(c.Request.Context(), apiKey, policyID); err != nil {
299295
log.Error("Failed to delete policy", "error", err, "apiKey", apiKey, "policyID", policyID)
300296
types.SendError(c, req.ID, types.InternalServerError, "Failed to delete policy")
301297
return

0 commit comments

Comments
 (0)