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
20 changes: 18 additions & 2 deletions ocp/data/currency/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ import (
"github.com/code-payments/ocp-server/solana/currencycreator"
)

type SocialLinkType uint8

const (
SocialLinkTypeUnknown SocialLinkType = iota
SocialLinkTypeWebsite
SocialLinkTypeX
)

type SocialLink struct {
Type SocialLinkType `json:"type"`
Value string `json:"value"`
}

type ExchangeRateRecord struct {
Id uint64
Time time.Time
Expand All @@ -26,7 +39,8 @@ type MetadataRecord struct {
Symbol string
Description string
ImageUrl string
BillColors []string
BillColors []string
SocialLinks []SocialLink

Seed string

Expand Down Expand Up @@ -156,7 +170,8 @@ func (m *MetadataRecord) Clone() *MetadataRecord {
Symbol: m.Symbol,
Description: m.Description,
ImageUrl: m.ImageUrl,
BillColors: append([]string(nil), m.BillColors...),
BillColors: append([]string(nil), m.BillColors...),
SocialLinks: append([]SocialLink(nil), m.SocialLinks...),

Seed: m.Seed,

Expand Down Expand Up @@ -195,6 +210,7 @@ func (m *MetadataRecord) CopyTo(dst *MetadataRecord) {
dst.Description = m.Description
dst.ImageUrl = m.ImageUrl
dst.BillColors = append([]string(nil), m.BillColors...)
dst.SocialLinks = append([]SocialLink(nil), m.SocialLinks...)

dst.Seed = m.Seed

Expand Down
38 changes: 31 additions & 7 deletions ocp/data/currency/postgres/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package postgres
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -56,7 +58,8 @@ type metadataModel struct {
Symbol string `db:"symbol"`
Description string `db:"description"`
ImageUrl string `db:"image_url"`
BillColors string `db:"bill_colors"`
BillColors string `db:"bill_colors"`
SocialLinks string `db:"social_links"`

Seed string `db:"seed"`

Expand Down Expand Up @@ -98,7 +101,8 @@ func toMetadataModel(obj *currency.MetadataRecord) (*metadataModel, error) {
Symbol: obj.Symbol,
Description: obj.Description,
ImageUrl: obj.ImageUrl,
BillColors: strings.Join(obj.BillColors, ","),
BillColors: strings.Join(obj.BillColors, ","),
SocialLinks: marshalSocialLinks(obj.SocialLinks),

Seed: obj.Seed,

Expand Down Expand Up @@ -142,7 +146,8 @@ func fromMetadataModel(obj *metadataModel) *currency.MetadataRecord {
Symbol: obj.Symbol,
Description: obj.Description,
ImageUrl: obj.ImageUrl,
BillColors: billColors,
BillColors: billColors,
SocialLinks: unmarshalSocialLinks(obj.SocialLinks),

Seed: obj.Seed,

Expand Down Expand Up @@ -204,6 +209,24 @@ func fromReserveModel(obj *reserveModel) *currency.ReserveRecord {
}
}

func marshalSocialLinks(links []currency.SocialLink) string {
if len(links) == 0 {
return "[]"
}
data, _ := json.Marshal(links)
fmt.Println(string(data))
return string(data)
}

func unmarshalSocialLinks(data string) []currency.SocialLink {
if data == "" || data == "[]" {
return nil
}
var links []currency.SocialLink
_ = json.Unmarshal([]byte(data), &links)
return links
}

func makeTimeBasedSelectQuery(table, condition string, ordering q.Ordering) string {
return `SELECT * FROM ` + table + ` WHERE ` + condition + ` ORDER BY for_timestamp ` + q.FromOrderingWithFallback(ordering, "asc")
}
Expand Down Expand Up @@ -252,14 +275,15 @@ func (m *metadataModel) dbSave(ctx context.Context, db *sqlx.DB) error {
return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
err := tx.QueryRowxContext(ctx,
`INSERT INTO `+metadataTableName+`
(name, symbol, description, image_url, bill_colors, 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, created_by, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22)
RETURNING id, name, symbol, description, image_url, bill_colors, 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, created_by, created_at`,
(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, created_by, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $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, created_by, created_at`,
m.Name,
m.Symbol,
m.Description,
m.ImageUrl,
m.BillColors,
m.SocialLinks,
m.Seed,
m.Authority,
m.Mint,
Expand Down Expand Up @@ -353,7 +377,7 @@ func dbGetAllExchangeRatesForRange(ctx context.Context, db *sqlx.DB, symbol stri
func dbGetMetadataByMint(ctx context.Context, db *sqlx.DB, mint string) (*metadataModel, error) {
res := &metadataModel{}
err := db.GetContext(ctx, res,
`SELECT id, name, symbol, description, image_url, bill_colors, 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, created_by, created_at
`SELECT 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, created_by, created_at
FROM `+metadataTableName+`
WHERE mint = $1`,
mint,
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 @@ -38,6 +38,7 @@ const (
description TEXT NOT NULL,
image_url TEXT NOT NULL,
bill_colors TEXT NOT NULL DEFAULT '',
social_links TEXT NOT NULL DEFAULT '[]',

seed TEXT UNIQUE NOT NULL,

Expand Down
7 changes: 6 additions & 1 deletion ocp/data/currency/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ func testMetadataRoundTrip(t *testing.T, s currency.Store) {
Symbol: "JFY",
Description: "A test currency for Flipcash created by Jeff Yanta so we can eat our own dog food as we build out the platform. Pun intended",
ImageUrl: "https://flipcash-currency-assets.s3.us-east-1.amazonaws.com/52MNGpgvydSwCtC2H4qeiZXZ1TxEuRVCRGa8LAfk2kSj/icon.png",
BillColors: []string{"#19191A", "#FFFFFF"},
BillColors: []string{"#19191A", "#FFFFFF"},
SocialLinks: []currency.SocialLink{
{Type: currency.SocialLinkTypeWebsite, Value: "https://flipcash.com"},
{Type: currency.SocialLinkTypeX, Value: "jeffycurrency"},
},

Seed: "H7WNaHtCa5h2k7AwZ8DbdLfM6bU2bi2jmWiUkFqgeBYk",

Expand Down Expand Up @@ -284,6 +288,7 @@ func assertEquivalentMetadataRecords(t *testing.T, obj1, obj2 *currency.Metadata
assert.Equal(t, obj1.Description, obj2.Description)
assert.Equal(t, obj1.ImageUrl, obj2.ImageUrl)
assert.Equal(t, obj1.BillColors, obj2.BillColors)
assert.Equal(t, obj1.SocialLinks, obj2.SocialLinks)
assert.Equal(t, obj1.Seed, obj2.Seed)
assert.Equal(t, obj1.Authority, obj2.Authority)
assert.Equal(t, obj1.Mint, obj2.Mint)
Expand Down
17 changes: 17 additions & 0 deletions ocp/rpc/currency/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,23 @@ func (s *currencyServer) GetMints(ctx context.Context, req *currencypb.GetMintsR
CreatedAt: timestamppb.New(metadataRecord.CreatedAt),
}

for _, link := range metadataRecord.SocialLinks {
switch link.Type {
case currency.SocialLinkTypeWebsite:
protoMetadata.SocialLinks = append(protoMetadata.SocialLinks, &currencypb.SocialLink{
Type: &currencypb.SocialLink_Website_{
Website: &currencypb.SocialLink_Website{Url: link.Value},
},
})
case currency.SocialLinkTypeX:
protoMetadata.SocialLinks = append(protoMetadata.SocialLinks, &currencypb.SocialLink{
Type: &currencypb.SocialLink_X_{
X: &currencypb.SocialLink_X{Username: link.Value},
},
})
}
}

billColors := metadataRecord.BillColors
if len(billColors) == 0 {
billColors = []string{"#AAAAAA", "#2C2C2C"}
Expand Down
Loading