Skip to content

Commit

Permalink
deposit via fiat added
Browse files Browse the repository at this point in the history
  • Loading branch information
TatarinovIgor committed Feb 13, 2024
1 parent 2049b50 commit a9cbb1f
Show file tree
Hide file tree
Showing 15 changed files with 963 additions and 41 deletions.
33 changes: 33 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM golang:1.20-alpine3.17 as builder

RUN apk update && \
apk add --no-cache file build-base

WORKDIR /go/src/app

COPY . .
RUN mkdir /app

RUN go mod download
RUN CGO_ENABLED=1 GOPROXY=direct go build -o /app/birdhouse -mod=mod ./cmd/main.go

# deploy-stage
FROM alpine:latest

RUN apk update && \
apk add --no-cache curl bash

RUN mkdir /key

WORKDIR /app

COPY --from=builder /app ./


RUN apk update && \
apk add --no-cache curl bash

HEALTHCHECK --interval=30s --start-period=1m --timeout=30s --retries=3 \
CMD curl --silent --fail --fail-early http://127.0.0.1:$PORT/docs || exit 1
EXPOSE $PORT
ENTRYPOINT ["/app/birdhouse"]
49 changes: 33 additions & 16 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package main

import (
"birdhouse/internal"
"birdhouse/modules/routing"
"birdhouse/modules/service"
"birdhouse/modules/storage"
stream_service "birdhouse/modules/stream-service"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/google/uuid"
"github.com/julienschmidt/httprouter"
"github.com/oklog/run"
"log"
"net/http"
"os"
"strconv"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/google/uuid"
"github.com/julienschmidt/httprouter"
"github.com/oklog/run"

"birdhouse/modules/routing"
"birdhouse/modules/service"
telegramservice "birdhouse/modules/telegram-service"
)

// @title Example API
Expand All @@ -27,7 +26,8 @@ import (
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT env variable must be set")
port = "8081"
log.Fatal("$PORT env was not found, running default at 8081")
}
basePath := os.Getenv("BASE_PATH")
if basePath == "" {
Expand Down Expand Up @@ -59,11 +59,11 @@ func main() {
}
walletUrl := os.Getenv("WALLET_URL")
if walletUrl == "" {
log.Fatal("$SEED env variable must be set")
log.Fatal("$WALLET_URL env variable must be set")
}
walletKey := os.Getenv("WALLET_KEY")
if walletKey == "" {
log.Fatal("$SEED env variable must be set")
log.Fatal("$WALLET_KEY env variable must be set")
}
publicKey, err := os.ReadFile(publicKeyPath)
if err != nil {
Expand All @@ -79,14 +79,21 @@ func main() {
log.Fatalf("incorrect $APP_GUID env variable: %s, error: %v", appGUIDStr, err)
}

bot, err := tgbotapi.NewBotAPI(telegramBotToken)
//bot, err := tgbotapi.NewBotAPI(telegramBotToken)
if err != nil {
log.Panic(err)
}

atWalletService := service.NewATWalletService(basePath, seed, pub, appGUID, tokenTimeToLive)
db := internal.DBConnect()
store, err := storage.NewKeys("wallets", db)
if err != nil {
log.Fatalf("could not make store for Wallets, error: %v", err)
}

atWalletService := service.NewATWalletService(basePath, seed, pub, appGUID, tokenTimeToLive, store)
//telegramService := telegramservice.NewTelegramService(bot, atWalletService, walletUrl, walletKey)

telegramService := telegramservice.NewTelegramService(bot, atWalletService, walletUrl, walletKey)
//db := internal.DBConnect()

router := httprouter.New()
urlPath := ""
Expand All @@ -99,12 +106,22 @@ func main() {
{
g.Add(func() error {
fmt.Println("telegram service starting")
telegramService.ListenAndServe()
//telegramService.ListenAndServe()
return nil
}, func(err error) {
fmt.Println("telegram service stopping")
})
}
// Stream transactions
{
g.Add(func() error {
fmt.Println("stream service starting")
stream_service.ListenAndServe()
return nil
}, func(err error) {
fmt.Println("stream service stopping")
})
}
// REST API
{
g.Add(func() error {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ module birdhouse
go 1.18

require (
github.com/BurntSushi/toml v1.2.1
github.com/go-openapi/runtime v0.24.1
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/google/uuid v1.3.0
github.com/julienschmidt/httprouter v1.3.0
github.com/lestrrat-go/jwx v1.2.25
github.com/lib/pq v1.2.0
github.com/oklog/run v1.1.0
github.com/stellar/go v0.0.0-20220902181744-56a707d71997
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ github.com/lestrrat-go/jwx v1.2.25 h1:tAx93jN2SdPvFn08fHNAhqFJazn5mBBOB8Zli0g0ot
github.com/lestrrat-go/jwx v1.2.25/go.mod h1:zoNuZymNl5lgdcu6P7K6ie2QRll5HVfF4xwxBBK1NxY=
github.com/lestrrat-go/option v1.0.0 h1:WqAWL8kh8VcSoD6xjSH34/1m8yxluXQbDeKNfvFeEO4=
github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
Expand Down Expand Up @@ -167,8 +169,6 @@ github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stellar/go v0.0.0-20220817094308-f4b20d89922e h1:0XKq3deV5eosPgn5N8oR9jjoMnaKxDGGActuvaWqwyQ=
github.com/stellar/go v0.0.0-20220817094308-f4b20d89922e/go.mod h1:QXwuKmYVvqQZlByv0EeNb0Rgog9AP+eMmARcdt3h2rI=
github.com/stellar/go v0.0.0-20220902181744-56a707d71997 h1:7w49Wl1uDkLfgIMVLjcJxnbTv3FAxO946oKVS74Vs4c=
github.com/stellar/go v0.0.0-20220902181744-56a707d71997/go.mod h1:QXwuKmYVvqQZlByv0EeNb0Rgog9AP+eMmARcdt3h2rI=
github.com/stellar/go-xdr v0.0.0-20211103144802-8017fc4bdfee h1:fbVs0xmXpBvVS4GBeiRmAE3Le70ofAqFMch1GTiq/e8=
Expand Down
75 changes: 75 additions & 0 deletions internal/contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package internal

import (
"log"
"os"
"strconv"
)

// MustString func returns environment variable value as a string value,
// If variable doesn't exist or is not set, exits from the runtime
func MustString(key string) string {
value, exists := os.LookupEnv(key)
if !exists {
log.Fatalf("required ENV %q is not set", key)
}
if value == "" {
log.Fatalf("required ENV %q is empty", key)
}
return value
}

// GetString func returns environment variable value as a string value,
// If variable doesn't exist or is not set, returns fallback value
func GetString(key string, fallback string) string {
value, exists := os.LookupEnv(key)
if !exists {
return fallback
}
return value
}

// MustInt func returns environment variable value as an integer value,
// If variable doesn't exist or is not set, exits from the runtime
func MustInt(key string) int {
value, exists := os.LookupEnv(key)
if !exists {
log.Fatalf("required ENV %q is not set", key)
}
if value == "" {
log.Fatalf("required ENV %q is empty", key)
}
res, err := strconv.ParseInt(value, 10, 32)
if err != nil {
log.Fatalf("required ENV %q must be a number but it's %q", key, value)
}
return int(res)
}

// GetInt func returns environment variable value as a integer value,
// If variable doesn't exist or is not set, returns fallback value
func GetInt(key string, fallback int) int {
value, exists := os.LookupEnv(key)
if !exists {
return fallback
}
res, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return fallback
}
return int(res)
}

// GetFloat func returns environment variable value as a integer value,
// If variable doesn't exist or is not set, returns fallback value
func GetFloat(key string, fallback int) int {
value, exists := os.LookupEnv(key)
if !exists {
return fallback
}
res, err := strconv.ParseFloat(value, 64)
if err != nil {
return fallback
}
return int(res)
}
39 changes: 39 additions & 0 deletions internal/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package internal

import (
"database/sql"
"fmt"
"log"
)

// DBConnect inits database connection
func DBConnect() *sql.DB {
// db connection env variables
var (
dbHost = MustString("DATABASE_HOST")
dbPort = MustInt("DATABASE_PORT")
dbName = MustString("DATABASE_NAME")
dbUsername = MustString("DATABASE_USER")
dbPassword = MustString("DATABASE_PASS")
dbTimeout = GetInt("DATABASE_CONNECT_TIMEOUT", 15)
dbSSLMode = GetString("DATABASE_SSL_MODE", "disable")
)

dbConnStr := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s connect_timeout=%d",
dbHost, dbPort, dbUsername, dbPassword, dbName, dbSSLMode, dbTimeout,
)
db, err := sql.Open("postgres", dbConnStr)
if err != nil {
log.Fatalf("could not connect to database: %v", err)
}

db.SetMaxOpenConns(50)
db.SetMaxIdleConns(0)

if err := db.Ping(); err != nil {
log.Fatalf("database is not available right now: %v", err)
}

return db
}
28 changes: 8 additions & 20 deletions modules/handler/fpf-payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,30 @@ package handler

import (
"birdhouse/modules/service"
"fmt"
"github.com/google/uuid"
"encoding/json"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
"strconv"
)

func MakeFPFLinkForWallet(atWallet *service.ATWalletService, isDeposit bool) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
jwtToken := r.URL.Query().Get("auth_key")
amount, _ := strconv.ParseFloat(r.URL.Query().Get("amount"), 64)
account := r.URL.Query().Get("acc_guid")
accountGUID, err := uuid.Parse(account)
var DepositRequest service.DepositRequest
err := json.NewDecoder(r.Body).Decode(&DepositRequest)
if err != nil {
http.Error(w, "could not parse request data", http.StatusBadRequest)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
token, err := atWallet.SignIn(jwtToken)
deposit, err := atWallet.CreateStellarDeposit(DepositRequest.ExternalID, DepositRequest.MerchantID, DepositRequest.Blockchain)
if err != nil {
log.Println(err)
http.Error(w, "could not get user data", http.StatusForbidden)
http.Error(w, "could not perform deposit", http.StatusBadRequest)
return
}
payment, err := atWallet.FPFPayment(jwtToken, token.AccessToken,
"ATUSD", "GBT4VVTDPCNA45MNWX5G6LUTLIEENSTUHDVXO2AQHAZ24KUZUPLPGJZH",
accountGUID, amount, isDeposit)
err = json.NewEncoder(w).Encode(deposit)
if err != nil {
log.Println(err)
http.Error(w, "error connecting to payment processor server", http.StatusFailedDependency)
return
}
_, err = fmt.Fprintf(w, "%s", payment.Action.Action)
if err != nil {
log.Println(err)
http.Error(w, "could not log the transaction", http.StatusInternalServerError)
http.Error(w, "could not parse response from server", http.StatusInternalServerError)
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func InitRouter(router *httprouter.Router, pathName string, atWallet *service.AT
routerWrap.GET("/deposit_wallet_link", middleware.AuthMiddleware(atWallet, handler.MakeFPFLinkForWallet(atWallet, true)))
routerWrap.GET("/withdraw_wallet_link", middleware.AuthMiddleware(atWallet, handler.MakeFPFLinkForWallet(atWallet, false)))
routerWrap.GET("/get_balance", middleware.AuthMiddleware(atWallet, handler.MakeGetBalance(atWallet)))
routerWrap.GET("/deposit", middleware.AuthMiddleware(atWallet, handler.MakeFPFLinkForWallet(atWallet, true)))
routerWrap.GET("/deposit", handler.MakeFPFLinkForWallet(atWallet, true))
routerWrap.GET("/withdraw", middleware.AuthMiddleware(atWallet, handler.MakeFPFLinkForWallet(atWallet, false)))
routerWrap.GET("/transfer/deposit", middleware.AuthMiddleware(atWallet, handler.TransferDeposit(atWallet)))
routerWrap.GET("/transfer/withdraw", middleware.AuthMiddleware(atWallet, handler.TransferWithdraw(atWallet)))
Expand Down
8 changes: 8 additions & 0 deletions modules/service/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ type TokenData struct {

type UserData struct {
ExternalID string `json:"external_id"`
MerchantID string `json:"merchant_id"`
FirsName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
Phone string `json:"phone"`
}

type DepositRequest struct {
ExternalID string `json:"external_id"`
MerchantID string `json:"merchant_id"`
Blockchain string `json:"blockchain"`
}

type Asset struct {
Platform string `json:"platform"`
Code string `json:"code"`
Expand Down
Loading

0 comments on commit a9cbb1f

Please sign in to comment.