Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions ocp/balance/calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (

commonpb "github.com/code-payments/ocp-protobuf-api/generated/go/common/v1"

currency_lib "github.com/code-payments/ocp-server/currency"
"github.com/code-payments/ocp-server/ocp/common"
ocp_data "github.com/code-payments/ocp-server/ocp/data"
"github.com/code-payments/ocp-server/ocp/data/account"
"github.com/code-payments/ocp-server/ocp/data/action"
"github.com/code-payments/ocp-server/ocp/data/deposit"
"github.com/code-payments/ocp-server/ocp/data/intent"
"github.com/code-payments/ocp-server/ocp/data/transaction"
currency_lib "github.com/code-payments/ocp-server/currency"
timelock_token_v1 "github.com/code-payments/ocp-server/solana/timelock/v1"
"github.com/code-payments/ocp-server/testutil"
)
Expand Down Expand Up @@ -436,10 +436,9 @@ func setupBalanceTestData(t *testing.T, env balanceTestEnv, data *balanceTestDat
// There's no intent, so we have an external deposit
if len(txn.intentID) == 0 && txn.transactionState != transaction.ConfirmationUnknown {
depositRecord := &deposit.Record{
Signature: fmt.Sprintf("txn%d", i),
Destination: txn.destination.PublicKey().ToBase58(),
Amount: txn.quantity,
UsdMarketValue: 1.0,
Signature: fmt.Sprintf("txn%d", i),
Destination: txn.destination.PublicKey().ToBase58(),
Amount: txn.quantity,

Slot: 12345,
ConfirmationState: txn.transactionState,
Expand Down
22 changes: 0 additions & 22 deletions ocp/data/deposit/memory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,12 @@ func (s *store) GetQuarkAmountBatch(_ context.Context, accounts ...string) (map[
return res, nil
}

// GetUsdAmount implements deposit.Store.GetUsdAmount
func (s *store) GetUsdAmount(ctx context.Context, account string) (float64, error) {
s.mu.Lock()
defer s.mu.Unlock()

return s.getUsdAmount(account), nil
}

func (s *store) getQuarkAmount(account string) uint64 {
items := s.findByDestination(account)
items = s.filterFinalized(items)
return s.sumAmounts(items)
}

func (s *store) getUsdAmount(account string) float64 {
items := s.findByDestination(account)
items = s.filterFinalized(items)
return s.sumUsd(items)
}

func (s *store) find(data *deposit.Record) *deposit.Record {
for _, item := range s.records {
if item.Id == data.Id {
Expand Down Expand Up @@ -155,14 +141,6 @@ func (s *store) sumAmounts(items []*deposit.Record) uint64 {
return res
}

func (s *store) sumUsd(items []*deposit.Record) float64 {
var res float64
for _, item := range items {
res += item.UsdMarketValue
}
return res
}

func (s *store) reset() {
s.mu.Lock()
defer s.mu.Unlock()
Expand Down
54 changes: 15 additions & 39 deletions ocp/data/deposit/postgres/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"github.com/jmoiron/sqlx"

pgutil "github.com/code-payments/ocp-server/database/postgres"
"github.com/code-payments/ocp-server/ocp/data/deposit"
"github.com/code-payments/ocp-server/ocp/data/transaction"
pgutil "github.com/code-payments/ocp-server/database/postgres"
)

const (
Expand All @@ -21,10 +21,9 @@ const (
type model struct {
Id sql.NullInt64 `db:"id"`

Signature string `db:"signature"`
Destination string `db:"destination"`
Amount uint64 `db:"amount"`
UsdMarketValue float64 `db:"usd_market_value"`
Signature string `db:"signature"`
Destination string `db:"destination"`
Amount uint64 `db:"amount"`

Slot uint64 `db:"slot"`
ConfirmationState int `db:"confirmation_state"`
Expand All @@ -38,10 +37,9 @@ func toModel(obj *deposit.Record) (*model, error) {
}

return &model{
Signature: obj.Signature,
Destination: obj.Destination,
Amount: obj.Amount,
UsdMarketValue: obj.UsdMarketValue,
Signature: obj.Signature,
Destination: obj.Destination,
Amount: obj.Amount,

Slot: obj.Slot,
ConfirmationState: int(obj.ConfirmationState),
Expand All @@ -54,10 +52,9 @@ func fromModel(obj *model) *deposit.Record {
return &deposit.Record{
Id: uint64(obj.Id.Int64),

Signature: obj.Signature,
Destination: obj.Destination,
Amount: obj.Amount,
UsdMarketValue: obj.UsdMarketValue,
Signature: obj.Signature,
Destination: obj.Destination,
Amount: obj.Amount,

Slot: obj.Slot,
ConfirmationState: transaction.Confirmation(obj.ConfirmationState),
Expand All @@ -68,15 +65,15 @@ func fromModel(obj *model) *deposit.Record {

func (m *model) dbSave(ctx context.Context, db *sqlx.DB) error {
query := `INSERT INTO ` + tableName + `
(signature, destination, amount, usd_market_value, slot, confirmation_state, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
(signature, destination, amount, slot, confirmation_state, created_at)
VALUES ($1, $2, $3, $4, $5, $6)

ON CONFLICT(signature, destination)
DO UPDATE
SET slot = $5, confirmation_state = $6
SET slot = $4, confirmation_state = $5
WHERE ` + tableName + `.signature = $1 AND ` + tableName + `.destination = $2

RETURNING id, signature, destination, amount, usd_market_value, slot, confirmation_state, created_at`
RETURNING id, signature, destination, amount, slot, confirmation_state, created_at`

if m.CreatedAt.IsZero() {
m.CreatedAt = time.Now()
Expand All @@ -88,7 +85,6 @@ func (m *model) dbSave(ctx context.Context, db *sqlx.DB) error {
m.Signature,
m.Destination,
m.Amount,
m.UsdMarketValue,
m.Slot,
m.ConfirmationState,
m.CreatedAt,
Expand All @@ -98,7 +94,7 @@ func (m *model) dbSave(ctx context.Context, db *sqlx.DB) error {
func dbGet(ctx context.Context, db *sqlx.DB, signature, account string) (*model, error) {
var res model

query := `SELECT id, signature, destination, amount, usd_market_value, slot, confirmation_state, created_at FROM ` + tableName + `
query := `SELECT id, signature, destination, amount, slot, confirmation_state, created_at FROM ` + tableName + `
WHERE signature = $1 AND destination = $2
`

Expand Down Expand Up @@ -167,23 +163,3 @@ func dbGetQuarkAmountBatch(ctx context.Context, db *sqlx.DB, accounts ...string)
}
return res, nil
}

func dbGetUsdAmount(ctx context.Context, db *sqlx.DB, account string) (float64, error) {
var res sql.NullFloat64

query := `SELECT SUM(usd_market_value) FROM ` + tableName + `
WHERE destination = $1 AND confirmation_state = $2
`

err := pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
return db.GetContext(ctx, &res, query, account, transaction.ConfirmationFinalized)
})
if err != nil {
return 0, err
}

if !res.Valid {
return 0, nil
}
return res.Float64, nil
}
5 changes: 0 additions & 5 deletions ocp/data/deposit/postgres/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,3 @@ func (s *store) GetQuarkAmount(ctx context.Context, account string) (uint64, err
func (s *store) GetQuarkAmountBatch(ctx context.Context, accounts ...string) (map[string]uint64, error) {
return dbGetQuarkAmountBatch(ctx, s.db, accounts...)
}

// GetUsdAmount implements deposit.Store.GetUsdAmount
func (s *store) GetUsdAmount(ctx context.Context, account string) (float64, error) {
return dbGetUsdAmount(ctx, s.db, account)
}
1 change: 0 additions & 1 deletion ocp/data/deposit/postgres/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const (
signature TEXT NOT NULL,
destination TEXT NOT NULL,
amount BIGINT NOT NULL CHECK (amount > 0),
usd_market_value NUMERIC(18, 9) NOT NULL,

slot BIGINT NOT NULL,
confirmation_state INTEGER NOT NULL,
Expand Down
23 changes: 6 additions & 17 deletions ocp/data/deposit/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ var (
type Record struct {
Id uint64

Signature string
Destination string
Amount uint64
UsdMarketValue float64
Signature string
Destination string
Amount uint64

Slot uint64
ConfirmationState transaction.Confirmation
Expand All @@ -40,10 +39,6 @@ type Store interface {

// GetQuarkAmountBatch is like GetQuarkAmount but for a batch of accounts
GetQuarkAmountBatch(ctx context.Context, accounts ...string) (map[string]uint64, error)

// GetUsdAmount gets the total deposited USD amount to an account for finalized
// transactions
GetUsdAmount(ctx context.Context, account string) (float64, error)
}

func (r *Record) Validate() error {
Expand All @@ -59,10 +54,6 @@ func (r *Record) Validate() error {
return errors.New("amount is required")
}

if r.UsdMarketValue <= 0 {
return errors.New("usd market value must be positive")
}

if r.ConfirmationState == transaction.ConfirmationUnknown {
return errors.New("confirmation state is required")
}
Expand All @@ -78,10 +69,9 @@ func (r *Record) Clone() Record {
return Record{
Id: r.Id,

Signature: r.Signature,
Destination: r.Destination,
Amount: r.Amount,
UsdMarketValue: r.UsdMarketValue,
Signature: r.Signature,
Destination: r.Destination,
Amount: r.Amount,

Slot: r.Slot,
ConfirmationState: r.ConfirmationState,
Expand All @@ -96,7 +86,6 @@ func (r *Record) CopyTo(dst *Record) {
dst.Signature = r.Signature
dst.Destination = r.Destination
dst.Amount = r.Amount
dst.UsdMarketValue = r.UsdMarketValue

dst.Slot = r.Slot
dst.ConfirmationState = r.ConfirmationState
Expand Down
24 changes: 5 additions & 19 deletions ocp/data/deposit/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func testRoundTrip(t *testing.T, s deposit.Store) {
Signature: "txn",
Destination: "destination",
Amount: 1,
UsdMarketValue: 1.23,
Slot: 0,
ConfirmationState: transaction.ConfirmationConfirmed,
}
Expand Down Expand Up @@ -78,16 +77,12 @@ func testGetAmounts(t *testing.T, s deposit.Store) {
assert.EqualValues(t, 0, quarksByAccount[destination1])
assert.EqualValues(t, 0, quarksByAccount[destination2])

usd, err := s.GetUsdAmount(ctx, destination1)
require.NoError(t, err)
assert.EqualValues(t, 0, usd)

records := []*deposit.Record{
{Signature: "txn1", Destination: destination1, Amount: 1, UsdMarketValue: 2, Slot: 12345, ConfirmationState: transaction.ConfirmationConfirmed},
{Signature: "txn2", Destination: destination1, Amount: 10, UsdMarketValue: 20, Slot: 12345, ConfirmationState: transaction.ConfirmationFailed},
{Signature: "txn3", Destination: destination1, Amount: 100, UsdMarketValue: 200, Slot: 12345, ConfirmationState: transaction.ConfirmationFinalized},
{Signature: "txn4", Destination: destination1, Amount: 1000, UsdMarketValue: 2000, Slot: 12345, ConfirmationState: transaction.ConfirmationFinalized},
{Signature: "txn1", Destination: destination2, Amount: 10000, UsdMarketValue: 20000, Slot: 12345, ConfirmationState: transaction.ConfirmationFinalized},
{Signature: "txn1", Destination: destination1, Amount: 1, Slot: 12345, ConfirmationState: transaction.ConfirmationConfirmed},
{Signature: "txn2", Destination: destination1, Amount: 10, Slot: 12345, ConfirmationState: transaction.ConfirmationFailed},
{Signature: "txn3", Destination: destination1, Amount: 100, Slot: 12345, ConfirmationState: transaction.ConfirmationFinalized},
{Signature: "txn4", Destination: destination1, Amount: 1000, Slot: 12345, ConfirmationState: transaction.ConfirmationFinalized},
{Signature: "txn1", Destination: destination2, Amount: 10000, Slot: 12345, ConfirmationState: transaction.ConfirmationFinalized},
}
for _, record := range records {
require.NoError(t, s.Save(ctx, record))
Expand All @@ -109,22 +104,13 @@ func testGetAmounts(t *testing.T, s deposit.Store) {
require.Len(t, quarksByAccount, 2)
assert.EqualValues(t, 1100, quarksByAccount[destination1])
assert.EqualValues(t, 10000, quarksByAccount[destination2])

usd, err = s.GetUsdAmount(ctx, destination1)
require.NoError(t, err)
assert.EqualValues(t, 2200, usd)

usd, err = s.GetUsdAmount(ctx, destination2)
require.NoError(t, err)
assert.EqualValues(t, 20000, usd)
})
}

func assertEquivalentRecords(t *testing.T, obj1, obj2 *deposit.Record) {
assert.Equal(t, obj1.Signature, obj2.Signature)
assert.Equal(t, obj1.Destination, obj2.Destination)
assert.Equal(t, obj1.Amount, obj2.Amount)
assert.Equal(t, obj1.UsdMarketValue, obj2.UsdMarketValue)
assert.Equal(t, obj1.Slot, obj2.Slot)
assert.Equal(t, obj1.ConfirmationState, obj2.ConfirmationState)
}
Loading
Loading