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

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into abiMap-require
Browse files Browse the repository at this point in the history
  • Loading branch information
jparklev committed May 27, 2020
2 parents 45202db + 0840241 commit 4be5798
Show file tree
Hide file tree
Showing 72 changed files with 889 additions and 470 deletions.
4 changes: 2 additions & 2 deletions packages/dai-plugin-governance/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@makerdao/dai-plugin-governance",
"description": "A dai.js plugin for adding MKR governance support to dapps.",
"version": "0.12.1",
"version": "0.12.5",
"license": "MIT",
"repository": {
"type": "git",
Expand Down Expand Up @@ -30,7 +30,7 @@
},
"dependencies": {
"@makerdao/currency": ">=0.9.0",
"@makerdao/services-core": ">=0.9.0",
"@makerdao/services-core": "^0.10.0",
"assert": "^2.0.0",
"debug": "^4.1.1",
"ramda": "^0.25.0"
Expand Down
36 changes: 21 additions & 15 deletions packages/dai-plugin-governance/src/GovPollingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,21 @@ export default class GovPollingService extends PrivateService {
tally.rounds++;

// eliminate the weakest candidate
const [optionToEliminate] = Object.entries(tally.options)
.filter(([, optionDetails]) => !optionDetails.eliminated)
.reduce((prv, cur) => {
const [, prvVotes] = prv;
const [, curVotes] = cur;
if (
curVotes.firstChoice
.plus(curVotes.transfer)
.lt(prvVotes.firstChoice.plus(prvVotes.transfer))
)
return cur;
return prv;
});
const filteredOptions = Object.entries(tally.options).filter(
([, optionDetails]) => !optionDetails.eliminated
);

const [optionToEliminate] = filteredOptions.reduce((prv, cur) => {
const [, prvVotes] = prv;
const [, curVotes] = cur;
if (
curVotes.firstChoice
.plus(curVotes.transfer)
.lt(prvVotes.firstChoice.plus(prvVotes.transfer))
)
return cur;
return prv;
});

tally.options[optionToEliminate].eliminated = true;
tally.options[optionToEliminate].transfer = BigNumber(0);
Expand Down Expand Up @@ -208,8 +210,12 @@ export default class GovPollingService extends PrivateService {
}
);

//if there's no more rounds, the winner is the option with the most votes
if (tally.rounds > MAX_ROUNDS && !tally.winner) {
//if there's no more rounds, or if there's only one option that hasn't been eliminated
// the winner is the option with the most votes
if (
(tally.rounds > MAX_ROUNDS && !tally.winner) ||
(filteredOptions.length === 1 && !tally.winner)
) {
let max = BigNumber(0);
let maxOption;
Object.entries(tally.options).forEach(
Expand Down
8 changes: 4 additions & 4 deletions packages/dai-plugin-mcd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ multi-collateral dai contracts
### Example usage

```js
import { McdPlugin, ETH, REP, MDAI } from '@makerdao/dai-plugin-mcd';
import { McdPlugin, ETH, REP, DAI } from '@makerdao/dai-plugin-mcd';
import Maker from '@makerdao/dai';
import { createCurrency } from '@makerdao/currency';
import { tokenAddress, tokenAbi } from 'someOtherTokenData';
Expand All @@ -33,9 +33,9 @@ const maker = await Maker.create('http', {

await maker.service('proxy').ensureProxy();
const cdpManager = maker.service('mcd:cdpManager');
const cdp1 = await cdpManager.openLockAndDraw('REP-A', REP(50), MDAI(1000));
const cdp2 = await cdpManager.openLockAndDraw('ETH-A', ETH(50), MDAI(1000));
const cdp3 = await cdpManager.openLockAndDraw('TOK-Z', TOK(50), MDAI(1000));
const cdp1 = await cdpManager.openLockAndDraw('REP-A', REP(50), DAI(1000));
const cdp2 = await cdpManager.openLockAndDraw('ETH-A', ETH(50), DAI(1000));
const cdp3 = await cdpManager.openLockAndDraw('TOK-Z', TOK(50), DAI(1000));
```

Please visit [docs.makerdao.com](https://docs.makerdao.com/building-with-maker/daijs) for more documentation.
Expand Down
2 changes: 1 addition & 1 deletion packages/dai-plugin-mcd/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@makerdao/dai-plugin-mcd",
"description": "Plugin to add Multi-Collateral Dai support to dai.js",
"version": "1.4.3",
"version": "1.5.0",
"license": "MIT",
"main": "dist/index.js",
"browser": "umd/index.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/dai-plugin-mcd/src/Auction.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { WAD } from './constants';
import BigNumber from 'bignumber.js';
import { MDAI } from './index';
import { DAI } from './index';

export default class Auction {
constructor(ilk, smartContractService) {
switch (ilk) {
case MDAI.symbol:
case DAI.symbol:
this.contract = smartContractService.getContract('MCD_FLAP');
break;
case 'MKR':
Expand Down
10 changes: 5 additions & 5 deletions packages/dai-plugin-mcd/src/CdpManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ManagedCdp from './ManagedCdp';
import { castAsCurrency, stringToBytes, bytesToString } from './utils';
import has from 'lodash/has';
import padStart from 'lodash/padStart';
import { MDAI, ETH, GNT } from './index';
import { DAI, ETH, GNT } from './index';
const { CDP_MANAGER, CDP_TYPE, SYSTEM_DATA } = ServiceRoles;
import getEventHistoryImpl from './EventHistory';

Expand Down Expand Up @@ -115,13 +115,13 @@ export default class CdpManager extends LocalService {
}

@tracksTransactionsWithOptions({ numArguments: 5 })
async lockAndDraw(id, ilk, lockAmount, drawAmount = MDAI(0), { promise }) {
async lockAndDraw(id, ilk, lockAmount, drawAmount = DAI(0), { promise }) {
assert(lockAmount && drawAmount, 'both amounts must be specified');
assert(
lockAmount instanceof Currency,
'lockAmount must be a Currency value'
);
drawAmount = castAsCurrency(drawAmount, MDAI);
drawAmount = castAsCurrency(drawAmount, DAI);
const proxyAddress = await this.get('proxy').ensureProxy({ promise });
const jugAddress = this.get('smartContract').getContractAddress('MCD_JUG');
const isEth = ETH.isInstance(lockAmount);
Expand Down Expand Up @@ -194,13 +194,13 @@ export default class CdpManager extends LocalService {
this.get('smartContract').getContractAddress('MCD_JUG'),
this._adapterAddress('DAI'),
this.getIdBytes(id),
castAsCurrency(drawAmount, MDAI).toFixed('wei'),
castAsCurrency(drawAmount, DAI).toFixed('wei'),
{ dsProxy: true, promise, metadata: { id, ilk, drawAmount } }
);
}

@tracksTransactionsWithOptions({ numArguments: 5 })
wipeAndFree(id, ilk, wipeAmount = MDAI(0), freeAmount, { promise }) {
wipeAndFree(id, ilk, wipeAmount = DAI(0), freeAmount, { promise }) {
const isEth = ETH.isInstance(freeAmount);
const method = isEth ? 'wipeAndFreeETH' : 'wipeAndFreeGem';
return this.proxyActions[method](
Expand Down
6 changes: 3 additions & 3 deletions packages/dai-plugin-mcd/src/CdpType.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';
import { ServiceRoles } from './constants';
import { stringToBytes } from './utils';
import { MDAI, ETH, MWETH } from './index';
import { DAI, ETH, WETH } from './index';
import * as math from './math';

export default class CdpType {
Expand All @@ -28,7 +28,7 @@ export default class CdpType {

get totalDebt() {
const { Art, rate } = this._getCached('vatInfo');
return MDAI.wei(Art)
return DAI.wei(Art)
.times(rate)
.shiftedBy(-27);
}
Expand Down Expand Up @@ -72,7 +72,7 @@ export default class CdpType {
// separate calls
if (!this._prefetchPromise) {
const adapterAddress = this._systemData.adapterAddress(this.ilk);
const { symbol } = this.currency === ETH ? MWETH : this.currency;
const { symbol } = this.currency === ETH ? WETH : this.currency;

this._prefetchPromise = Promise.all([
this._systemData
Expand Down
22 changes: 7 additions & 15 deletions packages/dai-plugin-mcd/src/ManagedCdp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import tracksTransactions, {
} from './utils/tracksTransactions';
import { ServiceRoles } from './constants';
import assert from 'assert';
import { MDAI } from './index';
import { DAI } from './index';
import * as math from './math';

export default class ManagedCdp {
Expand Down Expand Up @@ -93,14 +93,10 @@ export default class ManagedCdp {
}

@tracksTransactionsWithOptions({ numArguments: 3 })
lockAndDraw(
lockAmount = this.currency(0),
drawAmount = MDAI(0),
{ promise }
) {
lockAndDraw(lockAmount = this.currency(0), drawAmount = DAI(0), { promise }) {
assert(lockAmount && drawAmount, 'amounts must be defined');
lockAmount = castAsCurrency(lockAmount, this.currency);
drawAmount = castAsCurrency(drawAmount, MDAI);
drawAmount = castAsCurrency(drawAmount, DAI);
return this._cdpManager.lockAndDraw(
this.id,
this.ilk,
Expand All @@ -111,12 +107,12 @@ export default class ManagedCdp {
}

wipeDai(amount) {
amount = castAsCurrency(amount, MDAI);
amount = castAsCurrency(amount, DAI);
return this._cdpManager.wipe(this.id, amount, null);
}

unsafeWipe(amount) {
amount = castAsCurrency(amount, MDAI);
amount = castAsCurrency(amount, DAI);
return this._cdpManager.unsafeWipe(this.id, amount);
}

Expand All @@ -141,13 +137,9 @@ export default class ManagedCdp {
}

@tracksTransactionsWithOptions({ numArguments: 3 })
wipeAndFree(
wipeAmount = MDAI(0),
freeAmount = this.currency(0),
{ promise }
) {
wipeAndFree(wipeAmount = DAI(0), freeAmount = this.currency(0), { promise }) {
assert(wipeAmount && freeAmount, 'amounts must be defined');
wipeAmount = castAsCurrency(wipeAmount, MDAI);
wipeAmount = castAsCurrency(wipeAmount, DAI);
freeAmount = castAsCurrency(freeAmount, this.currency);
return this._cdpManager.wipeAndFree(
this.id,
Expand Down
10 changes: 5 additions & 5 deletions packages/dai-plugin-mcd/src/SavingsService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PublicService } from '@makerdao/services-core';
import { ServiceRoles } from './constants';
import { MDAI } from './index';
import { DAI } from './index';
import BigNumber from 'bignumber.js';
import { RAY, WAD, SECONDS_PER_YEAR } from './constants';
import tracksTransactions from './utils/tracksTransactions';
Expand Down Expand Up @@ -55,13 +55,13 @@ export default class SavingsService extends PublicService {
async balance() {
const proxy = await this.get('proxy').currentProxy();

return proxy ? this.balanceOf(proxy) : MDAI(0);
return proxy ? this.balanceOf(proxy) : DAI(0);
}

async balanceOf(guy) {
const slice = new BigNumber(await this._pot.pie(guy));
const chi = await this.chi();
return MDAI(
return DAI(
slice
.times(chi)
.div(WAD)
Expand All @@ -72,7 +72,7 @@ export default class SavingsService extends PublicService {
async getTotalDai() {
const totalPie = new BigNumber(await this._pot.Pie());
const chi = await this.chi();
return MDAI(
return DAI(
totalPie
.times(chi)
.div(WAD)
Expand Down Expand Up @@ -119,7 +119,7 @@ export default class SavingsService extends PublicService {
if (type === 'DSR_WITHDRAW') sum = sum.minus(amount);
});
const balance = await this.balanceOf(address);
return balance.gt(sum) ? balance.minus(sum) : MDAI(0);
return balance.gt(sum) ? balance.minus(sum) : DAI(0);
}

resetEventHistoryCache(address = null) {
Expand Down
10 changes: 10 additions & 0 deletions packages/dai-plugin-mcd/src/SystemDataService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PublicService } from '@makerdao/services-core';
import { RAD, RAY, ServiceRoles, SECONDS_PER_YEAR } from './constants';
import BigNumber from 'bignumber.js';
import { DAI } from './index';

export default class SystemDataService extends PublicService {
constructor(name = ServiceRoles.SYSTEM_DATA) {
Expand Down Expand Up @@ -33,6 +34,15 @@ export default class SystemDataService extends PublicService {
return live.eq(0);
}

async getSystemSurplus() {
const vowAddr = this.get('smartContract').getContractAddress('MCD_VOW');
const [dai, sin] = await Promise.all([
this.vat.dai(vowAddr),
this.vat.sin(vowAddr)
]);
return DAI.rad(dai).minus(DAI.rad(sin));
}

// Helpers ----------------------------------------------

get cat() {
Expand Down
18 changes: 8 additions & 10 deletions packages/dai-plugin-mcd/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import AuctionService from './AuctionService';
import SystemDataService from './SystemDataService';
import { ServiceRoles as ServiceRoles_ } from './constants';
import BigNumber from 'bignumber.js';
import wethAbi from '../contracts/abis/WETH9.json';

export const ServiceRoles = ServiceRoles_;
const { CDP_MANAGER, CDP_TYPE, SYSTEM_DATA, AUCTION, SAVINGS } = ServiceRoles;
Expand Down Expand Up @@ -54,11 +55,8 @@ export const MKR = createCurrency('MKR');
export const USD = createCurrency('USD');
export const USD_ETH = createCurrencyRatio(USD, ETH);

// these are prefixed with M so that they don't override their SCD versions--
// otherwise, adding the MCD plugin would break MCD. maybe there's a better way
// to work around this?
export const MWETH = createCurrency('MWETH');
export const MDAI = createCurrency('MDAI');
export const WETH = createCurrency('WETH');
export const DAI = createCurrency('DAI');

// Casting for savings dai
export const DSR_DAI = createCurrency('DSR-DAI');
Expand Down Expand Up @@ -88,8 +86,8 @@ export const ALLOWANCE_AMOUNT = BigNumber(
export const defaultTokens = [
...new Set([
...defaultCdpTypes.map(type => type.currency),
MDAI,
MWETH,
DAI,
WETH,
SAI,
DSR_DAI
])
Expand All @@ -107,7 +105,7 @@ export const McdPlugin = {
}));
}
const tokens = uniqBy(cdpTypes, 'currency').map(
({ currency, address, abi, decimals }, idx) => {
({ currency, address, abi, decimals }) => {
const data =
address && abi ? { address, abi } : addContracts[currency.symbol];
assert(data, `No address and ABI found for "${currency.symbol}"`);
Expand All @@ -127,8 +125,8 @@ export const McdPlugin = {
smartContract: { addContracts },
token: {
erc20: [
{ currency: MDAI, address: addContracts.MCD_DAI.address },
{ currency: MWETH, address: addContracts.ETH.address },
{ currency: DAI, address: addContracts.MCD_DAI.address },
{ currency: WETH, address: addContracts.ETH.address, abi: wethAbi },
...tokens
]
},
Expand Down
Loading

0 comments on commit 4be5798

Please sign in to comment.