Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.12 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "fmt"
  6.     "github.com/ashwanthkumar/slack-go-webhook"
  7.     "io/ioutil"
  8.     "log"
  9.     "net/http"
  10.     "os"
  11.     "time"
  12. )
  13.  
  14. type Effect struct {
  15.     PagingToken     string `json:"paging_token"`
  16.     SoldAmount      string `json:"sold_amount"`
  17.     SoldAssetCode   string `json:"sold_asset_code"`
  18.     BoughtAmount    string `json:"bought_amount"`
  19.     BoughtAssetCode string `json:"bought_asset_code"`
  20.     Type            string `json:"type"`
  21. }
  22.  
  23. type Embedded struct {
  24.     Records []Effect `json:"records"`
  25. }
  26.  
  27. type HorizonResponse struct {
  28.     Embedded Embedded `json:"_embedded"`
  29. }
  30.  
  31. // publicKey retrieves stellar's public key from environment.
  32. func publicKey() (key string) {
  33.     key = os.Getenv("STELLAR_PUBLIC_KEY")
  34.     return
  35. }
  36.  
  37. // lastTransactionID finds the latest effect at startup.
  38. // Used to only process newer effects.
  39. func lastTransactionID() (id string) {
  40.     resp, err := http.Get("https://horizon.stellar.org/accounts/" + publicKey() + "/effects?order=desc")
  41.     if err != nil {
  42.         log.Fatal("Can't find latest id: GET error")
  43.     }
  44.     defer resp.Body.Close()
  45.     body, err := ioutil.ReadAll(resp.Body)
  46.  
  47.     response := HorizonResponse{}
  48.     err = json.Unmarshal(body, &response)
  49.     if err != nil {
  50.         log.Fatal("Can't find latest id: can't decode json")
  51.     }
  52.  
  53.     id = response.Embedded.Records[0].PagingToken
  54.     return
  55. }
  56.  
  57. // checkNewTrades find any new trade since last check.
  58. func checkNewTrades(cursor string) (trades []Effect, lastId string, any bool) {
  59.     trades = make([]Effect, 0)
  60.     lastId = cursor
  61.  
  62.     resp, err := http.Get("https://horizon.stellar.org/accounts/" + publicKey() + "/effects?cursor=" + cursor)
  63.     if err != nil {
  64.         log.Printf("Can't find new effects: GET error")
  65.         return
  66.     }
  67.     defer resp.Body.Close()
  68.     body, err := ioutil.ReadAll(resp.Body)
  69.  
  70.     response := HorizonResponse{}
  71.     err = json.Unmarshal(body, &response)
  72.     if err != nil {
  73.         log.Printf("Can't find new effects: can't decode json")
  74.         return
  75.     }
  76.  
  77.     if len(response.Embedded.Records) > 0 {
  78.         for _, effect := range response.Embedded.Records {
  79.             if effect.Type == "trade" {
  80.                 trades = append(trades, effect)
  81.             }
  82.         }
  83.  
  84.         lastId = response.Embedded.Records[len(response.Embedded.Records)-1].PagingToken
  85.         any = true
  86.     }
  87.  
  88.     return
  89. }
  90.  
  91. // notify sends notice about any new trade to slack.
  92. func notify(trades []Effect) {
  93.     for _, trade := range trades {
  94.         if trade.BoughtAssetCode == "" {
  95.             trade.BoughtAssetCode = "XLM"
  96.         }
  97.  
  98.         if trade.SoldAssetCode == "" {
  99.             trade.SoldAssetCode = "XLM"
  100.         }
  101.  
  102.         webhookUrl := os.Getenv("SDEX_SLACK_HOOK")
  103.         msg := fmt.Sprintf("Traded %s%s for %s%s\n", trade.SoldAmount, trade.SoldAssetCode, trade.BoughtAmount, trade.BoughtAssetCode)
  104.  
  105.         payload := slack.Payload{
  106.             Text:      msg,
  107.             Username:  "horizon",
  108.             Channel:   "#sdex",
  109.             IconEmoji: ":monkey_face:",
  110.         }
  111.  
  112.         err := slack.Send(webhookUrl, "", payload)
  113.         if len(err) > 0 {
  114.             fmt.Printf("error: %s\n", err)
  115.         }
  116.  
  117.     }
  118. }
  119.  
  120. func main() {
  121.     startAt := lastTransactionID()
  122.  
  123.     for {
  124.         <-time.After(1 * time.Minute)
  125.  
  126.         var (
  127.             trades []Effect
  128.             any    bool
  129.         )
  130.  
  131.         trades, startAt, any = checkNewTrades(startAt)
  132.         if any {
  133.             notify(trades)
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement