-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ocean-api): integrate playground with ocean-api (#928)
* feat(ocean-api): integrate playground with ocean-api * automatically error if playground is configured on non regtest setup * Apply suggestions from code review
- Loading branch information
Showing
3 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { OceanApiTesting } from '../../testing/OceanApiTesting' | ||
import waitForExpect from 'wait-for-expect' | ||
|
||
describe('regtest', () => { | ||
const apiTesting = OceanApiTesting.create() | ||
|
||
beforeEach(async () => { | ||
apiTesting.playgroundEnable(false) | ||
await apiTesting.start() | ||
}) | ||
|
||
afterEach(async () => { | ||
await apiTesting.stop() | ||
}) | ||
|
||
it('should not increment block count', async () => { | ||
const initial = await apiTesting.rpc.blockchain.getBlockCount() | ||
|
||
await new Promise((resolve) => { | ||
setTimeout(_ => resolve(0), 5000) | ||
}) | ||
|
||
const next = await apiTesting.rpc.blockchain.getBlockCount() | ||
expect(next).toStrictEqual(initial) | ||
}) | ||
}) | ||
|
||
describe('playground', () => { | ||
const apiTesting = OceanApiTesting.create() | ||
|
||
beforeEach(async () => { | ||
apiTesting.playgroundEnable(true) | ||
await apiTesting.start() | ||
}) | ||
|
||
afterEach(async () => { | ||
await apiTesting.stop() | ||
}) | ||
|
||
it('should increment block count', async () => { | ||
const initial = await apiTesting.rpc.blockchain.getBlockCount() | ||
|
||
await waitForExpect(async () => { | ||
const next = await apiTesting.rpc.blockchain.getBlockCount() | ||
expect(next).toBeGreaterThan(initial) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Injectable, Logger, Module } from '@nestjs/common' | ||
import { ConfigService } from '@nestjs/config' | ||
import { Interval, ScheduleModule } from '@nestjs/schedule' | ||
import { BotLogger, Playground } from '@defichain/playground' | ||
import { ApiClient } from '@defichain/jellyfish-api-core' | ||
|
||
/** | ||
* Provide a PlaygroundRunner when PLAYGROUND_ENABLE is enabled. | ||
*/ | ||
@Module({ | ||
imports: [ | ||
ScheduleModule.forRoot() | ||
], | ||
providers: [ | ||
{ | ||
provide: 'PLAYGROUND_RUNNER', | ||
useFactory: (configService: ConfigService, apiClient: ApiClient): PlaygroundRunner | undefined => { | ||
if (configService.get<boolean>('PLAYGROUND_ENABLE') === false) { | ||
return undefined | ||
} | ||
|
||
if (configService.get<string>('API_NETWORK') !== 'regtest') { | ||
throw new Error('PLAYGROUND_ENABLE:true is only allowed on API_NETWORK:regtest') | ||
} | ||
|
||
return new PlaygroundRunner(apiClient) | ||
}, | ||
inject: [ConfigService, ApiClient] | ||
} | ||
] | ||
}) | ||
export class PlaygroundModule { | ||
} | ||
|
||
/** | ||
* Universal logger for playground using NestJS logger. | ||
*/ | ||
class PlaygroundLogger implements BotLogger { | ||
private readonly logger = new Logger(PlaygroundLogger.name) | ||
|
||
info (action: string, message: string): void { | ||
this.logger.log(`${action} ${message}`) | ||
} | ||
} | ||
|
||
@Injectable() | ||
class PlaygroundRunner { | ||
constructor ( | ||
private readonly apiClient: ApiClient, | ||
private readonly logger: PlaygroundLogger = new PlaygroundLogger(), | ||
private readonly playground: Playground = new Playground(apiClient, logger) | ||
) { | ||
} | ||
|
||
async onApplicationBootstrap (): Promise<void> { | ||
this.logger.info('onApplicationBootstrap', 'Bootstrapping') | ||
await this.playground.bootstrap() | ||
this.logger.info('onApplicationBootstrap', 'Bootstrapped') | ||
} | ||
|
||
@Interval(3000) | ||
async cycle (): Promise<void> { | ||
await this.playground.cycle() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters