fix mysteryBoxV1

This commit is contained in:
owen05
2021-04-16 13:36:09 +08:00
parent 930698f676
commit 81765596c0
4 changed files with 129 additions and 25 deletions

View File

@@ -4,12 +4,13 @@
SPDX-License-Identifier: Apache-2.0
*/
import { decimalStr, mweiStr, fromWei } from '../utils/Converter';
import { decimalStr, fromWei } from '../utils/Converter';
import { logGas } from '../utils/Log';
import { DVMContext, getDVMContext } from '../utils/DVMContext';
import { assert } from 'chai';
import * as contracts from '../utils/Contracts';
import { Contract } from 'web3-eth-contract';
const truffleAssert = require('truffle-assertions');
let owner: string;
let user1: string;
@@ -63,10 +64,11 @@ async function init(ctx: DVMContext): Promise<void> {
).send(ctx.sendParam(owner));
}
async function getTokenIdAndUrlByUser(user: string) {
async function getTokenIdAndUrlByUser(user: string, logInfo?: string) {
var tokenIds = []
var urls = []
var balance = await MysteryBoxV1.methods.balanceOf(user).call();
console.log(logInfo);
for (var i = 0; i < balance; i++) {
var curTokenId = await MysteryBoxV1.methods.tokenOfOwnerByIndex(user, i).call()
tokenIds.push(curTokenId);
@@ -77,6 +79,28 @@ async function getTokenIdAndUrlByUser(user: string) {
return [tokenIds, urls];
}
async function getTicketsInfo(user: string): Promise<[string, string]> {
var totalTickets = await MysteryBoxV1.methods._TOTAL_TICKETS_().call();
var userTickets = await MysteryBoxV1.methods.getTickets(user).call();
console.log("User Tickets:" + userTickets + " totalTickets:" + totalTickets);
return [totalTickets, userTickets];
}
async function getGlobalState(): Promise<[string, string]> {
var curSelling = await MysteryBoxV1.methods._CUR_SELLING_TICKETS_().call();
var curPrice = await MysteryBoxV1.methods._CUR_PRCIE_().call();
console.log("CurSellingTickets:" + curSelling + " CurPrice:" + fromWei(curPrice, 'ether'));
return [curSelling, curPrice];
}
async function batchMint(ctx: DVMContext) {
var ids = []
for (var i = 0; i < urls.length; i++) {
ids.push(i);
}
await logGas(await MysteryBoxV1.methods.batchMint(ids, urls), ctx.sendParam(owner), "batchMint-10");
}
describe("DODOMysteryBox", () => {
let snapshotId: string;
@@ -102,30 +126,91 @@ describe("DODOMysteryBox", () => {
});
describe("DODO MysteryBoxV1", () => {
it.only("batchMint", async () => {
await logGas(await MysteryBoxV1.methods.batchMint(urls), ctx.sendParam(owner), "batchMint-10");
it("batchMint", async () => {
var ids = []
for (var i = 0; i < urls.length; i++) {
ids.push(i);
}
await logGas(await MysteryBoxV1.methods.batchMint(ids, urls), ctx.sendParam(owner), "batchMint-10");
let [tokenIds,]: any = await getTokenIdAndUrlByUser(MysteryBoxV1.options.address);
assert(10, tokenIds.length);
});
it("buyTicket", async () => {
await MysteryBoxV1.methods.updateSellingInfo(100, decimalStr("0.5")).send(ctx.sendParam(owner));
await MysteryBoxV1.methods.updateSellingInfo(100, decimalStr("0.01")).send(ctx.sendParam(owner));
await truffleAssert.reverts(
MysteryBoxV1.methods.buyTicket().send(ctx.sendParam(user1, "0.001")),
"BNB_NOT_ENOUGH"
)
await logGas(await MysteryBoxV1.methods.buyTicket(), ctx.sendParam(user1, "0.5"), "buyTickets");
await logGas(await MysteryBoxV1.methods.buyTicket(), ctx.sendParam(user2, "0.4"), "buyTickets");
await truffleAssert.reverts(
MysteryBoxV1.methods.buyTicket().send(ctx.sendParam(user1, "0.2")),
"TICKETS_NOT_ENOUGH"
)
let [, userTickets] = await getTicketsInfo(user1)
assert(userTickets, "50")
let [curSelling0] = await getGlobalState()
assert(curSelling0, "10")
//两阶段卖币
await MysteryBoxV1.methods.updateSellingInfo(200, decimalStr("0.02")).send(ctx.sendParam(owner));
await logGas(await MysteryBoxV1.methods.buyTicket(), ctx.sendParam(user1, "1"), "buyTickets");
[, userTickets] = await getTicketsInfo(user1)
assert(userTickets, "100")
let [curSelling1] = await getGlobalState();
assert(curSelling1, "150")
//withdraw
var b_ETH = await ctx.Web3.eth.getBalance(owner);
var tx = await MysteryBoxV1.methods.withdraw().send(ctx.sendParam(owner))
var a_ETH = await ctx.Web3.eth.getBalance(owner);
console.log("b_ETH:" + fromWei(b_ETH, 'ether') + " a_ETH:" + fromWei(a_ETH, 'ether'));
assert.equal(
tx.events['Withdraw'].returnValues['amount'],
decimalStr("1.9")
);
});
it("redeemPrize", async () => {
//redeem
await batchMint(ctx);
//列表查询
await MysteryBoxV1.methods.updateSellingInfo(100, decimalStr("0.1")).send(ctx.sendParam(owner));
await logGas(await MysteryBoxV1.methods.buyTicket(), ctx.sendParam(user1, "0.5"), "buyTickets");
await getTicketsInfo(user1)
await getGlobalState()
await getTokenIdAndUrlByUser(user1, "redeemPrize前")
await getTokenIdAndUrlByUser(MysteryBoxV1.options.address, "盲盒合约redeemPrize前")
await logGas(await MysteryBoxV1.methods.redeemPrize(3), ctx.sendParam(user1), "redeemPrice");
await getTicketsInfo(user1)
await getGlobalState()
await getTokenIdAndUrlByUser(user1, "第一次redeemPrize后")
await logGas(await MysteryBoxV1.methods.redeemPrize(2), ctx.sendParam(user1), "redeemPrice");
let [, userTickets] = await getTicketsInfo(user1)
assert(userTickets, "0");
await getGlobalState()
await getTokenIdAndUrlByUser(user1, "第二次redeemPrize后")
let [tokenIds,] = await getTokenIdAndUrlByUser(MysteryBoxV1.options.address, "盲盒合约redeemPrize后")
assert(tokenIds, "5");
});
it("transferNFT", async () => {
await batchMint(ctx);
await MysteryBoxV1.methods.updateSellingInfo(100, decimalStr("0.1")).send(ctx.sendParam(owner));
await logGas(await MysteryBoxV1.methods.buyTicket(), ctx.sendParam(user1, "0.5"), "buyTickets");
await logGas(await MysteryBoxV1.methods.redeemPrize(1), ctx.sendParam(user1), "redeemPrice");
let [tokenId0,] = await getTokenIdAndUrlByUser(user1, "user1 转前")
assert(tokenId0.length == 1)
await getTokenIdAndUrlByUser(user2, "user2 转前")
await logGas(await MysteryBoxV1.methods.safeTransferFrom(user1, user2, tokenId0[0]), ctx.sendParam(user1), "transferNFT");
await getTokenIdAndUrlByUser(user1, "user1 转后")
let [tokenId1,] = await getTokenIdAndUrlByUser(user2, "user2 转后")
assert(tokenId1.length == 1)
});
});
});