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

Commit

Permalink
add test for join dai
Browse files Browse the repository at this point in the history
  • Loading branch information
b-pmcg committed Apr 16, 2021
1 parent c93a708 commit 68d2239
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 79 deletions.
12 changes: 8 additions & 4 deletions packages/dai-plugin-liquidations/src/LiquidationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import tracksTransactions from './utils/tracksTransactions';
// const LOCAL_URL = 'http://localhost:3001/graphql';
const LOCAL_URL = 'https://dd0965745ea7.ngrok.io/graphql'; // temporary ngrok
import BigNumber from 'bignumber.js';
const RAD = new BigNumber('1e45');
const WAD = new BigNumber('1e18');
const RAY = new BigNumber('1e27');

export const RAD = new BigNumber('1e45');
export const WAD = new BigNumber('1e18');
export const RAY = new BigNumber('1e27');

export const nullBytes = '0x';

Expand Down Expand Up @@ -284,7 +285,10 @@ export default class LiquidationService extends PublicService {

@tracksTransactions
async joinDaiToAdapter(address, amount, { promise }) {
return await this._joinDaiAdapter().join(address, amount, { promise });
const amt = BigNumber(amount)
.times(WAD)
.toFixed();
return await this._joinDaiAdapter().join(address, amt, { promise });
}

@tracksTransactions
Expand Down
91 changes: 30 additions & 61 deletions packages/dai-plugin-liquidations/test/LiquidationService.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Maker from '@makerdao/dai';
import McdPlugin from '@makerdao/dai-plugin-mcd';
import BigNumber from 'bignumber.js';
import liquidationPlugin from '../src';
import LiquidationService from '../src/LiquidationService';
import { createVaults } from './utils';
import LiquidationService, { RAD } from '../src/LiquidationService';
import { createVaults, setLiquidationsApprovals } from './utils';

// const MCD_CLIP_LINK_A = '0x1eB71cC879960606F8ab0E02b3668EEf92CE6D98'; // kovan
// const MCD_JOIN_DAI = '0x5AA71a3ae1C0bd6ac27A1f28e1415fFFB6F15B8c'; //kovan
Expand Down Expand Up @@ -66,73 +67,49 @@ test('can bark an unsafe urn', async () => {
const id = await service.bark('LINK-A', vaultUrnAddr);

expect(id).toEqual(1);
});

// try {
// //req to move dai from me to vat
// // const joinApproval = await maker
// // .getToken('DAI')
// // .approveUnlimited(MCD_JOIN_DAI);
// // console.log('join Approval', joinApproval);
// //req to manipulate my vat dai balance (to "pay" for take calls)
// // const hopedDj = await maker
// // .service('smartContract')
// // .getContract('MCD_VAT')
// // .hope(MCD_JOIN_DAI);
// // console.log('gave hope to DJ', hopedDj.receipt.logs);
// // req for clipper to manipulate vat balance (req for each clipper)
// // const hoped = await maker
// // .service('smartContract')
// // .getContract('MCD_VAT')
// // .hope(MCD_CLIP_LINK_A);
// // console.log('gave hope to clipper', hoped.receipt.logs);
// } catch (e) {
// console.error('errow with hope', e);
// }
// const vatDaiBalA = await maker
// .service('smartContract')
// .getContract('MCD_VAT')
// .dai(maker.currentAddress());
// console.log('vat dai balance before joining', vatDaiBalA.toString());

// // check the clipper's LINK balance to verify liquidated collateral was successfully moved into clipper
// const vatGemBal_ = await maker
// .service('smartContract')
// .getContract('MCD_VAT')
// .gem('0x4c494e4b2d41', MCD_CLIP_LINK_A);

// console.log('vat GEM bal', vatGemBal_.toString());

// // try {
// // const jd = await service.joinDaiToAdapter(maker.currentAddress(), '80');
// // console.log('joined dai', jd);
// // } catch (e) {
// // console.error('error joining dai', e);
// // }

// const vatDaiBal = await maker
// .service('smartContract')
// .getContract('MCD_VAT')
// .dai(maker.currentAddress());
// console.log('vat dai balance before take', vatDaiBal.toString());
test('can join DAI to the vat', async () => {
// Set up approvals
await setLiquidationsApprovals(maker);

const vatDaiBalBefore = await maker
.service('smartContract')
.getContract('MCD_VAT')
.dai(maker.currentAddress());

const joinAmt = 80;
await service.joinDaiToAdapter(maker.currentAddress(), joinAmt);

const vatDaiBalAfter = await maker
.service('smartContract')
.getContract('MCD_VAT')
.dai(maker.currentAddress());

expect(vatDaiBalAfter).toEqual(
vatDaiBalBefore.add(
BigNumber(joinAmt)
.times(RAD)
.toFixed()
)
);
});

xtest('can successfully bid on an auction', async () => {
// // const id =
// // '0x000000000000000000000000000000000000000000000000000000000000000f';
// const id = await liquidateVaults(maker, vaultId);
// console.log('ID:', id);

// // await mineBlocks(maker.service('web3'), 10);

// // const id = 1;
// // const amt = '1';
// // const max = '3.99999999999999999999';
// // const max = '20';
// try {
// const kicks = await service.kicks();
// console.log('KICKS:', kicks.toString());

// // const active = await service.active(0);
// // console.log('ACTIVE', active.toString());

// // const sales = await service.sales(id);
// // console.log(
// // 'SALES',
Expand All @@ -143,13 +120,10 @@ test('can bark an unsafe urn', async () => {
// // sales.tic.toString(),
// // sales.top.toString()
// // );

// const count = await service.count();
// console.log('COUNT', count.toString());

// const list = await service.list();
// console.log('LIST', list);

// // const status = await service.getStatus(id);
// // console.log(
// // 'STATUS',
Expand All @@ -158,23 +132,18 @@ test('can bark an unsafe urn', async () => {
// // status.lot.toString(),
// // status.tab.toString()
// // );

// // const txo = await service.take(id, amt, max, me);
// // console.log('called take', txo.receipt.logs);
// } catch (e) {
// console.error('take error:', e);
// }

// // await mineBlocks(maker.service('web3'), 10);

// // verify collateral was successfully moved to me after 'take'
// const usrVatGemBal = await maker
// .service('smartContract')
// .getContract('MCD_VAT')
// .gem('0x4c494e4b2d41', me);

// console.log('user vat gem bal', usrVatGemBal.toString());

// const daiBal2 = await maker
// .service('smartContract')
// .getContract('MCD_VAT')
Expand Down
50 changes: 36 additions & 14 deletions packages/dai-plugin-liquidations/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,41 @@ import { LINK, DAI, ServiceRoles } from '@makerdao/dai-plugin-mcd';
import { mineBlocks } from '../../test-helpers/src';
import BigNumber from 'bignumber.js';

const ilk = '0x4c494e4b2d41';
const ilk = '0x4c494e4b2d41'; // LINK-A

export async function setLiquidationsApprovals(maker) {
const joinAddress = maker.service('smartContract').getContract('MCD_JOIN_DAI')
.address;
const linkClipperAddress = maker
.service('smartContract')
.getContract('MCD_CLIP_LINK_A').address;

try {
//req to move dai from me to vat
await maker.getToken('DAI').approveUnlimited(joinAddress);
} catch (e) {
throw new Error(`Error approving DAI allowance for join address: ${e}`);
}

try {
//req to manipulate my vat dai balance (to "pay" for take calls)
await maker
.service('smartContract')
.getContract('MCD_VAT')
.hope(joinAddress);
} catch (e) {
throw new Error(`Error hoping the join address: ${e}`);
}
try {
//req for clipper to manipulate vat balance (req for each clipper)
await maker
.service('smartContract')
.getContract('MCD_VAT')
.hope(linkClipperAddress);
} catch (e) {
throw new Error(`Error hoping the link clipper address: ${e}`);
}
}

async function getPrice(maker) {
const cdpType = maker
Expand All @@ -17,7 +51,6 @@ async function setProxyAndAllowances(maker) {

await maker.service('proxy').ensureProxy();
const proxyAddress = await maker.service('proxy').getProxyAddress();
console.log('Proxy Address: ', proxyAddress);

const linkAllowance = await linkToken.allowance(kprAddress, proxyAddress);
if (Number(linkAllowance._amount) === 0) {
Expand All @@ -29,10 +62,7 @@ async function openVaultAndLock(maker, linkAmt) {
const manager = maker.service('mcd:cdpManager');
// open vault
const vault = await manager.open('LINK-A');
const vaultId = vault.id;
console.log('Vault ID', vaultId);
// lock collateral
console.log(`Locking ${linkAmt} LINK-A`);
try {
await manager.lock(vault.id, 'LINK-A', LINK(linkAmt));
} catch (e) {
Expand All @@ -50,9 +80,7 @@ async function resetVaultStats(vault) {
async function drawDai(manager, managedVault, vaultId) {
const percent = 0.985;
const amtDai = await managedVault.daiAvailable._amount;
console.log(
`Drawing ${amtDai.times(percent).toFixed(17)} from Vault #${vaultId}`
);

try {
let drawDai = await manager.draw(
vaultId,
Expand Down Expand Up @@ -107,16 +135,10 @@ export async function createVaults(maker, network = 'testchain') {
await jug.drip(ilk);
await drawDai(manager, managedVault, vaultId);
await resetVaultStats(managedVault);
console.log('Drawing DAI', count, 'more times');
managedVault = await manager.getCdp(vaultId);
isSafe = managedVault.isSafe;
}

console.log(
'amount to draw now after drawing',
(await managedVault.daiAvailable._amount).toFixed(18)
);

while (isSafe) {
if (network === 'testchain') await mineBlocks(maker.service('web3'), 10);
await jug.drip(ilk);
Expand Down

0 comments on commit 68d2239

Please sign in to comment.