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

Commit

Permalink
Using caching service from cdpType
Browse files Browse the repository at this point in the history
  • Loading branch information
Padraic-O-Mhuiris committed Jul 9, 2020
1 parent 9b20fc4 commit d9434e7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 25 deletions.
5 changes: 2 additions & 3 deletions packages/dai-plugin-mcd/contracts/addresses/kovan.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
1 change: 0 additions & 1 deletion packages/dai-plugin-mcd/contracts/addresses/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,4 @@
"MCD_JOIN_PSM_USDC_A": "0xc2FB16712AD538EbcBabB00DfAC5fa511A4cc657",
"MCD_FLIP_PSM_USDC_A": "0x481d4eb969a821B6A8137D9c352aA7a7cBc4C8F2",
"PSM_USDC_A": "0xE13eF5966031A50A8df9D7612366a56e3BB36CB1"

}
8 changes: 4 additions & 4 deletions packages/dai-plugin-mcd/src/CdpType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,7 +90,7 @@ export default class CdpType {
return this._prefetchPromise;
}

async reset() {
reset() {
this._prefetchPromise = null;
this.cache = {};
}
Expand Down
36 changes: 25 additions & 11 deletions packages/dai-plugin-mcd/src/PsmType.js
Original file line number Diff line number Diff line change
@@ -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();
}

Expand All @@ -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];
}
}
28 changes: 24 additions & 4 deletions packages/dai-plugin-mcd/src/PsmTypeService.js
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions packages/dai-plugin-mcd/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -167,7 +167,7 @@ export const McdPlugin = {
[SAVINGS]: SavingsService,
[AUCTION]: AuctionService,
[SYSTEM_DATA]: SystemDataService,
[PSM_TYPE]: [PsmService, { psmTypes, prefetch }]
[PSM_TYPE]: [PsmTypeService, { psmTypes, prefetch }]
};
}
};
Expand Down

0 comments on commit d9434e7

Please sign in to comment.