-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
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,54 @@ | ||
import { assert } from "chai"; | ||
import { crvusd } from "../src/crvusd"; | ||
import { getLlamma, LlammaTemplate } from "../src/llammas"; | ||
|
||
const LLAMMAS = ['sfrxeth']; | ||
|
||
const selfLiquidationTest = (id: string) => { | ||
describe(`${id} self-liquidation test`, function () { | ||
let llamma: LlammaTemplate; | ||
|
||
before(async function () { | ||
llamma = getLlamma(id); | ||
const maxDebt = await llamma.createLoanMaxRecv(0.3, 10); | ||
await llamma.createLoan(0.3, maxDebt, 10); | ||
await llamma.swap(0, 1, Number(maxDebt) * 10, 0.05); | ||
}); | ||
|
||
it('Self-liquidations', async function () { | ||
const initialBalances = await llamma.wallet.balances(); | ||
const initialState = await llamma.userState(); | ||
const initialTokensToLiquidate = await llamma.tokensToLiquidate(); | ||
|
||
assert.isAbove(Number(initialState.collateral), 0); | ||
assert.isAbove(Number(initialState.stablecoin), 0); | ||
assert.isAbove(Number(initialState.debt), 0); | ||
assert.isAtLeast(Number(initialBalances.stablecoin), Number(initialTokensToLiquidate)); | ||
|
||
await llamma.selfLiquidate(0.1); | ||
|
||
const balances = await llamma.wallet.balances(); | ||
const state = await llamma.userState(); | ||
|
||
const tokensToLiquidate = await llamma.tokensToLiquidate(); | ||
assert.equal(Number(tokensToLiquidate), 0, 'tokens to liquidate'); | ||
assert.equal(Number(balances.collateral), Number(initialBalances.collateral) + Number(initialState.collateral), 'wallet collateral'); | ||
assert.approximately(Number(balances.stablecoin), Number(initialBalances.stablecoin) - Number(initialTokensToLiquidate), 1e-4, 'wallet stablecoin'); | ||
assert.equal(Number(state.collateral), 0, 'state callateral'); | ||
assert.equal(Number(state.stablecoin), 0, 'state stablecoin'); | ||
assert.equal(Number(state.debt), 0, 'state debt'); | ||
}); | ||
}) | ||
} | ||
|
||
describe('Self-liquidation test', async function () { | ||
this.timeout(120000); | ||
|
||
before(async function () { | ||
await crvusd.init('JsonRpc', {},{ gasPrice: 0 }); | ||
}); | ||
|
||
for (const llammaId of LLAMMAS) { | ||
selfLiquidationTest(llammaId); | ||
} | ||
}) |
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,56 @@ | ||
import { assert } from "chai"; | ||
import { crvusd } from "../src/crvusd"; | ||
import { getLlamma, LlammaTemplate } from "../src/llammas"; | ||
import { BN } from "../src/utils"; | ||
|
||
const LLAMMAS = ['sfrxeth']; | ||
|
||
const swapTest = (id: string) => { | ||
let llamma: LlammaTemplate; | ||
let maxDebt: string; | ||
|
||
before(async function () { | ||
llamma = getLlamma(id); | ||
maxDebt = await llamma.createLoanMaxRecv(0.3, 10); | ||
await llamma.createLoan(0.3, maxDebt, 10) | ||
}); | ||
|
||
after(async function () { | ||
await llamma.fullRepay(); | ||
}); | ||
|
||
|
||
describe(`${id} llamma swap test`, function () { | ||
for (let i = 0; i < 2; i++) { | ||
for (let j = 0; j < 2; j++) { | ||
if (i === j) continue; | ||
|
||
it(`${i} --> ${j}`, async function () { | ||
const initialBalances = await llamma.wallet.balances(); | ||
const swapAmount = i === 0 ? Number(maxDebt) / 10 : 0.003; | ||
const expected = Number(await llamma.swapExpected(i, j, swapAmount)); | ||
|
||
await llamma.swap(i, j, swapAmount, 0.05); | ||
|
||
const balances = await llamma.wallet.balances(); | ||
const out = Number(Object.values(balances)[j]) - Number(Object.values(initialBalances)[j]); | ||
|
||
assert.deepStrictEqual(BN(Object.values(balances)[i]).toString(), BN(Object.values(initialBalances)[i]).minus(BN(swapAmount)).toString(), 'in'); | ||
assert.isAtMost(Math.abs(out - expected) / expected, 5 * 1e-3, 'out'); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
describe('Swap test', async function () { | ||
this.timeout(120000); | ||
|
||
before(async function () { | ||
await crvusd.init('JsonRpc', {},{ gasPrice: 0 }); | ||
}); | ||
|
||
for (const llammaId of LLAMMAS) { | ||
swapTest(llammaId); | ||
} | ||
}) |