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

Queries #1

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open

Queries #1

wants to merge 40 commits into from

Conversation

onnovisser
Copy link
Collaborator

@onnovisser onnovisser commented Oct 15, 2024

Description

Starts out the Centrifuge class with functionality for queries, using Observables, but allowing them to be awaited. Also includes caching and deduplication. A few example queries are added to get the liquidity pool domains where a pool is active.

the _query function caches the obserable by the keys given, similar to react-query. Everytime the same keys are passed, the same observable is returned. _query also multicasts the observable. So if there are multiple subscribers, the underlying observable is only subscribed to once, so data isn't fetched in duplicate. null can be passed as keys, in which case the observable isn't cached or multicasted. It can be used when a query is only transforming/aggregating the results from other queries and doesn't need to be cached.

Usage:

const centrifuge = new Centrifuge({ environment: 'demo' })
const pool = await centrifuge.pool('3783664923') // Pool
const domains = await pool.activeDomains() // PoolDomain[]
domains[0]?.chainId // 11155111

src/Account.ts Outdated
})
}

transfer(to: HexString, amount: bigint) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

seeing a lot of versions of the transfer function still, do you want to keep these or should we evaluate and choose the best?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed them, they were just there as examples


const envConfig = {
mainnet: {
subqueryUrl: 'https://api.subquery.network/sq/centrifuge/pools-demo-multichain',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
subqueryUrl: 'https://api.subquery.network/sq/centrifuge/pools-demo-multichain',
subqueryUrl: 'https://subql.embrio.tech/',

// Saves having to pass `account` and `chain` to every `writeContract` call
const walletClient = isLocalAccount(signer)
? (bareWalletClient as WalletClient<any, Chain, AccountType>)
: createWalletClient({ account: address, chain, transport: custom(signer) })
Copy link
Collaborator

Choose a reason for hiding this comment

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

having a hard time understanding why the wallet client has to be recreated, from the code it looks like the wallet client could already initially be set correctly to avoid the walletClient/bareWalletClient situation. The comment is suggesting that it's being recreated because the chainid may be incorrect but it's using the same chain id as the bareWalletClient

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it's mainly because of the address. It needs a wallet client to get the signer address, but it needs the address to create the wallet client 😕

import type { Centrifuge } from './Centrifuge.js'
import type { CentrifugeQueryOptions } from './types/query.js'

export class Entity {
Copy link
Collaborator

Choose a reason for hiding this comment

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

curious why you opted to redefine functions on the class (_transact, _query, _transactSequence) instead of extending the Centrifuge class and just overriding where necessary e.g the query keys

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Extending the Centrifuge class for each entity would result in there being many instances of it. But there needs to be only one, because that's where the cache lives. the extra _transact and _transactSequence here are a small convenience so you can do, this._transact() instead of this._root._transact(). I don't think the functions themselves could be moved to Entity, because we'll need something like Centrifuge.createPool() at some point.

src/Pool.ts Outdated
Comment on lines 64 to 84
activeDomains() {
return this._query(null, () => {
return this.domains().pipe(
switchMap((domains) => {
return combineLatest(
domains.map((domain) =>
domain.isActive().pipe(
timeout(8000),
catchError(() => {
return of(false)
})
)
)
).pipe(
map((isActive) => {
return domains.filter((_, index) => isActive[index])
})
)
})
)
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

would this fit better in the PoolDomain entity?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A PoolDomain concerns only a single chain. It has the isActive method, which returns whether the pool is active on that domain. Like Pool.domains() returns all PoolDomains for each existing chain, Pool.activeDomains() returns only the PoolDomains where the pool is active.

Copy link
Collaborator

Choose a reason for hiding this comment

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

still think this particular method doesn't belong here. Maybe we should rethink the PoolNetwork entity and figure out how it can handle all networks the pool is deployed on too. Possibly it's the name PoolNetwork that's just misleading in this case. Either way we should discuss how granular we want out entities to be and come up with a strategy together

Copy link
Collaborator Author

@onnovisser onnovisser Nov 15, 2024

Choose a reason for hiding this comment

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

The PoolNetwork entity, as I imagine it, represents the presence of a pool on a single network. I see it being used to query if the pool is active, get the tranche token, get the liquidity pools for the pool on a specific network. As well as deploying the tranches/liquidity pools/etc. Everything that is not network specific (like the NAV, TVL, closing an epoch, creating an Asset, etc.) can live in the Pool itself. The word "network" maybe makes it sound a bit ambiguous, but it's a common term to mean a single chain. Still happy to iterate on this though, and we can definitely brainstorm some ideas for how the entities may be laid out, and how granular they should be.

amount: bigint
) {
let domainOrCurrency: any = currencyAddress
if (currencyAddress.toLowerCase() === '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

what contract is this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

USDC

Copy link
Collaborator

Choose a reason for hiding this comment

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

can I recommend storing it in a named variable instead?

@onnovisser onnovisser marked this pull request as ready for review November 12, 2024 08:48
onnovisser and others added 11 commits November 12, 2024 10:07
* Use signer in setup and remove unused conditional

* Add impersonation functionality to tenderlyFork by overriding request

* Add test file for accounts

* Add minimal readme docs

* Fix failing test

* Add test case for sender address
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