-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCollectiveDefenseFund.sol
268 lines (212 loc) · 10.5 KB
/
CollectiveDefenseFund.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import {CurioTreaty} from "contracts/standards/CurioTreaty.sol";
import {CurioERC20} from "contracts/standards/CurioERC20.sol";
import {GetterFacet} from "contracts/facets/GetterFacet.sol";
import {AdminFacet} from "contracts/facets/AdminFacet.sol";
import {Position} from "contracts/libraries/Types.sol";
import {Set} from "contracts/Set.sol";
contract CollectiveDefenseFund is CurioTreaty {
// Game cache
CurioERC20 public goldToken;
CurioERC20 public foodToken;
// Treaty-specific data
uint256 public goldFee;
uint256 public foodFee;
uint256 public withdrawTimeInterval;
uint256 public depositTimeInterval;
uint256 public goldWithdrawQuota;
uint256 public foodWithdrawQuota;
mapping(uint256 => uint256) public lastPaid; // nationID => timestamp
mapping(uint256 => uint256) public lastWithdrawn; // nationID => timestamp
Set public council;
modifier onlyCouncilOrPact() {
GetterFacet getter = GetterFacet(diamond);
uint256 callerID = getter.getEntityByAddress(msg.sender);
require(council.includes(callerID) || msg.sender == address(this), "CDFund: Only council or pact can call");
_;
}
function init(address _diamond) public override {
super.init(_diamond);
goldFee = 10000;
foodFee = 10000;
withdrawTimeInterval = 86400;
depositTimeInterval = 86400;
goldWithdrawQuota = 5000;
foodWithdrawQuota = 5000;
// Initialize treaty
GetterFacet getter = GetterFacet(diamond);
goldToken = getter.getTokenContract("Gold");
foodToken = getter.getTokenContract("Food");
// Create new council
council = new Set();
}
function name() external pure override returns (string memory) {
return "Collective Defense Fund";
}
function description() external pure override returns (string memory) {
return "Owner of the League can point to which nation the league is sanctioning";
}
// ----------------------------------------------------------
// Set getters
// ----------------------------------------------------------
function getCouncilMembers() public view returns (uint256[] memory) {
return council.getAll();
}
// ----------------------------------------------------------
// Owner and council functions
// ----------------------------------------------------------
function addToWhitelist(uint256 _nationID) public onlyOwner {
AdminFacet admin = AdminFacet(diamond);
admin.addToTreatyWhitelist(_nationID);
}
function removeFromWhitelist(uint256 _nationID) public onlyOwner {
AdminFacet admin = AdminFacet(diamond);
admin.removeFromTreatyWhitelist(_nationID);
}
function addToCouncil(uint256 _nationID) public onlyOwner {
GetterFacet getter = GetterFacet(diamond);
require(getter.getNationTreatySignature(_nationID, getter.getEntityByAddress(address(this))) != 0, "CDFund: Only signed nations can join council");
council.add(_nationID);
}
function removeFromCouncil(uint256 _nationID) public onlyOwner {
council.remove(_nationID);
}
function updateFoodFee(uint256 _newFee) external onlyCouncilOrPact {
foodFee = _newFee;
}
function updateGoldFee(uint256 _newFee) external onlyCouncilOrPact {
goldFee = _newFee;
}
function removeMember(uint256 _nationID) public onlyCouncilOrPact {
GetterFacet getter = GetterFacet(diamond);
// Only owner can remove council members
uint256 treatyID = getter.getEntityByAddress(address(this));
uint256 ownerID = abi.decode(getter.getComponent("Owner").getBytesValue(treatyID), (uint256));
if (getter.getEntityByAddress(msg.sender) == ownerID) {
council.remove(_nationID);
} else {
require(!council.includes(_nationID), "CDFund: Need owner to `removeFromCouncil` first");
}
AdminFacet admin = AdminFacet(diamond);
admin.removeFromTreatyWhitelist(_nationID); // need to be whitelisted again for joining
admin.removeSigner(_nationID);
}
function withdraw(uint256 _goldAmount, uint256 _foodAmount) external onlyCouncilOrPact {
GetterFacet getter = GetterFacet(diamond);
// Check that withdrawal amount is within quota
require(_goldAmount <= goldWithdrawQuota, "CDFund: Amount exceeds quota");
require(_foodAmount <= foodWithdrawQuota, "CDFund: Amount exceeds quota");
// Check balance sufficience
require(goldToken.balanceOf(address(this)) >= _goldAmount, "CDFund: Insufficient balance");
require(foodToken.balanceOf(address(this)) >= _foodAmount, "CDFund: Insufficient balance");
// Check and update last withdrawn time
uint256 nationID = getter.getEntityByAddress(msg.sender);
require(block.timestamp > lastWithdrawn[nationID] + withdrawTimeInterval, "CDFund: Last withdrawal was too soon");
lastWithdrawn[nationID] = block.timestamp;
// Withdraw
address recipientAddress = getter.getAddress(getter.getCapital(getter.getEntityByAddress(msg.sender)));
goldToken.transfer(recipientAddress, _goldAmount);
foodToken.transfer(recipientAddress, _foodAmount);
}
function removeAllOverdueMembers() external onlyCouncilOrPact {
GetterFacet getter = GetterFacet(diamond);
uint256 treatyID = getter.getEntityByAddress(address(this));
uint256[] memory signers = getter.getTreatySigners(treatyID);
for (uint256 i = 0; i < signers.length; i++) {
if (block.timestamp > lastPaid[signers[i]] + depositTimeInterval) {
removeMember(signers[i]);
}
}
}
// Council member can distribute certain amount of fund to any in-game entity which can hold tokens
function distributeFund(
uint256 _toID,
string memory _resourceType,
uint256 _amount
) external onlyCouncilOrPact {
GetterFacet getter = GetterFacet(diamond);
// Check that withdrawal amount is within quota
if (_strEq(_resourceType, "Gold")) {
require(_amount <= goldWithdrawQuota, "CDFund: Amount exceeds quota");
} else if (_strEq(_resourceType, "Food")) {
require(_amount <= foodWithdrawQuota, "CDFund: Amount exceeds quota");
} else {
revert("CDFund: Invalid resource type");
}
// Check address is not null
address to = getter.getAddress(_toID);
require(to != address(0), "CDFund: Invalid address");
// Transfer
CurioERC20 token = getter.getTokenContract(_resourceType);
token.transfer(to, _amount);
}
// ----------------------------------------------------------
// Player functions
// ----------------------------------------------------------
function treatyJoin() public override onlyWhitelist {
GetterFacet getter = GetterFacet(diamond);
// Check whether the nation is whitelisted
uint256 nationID = getter.getEntityByAddress(msg.sender);
uint256 treatyID = getter.getEntityByAddress(address(this));
require(getter.isWhitelistedByTreaty(nationID, treatyID), "CDFund: Candidate is not whitelisted");
// Pay membership fees
_payFeesHelper(nationID);
// Add nation's signature
super.treatyJoin();
}
function treatyLeave() public override {
GetterFacet getter = GetterFacet(diamond);
// Check if nation has stayed in pact for at least 10 seconds
uint256 nationID = getter.getEntityByAddress(msg.sender);
uint256 treatyID = getter.getEntityByAddress(address(this));
uint256 nationJoinTime = abi.decode(getter.getComponent("InitTimestamp").getBytesValue(getter.getNationTreatySignature(nationID, treatyID)), (uint256));
require(block.timestamp >= nationJoinTime + 10, "NAPact: Nation must stay for at least 10 seconds");
// Remove nation from council
council.remove(nationID);
// Remove nation from whitelist
AdminFacet admin = AdminFacet(diamond);
admin.removeFromTreatyWhitelist(nationID); // need to be whitelisted again to join
// Remove nation's signature
super.treatyLeave();
}
/// @notice Can pay between half and full deposit interval
function payMembershipFee() external onlySigner {
GetterFacet getter = GetterFacet(diamond);
// Check last paid time
uint256 nationID = getter.getEntityByAddress(msg.sender);
require(block.timestamp > lastPaid[nationID] + depositTimeInterval / 2, "CDFund: Last payment was too soon");
_payFeesHelper(nationID);
}
function _payFeesHelper(uint256 _nationID) internal {
GetterFacet getter = GetterFacet(diamond);
address senderCapitalAddr = getter.getAddress(getter.getCapital(_nationID));
// Check balance sufficience
require(goldToken.balanceOf(senderCapitalAddr) >= goldFee, "CDFund: Insufficient gold balance");
require(foodToken.balanceOf(senderCapitalAddr) >= foodFee, "CDFund: Insufficient food balance");
// Pay fee
goldToken.transferFrom(senderCapitalAddr, address(this), goldFee);
foodToken.transferFrom(senderCapitalAddr, address(this), foodFee);
// Update last paid time
lastPaid[_nationID] = block.timestamp;
}
// ----------------------------------------------------------
// Permission functions
// ----------------------------------------------------------
function approveBattle(uint256 _nationID, bytes memory _encodedParams) public view override returns (bool) {
GetterFacet getter = GetterFacet(diamond);
// Disapprove if target nation is part of collective fund
(, , uint256 battleTargetID) = abi.decode(_encodedParams, (uint256, uint256, uint256));
uint256 targetNationID = getter.getNation(battleTargetID);
uint256 treatyID = getter.getEntityByAddress(address(this));
if (getter.getNationTreatySignature(targetNationID, treatyID) != 0) return false;
return super.approveBattle(_nationID, _encodedParams);
}
// ----------------------------------------------------------
// Helper functions
// ----------------------------------------------------------
function _strEq(string memory _s1, string memory _s2) private pure returns (bool) {
if (bytes(_s1).length != bytes(_s2).length) return false;
return (keccak256(abi.encodePacked((_s1))) == keccak256(abi.encodePacked((_s2))));
}
}