diff --git a/src/Centrifuge.ts b/src/Centrifuge.ts index 2748da1..5363854 100644 --- a/src/Centrifuge.ts +++ b/src/Centrifuge.ts @@ -431,11 +431,13 @@ export class Centrifuge { const sharedSubject = new Subject>() function createShared(): Observable { const $shared = observableCallback().pipe( - shareReplayWithDelayedReset({ - bufferSize: (options?.cache ?? true) ? 1 : 0, - resetDelay: (options?.cache === false ? 0 : (options?.observableCacheTime ?? 60)) * 1000, - windowTime: (options?.valueCacheTime ?? Infinity) * 1000, - }) + keys + ? shareReplayWithDelayedReset({ + bufferSize: (options?.cache ?? true) ? 1 : 0, + resetDelay: (options?.cache === false ? 0 : (options?.observableCacheTime ?? 60)) * 1000, + windowTime: (options?.valueCacheTime ?? Infinity) * 1000, + }) + : map((val) => val) ) sharedSubject.next($shared) return $shared diff --git a/src/Pool.ts b/src/Pool.ts index 0ee0641..c4536e8 100644 --- a/src/Pool.ts +++ b/src/Pool.ts @@ -2,8 +2,8 @@ import { catchError, combineLatest, map, of, switchMap, timeout } from 'rxjs' import type { Centrifuge } from './Centrifuge.js' import { Entity } from './Entity.js' import { PoolNetwork } from './PoolNetwork.js' -import { PoolMetadata } from './types/poolMetadata.js' import { Reports } from './Reports/index.js' +import { PoolMetadata } from './types/poolMetadata.js' export class Pool extends Entity { constructor( @@ -80,7 +80,9 @@ export class Pool extends Entity { return combineLatest( networks.map((network) => network.isActive().pipe( - timeout(8000), + // Because this is fetching from multiple networks and we're waiting on all of them before returning a value, + // we want a timeout in case one of the endpoints is too slow + timeout({ first: 5000 }), catchError(() => { return of(false) }) diff --git a/src/PoolNetwork.test.ts b/src/PoolNetwork.test.ts index 5cdc2fb..54df767 100644 --- a/src/PoolNetwork.test.ts +++ b/src/PoolNetwork.test.ts @@ -38,4 +38,28 @@ describe('PoolNetwork', () => { // Calls should get batched expect(fetchSpy.getCalls().length).to.equal(1) }) + + it('should deploy a tranche', async () => { + const poolId = '1287682503' + const trancheId = '0x02bbf52e452ddb47103913051212382c' + const pool = new Pool(context.centrifuge, poolId) + const poolNetwork = new PoolNetwork(context.centrifuge, pool, 11155111) + + const canTrancheBeDeployed = await poolNetwork.canTrancheBeDeployed(trancheId) + expect(canTrancheBeDeployed).to.equal(true) + + const result = await poolNetwork.deployTranche(trancheId) + expect(result.type).to.equal('TransactionConfirmed') + }) + + it('should deploy a vault', async () => { + const poolId = '1287682503' + const trancheId = '0x02bbf52e452ddb47103913051212382c' + const pool = new Pool(context.centrifuge, poolId) + const poolNetwork = new PoolNetwork(context.centrifuge, pool, 11155111) + const tUSD = '0x8503b4452Bf6238cC76CdbEE223b46d7196b1c93' + + const result = await poolNetwork.deployVault(trancheId, tUSD) + expect(result.type).to.equal('TransactionConfirmed') + }) }) diff --git a/src/tests/Centrifuge.test.ts b/src/tests/Centrifuge.test.ts index 23617d5..d3f7044 100644 --- a/src/tests/Centrifuge.test.ts +++ b/src/tests/Centrifuge.test.ts @@ -78,7 +78,7 @@ describe('Centrifuge', () => { it('should cache the latest value by default', async () => { let subscribedTimes = 0 - const query1 = context.centrifuge._query(null, () => + const query1 = context.centrifuge._query([Math.random()], () => defer(() => { subscribedTimes++ return lazy(1) @@ -93,9 +93,18 @@ describe('Centrifuge', () => { expect(value3).to.equal(1) }) + it("should't cache the value when no keys are passed", async () => { + let value = 0 + const query = context.centrifuge._query(null, () => defer(() => lazy(++value))) + const value1 = await query + const value2 = await query + expect(value1).to.equal(1) + expect(value2).to.equal(2) + }) + it("should invalidate the cache when there's no subscribers for a while on an infinite observable", async () => { const subject = new Subject() - const query1 = context.centrifuge._query(null, () => subject) + const query1 = context.centrifuge._query([Math.random()], () => subject) setTimeout(() => subject.next(1), 10) const value1 = await query1 setTimeout(() => subject.next(2), 10) @@ -113,7 +122,7 @@ describe('Centrifuge', () => { it('should invalidate the cache when a finite observable completes, when given a `valueCacheTime`', async () => { let value = 0 - const query1 = context.centrifuge._query(null, () => defer(() => lazy(++value)), { valueCacheTime: 1 }) + const query1 = context.centrifuge._query([Math.random()], () => defer(() => lazy(++value)), { valueCacheTime: 1 }) const value1 = await query1 const value2 = await query1 clock.tick(1_000) @@ -126,7 +135,7 @@ describe('Centrifuge', () => { it("shouldn't cache the latest value when `cache` is `false`", async () => { let value = 0 const query1 = context.centrifuge._query( - null, + [Math.random()], () => defer(() => { value++ @@ -144,7 +153,7 @@ describe('Centrifuge', () => { it("shouldn't reset the cache with a longer `observableCacheTime`", async () => { let value = 0 - const query1 = context.centrifuge._query(null, () => defer(() => lazy(++value)), { + const query1 = context.centrifuge._query([Math.random()], () => defer(() => lazy(++value)), { observableCacheTime: Infinity, }) const value1 = await query1 @@ -156,7 +165,7 @@ describe('Centrifuge', () => { it('should push new data for new subscribers to old subscribers', async () => { let value = 0 - const query1 = context.centrifuge._query(null, () => defer(() => lazy(++value)), { valueCacheTime: 1 }) + const query1 = context.centrifuge._query([Math.random()], () => defer(() => lazy(++value)), { valueCacheTime: 1 }) let lastValue: number | null = null const subscription = query1.subscribe((next) => (lastValue = next)) await query1 @@ -168,8 +177,8 @@ describe('Centrifuge', () => { }) it('should cache nested queries', async () => { - const query1 = context.centrifuge._query(['key1'], () => interval(50).pipe(map((i) => i + 1))) - const query2 = context.centrifuge._query(['key2'], () => interval(50).pipe(map((i) => (i + 1) * 2))) + const query1 = context.centrifuge._query([Math.random()], () => interval(50).pipe(map((i) => i + 1))) + const query2 = context.centrifuge._query([Math.random()], () => interval(50).pipe(map((i) => (i + 1) * 2))) const query3 = context.centrifuge._query(null, () => combineLatest([query1, query2]).pipe(map(([v1, v2]) => v1 + v2)) ) @@ -184,8 +193,8 @@ describe('Centrifuge', () => { it('should update dependant queries with values from dependencies', async () => { let i = 0 - const query1 = context.centrifuge._query(['key3'], () => of(++i), { valueCacheTime: 120 }) - const query2 = context.centrifuge._query(['key4'], () => query1.pipe(map((v1) => v1 * 10))) + const query1 = context.centrifuge._query([Math.random()], () => of(++i), { valueCacheTime: 120 }) + const query2 = context.centrifuge._query([Math.random()], () => query1.pipe(map((v1) => v1 * 10))) const value1 = await query2 clock.tick(150_000) const value3 = await query2 @@ -196,7 +205,7 @@ describe('Centrifuge', () => { it('should recreate the shared observable when the cached value is expired', async () => { let i = 0 const query1 = context.centrifuge._query( - null, + [Math.random()], () => defer(async function* () { yield await lazy(`${++i}-A`) diff --git a/src/tests/tenderly.ts b/src/tests/tenderly.ts index 049485e..22c83f4 100644 --- a/src/tests/tenderly.ts +++ b/src/tests/tenderly.ts @@ -201,7 +201,7 @@ export class TenderlyFork { display_name: `Centrifuge Sepolia Fork ${timestamp}`, fork_config: { network_id: this.chain.id, - block_number: '7116950', + block_number: '7210089', }, virtual_network_config: { chain_config: {