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

feat: local file metadata provider #56

Merged
merged 4 commits into from
Aug 26, 2024
Merged

Conversation

0xnigir1
Copy link
Collaborator

🤖 Linear

Closes ZKS-221

Description

  • Add LocalFileMetadataProvider
  • Add Cache to GithubMetadataProvider

@0xnigir1 0xnigir1 requested review from 0xkenj1 and 0xyaco August 23, 2024 19:59
Copy link

linear bot commented Aug 23, 2024

Copy link
Collaborator

@0xkenj1 0xkenj1 left a comment

Choose a reason for hiding this comment

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

Lets gooooo, added some comments but code looks great

throw new FetchError(`Failed to fetch chains metadata: ${e.message}`);
});
let cachedData = await this.cache.get<ZKChainMetadata | undefined>(
`${GITHUB_METADATA_PREFIX}-chains`,
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we use some sort of unique id here ? like UUID?

Copy link
Collaborator

Choose a reason for hiding this comment

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

any suggestions here ? @0xyaco

Copy link
Collaborator

@0xyaco 0xyaco Aug 23, 2024

Choose a reason for hiding this comment

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

I guess there would be a possibility of, given a shared Cache instance between different classes, to overwrite a cached value by accident if someone uses the same key in multiple classes.

I can't come up with anything simple as a workaround to that issue right now lol so I think we can keep it as is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

an alternative im thinking now would be a wrapper around the cache, like BrandedCache or smth like that:

BrandedCache is Cache {
constructor(cache: Cache, prefix: string)
...wraps calls
}

and that this class wraps the cache methods and automatically adds the prefix to the key

but seems too much at this point

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree that it's too much, let's just be aware of this issue when defining cache keys.

Copy link
Collaborator

Choose a reason for hiding this comment

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

lets keep it as it is nigiri, no need for uuid

Copy link
Collaborator

Choose a reason for hiding this comment

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

i was wondering if we want to index the token list by address like { [key:address] : Token<"erc20">} for example...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i thought about it too but i would leave that task for when we refactor the pricing service, so now everything still works as it is, wdyt?

throw new FileNotFound(chainJsonPath);
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets add natspec on IMetadataProvider and reuse it on the classes that implement the interface

Copy link
Collaborator

@0xyaco 0xyaco left a comment

Choose a reason for hiding this comment

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

Just one question and after that, it's ready to go!

private readonly tokenJsonPath: string,
private readonly chainJsonPath: string,
private readonly logger: ILogger,
private readonly cache: Cache,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are the local JSON files going to be live-updated while the app is running? If not, do we need the cache for local files? Wdyt about using two instance variables and reading the file contents inside this constructor?

Something along these lines:

constructor() {
  if (!existsSync(...)) {
    ...
  }


  if (!existsSync(...)) {
    ...
  }

  this.chainsMetadata = readMetadata()
  this.tokensMetadata = tokensMetadata()
}

async getChainsMetadata() {
  return Promise.resolve(this.chainsMetadata);
}

If for some business rule this provider needs to keep reading the local file from time to time (or if there's any other reason) dismiss this comment. 😎

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we had the exact same discussion with @0xkenj1 jajajaj, we ended up agreeing in make it work like the GithubMetadata as a matter of consistency

Copy link
Collaborator

Choose a reason for hiding this comment

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

we can reconsider this 😅

Copy link
Collaborator

@0xkenj1 0xkenj1 Aug 23, 2024

Choose a reason for hiding this comment

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

I think i didn't consider the case in which u redeploy the chainhub instance, if you keep redis running and you had updated the JSON file, then it will pick the data from redis instead of the updated file. (Supposing infinite ttl )

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we'll go with this option ser, makes sense

@0xnigir1 0xnigir1 requested review from 0xkenj1 and 0xyaco August 26, 2024 13:29
Copy link
Collaborator

@0xyaco 0xyaco left a comment

Choose a reason for hiding this comment

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

Sweet

Copy link
Collaborator

@0xkenj1 0xkenj1 left a comment

Choose a reason for hiding this comment

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

🚀

@0xnigir1 0xnigir1 merged commit 98b6036 into dev Aug 26, 2024
6 checks passed
@0xnigir1 0xnigir1 deleted the feat/local-file-metadata-provider branch August 26, 2024 13:39
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.

3 participants