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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
filippo.io/edwards25519 v1.1.0
github.com/aws/aws-sdk-go-v2 v0.17.0
github.com/code-payments/code-vm-indexer v1.2.0
github.com/code-payments/ocp-protobuf-api v1.9.0
github.com/code-payments/ocp-protobuf-api v1.9.3-0.20260428163429-9d7dee8bd6ca
github.com/emirpasic/gods v1.12.0
github.com/envoyproxy/protoc-gen-validate v1.2.1
github.com/golang/protobuf v1.5.4
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@ github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/code-payments/code-vm-indexer v1.2.0 h1:rSHpBMiT9BKgmKcXg/VIoi/h0t7jNxGx07Qz59m+6Q0=
github.com/code-payments/code-vm-indexer v1.2.0/go.mod h1:vn91YN2qNqb+gGJeZe2+l+TNxVmEEiRHXXnIn2Y40h8=
github.com/code-payments/ocp-protobuf-api v1.8.1 h1:IaCVADbbTUtZwf0Rk8Pf8PygsancuOXc+A3CcTG/74w=
github.com/code-payments/ocp-protobuf-api v1.8.1/go.mod h1:tw6BooY5a8l6CtSZnKOruyKII0W04n89pcM4BizrgG8=
github.com/code-payments/ocp-protobuf-api v1.9.0 h1:VpcOENVTmebpTENhpVaDbFfPPliK1zuMtjHzdhBQY2U=
github.com/code-payments/ocp-protobuf-api v1.9.0/go.mod h1:tw6BooY5a8l6CtSZnKOruyKII0W04n89pcM4BizrgG8=
github.com/code-payments/ocp-protobuf-api v1.9.3-0.20260428163429-9d7dee8bd6ca h1:I7k/Rq3OKn+E94zc2GvsUA76e7RVkAqXWT4inUwbtuQ=
github.com/code-payments/ocp-protobuf-api v1.9.3-0.20260428163429-9d7dee8bd6ca/go.mod h1:tw6BooY5a8l6CtSZnKOruyKII0W04n89pcM4BizrgG8=
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6 h1:NmTXa/uVnDyp0TY5MKi197+3HWcnYWfnHGyaFthlnGw=
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand Down
4 changes: 2 additions & 2 deletions ocp/antispam/guard.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ func (g *Guard) AllowDistribution(ctx context.Context, owner *common.Account, is
return allow, nil
}

func (g *Guard) AllowSwap(ctx context.Context, fundingSource swap.FundingSource, owner, fromMint, toMint *common.Account, swapAmount, feeAmount uint64, initializesMint bool) (bool, error) {
func (g *Guard) AllowSwap(ctx context.Context, kind swap.Kind, fundingSource swap.FundingSource, owner, fromMint, toMint *common.Account, swapAmount, feeAmount uint64, initializesMint bool) (bool, error) {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "AllowSwap")
defer tracer.End()

allow, reason, err := g.integration.AllowSwap(ctx, fundingSource, owner, fromMint, toMint, swapAmount, feeAmount, initializesMint)
allow, reason, err := g.integration.AllowSwap(ctx, kind, fundingSource, owner, fromMint, toMint, swapAmount, feeAmount, initializesMint)
if err != nil {
return false, err
}
Expand Down
28 changes: 18 additions & 10 deletions ocp/data/swap/postgres/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const (
type model struct {
Id sql.NullInt64 `db:"id"`
SwapId string `db:"swap_id"`
Kind uint8 `db:"kind"`
Owner string `db:"owner"`
DestinationOwner string `db:"destination_owner"`
FromMint string `db:"from_mint"`
ToMint string `db:"to_mint"`
SwapAmount uint64 `db:"amount"`
Expand Down Expand Up @@ -48,7 +50,9 @@ func toModel(obj *swap.Record) (*model, error) {
return &model{
Id: sql.NullInt64{Int64: int64(obj.Id), Valid: true},
SwapId: obj.SwapId,
Kind: uint8(obj.Kind),
Owner: obj.Owner,
DestinationOwner: obj.DestinationOwner,
FromMint: obj.FromMint,
ToMint: obj.ToMint,
SwapAmount: obj.SwapAmount,
Expand All @@ -70,7 +74,9 @@ func fromModel(m *model) *swap.Record {
return &swap.Record{
Id: uint64(m.Id.Int64),
SwapId: m.SwapId,
Kind: swap.Kind(m.Kind),
Owner: m.Owner,
DestinationOwner: m.DestinationOwner,
FromMint: m.FromMint,
ToMint: m.ToMint,
SwapAmount: m.SwapAmount,
Expand All @@ -91,22 +97,24 @@ func fromModel(m *model) *swap.Record {
func (m *model) dbSave(ctx context.Context, db *sqlx.DB) error {
return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
query := `INSERT INTO ` + tableName + `
(swap_id, owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15 + 1, $16)
(swap_id, kind, owner, destination_owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17 + 1, $18)

ON CONFLICT (swap_id)
DO UPDATE
SET transaction_blob = $13, state = $14, version = ` + tableName + `.version + 1
WHERE ` + tableName + `.swap_id = $1 AND ` + tableName + `.version = $15
SET transaction_blob = $15, state = $16, version = ` + tableName + `.version + 1
WHERE ` + tableName + `.swap_id = $1 AND ` + tableName + `.version = $17

RETURNING
id, swap_id, owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at`
id, swap_id, kind, owner, destination_owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at`

err := tx.QueryRowxContext(
ctx,
query,
m.SwapId,
m.Kind,
m.Owner,
m.DestinationOwner,
m.FromMint,
m.ToMint,
m.SwapAmount,
Expand All @@ -132,7 +140,7 @@ func (m *model) dbSave(ctx context.Context, db *sqlx.DB) error {
func dbGetById(ctx context.Context, db *sqlx.DB, id string) (*model, error) {
res := &model{}

query := `SELECT id, swap_id, owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at
query := `SELECT id, swap_id, kind, owner, destination_owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at
FROM ` + tableName + `
WHERE swap_id = $1
LIMIT 1`
Expand All @@ -147,7 +155,7 @@ func dbGetById(ctx context.Context, db *sqlx.DB, id string) (*model, error) {
func dbGetByFundingId(ctx context.Context, db *sqlx.DB, fundingId string) (*model, error) {
res := &model{}

query := `SELECT id, swap_id, owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at
query := `SELECT id, swap_id, kind, owner, destination_owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at
FROM ` + tableName + `
WHERE funding_id = $1
LIMIT 1`
Expand All @@ -162,7 +170,7 @@ func dbGetByFundingId(ctx context.Context, db *sqlx.DB, fundingId string) (*mode
func dbGetAllByOwnerAndState(ctx context.Context, db *sqlx.DB, owner string, state swap.State) ([]*model, error) {
res := []*model{}

query := `SELECT id, swap_id, owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at
query := `SELECT id, swap_id, kind, owner, destination_owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at
FROM ` + tableName + `
WHERE owner = $1 AND state = $2
ORDER BY id ASC`
Expand All @@ -182,7 +190,7 @@ func dbGetAllByOwnerAndState(ctx context.Context, db *sqlx.DB, owner string, sta
func dbGetAllByOwnerMintAndState(ctx context.Context, db *sqlx.DB, owner string, mint string, state swap.State) ([]*model, error) {
res := []*model{}

query := `SELECT id, swap_id, owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at
query := `SELECT id, swap_id, kind, owner, destination_owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at
FROM ` + tableName + `
WHERE owner = $1 AND state = $2 AND (from_mint = $3 OR to_mint = $3)
ORDER BY id ASC`
Expand All @@ -203,7 +211,7 @@ func dbGetAllByState(ctx context.Context, db *sqlx.DB, state swap.State, cursor
res := []*model{}

query := `SELECT
id, swap_id, owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at
id, swap_id, kind, owner, destination_owner, from_mint, to_mint, amount, fee_amount, funding_id, funding_source, nonce, blockhash, proof_signature, transaction_signature, transaction_blob, state, version, created_at
FROM ` + tableName + `
WHERE state = $1`

Expand Down
3 changes: 3 additions & 0 deletions ocp/data/swap/postgres/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ const (

swap_id TEXT NOT NULL UNIQUE,

kind INTEGER NOT NULL DEFAULT 1,

owner TEXT NOT NULL,
destination_owner TEXT NOT NULL DEFAULT '',

from_mint TEXT NOT NULL,
to_mint TEXT NOT NULL,
Expand Down
39 changes: 37 additions & 2 deletions ocp/data/swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@ const (
FundingSourceExternalWallet
)

type Kind uint8

const (
KindUnknown Kind = iota
KindReserve
KindStablecoin
)

type Record struct {
Id uint64

SwapId string

Owner string
Kind Kind

Owner string
DestinationOwner string

FromMint string
ToMint string
Expand Down Expand Up @@ -63,7 +74,10 @@ func (r *Record) Clone() Record {

SwapId: r.SwapId,

Owner: r.Owner,
Kind: r.Kind,

Owner: r.Owner,
DestinationOwner: r.DestinationOwner,

FromMint: r.FromMint,
ToMint: r.ToMint,
Expand Down Expand Up @@ -94,7 +108,10 @@ func (r *Record) CopyTo(dst *Record) {

dst.SwapId = r.SwapId

dst.Kind = r.Kind

dst.Owner = r.Owner
dst.DestinationOwner = r.DestinationOwner

dst.FromMint = r.FromMint
dst.ToMint = r.ToMint
Expand Down Expand Up @@ -124,10 +141,18 @@ func (r *Record) Validate() error {
return errors.New("swap id is requried")
}

if r.Kind == KindUnknown {
return errors.New("kind is required")
}

if len(r.Owner) == 0 {
return errors.New("owner is required")
}

if r.Kind == KindStablecoin && len(r.DestinationOwner) == 0 {
return errors.New("destination owner is required for stablecoin swaps")
}

if len(r.FromMint) == 0 {
return errors.New("source mint is required")
}
Expand Down Expand Up @@ -188,3 +213,13 @@ func (s State) String() string {
}
return "unknown"
}

func (k Kind) String() string {
switch k {
case KindReserve:
return "reserve"
case KindStablecoin:
return "stablecoin"
}
return "unknown"
}
Loading
Loading