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

358 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';
2021-02-05 17:04:53 +08:00
let dodoTeam: string;
2021-02-01 17:27:44 +08:00
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> {
2021-02-05 17:04:53 +08:00
dodoTeam = ctx.Deployer;
2021-02-01 17:27:44 +08:00
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) {
2021-02-09 18:56:35 +08:00
let [alpha,] = await ctx.VDODO.methods.getLatestAlpha().call();
var lastRewardBlock = await ctx.VDODO.methods._LAST_REWARD_BLOCK_().call();
2021-02-03 00:05:45 +08:00
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 = {
2021-02-09 18:56:35 +08:00
"stakingPower": info.stakingPower,
"superiorSP": info.superiorSP,
2021-02-03 00:05:45 +08:00
"superior": info.superior,
"credit": info.credit
}
2021-02-09 18:56:35 +08:00
console.log(logInfo + " stakingPower:" + fromWei(info.stakingPower, 'ether') + " superiorSP:" + fromWei(info.superiorSP, 'ether') + " superior:" + info.superior + " credit:" + fromWei(info.credit, 'ether'));
2021-02-03 00:05:45 +08:00
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");
2021-02-05 17:04:53 +08:00
await getUserInfo(ctx, dodoTeam, "Superior before")
2021-02-03 00:05:45 +08:00
await dodoBalance(ctx, account0, "before")
await logGas(await ctx.VDODO.methods.mint(
decimalStr("100"),
2021-02-05 17:04:53 +08:00
dodoTeam
2021-02-03 00:05:45 +08:00
), 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");
2021-02-05 17:04:53 +08:00
let superiorInfo = await getUserInfo(ctx, dodoTeam, "Superior after")
2021-02-03 00:05:45 +08:00
let [, dodo_u] = await dodoBalance(ctx, account0, "after")
2021-02-03 13:24:52 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(alpha, "1018181818181818181");
assert.equal(userInfo.stakingPower, "100000000000000000000");
assert.equal(userInfo.superiorSP, "10000000000000000000");
2021-02-03 00:05:45 +08:00
assert.equal(userInfo.credit, "0");
2021-02-05 17:04:53 +08:00
assert.equal(userInfo.superior, dodoTeam);
2021-02-03 00:05:45 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(superiorInfo.stakingPower, "10000000000000000000");
assert.equal(superiorInfo.superiorSP, "0");
2021-02-03 00:05:45 +08:00
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-05 17:04:53 +08:00
await mint(ctx, account0, decimalStr("100"), dodoTeam)
2021-02-03 00:05:45 +08:00
await getGlobalState(ctx, "before");
await getUserInfo(ctx, account0, "User before");
2021-02-05 17:04:53 +08:00
await getUserInfo(ctx, dodoTeam, "Superior before")
2021-02-03 00:05:45 +08:00
await dodoBalance(ctx, account0, "before")
await logGas(await ctx.VDODO.methods.mint(
decimalStr("100"),
2021-02-05 17:04:53 +08:00
dodoTeam
2021-02-03 00:05:45 +08:00
), 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");
2021-02-05 17:04:53 +08:00
let superiorInfo = await getUserInfo(ctx, dodoTeam, "Superior after")
2021-02-03 00:05:45 +08:00
let [, dodo_u] = await dodoBalance(ctx, account0, "after")
2021-02-03 13:24:52 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(alpha, "1013656931303990126");
assert.equal(userInfo.stakingPower, "199099099099099099188");
assert.equal(userInfo.superiorSP, "19909909909909909918");
2021-02-03 00:05:45 +08:00
assert.equal(userInfo.credit, "0");
2021-02-05 17:04:53 +08:00
assert.equal(userInfo.superior, dodoTeam);
2021-02-03 00:05:45 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(superiorInfo.stakingPower, "19909909909909909918");
assert.equal(superiorInfo.superiorSP, "0");
assert.equal(superiorInfo.credit, "19999999999999999999");
2021-02-03 00:05:45 +08:00
assert.equal(superiorInfo.superior, "0x0000000000000000000000000000000000000000");
2021-02-03 13:24:52 +08:00
assert.equal(dodo_u, "99800000000000000000000")
2021-02-05 17:04:53 +08:00
2021-02-02 16:16:21 +08:00
});
it("vdodo-mint-second-otherSuperior", async () => {
2021-02-05 17:04:53 +08:00
await mint(ctx, account0, decimalStr("100"), dodoTeam)
await mint(ctx, account1, decimalStr("100"), dodoTeam)
2021-02-03 00:05:45 +08:00
await getGlobalState(ctx, "before");
await getUserInfo(ctx, account0, "User before");
2021-02-05 17:04:53 +08:00
await getUserInfo(ctx, dodoTeam, "Superior before")
2021-02-03 00:05:45 +08:00
await dodoBalance(ctx, account0, "before")
await logGas(await ctx.VDODO.methods.mint(
decimalStr("100"),
2021-02-05 17:04:53 +08:00
account1
2021-02-03 00:05:45 +08:00
), 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");
2021-02-05 17:04:53 +08:00
let superiorInfo = await getUserInfo(ctx, dodoTeam, "Superior after")
2021-02-03 00:05:45 +08:00
let [, dodo_u] = await dodoBalance(ctx, account0, "after")
2021-02-03 13:24:52 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(alpha, "1016710114832014192");
assert.equal(userInfo.stakingPower, "198652706760814869070");
assert.equal(userInfo.superiorSP, "19865270676081486907");
2021-02-03 00:05:45 +08:00
assert.equal(userInfo.credit, "0");
2021-02-05 17:04:53 +08:00
assert.equal(userInfo.superior, dodoTeam);
2021-02-03 00:05:45 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(superiorInfo.stakingPower, "29775180585991396825");
assert.equal(superiorInfo.superiorSP, "0");
assert.equal(superiorInfo.credit, "29999999999999999998");
2021-02-05 17:04:53 +08:00
assert.equal(superiorInfo.superior, "0x0000000000000000000000000000000000000000")
2021-02-03 00:05:45 +08:00
2021-02-05 17:04:53 +08:00
assert.equal(dodo_u, "99800000000000000000000")
2021-02-03 00:05:45 +08:00
2021-02-05 17:04:53 +08:00
let otherInfo = await getUserInfo(ctx, account1, "Superior after")
2021-02-09 18:56:35 +08:00
assert.equal(otherInfo.stakingPower, "99099099099099099188");
assert.equal(otherInfo.superiorSP, "9909909909909909918");
2021-02-03 00:05:45 +08:00
assert.equal(otherInfo.credit, "0");
2021-02-05 17:04:53 +08:00
assert.equal(otherInfo.superior, dodoTeam)
2021-02-03 00:05:45 +08:00
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-05 17:04:53 +08:00
await mint(ctx, account0, decimalStr("100"), dodoTeam)
2021-02-03 00:05:45 +08:00
2021-02-09 18:56:35 +08:00
let [dodoReceive, burnDodoAmount, withdrawFeeDodoAmount] = await ctx.VDODO.methods.getWithdrawResult(decimalStr("1")).call();
2021-02-03 00:05:45 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(dodoReceive, decimalStr("0.85"));
2021-02-03 13:24:52 +08:00
assert.equal(burnDodoAmount, decimalStr("0"));
2021-02-09 18:56:35 +08:00
assert.equal(withdrawFeeDodoAmount, decimalStr("0.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-05 17:04:53 +08:00
await mint(ctx, account0, decimalStr("10000"), dodoTeam)
2021-02-03 13:24:52 +08:00
await getGlobalState(ctx, "before");
await getUserInfo(ctx, account0, "User before");
2021-02-05 17:04:53 +08:00
await getUserInfo(ctx, dodoTeam, "Superior before")
2021-02-03 13:24:52 +08:00
await dodoBalance(ctx, account0, "before")
2021-02-09 18:56:35 +08:00
await logGas(await ctx.VDODO.methods.redeem(decimalStr("10"), false), ctx.sendParam(account0), "redeem-partial-haveMint");
2021-02-03 13:24:52 +08:00
let [alpha,] = await getGlobalState(ctx, "after");
let userInfo = await getUserInfo(ctx, account0, "User after");
2021-02-05 17:04:53 +08:00
let superiorInfo = await getUserInfo(ctx, dodoTeam, "Superior after")
2021-02-03 13:24:52 +08:00
let [, dodo_u] = await dodoBalance(ctx, account0, "after")
2021-02-09 18:56:35 +08:00
assert.equal(alpha, "1015242271212274241");
assert.equal(userInfo.stakingPower, "9000090900827197526589");
assert.equal(userInfo.superiorSP, "900009090082719752659");
2021-02-03 13:24:52 +08:00
assert.equal(userInfo.credit, "0");
2021-02-05 17:04:53 +08:00
assert.equal(userInfo.superior, dodoTeam);
2021-02-03 13:24:52 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(superiorInfo.stakingPower, "900009090082719752659");
assert.equal(superiorInfo.superiorSP, "0");
assert.equal(superiorInfo.credit, "900000000000000000001");
2021-02-03 13:24:52 +08:00
assert.equal(superiorInfo.superior, "0x0000000000000000000000000000000000000000");
2021-02-09 18:56:35 +08:00
assert.equal(dodo_u, "90850000000000000000000")
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-05 17:04:53 +08:00
await mint(ctx, account1, decimalStr("100"), dodoTeam)
await mint(ctx, account2, decimalStr("100"), dodoTeam)
await mint(ctx, account3, decimalStr("100"), dodoTeam)
await mint(ctx, account4, decimalStr("100"), dodoTeam)
2021-02-03 13:24:52 +08:00
await getGlobalState(ctx, "before");
2021-02-05 17:04:53 +08:00
await getUserInfo(ctx, dodoTeam, "User before");
2021-02-03 13:24:52 +08:00
await getUserInfo(ctx, account3, "One of referer before");
2021-02-05 17:04:53 +08:00
await dodoBalance(ctx, dodoTeam, "before")
2021-02-03 13:24:52 +08:00
2021-02-05 17:04:53 +08:00
let dodoTeamVdodoAmount = await ctx.VDODO.methods.balanceOf(dodoTeam).call()
2021-02-03 00:05:45 +08:00
2021-02-09 18:56:35 +08:00
await logGas(await ctx.VDODO.methods.redeem((dodoTeamVdodoAmount - 3000) + "", false), ctx.sendParam(dodoTeam), "redeem-partial-NotMint");
2021-02-03 13:24:52 +08:00
let [alpha,] = await getGlobalState(ctx, "after");
2021-02-05 17:04:53 +08:00
let userInfo = await getUserInfo(ctx, dodoTeam, "User after");
2021-02-03 13:24:52 +08:00
let superiorInfo = await getUserInfo(ctx, account3, "One of referer after")
2021-02-05 17:04:53 +08:00
let [, dodo_u] = await dodoBalance(ctx, dodoTeam, "after")
2021-02-03 13:24:52 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(alpha, "1019099117914144640");
assert.equal(userInfo.stakingPower, "39343185109576338546");
assert.equal(userInfo.superiorSP, "0");
assert.equal(userInfo.credit, "39999999999999999997");
2021-02-03 13:24:52 +08:00
assert.equal(userInfo.superior, "0x0000000000000000000000000000000000000000");
2021-02-09 18:56:35 +08:00
assert.equal(superiorInfo.stakingPower, "98652706760814869070");
assert.equal(superiorInfo.superiorSP, "9865270676081486907");
2021-02-03 13:24:52 +08:00
assert.equal(superiorInfo.credit, "0");
2021-02-05 17:04:53 +08:00
assert.equal(superiorInfo.superior, dodoTeam);
2021-02-03 13:24:52 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(dodo_u, "231818181817926710")
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过小
2021-02-05 17:04:53 +08:00
await mint(ctx, account0, decimalStr("10000"), dodoTeam)
await mint(ctx, account1, decimalStr("100"), dodoTeam)
2021-02-03 13:24:52 +08:00
await getGlobalState(ctx, "before");
await getUserInfo(ctx, account1, "User before");
2021-02-05 17:04:53 +08:00
await getUserInfo(ctx, dodoTeam, "Superior before")
2021-02-03 13:24:52 +08:00
await dodoBalance(ctx, account1, "before")
2021-02-09 18:56:35 +08:00
await logGas(await ctx.VDODO.methods.redeem(0, true), ctx.sendParam(account1), "redeem-all-haveMint");
2021-02-03 13:24:52 +08:00
let [alpha,] = await getGlobalState(ctx, "after");
let userInfo = await getUserInfo(ctx, account1, "User after");
2021-02-05 17:04:53 +08:00
let superiorInfo = await getUserInfo(ctx, dodoTeam, "Superior after")
2021-02-03 13:24:52 +08:00
let [, dodo_u] = await dodoBalance(ctx, account1, "after")
2021-02-09 18:56:35 +08:00
assert.equal(alpha, "1001544677264954465");
assert.equal(userInfo.stakingPower, "0");
assert.equal(userInfo.superiorSP, "0");
2021-02-05 17:04:53 +08:00
assert.equal(userInfo.credit, "0");
assert.equal(userInfo.superior, dodoTeam);
2021-02-03 13:24:52 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(superiorInfo.stakingPower, "1000000000000000000000");
assert.equal(superiorInfo.superiorSP, "0");
assert.equal(superiorInfo.credit, "999999099990999910008");
2021-02-03 13:24:52 +08:00
assert.equal(superiorInfo.superior, "0x0000000000000000000000000000000000000000");
2021-02-09 18:56:35 +08:00
assert.equal(dodo_u, "985007650076500764931")
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-05 17:04:53 +08:00
await mint(ctx, account1, decimalStr("100"), dodoTeam)
await mint(ctx, account2, decimalStr("100"), dodoTeam)
await mint(ctx, account3, decimalStr("100"), dodoTeam)
await mint(ctx, account4, decimalStr("100"), dodoTeam)
2021-02-03 13:24:52 +08:00
await getGlobalState(ctx, "before");
2021-02-05 17:04:53 +08:00
await getUserInfo(ctx, dodoTeam, "User before");
2021-02-03 13:24:52 +08:00
await getUserInfo(ctx, account3, "One of referer before");
2021-02-05 17:04:53 +08:00
await dodoBalance(ctx, dodoTeam, "before")
2021-02-03 13:24:52 +08:00
2021-02-09 18:56:35 +08:00
await logGas(await ctx.VDODO.methods.redeem(0, true), ctx.sendParam(dodoTeam), "redeem-all-NotMint");
2021-02-03 13:24:52 +08:00
let [alpha,] = await getGlobalState(ctx, "after");
2021-02-05 17:04:53 +08:00
let userInfo = await getUserInfo(ctx, dodoTeam, "User after");
2021-02-03 13:24:52 +08:00
let superiorInfo = await getUserInfo(ctx, account3, "One of referer after")
2021-02-05 17:04:53 +08:00
let [, dodo_u] = await dodoBalance(ctx, dodoTeam, "after")
2021-02-03 13:24:52 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(alpha, "1019130459045726342");
assert.equal(userInfo.stakingPower, "39253971537899000903");
assert.equal(userInfo.superiorSP, "0");
assert.equal(userInfo.credit, "39999999999999999997");
2021-02-03 13:24:52 +08:00
assert.equal(userInfo.superior, "0x0000000000000000000000000000000000000000");
2021-02-09 18:56:35 +08:00
assert.equal(superiorInfo.stakingPower, "98652706760814869070");
assert.equal(superiorInfo.superiorSP, "9865270676081486907");
2021-02-03 13:24:52 +08:00
assert.equal(superiorInfo.credit, "0");
2021-02-05 17:04:53 +08:00
assert.equal(superiorInfo.superior, dodoTeam);
2021-02-03 13:24:52 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(dodo_u, "309090909090909029")
2021-02-03 13:24:52 +08:00
});
2021-02-01 17:27:44 +08:00
})
});