Skip to content

Commit

Permalink
Keep upgrade compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqiushi committed Feb 1, 2024
1 parent 322ed88 commit 0a220ed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
17 changes: 9 additions & 8 deletions contracts/public_data_storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
mapping(bytes32 => CycleDataInfo) dataInfos;

SortedScoreList.List scoreList;
uint256 totalReward; // 记录这个cycle的总奖励
uint256 totalAward; // 记录这个cycle的总奖励
}

struct CycleOutputInfo {
Expand All @@ -96,6 +96,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
uint256 _currectCycle;
mapping(uint256 => CycleInfo) _cycleInfos;
uint256 _startBlock;
uint64 _minRankingScore;

struct SysConfig {
uint32 minDepositRatio;
Expand Down Expand Up @@ -222,19 +223,19 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
CycleInfo storage cycleInfo = _cycleInfos[cycleNumber];
// 如果cycle的reward为0,说明这个周期还没有开始
// 开始一个周期:从上个周期的奖励中拿20%
if (cycleInfo.totalReward == 0) {
uint256 lastCycleReward = _cycleInfos[_currectCycle].totalReward;
if (cycleInfo.totalAward == 0) {
uint256 lastCycleReward = _cycleInfos[_currectCycle].totalAward;
// 5%作为基金会收入
uint256 fundationIncome = lastCycleReward * 5 / 100;
gwtToken.transfer(foundationAddress, fundationIncome);
// 如果上一轮的获奖数据不足32个,剩余的奖金也滚动进此轮奖池
uint16 remainScore = _getRemainScore(_cycleInfos[_currectCycle].scoreList.length());
uint256 remainReward = lastCycleReward * 4 * remainScore / totalRewardScore / 5;

cycleInfo.totalReward = lastCycleReward - (lastCycleReward * 4 / 5) - fundationIncome + remainReward;
cycleInfo.totalAward = lastCycleReward - (lastCycleReward * 4 / 5) - fundationIncome + remainReward;
_currectCycle = cycleNumber;

emit CycleStart(cycleNumber, cycleInfo.totalReward);
emit CycleStart(cycleNumber, cycleInfo.totalAward);
}

return cycleInfo;
Expand All @@ -243,7 +244,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable

function _addCycleReward(uint256 amount) private {
CycleInfo storage cycleInfo = _ensureCurrentCycleStart();
cycleInfo.totalReward += amount;
cycleInfo.totalAward += amount;
}

// 计算这些空间对应多少GWT,单位是wei
Expand Down Expand Up @@ -307,7 +308,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
}

function getCycleInfo(uint256 cycleNumber) public view returns(CycleOutputInfo memory) {
return CycleOutputInfo(_cycleInfos[cycleNumber].totalReward, _cycleInfos[cycleNumber].scoreList.getSortedList());
return CycleOutputInfo(_cycleInfos[cycleNumber].totalAward, _cycleInfos[cycleNumber].scoreList.getSortedList());
}

function getPledgeInfo(address supplier) public view returns(SupplierInfo memory) {
Expand Down Expand Up @@ -585,7 +586,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
require(dataInfo.score > 0, "already withdraw");

// 计算该得到多少奖励
uint256 totalReward = cycleInfo.totalReward * 8 / 10;
uint256 totalReward = cycleInfo.totalAward * 8 / 10;

uint8 score = _getRewardScore(scoreListRanking);
// 如果数据总量不足32,那么多余的奖励沉淀在合约账户中
Expand Down
16 changes: 14 additions & 2 deletions test/test_public_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("PublicDataStorage", function () {
await (await gwtToken.enableTransfer([await contract.getAddress()])).wait();

nftBridge = await (await ethers.getContractFactory("OwnedNFTBridge")).deploy();
await (await contract.allowPublicDataContract(await nftBridge.getAddress())).wait();
await (await contract.allowPublicDataContract([await nftBridge.getAddress()])).wait();
}

before(async () => {
Expand Down Expand Up @@ -325,7 +325,7 @@ describe("PublicDataStorage", function () {
// data[0]可分到67,621.76 * 240 / 1600 = 10,143.264
// owner获得10143.264*0.2=2028.6528
//let cycleInfo = await contract.getCycleInfo(1);
let tx = contract.connect(signers[0]).withdrawAward(1, TestDatas[0].hash);
let tx = contract.connect(signers[0]).withdrawReward(1, TestDatas[0].hash);
await expect(tx)
.changeTokenBalance(gwtToken, signers[0], ethers.parseEther("2028.6528"));
// sponser获得10143.264*0.5 = 5071.632
Expand All @@ -338,4 +338,16 @@ describe("PublicDataStorage", function () {
.changeTokenBalance(gwtToken, signers[index], ethers.parseEther("608.59584"));
}
});

it("next cycle reward", async () => {
//进行一次create来激活下个Cycle
await expect(contract.createPublicData(TestDatas[1].hash, 64, ethers.parseEther("768"), await nftBridge.getAddress()))
.emit(contract, "PublicDataCreated").withArgs(TestDatas[1].hash)
.emit(contract, "SponsorChanged").withArgs(TestDatas[1].hash, ethers.ZeroAddress, signers[0].address)
.emit(contract, "DepositData").withArgs(signers[0].address, TestDatas[1].hash, ethers.parseEther("614.4"), ethers.parseEther("153.6"))
.emit(contract, "CycleStart").withArgs(2, ethers.parseEther("70157.576"));

// 上期奖池数量:84527.2
// 本期奖池数量:84527.2 - 67,621.76 - 4,226.36 + 67,621.76 * (1600-240) / 1600 = 12,679.08 + 57,478.496 = 70,157.576
})
});

0 comments on commit 0a220ed

Please sign in to comment.