add: subscribe on oracle events functionality

This commit is contained in:
bahadylbekov 2020-08-07 00:47:58 +03:00
parent a70a79efa9
commit 953e54a41c
4 changed files with 31 additions and 3 deletions

View File

@ -14,6 +14,7 @@ type Config struct {
Rendezvous string `toml:"rendezvous"` Rendezvous string `toml:"rendezvous"`
ProtocolID string `toml:"protocol_id"` ProtocolID string `toml:"protocol_id"`
SessionKey string `toml:"session_key"` SessionKey string `toml:"session_key"`
PrivateKey string `toml:"private_key"`
} }
// viperEnvVariable loads config parameters from .env file // viperEnvVariable loads config parameters from .env file
@ -58,6 +59,7 @@ func NewConfig() *Config {
Rendezvous := viperEnvString("RENDEZVOUS", "filecoin-p2p-oracle") Rendezvous := viperEnvString("RENDEZVOUS", "filecoin-p2p-oracle")
ProtocolID := viperEnvString("PROTOCOL_ID", "p2p-oracle") ProtocolID := viperEnvString("PROTOCOL_ID", "p2p-oracle")
SessionKey := viperEnvString("SESSION_KEY", "go") SessionKey := viperEnvString("SESSION_KEY", "go")
PrivateKey := viperEnvString("PRIVATE_KEY", "")
return &Config{ return &Config{
ListenPort: ListenPort, ListenPort: ListenPort,
@ -67,5 +69,6 @@ func NewConfig() *Config {
Rendezvous: Rendezvous, Rendezvous: Rendezvous,
ProtocolID: ProtocolID, ProtocolID: ProtocolID,
SessionKey: SessionKey, SessionKey: SessionKey,
PrivateKey: PrivateKey,
} }
} }

View File

@ -36,6 +36,8 @@ func NewHandler(pb *pubsub.PubSub, oracleTopic string, peerID peer.ID, networkTo
identityMap: make(map[peer.ID]string), identityMap: make(map[peer.ID]string),
Logger: log.Logger("rendezvous"), Logger: log.Logger("rendezvous"),
} }
log.SetAllLoggers(log.LevelInfo)
return handler return handler
} }

View File

@ -34,6 +34,7 @@ func NewNode() *Node {
Logger: log.Logger("rendezvous"), Logger: log.Logger("rendezvous"),
networkTopics: mapset.NewSet(), networkTopics: mapset.NewSet(),
} }
log.SetAllLoggers(log.LevelInfo)
return node return node
} }

View File

@ -5,9 +5,12 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"io/ioutil" "io/ioutil"
"math/big" "math/big"
"strings"
"github.com/Secured-Finance/p2p-oracle-node/contracts/aggregator" "github.com/Secured-Finance/p2p-oracle-node/contracts/aggregator"
"github.com/Secured-Finance/p2p-oracle-node/contracts/oracleemitter"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -24,6 +27,13 @@ type EthereumClient struct {
Logger *log.ZapEventLogger Logger *log.ZapEventLogger
} }
type OracleEvent struct {
RequestType string
CallbackAddress common.Address
CallbackMethodID [4]byte
RequestID *big.Int
}
type Ethereum interface { type Ethereum interface {
Connect(context.Context, string) error Connect(context.Context, string) error
Balance(context.Context, string) (*big.Int, error) Balance(context.Context, string) (*big.Int, error)
@ -38,6 +48,7 @@ func NewEthereumClient() *EthereumClient {
ethereumClient := &EthereumClient{ ethereumClient := &EthereumClient{
Logger: log.Logger("rendezvous"), Logger: log.Logger("rendezvous"),
} }
log.SetAllLoggers(log.LevelInfo)
return ethereumClient return ethereumClient
} }
@ -136,12 +147,18 @@ func (c *EthereumClient) GenerateAddressFromPrivateKey(private_key string) strin
return address return address
} }
func (c *EthereumClient) SubscribeOnSmartContractEvents(ctx context.Context, address string) { func (c *EthereumClient) SubscribeOnOracleEvents(ctx context.Context, address string) {
contractAddress := common.HexToAddress(address) contractAddress := common.HexToAddress(address)
query := ethereum.FilterQuery{ query := ethereum.FilterQuery{
Addresses: []common.Address{contractAddress}, Addresses: []common.Address{contractAddress},
} }
contractAbi, err := abi.JSON(strings.NewReader(string(oracleemitter.SmartcontractsABI)))
if err != nil {
c.Logger.Fatal(err)
}
logs := make(chan types.Log) logs := make(chan types.Log)
sub, err := c.WsClient.SubscribeFilterLogs(context.Background(), query, logs) sub, err := c.WsClient.SubscribeFilterLogs(context.Background(), query, logs)
if err != nil { if err != nil {
@ -153,10 +170,15 @@ func (c *EthereumClient) SubscribeOnSmartContractEvents(ctx context.Context, add
case err := <-sub.Err(): case err := <-sub.Err():
c.Logger.Fatal(err) c.Logger.Fatal(err)
case vLog := <-logs: case vLog := <-logs:
c.Logger.Info(vLog) // pointer to event log var event OracleEvent
err := contractAbi.Unpack(&event, "NewOracleRequest", vLog.Data)
if err != nil {
c.Logger.Fatal(err)
}
c.Logger.Info(event) // pointer to event log
} }
} }
} }
func (c *EthereumClient) createKeyStore(password string) string { func (c *EthereumClient) createKeyStore(password string) string {