diff --git a/packages/dai-plugin-mcd/contracts/addresses/kovan.json b/packages/dai-plugin-mcd/contracts/addresses/kovan.json index 3a36c3d2a..94b82ec73 100644 --- a/packages/dai-plugin-mcd/contracts/addresses/kovan.json +++ b/packages/dai-plugin-mcd/contracts/addresses/kovan.json @@ -68,8 +68,7 @@ "MCD_JOIN_KNC_A": "0xE42427325A0e4c8e194692FfbcACD92C2C381598", "MCD_FLIP_KNC_A": "0xf14Ec3538C86A31bBf576979783a8F6dbF16d571", - "MCD_FLIP_PSM_USDC_A": "0x481d4eb969a821B6A8137D9c352aA7a7cBc4C8F2" + "MCD_FLIP_PSM_USDC_A": "0x481d4eb969a821B6A8137D9c352aA7a7cBc4C8F2", "MCD_JOIN_PSM_USDC_A": "0xaf774866c4b165b88c558C7891290698cf7Ed6B5", - "PSM_USDC_A": "0xAfB79e262B7A07666161B95992934eB0AE230Fb4", - + "PSM_USDC_A": "0xAfB79e262B7A07666161B95992934eB0AE230Fb4" } diff --git a/packages/dai-plugin-mcd/contracts/addresses/mainnet.json b/packages/dai-plugin-mcd/contracts/addresses/mainnet.json index f0ec1d16f..f16363a29 100644 --- a/packages/dai-plugin-mcd/contracts/addresses/mainnet.json +++ b/packages/dai-plugin-mcd/contracts/addresses/mainnet.json @@ -75,5 +75,4 @@ "MCD_JOIN_PSM_USDC_A": "0xc2FB16712AD538EbcBabB00DfAC5fa511A4cc657", "MCD_FLIP_PSM_USDC_A": "0x481d4eb969a821B6A8137D9c352aA7a7cBc4C8F2", "PSM_USDC_A": "0xE13eF5966031A50A8df9D7612366a56e3BB36CB1" - } diff --git a/packages/dai-plugin-mcd/src/CdpType.js b/packages/dai-plugin-mcd/src/CdpType.js index 6280081d7..d3d31012b 100644 --- a/packages/dai-plugin-mcd/src/CdpType.js +++ b/packages/dai-plugin-mcd/src/CdpType.js @@ -6,13 +6,13 @@ import * as math from './math'; export default class CdpType { constructor( - cdpTypeService, + typeService, { currency, ilk, decimals }, options = { prefetch: true } ) { assert(currency && ilk, 'currency and ilk are required'); - this._cdpTypeService = cdpTypeService; - this._systemData = cdpTypeService.get(ServiceRoles.SYSTEM_DATA); + this._cdpTypeService = typeService; + this._systemData = typeService.get(ServiceRoles.SYSTEM_DATA); this._web3Service = this._systemData.get('smartContract').get('web3'); this.currency = currency; this.decimals = decimals || 18; @@ -90,7 +90,7 @@ export default class CdpType { return this._prefetchPromise; } - async reset() { + reset() { this._prefetchPromise = null; this.cache = {}; } diff --git a/packages/dai-plugin-mcd/src/PsmType.js b/packages/dai-plugin-mcd/src/PsmType.js index 2723de84c..7171e9efc 100644 --- a/packages/dai-plugin-mcd/src/PsmType.js +++ b/packages/dai-plugin-mcd/src/PsmType.js @@ -1,20 +1,24 @@ import assert from 'assert'; -import { stringToBytes } from './utils'; +import CdpType from './CdpType'; export default class PsmType { constructor( psmTypeService, { currency, ilk, decimals }, - options = { prefect: true } + options = { prefetch: true } ) { assert(currency && ilk, 'currency and ilk are required'); this._psmTypeService = psmTypeService; + this._cdpType = new CdpType( + this._psmTypeService, + { currency, ilk, decimals }, + options + ); this.currency = currency; this.decimals = decimals; this.ilk = ilk; - this._ilkBytes = stringToBytes(this.ilk); - this.cache = {}; + this._cache = {}; if (options.prefetch) this.prefetch(); } @@ -26,17 +30,27 @@ export default class PsmType { return null; } - prefetch() { - return null; + async prefetch() { + if (!this._prefetchPromise) { + this._prefetchPromise = Promise.all([this._cdpType.prefetch()]); + } + return this._prefetchPromise; + } + + reset() { + this._cdpType.reset(); + this._cache = {}; } - async reset() { - this._prefetchPromise = null; - this.cache = {}; + cache() { + return { + ...this._cache, + ...this._cdpType.cache + }; } _getCached(name) { - assert(this.cache[name], `${name} is not cached`); - return this.cache[name]; + assert(this.cache()[name], `${name} is not cached`); + return this.cache()[name]; } } diff --git a/packages/dai-plugin-mcd/src/PsmTypeService.js b/packages/dai-plugin-mcd/src/PsmTypeService.js index 4a7ba6934..196809c17 100644 --- a/packages/dai-plugin-mcd/src/PsmTypeService.js +++ b/packages/dai-plugin-mcd/src/PsmTypeService.js @@ -1,12 +1,13 @@ +import assert from 'assert'; + import { PublicService } from '@makerdao/services-core'; import { ServiceRoles } from './constants'; import PsmType from './PsmType'; - -const { PSM_TYPE } = ServiceRoles; +const { PSM_TYPE, SYSTEM_DATA } = ServiceRoles; export default class PsmService extends PublicService { constructor(name = PSM_TYPE) { - super(name); + super(name, [SYSTEM_DATA]); this.reset = this.resetAllPsmTypes; } @@ -18,7 +19,26 @@ export default class PsmService extends PublicService { } async connect() { - if (this.settings.prefetch) await this.prefetchAllCdpTypes(); + if (this.settings.prefetch) await this.prefetchAllPsmTypes(); + } + + getPsmType(currency, ilk) { + const types = this.psmTypes.filter( + t => + (!currency || t.currency.symbol === currency.symbol) && + (!ilk || ilk === t.ilk) + ); + if (types.length === 1) return types[0]; + + const label = [ + currency && `currency ${currency.symbol}`, + ilk && `ilk ${ilk}` + ] + .filter(x => x) + .join(', '); + + assert(types.length <= 1, `${label} matches more than one psm type`); + assert(types.length > 0, `${label} matches no psm type`); } async prefetchAllPsmTypes() { diff --git a/packages/dai-plugin-mcd/src/index.js b/packages/dai-plugin-mcd/src/index.js index 3006a55c3..9ef72e6f5 100644 --- a/packages/dai-plugin-mcd/src/index.js +++ b/packages/dai-plugin-mcd/src/index.js @@ -12,7 +12,7 @@ import SavingsService from './SavingsService'; import CdpTypeService from './CdpTypeService'; import AuctionService from './AuctionService'; import SystemDataService from './SystemDataService'; -import PsmService from './PsmService'; +import PsmTypeService from './PsmTypeService'; import { ServiceRoles as ServiceRoles_ } from './constants'; import BigNumber from 'bignumber.js'; @@ -167,7 +167,7 @@ export const McdPlugin = { [SAVINGS]: SavingsService, [AUCTION]: AuctionService, [SYSTEM_DATA]: SystemDataService, - [PSM_TYPE]: [PsmService, { psmTypes, prefetch }] + [PSM_TYPE]: [PsmTypeService, { psmTypes, prefetch }] }; } };