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
7 changes: 7 additions & 0 deletions ocp/data/currency/memory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package memory
import (
"context"
"sort"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -186,6 +187,12 @@ func (s *store) SaveMetadata(ctx context.Context, data *currency.MetadataRecord)
}
}

for _, item := range s.metadataRecords {
if strings.EqualFold(item.Name, data.Name) {
return currency.ErrDuplicateCurrency
}
}

data.Version = 1
data.Id = s.lastMetadataIndex
s.metadataRecords = append(s.metadataRecords, data.Clone())
Expand Down
5 changes: 5 additions & 0 deletions ocp/data/currency/postgres/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ func (m *metadataModel) dbSave(ctx context.Context, db *sqlx.DB) error {
m.CreatedAt,
).StructScan(m)

err = pgutil.CheckUniqueViolation(err, currency.ErrDuplicateCurrency)
if err == currency.ErrDuplicateCurrency {
return err
}

return pgutil.CheckNoRows(err, currency.ErrStaleMetadataVersion)
})
}
Expand Down
1 change: 1 addition & 0 deletions ocp/data/currency/postgres/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const (
created_by TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL
);
CREATE UNIQUE INDEX ocp__core_currencymetadata__name__idx ON ocp__core_currencymetadata (LOWER(name));
CREATE TABLE ocp__core_currencyreserve (
id serial NOT NULL PRIMARY KEY,

Expand Down
1 change: 1 addition & 0 deletions ocp/data/currency/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
ErrInvalidInterval = errors.New("the provided interval is not valid")
ErrExists = errors.New("record exists")
ErrStaleMetadataVersion = errors.New("metadata version is stale")
ErrDuplicateCurrency = errors.New("duplicate currency detected")
)

type Store interface {
Expand Down
78 changes: 78 additions & 0 deletions ocp/data/currency/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func RunTests(t *testing.T, s currency.Store, teardown func()) {
testGetExchangeRatesInRange,
testMetadataRoundTrip,
testMetadataSaveWithVersioning,
testMetadataUniqueNameConstraint,
testGetAllMints,
testReserveRoundTrip,
testGetReservesInRange,
Expand Down Expand Up @@ -193,6 +194,83 @@ func testMetadataRoundTrip(t *testing.T, s currency.Store) {
assert.EqualValues(t, 1, actual.Version)
}

func testMetadataUniqueNameConstraint(t *testing.T, s currency.Store) {
record1 := &currency.MetadataRecord{
Name: "UniqueName",
Symbol: "UN1",
Description: "First currency",
ImageUrl: "https://example.com/un1.png",
BillColors: []string{"#000000"},
SocialLinks: []currency.SocialLink{{Type: currency.SocialLinkTypeWebsite, Value: "https://example.com"}},

Seed: "uniqueseed1",
Authority: "uniqueauth1",

Mint: "uniquemint1111111111111111111111111111111111111",
MintBump: 255,
Decimals: currencycreator.DefaultMintDecimals,

CurrencyConfig: "uniqueconfig11111111111111111111111111111111",
CurrencyConfigBump: 255,

LiquidityPool: "uniquepool1111111111111111111111111111111111",
LiquidityPoolBump: 255,

VaultMint: "uniquevmint111111111111111111111111111111111",
VaultMintBump: 255,

VaultCore: "uniquevcore111111111111111111111111111111111",
VaultCoreBump: 255,

SellFeeBps: currencycreator.DefaultSellFeeBps,

Alt: "uniquealt11111111111111111111111111111111111111",

CreatedBy: "uniquecreator1",
CreatedAt: time.Now(),
}

require.NoError(t, s.SaveMetadata(context.Background(), record1))

// Second record with the same name (different case) but different everything else
record2 := &currency.MetadataRecord{
Name: "uniquename",
Symbol: "UN2",
Description: "Second currency",
ImageUrl: "https://example.com/un2.png",
BillColors: []string{"#FFFFFF"},
SocialLinks: []currency.SocialLink{{Type: currency.SocialLinkTypeWebsite, Value: "https://example2.com"}},

Seed: "uniqueseed2",
Authority: "uniqueauth2",

Mint: "uniquemint2222222222222222222222222222222222222",
MintBump: 255,
Decimals: currencycreator.DefaultMintDecimals,

CurrencyConfig: "uniqueconfig22222222222222222222222222222222",
CurrencyConfigBump: 255,

LiquidityPool: "uniquepool2222222222222222222222222222222222",
LiquidityPoolBump: 255,

VaultMint: "uniquevmint222222222222222222222222222222222",
VaultMintBump: 255,

VaultCore: "uniquevcore222222222222222222222222222222222",
VaultCoreBump: 255,

SellFeeBps: currencycreator.DefaultSellFeeBps,

Alt: "uniquealt22222222222222222222222222222222222222",

CreatedBy: "uniquecreator2",
CreatedAt: time.Now(),
}

assert.Equal(t, currency.ErrDuplicateCurrency, s.SaveMetadata(context.Background(), record2))
}

func testGetAllMints(t *testing.T, s currency.Store) {
// No mints should exist initially
mints, err := s.GetAllMints(context.Background())
Expand Down
Loading