Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Commit

Permalink
Merge branch 'main' into canonbrother/future-swap-index-add
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Jun 1, 2022
2 parents f25bcc0 + 4868add commit b9ec957
Show file tree
Hide file tree
Showing 22 changed files with 432 additions and 253 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM node:16-alpine3.13
ENV NODE_OPTIONS="--max-old-space-size=4096"

# add curl for docker healthcheck capability
RUN apk --no-cache add curl
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
[![codecov](https://codecov.io/gh/JellyfishSDK/whale/branch/main/graph/badge.svg?token=kBCC9qSRrA)](https://codecov.io/gh/JellyfishSDK/whale)
[![npm](https://img.shields.io/npm/v/@defichain/whale-api-client)](https://www.npmjs.com/package/@defichain/whale-api-client)

## Following the migration path outlined in [jellyfish#580](https://github.com/JellyfishSDK/jellyfish/issues/580), this repository has been consolidated into [JellyfishSDK/jellyfish/apps/whale-api](https://github.com/JellyfishSDK/jellyfish/tree/main/apps/whale-api).

The docker artifact will be renamed
to [ghcr.io/jellyfishsdk/whale-api](https://github.com/JellyfishSDK/jellyfish/pkgs/container/whale-api). The versions
of the published npm packages will be bumped to follow jellyfish's release cycle.

# DeFi Whale

The super index for DeFi Blockchain to simplify DeFi light implementation.
Expand Down
443 changes: 258 additions & 185 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
"packages:publish:latest": "lerna exec --no-private -- npm publish --tag latest --access public"
},
"dependencies": {
"@defichain/jellyfish-address": "^2.44.0",
"@defichain/jellyfish-api-core": "^2.44.0",
"@defichain/jellyfish-api-jsonrpc": "^2.44.0",
"@defichain/jellyfish-crypto": "^2.44.0",
"@defichain/jellyfish-json": "^2.44.0",
"@defichain/jellyfish-network": "^2.44.0",
"@defichain/jellyfish-testing": "^2.44.0",
"@defichain/jellyfish-transaction": "^2.44.0",
"@defichain/jellyfish-transaction-builder": "^2.44.0",
"@defichain/jellyfish-wallet": "^2.44.0",
"@defichain/testcontainers": "^2.44.0",
"@defichain/testing": "^2.44.0",
"@defichain/jellyfish-address": "^2.45.5",
"@defichain/jellyfish-api-core": "^2.45.5",
"@defichain/jellyfish-api-jsonrpc": "^2.45.5",
"@defichain/jellyfish-crypto": "^2.45.5",
"@defichain/jellyfish-json": "^2.45.5",
"@defichain/jellyfish-network": "^2.45.5",
"@defichain/jellyfish-testing": "^2.45.5",
"@defichain/jellyfish-transaction": "^2.45.5",
"@defichain/jellyfish-transaction-builder": "^2.45.5",
"@defichain/jellyfish-wallet": "^2.45.5",
"@defichain/testcontainers": "^2.45.5",
"@defichain/testing": "^2.45.5",
"@nestjs/common": "^8.4.2",
"@nestjs/config": "^2.0.0",
"@nestjs/core": "^8.4.2",
Expand All @@ -50,7 +50,7 @@
"cache-manager": "^3.6.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"defichain": "^2.44.0",
"defichain": "^2.45.5",
"graphology": "^0.24.1",
"graphology-components": "^1.5.2",
"graphology-simple-path": "^0.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
const container = new MasterNodeRegTestContainer()
const service = new StubService(container)
const client = new StubWhaleApiClient(service)
let collateralTokenId1: string

/* eslint-disable no-lone-blocks */

Expand Down Expand Up @@ -107,7 +106,7 @@ beforeAll(async () => {
}

{
collateralTokenId1 = await testing.rpc.loan.setCollateralToken({
await testing.rpc.loan.setCollateralToken({
token: 'AAPL',
factor: new BigNumber(0.1),
fixedIntervalPriceId: 'AAPL/USD'
Expand Down Expand Up @@ -244,7 +243,7 @@ describe('get', () => {
it('should get collateral token by symbol', async () => {
const data = await client.loan.getCollateralToken('AAPL')
expect(data).toStrictEqual({
tokenId: collateralTokenId1,
tokenId: expect.any(String),
factor: '0.1',
token: {
collateralAddress: expect.any(String),
Expand All @@ -271,7 +270,7 @@ describe('get', () => {
symbolKey: expect.any(String),
tradeable: true
},
activateAfterBlock: 110,
activateAfterBlock: 0,
fixedIntervalPriceId: 'AAPL/USD',
activePrice: {
active: {
Expand Down
8 changes: 3 additions & 5 deletions packages/whale-api-client/__tests__/stub.client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Method, ResponseAsString, WhaleApiClient, WhaleRpcClient } from '../src'
import { StubService } from './stub.service'
import version from '../src/version'
import AbortController from 'abort-controller'

/**
Expand All @@ -9,18 +8,17 @@ import AbortController from 'abort-controller'
*/
export class StubWhaleApiClient extends WhaleApiClient {
constructor (readonly service: StubService) {
super({ url: 'not required for stub service' })
super({})
}

async requestAsString (method: Method, path: string, body?: string): Promise<ResponseAsString> {
if (this.service.app === undefined) {
throw new Error('StubService is not yet started.')
}

const version = this.options.version as string
const res = await this.service.app.inject({
method: method,
url: `/${version}/regtest/${path}`,
url: `/v0.0/regtest/${path}`,
payload: body,
headers: method !== 'GET' ? { 'Content-Type': 'application/json' } : {}
})
Expand All @@ -44,7 +42,7 @@ export class StubWhaleRpcClient extends WhaleRpcClient {

const res = await this.service.app.inject({
method: 'POST',
url: `/${version}/regtest/rpc`,
url: '/v0.0/regtest/rpc',
payload: body,
headers: { 'Content-Type': 'application/json' }
})
Expand Down
4 changes: 2 additions & 2 deletions packages/whale-api-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"dist"
],
"scripts": {
"build": "./prebuild && tsc"
"build": "tsc"
},
"dependencies": {
"@defichain/jellyfish-api-jsonrpc": "^2.44.0",
"@defichain/jellyfish-api-jsonrpc": "^2.45.5",
"abort-controller": "^3.0.0",
"cross-fetch": "^3.1.5",
"url-search-params-polyfill": "8.1.1"
Expand Down
3 changes: 0 additions & 3 deletions packages/whale-api-client/prebuild

This file was deleted.

4 changes: 2 additions & 2 deletions packages/whale-api-client/src/api/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ export interface AddressHistory {
amounts: string[]
block: {
height: number
hash: string
time: number
hash?: string
time?: number
}
}

Expand Down
12 changes: 11 additions & 1 deletion packages/whale-api-client/src/api/poolpairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,24 @@ export interface PoolPairData {
symbol: string
displaySymbol: string
reserve: string // BigNumber
blockCommission: string // BigNumber
blockCommission: string // BigNumber,
fee?: {
pct?: string // BigNumber
inPct?: string // BigNumber
outPct?: string // BigNumber
}
}
tokenB: {
id: string
symbol: string
displaySymbol: string
reserve: string // BigNumber
blockCommission: string // BigNumber
fee?: {
pct?: string // BigNumber
inPct?: string // BigNumber
outPct?: string // BigNumber
}
}
priceRatio: {
ab: string // BigNumber
Expand Down
2 changes: 0 additions & 2 deletions packages/whale-api-client/src/version.ts

This file was deleted.

9 changes: 4 additions & 5 deletions packages/whale-api-client/src/whale.api.client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'url-search-params-polyfill'
import AbortController from 'abort-controller'
import fetch from 'cross-fetch'
import version from './version'
import { raiseIfError, WhaleClientException, WhaleClientTimeoutException } from './errors'
import { ApiPagedResponse, WhaleApiResponse } from './whale.api.response'
import { Address } from './api/address'
Expand All @@ -22,7 +21,7 @@ import { Loan } from './api/loan'
* WhaleApiClient Options
*/
export interface WhaleApiClientOptions {
url: string
url?: string

/**
* Millis before request is aborted.
Expand All @@ -48,7 +47,7 @@ export interface WhaleApiClientOptions {
const DEFAULT_OPTIONS: WhaleApiClientOptions = {
url: 'https://ocean.defichain.com',
timeout: 60000,
version: version,
version: 'v0',
network: 'mainnet'
}

Expand Down Expand Up @@ -81,7 +80,7 @@ export class WhaleApiClient {
protected readonly options: WhaleApiClientOptions
) {
this.options = { ...DEFAULT_OPTIONS, ...options }
this.options.url = this.options.url.replace(/\/$/, '')
this.options.url = this.options.url?.replace(/\/$/, '')
}

/**
Expand Down Expand Up @@ -160,7 +159,7 @@ export class WhaleApiClient {
*/
async requestAsString (method: Method, path: string, body?: string): Promise<ResponseAsString> {
const { url: urlString, version, network, timeout } = this.options
const url = `${urlString}/${version as string}/${network as string}/${path}`
const url = `${urlString as string}/${version as string}/${network as string}/${path}`

const controller = new AbortController()
const id = setTimeout(() => controller.abort(), timeout)
Expand Down
4 changes: 2 additions & 2 deletions packages/whale-api-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"build": "tsc"
},
"dependencies": {
"@defichain/jellyfish-transaction-builder": "^2.44.0",
"@defichain/jellyfish-wallet": "^2.44.0",
"@defichain/jellyfish-transaction-builder": "^2.45.5",
"@defichain/jellyfish-wallet": "^2.45.5",
"@defichain/whale-api-client": "0.0.0"
}
}
42 changes: 42 additions & 0 deletions src/module.api/actuator.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,46 @@ describe('with peers', () => {
status: 'ok'
})
})

it('/_actuator/probes/readiness stale', async () => {
// Time travel 100 mins
const mockTime = jest.spyOn(Date, 'now').mockImplementation(() => new Date().getTime() + 6_000_000)

const res = await app.inject({
method: 'GET',
url: '/_actuator/probes/readiness'
})
expect(res.statusCode).toStrictEqual(503)
expect(res.json()).toStrictEqual({
details: {
defid: {
blocks: expect.any(Number),
headers: expect.any(Number),
initialBlockDownload: false,
peers: 1,
status: 'up'
},
model: {
status: 'down'
}
},
error: {
model: {
status: 'down'
}
},
info: {
defid: {
blocks: expect.any(Number),
headers: expect.any(Number),
initialBlockDownload: false,
peers: 1,
status: 'up'
}
},
status: 'error'
})

mockTime.mockRestore()
})
})
4 changes: 2 additions & 2 deletions src/module.api/cache/semaphore.cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ beforeAll(async () => {
cache = testing.get(SemaphoreCache)
})

it('should run twice with they same key, due to concurrency of 2', async () => {
it('should run one with they same key, due to concurrency of 1', async () => {
let counter = 0

async function fetch (): Promise<string> {
Expand All @@ -31,7 +31,7 @@ it('should run twice with they same key, due to concurrency of 2', async () => {
cache.get('collide-1', fetch)
])

expect(counter).toStrictEqual(2)
expect(counter).toStrictEqual(1)
})

it('key should not collide', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/module.api/cache/semaphore.cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Cache } from 'cache-manager'

@Injectable()
export class SemaphoreCache {
static MAX_CONCURRENCY = 2
static MAX_CONCURRENCY = 1
static TIMEOUT = 45000

protected readonly cache: GlobalCache
Expand Down
8 changes: 3 additions & 5 deletions src/module.api/loan.collateral.controller.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const container = new LoanMasterNodeRegTestContainer()
let app: NestFastifyApplication
let controller: LoanController

let collateralTokenId1: string

beforeAll(async () => {
await container.start()
await container.waitForWalletCoinbaseMaturity()
Expand Down Expand Up @@ -68,7 +66,7 @@ beforeAll(async () => {
})
await testing.generate(1)

collateralTokenId1 = await testing.rpc.loan.setCollateralToken({
await testing.rpc.loan.setCollateralToken({
token: 'AAPL',
factor: new BigNumber(0.1),
fixedIntervalPriceId: 'AAPL/USD'
Expand Down Expand Up @@ -175,10 +173,10 @@ describe('get', () => {
const data = await controller.getCollateral('AAPL')
expect(data).toStrictEqual(
{
tokenId: collateralTokenId1,
tokenId: expect.any(String),
fixedIntervalPriceId: 'AAPL/USD',
factor: '0.1',
activateAfterBlock: 108,
activateAfterBlock: 0,
token: {
collateralAddress: expect.any(String),
creation: {
Expand Down
2 changes: 1 addition & 1 deletion src/module.api/loan.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class LoanController {
tokenId: detail.tokenId,
token: mapTokenData(id, tokenInfo),
factor: detail.factor.toFixed(),
activateAfterBlock: detail.activateAfterBlock.toNumber(),
activateAfterBlock: 0,
fixedIntervalPriceId: detail.fixedIntervalPriceId,
activePrice: await this.getActivePrice(detail.fixedIntervalPriceId)
}
Expand Down
Loading

0 comments on commit b9ec957

Please sign in to comment.