Skip to content

Commit

Permalink
Merge branch 'fix/new-rfq' of https://github.com/synapsecns/sanguine
Browse files Browse the repository at this point in the history
…into fix/new-rfq
  • Loading branch information
trajan0x committed Dec 13, 2023
2 parents 9ee1656 + 7a46a5f commit da8b568
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
8 changes: 4 additions & 4 deletions services/rfq/api/db/api_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/shopspring/decimal"
)

type QuoteModel struct {
type Quote struct {
// ID is the unique identifier saved of each quote provided
ID uint64 `gorm:"column:id;primaryKey;"`
// DestChainID is the chain which the relayer is willing to provide liquidity for
Expand All @@ -18,19 +18,19 @@ type QuoteModel struct {
// Price is the price per origin token provided for which a relayer is indicating willingness to relay
Price decimal.Decimal `gorm:"column:price"`
// UpdatedAt is the time that the quote was last upserted
UpdatedAt time.Time `gorm:"column:updated_at"`
UpdatedAt time.Time
}

// ApiDBReader is the interface for reading from the database.
type ApiDBReader interface {
// GetQuote gets a quote from the database.
GetQuotesByDestChainAndToken(destChainId uint64, destTokenAddr string) ([]*QuoteModel, error)
GetQuotesByDestChainAndToken(destChainId uint64, destTokenAddr string) ([]*Quote, error)
}

// ApiDBWriter is the interface for writing to the database.
type ApiDBWriter interface {
// UpsertQuote upserts a quote in the database.
UpsertQuote(quote *QuoteModel) error
UpsertQuote(quote *Quote) error
}

// ApiDB is the interface for the database service.
Expand Down
9 changes: 3 additions & 6 deletions services/rfq/api/db/api_db_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package db_test

import (
"time"

"github.com/shopspring/decimal"
"github.com/synapsecns/sanguine/services/rfq/api/db"
)

func (d *DBSuite) TestGetQuotesByDestChainAndToken() {
d.RunOnAllDBs(func(testDB db.ApiDB) {
// Arrange: Create and insert a quote
expectedQuote := &db.QuoteModel{
expectedQuote := &db.Quote{
// Initialize fields like ID, DestChainID, DestTokenAddr, etc.
ID: 1,
DestChainID: 2,
DestTokenAddr: "0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE",
DestAmount: decimal.NewFromInt(1000),
Price: decimal.NewFromFloat(0.01),
UpdatedAt: time.Now(),
}
err := testDB.UpsertQuote(expectedQuote)
d.Require().NoError(err)
Expand All @@ -29,21 +26,21 @@ func (d *DBSuite) TestGetQuotesByDestChainAndToken() {
// Assert: Check if the retrieved quotes match the inserted quote
d.Len(quotes, 1)
d.Equal(expectedQuote.ID, quotes[0].ID)
d.NotEqual(quotes[0].UpdatedAt, nil)
// Continue asserting other fields
})
}

func (d *DBSuite) TestUpsertQuote() {
d.RunOnAllDBs(func(testDB db.ApiDB) {
// Arrange: Create a quote
quote := &db.QuoteModel{
quote := &db.Quote{
// Initialize fields like ID, DestChainID, DestTokenAddr, etc.
ID: 1,
DestChainID: 2,
DestTokenAddr: "0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE",
DestAmount: decimal.NewFromInt(1000),
Price: decimal.NewFromFloat(0.01),
UpdatedAt: time.Now(),
}

// Act & Assert: Insert new quote
Expand Down
2 changes: 1 addition & 1 deletion services/rfq/api/db/sql/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s Store) DB() *gorm.DB {
// GetAllModels gets all models to migrate.
// see: https://medium.com/@SaifAbid/slice-interfaces-8c78f8b6345d for an explanation of why we can't do this at initialization time
func GetAllModels() (allModels []interface{}) {
allModels = append(allModels, &db.QuoteModel{})
allModels = append(allModels, &db.Quote{})
return allModels
}

Expand Down
8 changes: 4 additions & 4 deletions services/rfq/api/db/sql/base/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (
"gorm.io/gorm"
)

func (s *Store) GetQuotesByDestChainAndToken(destChainId uint64, destTokenAddr string) ([]*db.QuoteModel, error) {
var quotes []*db.QuoteModel
func (s *Store) GetQuotesByDestChainAndToken(destChainId uint64, destTokenAddr string) ([]*db.Quote, error) {
var quotes []*db.Quote
result := s.db.Where("dest_chain_id = ? AND token = ?", destChainId, destTokenAddr).Find(&quotes)
if result.Error != nil {
return nil, result.Error
}
return quotes, nil
}

func (s *Store) UpsertQuote(quote *db.QuoteModel) error {
var existingQuote db.QuoteModel
func (s *Store) UpsertQuote(quote *db.Quote) error {
var existingQuote db.Quote
result := s.db.First(&existingQuote, quote.ID)

if result.Error == gorm.ErrRecordNotFound {
Expand Down

0 comments on commit da8b568

Please sign in to comment.