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"`
ProtocolID string `toml:"protocol_id"`
SessionKey string `toml:"session_key"`
PrivateKey string `toml:"private_key"`
}
// viperEnvVariable loads config parameters from .env file
@ -58,6 +59,7 @@ func NewConfig() *Config {
Rendezvous := viperEnvString("RENDEZVOUS", "filecoin-p2p-oracle")
ProtocolID := viperEnvString("PROTOCOL_ID", "p2p-oracle")
SessionKey := viperEnvString("SESSION_KEY", "go")
PrivateKey := viperEnvString("PRIVATE_KEY", "")
return &Config{
ListenPort: ListenPort,
@ -67,5 +69,6 @@ func NewConfig() *Config {
Rendezvous: Rendezvous,
ProtocolID: ProtocolID,
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),
Logger: log.Logger("rendezvous"),
}
log.SetAllLoggers(log.LevelInfo)
return handler
}

View File

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

View File

@ -5,9 +5,12 @@ import (
"crypto/ecdsa"
"io/ioutil"
"math/big"
"strings"
"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/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
@ -24,6 +27,13 @@ type EthereumClient struct {
Logger *log.ZapEventLogger
}
type OracleEvent struct {
RequestType string
CallbackAddress common.Address
CallbackMethodID [4]byte
RequestID *big.Int
}
type Ethereum interface {
Connect(context.Context, string) error
Balance(context.Context, string) (*big.Int, error)
@ -38,6 +48,7 @@ func NewEthereumClient() *EthereumClient {
ethereumClient := &EthereumClient{
Logger: log.Logger("rendezvous"),
}
log.SetAllLoggers(log.LevelInfo)
return ethereumClient
}
@ -136,12 +147,18 @@ func (c *EthereumClient) GenerateAddressFromPrivateKey(private_key string) strin
return address
}
func (c *EthereumClient) SubscribeOnSmartContractEvents(ctx context.Context, address string) {
func (c *EthereumClient) SubscribeOnOracleEvents(ctx context.Context, address string) {
contractAddress := common.HexToAddress(address)
query := ethereum.FilterQuery{
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)
sub, err := c.WsClient.SubscribeFilterLogs(context.Background(), query, logs)
if err != nil {
@ -153,10 +170,15 @@ func (c *EthereumClient) SubscribeOnSmartContractEvents(ctx context.Context, add
case err := <-sub.Err():
c.Logger.Fatal(err)
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 {