aboutsummaryrefslogtreecommitdiffstats
path: root/test/compilationTests/corion/publisher.sol
blob: 48090d02a14c48bd1e152875cf11c8224148fa0b (plain) (blame)
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
269
270
271
272
273
274
275
276
277
278
pragma solidity ^0.4.11;

import "./announcementTypes.sol";
import "./module.sol";
import "./moduleHandler.sol";
import "./safeMath.sol";

contract publisher is announcementTypes, module, safeMath {
    /*
        module callbacks
    */
    function transferEvent(address payable from, address payable to, uint256 value) external returns (bool success) {
        /*
            Transaction completed. This function is available only for moduleHandler
            If a transaction is carried out from or to an address which participated in the objection of an announcement, its objection purport is automatically set
        */
        require( super.isModuleHandler(msg.sender) );
        uint256 announcementID;
        uint256 a;
        // need reverse lookup
        for ( a=0 ; a<opponents[from].length ; a++ ) {
            announcementID = opponents[msg.sender][a];
            if ( announcements[announcementID].end < block.number && announcements[announcementID].open ) {
                announcements[announcementID].oppositionWeight = safeSub(announcements[a].oppositionWeight, value);
            }
        }
        for ( a=0 ; a<opponents[to].length ; a++ ) {
            announcementID = opponents[msg.sender][a];
            if ( announcements[announcementID].end < block.number && announcements[announcementID].open ) {
                announcements[announcementID].oppositionWeight = safeAdd(announcements[a].oppositionWeight, value);
            }
        }
        return true;
    }

    /*
        Pool
    */

    uint256 public  minAnnouncementDelay = 40320;
    uint256 public minAnnouncementDelayOnICO = 17280;
    uint8 public oppositeRate = 33;

    struct announcements_s {
        announcementType Type;
        uint256 start;
        uint256 end;
        bool open;
        string announcement;
        string link;
        bool oppositable;
        uint256 oppositionWeight;
        bool result;

        string _str;
        uint256 _uint;
        address payable _addr;
    }
    mapping(uint256 => announcements_s) public announcements;
    uint256 announcementsLength = 1;

    mapping (address => uint256[]) public opponents;

    constructor(address payable moduleHandler) public {
        /*
            Installation function.  The installer will be registered in the admin list automatically

            @moduleHandler      Address of moduleHandler
        */
        super.registerModuleHandler(moduleHandler);
    }

    function Announcements(uint256 id) public view returns (uint256 Type, uint256 Start, uint256 End, bool Closed, string memory Announcement, string memory Link, bool Opposited, string memory _str, uint256 _uint, address _addr) {
        /*
            Announcement data query

            @id             Its identification

            @Type           Subject of announcement
            @Start          Height of announcement block
            @End            Planned completion of announcement
            @Closed         Closed or not
            @Announcement   Announcement text
            @Link           Link  perhaps to a Forum
            @Opposited      Objected or not
            @_str           Text value
            @_uint          Number value
            @_addr          Address value
        */
        Type = uint256(announcements[id].Type);
        Start = announcements[id].start;
        End = announcements[id].end;
        Closed = ! announcements[id].open;
        Announcement = announcements[id].announcement;
        Link = announcements[id].link;
        if ( checkOpposited(announcements[id].oppositionWeight, announcements[id].oppositable) ) {
            Opposited = true;
        }
        _str = announcements[id]._str;
        _uint = announcements[id]._uint;
        _addr = announcements[id]._addr;
    }

    function checkOpposited(uint256 weight, bool oppositable) public view returns (bool success) {
        /*
            Veto check

            @weight         Purport of objections so far
            @oppositable    Opposable at all

            @success        Opposed or not
        */
        if ( ! oppositable ) { return false; }
        (bool _success, uint256 _amount) = moduleHandler(moduleHandlerAddress).totalSupply();
        require( _success );
        return _amount * oppositeRate / 100 > weight;
    }

    function newAnnouncement(announcementType Type, string calldata Announcement, string calldata Link, bool Oppositable, string calldata _str, uint256 _uint, address payable _addr) onlyOwner external {
        /*
            New announcement. Can be called  only by those in the admin list

            @Type           Topic of announcement
            @Start          height of announcement block
            @End            planned completion of announcement
            @Closed         Completed or not
            @Announcement   Announcement text
            @Link           link to a Forum
            @Opposition     opposed or not
            @_str           text box
            @_uint          number box
            @_addr          address box
        */
        announcementsLength++;
        announcements[announcementsLength].Type = Type;
        announcements[announcementsLength].start = block.number;
        if ( checkICO() ) {
            announcements[announcementsLength].end = block.number + minAnnouncementDelayOnICO;
        } else {
            announcements[announcementsLength].end = block.number + minAnnouncementDelay;
        }
        announcements[announcementsLength].open = true;
        announcements[announcementsLength].announcement = Announcement;
        announcements[announcementsLength].link = Link;
        announcements[announcementsLength].oppositable = Oppositable;
        announcements[announcementsLength].oppositionWeight = 0;
        announcements[announcementsLength].result = false;
        announcements[announcementsLength]._str = _str;
        announcements[announcementsLength]._uint = _uint;
        announcements[announcementsLength]._addr = _addr;
        emit ENewAnnouncement(announcementsLength, Type);
    }

    function closeAnnouncement(uint256 id) onlyOwner external {
        /*
            Close announcement. It can be closed only by those in the admin list. Windup is allowed only after the announcement is completed.

            @id     Announcement identification
        */
        require( announcements[id].open && announcements[id].end < block.number );
        if ( ! checkOpposited(announcements[id].oppositionWeight, announcements[id].oppositable) ) {
            announcements[id].result = true;
            if ( announcements[id].Type == announcementType.newModule ) {
                require( moduleHandler(moduleHandlerAddress).newModule(announcements[id]._str, announcements[id]._addr, true, true) );
            } else if ( announcements[id].Type == announcementType.dropModule ) {
                require( moduleHandler(moduleHandlerAddress).dropModule(announcements[id]._str, true) );
            } else if ( announcements[id].Type == announcementType.replaceModule ) {
                require( moduleHandler(moduleHandlerAddress).replaceModule(announcements[id]._str, announcements[id]._addr, true) );
            } else if ( announcements[id].Type == announcementType.replaceModuleHandler) {
                require( moduleHandler(moduleHandlerAddress).replaceModuleHandler(announcements[id]._addr) );
            } else if ( announcements[id].Type == announcementType.transactionFeeRate ||
                        announcements[id].Type == announcementType.transactionFeeMin ||
                        announcements[id].Type == announcementType.transactionFeeMax ||
                        announcements[id].Type == announcementType.transactionFeeBurn ) {
                require( moduleHandler(moduleHandlerAddress).configureModule("token", announcements[id].Type, announcements[id]._uint) );
            } else if ( announcements[id].Type == announcementType.providerPublicFunds ||
                        announcements[id].Type == announcementType.providerPrivateFunds ||
                        announcements[id].Type == announcementType.providerPrivateClientLimit ||
                        announcements[id].Type == announcementType.providerPublicMinRate ||
                        announcements[id].Type == announcementType.providerPublicMaxRate ||
                        announcements[id].Type == announcementType.providerPrivateMinRate ||
                        announcements[id].Type == announcementType.providerPrivateMaxRate ||
                        announcements[id].Type == announcementType.providerGasProtect ||
                        announcements[id].Type == announcementType.providerInterestMinFunds ||
                        announcements[id].Type == announcementType.providerRentRate ) {
                require( moduleHandler(moduleHandlerAddress).configureModule("provider", announcements[id].Type, announcements[id]._uint) );
            } else if ( announcements[id].Type == announcementType.schellingRoundBlockDelay ||
                        announcements[id].Type == announcementType.schellingCheckRounds ||
                        announcements[id].Type == announcementType.schellingCheckAboves ||
                        announcements[id].Type == announcementType.schellingRate ) {
                require( moduleHandler(moduleHandlerAddress).configureModule("schelling", announcements[id].Type, announcements[id]._uint) );
            } else if ( announcements[id].Type == announcementType.publisherMinAnnouncementDelay) {
                minAnnouncementDelay = announcements[id]._uint;
            } else if ( announcements[id].Type == announcementType.publisherOppositeRate) {
                oppositeRate = uint8(announcements[id]._uint);
            }
        }
        announcements[id].end = block.number;
        announcements[id].open = false;
    }

    function oppositeAnnouncement(uint256 id) external {
        /*
            Opposition of announcement
            If announcement is opposable, anyone owning a token can oppose it
            Opposition is automatically with the total amount of tokens
            If the quantity of his tokens changes, the purport of his opposition changes automatically
            The prime time is the windup  of the announcement, because this is the moment when the number of tokens in opposition are counted.
            One address is entitled to be in oppositon only once. An opposition cannot be withdrawn.
            Running announcements can be opposed only.

            @id     Announcement identification
        */
        uint256 newArrayID = 0;
        bool foundEmptyArrayID = false;
        require( announcements[id].open );
        require( announcements[id].oppositable );
        for ( uint256 a=0 ; a<opponents[msg.sender].length ; a++ ) {
            require( opponents[msg.sender][a] != id );
            if ( ! announcements[opponents[msg.sender][a]].open) {
                delete opponents[msg.sender][a];
                if ( ! foundEmptyArrayID ) {
                    foundEmptyArrayID = true;
                    newArrayID = a;
                }
            }
            if ( ! foundEmptyArrayID ) {
                foundEmptyArrayID = true;
                newArrayID = a;
            }
        }
        (bool _success, uint256 _balance) = moduleHandler(moduleHandlerAddress).balanceOf(msg.sender);
        require( _success );
        require( _balance > 0);
        if ( foundEmptyArrayID ) {
            opponents[msg.sender][newArrayID] = id;
        } else {
            opponents[msg.sender].push(id);
        }
        announcements[id].oppositionWeight += _balance;
        emit EOppositeAnnouncement(id, msg.sender, _balance);
    }

    function invalidateAnnouncement(uint256 id) onlyOwner external {
        /*
            Withdraw announcement. Only those in the admin list can withdraw it.

            @id     Announcement identification
        */
        require( announcements[id].open );
        announcements[id].end = block.number;
        announcements[id].open = false;
        emit EInvalidateAnnouncement(id);
    }

    modifier onlyOwner() {
        /*
            Only the owner  is allowed to call it.
        */
        require( moduleHandler(moduleHandlerAddress).owners(msg.sender) );
        _;
    }

    function checkICO() internal returns (bool isICO) {
        /*
            Inner function to check the ICO status.
            @bool       Is the ICO in process or not?
        */
        (bool _success, bool _isICO) = moduleHandler(moduleHandlerAddress).isICO();
        require( _success );
        return _isICO;
    }

    event ENewAnnouncement(uint256 id, announcementType typ);
    event EOppositeAnnouncement(uint256 id, address addr, uint256 value);
    event EInvalidateAnnouncement(uint256 id);
    event ECloseAnnouncement(uint256 id);
}