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

Commit

Permalink
Merge branch 'dev' of https://github.com/makerdao/dai.js into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanbennett committed Nov 12, 2019
2 parents 490eeb2 + d0826d7 commit c5f6293
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 60 deletions.
110 changes: 51 additions & 59 deletions packages/dai-plugin-mcd/src/CdpManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class CdpManager extends LocalService {
]);
this._getCdpIdsPromises = {};
this._getUrnPromises = {};
this._getEventHistoryPromises = {};
}

async getCdpIds(proxyAddress, descending = true) {
Expand Down Expand Up @@ -374,6 +375,11 @@ export default class CdpManager extends LocalService {
this._getUrnPromises = {};
}

resetEventHistoryCache(id = null) {
if (id !== null) delete this._getEventHistoryPromises[id];
else this._getEventHistoryPromises = {};
}

get _manager() {
return this.get('smartContract').getContract('CDP_MANAGER');
}
Expand Down Expand Up @@ -415,6 +421,7 @@ export default class CdpManager extends LocalService {
const MCD_VAT = this.get('smartContract').getContractAddress('MCD_VAT');

const id = managedCdp.id;
if (this._getEventHistoryPromises.hasOwnProperty(id)) return await this._getEventHistoryPromises[id];
const fromBlock = 1;
const web3 = this.get('smartContract').get('web3');

Expand Down Expand Up @@ -487,13 +494,14 @@ export default class CdpManager extends LocalService {
// For now, use an approach where we find the oldest frob
// (until we can get an indexed cdp param added to event NewCdp)
const events1 = r.reduce(
(acc, { blockNumber: block }) => {
(acc, { blockNumber: block, transactionHash: txHash }) => {
// Consider the earliest block to be the block the vault was opened
if (acc === null || acc.block > block) {
return {
type: 'OPEN',
order: 0,
block,
txHash,
id,
ilk
};
Expand All @@ -503,7 +511,7 @@ export default class CdpManager extends LocalService {
null
);
const events2 = r.reduce(
async (acc, { address, blockNumber: block, data, topics }) => {
async (acc, { blockNumber: block, data, topics }) => {
let { dart } = decodeManagerFrob(data);
acc = await acc;
dart = new BigNumber(dart);
Expand All @@ -525,16 +533,17 @@ export default class CdpManager extends LocalService {
});
acc.push(
...joinDaiEvents.map(
({ address, blockNumber: block, topics }) => ({
({ blockNumber: block, transactionHash: txHash, topics }) => ({
type: dart.lt(0) ? 'PAY_BACK' : 'GENERATE',
order: 2,
address,
block,
txHash,
id,
ilk,
adapter: MCD_JOIN_DAI.toLowerCase(),
proxy: formatAddress(topics[1]),
recipient: formatAddress(topics[2]),
amount: fromHexWei(topics[3])
amount: fromHexWei(topics[3]),
})
)
);
Expand All @@ -556,81 +565,64 @@ export default class CdpManager extends LocalService {
],
fromBlock
}),
result: r => {
return r.reduce(
(acc, { address, blockNumber: block, data, topics }) => {
let { ilk, dink } = decodeVatFrob(data);
dink = new BigNumber(dink);
// Gem withdrawal
if (dink.lt(0)) {
acc.push({
type: 'WITHDRAW',
order: 3,
block,
id,
ilk,
adapter: address.toLowerCase(),
amount: Math.abs(fromWei(dink.toString())).toString(),
proxy: formatAddress(topics[1])
});
}
// Gem deposit
if (dink.gt(0)) {
acc.push({
type: 'DEPOSIT',
order: 1,
block,
id,
ilk,
adapter: address.toLowerCase(),
amount: fromWei(dink.toString()),
proxy: formatAddress(topics[1])
});
}
return acc;
},
[]
);
}
result: r => r.map(({ address, blockNumber: block, transactionHash: txHash, data }) => {
let { ilk, dink, proxy } = decodeVatFrob(data);
dink = new BigNumber(dink);
if (dink.lt(0) || dink.gt(0)) return {
type: dink.lt(0) ? 'WITHDRAW' : 'DEPOSIT',
order: dink.lt(0) ? 3 : 1,
block,
txHash,
id,
ilk,
gem: managedCdp.currency.symbol,
adapter: address.toLowerCase(),
amount: Math.abs(fromWei(dink.toString())).toString(),
proxy
};
return null;
})
},
{
request: web3.getPastLogs({
address: CDP_MANAGER,
topics: [EVENT_GIVE, null, '0x' + padStart(id.toString(16), 64, '0')],
fromBlock
}),
result: r => r.map(({ address, blockNumber: block, data, topics }) => ({
result: r => r.map(({ blockNumber: block, transactionHash: txHash, topics }) => ({
type: 'GIVE',
block,
txHash,
prevOwner: formatAddress(topics[1]),
id: numberFromHex(topics[2]),
newOwner: formatAddress(topics[3])
}))
}
];

const results = await Promise.all(lookups.map(l => l.request));
const events = orderBy(
await Promise.all(
flatten(
await Promise.all(
results.map(async (r, i) => await lookups[i].result(r))
this._getEventHistoryPromises[id] = (async () => {
const results = await Promise.all(lookups.map(l => l.request));
const events = orderBy(
await Promise.all(
flatten(
await Promise.all(
results.map(async (r, i) => await lookups[i].result(r))
)
)
)
.filter(r => r !== null)
.map(async e => {
e.address && e.address.toLowerCase();
e.timestamp = (await getBlockTimestamp(e.block)).timestamp;
return e;
})
),
['block', 'order'],
['desc', 'desc']
).map(e => {
delete e.order;
return e;
});
return events;
),
['block', 'order'],
['desc', 'desc']
).map(e => {
delete e.order;
return e;
});
return events;
})();
return this._getEventHistoryPromises[id];
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/dai-plugin-mcd/test/CdpManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ test('get event history via web3', async () => {

expect(depositEventIdx).toBeGreaterThan(-1);
expect(events[depositEventIdx].ilk).toEqual('ETH-A');
expect(events[depositEventIdx].gem).toEqual('ETH');
expect(events[depositEventIdx].amount).toEqual('1');

expect(generateEventIdx).toBeGreaterThan(-1);
Expand All @@ -225,8 +226,13 @@ test('get event history via web3', async () => {

expect(withdrawEventIdx).toBeGreaterThan(-1);
expect(events[withdrawEventIdx].ilk).toEqual('ETH-A');
expect(events[withdrawEventIdx].gem).toEqual('ETH');
expect(events[withdrawEventIdx].amount).toEqual('0.5');

expect(giveEventIdx).toBeGreaterThan(-1);
expect(events[giveEventIdx].newOwner).toEqual('0x1000000000000000000000000000000000000000');

const cachedEvents = await cdpMgr.getEventHistory(cdp);
const openCachedEventIdx = findIndex(cachedEvents, { type: 'OPEN', id: cdp.id });
expect(openCachedEventIdx).toBeGreaterThan(-1);
});
2 changes: 1 addition & 1 deletion packages/dai-plugin-migrations/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@makerdao/dai-plugin-migrations",
"description": "Plugin to add migrations to dai.js",
"version": "0.1.7",
"version": "0.1.8",
"license": "MIT",
"main": "dist",
"scripts": {
Expand Down

0 comments on commit c5f6293

Please sign in to comment.