-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
65 lines (49 loc) · 1.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/netbound/dex-feed/db"
uniswapv3 "github.com/netbound/dex-feed/exchange/uniswap_v3"
"github.com/netbound/dex-feed/token"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/gin-gonic/gin"
)
func main() {
var (
factoryAddress = common.HexToAddress("0x1F98431c8aD98523631AE4a59f267346ea31F984")
rpcApi = "http://localhost:8080/eth"
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
client, err := ethclient.DialContext(ctx, rpcApi)
if err != nil {
log.Fatal(err)
}
dbOpts := db.Opts{Persistent: true, CacheSize: 2048}
tdb := token.NewTokenDB(client, dbOpts)
univ3 := uniswapv3.New(client, tdb, factoryAddress, dbOpts)
router := gin.Default()
router.Use(gin.Logger())
router.GET("/price/:base", func(c *gin.Context) {
base := common.HexToAddress(c.Param("base"))
quote := common.HexToAddress(c.Query("quote"))
fee, err := strconv.ParseInt(c.Query("fee"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fmt.Println(base, quote, fee)
price, err := univ3.GetPrice(c.Request.Context(), quote, base, fee)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"price": price})
})
router.Run(":3000")
}