diff --git a/go/coinstacks/mayachain/api/api.go b/go/coinstacks/mayachain/api/api.go index f47992ee8..e9af607e4 100644 --- a/go/coinstacks/mayachain/api/api.go +++ b/go/coinstacks/mayachain/api/api.go @@ -62,7 +62,7 @@ type API struct { httpClient *mayachain.HTTPClient } -func New(cfg cosmossdk.Config, httpClient *mayachain.HTTPClient, wsClient *mayachain.WSClient, blockService *cosmossdk.BlockService, indexer *mayachain.AffiliateFeeIndexer, swaggerPath string, swaggeruiPath string, prometheus *metrics.Prometheus) *API { +func New(cfg cosmossdk.Config, httpClient *mayachain.HTTPClient, wsClient *mayachain.WSClient, blockService *cosmossdk.BlockService, swaggerPath string, swaggeruiPath string, prometheus *metrics.Prometheus) *API { r := mux.NewRouter() handler := &Handler{ @@ -76,7 +76,6 @@ func New(cfg cosmossdk.Config, httpClient *mayachain.HTTPClient, wsClient *mayac HTTPClient: httpClient, WSClient: wsClient, }, - indexer: indexer, } manager := websocket.NewManager(prometheus) @@ -140,9 +139,6 @@ func New(cfg cosmossdk.Config, httpClient *mayachain.HTTPClient, wsClient *mayac v1Gas := v1.PathPrefix("/gas").Subrouter() v1Gas.HandleFunc("/estimate", a.EstimateGas).Methods("POST") - v1Affiliate := v1.PathPrefix("/affiliate").Subrouter() - v1Affiliate.HandleFunc("/fees", a.AffiliateFees).Methods("GET") - // proxy endpoints r.PathPrefix("/lcd").HandlerFunc(a.LCD).Methods("GET") r.PathPrefix("/rpc").HandlerFunc(a.RPC).Methods("GET", "POST") @@ -243,45 +239,6 @@ func (a *API) EstimateGas(w http.ResponseWriter, r *http.Request) { a.API.EstimateGas(w, r) } -// swagger:parameters GetAffiliateFees -type GetAffiliateFeesParams struct { - // Start timestamp - // in: query - Start string `json:"start"` - // End timestamp - // in: query - End string `json:"end"` -} - -// swagger:route Get /api/v1/affiliate/fees v1 GetAffiliateFees -// -// Get ss affiliate fee history. -// -// responses: -// -// 200: AffiliateFees -// 400: BadRequestError -// 500: InternalServerError -func (a *API) AffiliateFees(w http.ResponseWriter, r *http.Request) { - start, err := strconv.Atoi(r.URL.Query().Get("start")) - if err != nil { - start = 0 - } - - end, err := strconv.Atoi(r.URL.Query().Get("end")) - if err != nil { - end = int(time.Now().UnixMilli()) - } - - affiliateFees, err := a.handler.GetAffiliateFees(start, end) - if err != nil { - api.HandleError(w, http.StatusInternalServerError, err.Error()) - return - } - - api.HandleResponse(w, http.StatusOK, affiliateFees) -} - // swagger:route GET /lcd Proxy LCD // // Mayachain lcd rest api endpoints. diff --git a/go/coinstacks/mayachain/api/handler.go b/go/coinstacks/mayachain/api/handler.go index 120c797e8..8a60cde8c 100644 --- a/go/coinstacks/mayachain/api/handler.go +++ b/go/coinstacks/mayachain/api/handler.go @@ -9,7 +9,6 @@ import ( type Handler struct { *mayachain.Handler - indexer *mayachain.AffiliateFeeIndexer } // Contains info about the running coinstack @@ -52,29 +51,6 @@ func (h *Handler) GetTxHistory(pubkey string, cursor string, pageSize int) (api. return mayachain.GetTxHistory(h.Handler, pubkey, cursor, pageSize) } -// Contains info about affiliate fee history -// swagger:model AffiliateFees -type AffiliateFees struct { - // Affiliate fees - // required: true - Fees []*mayachain.AffiliateFee `json:"fees"` -} - -func (h *Handler) GetAffiliateFees(start int, end int) (*AffiliateFees, error) { - fees := []*mayachain.AffiliateFee{} - for _, fee := range h.indexer.AffiliateFees { - if fee.Timestamp >= int64(start) && fee.Timestamp <= int64(end) { - fees = append(fees, fee) - } - } - - a := &AffiliateFees{ - Fees: fees, - } - - return a, nil -} - func (h *Handler) ParseMessages(msgs []sdk.Msg, events cosmossdk.EventsByMsgIndex) []cosmossdk.Message { return mayachain.ParseMessages(msgs, events) } diff --git a/go/coinstacks/mayachain/api/swagger.json b/go/coinstacks/mayachain/api/swagger.json index e82c9fe9c..40b53db67 100644 --- a/go/coinstacks/mayachain/api/swagger.json +++ b/go/coinstacks/mayachain/api/swagger.json @@ -122,51 +122,6 @@ } } }, - "/api/v1/affiliate/fees": { - "get": { - "tags": [ - "v1" - ], - "summary": "Get ss affiliate fee history.", - "operationId": "GetAffiliateFees", - "parameters": [ - { - "type": "string", - "x-go-name": "Start", - "description": "Start timestamp", - "name": "start", - "in": "query" - }, - { - "type": "string", - "x-go-name": "End", - "description": "End timestamp", - "name": "end", - "in": "query" - } - ], - "responses": { - "200": { - "description": "AffiliateFees", - "schema": { - "$ref": "#/definitions/AffiliateFees" - } - }, - "400": { - "description": "BadRequestError", - "schema": { - "$ref": "#/definitions/BadRequestError" - } - }, - "500": { - "description": "InternalServerError", - "schema": { - "$ref": "#/definitions/InternalServerError" - } - } - } - } - }, "/api/v1/gas/estimate": { "post": { "tags": [ @@ -384,60 +339,6 @@ ], "x-go-package": "github.com/shapeshift/unchained/coinstacks/mayachain/api" }, - "AffiliateFee": { - "type": "object", - "properties": { - "address": { - "type": "string", - "x-go-name": "Address" - }, - "amount": { - "type": "string", - "x-go-name": "Amount" - }, - "asset": { - "type": "string", - "x-go-name": "Asset" - }, - "blockHash": { - "type": "string", - "x-go-name": "BlockHash" - }, - "blockHeight": { - "type": "integer", - "format": "int64", - "x-go-name": "BlockHeight" - }, - "timestamp": { - "type": "integer", - "format": "int64", - "x-go-name": "Timestamp" - }, - "txId": { - "type": "string", - "x-go-name": "TxID" - } - }, - "x-go-package": "github.com/shapeshift/unchained/pkg/mayachain" - }, - "AffiliateFees": { - "description": "Contains info about affiliate fee history", - "type": "object", - "required": [ - "fees" - ], - "properties": { - "fees": { - "description": "Affiliate fees", - "type": "array", - "items": { - "$ref": "#/definitions/AffiliateFee" - }, - "x-go-name": "Fees" - } - }, - "x-go-package": "github.com/shapeshift/unchained/coinstacks/mayachain/api" - }, "ApiError": { "description": "Generic api error for handling failed requests", "type": "object", diff --git a/go/coinstacks/mayachain/cmd/main.go b/go/coinstacks/mayachain/cmd/main.go index 499ca4599..7a3ad8390 100644 --- a/go/coinstacks/mayachain/cmd/main.go +++ b/go/coinstacks/mayachain/cmd/main.go @@ -93,12 +93,7 @@ func main() { logger.Panicf("failed to create new websocket client: %+v", err) } - indexer := mayachain.NewAffiliateFeeIndexer(httpClient, wsClient) - if err := indexer.Sync(); err != nil { - logger.Panicf("failed to index affiliate fees: %+v", err) - } - - api := api.New(cfg.Config, httpClient, wsClient, blockService, indexer, *swaggerPath, *swaggeruiPath, prometheus) + api := api.New(cfg.Config, httpClient, wsClient, blockService, *swaggerPath, *swaggeruiPath, prometheus) defer api.Shutdown() go api.Serve(errChan) diff --git a/go/coinstacks/thorchain/api/api.go b/go/coinstacks/thorchain/api/api.go index e5eb05469..f3a12a719 100644 --- a/go/coinstacks/thorchain/api/api.go +++ b/go/coinstacks/thorchain/api/api.go @@ -62,7 +62,7 @@ type API struct { httpClient *thorchain.HTTPClient } -func New(cfg thorchain.Config, httpClient *thorchain.HTTPClient, wsClient *thorchain.WSClient, blockService *cosmossdk.BlockService, indexer *thorchain.AffiliateFeeIndexer, swaggerPath string, swaggeruiPath string, prometheus *metrics.Prometheus) *API { +func New(cfg thorchain.Config, httpClient *thorchain.HTTPClient, wsClient *thorchain.WSClient, blockService *cosmossdk.BlockService, swaggerPath string, swaggeruiPath string, prometheus *metrics.Prometheus) *API { r := mux.NewRouter() handler := &Handler{ @@ -76,7 +76,6 @@ func New(cfg thorchain.Config, httpClient *thorchain.HTTPClient, wsClient *thorc HTTPClient: httpClient, WSClient: wsClient, }, - indexer: indexer, } manager := websocket.NewManager(prometheus) @@ -140,10 +139,6 @@ func New(cfg thorchain.Config, httpClient *thorchain.HTTPClient, wsClient *thorc v1Gas := v1.PathPrefix("/gas").Subrouter() v1Gas.HandleFunc("/estimate", a.EstimateGas).Methods("POST") - v1Affiliate := v1.PathPrefix("/affiliate").Subrouter() - v1Affiliate.HandleFunc("/revenue", a.AffiliateRevenue).Methods("GET") - v1Affiliate.HandleFunc("/fees", a.AffiliateFees).Methods("GET") - // proxy endpoints r.PathPrefix("/lcd").HandlerFunc(a.LCD).Methods("GET") r.PathPrefix("/rpc").HandlerFunc(a.RPC).Methods("GET", "POST") @@ -244,84 +239,6 @@ func (a *API) EstimateGas(w http.ResponseWriter, r *http.Request) { a.API.EstimateGas(w, r) } -// swagger:parameters GetAffiliateRevenue -type GetAffiliateRevenueParams struct { - // Start timestamp - // in: query - Start string `json:"start"` - // End timestamp - // in: query - End string `json:"end"` -} - -// swagger:route Get /api/v1/affiliate/revenue v1 GetAffiliateRevenue -// -// Get total ss affiliate revenue earned. -// -// responses: -// -// 200: AffiliateRevenue -// 400: BadRequestError -// 500: InternalServerError -func (a *API) AffiliateRevenue(w http.ResponseWriter, r *http.Request) { - start, err := strconv.Atoi(r.URL.Query().Get("start")) - if err != nil { - start = 0 - } - - end, err := strconv.Atoi(r.URL.Query().Get("end")) - if err != nil { - end = int(time.Now().UnixMilli()) - } - - affiliateRevenue, err := a.handler.GetAffiliateRevenue(start, end) - if err != nil { - api.HandleError(w, http.StatusInternalServerError, err.Error()) - return - } - - api.HandleResponse(w, http.StatusOK, affiliateRevenue) -} - -// swagger:parameters GetAffiliateFees -type GetAffiliateFeesParams struct { - // Start timestamp - // in: query - Start string `json:"start"` - // End timestamp - // in: query - End string `json:"end"` -} - -// swagger:route Get /api/v1/affiliate/fees v1 GetAffiliateFees -// -// Get ss affiliate fee history. -// -// responses: -// -// 200: AffiliateFees -// 400: BadRequestError -// 500: InternalServerError -func (a *API) AffiliateFees(w http.ResponseWriter, r *http.Request) { - start, err := strconv.Atoi(r.URL.Query().Get("start")) - if err != nil { - start = 0 - } - - end, err := strconv.Atoi(r.URL.Query().Get("end")) - if err != nil { - end = int(time.Now().UnixMilli()) - } - - affiliateFees, err := a.handler.GetAffiliateFees(start, end) - if err != nil { - api.HandleError(w, http.StatusInternalServerError, err.Error()) - return - } - - api.HandleResponse(w, http.StatusOK, affiliateFees) -} - // swagger:route GET /lcd Proxy LCD // // Thorchain lcd rest api endpoints. diff --git a/go/coinstacks/thorchain/api/handler.go b/go/coinstacks/thorchain/api/handler.go index 08337c717..3132ba817 100644 --- a/go/coinstacks/thorchain/api/handler.go +++ b/go/coinstacks/thorchain/api/handler.go @@ -1,11 +1,7 @@ package api import ( - "fmt" - "math/big" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/pkg/errors" "github.com/shapeshift/unchained/pkg/thorchain" "github.com/shapeshift/unchained/shared/api" "github.com/shapeshift/unchained/shared/cosmossdk" @@ -13,7 +9,6 @@ import ( type Handler struct { *thorchain.Handler - indexer *thorchain.AffiliateFeeIndexer } // Contains info about the running coinstack @@ -69,111 +64,6 @@ func (h *Handler) GetTxHistory(pubkey string, cursor string, pageSize int) (api. return thorchain.GetTxHistory(h.Handler, pubkey, cursor, pageSize) } -// Contains info about the affiliate revenue earned -// swagger:model AffiliateRevenue -type AffiliateRevenue struct { - // Affiliate addresses - // required: true - Addresses []string `json:"addresses"` - // Amount earned (RUNE) - // required: true - Amount string `json:"amount"` - // Revenue earned by denom - // required: true - Revenue map[string]string `json:"revenue"` -} - -func (h *Handler) GetAffiliateRevenue(start int, end int) (*AffiliateRevenue, error) { - total := big.NewInt(0) - revenueTotal := make(map[string]*big.Int) - assetInRune := make(map[string]*big.Float) - for _, fee := range h.indexer.AffiliateFees { - if fee.Timestamp >= int64(start) && fee.Timestamp <= int64(end) { - if amount, ok := new(big.Int).SetString(fee.Amount, 10); ok { - if _, ok := revenueTotal[fee.Asset]; !ok { - revenueTotal[fee.Asset] = big.NewInt(0) - } - - if _, ok := assetInRune[fee.Asset]; !ok { - if fee.Asset == "THOR.RUNE" { - assetInRune[fee.Asset] = big.NewFloat(1) - } else { - var res struct { - Asset string `json:"asset"` - BalanceAsset string `json:"balance_asset"` - BalanceRune string `json:"balance_rune"` - } - - var errRes struct { - Code string `json:"code"` - Message string `json:"message"` - } - - if _, err := h.HTTPClient.(*thorchain.HTTPClient).LCD.R().SetResult(&res).SetError(&errRes).Get(fmt.Sprintf("/thorchain/pool/%s", fee.Asset)); err != nil { - return nil, errors.Wrapf(err, "failed to get pool details for %s", fee.Asset) - } - if errRes.Message != "" { - return nil, errors.New(errRes.Message) - } - - balanceRune, ok := new(big.Float).SetString(res.BalanceRune) - if !ok { - return nil, errors.Errorf("failed convert balance rune: %s", res.BalanceRune) - } - - balanceAsset, ok := new(big.Float).SetString(res.BalanceAsset) - if !ok { - return nil, errors.Errorf("failed convert balance asset: %s", res.BalanceAsset) - } - - assetInRune[fee.Asset] = new(big.Float).Quo(balanceRune, balanceAsset) - } - } - - amountInRune, _ := new(big.Float).Mul(assetInRune[fee.Asset], new(big.Float).SetInt(amount)).Int(nil) - total.Add(total, amountInRune) - revenueTotal[fee.Asset].Add(revenueTotal[fee.Asset], amount) - } - } - } - - revenue := make(map[string]string) - for asset, amount := range revenueTotal { - revenue[asset] = amount.String() - } - - a := &AffiliateRevenue{ - Addresses: h.indexer.AffiliateAddresses, - Amount: total.String(), - Revenue: revenue, - } - - return a, nil -} - -// Contains info about affiliate fee history -// swagger:model AffiliateFees -type AffiliateFees struct { - // Affiliate fees - // required: true - Fees []*thorchain.AffiliateFee `json:"fees"` -} - -func (h *Handler) GetAffiliateFees(start int, end int) (*AffiliateFees, error) { - fees := []*thorchain.AffiliateFee{} - for _, fee := range h.indexer.AffiliateFees { - if fee.Timestamp >= int64(start) && fee.Timestamp <= int64(end) { - fees = append(fees, fee) - } - } - - a := &AffiliateFees{ - Fees: fees, - } - - return a, nil -} - func (h *Handler) ParseMessages(msgs []sdk.Msg, events cosmossdk.EventsByMsgIndex) []cosmossdk.Message { return thorchain.ParseMessages(msgs, events) } diff --git a/go/coinstacks/thorchain/api/swagger.json b/go/coinstacks/thorchain/api/swagger.json index 16d39e315..081b26986 100644 --- a/go/coinstacks/thorchain/api/swagger.json +++ b/go/coinstacks/thorchain/api/swagger.json @@ -122,96 +122,6 @@ } } }, - "/api/v1/affiliate/fees": { - "get": { - "tags": [ - "v1" - ], - "summary": "Get ss affiliate fee history.", - "operationId": "GetAffiliateFees", - "parameters": [ - { - "type": "string", - "x-go-name": "Start", - "description": "Start timestamp", - "name": "start", - "in": "query" - }, - { - "type": "string", - "x-go-name": "End", - "description": "End timestamp", - "name": "end", - "in": "query" - } - ], - "responses": { - "200": { - "description": "AffiliateFees", - "schema": { - "$ref": "#/definitions/AffiliateFees" - } - }, - "400": { - "description": "BadRequestError", - "schema": { - "$ref": "#/definitions/BadRequestError" - } - }, - "500": { - "description": "InternalServerError", - "schema": { - "$ref": "#/definitions/InternalServerError" - } - } - } - } - }, - "/api/v1/affiliate/revenue": { - "get": { - "tags": [ - "v1" - ], - "summary": "Get total ss affiliate revenue earned.", - "operationId": "GetAffiliateRevenue", - "parameters": [ - { - "type": "string", - "x-go-name": "Start", - "description": "Start timestamp", - "name": "start", - "in": "query" - }, - { - "type": "string", - "x-go-name": "End", - "description": "End timestamp", - "name": "end", - "in": "query" - } - ], - "responses": { - "200": { - "description": "AffiliateRevenue", - "schema": { - "$ref": "#/definitions/AffiliateRevenue" - } - }, - "400": { - "description": "BadRequestError", - "schema": { - "$ref": "#/definitions/BadRequestError" - } - }, - "500": { - "description": "InternalServerError", - "schema": { - "$ref": "#/definitions/InternalServerError" - } - } - } - } - }, "/api/v1/gas/estimate": { "post": { "tags": [ @@ -429,93 +339,6 @@ ], "x-go-package": "github.com/shapeshift/unchained/coinstacks/thorchain/api" }, - "AffiliateFee": { - "type": "object", - "properties": { - "address": { - "type": "string", - "x-go-name": "Address" - }, - "amount": { - "type": "string", - "x-go-name": "Amount" - }, - "asset": { - "type": "string", - "x-go-name": "Asset" - }, - "blockHash": { - "type": "string", - "x-go-name": "BlockHash" - }, - "blockHeight": { - "type": "integer", - "format": "int64", - "x-go-name": "BlockHeight" - }, - "timestamp": { - "type": "integer", - "format": "int64", - "x-go-name": "Timestamp" - }, - "txId": { - "type": "string", - "x-go-name": "TxID" - } - }, - "x-go-package": "github.com/shapeshift/unchained/pkg/thorchain" - }, - "AffiliateFees": { - "description": "Contains info about affiliate fee history", - "type": "object", - "required": [ - "fees" - ], - "properties": { - "fees": { - "description": "Affiliate fees", - "type": "array", - "items": { - "$ref": "#/definitions/AffiliateFee" - }, - "x-go-name": "Fees" - } - }, - "x-go-package": "github.com/shapeshift/unchained/coinstacks/thorchain/api" - }, - "AffiliateRevenue": { - "description": "Contains info about the affiliate revenue earned", - "type": "object", - "required": [ - "addresses", - "amount", - "revenue" - ], - "properties": { - "addresses": { - "description": "Affiliate addresses", - "type": "array", - "items": { - "type": "string" - }, - "x-go-name": "Addresses" - }, - "amount": { - "description": "Amount earned (RUNE)", - "type": "string", - "x-go-name": "Amount" - }, - "revenue": { - "description": "Revenue earned by denom", - "type": "object", - "additionalProperties": { - "type": "string" - }, - "x-go-name": "Revenue" - } - }, - "x-go-package": "github.com/shapeshift/unchained/coinstacks/thorchain/api" - }, "ApiError": { "description": "Generic api error for handling failed requests", "type": "object", diff --git a/go/coinstacks/thorchain/cmd/main.go b/go/coinstacks/thorchain/cmd/main.go index 4ff4f3a01..daff194be 100644 --- a/go/coinstacks/thorchain/cmd/main.go +++ b/go/coinstacks/thorchain/cmd/main.go @@ -30,12 +30,8 @@ var ( type Config struct { LCDURL string `mapstructure:"LCD_URL"` LCDAPIKEY string `mapstructure:"LCD_API_KEY"` - LCDV1URL string `mapstructure:"LCD_V1_URL"` - LCDV1APIKEY string `mapstructure:"LCD_V1_API_KEY"` RPCURL string `mapstructure:"RPC_URL"` RPCAPIKEY string `mapstructure:"RPC_API_KEY"` - RPCV1URL string `mapstructure:"RPC_V1_URL"` - RPCV1APIKEY string `mapstructure:"RPC_V1_API_KEY"` INDEXERURL string `mapstructure:"INDEXER_URL"` INDEXERAPIKEY string `mapstructure:"INDEXER_API_KEY"` WSURL string `mapstructure:"WS_URL"` @@ -51,7 +47,7 @@ func main() { conf := &Config{} if *envPath == "" { - if err := config.LoadFromEnv(conf, "LCD_URL", "LCD_API_KEY", "LCD_V1_URL", "LCD_V1_API_KEY", "RPC_URL", "RPC_API_KEY", "RPC_V1_URL", "RPC_V1_API_KEY", "INDEXER_URL", "INDEXER_API_KEY", "WS_URL", "WS_API_KEY"); err != nil { + if err := config.LoadFromEnv(conf, "LCD_URL", "LCD_API_KEY", "RPC_URL", "RPC_API_KEY", "INDEXER_URL", "INDEXER_API_KEY", "WS_URL", "WS_API_KEY"); err != nil { logger.Panicf("failed to load config from env: %+v", err) } } else { @@ -94,20 +90,6 @@ func main() { INDEXERAPIKEY: conf.INDEXERAPIKEY, } - cfgV1 := thorchain.Config{ - Config: cosmossdk.Config{ - Bech32AddrPrefix: "thor", - Bech32PkPrefix: "thorpub", - Denom: "rune", - NativeFee: 2000000, // https://daemon.thorchain.shapeshift.com/lcd/thorchain/constants - Encoding: encoding, - LCDURL: conf.LCDV1URL, - LCDAPIKEY: conf.LCDV1APIKEY, - RPCURL: conf.RPCV1URL, - RPCAPIKEY: conf.RPCV1APIKEY, - }, - } - prometheus := metrics.NewPrometheus("thorchain") sdk.GetConfig().SetBech32PrefixForAccount(cfg.Bech32AddrPrefix, cfg.Bech32PkPrefix) @@ -117,11 +99,6 @@ func main() { logger.Panicf("failed to create new http client: %+v", err) } - httpClientV1, err := thorchain.NewHTTPClient(cfgV1) - if err != nil { - logger.Panicf("failed to create new http client: %+v", err) - } - blockService, err := cosmossdk.NewBlockService(httpClient) if err != nil { logger.Panicf("failed to create new block service: %+v", err) @@ -132,12 +109,7 @@ func main() { logger.Panicf("failed to create new websocket client: %+v", err) } - indexer := thorchain.NewAffiliateFeeIndexer([]*thorchain.HTTPClient{httpClientV1, httpClient}, wsClient) - if err := indexer.Sync(); err != nil { - logger.Panicf("failed to index affiliate fees: %+v", err) - } - - api := api.New(cfg, httpClient, wsClient, blockService, indexer, *swaggerPath, *swaggeruiPath, prometheus) + api := api.New(cfg, httpClient, wsClient, blockService, *swaggerPath, *swaggeruiPath, prometheus) defer api.Shutdown() go api.Serve(errChan) diff --git a/go/coinstacks/thorchain/sample.env b/go/coinstacks/thorchain/sample.env index 70fed2708..5f1ed6c6e 100644 --- a/go/coinstacks/thorchain/sample.env +++ b/go/coinstacks/thorchain/sample.env @@ -1,15 +1,11 @@ # SECRET ENVIRONMENT VARIABLES LCD_API_KEY= -LCD_V1_API_KEY= RPC_API_KEY= -RPC_V1_API_KEY= INDEXER_API_KEY= WS_API_KEY= # ENVIRONMENT VARIABLES LCD_URL=https://gateway.liquify.com -LCD_V1_URL=https://gateway.liquify.com RPC_URL=https://gateway.liquify.com -RPC_V1_URL=https://gateway-lon.liquify.com INDEXER_URL=https://gateway.liquify.com -WS_URL=wss://gateway.liquify.com \ No newline at end of file +WS_URL=wss://gateway.liquify.com diff --git a/go/pkg/mayachain/affiliateFees.go b/go/pkg/mayachain/affiliateFees.go deleted file mode 100644 index 8e4ac5e72..000000000 --- a/go/pkg/mayachain/affiliateFees.go +++ /dev/null @@ -1,190 +0,0 @@ -package mayachain - -import ( - "fmt" - "math" - "slices" - "strings" - "sync" - "time" - - "github.com/shapeshift/unchained/shared/cosmossdk" - coretypes "github.com/tendermint/tendermint/rpc/core/types" - "github.com/tendermint/tendermint/types" - "golang.org/x/sync/errgroup" -) - -const ( - blockWorkers = 10 - pageSize = 50 - resultWorkers = 100 -) - -type AffiliateFeeIndexer struct { - AffiliateAddresses []string - AffiliateFeeDenoms []string - AffiliateFees []*AffiliateFee - httpClient *HTTPClient - mu sync.Mutex -} - -type AffiliateFee struct { - Amount string `json:"amount"` - Asset string `json:"asset"` - BlockHeight int64 `json:"blockHeight"` - BlockHash string `json:"blockHash"` - Timestamp int64 `json:"timestamp"` - Address string `json:"address"` - TxID string `json:"txId"` -} - -func NewAffiliateFeeIndexer(httpClient *HTTPClient, wsClient *WSClient) *AffiliateFeeIndexer { - affiliateAddresses := []string{"maya122h9hlrugzdny9ct95z6g7afvpzu34s73tgnyv"} - affilateFeeDenoms := []string{"MAYA.CACAO"} - - i := &AffiliateFeeIndexer{ - AffiliateAddresses: affiliateAddresses, - AffiliateFeeDenoms: affilateFeeDenoms, - AffiliateFees: []*AffiliateFee{}, - httpClient: httpClient, - } - - wsClient.NewBlockHandler(func(newBlock types.EventDataNewBlock, blockEvents []cosmossdk.ABCIEvent) { - i.processAffiliateFees(&NewBlock{EventDataNewBlock: newBlock}, blockEvents, i.AffiliateAddresses) - }) - - return i -} - -func (i *AffiliateFeeIndexer) Sync() error { - start := time.Now() - logger.Info("Started indexing affiliate fees") - - g := new(errgroup.Group) - - for _, affiliateAddress := range i.AffiliateAddresses { - g.Go(func() error { - result, err := i.httpClient.BlockSearch(fmt.Sprintf(`"outbound.to='%s'"`, affiliateAddress), 1, pageSize) - if err != nil { - return err - } - - maxPages := int(math.Ceil(float64(result.TotalCount) / float64(pageSize))) - - pageCh := make(chan int, maxPages) - for page := 2; page <= maxPages; page++ { - pageCh <- page - } - close(pageCh) - - resultCh := make(chan *coretypes.ResultBlockSearch, 1) - resultCh <- result - - go i.fetchBlocks(affiliateAddress, pageCh, resultCh) - i.handleBlocks(affiliateAddress, resultCh).Wait() - - return nil - }) - - if err := g.Wait(); err != nil { - return err - } - } - - logger.Infof("Finished indexing affiliate fees (%s)", time.Since(start)) - - return nil -} - -func (i *AffiliateFeeIndexer) fetchBlocks(affiliateAddress string, pageCh <-chan int, resultCh chan<- *coretypes.ResultBlockSearch) { - defer close(resultCh) - - wg := new(sync.WaitGroup) - wg.Add(blockWorkers) - - for range blockWorkers { - go func() { - defer wg.Done() - - for page := range pageCh { - result, err := i.httpClient.BlockSearch(fmt.Sprintf(`"outbound.to='%s'"`, affiliateAddress), page, pageSize) - if err != nil { - logger.Panicf("failed to fetch blocks for page: %d: %+v", page, err) - } - - resultCh <- result - } - }() - } - - wg.Wait() -} - -func (i *AffiliateFeeIndexer) handleBlocks(affiliateAddress string, resultCh <-chan *coretypes.ResultBlockSearch) *sync.WaitGroup { - wg := new(sync.WaitGroup) - wg.Add(resultWorkers) - - for range resultWorkers { - go func() { - defer wg.Done() - - for result := range resultCh { - for _, b := range result.Blocks { - blockResult, err := i.httpClient.BlockResults(int(b.Block.Height)) - if err != nil { - logger.Panicf("failed to handle block: %d: %+v", b.Block.Height, err) - } - - i.processAffiliateFees(&ResultBlock{Block: b.Block}, blockResult.GetBlockEvents(), []string{affiliateAddress}) - } - } - }() - } - - return wg -} - -func (i *AffiliateFeeIndexer) processAffiliateFees(block Block, blockEvents []cosmossdk.ABCIEvent, affiliateAddresses []string) { - _, typedEvents, err := ParseBlockEvents(blockEvents) - if err != nil { - logger.Panicf("failed to parse block events for block: %d: %+v", block.Height(), err) - } - - swaps := make(map[string]*EventSwap) - affiliateFees := make([]*AffiliateFee, 0) - for _, event := range typedEvents { - switch v := event.(type) { - case *EventOutbound: - coinParts := strings.Fields(v.Coin) - if slices.Contains(affiliateAddresses, v.To) && slices.Contains(i.AffiliateFeeDenoms, coinParts[1]) { - affiliateFee := &AffiliateFee{ - BlockHash: block.Hash(), - BlockHeight: block.Height(), - Timestamp: block.Timestamp(), - TxID: v.InTxID, - Address: v.To, - Amount: coinParts[0], - Asset: coinParts[1], - } - affiliateFees = append(affiliateFees, affiliateFee) - } - case *EventSwap: - parts := strings.Split(v.Memo, ":") - if len(parts) > 4 && parts[4] == "ssmaya" { - swaps[v.Id] = v - } - default: - continue - } - } - - for _, affiliateFee := range affiliateFees { - if _, ok := swaps[affiliateFee.TxID]; !ok { - continue - } - - i.mu.Lock() - i.AffiliateFees = append(i.AffiliateFees, affiliateFee) - i.mu.Unlock() - } -} diff --git a/go/pkg/thorchain/affiliateFees.go b/go/pkg/thorchain/affiliateFees.go deleted file mode 100644 index 445eeb842..000000000 --- a/go/pkg/thorchain/affiliateFees.go +++ /dev/null @@ -1,192 +0,0 @@ -package thorchain - -import ( - "fmt" - "math" - "slices" - "strings" - "sync" - "time" - - coretypes "github.com/cometbft/cometbft/rpc/core/types" - "github.com/cometbft/cometbft/types" - "github.com/shapeshift/unchained/shared/cosmossdk" - "golang.org/x/sync/errgroup" -) - -const ( - blockWorkers = 10 - pageSize = 50 - resultWorkers = 100 -) - -type AffiliateFeeIndexer struct { - AffiliateAddresses []string - AffiliateFeeDenoms []string - AffiliateFees []*AffiliateFee - httpClients []*HTTPClient - mu sync.Mutex -} - -type AffiliateFee struct { - Amount string `json:"amount"` - Asset string `json:"asset"` - BlockHeight int64 `json:"blockHeight"` - BlockHash string `json:"blockHash"` - Timestamp int64 `json:"timestamp"` - Address string `json:"address"` - TxID string `json:"txId"` -} - -func NewAffiliateFeeIndexer(httpClients []*HTTPClient, wsClient *WSClient) *AffiliateFeeIndexer { - affiliateAddresses := []string{"thor1xmaggkcln5m5fnha2780xrdrulmplvfrz6wj3l", "thor1crs0y53jfg224mettqeg883e6ume49tllktg2s", "thor122h9hlrugzdny9ct95z6g7afvpzu34s73uklju"} - affilateFeeDenoms := []string{"THOR.RUNE", "ETH.WBTC-0X2260FAC5E5542A773AA44FBCFEDF7C193BC2C599"} - - i := &AffiliateFeeIndexer{ - AffiliateAddresses: affiliateAddresses, - AffiliateFeeDenoms: affilateFeeDenoms, - AffiliateFees: []*AffiliateFee{}, - httpClients: httpClients, - } - - wsClient.NewBlockHandler(func(newBlock types.EventDataNewBlock, blockEvents []cosmossdk.ABCIEvent) { - i.processAffiliateFees(&NewBlock{EventDataNewBlock: newBlock}, blockEvents, i.AffiliateAddresses) - }) - - return i -} - -func (i *AffiliateFeeIndexer) Sync() error { - start := time.Now() - logger.Info("Started indexing affiliate fees") - - g := new(errgroup.Group) - - for _, affiliateAddress := range i.AffiliateAddresses { - for _, httpClient := range i.httpClients { - g.Go(func() error { - result, err := httpClient.BlockSearch(fmt.Sprintf(`"outbound.to='%s'"`, affiliateAddress), 1, pageSize) - if err != nil { - return err - } - - maxPages := int(math.Ceil(float64(result.TotalCount) / float64(pageSize))) - - pageCh := make(chan int, maxPages) - for page := 2; page <= maxPages; page++ { - pageCh <- page - } - close(pageCh) - - resultCh := make(chan *coretypes.ResultBlockSearch, 1) - resultCh <- result - - go i.fetchBlocks(httpClient, affiliateAddress, pageCh, resultCh) - i.handleBlocks(httpClient, affiliateAddress, resultCh).Wait() - - return nil - }) - } - - if err := g.Wait(); err != nil { - return err - } - } - - logger.Infof("Finished indexing affiliate fees (%s)", time.Since(start)) - - return nil -} - -func (i *AffiliateFeeIndexer) fetchBlocks(httpClient *HTTPClient, affiliateAddress string, pageCh <-chan int, resultCh chan<- *coretypes.ResultBlockSearch) { - defer close(resultCh) - - wg := new(sync.WaitGroup) - wg.Add(blockWorkers) - - for range blockWorkers { - go func() { - defer wg.Done() - - for page := range pageCh { - result, err := httpClient.BlockSearch(fmt.Sprintf(`"outbound.to='%s'"`, affiliateAddress), page, pageSize) - if err != nil { - logger.Panicf("failed to fetch blocks for page: %d: %+v", page, err) - } - - resultCh <- result - } - }() - } - - wg.Wait() -} - -func (i *AffiliateFeeIndexer) handleBlocks(httpClient *HTTPClient, affiliateAddress string, resultCh <-chan *coretypes.ResultBlockSearch) *sync.WaitGroup { - wg := new(sync.WaitGroup) - wg.Add(resultWorkers) - - for range resultWorkers { - go func() { - defer wg.Done() - - for result := range resultCh { - for _, b := range result.Blocks { - blockResult, err := httpClient.BlockResults(int(b.Block.Height)) - if err != nil { - logger.Panicf("failed to handle block: %d: %+v", b.Block.Height, err) - } - - i.processAffiliateFees(&ResultBlock{Block: b.Block}, blockResult.GetBlockEvents(), []string{affiliateAddress}) - } - } - }() - } - - return wg -} - -func (i *AffiliateFeeIndexer) processAffiliateFees(block Block, blockEvents []cosmossdk.ABCIEvent, affiliateAddresses []string) { - _, typedEvents, err := ParseBlockEvents(blockEvents) - if err != nil { - logger.Panicf("failed to parse block events for block: %d: %+v", block.Height(), err) - } - - swaps := make(map[string]*EventSwap) - affiliateFees := make([]*AffiliateFee, 0) - for _, event := range typedEvents { - switch v := event.(type) { - case *EventOutbound: - coinParts := strings.Fields(v.Coin) - if slices.Contains(affiliateAddresses, v.To) && slices.Contains(i.AffiliateFeeDenoms, coinParts[1]) { - affiliateFee := &AffiliateFee{ - BlockHash: block.Hash(), - BlockHeight: block.Height(), - Timestamp: block.Timestamp(), - TxID: v.InTxID, - Address: v.To, - Amount: coinParts[0], - Asset: coinParts[1], - } - affiliateFees = append(affiliateFees, affiliateFee) - } - case *EventSwap: - parts := strings.Split(v.Memo, ":") - if len(parts) > 4 && parts[4] == "ss" { - swaps[v.Id] = v - } - default: - continue - } - } - - for _, affiliateFee := range affiliateFees { - if _, ok := swaps[affiliateFee.TxID]; !ok { - continue - } - - i.mu.Lock() - i.AffiliateFees = append(i.AffiliateFees, affiliateFee) - i.mu.Unlock() - } -}