Skip to content

Commit

Permalink
fix pair selection string in oracle feeder
Browse files Browse the repository at this point in the history
  • Loading branch information
kaythxbye committed Sep 5, 2023
1 parent 03715c8 commit 9188bbe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/diadata-org/diadata/blockchain/diaOracleV2MultiupdateService
go 1.14

require (
github.com/diadata-org/diadata v1.4.332
github.com/diadata-org/diadata v1.4.339
github.com/ethereum/go-ethereum v1.10.10
github.com/machinebox/graphql v0.2.2
github.com/matryer/is v1.4.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions cmd/blockchain/ethereum/diaOracleV2MultiupdateService/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ github.com/diadata-org/diadata v1.4.331 h1:ZezFqi8woLzuX46fSOYdgFVIqmC5SZ8zW2RZ0
github.com/diadata-org/diadata v1.4.331/go.mod h1:qrtMmpXAViwIlMzMFwYlZC+Rr/d8nUBpqtupg98hncw=
github.com/diadata-org/diadata v1.4.332 h1:vJeXsuGf1G4yCQyqnMfMKhAJboH5i6k/lNFOqyP9Auw=
github.com/diadata-org/diadata v1.4.332/go.mod h1:qrtMmpXAViwIlMzMFwYlZC+Rr/d8nUBpqtupg98hncw=
github.com/diadata-org/diadata v1.4.339 h1:Y57dRAez6k3rTJLNnV+yY5hog65M4QdepMc1DUwth5M=
github.com/diadata-org/diadata v1.4.339/go.mod h1:qrtMmpXAViwIlMzMFwYlZC+Rr/d8nUBpqtupg98hncw=
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
Expand Down
45 changes: 32 additions & 13 deletions cmd/blockchain/ethereum/diaOracleV2MultiupdateService/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/diadata-org/diadata/pkg/utils"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
gql "github.com/machinebox/graphql"
)
Expand All @@ -44,7 +45,7 @@ type GqlParameters struct {
func main() {
key := utils.Getenv("PRIVATE_KEY", "")
key_password := utils.Getenv("PRIVATE_KEY_PASSWORD", "")
deployedContract := utils.Getenv("DEPLOYED_CONTRACT", "0x0000000000000000000000000000000000000000")
deployedContract := utils.Getenv("DEPLOYED_CONTRACT", "")
blockchainNode := utils.Getenv("BLOCKCHAIN_NODE", "")
frequencySeconds, err := strconv.Atoi(utils.Getenv("FREQUENCY_SECONDS", "120"))
if err != nil {
Expand Down Expand Up @@ -115,7 +116,7 @@ func main() {
publishedPrices := make(map[string]float64)

/*
* Setup connection to contract
* Setup connection to contract, deploy if necessary
*/

conn, err := ethclient.Dial(blockchainNode)
Expand All @@ -129,7 +130,7 @@ func main() {
}

var contract *diaOracleV2MultiupdateService.DiaOracleV2MultiupdateService
err = bindContract(deployedContract, conn, auth, &contract)
err = deployOrBindContract(deployedContract, conn, auth, &contract)
if err != nil {
log.Fatalf("Failed to Deploy or Bind contract: %v", err)
}
Expand Down Expand Up @@ -213,9 +214,10 @@ func oracleUpdateExecutor(
priceCollector := make(map[string]float64)
for _, asset := range assets {
newPrice := newPrices[asset.symbol]
fmt.Println("new price", newPrice)
oldPrice := publishedPrices[asset.symbol]

if newPrice > 0.0 && (newPrice > (oldPrice * (1 + float64(deviationPermille)/1000))) || (newPrice < (oldPrice * (1 - float64(deviationPermille)/1000))) {
if newPrice > 1e-8 && ((newPrice > (oldPrice * (1 + float64(deviationPermille)/1000))) || (newPrice < (oldPrice * (1 - float64(deviationPermille)/1000)))) {
log.Printf("Entering deviation based update zone for old price %.2f of asset %s. New price: %.2f", oldPrice, asset.symbol, newPrice)
updateCollector[asset.symbol] = newPrice
priceCollector[asset.symbol] = newPrice
Expand Down Expand Up @@ -271,15 +273,29 @@ func retrieveAssetPrice(asset Asset, useGql bool, gqlWindowSize int, gqlMethodol
return price, nil
}

func bindContract(
func deployOrBindContract(
deployedContract string,
conn *ethclient.Client,
auth *bind.TransactOpts,
contract **diaOracleV2MultiupdateService.DiaOracleV2MultiupdateService) error {
var err error
*contract, err = diaOracleV2MultiupdateService.NewDiaOracleV2MultiupdateService(common.HexToAddress(deployedContract), conn)
if err != nil {
return err
if deployedContract != "" {
*contract, err = diaOracleV2MultiupdateService.NewDiaOracleV2MultiupdateService(common.HexToAddress(deployedContract), conn)
if err != nil {
return err
}
} else {
// deploy contract
var addr common.Address
var tx *types.Transaction
addr, tx, *contract, err = diaOracleV2MultiupdateService.DeployDiaOracleV2MultiupdateService(auth, conn)
if err != nil {
log.Fatalf("could not deploy contract: %v", err)
return err
}
log.Printf("Contract pending deploy: 0x%x\n", addr)
log.Printf("Transaction waiting to be mined: 0x%x\n\n", tx.Hash())
time.Sleep(180000 * time.Millisecond)
}
return nil
}
Expand Down Expand Up @@ -380,12 +396,15 @@ func getGraphqlAssetQuotationFromDia(blockchain, address string, windowSize int,
exchangePairsString = "Exchangepairs:[\n"
for _, exchangePair := range selectedFeed.Exchangepairs {
exchangePairsString += `{
Exchange: "` + exchangePair.Exchange + `",
Pairs: [`
for _, pair := range exchangePair.Pairs {
exchangePairsString += `"` + pair + `",`
Exchange: "` + exchangePair.Exchange + `",`
if len(exchangePair.Pairs) > 0 {
exchangePairsString += `Pairs: [`
for _, pair := range exchangePair.Pairs {
exchangePairsString += `"` + pair + `",`
}
exchangePairsString += `]`
}
exchangePairsString += `]},`
exchangePairsString += `},`
}
exchangePairsString += "]"
} else {
Expand Down

0 comments on commit 9188bbe

Please sign in to comment.