diff --git a/contracts/SmartRoute/helper/DODOCalleeHelper.sol b/contracts/SmartRoute/helper/DODOCalleeHelper.sol index a77f606..5ea225f 100644 --- a/contracts/SmartRoute/helper/DODOCalleeHelper.sol +++ b/contracts/SmartRoute/helper/DODOCalleeHelper.sol @@ -76,7 +76,9 @@ contract DODOCalleeHelper is ReentrancyGuard { to.transfer(amount); } } else { - SafeERC20.safeTransfer(IERC20(token), to, amount); + if (amount > 0) { + SafeERC20.safeTransfer(IERC20(token), to, amount); + } } } } diff --git a/test/vDODO/mintRedeem.test.ts b/test/vDODO/mintRedeem.test.ts index b2fdc18..1da9efc 100644 --- a/test/vDODO/mintRedeem.test.ts +++ b/test/vDODO/mintRedeem.test.ts @@ -13,17 +13,27 @@ import { assert } from 'chai'; let account0: string; let account1: string; let account2: string; +let account3: string; +let account4: string; async function init(ctx: VDODOContext): Promise { account0 = ctx.SpareAccounts[0]; account1 = ctx.SpareAccounts[1]; account2 = ctx.SpareAccounts[2]; + account3 = ctx.SpareAccounts[3]; + account4 = ctx.SpareAccounts[4]; - await ctx.mintTestToken(account0, decimalStr("1000")); + await ctx.mintTestToken(account0, decimalStr("100000")); await ctx.mintTestToken(account1, decimalStr("1000")); + await ctx.mintTestToken(account2, decimalStr("1000")); + await ctx.mintTestToken(account3, decimalStr("1000")); + await ctx.mintTestToken(account4, decimalStr("1000")); await ctx.approveProxy(account0); await ctx.approveProxy(account1); + await ctx.approveProxy(account2); + await ctx.approveProxy(account3); + await ctx.approveProxy(account4); } async function getGlobalState(ctx: VDODOContext, logInfo?: string) { @@ -96,10 +106,11 @@ describe("VDODO", () => { await ctx.mintTestToken(account0, decimalStr("0")); let [alpha,] = await getGlobalState(ctx, "after"); - assert.equal(alpha, "101818181818181818181"); 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, "101818181818181818181"); assert.equal(userInfo.VDODOAmount, "1000000000000000000"); assert.equal(userInfo.superiorVDODO, "100000000000000000"); assert.equal(userInfo.credit, "0"); @@ -110,7 +121,7 @@ describe("VDODO", () => { assert.equal(superiorInfo.credit, "10000000000000000000"); assert.equal(superiorInfo.superior, "0x0000000000000000000000000000000000000000"); - assert.equal(dodo_u, "900000000000000000000") + assert.equal(dodo_u, "99900000000000000000000") }); it("vdodo-mint-second", async () => { @@ -130,10 +141,11 @@ describe("VDODO", () => { await ctx.mintTestToken(account0, decimalStr("0")); let [alpha,] = await getGlobalState(ctx, "after"); - assert.equal(alpha, "101365693130399012751"); 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, "101365693130399012751"); assert.equal(userInfo.VDODOAmount, "1990990990990990990"); assert.equal(userInfo.superiorVDODO, "199099099099099099"); assert.equal(userInfo.credit, "0"); @@ -144,7 +156,7 @@ describe("VDODO", () => { assert.equal(superiorInfo.credit, "19999999999999999990"); assert.equal(superiorInfo.superior, "0x0000000000000000000000000000000000000000"); - assert.equal(dodo_u, "800000000000000000000") + assert.equal(dodo_u, "99800000000000000000000") }); @@ -165,10 +177,11 @@ describe("VDODO", () => { await ctx.mintTestToken(account0, decimalStr("0")); let [alpha,] = await getGlobalState(ctx, "after"); - assert.equal(alpha, "101365693130399012751"); 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, "101365693130399012751"); assert.equal(userInfo.VDODOAmount, "1990990990990990990"); assert.equal(userInfo.superiorVDODO, "199099099099099099"); assert.equal(userInfo.credit, "0"); @@ -186,37 +199,156 @@ describe("VDODO", () => { assert.equal(otherInfo.credit, "0"); assert.equal(otherInfo.superior, "0x0000000000000000000000000000000000000000"); - assert.equal(dodo_u, "800000000000000000000") + assert.equal(dodo_u, "99800000000000000000000") }); - it.only("redeem-amount-read", async () => { + it("redeem-amount-read", async () => { await mint(ctx, account0, decimalStr("100"), account1) let [dodoReceive, burnDodoAmount, withdrawFeeDodoAmount] = await ctx.VDODO.methods.getWithdrawAmount(decimalStr("1")).call(); - console.log("dodoReceive:", dodoReceive) - console.log("burnDodoAmount:", burnDodoAmount) - console.log("withdrawFeeDodoAmount:", withdrawFeeDodoAmount) - + assert.equal(dodoReceive, decimalStr("85")); + assert.equal(burnDodoAmount, decimalStr("0")); + assert.equal(withdrawFeeDodoAmount, decimalStr("15")); }); + it("redeem-partial-haveMint", async () => { + 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") }); + it("redeem-partial-NotMint", async () => { //多个下级引用 + 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() + + 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") }); + it("redeem-all-haveMint", async () => { + //第一笔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") }); + it("redeem-all-NoMint", async () => { //多个下级引用 - }); + 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() + + 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") + }); }) });