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
13 changes: 10 additions & 3 deletions ocp/data/currency/memory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,16 @@ func (s *store) SaveMetadata(ctx context.Context, data *currency.MetadataRecord)
return currency.ErrStaleMetadataVersion
}

data.Version = item.Version + 1
data.Id = item.Id
s.metadataRecords[i] = data.Clone()
cloned := item.Clone()
cloned.Description = data.Description
cloned.ImageUrl = data.ImageUrl
cloned.BillColors = append([]string(nil), data.BillColors...)
cloned.SocialLinks = append([]currency.SocialLink(nil), data.SocialLinks...)
cloned.State = data.State
cloned.Version = item.Version + 1

s.metadataRecords[i] = cloned
cloned.CopyTo(data)
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion ocp/data/currency/postgres/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (m *metadataModel) dbSave(ctx context.Context, db *sqlx.DB) error {

ON CONFLICT (mint)
DO UPDATE
SET state = $22, version = `+metadataTableName+`.version + 1
SET description = $3, image_url = $4, bill_colors = $5, social_links = $6, state = $22, version = `+metadataTableName+`.version + 1
WHERE `+metadataTableName+`.mint = $9 AND `+metadataTableName+`.version = $23

RETURNING id, name, symbol, description, image_url, bill_colors, social_links, seed, authority, mint, mint_bump, decimals, currency_config, currency_config_bump, liquidity_pool, liquidity_pool_bump, vault_mint, vault_mint_bump, vault_core, vault_core_bump, sell_fee_bps, alt, state, version, created_by, created_at`,
Expand Down
3 changes: 2 additions & 1 deletion ocp/data/currency/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ type Store interface {
GetExchangeRatesInRange(ctx context.Context, symbol string, interval query.Interval, start time.Time, end time.Time, ordering query.Ordering) ([]*ExchangeRateRecord, error)

// SaveMetadata creates or updates currency creator metadata in the store.
// On insert, Version is set to 1. On update, Version is incremented.
// On insert, Version is set to 1. On update, only mutable fields (Description,
// ImageUrl, BillColors, SocialLinks, State) are updated and Version is incremented.
// ErrStaleMetadataVersion is returned when the provided version doesn't match.
SaveMetadata(ctx context.Context, record *MetadataRecord) error

Expand Down
38 changes: 35 additions & 3 deletions ocp/data/currency/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,28 +392,60 @@ func testMetadataSaveWithVersioning(t *testing.T, s currency.Store) {
assert.EqualValues(t, 1, record.Version)
assert.EqualValues(t, currency.MetadataStateUnknown, record.State)

// Update state and save again with correct version
// Update mutable fields and save again with correct version
record.State = currency.MetadataStateAvailable
record.Description = "Updated description"
record.ImageUrl = "https://example.com/updated.png"
record.BillColors = []string{"#FF0000", "#00FF00", "#0000FF"}
record.SocialLinks = []currency.SocialLink{
{Type: currency.SocialLinkTypeWebsite, Value: "https://updated.example.com"},
{Type: currency.SocialLinkTypeX, Value: "updatedhandle"},
}
require.NoError(t, s.SaveMetadata(context.Background(), record))
assert.EqualValues(t, 2, record.Version)
assert.EqualValues(t, currency.MetadataStateAvailable, record.State)

// Verify via get
// Verify mutable fields were updated
actual, err := s.GetMetadata(context.Background(), record.Mint)
require.NoError(t, err)
assert.EqualValues(t, 2, actual.Version)
assert.EqualValues(t, currency.MetadataStateAvailable, actual.State)
assert.Equal(t, "Updated description", actual.Description)
assert.Equal(t, "https://example.com/updated.png", actual.ImageUrl)
assert.Equal(t, []string{"#FF0000", "#00FF00", "#0000FF"}, actual.BillColors)
assert.Equal(t, []currency.SocialLink{
{Type: currency.SocialLinkTypeWebsite, Value: "https://updated.example.com"},
{Type: currency.SocialLinkTypeX, Value: "updatedhandle"},
}, actual.SocialLinks)

// Verify immutable fields were preserved
assert.Equal(t, "Versioned", actual.Name)
assert.Equal(t, "VER", actual.Symbol)

// Attempt save with stale version
record.Description = "Updated description 2"
record.ImageUrl = "https://example.com/updated2.png"
record.BillColors = []string{"#FFFFFF", "#FFFFFF", "#FFFFFF"}
record.SocialLinks = []currency.SocialLink{
{Type: currency.SocialLinkTypeWebsite, Value: "https://updated2.example.com"},
{Type: currency.SocialLinkTypeX, Value: "updatedhandle2"},
}
record.State = currency.MetadataStateUnknown
record.Version = 1
assert.Equal(t, currency.ErrStaleMetadataVersion, s.SaveMetadata(context.Background(), record))

// Verify via get
// Verify via get that nothing changed
actual, err = s.GetMetadata(context.Background(), record.Mint)
require.NoError(t, err)
assert.EqualValues(t, 2, actual.Version)
assert.EqualValues(t, currency.MetadataStateAvailable, actual.State)
assert.Equal(t, "Updated description", actual.Description)
assert.Equal(t, "https://example.com/updated.png", actual.ImageUrl)
assert.Equal(t, []string{"#FF0000", "#00FF00", "#0000FF"}, actual.BillColors)
assert.Equal(t, []currency.SocialLink{
{Type: currency.SocialLinkTypeWebsite, Value: "https://updated.example.com"},
{Type: currency.SocialLinkTypeX, Value: "updatedhandle"},
}, actual.SocialLinks)
}

func assertEquivalentMetadataRecords(t *testing.T, obj1, obj2 *currency.MetadataRecord) {
Expand Down
Loading