Files
dodo-contractV2/test/vDODO/mintRedeem.test.ts

355 lines
14 KiB
TypeScript
Raw Normal View History

2021-02-01 17:27:44 +08:00
/*
Copyright 2021 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
2021-02-03 00:05:45 +08:00
import { decimalStr, fromWei } from '../utils/Converter';
2021-02-01 17:27:44 +08:00
import { logGas } from '../utils/Log';
import { VDODOContext, getVDODOContext } from '../utils/VDODOContext';
import { assert } from 'chai';
let account0: string;
let account1: string;
2021-02-03 00:05:45 +08:00
let account2: string;
2021-02-03 13:24:52 +08:00
let account3: string;
let account4: string;
2021-02-01 17:27:44 +08:00
async function init(ctx: VDODOContext): Promise<void> {
account0 = ctx.SpareAccounts[0];
account1 = ctx.SpareAccounts[1];
2021-02-03 00:05:45 +08:00
account2 = ctx.SpareAccounts[2];
2021-02-03 13:24:52 +08:00
account3 = ctx.SpareAccounts[3];
account4 = ctx.SpareAccounts[4];
2021-02-01 17:27:44 +08:00
2021-02-03 13:24:52 +08:00
await ctx.mintTestToken(account0, decimalStr("100000"));
2021-02-01 17:27:44 +08:00
await ctx.mintTestToken(account1, decimalStr("1000"));
2021-02-03 13:24:52 +08:00
await ctx.mintTestToken(account2, decimalStr("1000"));
await ctx.mintTestToken(account3, decimalStr("1000"));
await ctx.mintTestToken(account4, decimalStr("1000"));
2021-02-01 17:27:44 +08:00
2021-02-02 01:32:53 +08:00
await ctx.approveProxy(account0);
await ctx.approveProxy(account1);
2021-02-03 13:24:52 +08:00
await ctx.approveProxy(account2);
await ctx.approveProxy(account3);
await ctx.approveProxy(account4);
2021-02-01 17:27:44 +08:00
}
2021-02-03 00:05:45 +08:00
async function getGlobalState(ctx: VDODOContext, logInfo?: string) {
var alpha = await ctx.VDODO.methods.getLatestAlpha().call();
var lastRewardBlock = await ctx.VDODO.methods.lastRewardBlock().call();
var totalSuppy = await ctx.VDODO.methods.totalSupply().call();
console.log(logInfo + " alpha:" + fromWei(alpha, 'ether') + " lastRewardBlock:" + lastRewardBlock + " totalSuppy:" + fromWei(totalSuppy, 'ether'));
return [alpha, lastRewardBlock]
}
async function dodoBalance(ctx: VDODOContext, user: string, logInfo?: string) {
var dodo_contract = await ctx.DODO.methods.balanceOf(ctx.VDODO.options.address).call();
var dodo_account = await ctx.DODO.methods.balanceOf(user).call();
console.log(logInfo + " DODO:" + fromWei(dodo_contract, 'ether') + " account:" + fromWei(dodo_account, 'ether'));
return [dodo_contract, dodo_account]
}
async function getUserInfo(ctx: VDODOContext, user: string, logInfo?: string) {
var info = await ctx.VDODO.methods.userInfo(user).call();
var res = {
"VDODOAmount": info.VDODOAmount,
"superiorVDODO": info.superiorVDODO,
"superior": info.superior,
"credit": info.credit
}
console.log(logInfo + " VDODOAmount:" + fromWei(info.VDODOAmount, 'ether') + " superiorVDODO:" + fromWei(info.superiorVDODO, 'ether') + " superior:" + info.superior + " credit:" + fromWei(info.credit, 'ether'));
return res
}
async function mint(ctx: VDODOContext, user: string, mintAmount: string, superior: string) {
await ctx.VDODO.methods.mint(
mintAmount,
superior
).send(ctx.sendParam(user));
}
2021-02-02 16:16:21 +08:00
2021-02-01 17:27:44 +08:00
describe("VDODO", () => {
let snapshotId: string;
let ctx: VDODOContext;
before(async () => {
ctx = await getVDODOContext();
await init(ctx);
});
beforeEach(async () => {
snapshotId = await ctx.EVM.snapshot();
});
afterEach(async () => {
await ctx.EVM.reset(snapshotId);
});
describe("vdodo", () => {
2021-02-02 16:16:21 +08:00
it("vdodo-mint-first", async () => {
2021-02-03 00:05:45 +08:00
await getGlobalState(ctx, "before");
await getUserInfo(ctx, account0, "User before");
await getUserInfo(ctx, account1, "Superior before")
await dodoBalance(ctx, account0, "before")
await logGas(await ctx.VDODO.methods.mint(
decimalStr("100"),
account1
), ctx.sendParam(account0), "mint-fisrt");
//增加两个区块
await ctx.mintTestToken(account0, decimalStr("0"));
await ctx.mintTestToken(account0, decimalStr("0"));
let [alpha,] = await getGlobalState(ctx, "after");
let userInfo = await getUserInfo(ctx, account0, "User after");
let superiorInfo = await getUserInfo(ctx, account1, "Superior after")
let [, dodo_u] = await dodoBalance(ctx, account0, "after")
2021-02-03 13:24:52 +08:00
assert.equal(alpha, "101818181818181818181");
2021-02-03 00:05:45 +08:00
assert.equal(userInfo.VDODOAmount, "1000000000000000000");
assert.equal(userInfo.superiorVDODO, "100000000000000000");
assert.equal(userInfo.credit, "0");
assert.equal(userInfo.superior, account1);
assert.equal(superiorInfo.VDODOAmount, "100000000000000000");
assert.equal(superiorInfo.superiorVDODO, "0");
assert.equal(superiorInfo.credit, "10000000000000000000");
assert.equal(superiorInfo.superior, "0x0000000000000000000000000000000000000000");
2021-02-03 13:24:52 +08:00
assert.equal(dodo_u, "99900000000000000000000")
2021-02-02 16:16:21 +08:00
});
it("vdodo-mint-second", async () => {
2021-02-03 00:05:45 +08:00
await mint(ctx, account0, decimalStr("100"), account1)
await getGlobalState(ctx, "before");
await getUserInfo(ctx, account0, "User before");
await getUserInfo(ctx, account1, "Superior before")
await dodoBalance(ctx, account0, "before")
await logGas(await ctx.VDODO.methods.mint(
decimalStr("100"),
account1
), ctx.sendParam(account0), "mint-second");
//增加一个区块
await ctx.mintTestToken(account0, decimalStr("0"));
let [alpha,] = await getGlobalState(ctx, "after");
let userInfo = await getUserInfo(ctx, account0, "User after");
let superiorInfo = await getUserInfo(ctx, account1, "Superior after")
let [, dodo_u] = await dodoBalance(ctx, account0, "after")
2021-02-03 13:24:52 +08:00
assert.equal(alpha, "101365693130399012751");
2021-02-03 00:05:45 +08:00
assert.equal(userInfo.VDODOAmount, "1990990990990990990");
assert.equal(userInfo.superiorVDODO, "199099099099099099");
assert.equal(userInfo.credit, "0");
assert.equal(userInfo.superior, account1);
assert.equal(superiorInfo.VDODOAmount, "199099099099099099");
assert.equal(superiorInfo.superiorVDODO, "0");
assert.equal(superiorInfo.credit, "19999999999999999990");
assert.equal(superiorInfo.superior, "0x0000000000000000000000000000000000000000");
2021-02-03 13:24:52 +08:00
assert.equal(dodo_u, "99800000000000000000000")
2021-02-02 16:16:21 +08:00
});
it("vdodo-mint-second-otherSuperior", async () => {
2021-02-03 00:05:45 +08:00
await mint(ctx, account0, decimalStr("100"), account1)
await getGlobalState(ctx, "before");
await getUserInfo(ctx, account0, "User before");
await getUserInfo(ctx, account1, "Superior before")
await dodoBalance(ctx, account0, "before")
await logGas(await ctx.VDODO.methods.mint(
decimalStr("100"),
account2
), ctx.sendParam(account0), "mint-second");
//增加一个区块
await ctx.mintTestToken(account0, decimalStr("0"));
let [alpha,] = await getGlobalState(ctx, "after");
let userInfo = await getUserInfo(ctx, account0, "User after");
let superiorInfo = await getUserInfo(ctx, account1, "Superior after")
let [, dodo_u] = await dodoBalance(ctx, account0, "after")
2021-02-03 13:24:52 +08:00
assert.equal(alpha, "101365693130399012751");
2021-02-03 00:05:45 +08:00
assert.equal(userInfo.VDODOAmount, "1990990990990990990");
assert.equal(userInfo.superiorVDODO, "199099099099099099");
assert.equal(userInfo.credit, "0");
assert.equal(userInfo.superior, account1);
assert.equal(superiorInfo.VDODOAmount, "199099099099099099");
assert.equal(superiorInfo.superiorVDODO, "0");
assert.equal(superiorInfo.credit, "19999999999999999990");
assert.equal(superiorInfo.superior, "0x0000000000000000000000000000000000000000");
let otherInfo = await getUserInfo(ctx, account2, "Superior after")
assert.equal(otherInfo.VDODOAmount, "0");
assert.equal(otherInfo.superiorVDODO, "0");
assert.equal(otherInfo.credit, "0");
assert.equal(otherInfo.superior, "0x0000000000000000000000000000000000000000");
2021-02-03 13:24:52 +08:00
assert.equal(dodo_u, "99800000000000000000000")
2021-02-02 16:16:21 +08:00
});
2021-02-03 13:24:52 +08:00
it("redeem-amount-read", async () => {
2021-02-03 00:05:45 +08:00
await mint(ctx, account0, decimalStr("100"), account1)
let [dodoReceive, burnDodoAmount, withdrawFeeDodoAmount] = await ctx.VDODO.methods.getWithdrawAmount(decimalStr("1")).call();
2021-02-03 13:24:52 +08:00
assert.equal(dodoReceive, decimalStr("85"));
assert.equal(burnDodoAmount, decimalStr("0"));
assert.equal(withdrawFeeDodoAmount, decimalStr("15"));
2021-02-02 16:16:21 +08:00
});
2021-02-03 13:24:52 +08:00
2021-02-02 16:16:21 +08:00
it("redeem-partial-haveMint", async () => {
2021-02-03 13:24:52 +08:00
await mint(ctx, account0, decimalStr("10000"), account1)
await getGlobalState(ctx, "before");
await getUserInfo(ctx, account0, "User before");
await getUserInfo(ctx, account1, "Superior before")
await dodoBalance(ctx, account0, "before")
await logGas(await ctx.VDODO.methods.redeem(decimalStr("10")), ctx.sendParam(account0), "redeem-partial-haveMint");
let [alpha,] = await getGlobalState(ctx, "after");
let userInfo = await getUserInfo(ctx, account0, "User after");
let superiorInfo = await getUserInfo(ctx, account1, "Superior after")
let [, dodo_u] = await dodoBalance(ctx, account0, "after")
assert.equal(alpha, "101524380165289256197");
assert.equal(userInfo.VDODOAmount, "90000000000000000000");
assert.equal(userInfo.superiorVDODO, "9000000000000000000");
assert.equal(userInfo.credit, "0");
assert.equal(userInfo.superior, account1);
assert.equal(superiorInfo.VDODOAmount, "9000000000000000000");
assert.equal(superiorInfo.superiorVDODO, "0");
assert.equal(superiorInfo.credit, "899990909090909090910");
assert.equal(superiorInfo.superior, "0x0000000000000000000000000000000000000000");
assert.equal(dodo_u, "90850077272727272727265")
2021-02-02 16:16:21 +08:00
});
2021-02-03 13:24:52 +08:00
2021-02-02 16:16:21 +08:00
it("redeem-partial-NotMint", async () => {
//多个下级引用
2021-02-03 13:24:52 +08:00
await mint(ctx, account1, decimalStr("100"), account0)
await mint(ctx, account2, decimalStr("100"), account0)
await mint(ctx, account3, decimalStr("100"), account0)
await mint(ctx, account4, decimalStr("100"), account0)
await getGlobalState(ctx, "before");
await getUserInfo(ctx, account0, "User before");
await getUserInfo(ctx, account3, "One of referer before");
await dodoBalance(ctx, account0, "before")
let account0VdodoAmount = await ctx.VDODO.methods.balanceOf(account0).call()
2021-02-03 00:05:45 +08:00
2021-02-03 13:24:52 +08:00
await logGas(await ctx.VDODO.methods.redeem((account0VdodoAmount - 3000) + ""), ctx.sendParam(account0), "redeem-partial-NotMint");
let [alpha,] = await getGlobalState(ctx, "after");
let userInfo = await getUserInfo(ctx, account0, "User after");
let superiorInfo = await getUserInfo(ctx, account3, "One of referer after")
let [, dodo_u] = await dodoBalance(ctx, account0, "after")
assert.equal(alpha, "101909933011338172201");
assert.equal(userInfo.VDODOAmount, "393425809544634067");
assert.equal(userInfo.superiorVDODO, "0");
assert.equal(userInfo.credit, "39999999999999999876");
assert.equal(userInfo.superior, "0x0000000000000000000000000000000000000000");
assert.equal(superiorInfo.VDODOAmount, "986527067608148689");
assert.equal(superiorInfo.superiorVDODO, "98652706760814868");
assert.equal(superiorInfo.credit, "0");
assert.equal(superiorInfo.superior, account0);
assert.equal(dodo_u, "100000232341473424735076")
2021-02-02 16:16:21 +08:00
});
2021-02-03 13:24:52 +08:00
2021-02-02 16:16:21 +08:00
it("redeem-all-haveMint", async () => {
2021-02-03 13:24:52 +08:00
//第一笔mint不动防止totalSupply过小
await mint(ctx, account0, decimalStr("10000"), account1)
await mint(ctx, account1, decimalStr("100"), account2)
await getGlobalState(ctx, "before");
await getUserInfo(ctx, account1, "User before");
await getUserInfo(ctx, account2, "Superior before")
await dodoBalance(ctx, account1, "before")
let account1VdodoAmount = await ctx.VDODO.methods.balanceOf(account1).call()
await logGas(await ctx.VDODO.methods.redeem(account1VdodoAmount), ctx.sendParam(account1), "redeem-all-haveMint");
let [alpha,] = await getGlobalState(ctx, "after");
let userInfo = await getUserInfo(ctx, account1, "User after");
let superiorInfo = await getUserInfo(ctx, account2, "Superior after")
let [, dodo_u] = await dodoBalance(ctx, account1, "after")
assert.equal(alpha, "100154592821433302856");
assert.equal(userInfo.VDODOAmount, "9999090991728024725");
assert.equal(userInfo.superiorVDODO, "0");
assert.equal(userInfo.credit, "1000000000000000000000");
assert.equal(userInfo.superior, account2);
assert.equal(superiorInfo.VDODOAmount, "8998462015594");
assert.equal(superiorInfo.superiorVDODO, "0");
assert.equal(superiorInfo.credit, "0");
assert.equal(superiorInfo.superior, "0x0000000000000000000000000000000000000000");
assert.equal(dodo_u, "985084929758388492933")
2021-02-02 16:16:21 +08:00
});
2021-02-03 13:24:52 +08:00
2021-02-02 16:16:21 +08:00
it("redeem-all-NoMint", async () => {
//多个下级引用
2021-02-03 13:24:52 +08:00
await mint(ctx, account1, decimalStr("100"), account0)
await mint(ctx, account2, decimalStr("100"), account0)
await mint(ctx, account3, decimalStr("100"), account0)
await mint(ctx, account4, decimalStr("100"), account0)
await getGlobalState(ctx, "before");
await getUserInfo(ctx, account0, "User before");
await getUserInfo(ctx, account3, "One of referer before");
await dodoBalance(ctx, account0, "before")
let account0VdodoAmount = await ctx.VDODO.methods.balanceOf(account0).call()
2021-02-02 16:16:21 +08:00
2021-02-03 13:24:52 +08:00
await logGas(await ctx.VDODO.methods.redeem(account0VdodoAmount), ctx.sendParam(account0), "redeem-all-NotMint");
let [alpha,] = await getGlobalState(ctx, "after");
let userInfo = await getUserInfo(ctx, account0, "User after");
let superiorInfo = await getUserInfo(ctx, account3, "One of referer after")
let [, dodo_u] = await dodoBalance(ctx, account0, "after")
assert.equal(alpha, "101909933011338182738");
assert.equal(userInfo.VDODOAmount, "393425809544631067");
assert.equal(userInfo.superiorVDODO, "0");
assert.equal(userInfo.credit, "39999999999999999876");
assert.equal(userInfo.superior, "0x0000000000000000000000000000000000000000");
assert.equal(superiorInfo.VDODOAmount, "986527067608148689");
assert.equal(superiorInfo.superiorVDODO, "98652706760814868");
assert.equal(superiorInfo.credit, "0");
assert.equal(superiorInfo.superior, account0);
assert.equal(dodo_u, "100000232341473424994923")
});
2021-02-01 17:27:44 +08:00
})
});