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

Commit

Permalink
Merge pull request #153 from makerdao/sdk-712
Browse files Browse the repository at this point in the history
add instance cache to CdpManager
  • Loading branch information
levity authored Aug 20, 2019
2 parents 34804e1 + 93f3d94 commit 1868890
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
38 changes: 33 additions & 5 deletions packages/dai-plugin-mcd/src/CdpManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ export default class CdpManager extends LocalService {
}

async getCdp(id, options) {
const cacheEnabled = !has(options, 'cache') || options.cache;
let cdp = this._getFromInstanceCache(id, cacheEnabled);
if (cdp) return cdp;

const ilk = bytesToString(await this._manager.ilks(id));
const cdp = new ManagedCdp(id, ilk, this, options);
cdp = new ManagedCdp(id, ilk, this, options);

this._putInInstanceCache(id, cdp, cacheEnabled);
if (!has(options, 'prefetch') || options.prefetch) await cdp.prefetch();
return cdp;
}
Expand Down Expand Up @@ -78,25 +84,34 @@ export default class CdpManager extends LocalService {
}

@tracksTransactions
async open(ilk, { promise }) {
async open(ilk, { promise, cache = true }) {
await this.get('proxy').ensureProxy({ promise });
const op = this.proxyActions.open(
this._managerAddress,
stringToBytes(ilk),
{ dsProxy: true, promise }
);
return ManagedCdp.create(await op, ilk, this);
const cdp = await ManagedCdp.create(await op, ilk, this);
this._putInInstanceCache(cdp.id, cdp, cache);
return cdp;
}

// ilk is required if the currency type corresponds to more than one ilk; if
// it's omitted, it is inferred from lockAmount's currency type
@tracksTransactions
async openLockAndDraw(ilk, lockAmount, drawAmount, { promise }) {
async openLockAndDraw(
ilk,
lockAmount,
drawAmount,
{ promise, cache = true }
) {
const type = this.get(CDP_TYPE).getCdpType(lockAmount.type, ilk);
const op = this.lockAndDraw(null, type.ilk, lockAmount, drawAmount, {
promise
});
return ManagedCdp.create(await op, type.ilk, this);
const cdp = await ManagedCdp.create(await op, type.ilk, this);
this._putInInstanceCache(cdp.id, cdp, cache);
return cdp;
}

@tracksTransactionsWithOptions({ numArguments: 5 })
Expand Down Expand Up @@ -267,6 +282,19 @@ export default class CdpManager extends LocalService {
? 'wei'
: this.get(CDP_TYPE).getCdpType(amount.type).decimals;
}

_getFromInstanceCache(id, enabled) {
if (!enabled) return;
if (!this._instanceCache) this._instanceCache = {};
const instance = this._instanceCache[id];
if (instance) return instance;
}

_putInInstanceCache(id, instance, enabled) {
if (!enabled) return;
if (!this._instanceCache) this._instanceCache = {};
this._instanceCache[id] = instance;
}
}

export function setMethod(isEth, id) {
Expand Down
8 changes: 6 additions & 2 deletions packages/dai-plugin-mcd/test/CdpManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,19 @@ test('getCombinedDebtValue', async () => {
expect(totalDebt).toEqual(MDAI(8));
});

test('getCdp looks up ilk', async () => {
test('getCdp looks up ilk and has cache', async () => {
const cdp = await cdpMgr.open('ETH-A');
const sameCdp = await cdpMgr.getCdp(cdp.id);
expect(sameCdp.ilk).toEqual(cdp.ilk);
expect(cdp).toBe(sameCdp);

const differentInstance = await cdpMgr.getCdp(cdp.id, { cache: false });
expect(differentInstance).not.toBe(cdp);
});

test('getCdp can disable prefetch', async () => {
const cdp = await cdpMgr.open('ETH-A');
const sameCdp = await cdpMgr.getCdp(cdp.id, { prefetch: false });
const sameCdp = await cdpMgr.getCdp(cdp.id, { prefetch: false, cache: false });
expect(sameCdp._urnInfoPromise).toBeUndefined();
});

Expand Down

0 comments on commit 1868890

Please sign in to comment.