Skip to content

Commit

Permalink
re-number migration
Browse files Browse the repository at this point in the history
start/end from snapshot.org are nix times
  • Loading branch information
asamere committed Feb 14, 2023
1 parent fc8fc0c commit 5c893e8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 deletions.
28 changes: 15 additions & 13 deletions cli/snapshot.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package cli

import (
"github.com/ArkeoNetwork/airdrop/pkg/db"
"github.com/ArkeoNetwork/airdrop/snapshot"
"github.com/ArkeoNetwork/common/utils"
"github.com/spf13/cobra"
)

func runSnapshotIndexer(cmd *cobra.Command, args []string) {
log.Info("starting gethering snapshot data process")
flags := cmd.InheritedFlags()
envPath, _ := flags.GetString("env")
c := readConfig(envPath)
c := utils.ReadDBConfig(envPath)
if c == nil {
cmd.PrintErrf("no config for path %s", envPath)
return
}
snapshotIndexerApp := snapshot.NewSnapshotIndexer(snapshot.SnapshotIndexerAppParams{
SnapshotStart: c.SnapshotStart,
SnapshotEnd: c.SnapshotEnd,
DBConfig: db.DBConfig{
Host: c.DBHost,
Port: c.DBPort,
User: c.DBUser,
Pass: c.DBPass,
DBName: c.DBName,
PoolMaxConns: c.DBPoolMaxConns,
PoolMinConns: c.DBPoolMinConns,
SSLMode: c.DBSSLMode,
DBConfig: utils.DBConfig{
DBHost: c.DBHost,
DBPort: c.DBPort,
DBUser: c.DBUser,
DBPass: c.DBPass,
DBName: c.DBName,
DBPoolMaxConns: c.DBPoolMaxConns,
DBPoolMinConns: c.DBPoolMinConns,
DBSSLMode: c.DBSSLMode,
},
})
snapshotIndexerApp.Start()
Expand Down
File renamed without changes.
56 changes: 43 additions & 13 deletions snapshot/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package snapshot
import (
"context"
"fmt"
"math/big"

"github.com/ArkeoNetwork/airdrop/pkg/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/hasura/go-graphql-client"
"github.com/pkg/errors"
)
Expand All @@ -13,18 +15,18 @@ var snapshotGraphqlclient = graphql.NewClient("https://hub.snapshot.org/graphql"
var pageSize = 1000

func getSingleProposalVotersPage(proposalId string, page int) ([]string, error) {
var proposalVotesQuery struct {
var proposalVotesQuery struct {
Votes []struct {
Id string `json:"id"`
Id string `json:"id"`
Voter string `json:"voter"`
} `graphql:"votes (first: $pageSize, skip: $skip, where: { proposal: $proposalId }, orderBy: \"created\", orderDirection: desc)"`
// NOTE: the above line should be a one-liner, and there should be no whitespaces
// NOTE: the above line should be a one-liner, and there should be no whitespaces
// between `graphql:` and `"`.
}
variables := map[string]interface{}{
"proposalId": proposalId,
"pageSize": pageSize,
"skip": pageSize * page,
"proposalId": proposalId,
"pageSize": pageSize,
"skip": pageSize * page,
}
err := snapshotGraphqlclient.Query(context.Background(), &proposalVotesQuery, variables)
if err != nil {
Expand All @@ -49,29 +51,57 @@ func getSingleProposalVoters(proposalId string) ([]string, error) {
voters = append(voters, pageVoters...)
page++
// if this page result size is lower than the pageSize, all votes have been gathered.
if (len(pageVoters) < pageSize) { break }
if len(pageVoters) < pageSize {
break
}
}
return voters, nil
}

func (app *SnapshotIndexerApp) GetSnapshotProposalVoters() ([]*types.SnapshotVoter, error) {
ethChain, err := app.db.FindChain("ETH")
if err != nil {
return nil, errors.Wrapf(err, "error finding ETH chain")
}

client, err := ethclient.Dial(ethChain.RpcUrl)
if err != nil {
return nil, errors.Wrap(err, "failed to connect to eth RPC client")
}

ctx := context.Background()
block, err := client.BlockByNumber(ctx, big.NewInt(int64(ethChain.SnapshotStartBlock)))
if err != nil {
return nil, errors.Wrap(err, "failed to get current block from RPC client")
}
startBlockTime := block.Time()
log.Infof("startBlockTime: %d", startBlockTime)

if block, err = client.BlockByNumber(ctx, big.NewInt(int64(ethChain.SnapshotEndBlock))); err != nil {
return nil, errors.Wrapf(err, "error getting snapshot end block")
}
endBlockTime := block.Time()
log.Infof("endBlockTime: %d", endBlockTime)

var proposalsQuery struct {
Proposals []struct {
Id string `json:"id"`
Id string `json:"id"`
Start uint64 `json:"start"`
End uint64 `json:"end"`
End uint64 `json:"end"`
Title string `json:"title"`
} `graphql:"proposals (first: 1000, where: { space: \"shapeshiftdao.eth\" }, orderBy: \"created\", orderDirection: desc)"`
// NOTE: the above line should be a one-liner, and there should be no whitespaces
// NOTE: the above line should be a one-liner, and there should be no whitespaces
// between `graphql:` and `"`.
}
err := snapshotGraphqlclient.Query(context.Background(), &proposalsQuery, nil)
if err != nil {

// map[string]interface{}{"start": 1669759199, "end": 1676400155}
if err = snapshotGraphqlclient.Query(context.Background(), &proposalsQuery, nil); err != nil {
return nil, errors.Wrapf(err, "error querying proposals")
}

allVoters := make(map[string]*types.SnapshotVoter)
for _, proposal := range proposalsQuery.Proposals {
if proposal.Start >= app.params.SnapshotStart && proposal.End <= app.params.SnapshotEnd {
if proposal.End >= startBlockTime && proposal.End <= endBlockTime {
// getting voters for a single proposal
log.Info("getting voters for proposal ", proposal.Title)
proposalVoters, err := getSingleProposalVoters(proposal.Id)
Expand Down
6 changes: 2 additions & 4 deletions snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"fmt"

"github.com/ArkeoNetwork/airdrop/pkg/db"
"github.com/ArkeoNetwork/common/utils"
"github.com/ArkeoNetwork/directory/pkg/logging"
)

type SnapshotIndexerAppParams struct {
SnapshotStart uint64
SnapshotEnd uint64
db.DBConfig
utils.DBConfig
}

type SnapshotIndexerApp struct {
Expand Down Expand Up @@ -38,4 +37,3 @@ func (app *SnapshotIndexerApp) Start() {
panic(fmt.Sprintf("error inserting snapshot proposal voters: %+v", err))
}
}

0 comments on commit 5c893e8

Please sign in to comment.