From 4d4ec3e205f8357b02fb601b0a72a37ea39962ab Mon Sep 17 00:00:00 2001 From: Tyler Sorensen Date: Mon, 2 Mar 2020 18:15:47 -1000 Subject: [PATCH] Sdk 779 remove vdb (#215) Remove functions that depend on vulcanize, since they will be moved to dai-plugin-vulcanize --- packages/dai-plugin-mcd/src/CdpManager.js | 56 +----------- packages/dai-plugin-mcd/src/CdpType.js | 16 ---- packages/dai-plugin-mcd/src/CdpTypeService.js | 4 +- packages/dai-plugin-mcd/src/ManagedCdp.js | 12 --- .../dai-plugin-mcd/src/QueryApiMcdService.js | 89 ------------------- packages/dai-plugin-mcd/src/constants.js | 1 - packages/dai-plugin-mcd/src/index.js | 14 +-- .../dai-plugin-mcd/test/CdpManager.spec.js | 16 ---- packages/dai-plugin-mcd/test/CdpType.spec.js | 24 +---- .../dai-plugin-mcd/test/ManagedCdp.spec.js | 13 +-- .../test/integration/QueryApiMcd.spec.js | 85 ------------------ 11 files changed, 7 insertions(+), 323 deletions(-) delete mode 100644 packages/dai-plugin-mcd/src/QueryApiMcdService.js delete mode 100644 packages/dai-plugin-mcd/test/integration/QueryApiMcd.spec.js diff --git a/packages/dai-plugin-mcd/src/CdpManager.js b/packages/dai-plugin-mcd/src/CdpManager.js index 4ccf77841..74ff2beb6 100644 --- a/packages/dai-plugin-mcd/src/CdpManager.js +++ b/packages/dai-plugin-mcd/src/CdpManager.js @@ -10,9 +10,7 @@ import { castAsCurrency, stringToBytes, bytesToString } from './utils'; import has from 'lodash/has'; import padStart from 'lodash/padStart'; import { MDAI, ETH, GNT } from './index'; -const { CDP_MANAGER, CDP_TYPE, SYSTEM_DATA, QUERY_API } = ServiceRoles; -import BigNumber from 'bignumber.js'; -import { RAY } from './constants'; +const { CDP_MANAGER, CDP_TYPE, SYSTEM_DATA } = ServiceRoles; import getEventHistoryImpl from './EventHistory'; export default class CdpManager extends LocalService { @@ -21,7 +19,6 @@ export default class CdpManager extends LocalService { 'smartContract', CDP_TYPE, SYSTEM_DATA, - QUERY_API, 'accounts', 'proxy', 'token', @@ -73,21 +70,6 @@ export default class CdpManager extends LocalService { return debts.reduce((a, b) => a.plus(b)); } - async getCombinedEventHistory(proxyAddress) { - const cdpIds = await this.getCdpIds(proxyAddress); - const ilksAndUrns = await Promise.all( - cdpIds.map(async c => { - const urn = await this.getUrn(c.id); - const ilk = stringToBytes(c.ilk); - return { urn, ilk }; - }) - ); - const events = await this.get(QUERY_API).getCdpEventsForArrayOfIlksAndUrns( - ilksAndUrns - ); - return this.parseFrobEvents(events, this.get(CDP_TYPE)); - } - @tracksTransactions async open(ilk, { promise, cache = true }) { const proxy = await this.get('proxy').ensureProxy({ promise }); @@ -326,42 +308,6 @@ export default class CdpManager extends LocalService { return this._manager.owns(this.getIdBytes(id)); } - parseFrobEvents(events) { - return events.map(e => { - const ilk = e.ilkIdentifier; - const currency = this.get(CDP_TYPE).getCdpType(null, ilk).currency; - const transactionHash = e.tx.transactionHash; - const rate = new BigNumber(e.ilkRate.toString()).dividedBy(RAY); - const changeInCollateral = currency.wei(Math.abs(e.dink)); - let collateralAction; - if (parseInt(e.dink) !== 0) { - collateralAction = parseInt(e.dink) > 0 ? 'lock' : 'free'; - } - const dart = MDAI.wei(Math.abs(e.dart)); - const changeInDai = dart.times(rate); - let daiAction; - if (parseInt(e.dart) !== 0) { - daiAction = parseInt(e.dart) > 0 ? 'draw' : 'wipe'; - } - const time = new Date(e.tx.era.iso); - const senderAddress = e.tx.txFrom; - //const resultingCollateral = currency.wei(e.urn.nodes[0].ink); - //const resultingDebt = MDAI.wei(e.urn.nodes[0].art); - return { - transactionHash, - changeInCollateral, - collateralAction, - changeInDai, - daiAction, - ilk, - time, - senderAddress - //resultingCollateral, - //resultingDebt - }; - }); - } - getIdBytes(id, prefix = true) { assert(typeof id === 'number', 'ID must be a number'); return (prefix ? '0x' : '') + padStart(id.toString(16), 24, '0'); diff --git a/packages/dai-plugin-mcd/src/CdpType.js b/packages/dai-plugin-mcd/src/CdpType.js index faf269cf1..fe9a8065e 100644 --- a/packages/dai-plugin-mcd/src/CdpType.js +++ b/packages/dai-plugin-mcd/src/CdpType.js @@ -58,22 +58,6 @@ export default class CdpType { return math.annualStabilityFee(this._getCached('jugInfo').duty); } - async getPriceHistory(num = 100) { - const prices = await this._cdpTypeService - .get(ServiceRoles.QUERY_API) - .getPriceHistoryForPip(this._pipAddress, num); - return Promise.all( - prices.map(async e => { - const price = this.currency.wei(e.val); - //todo: update this query to read the datetime directly from vdb once vdb is updated with that functionality - const timestamp = (await this._web3Service.getBlock(e.blockNumber)) - .timestamp; - const time = new Date(1000 * timestamp); - return { price, time }; - }) - ); - } - async ilkInfo(contract = 'vat') { return this._systemData[contract].ilks(this._ilkBytes); } diff --git a/packages/dai-plugin-mcd/src/CdpTypeService.js b/packages/dai-plugin-mcd/src/CdpTypeService.js index 3a5e9566a..4782b11b6 100644 --- a/packages/dai-plugin-mcd/src/CdpTypeService.js +++ b/packages/dai-plugin-mcd/src/CdpTypeService.js @@ -2,12 +2,12 @@ import { PublicService } from '@makerdao/services-core'; import CdpType from './CdpType'; import { ServiceRoles } from './constants'; import assert from 'assert'; -const { CDP_TYPE, SYSTEM_DATA, QUERY_API } = ServiceRoles; +const { CDP_TYPE, SYSTEM_DATA } = ServiceRoles; import * as math from './math'; export default class CdpTypeService extends PublicService { constructor(name = CDP_TYPE) { - super(name, [SYSTEM_DATA, QUERY_API]); + super(name, [SYSTEM_DATA]); } initialize(settings = {}) { diff --git a/packages/dai-plugin-mcd/src/ManagedCdp.js b/packages/dai-plugin-mcd/src/ManagedCdp.js index dae8802b3..37a9cd379 100644 --- a/packages/dai-plugin-mcd/src/ManagedCdp.js +++ b/packages/dai-plugin-mcd/src/ManagedCdp.js @@ -76,18 +76,6 @@ export default class ManagedCdp { return this._cdpManager.getOwner(this.id); } - async getEventHistory() { - const urn = await this.getUrn(); - const events = await this._cdpManager - .get(ServiceRoles.QUERY_API) - .getCdpEventsForIlkAndUrn(this.ilk, urn); - return this._cdpManager.parseFrobEvents( - events, - this._cdpManager.get(ServiceRoles.CDP_TYPE) - ); - } - - //todo: add caching? getUrn() { return this._cdpManager.getUrn(this.id); } diff --git a/packages/dai-plugin-mcd/src/QueryApiMcdService.js b/packages/dai-plugin-mcd/src/QueryApiMcdService.js deleted file mode 100644 index cf31a2c53..000000000 --- a/packages/dai-plugin-mcd/src/QueryApiMcdService.js +++ /dev/null @@ -1,89 +0,0 @@ -import { PublicService } from '@makerdao/services-core'; -import { ServiceRoles } from './constants'; -import { TESTNET_ID } from '@makerdao/dai/dist/contracts/networks'; -import { getQueryResponse } from '@makerdao/dai/dist/src/QueryApi'; - -const LOCAL_URL = 'http://localhost:5000/graphql'; -const KOVAN_SERVER_URL = 'http://vdb0.20c.com:5000/graphql'; - -export default class QueryApi extends PublicService { - constructor(name = ServiceRoles.QUERY_API) { - super(name, ['web3']); - } - - connect() { - const network = this.get('web3').network; - switch (network) { - case TESTNET_ID: - this.serverUrl = LOCAL_URL; - break; - case 'kovan': - case 42: - default: - this.serverUrl = KOVAN_SERVER_URL; - break; - } - } - - _buildFrobsQuery(ilk, urn) { - return ` - urnFrobs(ilkIdentifier: "${ilk}", urnIdentifier: "${urn}") { - nodes { - dart - dink - ilkRate - tx { - transactionHash - txFrom - era { - iso - } - } - ilkIdentifier - } - }`; - } - - async getCdpEventsForIlkAndUrn(ilkName, urn) { - const query = '{' + this._buildFrobsQuery(ilkName, urn) + '}'; - const response = await getQueryResponse(this.serverUrl, query); - return response.urnFrobs.nodes; - } - - //takes in an array of objects with ilk and urn properties - async getCdpEventsForArrayOfIlksAndUrns(cdps) { - let query = '{'; - cdps.forEach((cdp, index) => { - query += `frobs${index}: ` + this._buildFrobsQuery(cdp.ilk, cdp.urn); - }); - query += '}'; - const response = await getQueryResponse(this.serverUrl, query); - let events = []; - cdps.forEach((_, index) => { - events.push(response[`frobs${index}`].nodes); - }); - const arr = [].concat.apply([], events); //flatten array - const arrSort = arr.sort((a, b) => { - //sort by date descending - return new Date(b.tx.era.iso) - new Date(a.tx.era.iso); - }); - return arrSort; - } - - async getPriceHistoryForPip(pipAddress, num = 100) { - const query = `query ($pipAddress: String, $num: Int){ - allPipLogValues(last: $num, condition: { contractAddress: $pipAddress }){ - nodes{ - val, - blockNumber - } - } - }`; - - const response = await getQueryResponse(this.serverUrl, query, { - pipAddress, - num - }); - return response.allPipLogValues.nodes; - } -} diff --git a/packages/dai-plugin-mcd/src/constants.js b/packages/dai-plugin-mcd/src/constants.js index c00ea02d2..302153abc 100644 --- a/packages/dai-plugin-mcd/src/constants.js +++ b/packages/dai-plugin-mcd/src/constants.js @@ -5,7 +5,6 @@ export const ServiceRoles = { CDP_TYPE: 'mcd:cdpType', AUCTION: 'mcd:auction', SYSTEM_DATA: 'mcd:systemData', - QUERY_API: 'mcd:queryApi', SAVINGS: 'mcd:savings' }; diff --git a/packages/dai-plugin-mcd/src/index.js b/packages/dai-plugin-mcd/src/index.js index f30e54fc5..8738a3645 100644 --- a/packages/dai-plugin-mcd/src/index.js +++ b/packages/dai-plugin-mcd/src/index.js @@ -12,19 +12,11 @@ import SavingsService from './SavingsService'; import CdpTypeService from './CdpTypeService'; import AuctionService from './AuctionService'; import SystemDataService from './SystemDataService'; -import QueryApiMcdService from './QueryApiMcdService'; import { ServiceRoles as ServiceRoles_ } from './constants'; import BigNumber from 'bignumber.js'; export const ServiceRoles = ServiceRoles_; -const { - CDP_MANAGER, - CDP_TYPE, - SYSTEM_DATA, - AUCTION, - QUERY_API, - SAVINGS -} = ServiceRoles; +const { CDP_MANAGER, CDP_TYPE, SYSTEM_DATA, AUCTION, SAVINGS } = ServiceRoles; // look up contract ABIs using abiMap. // if an exact match is not found, prefix-match against keys ending in *, e.g. @@ -141,15 +133,13 @@ export const McdPlugin = { CDP_TYPE, AUCTION, SYSTEM_DATA, - QUERY_API, SAVINGS ], [CDP_TYPE]: [CdpTypeService, { cdpTypes, prefetch }], [CDP_MANAGER]: CdpManager, [SAVINGS]: SavingsService, [AUCTION]: AuctionService, - [SYSTEM_DATA]: SystemDataService, - [QUERY_API]: QueryApiMcdService + [SYSTEM_DATA]: SystemDataService }; } }; diff --git a/packages/dai-plugin-mcd/test/CdpManager.spec.js b/packages/dai-plugin-mcd/test/CdpManager.spec.js index 1d4b52b13..1ed5a17cf 100644 --- a/packages/dai-plugin-mcd/test/CdpManager.spec.js +++ b/packages/dai-plugin-mcd/test/CdpManager.spec.js @@ -3,9 +3,7 @@ import { mcdMaker, setupCollateral } from './helpers'; import { setMethod, transferToBag } from '../src/CdpManager'; import { ServiceRoles } from '../src/constants'; import { ETH, MDAI, GNT, DGD, BAT } from '../src'; -import { dummyEventData, formattedDummyEventData } from './fixtures'; import { takeSnapshot, restoreSnapshot } from '@makerdao/test-helpers'; - import TestAccountProvider from '@makerdao/test-helpers/src/TestAccountProvider'; let maker, cdpMgr, txMgr, snapshotData; @@ -82,20 +80,6 @@ test('getCdp can disable prefetch', async () => { expect(sameCdp._urnInfoPromise).toBeUndefined(); }); -test('getCombinedEventHistory', async () => { - const proxy = await maker.currentProxy(); - const mockFn = jest.fn(async () => dummyEventData('ETH-A')); - maker.service( - ServiceRoles.QUERY_API - ).getCdpEventsForArrayOfIlksAndUrns = mockFn; - const events = await cdpMgr.getCombinedEventHistory(proxy); - expect(mockFn).toBeCalled(); - const GEM = maker - .service(ServiceRoles.CDP_TYPE) - .getCdpType(null, events[0].ilk).currency; - expect(events).toEqual(formattedDummyEventData(GEM, events[0].ilk)); -}); - test('transaction tracking for openLockAndDraw', async () => { const cdpMgr = maker.service(ServiceRoles.CDP_MANAGER); const txMgr = maker.service('transactionManager'); diff --git a/packages/dai-plugin-mcd/test/CdpType.spec.js b/packages/dai-plugin-mcd/test/CdpType.spec.js index 72df78921..de782e294 100644 --- a/packages/dai-plugin-mcd/test/CdpType.spec.js +++ b/packages/dai-plugin-mcd/test/CdpType.spec.js @@ -2,7 +2,7 @@ import { createCurrencyRatio } from '@makerdao/currency'; import { mcdMaker, setupCollateral } from './helpers'; import { ServiceRoles } from '../src/constants'; import { ETH, MDAI, USD, BAT } from '../src'; -const { CDP_MANAGER, CDP_TYPE, QUERY_API } = ServiceRoles; +const { CDP_MANAGER, CDP_TYPE } = ServiceRoles; let maker, service; @@ -78,28 +78,6 @@ describe.each(scenarios)('%s', (ilk, GEM) => { ); }); - test('get price history', async () => { - const dummyData = [ - { - val: '177315000000000000000', - blockNumber: '1' - } - ]; - const formattedDummyData = [ - { - price: GEM(177.315), - time: new Date( - 1000 * (await cdpType._web3Service.getBlock(1)).timestamp - ) - } - ]; - const mockFn = jest.fn(async () => dummyData); - maker.service(QUERY_API).getPriceHistoryForPip = mockFn; - const prices = await cdpType.getPriceHistory(); - expect(mockFn).toBeCalled(); - expect(prices).toEqual(formattedDummyData); - }); - test('get ilk id', () => { expect(cdpType.ilk).toBe(ilk); }); diff --git a/packages/dai-plugin-mcd/test/ManagedCdp.spec.js b/packages/dai-plugin-mcd/test/ManagedCdp.spec.js index b7d1fbe32..512270200 100644 --- a/packages/dai-plugin-mcd/test/ManagedCdp.spec.js +++ b/packages/dai-plugin-mcd/test/ManagedCdp.spec.js @@ -2,10 +2,8 @@ import { takeSnapshot, restoreSnapshot } from '@makerdao/test-helpers'; import { mcdMaker, setupCollateral } from './helpers'; import { ETH, MDAI, USD, BAT, GNT, DGD } from '../src'; import { ServiceRoles } from '../src/constants'; -import { dummyEventData, formattedDummyEventData } from './fixtures'; import { createCurrencyRatio } from '@makerdao/currency'; - -const { CDP_MANAGER, QUERY_API } = ServiceRoles; +const { CDP_MANAGER } = ServiceRoles; let dai, maker, proxy, snapshotData, txMgr; @@ -159,15 +157,6 @@ describe.each([ await expectValues(cdp, { collateral: 0, debt: 0 }); }); - test('getEventHistory', async () => { - const mockFn = jest.fn(async () => dummyEventData(ilk)); - maker.service(QUERY_API).getCdpEventsForIlkAndUrn = mockFn; - const cdp = await maker.service(CDP_MANAGER).open(ilk); - const events = await cdp.getEventHistory(); - expect(mockFn).toBeCalled(); - expect(events).toEqual(formattedDummyEventData(GEM, ilk)); - }); - test('getOwner', async () => { const cdp = await maker.service(CDP_MANAGER).open(ilk); const proxy = await maker.service('proxy').currentProxy(); diff --git a/packages/dai-plugin-mcd/test/integration/QueryApiMcd.spec.js b/packages/dai-plugin-mcd/test/integration/QueryApiMcd.spec.js deleted file mode 100644 index 17c5dff5d..000000000 --- a/packages/dai-plugin-mcd/test/integration/QueryApiMcd.spec.js +++ /dev/null @@ -1,85 +0,0 @@ -import { mcdMaker } from '../helpers'; -import { ServiceRoles } from '../../src/constants'; -import { infuraProjectId } from './index'; - -let service; - -beforeAll(async () => { - const settings = { - web3: { - provider: { - infuraProjectId - } - } - }; - const network = - process.env.NETWORK === 'test' ? 'testnet' : process.env.NETWORK; - const maker = await mcdMaker({ - preset: process.env.NETWORK, - network: network, - ...settings - }); - service = maker.service(ServiceRoles.QUERY_API); -}); - -test('getPriceHistoryForPip for ETH', async () => { - const prices = await service.getPriceHistoryForPip( - '0x75dd74e8afe8110c8320ed397cccff3b8134d981' - ); - expect(!!prices[0].val && !!prices[0].blockNumber).toBe(true); -}); - -function expectFrobEvents(events) { - const event = events[0]; - expect( - !!event.dart && - !!event.dink && - !!event.ilkRate && - !!event.tx.transactionHash && - !!event.tx.txFrom && - !!event.tx.era.iso && - !!event.ilkIdentifier - ).toBe(true); - expect(new Date(events[0].tx.era.iso) > new Date(events[1].tx.era.iso)).toBe( - true - ); -} - -//these are ilks and urns that correspond to frobEvets in the current vdb data generator and remote kovan vdb instance -const frobParams = { - test: [ - { - urn: '0xd6a62aadf5a1593078b55a30c1067ff0e4d24369', - ilk: 'BEF5872' - }, - { - urn: '0xbe72af4323df8b82949d45857ea7c5d7f0cc9246', - ilk: 'D06C8A8' - } - ], - kovan: [ - { - urn: '0xAE21412A422279B72aA8641a3D5F1da4BF6cfD30', - ilk: 'ETH-A' - }, - { - urn: '0xB8de18329DAcA5c712a341596a66483366E3E3F6', - ilk: 'ETH-A' - } - ] -}; - -test('getCdpEventsForIlkAndUrn', async () => { - const events = await service.getCdpEventsForIlkAndUrn( - frobParams[process.env.NETWORK][0].ilk, - frobParams[process.env.NETWORK][0].urn - ); - expectFrobEvents(events); -}); - -test('getCdpEventsForArrayOfIlksAndUrns', async () => { - const events = await service.getCdpEventsForArrayOfIlksAndUrns( - frobParams[process.env.NETWORK] - ); - expectFrobEvents(events); -});