DEXPOLYGON
Search…
Smart Contract
1
// SPDX-License-Identifier: MIT
2
pragma solidity 0.6.12;
3
4
5
//
6
/*
7
* @dev Provides information about the current execution context, including the
8
* sender of the transaction and its data. While these are generally available
9
* via msg.sender and msg.data, they should not be accessed in such a direct
10
* manner, since when dealing with GSN meta-transactions the account sending and
11
* paying for execution may not be the actual sender (as far as an application
12
* is concerned).
13
*
14
* This contract is only required for intermediate, library-like contracts.
15
*/
16
contract Context {
17
// Empty internal constructor, to prevent people from mistakenly deploying
18
// an instance of this contract, which should be used via inheritance.
19
constructor() internal {}
20
21
function _msgSender() internal view returns (address payable) {
22
return msg.sender;
23
}
24
25
function _msgData() internal view returns (bytes memory) {
26
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
27
return msg.data;
28
}
29
}
30
31
//
32
/**
33
* @dev Contract module which provides a basic access control mechanism, where
34
* there is an account (an owner) that can be granted exclusive access to
35
* specific functions.
36
*
37
* By default, the owner account will be the one that deploys the contract. This
38
* can later be changed with {transferOwnership}.
39
*
40
* This module is used through inheritance. It will make available the modifier
41
* `onlyOwner`, which can be applied to your functions to restrict their use to
42
* the owner.
43
*/
44
contract Ownable is Context {
45
address private _owner;
46
47
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
48
49
/**
50
* @dev Initializes the contract setting the deployer as the initial owner.
51
*/
52
constructor() internal {
53
address msgSender = _msgSender();
54
_owner = msgSender;
55
emit OwnershipTransferred(address(0), msgSender);
56
}
57
58
/**
59
* @dev Returns the address of the current owner.
60
*/
61
function owner() public view returns (address) {
62
return _owner;
63
}
64
65
/**
66
* @dev Throws if called by any account other than the owner.
67
*/
68
modifier onlyOwner() {
69
require(_owner == _msgSender(), 'Ownable: caller is not the owner');
70
_;
71
}
72
73
/**
74
* @dev Leaves the contract without owner. It will not be possible to call
75
* `onlyOwner` functions anymore. Can only be called by the current owner.
76
*
77
* NOTE: Renouncing ownership will leave the contract without an owner,
78
* thereby removing any functionality that is only available to the owner.
79
*/
80
function renounceOwnership() public onlyOwner {
81
emit OwnershipTransferred(_owner, address(0));
82
_owner = address(0);
83
}
84
85
/**
86
* @dev Transfers ownership of the contract to a new account (`newOwner`).
87
* Can only be called by the current owner.
88
*/
89
function transferOwnership(address newOwner) public onlyOwner {
90
_transferOwnership(newOwner);
91
}
92
93
/**
94
* @dev Transfers ownership of the contract to a new account (`newOwner`).
95
*/
96
function _transferOwnership(address newOwner) internal {
97
require(newOwner != address(0), 'Ownable: new owner is the zero address');
98
emit OwnershipTransferred(_owner, newOwner);
99
_owner = newOwner;
100
}
101
}
102
103
//
104
interface IPOLYGON {
105
/**
106
* @dev Returns the amount of tokens in existence.
107
*/
108
function totalSupply() external view returns (uint256);
109
110
/**
111
* @dev Returns the token decimals.
112
*/
113
function decimals() external view returns (uint8);
114
115
/**
116
* @dev Returns the token symbol.
117
*/
118
function symbol() external view returns (string memory);
119
120
/**
121
* @dev Returns the token name.
122
*/
123
function name() external view returns (string memory);
124
125
/**
126
* @dev Returns the bep token owner.
127
*/
128
function getOwner() external view returns (address);
129
130
/**
131
* @dev Returns the amount of tokens owned by `account`.
132
*/
133
function balanceOf(address account) external view returns (uint256);
134
135
/**
136
* @dev Moves `amount` tokens from the caller's account to `recipient`.
137
*
138
* Returns a boolean value indicating whether the operation succeeded.
139
*
140
* Emits a {Transfer} event.
141
*/
142
function transfer(address recipient, uint256 amount) external returns (bool);
143
144
/**
145
* @dev Returns the remaining number of tokens that `spender` will be
146
* allowed to spend on behalf of `owner` through {transferFrom}. This is
147
* zero by default.
148
*
149
* This value changes when {approve} or {transferFrom} are called.
150
*/
151
function allowance(address _owner, address spender) external view returns (uint256);
152
153
/**
154
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
155
*
156
* Returns a boolean value indicating whether the operation succeeded.
157
*
158
* IMPORTANT: Beware that changing an allowance with this method brings the risk
159
* that someone may use both the old and the new allowance by unfortunate
160
* transaction ordering. One possible solution to mitigate this race
161
* condition is to first reduce the spender's allowance to 0 and set the
162
* desired value afterwards:
163
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
164
*
165
* Emits an {Approval} event.
166
*/
167
function approve(address spender, uint256 amount) external returns (bool);
168
169
/**
170
* @dev Moves `amount` tokens from `sender` to `recipient` using the
171
* allowance mechanism. `amount` is then deducted from the caller's
172
* allowance.
173
*
174
* Returns a boolean value indicating whether the operation succeeded.
175
*
176
* Emits a {Transfer} event.
177
*/
178
function transferFrom(
179
address sender,
180
address recipient,
181
uint256 amount
182
) external returns (bool);
183
184
/**
185
* @dev Emitted when `value` tokens are moved from one account (`from`) to
186
* another (`to`).
187
*
188
* Note that `value` may be zero.
189
*/
190
event Transfer(address indexed from, address indexed to, uint256 value);
191
192
/**
193
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
194
* a call to {approve}. `value` is the new allowance.
195
*/
196
event Approval(address indexed owner, address indexed spender, uint256 value);
197
}
198
199
//
200
/**
201
* @dev Wrappers over Solidity's arithmetic operations with added overflow
202
* checks.
203
*
204
* Arithmetic operations in Solidity wrap on overflow. This can easily result
205
* in bugs, because programmers usually assume that an overflow raises an
206
* error, which is the standard behavior in high level programming languages.
207
* `SafeMath` restores this intuition by reverting the transaction when an
208
* operation overflows.
209
*
210
* Using this library instead of the unchecked operations eliminates an entire
211
* class of bugs, so it's recommended to use it always.
212
*/
213
library SafeMath {
214
/**
215
* @dev Returns the addition of two unsigned integers, reverting on
216
* overflow.
217
*
218
* Counterpart to Solidity's `+` operator.
219
*
220
* Requirements:
221
*
222
* - Addition cannot overflow.
223
*/
224
function add(uint256 a, uint256 b) internal pure returns (uint256) {
225
uint256 c = a + b;
226
require(c >= a, 'SafeMath: addition overflow');
227
228
return c;
229
}
230
231
/**
232
* @dev Returns the subtraction of two unsigned integers, reverting on
233
* overflow (when the result is negative).
234
*
235
* Counterpart to Solidity's `-` operator.
236
*
237
* Requirements:
238
*
239
* - Subtraction cannot overflow.
240
*/
241
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
242
return sub(a, b, 'SafeMath: subtraction overflow');
243
}
244
245
/**
246
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
247
* overflow (when the result is negative).
248
*
249
* Counterpart to Solidity's `-` operator.
250
*
251
* Requirements:
252
*
253
* - Subtraction cannot overflow.
254
*/
255
function sub(
256
uint256 a,
257
uint256 b,
258
string memory errorMessage
259
) internal pure returns (uint256) {
260
require(b <= a, errorMessage);
261
uint256 c = a - b;
262
263
return c;
264
}
265
266
/**
267
* @dev Returns the multiplication of two unsigned integers, reverting on
268
* overflow.
269
*
270
* Counterpart to Solidity's `*` operator.
271
*
272
* Requirements:
273
*
274
* - Multiplication cannot overflow.
275
*/
276
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
277
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
278
// benefit is lost if 'b' is also tested.
279
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
280
if (a == 0) {
281
return 0;
282
}
283
284
uint256 c = a * b;
285
require(c / a == b, 'SafeMath: multiplication overflow');
286
287
return c;
288
}
289
290
/**
291
* @dev Returns the integer division of two unsigned integers. Reverts on
292
* division by zero. The result is rounded towards zero.
293
*
294
* Counterpart to Solidity's `/` operator. Note: this function uses a
295
* `revert` opcode (which leaves remaining gas untouched) while Solidity
296
* uses an invalid opcode to revert (consuming all remaining gas).
297
*
298
* Requirements:
299
*
300
* - The divisor cannot be zero.
301
*/
302
function div(uint256 a, uint256 b) internal pure returns (uint256) {
303
return div(a, b, 'SafeMath: division by zero');
304
}
305
306
/**
307
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
308
* division by zero. The result is rounded towards zero.
309
*
310
* Counterpart to Solidity's `/` operator. Note: this function uses a
311
* `revert` opcode (which leaves remaining gas untouched) while Solidity
312
* uses an invalid opcode to revert (consuming all remaining gas).
313
*
314
* Requirements:
315
*
316
* - The divisor cannot be zero.
317
*/
318
function div(
319
uint256 a,
320
uint256 b,
321
string memory errorMessage
322
) internal pure returns (uint256) {
323
require(b > 0, errorMessage);
324
uint256 c = a / b;
325
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
326
327
return c;
328
}
329
330
/**
331
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
332
* Reverts when dividing by zero.
333
*
334
* Counterpart to Solidity's `%` operator. This function uses a `revert`
335
* opcode (which leaves remaining gas untouched) while Solidity uses an
336
* invalid opcode to revert (consuming all remaining gas).
337
*
338
* Requirements:
339
*
340
* - The divisor cannot be zero.
341
*/
342
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
343
return mod(a, b, 'SafeMath: modulo by zero');
344
}
345
346
/**
347
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
348
* Reverts with custom message when dividing by zero.
349
*
350
* Counterpart to Solidity's `%` operator. This function uses a `revert`
351
* opcode (which leaves remaining gas untouched) while Solidity uses an
352
* invalid opcode to revert (consuming all remaining gas).
353
*
354
* Requirements:
355
*
356
* - The divisor cannot be zero.
357
*/
358
function mod(
359
uint256 a,
360
uint256 b,
361
string memory errorMessage
362
) internal pure returns (uint256) {
363
require(b != 0, errorMessage);
364
return a % b;
365
}
366
367
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
368
z = x < y ? x : y;
369
}
370
371
// babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
372
function sqrt(uint256 y) internal pure returns (uint256 z) {
373
if (y > 3) {
374
z = y;
375
uint256 x = y / 2 + 1;
376
while (x < z) {
377
z = x;
378
x = (y / x + x) / 2;
379
}
380
} else if (y != 0) {
381
z = 1;
382
}
383
}
384
}
385
386
//
387
/**
388
* @dev Collection of functions related to the address type
389
*/
390
library Address {
391
/**
392
* @dev Returns true if `account` is a contract.
393
*
394
* [IMPORTANT]
395
* ====
396
* It is unsafe to assume that an address for which this function returns
397
* false is an externally-owned account (EOA) and not a contract.
398
*
399
* Among others, `isContract` will return false for the following
400
* types of addresses:
401
*
402
* - an externally-owned account
403
* - a contract in construction
404
* - an address where a contract will be created
405
* - an address where a contract lived, but was destroyed
406
* ====
407
*/
408
function isContract(address account) internal view returns (bool) {
409
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
410
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
411
// for accounts without code, i.e. `keccak256('')`
412
bytes32 codehash;
413
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
414
// solhint-disable-next-line no-inline-assembly
415
assembly {
416
codehash := extcodehash(account)
417
}
418
return (codehash != accountHash && codehash != 0x0);
419
}
420
421
/**
422
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
423
* `recipient`, forwarding all available gas and reverting on errors.
424
*
425
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
426
* of certain opcodes, possibly making contracts go over the 2300 gas limit
427
* imposed by `transfer`, making them unable to receive funds via
428
* `transfer`. {sendValue} removes this limitation.
429
*
430
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
431
*
432
* IMPORTANT: because control is transferred to `recipient`, care must be
433
* taken to not create reentrancy vulnerabilities. Consider using
434
* {ReentrancyGuard} or the
435
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
436
*/
437
function sendValue(address payable recipient, uint256 amount) internal {
438
require(address(this).balance >= amount, 'Address: insufficient balance');
439
440
// solhint-disable-next-line avoid-low-level-calls, avoid-call-value
441
(bool success, ) = recipient.call{value: amount}('');
442
require(success, 'Address: unable to send value, recipient may have reverted');
443
}
444
445
/**
446
* @dev Performs a Solidity function call using a low level `call`. A
447
* plain`call` is an unsafe replacement for a function call: use this
448
* function instead.
449
*
450
* If `target` reverts with a revert reason, it is bubbled up by this
451
* function (like regular Solidity function calls).
452
*
453
* Returns the raw returned data. To convert to the expected return value,
454
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
455
*
456
* Requirements:
457
*
458
* - `target` must be a contract.
459
* - calling `target` with `data` must not revert.
460
*
461
* _Available since v3.1._
462
*/
463
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
464
return functionCall(target, data, 'Address: low-level call failed');
465
}
466
467
/**
468
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
469
* `errorMessage` as a fallback revert reason when `target` reverts.
470
*
471
* _Available since v3.1._
472
*/
473
function functionCall(
474
address target,
475
bytes memory data,
476
string memory errorMessage
477
) internal returns (bytes memory) {
478
return _functionCallWithValue(target, data, 0, errorMessage);
479
}
480
481
/**
482
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
483
* but also transferring `value` wei to `target`.
484
*
485
* Requirements:
486
*
487
* - the calling contract must have an ETH balance of at least `value`.
488
* - the called Solidity function must be `payable`.
489
*
490
* _Available since v3.1._
491
*/
492
function functionCallWithValue(
493
address target,
494
bytes memory data,
495
uint256 value
496
) internal returns (bytes memory) {
497
return functionCallWithValue(target, data, value, 'Address: low-level call with value failed');
498
}
499
500
/**
501
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
502
* with `errorMessage` as a fallback revert reason when `target` reverts.
503
*
504
* _Available since v3.1._
505
*/
506
function functionCallWithValue(
507
address target,
508
bytes memory data,
509
uint256 value,
510
string memory errorMessage
511
) internal returns (bytes memory) {
512
require(address(this).balance >= value, 'Address: insufficient balance for call');
513
return _functionCallWithValue(target, data, value, errorMessage);
514
}
515
516
function _functionCallWithValue(
517
address target,
518
bytes memory data,
519
uint256 weiValue,
520
string memory errorMessage
521
) private returns (bytes memory) {
522
require(isContract(target), 'Address: call to non-contract');
523
524
// solhint-disable-next-line avoid-low-level-calls
525
(bool success, bytes memory returndata) = target.call{value: weiValue}(data);
526
if (success) {
527
return returndata;
528
} else {
529
// Look for revert reason and bubble it up if present
530
if (returndata.length > 0) {
531
// The easiest way to bubble the revert reason is using memory via assembly
532
533
// solhint-disable-next-line no-inline-assembly
534
assembly {
535
let returndata_size := mload(returndata)
536
revert(add(32, returndata), returndata_size)
537
}
538
} else {
539
revert(errorMessage);
540
}
541
}
542
}
543
}
544
545
//
546
/**
547
* @dev Implementation of the {IPOLYGON} interface.
548
*
549
* This implementation is agnostic to the way tokens are created. This means
550
* that a supply mechanism has to be added in a derived contract using {_mint}.
551
* For a generic mechanism see {POLYGONPresetMinterPauser}.
552
*
553
* TIP: For a detailed writeup see our guide
554
* https://forum.zeppelin.solutions/t/how-to-implement-Polygon-supply-mechanisms/226[How
555
* to implement supply mechanisms].
556
*
557
* We have followed general OpenZeppelin guidelines: functions revert instead
558
* of returning `false` on failure. This behavior is nonetheless conventional
559
* and does not conflict with the expectations of Polygon applications.
560
*
561
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
562
* This allows applications to reconstruct the allowance for all accounts just
563
* by listening to said events. Other implementations of the EIP may not emit
564
* these events, as it isn't required by the specification.
565
*
566
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
567
* functions have been added to mitigate the well-known issues around setting
568
* allowances. See {IPOLYGON-approve}.
569
*/
570
contract Polygon is Context, IPOLYGON, Ownable {
571
using SafeMath for uint256;
572
using Address for address;
573
574
mapping(address => uint256) private _balances;
575
576
mapping(address => mapping(address => uint256)) private _allowances;
577
578
uint256 private _totalSupply;
579
580
string private _name;
581
string private _symbol;
582
uint8 private _decimals;
583
584
/**
585
* @dev Sets the values for {name} and {symbol}, initializes {decimals} with
586
* a default value of 18.
587
*
588
* To select a different value for {decimals}, use {_setupDecimals}.
589
*
590
* All three of these values are immutable: they can only be set once during
591
* construction.
592
*/
593
constructor(string memory name, string memory symbol) public {
594
_name = name;
595
_symbol = symbol;
596
_decimals = 18;
597
}
598
599
/**
600
* @dev Returns the bep token owner.
601
*/
602
function getOwner() external override view returns (address) {
603
return owner();
604
}
605
606
/**
607
* @dev Returns the token name.
608
*/
609
function name() public override view returns (string memory) {
610
return _name;
611
}
612
613
/**
614
* @dev Returns the token decimals.
615
*/
616
function decimals() public override view returns (uint8) {
617
return _decimals;
618
}
619
620
/**
621
* @dev Returns the token symbol.
622
*/
623
function symbol() public override view returns (string memory) {
624
return _symbol;
625
}
626
627
/**
628
* @dev See {Polygon-totalSupply}.
629
*/
630
function totalSupply() public override view returns (uint256) {
631
return _totalSupply;
632
}
633
634
/**
635
* @dev See {Polygon-balanceOf}.
636
*/
637
function balanceOf(address account) public override view returns (uint256) {
638
return _balances[account];
639
}
640
641
/**
642
* @dev See {Polygon-transfer}.
643
*
644
* Requirements:
645
*
646
* - `recipient` cannot be the zero address.
647
* - the caller must have a balance of at least `amount`.
648
*/
649
function transfer(address recipient, uint256 amount) public override returns (bool) {
650
_transfer(_msgSender(), recipient, amount);
651
return true;
652
}
653
654
/**
655
* @dev See {Polygon-allowance}.
656
*/
657
function allowance(address owner, address spender) public override view returns (uint256) {
658
return _allowances[owner][spender];
659
}
660
661
/**
662
* @dev See {Polygon-approve}.
663
*
664
* Requirements:
665
*
666
* - `spender` cannot be the zero address.
667
*/
668
function approve(address spender, uint256 amount) public override returns (bool) {
669
_approve(_msgSender(), spender, amount);
670
return true;
671
}
672
673
/**
674
* @dev See {Polygon-transferFrom}.
675
*
676
* Emits an {Approval} event indicating the updated allowance. This is not
677
* required by the EIP. See the note at the beginning of {Polygon};
678
*
679
* Requirements:
680
* - `sender` and `recipient` cannot be the zero address.
681
* - `sender` must have a balance of at least `amount`.
682
* - the caller must have allowance for `sender`'s tokens of at least
683
* `amount`.
684
*/
685
function transferFrom(
686
address sender,
687
address recipient,
688
uint256 amount
689
) public override returns (bool) {
690
_transfer(sender, recipient, amount);
691
_approve(
692
sender,
693
_msgSender(),
694
_allowances[sender][_msgSender()].sub(amount, 'Polygon: transfer amount exceeds allowance')
695
);
696
return true;
697
}
698
699
/**
700
* @dev Atomically increases the allowance granted to `spender` by the caller.
701
*
702
* This is an alternative to {approve} that can be used as a mitigation for
703
* problems described in {Polygon-approve}.
704
*
705
* Emits an {Approval} event indicating the updated allowance.
706
*
707
* Requirements:
708
*
709
* - `spender` cannot be the zero address.
710
*/
711
function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
712
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
713
return true;
714
}
715
716
/**
717
* @dev Atomically decreases the allowance granted to `spender` by the caller.
718
*
719
* This is an alternative to {approve} that can be used as a mitigation for
720
* problems described in {Polygon-approve}.
721
*
722
* Emits an {Approval} event indicating the updated allowance.
723
*
724
* Requirements:
725
*
726
* - `spender` cannot be the zero address.
727
* - `spender` must have allowance for the caller of at least
728
* `subtractedValue`.
729
*/
730
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
731
_approve(
732
_msgSender(),
733
spender,
734
_allowances[_msgSender()][spender].sub(subtractedValue, 'Polygon: decreased allowance below zero')
735
);
736
return true;
737
}
738
739
/**
740
* @dev Creates `amount` tokens and assigns them to `msg.sender`, increasing
741
* the total supply.
742
*
743
* Requirements
744
*
745
* - `msg.sender` must be the token owner
746
*/
747
function mint(uint256 amount) public onlyOwner returns (bool) {
748
_mint(_msgSender(), amount);
749
return true;
750
}
751
752
/**
753
* @dev Moves tokens `amount` from `sender` to `recipient`.
754
*
755
* This is internal function is equivalent to {transfer}, and can be used to
756
* e.g. implement automatic token fees, slashing mechanisms, etc.
757
*
758
* Emits a {Transfer} event.
759
*
760
* Requirements:
761
*
762
* - `sender` cannot be the zero address.
763
* - `recipient` cannot be the zero address.
764
* - `sender` must have a balance of at least `amount`.
765
*/
766
function _transfer(
767
address sender,
768
address recipient,
769
uint256 amount
770
) internal {
771
require(sender != address(0), 'Polygon: transfer from the zero address');
772
require(recipient != address(0), 'Polygon: transfer to the zero address');
773
774
_balances[sender] = _balances[sender].sub(amount, 'Polygon: transfer amount exceeds balance');
775
_balances[recipient] = _balances[recipient].add(amount);
776
emit Transfer(sender, recipient, amount);
777
}
778
779
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
780
* the total supply.
781
*
782
* Emits a {Transfer} event with `from` set to the zero address.
783
*
784
* Requirements
785
*
786
* - `to` cannot be the zero address.
787
*/
788
function _mint(address account, uint256 amount) internal {
789
require(account != address(0), 'Polygon: mint to the zero address');
790
791
_totalSupply = _totalSupply.add(amount);
792
_balances[account] = _balances[account].add(amount);
793
emit Transfer(address(0), account, amount);
794
}
795
796
/**
797
* @dev Destroys `amount` tokens from `account`, reducing the
798
* total supply.
799
*
800
* Emits a {Transfer} event with `to` set to the zero address.
801
*
802
* Requirements
803
*
804
* - `account` cannot be the zero address.
805
* - `account` must have at least `amount` tokens.
806
*/
807
function _burn(address account, uint256 amount) internal {
808
require(account != address(0), 'Polygon: burn from the zero address');
809
810
_balances[account] = _balances[account].sub(amount, 'Polygon: burn amount exceeds balance');
811
_totalSupply = _totalSupply.sub(amount);
812
emit Transfer(account, address(0), amount);
813
}
814
815
/**
816
* @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens.
817
*
818
* This is internal function is equivalent to `approve`, and can be used to
819
* e.g. set automatic allowances for certain subsystems, etc.
820
*
821
* Emits an {Approval} event.
822
*
823
* Requirements:
824
*
825
* - `owner` cannot be the zero address.
826
* - `spender` cannot be the zero address.
827
*/
828
function _approve(
829
address owner,
830
address spender,
831
uint256 amount
832
) internal {
833
require(owner != address(0), 'Polygon: approve from the zero address');
834
require(spender != address(0), 'Polygon: approve to the zero address');
835
836
_allowances[owner][spender] = amount;
837
emit Approval(owner, spender, amount);
838
}
839
840
/**
841
* @dev Destroys `amount` tokens from `account`.`amount` is then deducted
842
* from the caller's allowance.
843
*
844
* See {_burn} and {_approve}.
845
*/
846
function _burnFrom(address account, uint256 amount) internal {
847
_burn(account, amount);
848
_approve(
849
account,
850
_msgSender(),
851
_allowances[account][_msgSender()].sub(amount, 'Polygon: burn amount exceeds allowance')
852
);
853
}
854
}
855
856
// DpolyToken with Governance.
857
contract DpolyToken is Polygon('Dexpolygon', 'DPOLY') {
858
/// @notice Creates `_amount` token to `_to`. Must only be called by the owner (MasterChef).
859
function mint(address _to, uint256 _amount) public onlyOwner {
860
_mint(_to, _amount);
861
_moveDelegates(address(0), _delegates[_to], _amount);
862
}
863
864
// Copied and modified from YAM code:
865
// https://github.com/yam-finance/yam-protocol/blob/master/contracts/token/YAMGovernanceStorage.sol
866
// https://github.com/yam-finance/yam-protocol/blob/master/contracts/token/YAMGovernance.sol
867
// Which is copied and modified from COMPOUND:
868
// https://github.com/compound-finance/compound-protocol/blob/master/contracts/Governance/Comp.sol
869
870
871
mapping (address => address) internal _delegates;
872
873
/// @notice A checkpoint for marking number of votes from a given block
874
struct Checkpoint {
875
uint32 fromBlock;
876
uint256 votes;
877
}
878
879
/// @notice A record of votes checkpoints for each account, by index
880
mapping (address => mapping (uint32 => Checkpoint)) public checkpoints;
881
882
/// @notice The number of checkpoints for each account
883
mapping (address => uint32) public numCheckpoints;
884
885
/// @notice The EIP-712 typehash for the contract's domain
886
bytes32 public constant DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)");
887
888
/// @notice The EIP-712 typehash for the delegation struct used by the contract
889
bytes32 public constant DELEGATION_TYPEHASH = keccak256("Delegation(address delegatee,uint256 nonce,uint256 expiry)");
890
891
/// @notice A record of states for signing / validating signatures
892
mapping (address => uint) public nonces;
893
894
/// @notice An event thats emitted when an account changes its delegate
895
event DelegateChanged(address indexed delegator, address indexed fromDelegate, address indexed toDelegate);
896
897
/// @notice An event thats emitted when a delegate account's vote balance changes
898
event DelegateVotesChanged(address indexed delegate, uint previousBalance, uint newBalance);
899
900
/**
901
* @notice Delegate votes from `msg.sender` to `delegatee`
902
* @param delegator The address to get delegatee for
903
*/
904
function delegates(address delegator)
905
external
906
view
907
returns (address)
908
{
909
return _delegates[delegator];
910
}
911
912
/**
913
* @notice Delegate votes from `msg.sender` to `delegatee`
914
* @param delegatee The address to delegate votes to
915
*/
916
function delegate(address delegatee) external {
917
return _delegate(msg.sender, delegatee);
918
}
919
920
/**
921
* @notice Delegates votes from signatory to `delegatee`
922
* @param delegatee The address to delegate votes to
923
* @param nonce The contract state required to match the signature
924
* @param expiry The time at which to expire the signature
925
* @param v The recovery byte of the signature
926
* @param r Half of the ECDSA signature pair
927
* @param s Half of the ECDSA signature pair
928
*/
929
function delegateBySig(
930
address delegatee,
931
uint nonce,
932
uint expiry,
933
uint8 v,
934
bytes32 r,
935
bytes32 s
936
)
937
external
938
{
939
bytes32 domainSeparator = keccak256(
940
abi.encode(
941
DOMAIN_TYPEHASH,
942
keccak256(bytes(name())),
943
getChainId(),
944
address(this)
945
)
946
);
947
948
bytes32 structHash = keccak256(
949
abi.encode(
950
DELEGATION_TYPEHASH,
951
delegatee,
952
nonce,
953
expiry
954
)
955
);
956
957
bytes32 digest = keccak256(
958
abi.encodePacked(
959
"\x19\x01",
960
domainSeparator,
961
structHash
962
)
963
);
964
965
address signatory = ecrecover(digest, v, r, s);
966
require(signatory != address(0), "DPOLY::delegateBySig: invalid signature");
967
require(nonce == nonces[signatory]++, "DPOLY::delegateBySig: invalid nonce");
968
require(now <= expiry, "DPOLY::delegateBySig: signature expired");
969
return _delegate(signatory, delegatee);
970
}
971
972
/**
973
* @notice Gets the current votes balance for `account`
974
* @param account The address to get votes balance
975
* @return The number of current votes for `account`
976
*/
977
function getCurrentVotes(address account)
978
external
979
view
980
returns (uint256)
981
{
982
uint32 nCheckpoints = numCheckpoints[account];
983
return nCheckpoints > 0 ? checkpoints[account][nCheckpoints - 1].votes : 0;
984
}
985
986
/**
987
* @notice Determine the prior number of votes for an account as of a block number
988
* @dev Block number must be a finalized block or else this function will revert to prevent misinformation.
989
* @param account The address of the account to check
990
* @param blockNumber The block number to get the vote balance at
991
* @return The number of votes the account had as of the given block
992
*/
993
function getPriorVotes(address account, uint blockNumber)
994
external
995
view
996
returns (uint256)
997
{
998
require(blockNumber < block.number, "DPOLY::getPriorVotes: not yet determined");
999
1000
uint32 nCheckpoints = numCheckpoints[account];
1001
if (nCheckpoints == 0) {
1002
return 0;
1003
}
1004
1005
// First check most recent balance
1006
if (checkpoints[account][nCheckpoints - 1].fromBlock <= blockNumber) {
1007
return checkpoints[account][nCheckpoints - 1].votes;
1008
}
1009
1010
// Next check implicit zero balance
1011
if (checkpoints[account][0].fromBlock > blockNumber) {
1012
return 0;
1013
}
1014
1015
uint32 lower = 0;
1016
uint32 upper = nCheckpoints - 1;
1017
while (upper > lower) {
1018
uint32 center = upper - (upper - lower) / 2; // ceil, avoiding overflow
1019
Checkpoint memory cp = checkpoints[account][center];
1020
if (cp.fromBlock == blockNumber) {
1021
return cp.votes;
1022
} else if (cp.fromBlock < blockNumber) {
1023
lower = center;
1024
} else {
1025
upper = center - 1;
1026
}
1027
}
1028
return checkpoints[account][lower].votes;
1029
}
1030
1031
function _delegate(address delegator, address delegatee)
1032
internal
1033
{
1034
address currentDelegate = _delegates[delegator];
1035
uint256 delegatorBalance = balanceOf(delegator); // balance of underlying DPOLY (not scaled);
1036
_delegates[delegator] = delegatee;
1037
1038
emit DelegateChanged(delegator, currentDelegate, delegatee);
1039
1040
_moveDelegates(currentDelegate, delegatee, delegatorBalance);
1041
}
1042
1043
function _moveDelegates(address srcRep, address dstRep, uint256 amount) internal {
1044
if (srcRep != dstRep && amount > 0) {
1045
if (srcRep != address(0)) {
1046
// decrease old representative
1047
uint32 srcRepNum = numCheckpoints[srcRep];
1048
uint256 srcRepOld = srcRepNum > 0 ? checkpoints[srcRep][srcRepNum - 1].votes : 0;
1049
uint256 srcRepNew = srcRepOld.sub(amount);
1050
_writeCheckpoint(srcRep, srcRepNum, srcRepOld, srcRepNew);
1051
}
1052
1053
if (dstRep != address(0)) {
1054
// increase new representative
1055
uint32 dstRepNum = numCheckpoints[dstRep];
1056
uint256 dstRepOld = dstRepNum > 0 ? checkpoints[dstRep][dstRepNum - 1].votes : 0;
1057
uint256 dstRepNew = dstRepOld.add(amount);
1058
_writeCheckpoint(dstRep, dstRepNum, dstRepOld, dstRepNew);
1059
}
1060
}
1061
}
1062
1063
function _writeCheckpoint(
1064
address delegatee,
1065
uint32 nCheckpoints,
1066
uint256 oldVotes,
1067
uint256 newVotes
1068
)
1069
internal
1070
{
1071
uint32 blockNumber = safe32(block.number, "DPOLY::_writeCheckpoint: block number exceeds 32 bits");
1072
1073
if (nCheckpoints > 0 && checkpoints[delegatee][nCheckpoints - 1].fromBlock == blockNumber) {
1074
checkpoints[delegatee][nCheckpoints - 1].votes = newVotes;
1075
} else {
1076
checkpoints[delegatee][nCheckpoints] = Checkpoint(blockNumber, newVotes);
1077
numCheckpoints[delegatee] = nCheckpoints + 1;
1078
}
1079
1080
emit DelegateVotesChanged(delegatee, oldVotes, newVotes);
1081
}
1082
1083
function safe32(uint n, string memory errorMessage) internal pure returns (uint32) {
1084
require(n < 2**32, errorMessage);
1085
return uint32(n);
1086
}
1087
1088
function getChainId() internal pure returns (uint) {
1089
uint256 chainId;
1090
assembly { chainId := chainid() }
1091
return chainId;
1092
}
1093
}
Copied!
Last modified 4mo ago
Copy link