Merge branch 'feature/V2' of https://github.com/DODOEX/contractV2 into feature/V2

This commit is contained in:
杨新刚
2021-01-08 17:01:42 +08:00
6 changed files with 125 additions and 10 deletions

View File

@@ -16,6 +16,7 @@ import {IDVM} from "../../DODOVendingMachine/intf/IDVM.sol";
import {IDVMFactory} from "../../Factory/DVMFactory.sol"; import {IDVMFactory} from "../../Factory/DVMFactory.sol";
import {CPStorage} from "./CPStorage.sol"; import {CPStorage} from "./CPStorage.sol";
import {PMMPricing} from "../../lib/PMMPricing.sol"; import {PMMPricing} from "../../lib/PMMPricing.sol";
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
contract CPFunding is CPStorage { contract CPFunding is CPStorage {
using SafeERC20 for IERC20; using SafeERC20 for IERC20;
@@ -39,12 +40,17 @@ contract CPFunding is CPStorage {
emit Bid(to, input, mtFee); emit Bid(to, input, mtFee);
} }
function cancel(address assetTo, uint256 amount) external phaseBidOrCalm preventReentrant { function cancel(address to, uint256 amount, bytes calldata data) external phaseBidOrCalm preventReentrant {
require(_SHARES_[msg.sender] >= amount, "SHARES_NOT_ENOUGH"); require(_SHARES_[msg.sender] >= amount, "SHARES_NOT_ENOUGH");
_burnShares(msg.sender, amount); _burnShares(msg.sender, amount);
_transferQuoteOut(assetTo, amount); _transferQuoteOut(to, amount);
_sync(); _sync();
emit Cancel(assetTo,amount);
if(data.length > 0){
IDODOCallee(to).CPCancelCall(msg.sender,amount,data);
}
emit Cancel(msg.sender,amount);
} }
function _mintShares(address to, uint256 amount) internal { function _mintShares(address to, uint256 amount) internal {

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2020 DODO ZOO. Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0 SPDX-License-Identifier: Apache-2.0
@@ -43,6 +43,15 @@ contract DODOCalleeHelper is ReentrancyGuard {
_withdraw(assetTo, _quoteToken, quoteAmount, _quoteToken == _WETH_); _withdraw(assetTo, _quoteToken, quoteAmount, _quoteToken == _WETH_);
} }
function CPCancelCall(
address payable assetTo,
uint256 amount,
bytes calldata
)external preventReentrant{
address _quoteToken = IDODOV2(msg.sender)._QUOTE_TOKEN_();
_withdraw(assetTo, _quoteToken, amount, _quoteToken == _WETH_);
}
function _withdraw( function _withdraw(
address payable to, address payable to,
address token, address token,

View File

@@ -31,7 +31,7 @@ interface IDODOCallee {
bytes calldata data bytes calldata data
) external; ) external;
function CACancelCall( function CPCancelCall(
address sender, address sender,
uint256 amount, uint256 amount,
bytes calldata data bytes calldata data

View File

@@ -41,6 +41,7 @@ describe("Funding", () => {
freezeDuration: new BigNumber(86400), freezeDuration: new BigNumber(86400),
vestingDuration: new BigNumber(86400), vestingDuration: new BigNumber(86400),
cliffRate: decimalStr("1"), cliffRate: decimalStr("1"),
quoteTokenContract:""
} }
ctx = new CPContext(); ctx = new CPContext();
await ctx.init(config); await ctx.init(config);
@@ -57,7 +58,7 @@ describe("Funding", () => {
describe("bid & cancel", () => { describe("bid & cancel", () => {
it("bid", async () => { it("bid and cancel", async () => {
await ctx.QUOTE.methods.transfer(ctx.CP.options.address, decimalStr("100")).send(ctx.sendParam(bidder1)) await ctx.QUOTE.methods.transfer(ctx.CP.options.address, decimalStr("100")).send(ctx.sendParam(bidder1))
await logGas(ctx.CP.methods.bid(bidder1), ctx.sendParam(bidder1), "bid") await logGas(ctx.CP.methods.bid(bidder1), ctx.sendParam(bidder1), "bid")
assert.equal(await ctx.CP.methods.getShares(bidder1).call(), decimalStr("99.9")) assert.equal(await ctx.CP.methods.getShares(bidder1).call(), decimalStr("99.9"))
@@ -75,6 +76,7 @@ describe("Funding", () => {
assert.equal(await ctx.CP.methods.getShares(bidder1).call(), decimalStr("79.9")) assert.equal(await ctx.CP.methods.getShares(bidder1).call(), decimalStr("79.9"))
assert.equal(await ctx.CP.methods._TOTAL_SHARES_().call(), decimalStr("129.85")) assert.equal(await ctx.CP.methods._TOTAL_SHARES_().call(), decimalStr("129.85"))
assert.equal(await ctx.QUOTE.methods.balanceOf(bidder1).call(), decimalStr("920")) assert.equal(await ctx.QUOTE.methods.balanceOf(bidder1).call(), decimalStr("920"))
}) })
}) })

View File

@@ -0,0 +1,88 @@
/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
// import * as assert from 'assert';
import { decimalStr, mweiStr } from "../utils/Converter";
import { logGas } from '../utils/Log';
import { CPContext, CPContextInitConfig } from '../utils/CrowdPoolingContext';
import BigNumber from 'bignumber.js';
import { assert } from 'chai';
const truffleAssert = require('truffle-assertions');
let bidder1: string;
let bidder2: string;
let config: CPContextInitConfig
async function init(ctx: CPContext): Promise<void> {
bidder1 = ctx.SpareAccounts[1]
bidder2 = ctx.SpareAccounts[2]
await ctx.QUOTE.methods.deposit().send(ctx.sendParam(bidder1,"0.2"))
await ctx.QUOTE.methods.deposit().send(ctx.sendParam(bidder2,"0.3"))
}
describe("Funding", () => {
let snapshotId: string;
let ctx: CPContext;
before(async () => {
config = {
totalBase: decimalStr("10000"),
poolQuoteCap: decimalStr("50000"),
k: decimalStr("0"),
i: decimalStr("10"),
lpFeeRate: decimalStr("0.002"),
bidDuration: new BigNumber(86400),
calmDuration: new BigNumber(86400),
freezeDuration: new BigNumber(86400),
vestingDuration: new BigNumber(86400),
cliffRate: decimalStr("1"),
quoteTokenContract:"WETH9"
}
ctx = new CPContext();
await ctx.init(config);
await init(ctx);
});
beforeEach(async () => {
snapshotId = await ctx.EVM.snapshot();
});
afterEach(async () => {
await ctx.EVM.reset(snapshotId);
});
describe("eth bid & cancel", () => {
it("bid and cancel", async () => {
await ctx.QUOTE.methods.transfer(ctx.CP.options.address, decimalStr("0.1")).send(ctx.sendParam(bidder1))
await logGas(ctx.CP.methods.bid(bidder1), ctx.sendParam(bidder1), "bid")
assert.equal(await ctx.CP.methods.getShares(bidder1).call(), decimalStr("0.0999"))
assert.equal(await ctx.CP.methods._TOTAL_SHARES_().call(), decimalStr("0.0999"))
assert.equal(await ctx.QUOTE.methods.balanceOf(ctx.Maintainer).call(), decimalStr("0.0001"))
assert.equal(await ctx.QUOTE.methods.balanceOf(bidder1).call(), decimalStr("0.1"))
await ctx.EVM.increaseTime(86400)
await logGas(ctx.CP.methods.cancel(bidder1, decimalStr("0.05"),"0x"), ctx.sendParam(bidder1), "cancel and get 0.05 weth")
assert.equal(await ctx.CP.methods.getShares(bidder1).call(), decimalStr("0.0499"))
assert.equal(await ctx.CP.methods._TOTAL_SHARES_().call(), decimalStr("0.0499"))
assert.equal(await ctx.QUOTE.methods.balanceOf(bidder1).call(), decimalStr("0.15"))
let beforeEthBalance = await ctx.Web3.eth.getBalance(bidder1);
let receipt = await logGas(ctx.CP.methods.cancel(ctx.DODOCallee.options.address, decimalStr("0.02"),"0x00"), ctx.sendParam(bidder1), "cancel and get 0.02 eth")
assert.equal(await ctx.CP.methods.getShares(bidder1).call(), decimalStr("0.0299"))
assert.equal(await ctx.CP.methods._TOTAL_SHARES_().call(), decimalStr("0.0299"))
assert.equal(await ctx.QUOTE.methods.balanceOf(bidder1).call(), decimalStr("0.15"))
let afterEthBalance = await ctx.Web3.eth.getBalance(bidder1);
assert.equal(Number.parseInt(receipt["events"]["1"]["raw"]["data"],16),Number(decimalStr("0.02")));
// assert.equal(Number(afterEthBalance) - Number(beforeEthBalance) + Number(receipt.gasUsed)*Number(mweiStr("1000")),Number(decimalStr("0.02")));
})
})
})

View File

@@ -30,6 +30,7 @@ export interface CPContextInitConfig {
freezeDuration: BigNumber; freezeDuration: BigNumber;
vestingDuration: BigNumber; vestingDuration: BigNumber;
cliffRate: string; cliffRate: string;
quoteTokenContract: string;
} }
@@ -43,6 +44,7 @@ export class CPContext {
Deployer: string; Deployer: string;
Maintainer: string; Maintainer: string;
SpareAccounts: string[]; SpareAccounts: string[];
DODOCallee: Contract;
constructor() { } constructor() { }
@@ -67,10 +69,18 @@ export class CPContext {
contracts.MINTABLE_ERC20_CONTRACT_NAME, contracts.MINTABLE_ERC20_CONTRACT_NAME,
["TestBase", "BASE", 18] ["TestBase", "BASE", 18]
); );
this.QUOTE = await contracts.newContract( if(config.quoteTokenContract){
contracts.MINTABLE_ERC20_CONTRACT_NAME, this.QUOTE = await contracts.newContract(
["TestQuote", "QUOTE", 18] config.quoteTokenContract,
); ["TestQuote", "QUOTE", 18]
);
}else{
this.QUOTE = await contracts.newContract(
contracts.MINTABLE_ERC20_CONTRACT_NAME,
["TestQuote", "QUOTE", 18]
);
}
this.DODOCallee = await contracts.newContract(contracts.DODO_CALLEE_HELPER_NAME,[this.QUOTE.options.address]);
this.DVMFactory = await contracts.newContract(contracts.DVM_FACTORY_NAME, this.DVMFactory = await contracts.newContract(contracts.DVM_FACTORY_NAME,
[ [