2020-10-27 02:53:23 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
Copyright 2020 DODO ZOO.
|
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// import * as assert from 'assert';
|
|
|
|
|
|
2020-11-30 00:04:57 +08:00
|
|
|
import { decimalStr, MAX_UINT256 } from '../utils/Converter';
|
2020-10-27 02:53:23 +08:00
|
|
|
import { logGas } from '../utils/Log';
|
|
|
|
|
import { DVMContext, getDVMContext } from '../utils/DVMContext';
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
|
import BigNumber from 'bignumber.js';
|
2020-11-29 17:38:13 +08:00
|
|
|
const truffleAssert = require('truffle-assertions');
|
2020-10-27 02:53:23 +08:00
|
|
|
|
|
|
|
|
let lp: string;
|
|
|
|
|
let trader: string;
|
|
|
|
|
|
|
|
|
|
async function init(ctx: DVMContext): Promise<void> {
|
|
|
|
|
lp = ctx.SpareAccounts[0];
|
|
|
|
|
trader = ctx.SpareAccounts[1];
|
|
|
|
|
|
|
|
|
|
await ctx.mintTestToken(lp, decimalStr("10"), decimalStr("1000"));
|
|
|
|
|
await ctx.mintTestToken(trader, decimalStr("10"), decimalStr("1000"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("Funding", () => {
|
|
|
|
|
let snapshotId: string;
|
|
|
|
|
let ctx: DVMContext;
|
|
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
|
ctx = await getDVMContext();
|
|
|
|
|
await init(ctx);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
snapshotId = await ctx.EVM.snapshot();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
|
await ctx.EVM.reset(snapshotId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("buy shares", () => {
|
|
|
|
|
|
|
|
|
|
it("buy shares from init states", async () => {
|
|
|
|
|
|
2020-11-25 17:16:49 +08:00
|
|
|
await ctx.transferBaseToDVM(lp, decimalStr("10"))
|
|
|
|
|
await logGas(ctx.DVM.methods.buyShares(lp), ctx.sendParam(lp), "buy shares");
|
2020-10-27 02:53:23 +08:00
|
|
|
|
|
|
|
|
// vault balances
|
|
|
|
|
assert.equal(
|
2020-11-20 19:55:32 +08:00
|
|
|
await ctx.BASE.methods.balanceOf(ctx.DVM.options.address).call(),
|
2020-10-27 02:53:23 +08:00
|
|
|
decimalStr("10")
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
2020-11-20 19:55:32 +08:00
|
|
|
await ctx.QUOTE.methods.balanceOf(ctx.DVM.options.address).call(),
|
2020-10-27 02:53:23 +08:00
|
|
|
decimalStr("0")
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
2020-11-20 19:55:32 +08:00
|
|
|
await ctx.DVM.methods._BASE_RESERVE_().call(),
|
2020-10-27 02:53:23 +08:00
|
|
|
decimalStr("10")
|
|
|
|
|
)
|
|
|
|
|
assert.equal(
|
2020-11-20 19:55:32 +08:00
|
|
|
await ctx.DVM.methods._QUOTE_RESERVE_().call(),
|
2020-10-27 02:53:23 +08:00
|
|
|
decimalStr("0")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// shares number
|
2020-11-20 19:55:32 +08:00
|
|
|
assert.equal(await ctx.DVM.methods.balanceOf(lp).call(), decimalStr("10"))
|
2020-10-27 02:53:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("buy shares from init states with quote != 0", async () => {
|
2020-11-25 17:16:49 +08:00
|
|
|
await ctx.transferBaseToDVM(lp, decimalStr("10"))
|
|
|
|
|
await ctx.transferQuoteToDVM(lp, decimalStr("100"))
|
|
|
|
|
await ctx.DVM.methods.buyShares(lp).send(ctx.sendParam(lp));
|
2020-11-20 19:55:32 +08:00
|
|
|
assert.equal(await ctx.DVM.methods.balanceOf(lp).call(), decimalStr("10"))
|
2020-10-27 02:53:23 +08:00
|
|
|
assert.equal(await ctx.DVM.methods.getMidPrice().call(), "102078438912577213500")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("buy shares with balanced input", async () => {
|
2020-11-25 17:16:49 +08:00
|
|
|
await ctx.transferBaseToDVM(lp, decimalStr("10"))
|
|
|
|
|
await ctx.DVM.methods.buyShares(lp).send(ctx.sendParam(lp))
|
|
|
|
|
|
|
|
|
|
await ctx.transferQuoteToDVM(trader, decimalStr("200"))
|
|
|
|
|
await ctx.DVM.methods.sellQuote(trader).send(ctx.sendParam(trader))
|
2020-10-27 02:53:23 +08:00
|
|
|
|
2020-11-20 19:55:32 +08:00
|
|
|
var vaultBaseBalance = new BigNumber(await ctx.BASE.methods.balanceOf(ctx.DVM.options.address).call())
|
|
|
|
|
var vaultQuoteBalance = new BigNumber(await ctx.QUOTE.methods.balanceOf(ctx.DVM.options.address).call())
|
2020-10-27 02:53:23 +08:00
|
|
|
var increaseRatio = new BigNumber("0.1")
|
|
|
|
|
|
2020-11-25 17:16:49 +08:00
|
|
|
await ctx.transferBaseToDVM(trader, vaultBaseBalance.multipliedBy(increaseRatio).toFixed(0))
|
|
|
|
|
await ctx.transferQuoteToDVM(trader, vaultQuoteBalance.multipliedBy(increaseRatio).toFixed(0))
|
|
|
|
|
await ctx.DVM.methods.buyShares(trader).send(ctx.sendParam(trader))
|
2020-10-27 02:53:23 +08:00
|
|
|
|
|
|
|
|
assert.equal(
|
2020-11-20 19:55:32 +08:00
|
|
|
await ctx.BASE.methods.balanceOf(ctx.DVM.options.address).call(),
|
2021-01-07 14:31:15 +08:00
|
|
|
"8852116395368015179"
|
2020-10-27 02:53:23 +08:00
|
|
|
);
|
|
|
|
|
assert.equal(
|
2020-11-20 19:55:32 +08:00
|
|
|
await ctx.QUOTE.methods.balanceOf(ctx.DVM.options.address).call(),
|
2020-11-25 17:16:49 +08:00
|
|
|
"220000000000000000000"
|
2020-10-27 02:53:23 +08:00
|
|
|
);
|
|
|
|
|
|
2020-11-20 19:55:32 +08:00
|
|
|
assert.equal(await ctx.DVM.methods.balanceOf(trader).call(), "999999999999999990")
|
2020-10-27 02:53:23 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("buy shares with unbalanced input (less quote)", async () => {
|
2020-11-25 17:16:49 +08:00
|
|
|
await ctx.transferBaseToDVM(lp, decimalStr("10"))
|
|
|
|
|
await ctx.DVM.methods.buyShares(lp).send(ctx.sendParam(lp))
|
|
|
|
|
|
|
|
|
|
await ctx.transferQuoteToDVM(trader, decimalStr("200"))
|
|
|
|
|
await ctx.DVM.methods.sellQuote(trader).send(ctx.sendParam(trader))
|
2020-10-27 02:53:23 +08:00
|
|
|
|
2020-11-20 19:55:32 +08:00
|
|
|
var vaultBaseBalance = new BigNumber(await ctx.BASE.methods.balanceOf(ctx.DVM.options.address).call())
|
|
|
|
|
var vaultQuoteBalance = new BigNumber(await ctx.QUOTE.methods.balanceOf(ctx.DVM.options.address).call())
|
2020-10-27 02:53:23 +08:00
|
|
|
var increaseRatio = new BigNumber("0.1")
|
2020-11-25 17:16:49 +08:00
|
|
|
|
|
|
|
|
await ctx.transferBaseToDVM(trader, vaultBaseBalance.multipliedBy(increaseRatio).toFixed(0))
|
|
|
|
|
await ctx.transferQuoteToDVM(trader, vaultQuoteBalance.multipliedBy(increaseRatio).div(2).toFixed(0))
|
|
|
|
|
await ctx.DVM.methods.buyShares(trader).send(ctx.sendParam(trader))
|
|
|
|
|
|
|
|
|
|
assert.equal(await ctx.DVM.methods.balanceOf(trader).call(), "500000000000000000")
|
2020-10-27 02:53:23 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("buy shares with unbalanced input (less base)", async () => {
|
2020-11-25 17:16:49 +08:00
|
|
|
await ctx.transferBaseToDVM(lp, decimalStr("10"))
|
|
|
|
|
await ctx.DVM.methods.buyShares(lp).send(ctx.sendParam(lp))
|
|
|
|
|
|
|
|
|
|
await ctx.transferQuoteToDVM(trader, decimalStr("200"))
|
|
|
|
|
await ctx.DVM.methods.sellQuote(trader).send(ctx.sendParam(trader))
|
2020-10-27 02:53:23 +08:00
|
|
|
|
2020-11-20 19:55:32 +08:00
|
|
|
var vaultBaseBalance = new BigNumber(await ctx.BASE.methods.balanceOf(ctx.DVM.options.address).call())
|
|
|
|
|
var vaultQuoteBalance = new BigNumber(await ctx.QUOTE.methods.balanceOf(ctx.DVM.options.address).call())
|
2020-10-27 02:53:23 +08:00
|
|
|
var increaseRatio = new BigNumber("0.1")
|
2020-11-25 17:16:49 +08:00
|
|
|
|
|
|
|
|
await ctx.transferBaseToDVM(trader, vaultBaseBalance.multipliedBy(increaseRatio).div(2).toFixed(0))
|
|
|
|
|
await ctx.transferQuoteToDVM(trader, vaultQuoteBalance.multipliedBy(increaseRatio).toFixed(0))
|
|
|
|
|
await ctx.DVM.methods.buyShares(trader).send(ctx.sendParam(trader))
|
|
|
|
|
|
2020-11-20 19:55:32 +08:00
|
|
|
assert.equal(await ctx.DVM.methods.balanceOf(trader).call(), "499999999999999990")
|
2020-10-27 02:53:23 +08:00
|
|
|
})
|
|
|
|
|
});
|
2020-11-05 14:10:59 +08:00
|
|
|
|
|
|
|
|
describe("sell shares", () => {
|
2020-11-25 22:21:43 +08:00
|
|
|
it("not the last one sell shares", async () => {
|
|
|
|
|
await ctx.transferBaseToDVM(lp, decimalStr("10"))
|
|
|
|
|
await ctx.transferQuoteToDVM(lp, decimalStr("100"))
|
|
|
|
|
await ctx.DVM.methods.buyShares(lp).send(ctx.sendParam(lp))
|
|
|
|
|
|
|
|
|
|
await ctx.transferBaseToDVM(trader, decimalStr("1"))
|
|
|
|
|
await ctx.transferQuoteToDVM(trader, decimalStr("10"))
|
|
|
|
|
await ctx.DVM.methods.buyShares(trader).send(ctx.sendParam(trader))
|
|
|
|
|
|
|
|
|
|
var vaultShares = new BigNumber(await ctx.DVM.methods.balanceOf(lp).call())
|
|
|
|
|
var bob = ctx.SpareAccounts[5]
|
2020-11-30 00:04:57 +08:00
|
|
|
await ctx.DVM.methods.sellShares(vaultShares.div(2).toFixed(0), bob, 0, 0, "0x", MAX_UINT256).send(ctx.sendParam(lp))
|
2020-11-25 22:21:43 +08:00
|
|
|
assert.equal(await ctx.BASE.methods.balanceOf(bob).call(), decimalStr("5"))
|
|
|
|
|
assert.equal(await ctx.QUOTE.methods.balanceOf(bob).call(), decimalStr("50"))
|
|
|
|
|
|
2020-11-30 00:04:57 +08:00
|
|
|
await ctx.DVM.methods.sellShares(vaultShares.div(2).toFixed(0), bob, 0, 0, "0x", MAX_UINT256).send(ctx.sendParam(lp))
|
2020-11-25 22:21:43 +08:00
|
|
|
assert.equal(await ctx.BASE.methods.balanceOf(bob).call(), decimalStr("10"))
|
|
|
|
|
assert.equal(await ctx.QUOTE.methods.balanceOf(bob).call(), decimalStr("100"))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("the last one sell shares", async () => {
|
2020-11-25 17:16:49 +08:00
|
|
|
await ctx.transferBaseToDVM(lp, decimalStr("10"))
|
|
|
|
|
await ctx.transferQuoteToDVM(lp, decimalStr("100"))
|
|
|
|
|
await ctx.DVM.methods.buyShares(lp).send(ctx.sendParam(lp))
|
|
|
|
|
|
2020-11-20 19:55:32 +08:00
|
|
|
var vaultShares = await ctx.DVM.methods.balanceOf(lp).call()
|
2020-11-11 16:42:00 +08:00
|
|
|
var bob = ctx.SpareAccounts[5]
|
2020-11-30 00:04:57 +08:00
|
|
|
await ctx.DVM.methods.sellShares(vaultShares, bob, 0, 0, "0x", MAX_UINT256).send(ctx.sendParam(lp))
|
2020-11-05 14:10:59 +08:00
|
|
|
assert.equal(await ctx.BASE.methods.balanceOf(bob).call(), decimalStr("10"))
|
|
|
|
|
assert.equal(await ctx.QUOTE.methods.balanceOf(bob).call(), decimalStr("100"))
|
|
|
|
|
})
|
2020-11-29 17:38:13 +08:00
|
|
|
|
|
|
|
|
it("revert cases", async () => {
|
|
|
|
|
await ctx.transferBaseToDVM(lp, decimalStr("10"))
|
|
|
|
|
await ctx.transferQuoteToDVM(lp, decimalStr("100"))
|
|
|
|
|
await ctx.DVM.methods.buyShares(lp).send(ctx.sendParam(lp))
|
|
|
|
|
|
|
|
|
|
var vaultShares = await ctx.DVM.methods.balanceOf(lp).call()
|
|
|
|
|
var bob = ctx.SpareAccounts[5]
|
|
|
|
|
await truffleAssert.reverts(
|
2020-11-30 00:04:57 +08:00
|
|
|
ctx.DVM.methods.sellShares(new BigNumber(vaultShares).multipliedBy(2), bob, 0, 0, "0x", MAX_UINT256).send(ctx.sendParam(lp)),
|
2020-11-29 17:38:13 +08:00
|
|
|
"DLP_NOT_ENOUGH"
|
|
|
|
|
)
|
|
|
|
|
await truffleAssert.reverts(
|
2020-11-30 00:04:57 +08:00
|
|
|
ctx.DVM.methods.sellShares(vaultShares, bob, decimalStr("100"), 0, "0x", MAX_UINT256).send(ctx.sendParam(lp)),
|
2020-11-29 17:38:13 +08:00
|
|
|
"WITHDRAW_NOT_ENOUGH"
|
|
|
|
|
)
|
|
|
|
|
await truffleAssert.reverts(
|
2020-11-30 00:04:57 +08:00
|
|
|
ctx.DVM.methods.sellShares(vaultShares, bob, 0, decimalStr("10000"), "0x", MAX_UINT256).send(ctx.sendParam(lp)),
|
2020-11-29 17:38:13 +08:00
|
|
|
"WITHDRAW_NOT_ENOUGH"
|
|
|
|
|
)
|
2020-12-01 00:02:33 +08:00
|
|
|
await truffleAssert.reverts(
|
|
|
|
|
ctx.DVM.methods.sellShares(vaultShares, bob, 0, decimalStr("10000"), "0x", "0").send(ctx.sendParam(lp)),
|
|
|
|
|
"TIME_EXPIRED"
|
|
|
|
|
)
|
2020-11-29 17:38:13 +08:00
|
|
|
})
|
2020-11-05 14:10:59 +08:00
|
|
|
})
|
2020-10-27 02:53:23 +08:00
|
|
|
});
|