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

Ethers.js Update #303

Merged
merged 12 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147,216 changes: 147,216 additions & 0 deletions .yarn/releases/yarn-1.19.1.cjs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


yarn-path ".yarn/releases/yarn-1.19.1.cjs"
8 changes: 4 additions & 4 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.15.0",
"version": "0.16.1",
"license": "MIT",
"repository": {
"type": "git",
Expand Down Expand Up @@ -29,14 +29,14 @@
},
"dependencies": {
"@makerdao/currency": "0.9.9",
"@makerdao/dai": "^0.41.0",
"@makerdao/services-core": "^0.11.0",
"@makerdao/dai": "^0.42.1",
"@makerdao/services-core": "^0.11.2",
"assert": "^2.0.0",
"debug": "^4.1.1",
"ramda": "^0.25.0"
},
"peerDependencies": {
"@makerdao/dai": "^0.41.0-alpha.4"
"@makerdao/dai": "^0.42.0"
},
"devDependencies": {
"@makerdao/dai-plugin-config": "^0.2.14-rc.1",
Expand Down
22 changes: 12 additions & 10 deletions packages/dai-plugin-governance/src/ApproveLinkTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ export default class ApproveLinkTransaction {
}

_parseLogs(logs) {
const { LinkConfirmed } = this._contract.interface.events;
const web3 = this._txMgr.get('web3')._web3;
const topic = web3.utils.keccak256(
web3.utils.toHex(LinkConfirmed.signature)
const [topic] = this._contract.interface.encodeFilterTopics(
'LinkConfirmed',
[]
Comment on lines +31 to +33
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good example of an ethers built in function that simplifies things a little bit from before.

);
const receiptEvent = logs.filter(

const [receiptEvent] = logs.filter(
e => e.topics[0].toLowerCase() === topic.toLowerCase() //filter for LinkConfirmed events
);
const parsedLog = LinkConfirmed.parse(
receiptEvent[0].topics,
receiptEvent[0].data
);
this.proxyAddress = parsedLog['voteProxy'];

const { args: eventArgs } = this._contract.interface.parseLog({
data: receiptEvent.data,
topics: receiptEvent.topics
});

this.proxyAddress = eventArgs['voteProxy'];
}
}
4 changes: 2 additions & 2 deletions packages/dai-plugin-governance/src/ChiefService.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ export default class ChiefService extends LocalService {
getNumDeposits(address) {
return this._chiefContract()
.deposits(address)
.then(MKR.wei);
.then(n => MKR.wei(n._hex));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all our contracts are now created with ethers 5, the returned number value is of BN.js bignumber type (not bignumber.js). Passing a BN bignumber into currency does not work, so instead we now have to pass it's hex value (passing .toString() would also work, but I figure the hex value is less prone to a conversion error).

This is the first of many examples of converting an ethers returned BN type into a hex for currency to consume.

}

getApprovalCount(address) {
return this._chiefContract()
.approvals(address)
.then(MKR.wei);
.then(n => MKR.wei(n._hex));
}

getHat() {
Expand Down
6 changes: 3 additions & 3 deletions packages/dai-plugin-governance/src/EsmService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class EsmService extends PrivateService {

async thresholdAmount() {
const min = await this._esmContract().min();
return getCurrency(min, MKR).shiftedBy(-18);
return getCurrency(min._hex, MKR).shiftedBy(-18);
}

async emergencyShutdownActive() {
Expand All @@ -25,15 +25,15 @@ export default class EsmService extends PrivateService {

async getTotalStaked() {
const total = await this._esmContract().Sum();
return getCurrency(total, MKR).shiftedBy(-18);
return getCurrency(total._hex, MKR).shiftedBy(-18);
}

async getTotalStakedByAddress(address = false) {
if (!address) {
address = this.get('web3').currentAddress();
}
const total = await this._esmContract().sum(address);
return getCurrency(total, MKR).shiftedBy(-18);
return getCurrency(total._hex, MKR).shiftedBy(-18);
}

@tracksTransactionsWithOptions({ numArguments: 3 })
Expand Down
2 changes: 1 addition & 1 deletion packages/dai-plugin-governance/src/VoteDelegateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class VoteDelegateService extends LocalService {
const bal = await this._getStakedBalanceForAddress(
delegateAddress,
address
).then(MKR.wei);
).then(val => MKR.wei(val._hex));

return bal;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/dai-plugin-liquidations/contracts/abis/Clipper.json
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,7 @@
"name": "upchost",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function",
"constant": true
"type": "function"
},
{
"inputs": [],
Expand Down
6 changes: 3 additions & 3 deletions packages/dai-plugin-liquidations/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@makerdao/dai-plugin-liquidations",
"description": "Plugin to add liquidations to dai.js",
"version": "0.2.1-alpha.1",
"version": "0.3.1",
"license": "MIT",
"typings": "dist/index.d.ts",
"main": "dist/index.js",
Expand All @@ -17,11 +17,11 @@
"test": "yarn testchain --ci jest --runInBand"
},
"peerDependencies": {
"@makerdao/dai": "^0.41.0"
"@makerdao/dai": "^0.42.0"
},
"dependencies": {
"@makerdao/currency": "0.9.9",
"@makerdao/services-core": "^0.11.0",
"@makerdao/services-core": "^0.11.2",
"bignumber.js": "^8.1.1",
"ramda": "^0.27.1"
},
Expand Down
13 changes: 6 additions & 7 deletions packages/dai-plugin-liquidations/src/LiquidationService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PublicService } from '@makerdao/services-core';
import { utils } from '@makerdao/dai';
import { utils as daiUtils } from '@makerdao/dai';
import assert from 'assert';
import { utils } from 'ethers';
import tracksTransactions from './utils/tracksTransactions';
const MAINNET_SERVER_URL = 'https://api.makerdao.com/graphql';
//const LOCAL_URL = 'http://localhost:3001/graphql';
Expand All @@ -12,13 +13,11 @@ export const RAY = new BigNumber('1e27');

export const nullBytes = '0x';

const { bytes32ToNumber, numberToBytes32 } = utils;
const { bytes32ToNumber, numberToBytes32 } = daiUtils;

export const stringToBytes = str => {
assert(!!str, 'argument is falsy');
assert(typeof str === 'string', 'argument is not a string');
return '0x' + Buffer.from(str).toString('hex');
};
export function stringToBytes(str) {
return utils.formatBytes32String(str);
Copy link
Contributor Author

@b-pmcg b-pmcg Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ethers is more strict about types, in some places where we used to pass a function signature as 0x4554482d41, we now have to pass the full 32 bytes, eg. 0x4554482d41000000000000000000000000000000000000000000000000000000. Fortunately ethers utils has a built in function for that.

}

//hard-coded for now, but can get from pips, which you can get from ilk registry
const medianizers = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ describe('LiquidationService', () => {
test('can get status from on chain', async () => {
const { needsRedo, price, lot, tab } = await service.getStatus(ilk, 1);

const collateralAmount = new BigNumber(lot).div(WAD);
const daiNeeded = new BigNumber(tab).div(RAD);
const collateralAmount = new BigNumber(lot._hex).div(WAD);
const daiNeeded = new BigNumber(tab._hex).div(RAD);

expect(collateralAmount.gt(0)).toBe(true);
expect(new BigNumber(price).div(RAY).toString()).toEqual('19500');
expect(new BigNumber(price._hex).div(RAY).toString()).toEqual('19500');
expect(daiNeeded.toNumber()).toBeCloseTo(1000);
expect(needsRedo).toEqual(false);
});
Expand Down
10 changes: 5 additions & 5 deletions 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.7.0",
"version": "1.8.1",
"license": "MIT",
"main": "dist/index.js",
"unpkg": "dist/index.js",
Expand All @@ -21,13 +21,13 @@
"test:integration": "export NETWORK='test' && yarn test --config ./test/config/jestIntegrationConfig.json"
},
"peerDependencies": {
"@makerdao/dai": "^0.41.0-alpha.2",
"@makerdao/services-core": "^0.11.0-alpha.1"
"@makerdao/dai": "^0.42",
"@makerdao/services-core": "^0.11.1"
},
"dependencies": {
"@makerdao/currency": "0.9.9",
"@makerdao/dai": "^0.41.0",
"@makerdao/services-core": "^0.11.0",
"@makerdao/dai": "^0.42.1",
"@makerdao/services-core": "^0.11.2",
"bignumber.js": "^8.1.1",
"rxjs": "^6.5.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/dai-plugin-mcd/src/Auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class Auction {
const beg = await this.contract.beg();
// the contract's BigNumber implementation (bn.js) doesn't support decimals,
// so we use bignumber.js instead
return new BigNumber(beg)
return new BigNumber(beg._hex)
.div(WAD)
.minus(1)
.toNumber();
Expand Down
18 changes: 8 additions & 10 deletions packages/dai-plugin-mcd/src/CdpManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,16 @@ export default class CdpManager extends LocalService {
const managerContract = this.get('smartContract').getContract(
'CDP_MANAGER'
);
const web3 = this.get('web3')._web3;
const { NewCdp } = managerContract.interface.events;
const topic = web3.utils.keccak256(web3.utils.toHex(NewCdp.signature));
const receiptEvent = logs.filter(
const [topic] = managerContract.interface.encodeFilterTopics('NewCdp', []);
const [receiptEvent] = logs.filter(
e => e.topics[0].toLowerCase() === topic.toLowerCase()
);
const parsedLog = NewCdp.parse(
receiptEvent[0].topics,
receiptEvent[0].data
);
assert(parsedLog['cdp'], 'could not find log for NewCdp event');
return parseInt(parsedLog['cdp']);
const { args: eventArgs } = managerContract.interface.parseLog({
data: receiptEvent.data,
topics: receiptEvent.topics
});
assert(eventArgs['cdp'], 'could not find log for NewCdp event');
return parseInt(eventArgs['cdp']);
}

getEventHistory(managedCdp) {
Expand Down
4 changes: 2 additions & 2 deletions packages/dai-plugin-mcd/src/CdpType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export default class CdpType {

get totalDebt() {
const { Art, rate } = this._getCached('vatInfo');
return DAI.wei(Art)
.times(rate)
return DAI.wei(Art._hex)
.times(rate._hex)
.shiftedBy(-27);
}

Expand Down
38 changes: 23 additions & 15 deletions packages/dai-plugin-mcd/src/EventHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export default async function getEventHistory(cdpManager, managedCdp, cache) {
const MCD_JOIN_DAI = cdpManager
.get('smartContract')
.getContractAddress('MCD_JOIN_DAI');
const MCD_JOIN_SAI = cdpManager
.get('smartContract')
.getContractAddress('MCD_JOIN_SAI');
// const MCD_JOIN_SAI = cdpManager
// .get('smartContract')
// .getContractAddress('MCD_JOIN_SAI');
Comment on lines +71 to +73
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to leave this commented out?

const CDP_MANAGER = cdpManager
.get('smartContract')
.getContractAddress('CDP_MANAGER');
Expand All @@ -87,7 +87,6 @@ export default async function getEventHistory(cdpManager, managedCdp, cache) {
if (cache[id]) return cache[id];

const web3 = cdpManager.get('web3');
const utils = web3._web3.utils;

// 8600000 is 2019-09-22 on mainnet and 2018-09-04 on kovan
const genesis = [1, 42].includes(web3.network) ? 8600000 : 1;
Expand All @@ -103,19 +102,26 @@ export default async function getEventHistory(cdpManager, managedCdp, cache) {
const urnHandler = (await cdpManager.getUrn(id)).toLowerCase();
const ilk = managedCdp.ilk;

const { NewCdp } = cdpManager
const Bite = cdpManager
.get('smartContract')
.getContract('MCD_CAT')
.interface.getEvent('Bite');

const [newCdpTopic0] = cdpManager
.get('smartContract')
.getContract('CDP_MANAGER').interface.events;
.getContract('CDP_MANAGER')
.interface.encodeFilterTopics('NewCdp', []);

const { Bite } = cdpManager
const [biteTopic0] = cdpManager
.get('smartContract')
.getContract('MCD_CAT').interface.events;
.getContract('MCD_CAT')
.interface.encodeFilterTopics('Bite', []);

const cdpManagerNewCdp = {
request: web3.getPastLogs({
address: CDP_MANAGER,
topics: [
utils.keccak256(utils.toHex(NewCdp.signature)),
newCdpTopic0,
null,
null,
'0x' + padStart(id.toString(16), 64, '0')
Expand Down Expand Up @@ -157,7 +163,13 @@ export default async function getEventHistory(cdpManager, managedCdp, cache) {

const [joinDaiEvents, cdpMoveEvents] = await Promise.all([
web3.getPastLogs({
address: [MCD_JOIN_DAI, MCD_JOIN_SAI],
/*
FIXME: ethers 5 doesn't support passing an array of addresses into getLogs
as we move further away from SCD, join Sai events are less likely, but
we should eventually re-add this functionality by duplicating this call for MCD_JOIN_SAI.
*/
// address: [MCD_JOIN_DAI, MCD_JOIN_SAI],
address: MCD_JOIN_DAI,
Comment on lines +166 to +172
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here. How do you think we should address this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it there commented out as a reminder, but I don't know if we should prioritize it at the moment. Given that nobody uses the event history in the mcd plugin anymore since Oasis moved away from it, and given that it only affects parsing SCD events, I'm willing to release this version without it. If anyone feels differently let me know.

The fix would be to duplicate the same function using MCD_JOIN_SAI as the address, merge & sort the results.

topics: [
dart.lt(0) ? EVENT_DAI_ADAPTER_JOIN : EVENT_DAI_ADAPTER_EXIT,
proxy
Expand Down Expand Up @@ -277,11 +289,7 @@ export default async function getEventHistory(cdpManager, managedCdp, cache) {
const catBite = (address, fromBlock, toBlock) => ({
request: web3.getPastLogs({
address,
topics: [
utils.keccak256(utils.toHex(Bite.signature)),
null,
'0x' + padStart(urnHandler.slice(2), 64, '0')
],
topics: [biteTopic0, null, '0x' + padStart(urnHandler.slice(2), 64, '0')],
fromBlock,
toBlock
}),
Expand Down
8 changes: 4 additions & 4 deletions packages/dai-plugin-mcd/src/SavingsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class SavingsService extends PublicService {
}

async balanceOf(guy) {
const slice = new BigNumber(await this._pot.pie(guy));
const slice = new BigNumber((await this._pot.pie(guy))._hex);
const chi = await this.chi();
return DAI(
slice
Expand All @@ -72,7 +72,7 @@ export default class SavingsService extends PublicService {
}

async getTotalDai() {
const totalPie = new BigNumber(await this._pot.Pie());
const totalPie = new BigNumber((await this._pot.Pie())._hex);
const chi = await this.chi();
return DAI(
totalPie
Expand All @@ -83,12 +83,12 @@ export default class SavingsService extends PublicService {
}

async getYearlyRate() {
const dsr = new BigNumber(await this._pot.dsr()).div(RAY);
const dsr = new BigNumber((await this._pot.dsr())._hex).div(RAY);
return dsr.pow(SECONDS_PER_YEAR).minus(1);
}

async chi() {
return new BigNumber(await this._pot.chi()).div(RAY);
return new BigNumber((await this._pot.chi())._hex).div(RAY);
}

get _proxyActions() {
Expand Down
4 changes: 2 additions & 2 deletions packages/dai-plugin-mcd/src/SystemDataService.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export default class SystemDataService extends PublicService {
this.vat.dai(vowAddr),
this.vat.sin(vowAddr)
]);
return DAI.rad(dai).minus(DAI.rad(sin));
return DAI.rad(dai._hex).minus(DAI.rad(sin._hex));
}

async getTotalDai() {
return DAI.rad(await this.vat.debt());
return DAI.rad((await this.vat.debt())._hex);
}

// Helpers ----------------------------------------------
Expand Down
Loading