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

Commit

Permalink
add CdpManager.draw
Browse files Browse the repository at this point in the history
BAT doesn't like it when you try to transfer 0, which is what happens if you use lockAndDraw when just drawing
  • Loading branch information
levity committed Nov 18, 2019
1 parent 8390e24 commit cf62458
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 60 deletions.
70 changes: 37 additions & 33 deletions packages/dai-plugin-mcd/src/CdpManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default class CdpManager extends LocalService {
const jugAddress = this.get('smartContract').getContractAddress('MCD_JUG');
const isEth = ETH.isInstance(lockAmount);
const isGnt = GNT.isInstance(lockAmount);
const method = setMethod(isEth, isGnt, id);
const method = setMethod(isEth, isGnt, id, lockAmount);

This comment has been minimized.

Copy link
@jparklev

jparklev Nov 18, 2019

Contributor

what's this final arg for?

This comment has been minimized.

Copy link
@levity

levity Nov 18, 2019

Author Contributor

it's for removing in the next commit :)

const args = [
this._managerAddress,
jugAddress,
Expand Down Expand Up @@ -200,48 +200,54 @@ export default class CdpManager extends LocalService {
return this.proxyActions[method](...args);
}

@tracksTransactions
async draw(id, ilk, drawAmount, { promise }) {
return this.proxyActions.draw(
this._managerAddress,
this.get('smartContract').getContractAddress('MCD_JUG'),
this._adapterAddress('DAI'),
this.getIdBytes(id),
castAsCurrency(drawAmount, MDAI).toFixed('wei'),
{ dsProxy: true, promise }
);
}

@tracksTransactionsWithOptions({ numArguments: 5 })
wipeAndFree(id, ilk, wipeAmount = MDAI(0), freeAmount, { promise }) {
const isEth = ETH.isInstance(freeAmount);
const method = isEth ? 'wipeAndFreeETH' : 'wipeAndFreeGem';
return this.proxyActions[method](
...[
this._managerAddress,
this._adapterAddress(ilk),
this._adapterAddress('DAI'),
this.getIdBytes(id),
freeAmount.toFixed(this._precision(freeAmount)),
wipeAmount.toFixed('wei'),
{ dsProxy: true, promise }
].filter(x => x)
this._managerAddress,
this._adapterAddress(ilk),
this._adapterAddress('DAI'),
this.getIdBytes(id),
freeAmount.toFixed(this._precision(freeAmount)),
wipeAmount.toFixed('wei'),
{ dsProxy: true, promise }
);
}

@tracksTransactions
async wipe(id, wipeAmount, owner, { promise }) {
if (!owner) owner = await this.getOwner(id);
return this.proxyActions.safeWipe(
...[
this._managerAddress,
this._adapterAddress('DAI'),
this.getIdBytes(id),
wipeAmount.toFixed('wei'),
owner,
{ dsProxy: true, promise }
].filter(x => x)
this._managerAddress,
this._adapterAddress('DAI'),
this.getIdBytes(id),
wipeAmount.toFixed('wei'),
owner,
{ dsProxy: true, promise }
);
}

@tracksTransactions
unsafeWipe(id, wipeAmount, { promise }) {
return this.proxyActions.wipe(
...[
this._managerAddress,
this._adapterAddress('DAI'),
this.getIdBytes(id),
wipeAmount.toFixed('wei'),
{ dsProxy: true, promise }
].filter(x => x)
this._managerAddress,
this._adapterAddress('DAI'),
this.getIdBytes(id),
wipeAmount.toFixed('wei'),
{ dsProxy: true, promise }
);
}

Expand Down Expand Up @@ -272,14 +278,12 @@ export default class CdpManager extends LocalService {
const isEth = ETH.isInstance(freeAmount);
const method = isEth ? 'wipeAllAndFreeETH' : 'wipeAllAndFreeGem';
return this.proxyActions[method](
...[
this._managerAddress,
this._adapterAddress(ilk),
this._adapterAddress('DAI'),
this.getIdBytes(id),
freeAmount.toFixed(this._precision(freeAmount)),
{ dsProxy: true, promise }
].filter(x => x)
this._managerAddress,
this._adapterAddress(ilk),
this._adapterAddress('DAI'),
this.getIdBytes(id),
freeAmount.toFixed(this._precision(freeAmount)),
{ dsProxy: true, promise }
);
}

Expand Down
9 changes: 6 additions & 3 deletions packages/dai-plugin-mcd/src/ManagedCdp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { castAsCurrency, stringToBytes } from './utils';
import { tracksTransactionsWithOptions } from './utils/tracksTransactions';
import tracksTransactions, {
tracksTransactionsWithOptions
} from './utils/tracksTransactions';
import { ServiceRoles } from './constants';
import assert from 'assert';
import { MDAI } from './index';
Expand Down Expand Up @@ -98,8 +100,9 @@ export default class ManagedCdp {
return this._cdpManager.lock(this.id, this.ilk, amount, null);
}

drawDai(amount) {
return this.lockAndDraw(undefined, amount);
@tracksTransactions
drawDai(amount, { promise }) {
return this._cdpManager.draw(this.id, this.ilk, amount, { promise });
}

@tracksTransactionsWithOptions({ numArguments: 3 })
Expand Down
39 changes: 15 additions & 24 deletions packages/dai-plugin-mcd/test/ManagedCdp.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { takeSnapshot, restoreSnapshot } from '@makerdao/test-helpers';
import { mcdMaker, setupCollateral } from './helpers';
import { ETH, REP, MDAI, USD, GNT, OMG, DGD, BAT } from '../src';
import { ETH, MDAI, USD, BAT, GNT, DGD } from '../src';
import { ServiceRoles } from '../src/constants';
import { dummyEventData, formattedDummyEventData } from './fixtures';
import { createCurrencyRatio } from '@makerdao/currency';
Expand All @@ -13,11 +13,9 @@ beforeAll(async () => {
maker = await mcdMaker({
cdpTypes: [
{ currency: ETH, ilk: 'ETH-A' },
{ currency: BAT, ilk: 'BAT-A' }
// { currency: REP, ilk: 'REP-A' },
// { currency: OMG, ilk: 'OMG-A' },
// { currency: DGD, ilk: 'DGD-A', decimals: 9 },
// { currency: GNT, ilk: 'GNT-A' }
{ currency: BAT, ilk: 'BAT-A' },
{ currency: DGD, ilk: 'DGD-A', decimals: 9 },
{ currency: GNT, ilk: 'GNT-A' }
]
});
dai = maker.getToken(MDAI);
Expand Down Expand Up @@ -131,22 +129,17 @@ describe.each([
'BAT-A',
BAT,
async () => setupCollateral(maker, 'BAT-A', { price: 100, debtCeiling: 50 })
],
[
'GNT-A',
GNT,
async () => setupCollateral(maker, 'GNT-A', { price: 100, debtCeiling: 50 })
],
[
'DGD-A',
DGD,
async () => setupCollateral(maker, 'DGD-A', { price: 100, debtCeiling: 50 })
]
// [
// 'GNT-A',
// GNT,
// async () => setupCollateral(maker, 'GNT-A', { price: 100, debtCeiling: 50 })
// ],
// [
// 'OMG-A',
// OMG,
// async () => setupCollateral(maker, 'OMG-A', { price: 100, debtCeiling: 50 })
// ],
// [
// 'DGD-A',
// DGD,
// async () => setupCollateral(maker, 'DGD-A', { price: 100, debtCeiling: 50 })
// ]
])('%s', (ilk, GEM, setup) => {
let startingGemBalance, startingDaiBalance;

Expand Down Expand Up @@ -236,9 +229,7 @@ describe.each([

const draw = cdp.drawDai(1);
const drawHandler = jest.fn((tx, state) => {
expect(tx.metadata.method).toBe(
`lock${GEM == ETH ? 'ETH' : 'Gem'}AndDraw`
);
expect(tx.metadata.method).toBe('draw');
expect(state).toBe(txStates[drawHandler.mock.calls.length - 1]);
});
txMgr.listen(draw, drawHandler);
Expand Down

0 comments on commit cf62458

Please sign in to comment.