Skip to content

Commit 051b7cf

Browse files
committed
clean code
1 parent 603966c commit 051b7cf

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

gno_github_agent/.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
GNO_MNEMONIC=scissors razor beauty delay derive chronic toss burger gravity shallow couch slogan change tray connect frame token slight zone usage sad monkey pyramid change
22
GNO_CHAIN_ID=dev
33
GNO_RPC_ADDR=http://127.0.0.1:26657
4-
GNO_REALM_PATH=gno.land/r/teritori/ghverify
4+
GNO_REALM_PATH=gno.land/r/teritori/ghverify
5+
GNO_TX_INDEXER=http://localhost:8546/graphql/query

gno_github_agent/clientql/client.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package clientql
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"net/http"
@@ -29,7 +30,7 @@ func New(graphqlEndpoint string, db *gorm.DB, logger *zap.SugaredLogger, gnoSign
2930
}
3031

3132
func (client *IndexerQL) DealWithVerifications() error {
32-
lastBlock, err := getLastTreatedBlock()
33+
lastBlock, err := client.getLastTreatedBlock()
3334
if err != nil {
3435
return err
3536
}
@@ -45,7 +46,8 @@ func (client *IndexerQL) DealWithVerifications() error {
4546
switch event := responseEvent.(type) {
4647
case *gnoindexerql.GetValidationRequestsTransactionsTransactionResponseEventsGnoEvent:
4748
client.logger.Infof("args %v\n", event.Attrs)
48-
err := client.dealWithVerification(event)
49+
50+
err := client.dealWithVerification(event, validationRequest.Block_height)
4951
if err != nil {
5052
client.logger.Errorf("failed to deal with verification: %s", err.Error())
5153
continue
@@ -60,11 +62,21 @@ func (client *IndexerQL) DealWithVerifications() error {
6062
return nil
6163
}
6264

63-
func getLastTreatedBlock() (int, error) {
64-
return 0, nil
65+
func (client *IndexerQL) getLastTreatedBlock() (int, error) {
66+
var verification db.Verification
67+
err := client.db.Model(&db.Verification{}).Where("status = ?", string(db.VerificationStatusVerified)).Order("id desc").First(&verification).Error
68+
69+
if err != nil {
70+
if errors.Is(err, gorm.ErrRecordNotFound) {
71+
return 0, nil
72+
}
73+
74+
return 0, nil
75+
}
76+
return verification.BlockHeight, err
6577
}
6678

67-
func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationRequestsTransactionsTransactionResponseEventsGnoEvent) error {
79+
func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationRequestsTransactionsTransactionResponseEventsGnoEvent, blockHeight int) error {
6880
var handle string
6981
var callerAddress string
7082
for _, attr := range event.Attrs {
@@ -75,6 +87,7 @@ func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationR
7587
callerAddress = attr.Value
7688
}
7789
}
90+
7891
var verification db.Verification
7992
err := client.db.Model(&db.Verification{}).Where("handle = ? AND address = ?", handle, callerAddress).Find(&verification).Error
8093
if err != nil {
@@ -92,36 +105,37 @@ func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationR
92105
return err
93106
}
94107

95-
client.logger.Infof("Get\n")
96108
defer res.Body.Close()
97109
if res.StatusCode != 200 {
98-
return client.updateVerification(handle, callerAddress, "config_not_found")
110+
return client.updateVerification(handle, callerAddress, db.VerificationStatusConfigNotFound, blockHeight)
99111
}
112+
100113
data, err := io.ReadAll(res.Body)
101114
if err != nil {
102-
client.updateVerification(handle, callerAddress, "invalid_data")
115+
client.updateVerification(handle, callerAddress, db.VerificationStatusInvalidData, blockHeight)
103116
return err
104117
}
105-
client.logger.Infof("config.yml: %s\n", string(data))
118+
106119
githubConfiguredAddress := strings.TrimSpace(string(data))
107120
if githubConfiguredAddress == callerAddress {
108121
err = client.signer.CallVerify(githubConfiguredAddress)
109122
if err != nil {
110123
return err
111124
}
112125

113-
return client.updateVerification(handle, callerAddress, "verified")
126+
return client.updateVerification(handle, callerAddress, db.VerificationStatusVerified, blockHeight)
114127
}
115-
return client.updateVerification(handle, callerAddress, "caller_address_mismatch")
128+
return client.updateVerification(handle, callerAddress, db.VerificationStatusCallerAddressMismatch, blockHeight)
116129
}
117130

118-
func (client *IndexerQL) updateVerification(handle, address, status string) error {
131+
func (client *IndexerQL) updateVerification(handle, address string, status db.VerificationStatus, blockHeight int) error {
119132
verification := db.Verification{
120-
Handle: handle,
121-
Address: address,
122-
Status: status,
123-
CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
133+
Handle: handle,
134+
Address: address,
135+
Status: string(status),
136+
CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
137+
BlockHeight: blockHeight,
124138
}
125139

126-
return client.db.Model(&verification).Where("handle = ? AND address = ?", handle, address).Assign(db.Verification{Status: status}).FirstOrCreate(&verification).Error
140+
return client.db.Model(&verification).Where("handle = ? AND address = ?", handle, address).Assign(db.Verification{Status: string(status)}).FirstOrCreate(&verification).Error
127141
}

gno_github_agent/db/db.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ type Verification struct {
2929
gorm.Model
3030
Id uint `json:"id" gorm:"unique;primaryKey;autoIncrement"`
3131

32-
Handle string
33-
Address string
34-
Status string
35-
CreatedAt string
32+
Handle string
33+
Address string
34+
Status string
35+
CreatedAt string
36+
BlockHeight int
3637
}
38+
39+
type VerificationStatus string
40+
41+
const (
42+
VerificationStatusUnverified VerificationStatus = "unverified"
43+
VerificationStatusVerified VerificationStatus = "verified"
44+
VerificationStatusConfigNotFound VerificationStatus = "config_not_found"
45+
VerificationStatusInvalidData VerificationStatus = "invalid_data"
46+
VerificationStatusCallerAddressMismatch VerificationStatus = "caller_address_mismatch"
47+
)

gno_github_agent/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ func main() {
3434
chainID := os.Getenv("GNO_CHAIN_ID")
3535
rpcAddr := os.Getenv("GNO_RPC_ADDR")
3636
realmPath := os.Getenv("GNO_REALM_PATH")
37+
txIndexerHost := os.Getenv("GNO_TX_INDEXER")
3738

3839
gnoSigner = signer.New(db, logger.Sugar(), mnemonic, chainID, rpcAddr, realmPath)
3940

40-
clientql := clientql.New("http://localhost:8546/graphql/query", db, logger.Sugar(), gnoSigner)
41+
clientql := clientql.New(txIndexerHost, db, logger.Sugar(), gnoSigner)
4142
schedule := gocron.NewScheduler(time.UTC)
4243

4344
schedule.Every(30).Seconds().Do(func() {
4445
err = clientql.DealWithVerifications()
4546
if err != nil {
46-
logger.Error("failed to get names list", zap.Error(err))
47+
logger.Error("failed to deal with verifications", zap.Error(err))
4748
panic(err)
4849
}
4950
})

0 commit comments

Comments
 (0)