From 00fa9de912de277ad0c4df0c4c0635476d679523 Mon Sep 17 00:00:00 2001 From: Bill Liang Date: Wed, 5 Jun 2024 18:27:16 -0700 Subject: [PATCH 1/2] fea: add new analytics v3 for new lista-api --- cmd/bot/main.go | 2 +- config/example.config.yaml | 3 +- internal/analytics/v3/client.go | 49 +++++++++++++++++++++++++++++++++ internal/analytics/v3/misc.go | 28 +++++++++++++++++++ internal/jobs/start_auction.go | 8 +++--- pkg/config/config.go | 3 +- pkg/config/resource.go | 12 +++++--- 7 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 internal/analytics/v3/client.go create mode 100644 internal/analytics/v3/misc.go diff --git a/cmd/bot/main.go b/cmd/bot/main.go index f01fa3c..d69a55c 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -125,7 +125,7 @@ func Run(cfg *config.Config) { context.Background(), resource.Log, resource.Wallet, - resource.AnalyticsClient, + resource.AnalyticsClientV3, resource.HttpNodeClient, interaction, collateral, diff --git a/config/example.config.yaml b/config/example.config.yaml index 6b47682..27c8fe0 100644 --- a/config/example.config.yaml +++ b/config/example.config.yaml @@ -28,4 +28,5 @@ contract: settings: maxPricePercentage: 80 analytics: - url: "https://api.prod.helio.money/" \ No newline at end of file + url: "https://api.prod.helio.money/" + lista-api-url: "https://api.lista.org/" \ No newline at end of file diff --git a/internal/analytics/v3/client.go b/internal/analytics/v3/client.go new file mode 100644 index 0000000..fd81aac --- /dev/null +++ b/internal/analytics/v3/client.go @@ -0,0 +1,49 @@ +package v1 + +import ( + "context" + "encoding/json" + "fmt" + httpcon "github.com/lista-dao/AuctionBots-go/pkg/httpconn" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "net/http" + "net/url" +) + +type Client struct { + cn *httpcon.Connector + log *logrus.Logger +} + +func NewClient( + base *url.URL, + log *logrus.Logger, +) *Client { + cli := &Client{ + cn: httpcon.NewConnector(base, nil), + log: log, + } + cli.log.Debug("V3 New Client ", base.Host) + return cli +} + +func (cli *Client) GetRedUsers(ctx context.Context) ([]User, error) { + cli.log.Debug("V3 GetRedUsers Starting...") + code, body, err := cli.cn.Get(ctx, "/api/v2/liquidations/red", nil) + if err != nil { + return nil, err + } + + if *code == http.StatusOK { + var resp CommonDataResp + + if err := json.Unmarshal(body, &resp); err != nil { + return nil, errors.Wrapf(err, "failed to unmarshal body %s", string(body)) + } + + return resp.Data.Users, nil + } + + return nil, errors.New(fmt.Sprintf("failed with %s", http.StatusText(*code))) +} diff --git a/internal/analytics/v3/misc.go b/internal/analytics/v3/misc.go new file mode 100644 index 0000000..7ab3796 --- /dev/null +++ b/internal/analytics/v3/misc.go @@ -0,0 +1,28 @@ +package v1 + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/shopspring/decimal" +) + +type LiquidationUsersResp struct { + Users []User `json:"users"` +} + +type CommonDataResp struct { + Code string `json:"code"` + Msg string `json:"msg"` + Data struct { + Users []User `json:"users"` + } `json:"data"` + Timestamp int64 `json:"timestamp"` +} + +type User struct { + UserAddress common.Address `json:"userAddress"` + TokenName string `json:"tokenName"` + Collateral decimal.Decimal `json:"collateral"` + //LiquidationCost decimal.Decimal `json:"liquidationCost"` + //RangeFromLiquidation decimal.Decimal `json:"rangeFromLiquidation"` + //LiquidationPrice decimal.Decimal `json:"liquidationPrice"` +} diff --git a/internal/jobs/start_auction.go b/internal/jobs/start_auction.go index e27d32a..2a88d74 100644 --- a/internal/jobs/start_auction.go +++ b/internal/jobs/start_auction.go @@ -9,7 +9,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" - analyticsv1 "github.com/lista-dao/AuctionBots-go/internal/analytics/v1" + analyticsv3 "github.com/lista-dao/AuctionBots-go/internal/analytics/v3" daov2 "github.com/lista-dao/AuctionBots-go/internal/dao/v2/interaction" "github.com/lista-dao/AuctionBots-go/internal/wallet" "github.com/pkg/errors" @@ -22,7 +22,7 @@ func NewStartAuctionJob( ctx context.Context, log *logrus.Logger, wall wallet.Walleter, - analyticsCli *analyticsv1.Client, + analyticsCli *analyticsv3.Client, ethCli *ethclient.Client, interactAddr common.Address, collateralAddr common.Address, @@ -52,7 +52,7 @@ type startAuctionJob struct { ctx context.Context wallet wallet.Walleter - analyticsCli *analyticsv1.Client + analyticsCli *analyticsv3.Client ethCli *ethclient.Client log *logrus.Entry interactAddr common.Address @@ -120,7 +120,7 @@ func (j *startAuctionJob) Run(ctx context.Context) { }() } -func (j *startAuctionJob) startAuction(user analyticsv1.User) error { +func (j *startAuctionJob) startAuction(user analyticsv3.User) error { logrus.Infof("start auction for user: %s collateral: %s", user.UserAddress.String(), j.collateralAddr.String()) opts, err := j.wallet.Opts(j.ctx) diff --git a/pkg/config/config.go b/pkg/config/config.go index e2ee4b0..27e8f87 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -32,7 +32,8 @@ type Config struct { MaxPricePercentage int64 `mapstructure:"maxPricePercentage"` } `mapstructure:"settings"` Analytics struct { - Url string `mapstructure:"url"` + Url string `mapstructure:"url"` + ListaApiUrl string `mapstructure:"lista-api-url"` } `mapstructure:"analytics"` } diff --git a/pkg/config/resource.go b/pkg/config/resource.go index 8a44ebf..22594ad 100644 --- a/pkg/config/resource.go +++ b/pkg/config/resource.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/ethereum/go-ethereum/rpc" "github.com/gorilla/websocket" + analyticsv3 "github.com/lista-dao/AuctionBots-go/internal/analytics/v3" "net/http" "net/url" "sync" @@ -17,10 +18,11 @@ import ( ) type Resource struct { - Log *logrus.Logger - HttpNodeClient *ethclient.Client - Wallet wallet.Walleter - AnalyticsClient *analyticsv1.Client + Log *logrus.Logger + HttpNodeClient *ethclient.Client + Wallet wallet.Walleter + AnalyticsClient *analyticsv1.Client + AnalyticsClientV3 *analyticsv3.Client } func LoadEnvironmentResource(config *Config) (*Resource, error) { @@ -36,10 +38,12 @@ func LoadEnvironmentResource(config *Config) (*Resource, error) { resource.Log.SetReportCaller(config.Log.Caller) analyticsUrl, err := url.Parse(config.Analytics.Url) + analyticsUrlV3, err := url.Parse(config.Analytics.ListaApiUrl) if err != nil { return nil, fmt.Errorf("url.Parse err: %w", err) } resource.AnalyticsClient = analyticsv1.NewClient(analyticsUrl) + resource.AnalyticsClientV3 = analyticsv3.NewClient(analyticsUrlV3, resource.Log) resource.HttpNodeClient, err = initHttpNodeClient(config.RpcNode.Http) if err != nil { From 12aad8ebd98a92f2679e15205897ed6daf1d9164 Mon Sep 17 00:00:00 2001 From: Bill Liang Date: Tue, 2 Jul 2024 23:05:04 -0700 Subject: [PATCH 2/2] fea:add new collateral: BBTC --- config/example.config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/example.config.yaml b/config/example.config.yaml index 27c8fe0..70bfa99 100644 --- a/config/example.config.yaml +++ b/config/example.config.yaml @@ -23,6 +23,7 @@ contract: - "0x04C0599Ae5A44757c0af6F9eC3b93da8976c150A" - "0x80137510979822322193fc997d400d5a6c747bf7" - "0x4aae823a6a0b376de6a78e74ecc5b079d38cbcf7" + - "0xF5e11df1ebCf78b6b6D26E04FF19cD786a1e81dC" hay: "0x0782b6d8c4551B9760e74c0545a9bCD90bdc41E5" flushBuy: "" settings: