Skip to content

Commit

Permalink
feat: add evm price oracle (#1235)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Jan 23, 2025
1 parent 5f252c8 commit 5f6f3b5
Show file tree
Hide file tree
Showing 16 changed files with 973 additions and 292 deletions.
2 changes: 2 additions & 0 deletions cmd/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var Executables = struct {
Eibc string
CelestiaApp string
Oracle string
Solc string
}{
Roller: fmt.Sprintf("%s/roller", binsDir),
RollappEVM: fmt.Sprintf("%s/rollappd", binsDir),
Expand All @@ -45,6 +46,7 @@ var Executables = struct {
Eibc: fmt.Sprintf("%s/eibc-client", binsDir),
CelestiaApp: fmt.Sprintf("%s/celestia-appd", InternalBinsDir),
Oracle: fmt.Sprintf("%s/oracle", InternalBinsDir),
Solc: fmt.Sprintf("%s/solc", InternalBinsDir),
}

var KeysIds = struct {
Expand Down
3 changes: 1 addition & 2 deletions cmd/consts/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ var DaNetworks = map[string]DaData{
ApiUrl: DefaultCelestiaRestApiEndpoint,
ID: CelestiaTestnet,
RpcUrl: DefaultCelestiaRPC,
CurrentStateNode: "mocha-4-consensus.mesa.newmetric.xyz",
CurrentStateNode: "rpc-mocha.pops.one",
StateNodes: []string{
"mocha-4-consensus.mesa.newmetric.xyz",
"public-celestia-mocha4-consensus.numia.xyz",
"full.consensus.mocha-4.celestia-mocha.com",
"consensus-full-mocha-4.celestia-mocha.com",
Expand Down
2 changes: 1 addition & 1 deletion cmd/da-light-client/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func Cmd() *cobra.Command {

func addFlags(cmd *cobra.Command) {
cmd.Flags().
StringP(rpcEndpointFlag, "", "mocha-4-consensus.mesa.newmetric.xyz", "The DA rpc endpoint to connect to.")
StringP(rpcEndpointFlag, "", "rpc-mocha.pops.one", "The DA rpc endpoint to connect to.")
cmd.Flags().
StringP(metricsEndpointFlag, "", "", "The OTEL collector metrics endpoint to connect to.")
}
2 changes: 1 addition & 1 deletion cmd/da-light-client/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Cmd() *cobra.Command {

func addFlags(cmd *cobra.Command) {
cmd.Flags().
StringP(rpcEndpointFlag, "", "mocha-4-consensus.mesa.newmetric.xyz", "The DA rpc endpoint to connect to.")
StringP(rpcEndpointFlag, "", "rpc-mocha.pops.one", "The DA rpc endpoint to connect to.")
cmd.Flags().
StringP(metricsEndpointFlag, "", "", "The OTEL collector metrics endpoint to connect to.")
}
97 changes: 26 additions & 71 deletions cmd/oracle/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package oracle
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"os"
Expand All @@ -13,8 +12,6 @@ import (
"time"

cosmossdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/go-bip39"
"github.com/pterm/pterm"

"github.com/dymensionxyz/roller/cmd/consts"
Expand All @@ -26,7 +23,6 @@ import (
)

type OracleConfig struct {
PrivateKey string
OracleVmType string
ConfigDirPath string
CodeID string
Expand All @@ -35,7 +31,12 @@ type OracleConfig struct {
ContractAddress string
}

func NewOracle(rollerData roller.RollappConfig) *OracleConfig {
type KeyData struct {
Name string
Address string
}

func NewOracleConfig(rollerData roller.RollappConfig) *OracleConfig {
cd := filepath.Join(rollerData.Home, consts.ConfigDirName.Oracle)
ovt := rollerData.RollappVMType
return &OracleConfig{
Expand All @@ -51,67 +52,32 @@ func (o *OracleConfig) ConfigDir(rollerData roller.RollappConfig) string {
return o.ConfigDirPath
}

func (o *OracleConfig) SetKey(rollerData roller.RollappConfig) error {
addr, err := generateRaOracleKeys(rollerData.Home, rollerData)
// generateRaOracleKeys generates a new key or retrieves an existing one from the keyring
// as the second return value it returns whether the key was generated or retrieved where
// true is retrieved and false is new generated key
func generateRaOracleKeys(
home string,
rollerData roller.RollappConfig,
) ([]keys.KeyInfo, bool, error) {
oracleKeys, err := getOracleKeyConfig(rollerData.RollappVMType)
if err != nil {
return fmt.Errorf("failed to retrieve oracle keys: %v", err)
}

if len(addr) == 0 {
return fmt.Errorf("no oracle keys generated")
}

hexKey, err := GetSecp256k1PrivateKey(addr[0].Mnemonic)
if err != nil {
return err
}

o.KeyAddress = addr[0].Address
o.KeyName = addr[0].Name
o.PrivateKey = hexKey
return nil
}

func GetSecp256k1PrivateKey(mnemonic string) (string, error) {
if !bip39.IsMnemonicValid(mnemonic) {
return "", fmt.Errorf("invalid mnemonic")
}

seed := bip39.NewSeed(mnemonic, "")

hdPath := "m/44'/60'/0'/0/0"
master, ch := hd.ComputeMastersFromSeed(seed)
privKey, err := hd.DerivePrivateKeyForPath(master, ch, hdPath)
if err != nil {
return "", err
}

// Convert private key bytes to hex string
hexKey := hex.EncodeToString(privKey)
fmt.Println(hexKey)

return hexKey, nil
}

func generateRaOracleKeys(home string, rollerData roller.RollappConfig) ([]keys.KeyInfo, error) {
oracleKeys, err := getOracleKeyConfig()
if err != nil {
return nil, err
return nil, false, err
}

kc := oracleKeys[0]
ok, err := kc.IsInKeyring(home)
if err != nil {
return nil, err
return nil, false, err
}

if ok {
pterm.Info.Printfln("existing oracle key found, using it")
ki, err := kc.Info(home)
ki.Mnemonic = "not available for already existing keys"
if err != nil {
return nil, err
return nil, false, err
}
return []keys.KeyInfo{*ki}, nil
return []keys.KeyInfo{*ki}, true, nil
}

shouldImportWallet, _ := pterm.DefaultInteractiveConfirm.WithDefaultText(
Expand All @@ -123,21 +89,21 @@ func generateRaOracleKeys(home string, rollerData roller.RollappConfig) ([]keys.
if shouldImportWallet {
ki, err := kc.Create(home)
if err != nil {
return nil, err
return nil, false, err
}
addr = append(addr, *ki)
} else {
addr, err = createOraclesKeys(rollerData)
if err != nil {
return nil, err
return nil, false, err
}
}

return addr, nil
return addr, false, nil
}

func createOraclesKeys(rollerData roller.RollappConfig) ([]keys.KeyInfo, error) {
oracleKeys, err := getOracleKeyConfig()
oracleKeys, err := getOracleKeyConfig(rollerData.RollappVMType)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -218,7 +184,7 @@ func (o *OracleConfig) StoreWasmContract(rollerData roller.RollappConfig) error
isAddrFunded := balance.Amount.GTE(one)

if !isAddrFunded {
oracleKeys, err := getOracleKeyConfig()
oracleKeys, err := getOracleKeyConfig(rollerData.RollappVMType)
if err != nil {
return fmt.Errorf("failed to get oracle keys: %v", err)
}
Expand Down Expand Up @@ -326,7 +292,7 @@ func (o *OracleConfig) StoreEvmContract(rollerData roller.RollappConfig) error {
isAddrFunded := balance.Amount.GTE(one)

if !isAddrFunded {
oracleKeys, err := getOracleKeyConfig()
oracleKeys, err := getOracleKeyConfig(rollerData.RollappVMType)
if err != nil {
return fmt.Errorf("failed to get oracle keys: %v", err)
}
Expand Down Expand Up @@ -357,26 +323,15 @@ func (o *OracleConfig) StoreEvmContract(rollerData roller.RollappConfig) error {
return fmt.Errorf("failed to store contract: %v, output: %s", err, output)
}

tob := bytes.NewBufferString(output.String())
err = tx_utils.CheckTxYamlStdOut(*tob)
if err != nil {
return err
}
time.Sleep(time.Second * 2)

// Extract transaction hash
txHash, err := bash.ExtractTxHash(output.String())
if err != nil {
return fmt.Errorf("failed to extract transaction hash: %v", err)
}

pterm.Info.Printfln("transaction hash: %s", txHash)

// // Monitor transaction
// wsURL := "http://localhost:26657"
// if err := tx.MonitorTransaction(wsURL, txHash); err != nil {
// return fmt.Errorf("failed to monitor transaction: %v", err)
// }

return nil
}

Expand Down
16 changes: 13 additions & 3 deletions cmd/oracle/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package oracle

import (
"context"
"crypto/ecdsa"
)

// ContractDeployer defines the interface for deploying contracts on different chains
Expand All @@ -13,9 +12,20 @@ type ContractDeployer interface {
// DeployContract deploys the contract on chain and returns its address
DeployContract(
ctx context.Context,
privateKey *ecdsa.PrivateKey,
contractCode []byte,
) (string, error)

// Config returns the OracleConfig
Config() *OracleConfig

// PrivateKey returns the private key used to deploy the contract
PrivateKey() string

// IsContractDeployed returns whether the contract has been deployed to the chain
IsContractDeployed() (string, bool) // address, bool

// ContractPath returns the path to the contract file on the local machine
ContractPath() string

// ClientConfigPath returns the filepath to the client config file
ClientConfigPath() string
}
Loading

0 comments on commit 5f6f3b5

Please sign in to comment.