Advertisement
FrankDFarrell

main.go

Nov 6th, 2024
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.03 KB | Source Code | 0 0
  1. package main
  2.  
  3. import (
  4.     "context"
  5.     "flag"
  6.     "fmt"
  7.     "log"
  8.  
  9.     "github.com/etherdev12/go-defi/bclient"
  10.     "github.com/etherdev12/go-defi/config"
  11. )
  12.  
  13. var (
  14.     THRESHOLD = flag.Float64("threshold", 0, "Threshold of ETH Price")
  15.     BALANCE   = flag.Float64("balance", 0, "Balance of ETH to Swap")
  16. )
  17.  
  18. func init() {
  19.     flag.Parse()
  20. }
  21.  
  22. func main() {
  23.     ctx, cancel := context.WithCancel(context.Background())
  24.     defer cancel()
  25.  
  26.     cfg := &config.Config{
  27.         Blockchain: config.Blockchain{
  28.             ProviderType: "direct",
  29.             RPC:          "https://eth.llamarpc.com",
  30.         },
  31.     }
  32.     client, err := cfg.EthClient(ctx)
  33.     if err != nil {
  34.         log.Fatal(err)
  35.     }
  36.     bc, err := bclient.NewClient(ctx, client)
  37.     if err != nil {
  38.         log.Fatal(err)
  39.     }
  40.  
  41.     // Get price on uniswap
  42.     price, err := bc.EthDaiPrice()
  43.     if err != nil {
  44.         log.Fatal(err)
  45.     }
  46.     fmt.Println(price)
  47.  
  48.     priceF, _ := price.Float64()
  49.     if priceF > *THRESHOLD && *BALANCE > 0 {
  50.         // Swap ETH into DAI
  51.         fmt.Println("Try to swap", *BALANCE, "ETH into DAI...")
  52.         //
  53.         // Add swap code here
  54.         //
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement