Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- "fmt"
- "github.com/ashwanthkumar/slack-go-webhook"
- "io/ioutil"
- "log"
- "net/http"
- "os"
- "time"
- )
- type Effect struct {
- PagingToken string `json:"paging_token"`
- SoldAmount string `json:"sold_amount"`
- SoldAssetCode string `json:"sold_asset_code"`
- BoughtAmount string `json:"bought_amount"`
- BoughtAssetCode string `json:"bought_asset_code"`
- Type string `json:"type"`
- }
- type Embedded struct {
- Records []Effect `json:"records"`
- }
- type HorizonResponse struct {
- Embedded Embedded `json:"_embedded"`
- }
- // publicKey retrieves stellar's public key from environment.
- func publicKey() (key string) {
- key = os.Getenv("STELLAR_PUBLIC_KEY")
- return
- }
- // lastTransactionID finds the latest effect at startup.
- // Used to only process newer effects.
- func lastTransactionID() (id string) {
- resp, err := http.Get("https://horizon.stellar.org/accounts/" + publicKey() + "/effects?order=desc")
- if err != nil {
- log.Fatal("Can't find latest id: GET error")
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- response := HorizonResponse{}
- err = json.Unmarshal(body, &response)
- if err != nil {
- log.Fatal("Can't find latest id: can't decode json")
- }
- id = response.Embedded.Records[0].PagingToken
- return
- }
- // checkNewTrades find any new trade since last check.
- func checkNewTrades(cursor string) (trades []Effect, lastId string, any bool) {
- trades = make([]Effect, 0)
- lastId = cursor
- resp, err := http.Get("https://horizon.stellar.org/accounts/" + publicKey() + "/effects?cursor=" + cursor)
- if err != nil {
- log.Printf("Can't find new effects: GET error")
- return
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- response := HorizonResponse{}
- err = json.Unmarshal(body, &response)
- if err != nil {
- log.Printf("Can't find new effects: can't decode json")
- return
- }
- if len(response.Embedded.Records) > 0 {
- for _, effect := range response.Embedded.Records {
- if effect.Type == "trade" {
- trades = append(trades, effect)
- }
- }
- lastId = response.Embedded.Records[len(response.Embedded.Records)-1].PagingToken
- any = true
- }
- return
- }
- // notify sends notice about any new trade to slack.
- func notify(trades []Effect) {
- for _, trade := range trades {
- if trade.BoughtAssetCode == "" {
- trade.BoughtAssetCode = "XLM"
- }
- if trade.SoldAssetCode == "" {
- trade.SoldAssetCode = "XLM"
- }
- webhookUrl := os.Getenv("SDEX_SLACK_HOOK")
- msg := fmt.Sprintf("Traded %s%s for %s%s\n", trade.SoldAmount, trade.SoldAssetCode, trade.BoughtAmount, trade.BoughtAssetCode)
- payload := slack.Payload{
- Text: msg,
- Username: "horizon",
- Channel: "#sdex",
- IconEmoji: ":monkey_face:",
- }
- err := slack.Send(webhookUrl, "", payload)
- if len(err) > 0 {
- fmt.Printf("error: %s\n", err)
- }
- }
- }
- func main() {
- startAt := lastTransactionID()
- for {
- <-time.After(1 * time.Minute)
- var (
- trades []Effect
- any bool
- )
- trades, startAt, any = checkNewTrades(startAt)
- if any {
- notify(trades)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement