Skip to content

Commit

Permalink
mock db
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed May 16, 2024
1 parent da4c380 commit 2f567bd
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 308 deletions.
103 changes: 0 additions & 103 deletions conn.go

This file was deleted.

29 changes: 0 additions & 29 deletions conn_test.go

This file was deleted.

21 changes: 20 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@ module github.com/gnoswap-labs/vwap

go 1.22.2

require github.com/stretchr/testify v1.9.0
require (
github.com/bxcodec/faker/v3 v3.8.1
github.com/go-sql-driver/mysql v1.8.1
github.com/stretchr/testify v1.9.0
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.10
)

require (
github.com/alecthomas/colour v0.1.0 // indirect
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/alecthomas/assert v1.0.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
41 changes: 40 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/alecthomas/assert v1.0.0 h1:3XmGh/PSuLzDbK3W2gUbRXwgW5lqPkuqvRgeQ30FI5o=
github.com/alecthomas/assert v1.0.0/go.mod h1:va/d2JC+M7F6s+80kl/R3G7FUiW6JzUO+hPhLyJ36ZY=
github.com/alecthomas/colour v0.1.0 h1:nOE9rJm6dsZ66RGWYSFrXw461ZIt9A6+nHgL7FRrDUk=
github.com/alecthomas/colour v0.1.0/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/bxcodec/faker/v3 v3.8.1 h1:qO/Xq19V6uHt2xujwpaetgKhraGCapqY2CRWGD/SqcM=
github.com/bxcodec/faker/v3 v3.8.1/go.mod h1:DdSDccxF5msjFo5aO4vrobRQ8nIApg8kq3QWPEQD6+o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E=
gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
49 changes: 27 additions & 22 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@ package main
import (
"fmt"
"log"
"time"

"github.com/gnoswap-labs/vwap"
)

// testing purposes
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

func main() {
start := time.Now().Unix()
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()

for range ticker.C {
<-ticker.C
vwapResults, err := vwap.VWAP()
if err != nil {
log.Printf("Failed to calculate VWAP: %v", err)
continue
}

fmt.Println("VWAP Results:")
for token, vwap := range vwapResults {
fmt.Printf("%s: %.8f\n", token, vwap)
}

fmt.Println(time.Now().Unix() - start)
fmt.Println("--------------------")
db, err := gorm.Open(sqlite.Open("vwap.db"), &gorm.Config{})
if err != nil {
log.Fatalf("failed to connect database: %v", err)
}

err = db.AutoMigrate(&vwap.VWAPData{})
if err != nil {
log.Fatalf("failed to migrate database: %v", err)
}

err = vwap.PopulateVWAPData(db, 10)
if err != nil {
log.Fatalf("failed to populate vwap data: %v", err)
}

var VWAPDataList []vwap.VWAPData
result := db.Find(&VWAPDataList)
if result.Error != nil {
log.Fatalf("failed to retrieve vwap data: %v", result.Error)
}

for _, VWAPData := range VWAPDataList {
fmt.Printf("TokenName: %s, VWAP: %.2f, TotalVolume: %.2f, CalculatedAt: %s\n",
VWAPData.TokenName, VWAPData.VWAP, VWAPData.TotalVolume, VWAPData.CalculatedAt)
}
}
84 changes: 51 additions & 33 deletions store.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,65 @@
package vwap

const interval = 600
import (
"fmt"
"math/rand"
"time"

"github.com/bxcodec/faker/v3"
_ "github.com/go-sql-driver/mysql"
"gorm.io/gorm"
)

/* Schema:
* CREATE TABLE vwap_data (
* id SERIAL PRIMARY KEY,
* token_name VARCHAR(50) NOT NULL,
* calculated_at TIMESTAMP NOT NULL DEFAULT NOW(),
* vwap DECIMAL(20, 10) NOT NULL,
* total_volume DECIMAL(20, 10) NOT NULL
* );
*/

// VWAPData represents the specific token's VWAP data.
type VWAPData struct {
TokenName string `json:"token_name"` // VWAP data belongs token name
VWAP float64 `json:"vwap"` // calculated VWAP value
Timestamp int `json:"timestamp"` // timestamp of the VWAP value (UNIX, 10 min interval)
gorm.Model
TokenName string
VWAP float64
TotalVolume float64
CalculatedAt time.Time
}

var vwapDataMap map[string][]VWAPData
func store(db *gorm.DB, tokenName string, vwap, totalVolume float64, calculatedAt time.Time) error {
vwapData := VWAPData{
TokenName: tokenName,
VWAP: vwap,
TotalVolume: totalVolume,
CalculatedAt: calculatedAt,
}

result := db.Create(&vwapData)
if result.Error != nil {
return fmt.Errorf("failed to insert data: %v", result.Error)
}

func init() {
vwapDataMap = make(map[string][]VWAPData)
return nil
}

// store stores the VWAP data for the token or updates the existing data.
//
// Parameters:
//
// - tokenName: the token name
// - vwap: the VWAP value to store
// - timestamp: the timestamp of the VWAP value
func store(tokenName string, vwap float64, timestamp int) {
// adjust the timestamp to the 10 minutes interval.
adjustedTimestamp := timestamp - (timestamp % interval)

// get the VWAP data for the token
lst, ok := vwapDataMap[tokenName]
if !ok {
lst = []VWAPData{}
}
// testing purpose only

func PopulateVWAPData(db *gorm.DB, count int) error {
for i := 0; i < count; i++ {
vwapData := VWAPData{
TokenName: faker.Currency(),
VWAP: rand.Float64(),
TotalVolume: rand.Float64(),
CalculatedAt: time.Now().Add(time.Duration(rand.Intn(1000)) * time.Minute),
}

// check last VWAP data for the list
if len(lst) > 0 {
last := lst[len(lst)-1]
if last.Timestamp == adjustedTimestamp {
last.VWAP = vwap
vwapDataMap[tokenName] = lst
return
result := db.Create(&vwapData)
if result.Error != nil {
return fmt.Errorf("failed to insert data: %v", result.Error)
}
}

lst = append(lst, VWAPData{tokenName, vwap, adjustedTimestamp})
vwapDataMap[tokenName] = lst
return nil
}
Loading

0 comments on commit 2f567bd

Please sign in to comment.