refactor: move pkg's out

- to cic_go
This commit is contained in:
Mohamed Sohail 2022-05-06 16:53:57 +03:00
parent b4a7668ca4
commit 9882a9ddc6
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
9 changed files with 10 additions and 281 deletions

View File

@ -1,9 +1,9 @@
package main package main
import ( import (
"cic-dw/pkg/cicnet"
"context" "context"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/cic_go/cic_net"
"github.com/hibiken/asynq" "github.com/hibiken/asynq"
"github.com/jackc/pgx/v4/pgxpool" "github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf" "github.com/knadh/koanf"
@ -69,7 +69,7 @@ func parseRedis(dsn string) (asynq.RedisConnOpt, error) {
func connectCicNet(rpcProvider string, tokenIndex common.Address) error { func connectCicNet(rpcProvider string, tokenIndex common.Address) error {
var err error var err error
cicnetClient, err = cicnet.NewCicNet(rpcProvider, tokenIndex) cicnetClient, err = cic_net.NewCicNet(rpcProvider, tokenIndex)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"cic-dw/pkg/cicnet" "github.com/grassrootseconomics/cic_go/cic_net"
"github.com/jackc/pgx/v4/pgxpool" "github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf" "github.com/knadh/koanf"
"github.com/lmittmann/w3" "github.com/lmittmann/w3"
@ -19,7 +19,7 @@ var (
queries goyesql.Queries queries goyesql.Queries
conf config conf config
db *pgxpool.Pool db *pgxpool.Pool
cicnetClient *cicnet.CicNet cicnetClient *cic_net.CicNet
) )
func init() { func init() {
@ -37,7 +37,6 @@ func init() {
log.Fatal().Err(err).Msg("failed to connect to postgres") log.Fatal().Err(err).Msg("failed to connect to postgres")
} }
// TODO: Not core, should be handled by job processor
if err := connectCicNet(conf.Chain.RpcProvider, w3.A(conf.Chain.TokenRegistry)); err != nil { if err := connectCicNet(conf.Chain.RpcProvider, w3.A(conf.Chain.TokenRegistry)); err != nil {
log.Fatal().Err(err).Msg("failed to connect to postgres") log.Fatal().Err(err).Msg("failed to connect to postgres")
} }

4
go.mod
View File

@ -5,19 +5,19 @@ go 1.18
require ( require (
github.com/ethereum/go-ethereum v1.10.17 github.com/ethereum/go-ethereum v1.10.17
github.com/georgysavva/scany v0.3.0 github.com/georgysavva/scany v0.3.0
github.com/grassrootseconomics/cic_go v0.1.0
github.com/hibiken/asynq v0.23.0 github.com/hibiken/asynq v0.23.0
github.com/jackc/pgx/v4 v4.16.0 github.com/jackc/pgx/v4 v4.16.0
github.com/knadh/koanf v1.4.1 github.com/knadh/koanf v1.4.1
github.com/lmittmann/w3 v0.7.0 github.com/lmittmann/w3 v0.7.0
github.com/nleof/goyesql v1.0.1 github.com/nleof/goyesql v1.0.1
github.com/rs/zerolog v1.26.1 github.com/rs/zerolog v1.26.1
golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6
) )
require ( require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.1.2 // indirect github.com/btcsuite/btcd/btcec/v2 v2.1.2 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect

7
go.sum
View File

@ -67,7 +67,6 @@ github.com/btcsuite/btcd/btcec/v2 v2.1.2 h1:YoYoC9J0jwfukodSBMzZYUVQ8PTiYg4BnOWi
github.com/btcsuite/btcd/btcec/v2 v2.1.2/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE= github.com/btcsuite/btcd/btcec/v2 v2.1.2/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34= github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s= github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
@ -226,6 +225,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/graph-gophers/graphql-go v1.3.0/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc= github.com/graph-gophers/graphql-go v1.3.0/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc=
github.com/grassrootseconomics/cic_go v0.1.0 h1:Z7HL3QuEGeUVo/9rNbpweIqolpQ0wHJfGGDa/x49diQ=
github.com/grassrootseconomics/cic_go v0.1.0/go.mod h1:7jATeNdmgfuai59yXcVrh8wOTJJJmR44ynftOm7PISA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-bexpr v0.1.10/go.mod h1:oxlubA2vC/gFVfX1A6JGp7ls7uCDlfJn732ehYYg+g0= github.com/hashicorp/go-bexpr v0.1.10/go.mod h1:oxlubA2vC/gFVfX1A6JGp7ls7uCDlfJn732ehYYg+g0=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
@ -699,8 +700,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba h1:AyHWHCBVlIYI5rgEM3o+1PLd0sLPcIAoaUckGQMaWtw= golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60=
golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View File

@ -1,32 +0,0 @@
package cicnet
import (
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
)
type CicNet struct {
ethClient *w3.Client
tokenIndex common.Address
}
func NewCicNet(rpcEndpoint string, tokenIndex common.Address) (*CicNet, error) {
ethClient, err := w3.Dial(rpcEndpoint)
if err != nil {
return nil, err
}
return &CicNet{
ethClient: ethClient,
tokenIndex: tokenIndex,
}, nil
}
func (c *CicNet) Close() error {
err := c.ethClient.Close()
if err != nil {
return err
}
return nil
}

View File

@ -1,39 +0,0 @@
package cicnet
import (
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth"
"math/big"
)
type DemurrageToken struct {
Name string
Symbol string
Decimals big.Int
}
func (c *CicNet) TokenInfo(ctx context.Context, tokenAddress common.Address) (DemurrageToken, error) {
var (
tokenName string
tokenSymbol string
tokenDecimals big.Int
)
err := c.ethClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("name()", "string"), tokenAddress).Returns(&tokenName),
eth.CallFunc(w3.MustNewFunc("symbol()", "string"), tokenAddress).Returns(&tokenSymbol),
eth.CallFunc(w3.MustNewFunc("decimals()", "uint256"), tokenAddress).Returns(&tokenDecimals),
)
if err != nil {
return DemurrageToken{}, err
}
return DemurrageToken{
Name: tokenName,
Symbol: tokenSymbol,
Decimals: tokenDecimals,
}, nil
}

View File

@ -1,37 +0,0 @@
package cicnet
import (
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth"
"math/big"
)
func (c *CicNet) EntryCount(ctx context.Context) (big.Int, error) {
var tokenCount big.Int
err := c.ethClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("entryCount()", "uint256"), c.tokenIndex).Returns(&tokenCount),
)
if err != nil {
return big.Int{}, err
}
return tokenCount, nil
}
func (c *CicNet) AddressAtIndex(ctx context.Context, index *big.Int) (string, error) {
var address common.Address
err := c.ethClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("entry(uint256 _idx)", "address"), c.tokenIndex, index).Returns(&address),
)
if err != nil {
return "", err
}
return address.String(), nil
}

View File

@ -1,113 +0,0 @@
package meta
import (
"github.com/ethereum/go-ethereum/common"
"net/http"
"strings"
"time"
)
const (
personPointer = ":cic.person"
phonePointer = ":cic.phone"
preferencesPointer = ":cic.preferences"
customPointer = ":cic.custom"
)
type CicMeta struct {
httpClient *http.Client
baseUrl string
}
type CustomResponse struct {
Tags []string `json:"tags"`
}
type PreferencesResponse struct {
PreferredLanguage string `json:"preferred_language"`
}
type PersonResponse struct {
DateRegistered int `json:"date_registered"`
VCard string `json:"vcard"`
Gender string `json:"gender"`
Location Location `json:"location"`
Products []string `json:"products"`
DateOfBirth DateOfBirth `json:"date_of_birth"`
}
type Location struct {
AreaName string `json:"area_name"`
}
type DateOfBirth struct {
Year int `json:"year"`
}
func NewCicMeta(metaEndpoint string) *CicMeta {
return &CicMeta{
httpClient: &http.Client{
Timeout: time.Second * 3,
},
baseUrl: metaEndpoint,
}
}
func (c *CicMeta) GetPhonePointer(phone string) (string, error) {
hashedKey := mergeSha256Key([]byte(phone), []byte(phonePointer))
r, err := requestHandler(c, hashedKey)
if err != nil {
return "", err
}
return strings.Trim(string(r), "\""), nil
}
func (c *CicMeta) GetPersonMetadata(address string) (PersonResponse, error) {
hashedKey := mergeSha256Key(common.HexToAddress(address).Bytes(), []byte(personPointer))
respData, err := requestHandler(c, hashedKey)
if err != nil {
return PersonResponse{}, err
}
metadata, err := jsonUnmarshaler(respData, PersonResponse{})
if err != nil {
return PersonResponse{}, err
}
return metadata, nil
}
func (c *CicMeta) GetPreferencesMetadata(address string) (PreferencesResponse, error) {
hashedKey := mergeSha256Key(common.HexToAddress(address).Bytes(), []byte(preferencesPointer))
respData, err := requestHandler(c, hashedKey)
if err != nil {
return PreferencesResponse{}, err
}
metadata, err := jsonUnmarshaler(respData, PreferencesResponse{})
if err != nil {
return PreferencesResponse{}, err
}
return metadata, nil
}
func (c *CicMeta) GetCustomMetadata(address string) (CustomResponse, error) {
hashedKey := mergeSha256Key(common.HexToAddress(address).Bytes(), []byte(customPointer))
respData, err := requestHandler(c, hashedKey)
if err != nil {
return CustomResponse{}, err
}
metadata, err := jsonUnmarshaler(respData, CustomResponse{})
if err != nil {
return CustomResponse{}, err
}
return metadata, nil
}

View File

@ -1,50 +0,0 @@
package meta
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func generateMetaUrl(metaBaseUrl string, key string) string {
return fmt.Sprintf("%s/%s", metaBaseUrl, key)
}
func mergeSha256Key(x []byte, y []byte) string {
h := sha256.New()
h.Write(x)
h.Write(y)
return hex.EncodeToString(h.Sum(nil))
}
func requestHandler(cicMeta *CicMeta, metadataKey string) ([]byte, error) {
resp, err := cicMeta.httpClient.Get(generateMetaUrl(cicMeta.baseUrl, metadataKey))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error fetching metadata for key %s: %s", metadataKey, resp.Status)
}
respData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respData, nil
}
func jsonUnmarshaler[T PersonResponse | PreferencesResponse | CustomResponse](respBody []byte, binding T) (T, error) {
if err := json.Unmarshal(respBody, &binding); err != nil {
return binding, err
}
return binding, nil
}