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

310 lines
13 KiB
TypeScript
Raw Normal View History

2021-02-02 16:16:21 +08:00
/*
Copyright 2021 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
2021-02-03 15:28:49 +08:00
import { decimalStr, fromWei } from '../utils/Converter';
2021-02-02 16:16:21 +08:00
import { logGas } from '../utils/Log';
import { VDODOContext, getVDODOContext } from '../utils/VDODOContext';
import { assert } from 'chai';
import BigNumber from 'bignumber.js';
2021-02-02 17:39:59 +08:00
const truffleAssert = require('truffle-assertions');
2021-02-02 16:16:21 +08:00
let account0: string;
let account1: string;
2021-02-03 15:28:49 +08:00
let account2: string;
let account3: string;
2021-02-09 18:56:35 +08:00
let dodoTeam: string;
2021-02-02 17:39:59 +08:00
let defaultSuperAddress: string;
2021-02-03 15:28:49 +08:00
let owner: string;
2021-02-02 16:16:21 +08:00
async function init(ctx: VDODOContext): Promise<void> {
2021-02-09 18:56:35 +08:00
dodoTeam = ctx.Deployer;
2021-02-02 16:16:21 +08:00
account0 = ctx.SpareAccounts[0];
account1 = ctx.SpareAccounts[1];
2021-02-03 15:28:49 +08:00
account2 = ctx.SpareAccounts[2];
account3 = ctx.SpareAccounts[3];
2021-02-02 17:39:59 +08:00
defaultSuperAddress = ctx.Maintainer
2021-02-03 15:28:49 +08:00
owner = ctx.Deployer
2021-02-02 16:16:21 +08:00
await ctx.mintTestToken(account0, decimalStr("1000"));
2021-02-09 18:56:35 +08:00
await ctx.mintTestToken(account1, decimalStr("1000"));
2021-02-03 15:28:49 +08:00
await ctx.mintTestToken(account2, decimalStr("1000"));
2021-02-09 18:56:35 +08:00
await ctx.mintTestToken(account3, decimalStr("1000"));
2021-02-02 16:16:21 +08:00
await ctx.approveProxy(account0);
await ctx.approveProxy(account1);
2021-02-03 15:28:49 +08:00
await ctx.approveProxy(account2);
await ctx.approveProxy(account3);
await ctx.VDODO.methods.setCantransfer(true).send(ctx.sendParam(owner))
2021-02-02 16:16:21 +08:00
}
2021-02-03 15:28:49 +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 15:28:49 +08:00
var totalSuppy = await ctx.VDODO.methods.totalSupply().call();
// console.log(logInfo + " alpha:" + fromWei(alpha, 'ether') + " lastRewardBlock:" + lastRewardBlock + " totalSuppy:" + fromWei(totalSuppy, 'ether'));
2021-02-09 18:56:35 +08:00
return [alpha, lastRewardBlock, totalSuppy]
}
async function dodoBalance(ctx: VDODOContext, user: string, logInfo?: string) {
2021-02-03 15:28:49 +08:00
var dodo_contract = await ctx.DODO.methods.balanceOf(ctx.VDODO.options.address).call();
var dodo_account = await ctx.DODO.methods.balanceOf(user).call();
2021-02-09 18:56:35 +08:00
2021-02-03 15:28:49 +08:00
// console.log(logInfo + " DODO:" + fromWei(dodo_contract, 'ether') + " account:" + fromWei(dodo_account, 'ether'));
return [dodo_contract, dodo_account]
2021-02-09 18:56:35 +08:00
}
async function getUserInfo(ctx: VDODOContext, user: string, logInfo?: string) {
2021-02-03 15:28:49 +08:00
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,
"superior": info.superior,
"credit": info.credit
2021-02-03 15:28:49 +08:00
}
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 15:28:49 +08:00
return res
2021-02-09 18:56:35 +08:00
}
async function mint(ctx: VDODOContext, user: string, mintAmount: string, superior: string) {
2021-02-03 15:28:49 +08:00
await ctx.VDODO.methods.mint(
2021-02-09 18:56:35 +08:00
mintAmount,
superior
2021-02-03 15:28:49 +08:00
).send(ctx.sendParam(user));
2021-02-09 18:56:35 +08:00
}
2021-02-03 15:28:49 +08:00
2021-02-02 16:16:21 +08:00
describe("vDODO-erc20", () => {
let snapshotId: string;
let ctx: VDODOContext;
before(async () => {
ctx = await getVDODOContext();
//打开transfer开关
await init(ctx);
});
beforeEach(async () => {
snapshotId = await ctx.EVM.snapshot();
});
afterEach(async () => {
await ctx.EVM.reset(snapshotId);
});
describe("vdodo-erc20", () => {
2021-02-09 18:56:35 +08:00
it("totalSupply", async () => {
var lastRewardBlock = await ctx.VDODO.methods._LAST_REWARD_BLOCK_().call();
var curBlock = await ctx.Web3.eth.getBlockNumber();
console.log("init-block:" + lastRewardBlock + " blockNumber:" + curBlock)
var totalSuppy = await ctx.VDODO.methods.totalSupply().call();
assert(totalSuppy, decimalStr("0.09"))
await ctx.VDODO.methods.mint(decimalStr("10"), dodoTeam).send(ctx.sendParam(account0))
var totalSuppy = await ctx.VDODO.methods.totalSupply().call();
assert(totalSuppy, decimalStr("0.2"))
await ctx.VDODO.methods.mint(decimalStr("10"), dodoTeam).send(ctx.sendParam(account0))
var totalSuppy = await ctx.VDODO.methods.totalSupply().call();
assert(totalSuppy, decimalStr("0.31"))
})
2021-02-02 16:16:21 +08:00
it("transfer-vdodo", async () => {
//检查四个人 【包括from, to 以及各自的上级】info变化
//alpha lastRewardBlock
//各自dodo余额变化
2021-02-03 15:28:49 +08:00
2021-02-09 18:56:35 +08:00
let [, lastRewardBlockStart,] = await getGlobalState(ctx, "before");
await ctx.VDODO.methods.mint(decimalStr("10"), dodoTeam).send(ctx.sendParam(account0))
await ctx.VDODO.methods.mint(decimalStr("10"), account0).send(ctx.sendParam(account1))
await ctx.VDODO.methods.mint(decimalStr("10"), account1).send(ctx.sendParam(account2))
await ctx.VDODO.methods.mint(decimalStr("10"), account2).send(ctx.sendParam(account3))
2021-02-03 15:28:49 +08:00
//增加一个区块
await ctx.mintTestToken(account0, decimalStr("0"));
2021-02-09 18:56:35 +08:00
let [alpha, lastRewardBlock,] = await getGlobalState(ctx, "after");
2021-02-03 15:28:49 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(alpha, "1195775916960005765");
2021-02-03 15:28:49 +08:00
var totalSuppy = await ctx.VDODO.methods.totalSupply().call();
2021-02-09 18:56:35 +08:00
assert.equal(totalSuppy, "540000000000000000");
2021-02-03 15:28:49 +08:00
let userInfo0 = await getUserInfo(ctx, account0, "User0 ");
2021-02-09 18:56:35 +08:00
assert.equal(userInfo0.stakingPower, "10916666666666666666");
assert.equal(userInfo0.superiorSP, decimalStr("1"));
assert.equal(userInfo0.credit, "999999999999999999");
let userInfo1 = await getUserInfo(ctx, account1, "User1 ")
assert.equal(userInfo1.stakingPower, "10045138888888888889");
assert.equal(userInfo1.superiorSP, "916666666666666666");
assert.equal(userInfo1.credit, "999999999999999999");
2021-02-03 15:28:49 +08:00
let userInfo2 = await getUserInfo(ctx, account2, "User2 ");
2021-02-09 18:56:35 +08:00
assert.equal(userInfo2.stakingPower, "9638792438271604945");
assert.equal(userInfo2.superiorSP, "878472222222222222");
assert.equal(userInfo2.credit, "999999999999999999");
let userInfo3 = await getUserInfo(ctx, account3, "User3 ");
assert.equal(userInfo3.stakingPower, "8540702160493827171");
assert.equal(userInfo3.superiorSP, "854070216049382717");
assert.equal(userInfo3.credit, decimalStr("0"));
2021-02-03 15:28:49 +08:00
let [, dodo_u0] = await dodoBalance(ctx, account0, "start")
assert.equal(dodo_u0, "990000000000000000000");
let [, dodo_u1] = await dodoBalance(ctx, account1, "start")
2021-02-09 18:56:35 +08:00
assert.equal(dodo_u1, "990000000000000000000");
2021-02-03 15:28:49 +08:00
let [, dodo_u2] = await dodoBalance(ctx, account2, "start")
assert.equal(dodo_u2, "990000000000000000000");
let [, dodo_u3] = await dodoBalance(ctx, account3, "start")
2021-02-09 18:56:35 +08:00
assert.equal(dodo_u3, "990000000000000000000");
2021-02-03 15:28:49 +08:00
2021-02-09 18:56:35 +08:00
let account1Balance = await ctx.VDODO.methods.balanceOf(account1).call()
2021-02-03 15:28:49 +08:00
await logGas(await ctx.VDODO.methods.transfer(
2021-02-09 18:56:35 +08:00
account3,
account1Balance
), ctx.sendParam(account1), "transfer");
2021-02-03 15:28:49 +08:00
let userInfo0_after = await getUserInfo(ctx, account0, "userInfo0_after");
let userInfo1_after = await getUserInfo(ctx, account1, "userInfo1_after");
let userInfo2_after = await getUserInfo(ctx, account2, "userInfo2_after");
let userInfo3_after = await getUserInfo(ctx, account3, "userInfo3_after");
2021-02-09 18:56:35 +08:00
assert.equal(userInfo0_after.stakingPower, "10097456459435626102");
assert.equal(userInfo0_after.superiorSP, decimalStr("1"));
2021-02-03 15:28:49 +08:00
assert.equal(userInfo0_after.credit, "0");
2021-02-09 18:56:35 +08:00
assert.equal(userInfo1_after.stakingPower, "1024213041698160810");
assert.equal(userInfo1_after.superiorSP, "14574081947593859");
assert.equal(userInfo1_after.credit, "999999999999999999");
2021-02-03 15:28:49 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(userInfo2_after.stakingPower, "10540885022990677752");
assert.equal(userInfo2_after.superiorSP, "878472222222222222");
assert.equal(userInfo2_after.credit, "2101173516585172447");
2021-02-03 15:28:49 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(userInfo3_after.stakingPower, "17561628007684555250");
assert.equal(userInfo3_after.superiorSP, "1756162800768455524");
assert.equal(userInfo3_after.credit, "0");
2021-02-03 15:28:49 +08:00
2021-02-09 18:56:35 +08:00
let [alphaEnd, lastRewardBlockEnd, totalSuppyEnd] = await getGlobalState(ctx, "end");
assert.equal(alphaEnd, "1220687915230005885");
assert.equal(totalSuppyEnd, "550000000000000000");
assert.equal(lastRewardBlockEnd, Number(lastRewardBlock) + 2);
2021-02-02 16:16:21 +08:00
});
it("transferFrom-vdodo", async () => {
2021-02-09 18:56:35 +08:00
await ctx.VDODO.methods.mint(decimalStr("10"), dodoTeam).send(ctx.sendParam(account0))
await ctx.VDODO.methods.mint(decimalStr("10"), dodoTeam).send(ctx.sendParam(account1))
2021-02-03 15:28:49 +08:00
//增加一个区块
await ctx.mintTestToken(account0, decimalStr("0"));
2021-02-09 18:56:35 +08:00
let [alpha, lastRewardBlock,] = await getGlobalState(ctx, "after");
2021-02-03 15:28:49 +08:00
2021-02-09 18:56:35 +08:00
assert.equal(alpha, "1138339920948616600");
2021-02-03 15:28:49 +08:00
var totalSuppy = await ctx.VDODO.methods.totalSupply().call();
2021-02-09 18:56:35 +08:00
assert.equal(totalSuppy, "320000000000000000");
2021-02-03 15:28:49 +08:00
let userInfo0 = await getUserInfo(ctx, account0, "User0 ");
2021-02-09 18:56:35 +08:00
assert.equal(userInfo0.stakingPower, decimalStr("10"));
assert.equal(userInfo0.superiorSP, decimalStr("1"));
2021-02-03 15:28:49 +08:00
assert.equal(userInfo0.credit, "0");
2021-02-09 18:56:35 +08:00
let userInfo1 = await getUserInfo(ctx, account1, "User1 ")
assert.equal(userInfo1.stakingPower, "9166666666666666667");
assert.equal(userInfo1.superiorSP, "916666666666666666");
assert.equal(userInfo1.credit, decimalStr("0"));
2021-02-03 15:28:49 +08:00
let [, dodo_u0] = await dodoBalance(ctx, account0, "start")
assert.equal(dodo_u0, "990000000000000000000");
let [, dodo_u1] = await dodoBalance(ctx, account1, "start")
2021-02-09 18:56:35 +08:00
assert.equal(dodo_u1, "990000000000000000000");
2021-02-03 15:28:49 +08:00
2021-02-09 18:56:35 +08:00
let account0Balance = await ctx.VDODO.methods.balanceOf(account0).call()
2021-02-03 15:28:49 +08:00
await logGas(await ctx.VDODO.methods.approve(
2021-02-09 18:56:35 +08:00
account2,
account0Balance
2021-02-03 15:28:49 +08:00
), ctx.sendParam(account0), "approve");
await logGas(await ctx.VDODO.methods.transferFrom(
account0,
2021-02-09 18:56:35 +08:00
account1,
account0Balance
), ctx.sendParam(account2), "transferFrom");
2021-02-03 15:28:49 +08:00
let userInfo0_after = await getUserInfo(ctx, account0, "userInfo0_after");
let userInfo1_after = await getUserInfo(ctx, account1, "userInfo1_after");
let userInfo2_after = await getUserInfo(ctx, account2, "userInfo2_after");
2021-02-09 18:56:35 +08:00
assert.equal(userInfo0_after.stakingPower, "769230769230769236");
assert.equal(userInfo0_after.superiorSP, "76923076923076924");
2021-02-03 15:28:49 +08:00
assert.equal(userInfo0_after.credit, "0");
2021-02-09 18:56:35 +08:00
assert.equal(userInfo1_after.stakingPower, "18397435897435897431");
assert.equal(userInfo1_after.superiorSP, "1839743589743589742");
2021-02-03 15:28:49 +08:00
assert.equal(userInfo1_after.credit, "0");
2021-02-09 18:56:35 +08:00
assert.equal(userInfo2_after.stakingPower, "0");
assert.equal(userInfo2_after.superiorSP, "0");
2021-02-03 15:28:49 +08:00
assert.equal(userInfo2_after.credit, "0");
2021-02-09 18:56:35 +08:00
let [alphaEnd, lastRewardBlockEnd, totalSuppyEnd] = await getGlobalState(ctx, "end");
assert.equal(alphaEnd, "1233201581027667984");
assert.equal(totalSuppyEnd, "340000000000000000");
assert.equal(lastRewardBlockEnd, Number(lastRewardBlock) + 3);
2021-02-02 16:16:21 +08:00
//再次transferFrom 预期revert
2021-02-03 15:28:49 +08:00
//预期revert
await truffleAssert.reverts(
2021-02-09 18:56:35 +08:00
ctx.VDODO.methods.transferFrom(account0, account1, 1).send(ctx.sendParam(account2)),
2021-02-03 15:28:49 +08:00
"ALLOWANCE_NOT_ENOUGH"
)
2021-02-02 16:16:21 +08:00
});
it("transfer - close", async () => {
2021-02-03 15:28:49 +08:00
await ctx.VDODO.methods.setCantransfer(false).send(ctx.sendParam(owner))
2021-02-09 18:56:35 +08:00
await ctx.VDODO.methods.mint(decimalStr("10"), dodoTeam).send(ctx.sendParam(account0))
2021-02-03 15:28:49 +08:00
assert.equal(
2021-02-02 17:39:59 +08:00
await ctx.DODO.methods.balanceOf(account0).call(),
decimalStr("990")
);
assert.equal(
await ctx.DODO.methods.balanceOf(ctx.VDODO.options.address).call(),
2021-02-09 18:56:35 +08:00
decimalStr("10010")
2021-02-02 17:39:59 +08:00
);
2021-02-09 18:56:35 +08:00
2021-02-02 17:39:59 +08:00
assert.equal(
await ctx.VDODO.methods.balanceOf(account0).call(),
decimalStr("0.1")
);
2021-02-09 18:56:35 +08:00
2021-02-02 17:39:59 +08:00
assert.equal(
2021-02-09 18:56:35 +08:00
await ctx.VDODO.methods.balanceOf(dodoTeam).call(),
2021-02-02 17:39:59 +08:00
decimalStr("0")
);
2021-02-09 18:56:35 +08:00
2021-02-02 16:16:21 +08:00
//预期revert
2021-02-02 17:39:59 +08:00
await truffleAssert.reverts(
2021-02-09 18:56:35 +08:00
ctx.VDODO.methods.transfer(account1, 1).send(ctx.sendParam(account0)),
2021-02-02 17:39:59 +08:00
"vDODOToken: not allowed transfer"
)
2021-02-09 18:56:35 +08:00
//revert 触发产生区块造成vdodo增加
2021-02-02 17:39:59 +08:00
assert.equal(
await ctx.VDODO.methods.balanceOf(account0).call(),
2021-02-09 18:56:35 +08:00
"109090909090909090"
2021-02-02 17:39:59 +08:00
);
assert.equal(
await ctx.VDODO.methods.balanceOf(account1).call(),
decimalStr("0")
);
});
2021-02-02 16:16:21 +08:00
})
});