diff --git a/Makefile b/Makefile index 36eda37..f6c0c5f 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,6 @@ CONF_FILE = ./config/oracle_config.yml E2E_TEST_DIR = ./e2e_test E2E_TEST_PLUGIN_DIR = $(E2E_TEST_DIR)/plugins E2E_TEST_TEMPLATE_PLUGIN_DIR = $(E2E_TEST_PLUGIN_DIR)/template_plugins -E2E_TEST_PRD_PLUGIN_DIR = $(E2E_TEST_PLUGIN_DIR)/production_plugins E2E_TEST_SML_PLUGIN_DIR = $(E2E_TEST_PLUGIN_DIR)/simulator_plugins E2E_TEST_OUTLIER_PLUGIN_DIR = $(E2E_TEST_PLUGIN_DIR)/outlier_plugins E2E_TEST_MIX_PLUGIN_DIR = $(E2E_TEST_PLUGIN_DIR)/mix_plugins @@ -44,7 +43,6 @@ mkdir: mkdir -p $(SIMULATOR_BIN_DIR) mkdir -p $(E2E_TEST_PLUGIN_DIR) mkdir -p $(E2E_TEST_TEMPLATE_PLUGIN_DIR) - mkdir -p $(E2E_TEST_PRD_PLUGIN_DIR) mkdir -p $(E2E_TEST_SML_PLUGIN_DIR) mkdir -p $(E2E_TEST_MIX_PLUGIN_DIR) mkdir -p $(E2E_TEST_FOREX_PLUGIN_DIR) @@ -74,10 +72,6 @@ e2e-test-stuffs: go build -o $(E2E_TEST_DIR)/simulator $(SIMULATOR_SRC_DIR)/main.go chmod +x $(E2E_TEST_DIR)/simulator - # build binance plugin for e2e test. - go build -o $(E2E_TEST_PRD_PLUGIN_DIR)/binance $(PLUGIN_SRC_DIR)/binance/binance.go - chmod +x $(E2E_TEST_PRD_PLUGIN_DIR)/binance - # build amm plugin for e2e test. go build -o $(E2E_TEST_CRYPTO_PLUGIN_DIR)/crypto_uniswap $(PLUGIN_SRC_DIR)/crypto_uniswap/uniswap_usdcx/crypto_uniswap_usdcx.go chmod +x $(E2E_TEST_CRYPTO_PLUGIN_DIR)/* @@ -120,13 +114,6 @@ amm-plugins: go build -o $(PLUGIN_DIR)/crypto_uniswap $(PLUGIN_SRC_DIR)/crypto_uniswap/uniswap_usdcx/crypto_uniswap_usdcx.go chmod +x $(PLUGIN_DIR)/* -# legacy piccadilly cax plugin, it sources order books from a CEX service built in python. -piccadilly-cax-plugin: - go build -o $(PLUGIN_DIR)/pcgc_cax $(PLUGIN_SRC_DIR)/pcgc_cax/ - chmod +x $(PLUGIN_DIR)/pcgc_cax - # cp autonity round4 game PCGC CAX plugins for e2e testing - cp $(PLUGIN_DIR)/pcgc_cax $(E2E_TEST_CRYPTO_PLUGIN_DIR)/pcgc_cax - # build ATN-USDC, NTN-USDC, NTN-ATN data point simulator binary crypto_source_simulator: go build -o $(SIMULATOR_BIN_DIR)/simulator $(SIMULATOR_SRC_DIR)/main.go diff --git a/e2e_test/e2e_test.go b/e2e_test/e2e_test.go index 7411bee..3904090 100644 --- a/e2e_test/e2e_test.go +++ b/e2e_test/e2e_test.go @@ -201,30 +201,6 @@ func TestAddCommitteeMember(t *testing.T) { testNewValidatorJoinToCommittee(t, network, client, o, aut, endRound, pricePrecision) } -func TestHappyCaseWithBinanceDataService(t *testing.T) { - var netConf = &NetworkConfig{ - EnableL1Logs: false, - Symbols: []string{"BTC-USD", "BTC-USDC", "BTC-USDT", "BTC-USD4"}, - VotePeriod: defaultVotePeriod, - PluginDIRs: []string{binancePlugDir, binancePlugDir, binancePlugDir, binancePlugDir}, - } - network, err := createNetwork(netConf, numberOfValidators) - require.NoError(t, err) - defer network.Stop() - - client, err := ethclient.Dial(fmt.Sprintf("ws://%s:%d", network.L1Nodes[0].Host, network.L1Nodes[0].WSPort)) - require.NoError(t, err) - defer client.Close() - - // bind client with oracle contract address - o, err := contract.NewOracle(types.OracleContractAddress, client) - require.NoError(t, err) - - // first test happy case. - endRound := uint64(10) - testBinanceDataHappyCase(t, o, endRound) -} - func TestFeeRefund(t *testing.T) { var netConf = &NetworkConfig{ EnableL1Logs: false, @@ -821,36 +797,6 @@ func testRestartL1Node(t *testing.T, net *Network, index int, o *contract.Oracle } } -func testBinanceDataHappyCase(t *testing.T, o *contract.Oracle, beforeRound uint64) { - for { - time.Sleep(1 * time.Minute) - round, err := o.GetRound(nil) - require.NoError(t, err) - - // continue to wait until end round. - if round.Uint64() < beforeRound { - continue - } - - symbols, err := o.GetSymbols(nil) - require.NoError(t, err) - - // as current round is not finalized yet, thus the round data of it haven't being aggregate, - // thus we will query the last round's data for the verification. - lastRound := new(big.Int).SetUint64(round.Uint64() - 1) - - // get last round data for each symbol. - for _, s := range symbols { - d, err := o.GetRoundData(nil, lastRound, s) - require.NoError(t, err) - require.NotEqual(t, uint64(0), d.Price.Uint64()) - require.Equal(t, true, d.Success) - } - - break - } -} - func testAddNewSymbols(t *testing.T, network *Network, client *ethclient.Client, o *contract.Oracle, beforeRound uint64, pricePrecision decimal.Decimal) { chainID, err := client.ChainID(context.Background()) diff --git a/e2e_test/helpers.go b/e2e_test/helpers.go index 4518a07..a21496d 100644 --- a/e2e_test/helpers.go +++ b/e2e_test/helpers.go @@ -36,7 +36,6 @@ var ( outlierPlugDir = "./plugins/outlier_plugins" forexPlugDir = "./plugins/forex_plugins" cryptoPlugDir = "./plugins/crypto_plugins" - binancePlugDir = "./plugins/production_plugins" simulatorPlugDir = "./plugins/simulator_plugins" mixPluginDir = "./plugins/mix_plugins" defaultGenesis = "./autonity_l1_config/genesis_template.json" diff --git a/plugins/README.md b/plugins/README.md index becfcb4..4f42d7f 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -419,7 +419,7 @@ func (tc *TemplateClient) FetchPrice(symbols []string) (common.Prices, error) { return nil, err } - tc.logger.Info("binance", "data", prices) + tc.logger.Info("template_plugin", "data", prices) */ // in this template, we just return fix values. diff --git a/plugins/binance/binance.go b/plugins/binance/binance.go deleted file mode 100644 index d01f45e..0000000 --- a/plugins/binance/binance.go +++ /dev/null @@ -1,127 +0,0 @@ -package main - -import ( - "autonity-oracle/config" - "autonity-oracle/plugins/common" - "autonity-oracle/types" - "encoding/json" - "github.com/hashicorp/go-hclog" - "io" - "net/url" - "os" - "time" -) - -const ( - version = "v0.0.2" - apiPath = "api/v3/ticker/price" - symbol = "symbols" -) - -var defaultConfig = config.PluginConfig{ - Key: "", - Scheme: "https", - Endpoint: "api.binance.us", - Timeout: 10, //10s - DataUpdateInterval: 30, //10s -} - -type BIClient struct { - conf *config.PluginConfig - client *common.Client - logger hclog.Logger -} - -func NewBIClient(conf *config.PluginConfig) *BIClient { - client := common.NewClient(conf.Key, time.Second*time.Duration(conf.Timeout), conf.Endpoint) - logger := hclog.New(&hclog.LoggerOptions{ - Name: conf.Name, - Level: hclog.Info, - Output: os.Stdout, - }) - - return &BIClient{conf: conf, client: client, logger: logger} -} - -func (bi *BIClient) KeyRequired() bool { - return false -} - -func (bi *BIClient) FetchPrice(symbols []string) (common.Prices, error) { - var prices common.Prices - u, err := bi.buildURL(symbols) - if err != nil { - return nil, err - } - - res, err := bi.client.Conn.Request(bi.conf.Scheme, u) - if err != nil { - bi.logger.Error("https get", "error", err.Error()) - return nil, err - } - defer res.Body.Close() - - if err = common.CheckHTTPStatusCode(res.StatusCode); err != nil { - return nil, err - } - - body, err := io.ReadAll(res.Body) - if err != nil { - bi.logger.Error("io read", "error", err.Error()) - return nil, err - } - - err = json.Unmarshal(body, &prices) - if err != nil { - return nil, err - } - - for i := range prices { - prices[i].Volume = types.DefaultVolume.String() - } - - return prices, nil -} - -func (bi *BIClient) AvailableSymbols() ([]string, error) { - var res []string - prices, err := bi.FetchPrice(nil) - if err != nil { - return nil, err - } - - for _, p := range prices { - res = append(res, p.Symbol) - } - return res, nil -} - -func (bi *BIClient) Close() { - bi.client.Conn.Close() -} - -func (bi *BIClient) buildURL(symbols []string) (*url.URL, error) { - endpoint := &url.URL{} - endpoint.Path = apiPath - - if len(symbols) != 0 { - parameters, err := json.Marshal(symbols) - if err != nil { - return nil, err - } - - query := endpoint.Query() - query.Set(symbol, string(parameters)) - endpoint.RawQuery = query.Encode() - } - - return endpoint, nil -} - -func main() { - conf := common.ResolveConf(os.Args[0], &defaultConfig) - adapter := common.NewPlugin(conf, NewBIClient(conf), version, types.SrcCEX, nil) - defer adapter.Close() - - common.PluginServe(adapter) -} diff --git a/plugins/binance/binance_test.go b/plugins/binance/binance_test.go deleted file mode 100644 index 6959d35..0000000 --- a/plugins/binance/binance_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import ( - "github.com/stretchr/testify/require" - "testing" -) - -func TestNewBIClient(t *testing.T) { - client := NewBIClient(&defaultConfig) - defer client.Close() - prices, err := client.FetchPrice([]string{"BTCUSD", "ETHUSD"}) - require.NoError(t, err) - require.Equal(t, 2, len(prices)) -} - -func TestBIClient_AvailableSymbols(t *testing.T) { - client := NewBIClient(&defaultConfig) - defer client.Close() - symbols, err := client.AvailableSymbols() - require.NoError(t, err) - - require.Contains(t, symbols, "BTCUSD") -} diff --git a/plugins/pcgc_cax/pcgc_cax.go b/plugins/pcgc_cax/pcgc_cax.go deleted file mode 100644 index 7391a03..0000000 --- a/plugins/pcgc_cax/pcgc_cax.go +++ /dev/null @@ -1,193 +0,0 @@ -package main - -import ( - "autonity-oracle/config" - "autonity-oracle/plugins/common" - "autonity-oracle/types" - "encoding/json" - "github.com/hashicorp/go-hclog" - "github.com/shopspring/decimal" - "io" - "net/url" - "os" - "strings" - "time" -) - -// This plugin is only used for autonity round 4 game purpose, the data of NTN-USDC & ATN-USDC come from a simulated -// exchange service build by Clearmatics. -const ( - version = "v0.2.0" - quote = "quote" - NTNATN = "NTN-ATN" - NTNUSDC = "NTN-USDC" - ATNUSDC = "ATN-USDC" -) - -// take piccadilly setup as the default setting -var routers = "api/orderbooks" -var defaultEndpoint = "cax.piccadilly.autonity.org" - -var defaultConfig = config.PluginConfig{ - Name: "pcgc_cax", - Key: "", - Scheme: "https", - Endpoint: defaultEndpoint, - Timeout: 10, //10s - DataUpdateInterval: 30, //30s, -} - -type CAXQuote struct { - Timestamp string `json:"timestamp"` - BidPrice string `json:"bid_price"` - BidAmount string `json:"bid_amount"` - AskPrice string `json:"ask_price"` - AskAmount string `json:"ask_amount"` -} - -type CAXClient struct { - conf *config.PluginConfig - client *common.Client - logger hclog.Logger -} - -func NewCAXClient(conf *config.PluginConfig) *CAXClient { - client := common.NewClient(conf.Key, time.Second*time.Duration(conf.Timeout), conf.Endpoint) - logger := hclog.New(&hclog.LoggerOptions{ - Name: "AutonityR4CAX", - Level: hclog.Info, - Output: os.Stdout, - }) - - return &CAXClient{ - conf: conf, - client: client, - logger: logger, - } -} - -func (cc *CAXClient) KeyRequired() bool { - return false -} - -func (cc *CAXClient) FetchPrice(symbols []string) (common.Prices, error) { - var prices common.Prices - priceMap := make(map[string]common.Price) - - for _, s := range symbols { - // all CAXs of Autonity test networks do not provice NTN-ATN price, thus the price of NTN-ATN is derived from - // the price of NTN-USDC and ATN-USDC for the time being. - if s == NTNATN { - continue - } - p, err := cc.fetchPrice(s) - if err != nil { - cc.logger.Error("query price", "error", err.Error()) - continue - } - priceMap[s] = p - prices = append(prices, p) - } - - if len(prices) == 0 { - return nil, common.ErrDataNotAvailable - } - - // for autonity round4 game, the price of "NTN-ATN" is derived from the price of "NTN-USDC" and "ATN-USDC" - if _, ok := priceMap[NTNATN]; !ok && len(priceMap) == 2 { - pNTN, ok := priceMap[NTNUSDC] - if !ok { - cc.logger.Error("missing NTN-USDC data to compute derived price: NTN-ATN") - return prices, nil - } - - pATN, ok := priceMap[ATNUSDC] - if !ok { - cc.logger.Error("missing ATN-USDC data to compute derived price: NTN-ATN") - return prices, nil - } - - // since only 3 symbols are supported, thus we assume the collected two symbols are NTN-USDC and ATN-USDC. - pNTNATN, err := common.ComputeDerivedPrice(pNTN.Price, pATN.Price) - if err != nil { - cc.logger.Error("compute derived price NTN-ATN", "error", err.Error()) - return prices, nil - } - prices = append(prices, pNTNATN) - } - - return prices, nil -} - -func (cc *CAXClient) fetchPrice(symbol string) (common.Price, error) { - var price common.Price - u := cc.buildURL(symbol) - res, err := cc.client.Conn.Request(cc.conf.Scheme, u) - if err != nil { - cc.logger.Error("https request", "error", err.Error()) - return price, err - } - defer res.Body.Close() - - if err = common.CheckHTTPStatusCode(res.StatusCode); err != nil { - return price, err - } - - body, err := io.ReadAll(res.Body) - if err != nil { - cc.logger.Error("io read", "error", err.Error()) - return price, err - } - - var result CAXQuote - err = json.Unmarshal(body, &result) - if err != nil { - cc.logger.Error("unmarshal quote", "error", err.Error()) - return price, err - } - - if result.Timestamp == "" { - cc.logger.Error("data source returns", "data", string(body)) - return price, common.ErrDataNotAvailable - } - - askPrice, err := decimal.NewFromString(result.AskPrice) - if err != nil { - cc.logger.Error("invalid askPrice value", "error", err) - return price, err - } - - bidPrice, err := decimal.NewFromString(result.BidPrice) - if err != nil { - cc.logger.Error("invalid bidPrice value", "error", err) - return price, err - } - - // the aggregated price takes the average value of ask and bid prices. - price.Price = askPrice.Add(bidPrice).Div(decimal.NewFromInt(2)).String() - price.Symbol = symbol - price.Volume = types.DefaultVolume.String() - - return price, nil -} - -func (cc *CAXClient) buildURL(symbol string) *url.URL { - endpoint := &url.URL{} - endpoint.Path = strings.Join([]string{routers, symbol, quote}, "/") - return endpoint -} - -func (cc *CAXClient) AvailableSymbols() ([]string, error) { - return common.DefaultCryptoSymbols, nil -} - -func (cc *CAXClient) Close() { - cc.client.Conn.Close() -} - -func main() { - conf := common.ResolveConf(os.Args[0], &defaultConfig) - adapter := common.NewPlugin(conf, NewCAXClient(conf), version, types.SrcCEX, common.ChainIDPiccadilly) - defer adapter.Close() - common.PluginServe(adapter) -} diff --git a/plugins/pcgc_cax/pcgc_cax_test.go b/plugins/pcgc_cax/pcgc_cax_test.go deleted file mode 100644 index 2c578d7..0000000 --- a/plugins/pcgc_cax/pcgc_cax_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import ( - "github.com/stretchr/testify/require" - "testing" -) - -func TestNewCAXClient(t *testing.T) { - // set the CAX of dev-net for testing by default since piccadilly CAX is not ready. - defaultConfig.Endpoint = "cax.devnet.clearmatics.network" - client := NewCAXClient(&defaultConfig) - defer client.Close() - prices, err := client.FetchPrice([]string{"ATN-USDC", "NTN-USDC", "NTN-ATN"}) - require.NoError(t, err) - require.Equal(t, 3, len(prices)) -} diff --git a/plugins/template_plugin/template_plugin.go b/plugins/template_plugin/template_plugin.go index 182e56b..b34099c 100644 --- a/plugins/template_plugin/template_plugin.go +++ b/plugins/template_plugin/template_plugin.go @@ -235,7 +235,7 @@ func (tc *TemplateClient) FetchPrice(symbols []string) (common.Prices, error) { return nil, err } - tc.logger.Info("binance", "data", prices) + tc.logger.Info("template_plugin", "data", prices) */ // in this template, we just return fix values.