Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into 1620-implement-uptime-new-design2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanisorn Thongprapaisaeng authored Jun 9, 2020
2 parents ba67183 + 195e060 commit 4cf0549
Show file tree
Hide file tree
Showing 15 changed files with 435 additions and 262 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

### Chain

- (impv/chore) [\#1893](https://github.com/bandprotocol/bandchain/pull/1893) Cleanup and Add genesis command to add data sources / oracle scripts.
- (feat) [\#1910](https://github.com/bandprotocol/bandchain/pull/1910) Implement request-search REST and CLI endpoint.
- (impv) [\#1903](https://github.com/bandprotocol/bandchain/pull/1903) Store result in store instead of result hash.
- (impv) [\#1901](https://github.com/bandprotocol/bandchain/pull/1901) Added `moniker` field to `delegations_view` table.
- (feat) [\#1879](https://github.com/bandprotocol/bandchain/pull/1879) Keep `Request` and `Report` around. We avoid premature optimization at the moment.
Expand Down
222 changes: 0 additions & 222 deletions chain/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package app

import (
"encoding/json"
"io/ioutil"
"path/filepath"
"time"

codecstd "github.com/cosmos/cosmos-sdk/codec/std"
Expand All @@ -22,12 +20,8 @@ import (
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/upgrade"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"

"github.com/bandprotocol/bandchain/chain/pkg/filecache"
"github.com/bandprotocol/bandchain/chain/x/oracle"
otypes "github.com/bandprotocol/bandchain/chain/x/oracle/types"
)

// GenesisState defines a type alias for the Band genesis application state.
Expand Down Expand Up @@ -74,219 +68,3 @@ func NewDefaultGenesisState() GenesisState {
oracle.ModuleName: oracle.AppModuleBasic{}.DefaultGenesis(cdc),
}
}

func GetDefaultDataSourcesAndOracleScripts(owner sdk.AccAddress) json.RawMessage {
state := oracle.DefaultGenesisState()
dataSources := []struct {
name string
description string
path string
}{
{
"Coingecko script",
"The script that queries crypto price from https://coingecko.com",
"./datasources/coingecko_price.py",
},
{
"Crypto compare script",
"The script that queries crypto price from https://cryptocompare.com",
"./datasources/crypto_compare_price.sh",
},
{
"Binance price",
"The script that queries crypto price from https://www.binance.com/en",
"./datasources/binance_price.sh",
},
{
"Open weather map",
"The script that queries weather information from https://api.openweathermap.org",
"./datasources/open_weather_map.sh",
},
{
"Gold price",
"The script that queries current gold price",
"./datasources/gold_price.sh",
},
{
"Alphavantage",
"The script that queries stock price from Alphavantage",
"./datasources/alphavantage.sh",
},
{
"Bitcoin block count",
"The script that queries latest block height of Bitcoin",
"./datasources/bitcoin_count.sh",
},
{
"Bitcoin block hash",
"The script that queries block hash of Bitcoin",
"./datasources/bitcoin_hash.sh",
},
{
"Coingecko volume script",
"The script that queries crypto volume from Coingecko",
"./datasources/coingecko_volume.sh",
},
{
"Crypto compare volume script",
"The script that queries crypto volume from Crypto compare",
"./datasources/crypto_compare_volume.sh",
},
{
"ETH gas station",
"The script that queries current Ethereum gas price https://ethgasstation.info",
"./datasources/ethgasstation.sh",
},
{
"Open sky network",
"The script that queries flight information from https://opensky-network.org",
"./datasources/open_sky_network.sh",
},
{
"Quantum random numbers",
"The script that queries array of random number from https://qrng.anu.edu.au",
"./datasources/qrng_anu.sh",
},
{
"Yahoo finance",
"The script that queries stock price from https://finance.yahoo.com",
"./datasources/yahoo_finance.py",
},
}

// TODO: Find a better way to specify path to data sources
state.DataSources = make([]otypes.DataSource, len(dataSources))
for i, dataSource := range dataSources {
script, err := ioutil.ReadFile(dataSource.path)
if err != nil {
panic(err)
}
f := filecache.New(filepath.Join(viper.GetString(cli.HomeFlag), "files"))
filename := f.AddFile(script)
state.DataSources[i] = otypes.NewDataSource(
owner,
dataSource.name,
dataSource.description,
filename,
)
}

// TODO: Find a better way to specify path to oracle scripts
oracleScripts := []struct {
name string
description string
path string
schema string
sourceCodeURL string
}{
{
"Crypto price script",
"Oracle script for getting the current an average cryptocurrency price from various sources.",
"./pkg/owasm/res/crypto_price.wasm",
`{symbol:string,multiplier:u64}/{px:u64}`,
"https://ipfs.io/ipfs/QmUbdfoRR9ge6P39EoqDjBhQoDeaT6gJu76Ce9kKsz938N",
},
{
"Gold price script",
"Oracle script for getting the current average gold price in ATOMs",
"./pkg/owasm/res/gold_price.wasm",
`{multiplier:u64}/{px:u64}`,
"https://ipfs.io/ipfs/Qmbcdr3UZXMrJeoRtHzTtHHepnzjyX1gWNhewWe6BXgmPm",
},
{
"Alpha Vantage stock price script",
"Oracle script for getting the current price of a stock from Alpha Vantage",
"./pkg/owasm/res/alphavantage.wasm",
`{symbol:string,api_key:string,multiplier:u64}/{px:u64}`,
"https://ipfs.io/ipfs/QmPsSmJ9gEdBoeQqwtk6bJykyFtqSpztCeGb9J1VFW65av",
},
{
"Bitcoin block count",
"Oracle script for getting the latest Bitcoin block height",
"./pkg/owasm/res/bitcoin_block_count.wasm",
`{_unused:u8}/{block_count:u64}`,
"https://ipfs.io/ipfs/QmUkpTCvdKMEFxwgeTpjP9hszZ11e5ioXZAS7XLpQLbV2k",
},
{
"Bitcoin block hash",
"Oracle script for getting the Bitcoin block hash at the given block height",
"./pkg/owasm/res/bitcoin_block_hash.wasm",
`{block_height:u64}/{block_hash:string}`,
"https://ipfs.io/ipfs/QmXu5NyUrtbcdPxut4WhVsRT4KjsPjy2NwJzEts7rjuEDf",
},
{
"CoinGecko crypto volume",
"Oracle script for getting a cryptocurrency's average trading volume for the past day from Coingecko",
"./pkg/owasm/res/coingecko_volume.wasm",
`{symbol:string,multiplier:u64}/{volume:u64}`,
"https://ipfs.io/ipfs/QmVuYP5cSujNSv33ZNMiFbcSMoRtBa7WMzG2q55j21Vhxj",
},
{
"CryptoCompare crypto volume",
"Oracle script for getting a cryptocurrency's average trading volume for the past day from CryptoCompare",
"./pkg/owasm/res/crypto_compare_volume.wasm",
`{symbol:string,multiplier:u64}/{volume:u64}`,
"https://ipfs.io/ipfs/Qmf2e5VF3uscGzBMwfQRZbaYLWxAZPWAgwv3m3iSv6BoGE",
},
{
"Ethereum gas price",
"Oracle script for getting the current Ethereum gas price from ETH gas station",
"./pkg/owasm/res/eth_gas_station.wasm",
`{gas_option:string}/{gweix10:u64}`,
"https://ipfs.io/ipfs/QmP1i61XdPnfKSewh7vyh3xLgjxT42Gqpiv7CYLFK6V3Mg",
},
{
"Open sky network",
"Oracle script for checking whether a given flight number exists during the given time period",
"./pkg/owasm/res/open_sky_network.wasm",
`{flight_op:string,icao24:string,begin:string,end:string}/{flight_existence:u8}`,
"https://ipfs.io/ipfs/QmST4us1xAXmfXZFqBRZqjsDpNhTMY1CRsgjNccwmB4FTX",
},
{
"Open weather map",
"Oracle script for getting the current weather data of a location",
"./pkg/owasm/res/open_weather_map.wasm",
`{country:string,main_field:string,sub_field:string,multiplier:u64}/{value:u64}`,
"https://ipfs.io/ipfs/QmNWvYfqZztrMNKjKyKLTATvnVPVUMPCdLJFkV5HfHBQoo",
},
{
"Quantum random number generator",
"Oracle script for getting a big random number from quantum computer",
"./pkg/owasm/res/qrng.wasm",
`{size:u64}/{random_bytes:string}`,
"https://ipfs.io/ipfs/QmZ62dxgAmCtDnt5XcAs2zP4UjGzozDzdKiHoR1Wo9MVeV",
},
{
"Yahoo stock price",
"Oracle script for getting the current price of a stock from Yahoo Finance",
"./pkg/owasm/res/yahoo_price.wasm",
`{symbol:string,multiplier:u64}/{px:u64}`,
"https://ipfs.io/ipfs/QmfEUKFoX9PY3LHnT7Deixwb8qRrgWvdf5v8MzTinTYXLu",
},
{
"Fair price from 3 sources",
"Oracle script that query prices from many markets and then aggregate them together",
"./pkg/owasm/res/fair_crypto_market_price.wasm",
`{base_symbol:string,quote_symbol:string,aggregation_method:string,multiplier:u64}/{px:u64}`,
"https://ipfs.io/ipfs/QmbnRei1WG8gdstsVuU7Qqq4PwqED9LuHFvjDgS5asShoM",
},
}
state.OracleScripts = make([]otypes.OracleScript, len(oracleScripts))
for i, oracleScript := range oracleScripts {
code, err := ioutil.ReadFile(oracleScript.path)
if err != nil {
panic(err)
}
f := filecache.New(filepath.Join(viper.GetString(cli.HomeFlag), "files"))
filename := f.AddFile(code)
state.OracleScripts[i] = otypes.NewOracleScript(
owner,
oracleScript.name,
oracleScript.description,
filename,
oracleScript.schema,
oracleScript.sourceCodeURL,
)
}
return oracle.ModuleCdc.MustMarshalJSON(state)
}
65 changes: 65 additions & 0 deletions chain/cmd/bandd/gends.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"fmt"
"io/ioutil"
"path/filepath"

"github.com/bandprotocol/bandchain/chain/pkg/filecache"
codecstd "github.com/cosmos/cosmos-sdk/codec/std"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/libs/cli"

"github.com/bandprotocol/bandchain/chain/x/oracle"
otypes "github.com/bandprotocol/bandchain/chain/x/oracle/types"
)

// AddGenesisDataSourceCmd returns add-data-source cobra Command.
func AddGenesisDataSourceCmd(
ctx *server.Context, depCdc *amino.Codec, cdc *codecstd.Codec, defaultNodeHome string,
) *cobra.Command {

cmd := &cobra.Command{
Use: "add-data-source [name] [description] [owner] [filepath]",
Short: "Add a data source to genesis.json",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
config := ctx.Config
config.SetRoot(viper.GetString(cli.HomeFlag))
f := filecache.New(filepath.Join(viper.GetString(cli.HomeFlag), "files"))
data, err := ioutil.ReadFile(args[3])
if err != nil {
return err
}
filename := f.AddFile(data)
owner, err := sdk.AccAddressFromBech32(args[2])
if err != nil {
return err
}

genFile := config.GenesisFile()
appState, genDoc, err := genutil.GenesisStateFromGenFile(depCdc, genFile)
if err != nil {
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
}

oracleGenState := oracle.GetGenesisStateFromAppState(depCdc, appState)
oracleGenState.DataSources = append(oracleGenState.DataSources, otypes.NewDataSource(
owner, args[0], args[1], filename,
))

appState[oracle.ModuleName] = cdc.MustMarshalJSON(oracleGenState)
appStateJSON := cdc.MustMarshalJSON(appState)
genDoc.AppState = appStateJSON
return genutil.ExportGenesisFile(genDoc, genFile)
},
}

cmd.Flags().String(cli.HomeFlag, defaultNodeHome, "node's home directory")
return cmd
}
65 changes: 65 additions & 0 deletions chain/cmd/bandd/genos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"fmt"
"io/ioutil"
"path/filepath"

"github.com/bandprotocol/bandchain/chain/pkg/filecache"
codecstd "github.com/cosmos/cosmos-sdk/codec/std"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/libs/cli"

"github.com/bandprotocol/bandchain/chain/x/oracle"
otypes "github.com/bandprotocol/bandchain/chain/x/oracle/types"
)

// AddGenesisOracleScriptCmd returns add-oracle-script cobra Command.
func AddGenesisOracleScriptCmd(
ctx *server.Context, depCdc *amino.Codec, cdc *codecstd.Codec, defaultNodeHome string,
) *cobra.Command {

cmd := &cobra.Command{
Use: "add-oracle-script [name] [description] [schema] [url] [owner] [filepath]",
Short: "Add a data source to genesis.json",
Args: cobra.ExactArgs(6),
RunE: func(cmd *cobra.Command, args []string) error {
config := ctx.Config
config.SetRoot(viper.GetString(cli.HomeFlag))
f := filecache.New(filepath.Join(viper.GetString(cli.HomeFlag), "files"))
data, err := ioutil.ReadFile(args[5])
if err != nil {
return err
}
filename := f.AddFile(data)
owner, err := sdk.AccAddressFromBech32(args[4])
if err != nil {
return err
}

genFile := config.GenesisFile()
appState, genDoc, err := genutil.GenesisStateFromGenFile(depCdc, genFile)
if err != nil {
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
}

oracleGenState := oracle.GetGenesisStateFromAppState(depCdc, appState)
oracleGenState.OracleScripts = append(oracleGenState.OracleScripts, otypes.NewOracleScript(
owner, args[0], args[1], filename, args[2], args[3],
))

appState[oracle.ModuleName] = cdc.MustMarshalJSON(oracleGenState)
appStateJSON := cdc.MustMarshalJSON(appState)
genDoc.AppState = appStateJSON
return genutil.ExportGenesisFile(genDoc, genFile)
},
}

cmd.Flags().String(cli.HomeFlag, defaultNodeHome, "node's home directory")
return cmd
}
Loading

0 comments on commit 4cf0549

Please sign in to comment.