add events from user bid/cancel in crowdpooling

This commit is contained in:
牛涛涛
2021-01-05 23:43:27 +08:00
parent 1bc41f9a61
commit 5a81c6a404
5 changed files with 49 additions and 44 deletions

View File

@@ -19,6 +19,9 @@ import {PMMPricing} from "../../lib/PMMPricing.sol";
contract CPFunding is CPStorage {
using SafeERC20 for IERC20;
// ============ Events ============
event Bid(address to, uint256 amount, uint256 fee);
event Cancel(address to,uint256 amount);
// ============ BID & CALM PHASE ============
@@ -33,6 +36,7 @@ contract CPFunding is CPStorage {
_transferQuoteOut(_MAINTAINER_, mtFee);
_mintShares(to, input.sub(mtFee));
_sync();
emit Bid(to, input, mtFee);
}
function cancel(address assetTo, uint256 amount) external phaseBidOrCalm preventReentrant {
@@ -40,6 +44,7 @@ contract CPFunding is CPStorage {
_burnShares(msg.sender, amount);
_transferQuoteOut(assetTo, amount);
_sync();
emit Cancel(assetTo,amount);
}
function _mintShares(address to, uint256 amount) internal {
@@ -74,8 +79,8 @@ contract CPFunding is CPStorage {
uint256 _poolI;
uint256 avgPrice = _UNUSED_BASE_ == 0
? _I_
: DecimalMath.divCeil(poolQuote, _UNUSED_BASE_);
? _I_
: DecimalMath.divCeil(poolQuote, _UNUSED_BASE_);
uint256 baseDepth = DecimalMath.mulFloor(avgPrice, poolBase);
if (poolQuote == 0) {
@@ -140,7 +145,7 @@ contract CPFunding is CPStorage {
if (poolQuote > _POOL_QUOTE_CAP_) {
poolQuote = _POOL_QUOTE_CAP_;
}
(uint256 soldBase, ) = PMMPricing.sellQuoteToken(_getPMMState(), poolQuote);
(uint256 soldBase,) = PMMPricing.sellQuoteToken(_getPMMState(), poolQuote);
poolBase = _TOTAL_BASE_.sub(soldBase);
}

View File

@@ -33,14 +33,14 @@ describe("Funding", () => {
config = {
totalBase: decimalStr("10000"),
poolQuoteCap: decimalStr("50000"),
k: decimalStr("0.5"),
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("0.1"),
cliffRate: decimalStr("1"),
}
ctx = new CPContext();
await ctx.init(config);
@@ -78,4 +78,4 @@ describe("Funding", () => {
})
})
})
})

View File

@@ -32,14 +32,14 @@ describe("Funding", () => {
config = {
totalBase: decimalStr("10000"),
poolQuoteCap: decimalStr("50000"),
k: decimalStr("0.5"),
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("0.1"),
cliffRate: decimalStr("1"),
}
ctx = new CPContext();
await ctx.init(config);
@@ -69,14 +69,14 @@ describe("Funding", () => {
var poolAddress = await ctx.CP.methods._POOL_().call()
var pool = getContractWithAddress(DVM_NAME, poolAddress)
assert.equal(await pool.methods.getMidPrice().call(), "10050199494025273134")
assert.equal(await ctx.CP.methods._AVG_SETTLED_PRICE_().call(), "10050199494025273136")
assert.equal(await pool.methods.getMidPrice().call(), "9999999999999999987") //todo 验证这个价格
assert.equal(await ctx.CP.methods._AVG_SETTLED_PRICE_().call(), "10000000000000000000")
assert.equal(await ctx.CP.methods._UNUSED_QUOTE_().call(), "0")
assert.equal(await ctx.CP.methods._UNUSED_BASE_().call(), "99401011949453729399")
assert.equal(await ctx.CP.methods._UNUSED_BASE_().call(), "99900000000000000000")
assert.equal(await ctx.BASE.methods.balanceOf(poolAddress).call(), "9900598988050546270601")
assert.equal(await ctx.BASE.methods.balanceOf(ctx.CP.options.address).call(), "99401011949453729399")
assert.equal(await ctx.BASE.methods.balanceOf(poolAddress).call(), "9900100000000000000000")
assert.equal(await ctx.BASE.methods.balanceOf(ctx.CP.options.address).call(), "99900000000000000000")
assert.equal(await ctx.QUOTE.methods.balanceOf(poolAddress).call(), decimalStr("999"))
assert.equal(await ctx.QUOTE.methods.balanceOf(ctx.CP.options.address).call(), "0")
@@ -94,15 +94,15 @@ describe("Funding", () => {
var poolAddress = await ctx.CP.methods._POOL_().call()
var pool = getContractWithAddress(DVM_NAME, poolAddress)
assert.equal(await pool.methods.getMidPrice().call(), "13090169943749474228")
assert.equal(await ctx.CP.methods._AVG_SETTLED_PRICE_().call(), "13090169943749474242")
assert.equal(await pool.methods.getMidPrice().call(), "10000000003162277660")
assert.equal(await ctx.CP.methods._AVG_SETTLED_PRICE_().call(), "10000000000000000000")
assert.equal(await ctx.CP.methods._UNUSED_QUOTE_().call(), decimalStr("49900"))
assert.equal(await ctx.CP.methods._UNUSED_BASE_().call(), "3819660112501051517955")
assert.equal(await ctx.CP.methods._UNUSED_BASE_().call(), "5000000000000000000000")
assert.equal(await ctx.BASE.methods.balanceOf(ctx.Deployer).call(), "0")
assert.equal(await ctx.BASE.methods.balanceOf(poolAddress).call(), "6180339887498948482045")
assert.equal(await ctx.BASE.methods.balanceOf(ctx.CP.options.address).call(), "3819660112501051517955")
assert.equal(await ctx.BASE.methods.balanceOf(poolAddress).call(), "5000000000000000000000")
assert.equal(await ctx.BASE.methods.balanceOf(ctx.CP.options.address).call(), "5000000000000000000000")
assert.equal(await ctx.QUOTE.methods.balanceOf(poolAddress).call(), decimalStr("50000"))
assert.equal(await ctx.QUOTE.methods.balanceOf(ctx.CP.options.address).call(), decimalStr("49900"))
@@ -130,4 +130,4 @@ describe("Funding", () => {
})
})
})
})

View File

@@ -32,15 +32,15 @@ describe("Funding", () => {
before(async () => {
config = {
totalBase: decimalStr("10000"),
poolQuoteCap: decimalStr("80000"),
k: decimalStr("0.5"),
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("0.1"),
cliffRate: decimalStr("1"),
}
ctx = new CPContext();
await ctx.init(config);
@@ -69,18 +69,18 @@ describe("Funding", () => {
var poolAddress = await ctx.CP.methods._POOL_().call()
var pool = getContractWithAddress(DVM_NAME, poolAddress)
assert.equal(await pool.methods.getMidPrice().call(), "64921894064178782")
assert.equal(await ctx.CP.methods._AVG_SETTLED_PRICE_().call(), "15403124237432848687")
assert.equal(await pool.methods.getMidPrice().call(), "10000000003162277660")
assert.equal(await ctx.CP.methods._AVG_SETTLED_PRICE_().call(), "10000000000000000000")
assert.equal(await ctx.CP.methods._UNUSED_QUOTE_().call(), decimalStr("19900"))
assert.equal(await ctx.CP.methods._UNUSED_BASE_().call(), "5193751525134302627024")
assert.equal(await ctx.CP.methods._UNUSED_QUOTE_().call(), decimalStr("49900"))
assert.equal(await ctx.CP.methods._UNUSED_BASE_().call(), "5000000000000000000000")
assert.equal(await ctx.BASE.methods.balanceOf(poolAddress).call(), "4806248474865697372976")
assert.equal(await ctx.BASE.methods.balanceOf(ctx.CP.options.address).call(), "5193751525134302627024")
assert.equal(await ctx.BASE.methods.balanceOf(poolAddress).call(), "5000000000000000000000")
assert.equal(await ctx.BASE.methods.balanceOf(ctx.CP.options.address).call(), "5000000000000000000000")
assert.equal(await ctx.QUOTE.methods.balanceOf(poolAddress).call(), decimalStr("80000"))
assert.equal(await ctx.QUOTE.methods.balanceOf(ctx.CP.options.address).call(), decimalStr("19900"))
assert.equal(await ctx.QUOTE.methods.balanceOf(poolAddress).call(), decimalStr("50000"))
assert.equal(await ctx.QUOTE.methods.balanceOf(ctx.CP.options.address).call(), decimalStr("49900"))
})
})
})
})

View File

@@ -31,14 +31,14 @@ describe("Funding", () => {
config = {
totalBase: decimalStr("10000"),
poolQuoteCap: decimalStr("50000"),
k: decimalStr("0.5"),
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("0.1"),
cliffRate: decimalStr("1"),
}
ctx = new CPContext();
await ctx.init(config);
@@ -66,15 +66,15 @@ describe("Funding", () => {
await logGas(ctx.CP.methods.settle(), ctx.sendParam(ctx.Deployer), "settle")
assert.equal(await ctx.BASE.methods.balanceOf(ctx.CP.options.address).call(), "2557555139280633184959")
assert.equal(await ctx.BASE.methods.balanceOf(ctx.CP.options.address).call(), "2997000000000000000000")
assert.equal(await ctx.QUOTE.methods.balanceOf(ctx.CP.options.address).call(), "0")
await ctx.CP.methods.bidderClaim().send(ctx.sendParam(bidder1))
assert.equal(await ctx.BASE.methods.balanceOf(bidder1).call(), "852518379760211061653")
assert.equal(await ctx.BASE.methods.balanceOf(bidder1).call(), "999000000000000000000")
assert.equal(await ctx.QUOTE.methods.balanceOf(bidder1).call(), "0")
await ctx.CP.methods.bidderClaim().send(ctx.sendParam(bidder2))
assert.equal(await ctx.BASE.methods.balanceOf(bidder2).call(), "1705036759520422123306")
assert.equal(await ctx.BASE.methods.balanceOf(bidder2).call(), "1998000000000000000000")
assert.equal(await ctx.QUOTE.methods.balanceOf(bidder2).call(), "0")
})
@@ -89,15 +89,15 @@ describe("Funding", () => {
await logGas(ctx.CP.methods.settle(), ctx.sendParam(ctx.Deployer), "settle")
assert.equal(await ctx.BASE.methods.balanceOf(ctx.CP.options.address).call(), "3819660112501051517955")
assert.equal(await ctx.BASE.methods.balanceOf(ctx.CP.options.address).call(), "5000000000000000000000")
assert.equal(await ctx.QUOTE.methods.balanceOf(ctx.CP.options.address).call(), decimalStr("39910"))
await ctx.CP.methods.bidderClaim().send(ctx.sendParam(bidder1))
assert.equal(await ctx.BASE.methods.balanceOf(bidder1).call(), "1273220037500350505985")
assert.equal(await ctx.BASE.methods.balanceOf(bidder1).call(), "1666666666666666666666")
assert.equal(await ctx.QUOTE.methods.balanceOf(bidder1).call(), "13303333333333333333333")
await ctx.CP.methods.bidderClaim().send(ctx.sendParam(bidder2))
assert.equal(await ctx.BASE.methods.balanceOf(bidder2).call(), "2546440075000701011970")
assert.equal(await ctx.BASE.methods.balanceOf(bidder2).call(), "3333333333333333333333")
assert.equal(await ctx.QUOTE.methods.balanceOf(bidder2).call(), "26606666666666666666666")
})
@@ -117,15 +117,15 @@ describe("Funding", () => {
var pool = getContractWithAddress(DVM_NAME, poolAddress)
await ctx.CP.methods.claimLPToken().send(ctx.sendParam(ctx.Deployer))
assert.equal(await pool.methods.balanceOf(ctx.Deployer).call(), "618033988749894848205")
assert.equal(await pool.methods.balanceOf(ctx.Deployer).call(), "5000000000000000000000")
// Vesting end
await ctx.EVM.increaseTime(86400)
await ctx.CP.methods.claimLPToken().send(ctx.sendParam(ctx.Deployer))
assert.equal(await pool.methods.balanceOf(ctx.Deployer).call(), "6180339887498948482045")
assert.equal(await pool.methods.balanceOf(ctx.Deployer).call(), "5000000000000000000000")
await pool.methods.sellShares("6180339887498948482045", bidder1, 0, 0, "0x", MAX_UINT256).send(ctx.sendParam(ctx.Deployer))
assert.equal(await ctx.BASE.methods.balanceOf(bidder1).call(), "6180339887498948482045")
await pool.methods.sellShares("5000000000000000000000", bidder1, 0, 0, "0x", MAX_UINT256).send(ctx.sendParam(ctx.Deployer))
assert.equal(await ctx.BASE.methods.balanceOf(bidder1).call(), "5000000000000000000000")
assert.equal(await ctx.QUOTE.methods.balanceOf(bidder1).call(), "50000000000000000000000")
assert.equal(await ctx.BASE.methods.balanceOf(poolAddress).call(), "0")
@@ -133,4 +133,4 @@ describe("Funding", () => {
})
})
})
})