Skip to content

Commit

Permalink
Merge branch 'uddugteam:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
andskur authored Jan 22, 2024
2 parents 0a56247 + afeafd3 commit 37402ee
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 40 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ run-ws-example:

# To run whitelist command use this example
# go run -race ./cmd/oracle-flare.go whitelist --address 0x8382Be7cc5C2Cd8b14F44108444ced6745c5feCb --token testETH
# go run -race ./cmd/oracle-flare.go whitelistall --address 0x8382Be7cc5C2Cd8b14F44108444ced6745c5feCb

# The clean target deletes the build output file
clean:
Expand Down
2 changes: 2 additions & 0 deletions cmd/oracle-flare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"oracle-flare/cmd/whitelist"
"oracle-flare/cmd/whitelistall"
"os"

"github.com/misnaged/annales/logger"
Expand All @@ -26,6 +27,7 @@ func main() {
rootCmd := root.Cmd(app)
rootCmd.AddCommand(serve.Cmd(app))
rootCmd.AddCommand(whitelist.Cmd(app))
rootCmd.AddCommand(whitelistall.Cmd(app))

if err := rootCmd.Execute(); err != nil {
logger.Log().Infof("An error occurred: %s", err.Error())
Expand Down
38 changes: 38 additions & 0 deletions cmd/whitelistall/whitelistall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package whitelistall

import (
"fmt"

"github.com/spf13/cobra"

"oracle-flare/internal"
"oracle-flare/pkg/logger"
)

// Cmd returns the "whitelist" command of the application.
// This command is responsible for initializing and adding given address for given token to the smart-contract whitelist
func Cmd(app *internal.App) *cobra.Command {
cmd := &cobra.Command{
Use: "whitelistall",
Short: "Whitelist address for all tokens in the config file",
RunE: func(cmd *cobra.Command, args []string) error {
address, err := cmd.Flags().GetString("address")
if err != nil {
return fmt.Errorf("err get address flag: %w", err)
}

if err := app.InitForWhiteList(); err != nil {
return fmt.Errorf("application initialisation: %w", err)
}

return app.WhiteListAddressAll(address)
},
PreRun: func(cmd *cobra.Command, args []string) {
logger.Log().Info(app.Version())
},
}

cmd.Flags().String("address", "", "wallet address for whitelist")

return cmd
}
16 changes: 11 additions & 5 deletions internal/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,22 @@ func (app *App) InitForWhiteList() error {

// WhiteListAddress is used to run for whitelist command
func (app *App) WhiteListAddress(address string, token string) error {
ok, err := app.srv.WhiteListAddress(address, token)
_, err := app.srv.WhiteListAddress(address, []string{token})
if err != nil {
app.Stop()
return err
}

if ok {
logInfo(fmt.Sprintf("address: %s for %s whitelisted successfully", address, token), "WhiteListAddress")
} else {
logWarn(fmt.Sprintf("address: %s for %s whitelisted unsuccessfully", address, token), "WhiteListAddress")
app.Stop()
return nil
}

// WhiteListAddressAll is used to run for whitelistall command
func (app *App) WhiteListAddressAll(address string) error {
_, err := app.srv.WhiteListAddress(address, app.config.Tokens)
if err != nil {
app.Stop()
return err
}

app.Stop()
Expand Down
12 changes: 11 additions & 1 deletion internal/service/coinAvgPrice.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ func (s *service) SendCoinAveragePrice(tokens []string) {
parsedTokens := []contracts.TokenID{}

for _, t := range tokens {
parsedTokens = append(parsedTokens, contracts.GetTokenIDFromName(t))
parsedToken := contracts.GetTokenIDFromName(t)
if parsedToken == contracts.UnknownToken {
logWarn(fmt.Sprintln("received unknown token:", t), "SendCoinAveragePrice")
} else {
parsedTokens = append(parsedTokens, parsedToken)
}
}

if len(parsedTokens) == 0 {
logErr("all tokens are invalid", "SendCoinAveragePrice")
return
}

sender := newCoinAvgPriceSender(len(s.avgPriceSenders), s.flare, s.wsClient, parsedTokens)
Expand Down
8 changes: 4 additions & 4 deletions internal/service/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
//func logFatal(msg string, method string) {
// logger.Log().WithField("layer", fmt.Sprintf("Service-%s", method)).Fatal(msg)
//}
//
//func logWarn(msg string, method string) {
// logger.Log().WithField("layer", fmt.Sprintf("Service-%s", method)).Warning(msg)
//}

func logWarn(msg string, method string) {
logger.Log().WithField("layer", fmt.Sprintf("Service-%s", method)).Warning(msg)
}

func logInfo(msg string, method string) {
logger.Log().WithField("layer", fmt.Sprintf("Service-%s", method)).Info(msg)
Expand Down
4 changes: 2 additions & 2 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

// IService is a service layer interface
type IService interface {
// WhiteListAddress is used to add address to the smart-contract whitelist with given token
WhiteListAddress(addressS string, indexS string) (bool, error)
// WhiteListAddress is used to add address to the smart-contract whitelist with given tokens
WhiteListAddress(addressS string, indicesS []string) ([]bool, error)
// SendCoinAveragePrice is used to send coin average price from the ws service to the flare smart-contracts
SendCoinAveragePrice(tokens []string)
// Close is used to stop the service
Expand Down
72 changes: 53 additions & 19 deletions internal/service/whitLister.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,70 @@ import (
"oracle-flare/pkg/flare/contracts"
)

func (s *service) WhiteListAddress(addressS string, indexS string) (bool, error) {
func (s *service) WhiteListAddress(addressS string, indicesS []string) ([]bool, error) {
if addressS == "" {
return false, fmt.Errorf("no address given")
return nil, fmt.Errorf("no address given")
}

if indexS == "" {
return false, fmt.Errorf("no index given")
if len(indicesS) == 0 {
return nil, fmt.Errorf("no indices given")
}

address := common.HexToAddress(addressS)
index := contracts.GetTokenIDFromSymbol(indexS)

isWhitListed, err := s.isAddressWhitelisted(index, address)
if err != nil {
return false, err
}
res := []bool{}
for _, i := range indicesS {
logInfo(fmt.Sprintln("whitelisting for:", i), "WhiteListAddress")

if isWhitListed {
logInfo("address already whitelisted", "WhiteListAddress")
return true, nil
}
index := contracts.GetTokenIDFromName(i)

if err := s.flare.RequestWhitelistingVoter(address, index); err != nil {
return false, nil
}
if index == contracts.UnknownToken {
logWarn("unknown token", "WhiteListAddress")
res = append(res, false)
continue
}

isWhitListed, err := s.isAddressWhitelisted(index, address)
if err != nil {
logErr(fmt.Sprintln("err isAddressWhitelisted:", err.Error()), "WhiteListAddress")
res = append(res, false)
continue
}

// wait for the tx
time.Sleep(time.Second * 3)
if isWhitListed {
if err != nil {
logErr("address already whitelisted", "WhiteListAddress")
res = append(res, true)
continue
}
}

if err := s.flare.RequestWhitelistingVoter(address, index); err != nil {
logErr(fmt.Sprintln("err RequestWhitelistingVoter:", err.Error()), "WhiteListAddress")
res = append(res, false)
continue
}

// wait for the tx
time.Sleep(time.Second * 3)

isWhitListed, err = s.isAddressWhitelisted(index, address)
if err != nil {
logErr(fmt.Sprintln("err isAddressWhitelisted:", err.Error()), "WhiteListAddress")
res = append(res, false)
continue
}

if isWhitListed {
logInfo("token whitelisted", "WhiteListAddress")
res = append(res, true)
} else {
logWarn("token not whitelisted", "WhiteListAddress")
res = append(res, false)
}
}

return s.isAddressWhitelisted(index, address)
return res, nil
}

func (s *service) isAddressWhitelisted(index contracts.TokenID, target common.Address) (bool, error) {
Expand Down
Loading

0 comments on commit 37402ee

Please sign in to comment.