This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of https://github.com/makerdao/dai.js into dev
- Loading branch information
Showing
18 changed files
with
436 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"constant":true,"inputs":[],"name":"SAIMOM","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"SCDCAP","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"action","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"cast","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"done","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"eta","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"pause","outputs":[{"internalType":"contract DSPauseAbstract","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"schedule","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"sig","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"tag","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { PublicService } from '@makerdao/services-core'; | ||
import { PAUSE } from './utils/constants'; | ||
import DsSpellAbi from '../contracts/abis/DSSpell.json'; | ||
import padStart from 'lodash/padStart'; | ||
import assert from 'assert'; | ||
import contractInfo from '../contracts/contract-info.json'; | ||
const pauseInfo = contractInfo.pause; | ||
import { netIdToName } from './utils/helpers'; | ||
|
||
export default class SpellService extends PublicService { | ||
constructor(name = 'spell') { | ||
super(name, ['smartContract', 'web3']); | ||
this.eta = {}; | ||
this.done = {}; | ||
this.action = {}; | ||
this.executionDate = {}; | ||
} | ||
|
||
getDelayInSeconds() { | ||
if (this.delay) return this.delay; | ||
this.delay = this._pauseContract().delay(); | ||
return this.delay; | ||
} | ||
|
||
async getEta(spellAddress) { | ||
if (this.eta[spellAddress]) return this.eta[spellAddress]; | ||
const spell = this.get('smartContract').getContractByAddressAndAbi( | ||
spellAddress, | ||
DsSpellAbi | ||
); | ||
const eta = await spell.eta(); | ||
if (!eta.toNumber()) return undefined; | ||
this.eta[spellAddress] = new Date(eta.toNumber() * 1000); | ||
return this.eta[spellAddress]; | ||
} | ||
|
||
async getScheduledDate(spellAddress) { | ||
const delay = await this.getDelayInSeconds(); | ||
const eta = await this.getEta(spellAddress); | ||
assert(eta, `spell ${spellAddress} has not been scheduled yet`); | ||
return new Date(eta.getTime() - delay * 1000); | ||
} | ||
|
||
async getDone(spellAddress) { | ||
if (this.done[spellAddress]) return this.done[spellAddress]; | ||
const spell = this.get('smartContract').getContractByAddressAndAbi( | ||
spellAddress, | ||
DsSpellAbi | ||
); | ||
this.done[spellAddress] = spell.done(); | ||
return this.done[spellAddress]; | ||
} | ||
|
||
async getAction(spellAddress) { | ||
if (this.action[spellAddress]) return this.action[spellAddress]; | ||
const spell = this.get('smartContract').getContractByAddressAndAbi( | ||
spellAddress, | ||
DsSpellAbi | ||
); | ||
this.action[spellAddress] = spell.action(); | ||
return this.action[spellAddress]; | ||
} | ||
|
||
async getExecutionDate(spellAddress) { | ||
if (this.executionDate[spellAddress]) | ||
return this.executionDate[spellAddress]; | ||
const done = await this.getDone(spellAddress); | ||
assert(done, `spell ${spellAddress} has not been executed`); | ||
const pauseAddress = this._pauseContract().address; | ||
const web3Service = this.get('web3'); | ||
const netId = web3Service.network; | ||
const networkName = netIdToName(netId); | ||
const paddedSpellAddress = | ||
'0x' + padStart(spellAddress.replace(/^0x/, ''), 64, '0'); | ||
const [execEvent] = await web3Service.getPastLogs({ | ||
fromBlock: pauseInfo.inception_block[networkName], | ||
toBlock: 'latest', | ||
address: pauseAddress, | ||
topics: [pauseInfo.events.exec, paddedSpellAddress] | ||
}); | ||
const { timestamp } = await web3Service.getBlock(execEvent.blockNumber); | ||
this.executionDate[spellAddress] = new Date(timestamp * 1000); | ||
return this.executionDate[spellAddress]; | ||
} | ||
|
||
refresh() { | ||
this.delay = null; | ||
this.eta = {}; | ||
this.done = {}; | ||
this.executionDate = {}; | ||
} | ||
|
||
_pauseContract() { | ||
return this.get('smartContract').getContractByName(PAUSE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
setupTestMakerInstance, | ||
restoreSnapshotOriginal, | ||
sleep | ||
} from './helpers'; | ||
|
||
let maker, spellService; | ||
|
||
afterAll(async done => { | ||
if (global.useOldChain) { | ||
await restoreSnapshotOriginal(global.snapshotId); | ||
done(); | ||
} else { | ||
global.client.restoreSnapshot(global.testchainId, global.defaultSnapshotId); | ||
await sleep(15000); | ||
|
||
await global.client.delete(global.testchainId); | ||
await sleep(15000); | ||
|
||
done(); | ||
} | ||
}); | ||
|
||
//NOTE: these tests use mainnet instead of the testchain, because the | ||
// testchain doesn't have spells (that have been executed) yet | ||
describe('use mainnet', () => { | ||
beforeAll(async () => { | ||
maker = await setupTestMakerInstance('mainnet'); | ||
spellService = maker.service('spell'); | ||
}); | ||
|
||
test('get spell execution date', async () => { | ||
const date = await spellService.getExecutionDate( | ||
'0x48916a2b11fa7a895426eedf9acf2d70523b1677' | ||
); | ||
expect(date).toEqual(new Date('2020-02-04T11:35:48.000Z')); | ||
}); | ||
|
||
test('get spell eta', async () => { | ||
const eta = await spellService.getEta( | ||
'0xf880d43bb9a32dd212c77b82a7336be31ecaee08' | ||
); | ||
expect(eta).toEqual(new Date('2020-01-26T11:53:19.000Z')); | ||
}); | ||
|
||
test('get spell done boolean', async () => { | ||
const done = await spellService.getDone( | ||
'0xf880d43bb9a32dd212c77b82a7336be31ecaee08' | ||
); | ||
expect(done).toBe(true); | ||
}); | ||
|
||
test('get spell action address', async () => { | ||
const action = await spellService.getAction( | ||
'0xf880d43bb9a32dd212c77b82a7336be31ecaee08' | ||
); | ||
expect(action).toBe('0x68D4e46c1ca8a346f82e36f324A9C0935041De79'); | ||
}); | ||
}); | ||
|
||
describe('use testchain', () => { | ||
beforeAll(async () => { | ||
maker = await setupTestMakerInstance(); | ||
spellService = maker.service('spell'); | ||
}); | ||
|
||
test('get delay', async () => { | ||
const delay = await spellService.getDelayInSeconds(); | ||
expect(delay.toNumber()).toBe(1); | ||
}); | ||
|
||
test('get date spell was scheduled', async () => { | ||
const mockGetEta = jest.fn(() => new Date('2020-02-04T11:35:48.000Z')); | ||
const tempGetEta = spellService.getEta; | ||
spellService.getEta = mockGetEta; | ||
const date = await spellService.getScheduledDate('mockSpellAddress'); | ||
expect(mockGetEta).toBeCalled(); | ||
expect(date).toEqual(new Date('2020-02-04T11:35:47.000Z')); //1 second before eta | ||
spellService.getEta = tempGetEta; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.