Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing fields #110

Merged
merged 32 commits into from
Oct 12, 2023
Merged

Add missing fields #110

merged 32 commits into from
Oct 12, 2023

Conversation

daiagi
Copy link
Contributor

@daiagi daiagi commented Oct 6, 2023

fork from https://github.com/daiagi/stick/tree/tokenEnityt/fix-count-calc

implement kodadot/loligo#51 for stick

specifically,

  1. add chepeastNft field to tokenEntity
  2. listing event handler to sync cheapest nft
  3. resolver for TokenEntities (with correct count) of specific owner (for "own" filter in UI)
  4. add meta and metadata

@daiagi daiagi changed the title Add mssing fields Add cheapestNft field to tokenEntity Oct 6, 2023
@daiagi daiagi requested a review from vikiival October 6, 2023 04:32
@daiagi daiagi changed the title Add cheapestNft field to tokenEntity Add missing fields Oct 6, 2023
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never alter migration pls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Member

@vikiival vikiival left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, thanks for the implementation I see that you tried to do your best!

I have a few main issues:

1. Code structure

as we spoke before I try to keep some code structure.
I suggest create src/mappings/shared/tokens
and create file per each handler: create.ts, list.ts etc.

2. you forget awaits

eventHandlers.listHandler(context, entity) will make the indexer fail in the next block because connection will still locked by this

3. too much repetitive code

I see that some simple functions are often repeated a lot.
When someone decides to refactor the code. he/she can fail so this simple code functions.

4. missing buy handler

when someone buys the cheapest NFT the token will not be reflected.

5. the resolver will kill the squid

FOUR joins will make the server cry.

Otherwise

very good job!

schema.graphql Outdated Show resolved Hide resolved
src/mappings/nfts/burn.ts Outdated Show resolved Hide resolved
src/mappings/nfts/list.ts Outdated Show resolved Hide resolved
src/mappings/shared/handleTokenEntity.ts Outdated Show resolved Hide resolved
src/mappings/shared/handleTokenEntity.ts Outdated Show resolved Hide resolved
src/server-extension/query/tokenEntityByOwner.ts Outdated Show resolved Hide resolved
src/server-extension/query/tokenEntityByOwner.ts Outdated Show resolved Hide resolved
src/server-extension/query/tokenEntityByOwner.ts Outdated Show resolved Hide resolved
Comment on lines 32 to 37
FROM
token_entity as t
LEFT JOIN nft_entity as ne ON t.id = ne.token_id AND ne.current_owner = $1
LEFT JOIN CheapestNFT as c ON t.id = c.token_id
LEFT JOIN collection_entity as col ON t.collection_id = col.id
LEFT JOIN metadata_entity as me ON t.meta_id = me.id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 left joins is not a performance wise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have benchmarked a few queries, some with 3 and some with 4 joins

tested by a bash script that runs an SQL query 200 times against the fully loaded DB on localhost

results are, with no significant difference between the approaches

mean between 1.55 to 1.65 ms
standard deviation ~0.25 ms

src/server-extension/resolvers/tokenEntityByOwner.ts Outdated Show resolved Hide resolved
@daiagi
Copy link
Contributor Author

daiagi commented Oct 6, 2023

Hi @vikiival

let's address the points:

1, 2, 4

sure, np, will fix

  1. too much repetitive code

mind point the finger at where?

5.the resolver will kill the squid

I understand

  1. I hope you see why we need the resolver in the first place (basically just for the own filter)
  2. I only see it possible to remove 1 JOIN, not considering at the moment namings what do you think of this:
WITH CheapestNFT AS (
    SELECT 
        ne.token_id,
        MIN(ne.price) AS "cheapestNFTPrice",
        COUNT(ne.id) as count
    FROM 
        nft_entity ne
    WHERE 
        ne.current_owner = $1
    GROUP BY
        ne.token_id
)

SELECT
    t.id as id,
    t.name as name,
    t.image as image,
    t.media as media,
    t.metadata as metadata,
    me.id as "metaId",
    me.description as "metaDescription",
    me.image as "metaImage",
    me.animation_url as "metaAnimationUrl",
    t.block_number as "blockNumber",
    t.created_at AS "createdAt",
    t.updated_at AS "updatedAt",
    c.count as count,
    c."cheapestNFTPrice",
    col.id AS "collectionId",
    col.name AS "collectionName"
FROM
    token_entity as t
        JOIN CheapestNFT as c ON t.id = c.token_id
        JOIN collection_entity as col ON t.collection_id = col.id
        JOIN metadata_entity as me ON t.meta_id = me.id
WHERE
    ($5::bigint IS NULL OR c."cheapestNFTPrice" >= $5::bigint) AND
    ($6::bigint IS NULL OR c."cheapestNFTPrice" > $6::bigint) AND
    ($7::bigint IS NULL OR c."cheapestNFTPrice" <= $7::bigint)
ORDER BY $4 LIMIT $2 OFFSET $3;

@vikiival
Copy link
Member

vikiival commented Oct 6, 2023

mind point the finger at where?

Screenshot 2023-10-06 at 12 31 27

Screenshot 2023-10-06 at 12 31 35

Screenshot 2023-10-06 at 12 31 42

Screenshot 2023-10-06 at 12 31 49

@daiagi
Copy link
Contributor Author

daiagi commented Oct 8, 2023

all done @vikiival

please check again

@daiagi daiagi requested a review from vikiival October 8, 2023 15:02
Copy link
Member

@vikiival vikiival left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Far better!

some updates

src/mappings/shared/token/tokenAPI.ts Outdated Show resolved Hide resolved
}
const tokenId = generateTokenId(collection.id, nftMedia)
debug(OPERATION, { createToken: `Create TOKEN ${tokenId} for NFT ${nft.id}` })
const tokenName = typeof nft.name === 'string' ? nft.name?.replace(/([#_]\d+$)/g, '').trim() : ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utils

src/mappings/shared/token/tokenAPI.ts Outdated Show resolved Hide resolved
src/mappings/shared/token/utils.ts Show resolved Hide resolved
@daiagi
Copy link
Contributor Author

daiagi commented Oct 9, 2023

@vikiival fixed utils issue

also, now i realize, the usage from FE will have to be via the resolver only

  1. to simplify usage of orderBy price, that needs to check cheapest
  2. to support deny list (count and supply need to reflect forbidden nfts)

@vikiival
Copy link
Member

vikiival commented Oct 9, 2023

also, now i realize, the usage from FE will have to be via the resolver only

The scary one? 🥺

@daiagi
Copy link
Contributor Author

daiagi commented Oct 9, 2023

The scary one? 🥺

indeed

owner filtering => count/supply change => resolver
denyList => same as owner filtering really
provides easier API for price ordering etc... but that is not a must have

it's not so scary. just join on collection and metadata and 2 CTE's

@vikiival
Copy link
Member

indeed

have you ran some sample tests? how it behaves on the frontend?

@daiagi
Copy link
Contributor Author

daiagi commented Oct 10, 2023

have you ran some sample tests? how it behaves on the frontend?

yes , I am currently integrating with FE running the squid on localhost
and improving the resolver as needed (for example need to add total count field that disregards limit and offset)

so far it works very well

also, as mentioned here #110 (comment)
I did a benchmark, and the results are pleasing (sub 2ms for query)

@daiagi daiagi marked this pull request as draft October 10, 2023 10:30
@daiagi
Copy link
Contributor Author

daiagi commented Oct 10, 2023

work in progress:

  1. spotted a bug with cheapest
  2. collection floor price can be dropped
  3. need another resolver for the total amount of tokenEntities within a search criteria (price, buy, own)

image

@daiagi daiagi marked this pull request as ready for review October 11, 2023 10:10
@daiagi daiagi requested a review from vikiival October 11, 2023 10:10
@daiagi
Copy link
Contributor Author

daiagi commented Oct 11, 2023

spotted a bug with cheapest

a. dropped it from schmea as it is problematic (but and but events of chepeast will require fetching all token's nft's to find the cheapest again)
b. moved it to the resolver instead

collection floor price can be dropped

dropped, replaced by cheapest

need another resolver for the total amount of tokenEntities within a search criteria (price, buy, own)

done

Copy link
Member

@vikiival vikiival left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smol

src/mappings/uniques/transfer.ts Outdated Show resolved Hide resolved
src/mappings/shared/token/mint.ts Show resolved Hide resolved
src/mappings/shared/token/setMetadata.ts Show resolved Hide resolved
@daiagi
Copy link
Contributor Author

daiagi commented Oct 12, 2023

@vikiival
left some comments

let me know if you still want me to change the handlers names

Thank you :)

@vikiival
Copy link
Member

let me know if you still want me to change the handlers names

Like this is ok

Copy link
Member

@vikiival vikiival left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let it roll

@vikiival vikiival merged commit 463d882 into kodadot:main Oct 12, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants