Skip to content

Commit

Permalink
l1 queries
Browse files Browse the repository at this point in the history
  • Loading branch information
benny-conn committed Oct 11, 2023
1 parent 22453ef commit 21f0aca
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 29 deletions.
22 changes: 14 additions & 8 deletions db/gen/coredb/contract_gallery.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions db/gen/coredb/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions db/queries/core/contract_gallery.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- name: UpsertParentContracts :many
insert into contracts(id, deleted, version, created_at, address, symbol, name, owner_address, chain, description, profile_image_url, is_provider_marked_spam) (
insert into contracts(id, deleted, version, created_at, address, symbol, name, owner_address, chain, l1_chain, description, profile_image_url, is_provider_marked_spam) (
select unnest(@ids::varchar[])
, false
, unnest(@version::int[])
Expand All @@ -9,11 +9,12 @@ insert into contracts(id, deleted, version, created_at, address, symbol, name, o
, unnest(@name::varchar[])
, unnest(@owner_address::varchar[])
, unnest(@chain::int[])
, unnest(@l1_chain::int[])
, unnest(@description::varchar[])
, unnest(@profile_image_url::varchar[])
, unnest(@provider_marked_spam::bool[])
)
on conflict (chain, address) where parent_id is null
on conflict (l1_chain, address) where parent_id is null
do update set symbol = coalesce(nullif(excluded.symbol, ''), nullif(contracts.symbol, ''))
, version = excluded.version
, name = coalesce(nullif(excluded.name, ''), nullif(contracts.name, ''))
Expand All @@ -31,7 +32,7 @@ do update set symbol = coalesce(nullif(excluded.symbol, ''), nullif(contracts.sy
returning *;

-- name: UpsertChildContracts :many
insert into contracts(id, deleted, version, created_at, name, address, creator_address, owner_address, chain, description, parent_id) (
insert into contracts(id, deleted, version, created_at, name, address, creator_address, owner_address, chain, l1_chain, description, parent_id) (
select unnest(@id::varchar[]) as id
, false
, 0
Expand All @@ -41,10 +42,11 @@ insert into contracts(id, deleted, version, created_at, name, address, creator_a
, unnest(@creator_address::varchar[])
, unnest(@owner_address::varchar[])
, unnest(@chain::int[])
, unnest(@l1_chain::int[])
, unnest(@description::varchar[])
, unnest(@parent_ids::varchar[])
)
on conflict (chain, parent_id, address) where parent_id is not null
on conflict (l1_chain, parent_id, address) where parent_id is not null
do update set deleted = excluded.deleted
, name = excluded.name
, creator_address = excluded.creator_address
Expand Down
2 changes: 1 addition & 1 deletion db/queries/core/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,7 @@ from users u, contracts c, wallets w
where u.id = @user_id
and c.chain = any(@chains::int[])
and w.id = any(u.wallets) and coalesce(nullif(c.owner_address, ''), nullif(c.creator_address, '')) = w.address
and w.chain = any(@l1_chains::int[])
and w.l1_chain = c.l1_chain
and u.deleted = false
and c.deleted = false
and w.deleted = false
Expand Down
5 changes: 4 additions & 1 deletion service/multichain/multichain.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ func (p *Provider) SyncTokensCreatedOnSharedContracts(ctx context.Context, userI
params.CreatorAddress = append(params.CreatorAddress, child.CreatorAddress.String())
params.OwnerAddress = append(params.OwnerAddress, child.OwnerAddress.String())
params.Chain = append(params.Chain, int32(result.Chain))
params.L1Chain = append(params.L1Chain, int32(result.Chain.L1Chain()))
params.Description = append(params.Description, child.Description)
params.ParentIds = append(params.ParentIds, contractToDBID[persist.NewContractIdentifiers(edge.Parent.Address, result.Chain)].String())
}
Expand Down Expand Up @@ -1463,8 +1464,9 @@ func (p *Provider) RefreshTokenDescriptorsByTokenIdentifiers(ctx context.Context
return persist.ErrTokenNotFoundByTokenIdentifiers{Token: ti}
}

contractID, err := p.Repos.ContractRepository.UpsertByAddress(ctx, ti.ContractAddress, ti.Chain, persist.ContractGallery{
contractID, err := p.Repos.ContractRepository.UpsertByAddress(ctx, ti.ContractAddress, persist.ContractGallery{
Chain: ti.Chain,
L1CHain: ti.Chain.L1Chain(),
Address: persist.Address(ti.Chain.NormalizeAddress(ti.ContractAddress)),
Symbol: persist.NullString(finalContractDescriptors.Symbol),
Name: persist.NullString(finalContractDescriptors.Name),
Expand Down Expand Up @@ -2139,6 +2141,7 @@ func contractsToNewDedupedContracts(contracts []chainContracts, existingContract
for address, meta := range contractMetadatas {
res = append(res, persist.ContractGallery{
Chain: address.Chain(),
L1CHain: address.Chain().L1Chain(),
Address: address.Address(),
Symbol: persist.NullString(meta.Symbol),
Name: persist.NullString(meta.Name),
Expand Down
1 change: 1 addition & 0 deletions service/persist/contract_gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ContractGallery struct {
LastUpdated time.Time `json:"last_updated"`

Chain Chain `json:"chain"`
L1CHain L1Chain `json:"l1_chain"`
Address Address `json:"address"`
Symbol NullString `json:"symbol"`
Name NullString `json:"name"`
Expand Down
12 changes: 7 additions & 5 deletions service/persist/postgres/contract_gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ func NewContractGalleryRepository(db *sql.DB, queries *db.Queries) *ContractGall
checkNoErr(err)

upsertByAddressStmt, err := db.PrepareContext(ctx, `
insert into contracts (id,version,address,symbol,name,owner_address,chain,description,profile_image_url) values ($1,$2,$3,$4,$5,$6,$7,$8,$9)
insert into contracts (id,version,address,symbol,name,owner_address,chain,l1_chain,description,profile_image_url) values ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
on conflict (address,chain) where parent_id is null do update set
version = $2,
address = $3,
symbol = coalesce(nullif(contracts.symbol, ''), nullif($4, '')),
name = coalesce(nullif(contracts.name, ''), nullif($5, '')),
description = coalesce(nullif(contracts.description, ''), nullif($8, '')),
profile_image_url = coalesce(nullif(contracts.profile_image_url, ''), nullif($9, '')),
description = coalesce(nullif(contracts.description, ''), nullif($9, '')),
profile_image_url = coalesce(nullif(contracts.profile_image_url, ''), nullif($10, '')),
owner_address = case when nullif(contracts.owner_address, '') is null then $6 else contracts.owner_address end,
chain = $7
l1_chain = $8
returning id;
`)
checkNoErr(err)
Expand Down Expand Up @@ -154,8 +155,8 @@ func (c *ContractGalleryRepository) GetByTokenIDs(pCtx context.Context, pDBIDs p
}

// UpsertByAddress upserts the contract with the given address
func (c *ContractGalleryRepository) UpsertByAddress(pCtx context.Context, pAddress persist.Address, pChain persist.Chain, pContract persist.ContractGallery) (contractID persist.DBID, err error) {
err = c.upsertByAddressStmt.QueryRowContext(pCtx, persist.GenerateID(), pContract.Version, pContract.Address, pContract.Symbol, pContract.Name, pContract.OwnerAddress, pContract.Chain, pContract.Description, pContract.ProfileImageURL).Scan(&contractID)
func (c *ContractGalleryRepository) UpsertByAddress(pCtx context.Context, pAddress persist.Address, pContract persist.ContractGallery) (contractID persist.DBID, err error) {
err = c.upsertByAddressStmt.QueryRowContext(pCtx, persist.GenerateID(), pContract.Version, pContract.Address, pContract.Symbol, pContract.Name, pContract.OwnerAddress, pContract.Chain, pContract.L1CHain, pContract.Description, pContract.ProfileImageURL).Scan(&contractID)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -183,6 +184,7 @@ func (c *ContractGalleryRepository) BulkUpsert(pCtx context.Context, pContracts
params.Name = append(params.Name, c.Name.String())
params.OwnerAddress = append(params.OwnerAddress, c.OwnerAddress.String())
params.Chain = append(params.Chain, int32(c.Chain))
params.L1Chain = append(params.L1Chain, int32(c.Chain.L1Chain()))
params.Description = append(params.Description, c.Description.String())
params.ProfileImageUrl = append(params.ProfileImageUrl, c.ProfileImageURL.String())
params.ProviderMarkedSpam = append(params.ProviderMarkedSpam, c.IsProviderMarkedSpam)
Expand Down

0 comments on commit 21f0aca

Please sign in to comment.