drops testing

This commit is contained in:
owen05
2021-05-24 20:32:05 +08:00
parent c080031b8b
commit d85411e8d9
10 changed files with 315 additions and 22 deletions

View File

@@ -0,0 +1,108 @@
/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
import { decimalStr, fromWei } from '../utils/Converter';
import { logGas } from '../utils/Log';
import { assert } from 'chai';
import * as contracts from '../utils/Contracts';
import { Contract } from 'web3-eth-contract';
import { DropsContext, getDropsContext } from '../utils/DropsContext';
const truffleAssert = require('truffle-assertions');
let maintainer: string;
let user1: string;
let user2: string;
let user3: string;
async function init(ctx: DropsContext): Promise<void> {
maintainer = ctx.SpareAccounts[0];
user1 = ctx.SpareAccounts[1];
user2 = ctx.SpareAccounts[2];
user3 = ctx.SpareAccounts[3];
await ctx.mintTestToken(user1, ctx.DODO, decimalStr("10000"));
await ctx.mintTestToken(user2, ctx.DODO, decimalStr("10000"));
await ctx.mintTestToken(user3, ctx.DODO, decimalStr("10000"));
await ctx.approveProxy(ctx.DODO, user1);
await ctx.approveProxy(ctx.DODO, user2);
await ctx.approveProxy(ctx.DODO, user3);
var addrList = [
ctx.Deployer,
ctx.DODO.options.address,
ctx.DropsFeeModel.options.address,
maintainer,
"0x0000000000000000000000000000000000000000",
ctx.DropsERC721.options.address
]
var curTime = Math.floor(new Date().getTime() / 1000)
await ctx.DropsV2.methods.init(
addrList,
[curTime + 10, curTime + 20, curTime + 30],
[10000000000000, 10000000000000, 10000000000000],
[10, 10, 0],
curTime+10,
true,
false
).send(ctx.sendParam(ctx.Deployer));
}
async function getTicketsInfo(ctx: DropsContext, user: string): Promise<[string, string]> {
var totalTickets = await ctx.DropsV2.methods.totalSupply().call();
var userTickets = await ctx.DropsV2.methods.balanceOf(user).call();
console.log("User Tickets:" + userTickets + " totalTickets:" + totalTickets);
return [totalTickets, userTickets];
}
async function getBuyTokenBalance(ctx: DropsContext, user: string, token: Contract): Promise<[string,string]> {
var userDodo = await token.methods.balanceOf(user).call();
var dropsDodo = await token.methods.balanceOf(ctx.DropsV2.options.address).call();
console.log("User Dodo:" + userDodo + " Drops Dodo:" + dropsDodo);
return [userDodo, dropsDodo];
}
describe("DODODropsV2", () => {
let snapshotId: string;
let ctx: DropsContext;
before(async () => {
ctx = await getDropsContext();
await init(ctx);
});
beforeEach(async () => {
snapshotId = await ctx.EVM.snapshot();
});
afterEach(async () => {
await ctx.EVM.reset(snapshotId);
});
describe("DODODropsV2", () => {
it("buyTicket", async () => {
// await ctx.EVM.increaseTime(10);
// await logGas(await ctx.DropsProxy.methods.buyTickets(ctx.DropsV2.options.address,2), ctx.sendParam(user1), "buyTickets");
// await logGas(await ctx.DropsProxy.methods.buyTickets(ctx.DropsV2.options.address,3), ctx.sendParam(user2), "buyTickets");
// await getTicketsInfo(ctx, user1)
// await getTicketsInfo(ctx, user2)
// await getBuyTokenBalance(ctx, user1, ctx.DODO)
// await getBuyTokenBalance(ctx, user2, ctx.DODO)
});
it("redeemPrize", async () => {
});
//Owner 设置
});
});

View File

@@ -65,7 +65,13 @@ export const NFT_REGISTER = "DODONFTRegistry"
export const NFT_PROXY = "DODONFTProxy"
export const RANDOM_GENERATOR = "RandomGenerator"
export const MYSTERY_BOX_V1 = "MysteryBoxV1"
export const MYSTERY_BOX_V1 = "DODODropsV1"
export const DROPS_V2 = "BaseDrops"
export const DROPS_ERC721 = "DropsERC721"
export const DROPS_ERC1155 = "DropsERC1155"
export const DROPS_FEE_MODEL = "DropsFeeModel"
export const DROPS_PROXY = "DODODropsProxy"
interface ContractJson {
abi: any;

129
test/utils/DropsContext.ts Normal file
View File

@@ -0,0 +1,129 @@
/*
Copyright 2021 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
import BigNumber from 'bignumber.js';
import Web3 from 'web3';
import { Contract } from 'web3-eth-contract';
import * as contracts from './Contracts';
import { decimalStr, mweiStr, MAX_UINT256 } from './Converter';
import { EVM, getDefaultWeb3 } from './EVM';
import * as log from './Log';
BigNumber.config({
EXPONENTIAL_AT: 1000,
DECIMAL_PLACES: 80,
});
export class DropsContext {
EVM: EVM;
Web3: Web3;
DropsV2: Contract;
DropsERC721: Contract;
DropsERC1155: Contract;
DropsFeeModel: Contract;
DropsProxy: Contract;
DODOApprove: Contract;
DODOApproveProxy: Contract;
//token
DODO: Contract;
Deployer: string;
Maintainer: string;
SpareAccounts: string[];
constructor() { }
async init() {
this.EVM = new EVM();
this.Web3 = getDefaultWeb3();
const allAccounts = await this.Web3.eth.getAccounts();
this.Deployer = allAccounts[0];
this.Maintainer = allAccounts[1];
this.SpareAccounts = allAccounts.slice(2, 10);
this.DODO = await contracts.newContract(
contracts.MINTABLE_ERC20_CONTRACT_NAME,
["DODO Token", "DODO", 18]
);
this.DropsFeeModel = await contracts.newContract(
contracts.DROPS_FEE_MODEL
)
await this.DropsFeeModel.methods.initOwner(this.Deployer).send(this.sendParam(this.Deployer));
this.DropsERC721 = await contracts.newContract(
contracts.DROPS_ERC721
)
await this.DropsERC721.methods.init(this.Deployer, "","","").send(this.sendParam(this.Deployer));
this.DropsERC1155 = await contracts.newContract(
contracts.DROPS_ERC1155
)
await this.DropsERC1155.methods.init(this.Deployer, "").send(this.sendParam(this.Deployer));
this.DODOApprove = await contracts.newContract(
contracts.SMART_APPROVE
);
this.DODOApproveProxy = await contracts.newContract(
contracts.SMART_APPROVE_PROXY,
[this.DODOApprove.options.address]
)
this.DropsProxy = await contracts.newContract(contracts.DROPS_PROXY,
[
this.DODOApproveProxy.options.address
]
)
this.DropsV2 = await contracts.newContract(
contracts.DROPS_V2
);
await this.DropsERC721.methods.addMintAccount(this.DropsProxy.options.address).send(this.sendParam(this.Deployer));
await this.DropsERC1155.methods.addMintAccount(this.DropsProxy.options.address).send(this.sendParam(this.Deployer));
await this.DODOApprove.methods.init(this.Deployer, this.DODOApproveProxy.options.address).send(this.sendParam(this.Deployer));
await this.DODOApproveProxy.methods.init(this.Deployer, [this.DropsProxy.options.address]).send(this.sendParam(this.Deployer));
console.log(log.blueText("[Init DODODrops context]"));
}
sendParam(sender, value = "0") {
return {
from: sender,
gas: process.env["COVERAGE"] ? 10000000000 : 7000000,
gasPrice: mweiStr("1000"),
value: decimalStr(value),
};
}
async mintTestToken(to: string, token: Contract, amount: string) {
await token.methods.mint(to, amount).send(this.sendParam(this.Deployer));
}
async approveProxy(token: Contract, account: string) {
await token.methods
.approve(this.DODOApprove.options.address, MAX_UINT256)
.send(this.sendParam(account));
}
}
export async function getDropsContext(): Promise<DropsContext> {
var context = new DropsContext();
await context.init();
return context;
}