Working on

Giveaway Contract

BNBRewardNiubi contract (stake NIU get BNB)

Address: 0x...
Similar functionality as Pancakeswap’s Smartchef contract (0x92E8CeB7eAeD69fB6E4d9dA43F605D2610214E68). Stake NIU (Niubi Token) tokens, and get reward in native BNB (not WBNB). Hence few changes and additions are made.
depositBNBRewards is an external payable function. Anyone can send BNB to the contract and increase the contract’s BNB balance.
rewardBalance is a public view function which returns the BNB balance of the contract.
safeTransferBNB is an internal function used to transfer native BNB to an address. The external call to transfer is done with a gas limit of 23000.
The contract owner is able to withdraw an arbitrary amount of BNB, up to a maximum of the contract’s balance, from the contract using emergencyRewardWithdraw.
1
contracts/BNBRewardNiubi.sol
Copied!
1
/**
2
*Submitted for verification at BscScan.com on 2021-03-XX
3
*/
4
​
5
// File: @pancakeswap/pancake-swap-lib/contracts/math/SafeMath.sol
6
​
7
// SPDX-License-Identifier: MIT
8
​
9
pragma solidity >=0.4.0;
10
​
11
/**
12
* @dev Wrappers over Solidity's arithmetic operations with added overflow
13
* checks.
14
*
15
* Arithmetic operations in Solidity wrap on overflow. This can easily result
16
* in bugs, because programmers usually assume that an overflow raises an
17
* error, which is the standard behavior in high level programming languages.
18
* `SafeMath` restores this intuition by reverting the transaction when an
19
* operation overflows.
20
*
21
* Using this library instead of the unchecked operations eliminates an entire
22
* class of bugs, so it's recommended to use it always.
23
*/
24
library SafeMath {
25
/**
26
* @dev Returns the addition of two unsigned integers, reverting on
27
* overflow.
28
*
29
* Counterpart to Solidity's `+` operator.
30
*
31
* Requirements:
32
*
33
* - Addition cannot overflow.
34
*/
35
function add(uint256 a, uint256 b) internal pure returns (uint256) {
36
uint256 c = a + b;
37
require(c >= a, 'SafeMath: addition overflow');
38
​
39
return c;
40
}
41
​
42
/**
43
* @dev Returns the subtraction of two unsigned integers, reverting on
44
* overflow (when the result is negative).
45
*
46
* Counterpart to Solidity's `-` operator.
47
*
48
* Requirements:
49
*
50
* - Subtraction cannot overflow.
51
*/
52
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
53
return sub(a, b, 'SafeMath: subtraction overflow');
54
}
55
​
56
/**
57
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
58
* overflow (when the result is negative).
59
*
60
* Counterpart to Solidity's `-` operator.
61
*
62
* Requirements:
63
*
64
* - Subtraction cannot overflow.
65
*/
66
function sub(
67
uint256 a,
68
uint256 b,
69
string memory errorMessage
70
) internal pure returns (uint256) {
71
require(b <= a, errorMessage);
72
uint256 c = a - b;
73
​
74
return c;
75
}
76
​
77
/**
78
* @dev Returns the multiplication of two unsigned integers, reverting on
79
* overflow.
80
*
81
* Counterpart to Solidity's `*` operator.
82
*
83
* Requirements:
84
*
85
* - Multiplication cannot overflow.
86
*/
87
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
88
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
89
// benefit is lost if 'b' is also tested.
90
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
91
if (a == 0) {
92
return 0;
93
}
94
​
95
uint256 c = a * b;
96
require(c / a == b, 'SafeMath: multiplication overflow');
97
​
98
return c;
99
}
100
​
101
/**
102
* @dev Returns the integer division of two unsigned integers. Reverts on
103
* division by zero. The result is rounded towards zero.
104
*
105
* Counterpart to Solidity's `/` operator. Note: this function uses a
106
* `revert` opcode (which leaves remaining gas untouched) while Solidity
107
* uses an invalid opcode to revert (consuming all remaining gas).
108
*
109
* Requirements:
110
*
111
* - The divisor cannot be zero.
112
*/
113
function div(uint256 a, uint256 b) internal pure returns (uint256) {
114
return div(a, b, 'SafeMath: division by zero');
115
}
116
​
117
/**
118
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
119
* division by zero. The result is rounded towards zero.
120
*
121
* Counterpart to Solidity's `/` operator. Note: this function uses a
122
* `revert` opcode (which leaves remaining gas untouched) while Solidity
123
* uses an invalid opcode to revert (consuming all remaining gas).
124
*
125
* Requirements:
126
*
127
* - The divisor cannot be zero.
128
*/
129
function div(
130
uint256 a,
131
uint256 b,
132
string memory errorMessage
133
) internal pure returns (uint256) {
134
require(b > 0, errorMessage);
135
uint256 c = a / b;
136
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
137
​
138
return c;
139
}
140
​
141
/**
142
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
143
* Reverts when dividing by zero.
144
*
145
* Counterpart to Solidity's `%` operator. This function uses a `revert`
146
* opcode (which leaves remaining gas untouched) while Solidity uses an
147
* invalid opcode to revert (consuming all remaining gas).
148
*
149
* Requirements:
150
*
151
* - The divisor cannot be zero.
152
*/
153
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
154
return mod(a, b, 'SafeMath: modulo by zero');
155
}
156
​
157
/**
158
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
159
* Reverts with custom message when dividing by zero.
160
*
161
* Counterpart to Solidity's `%` operator. This function uses a `revert`
162
* opcode (which leaves remaining gas untouched) while Solidity uses an
163
* invalid opcode to revert (consuming all remaining gas).
164
*
165
* Requirements:
166
*
167
* - The divisor cannot be zero.
168
*/
169
function mod(
170
uint256 a,
171
uint256 b,
172
string memory errorMessage
173
) internal pure returns (uint256) {
174
require(b != 0, errorMessage);
175
return a % b;
176
}
177
​
178
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
179
z = x < y ? x : y;
180
}
181
​
182
// babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
183
function sqrt(uint256 y) internal pure returns (uint256 z) {
184
if (y > 3) {
185
z = y;
186
uint256 x = y / 2 + 1;
187
while (x < z) {
188
z = x;
189
x = (y / x + x) / 2;
190
}
191
} else if (y != 0) {
192
z = 1;
193
}
194
}
195
}
196
​
197
// File: @pancakeswap/pancake-swap-lib/contracts/token/BEP20/IBEP20.sol
198
​
199
// License-Identifier: GPL-3.0-or-later
200
​
201
pragma solidity >=0.4.0;
202
​
203
interface IBEP20 {
204
/**
205
* @dev Returns the amount of tokens in existence.
206
*/
207
function totalSupply() external view returns (uint256);
208
​
209
/**
210
* @dev Returns the token decimals.
211
*/
212
function decimals() external view returns (uint8);
213
​
214
/**
215
* @dev Returns the token symbol.
216
*/
217
function symbol() external view returns (string memory);
218
​
219
/**
220
* @dev Returns the token name.
221
*/
222
function name() external view returns (string memory);
223
​
224
/**
225
* @dev Returns the bep token owner.
226
*/
227
function getOwner() external view returns (address);
228
​
229
/**
230
* @dev Returns the amount of tokens owned by `account`.
231
*/
232
function balanceOf(address account) external view returns (uint256);
233
​
234
/**
235
* @dev Moves `amount` tokens from the caller's account to `recipient`.
236
*
237
* Returns a boolean value indicating whether the operation succeeded.
238
*
239
* Emits a {Transfer} event.
240
*/
241
function transfer(address recipient, uint256 amount) external returns (bool);
242
​
243
/**
244
* @dev Returns the remaining number of tokens that `spender` will be
245
* allowed to spend on behalf of `owner` through {transferFrom}. This is
246
* zero by default.
247
*
248
* This value changes when {approve} or {transferFrom} are called.
249
*/
250
function allowance(address _owner, address spender) external view returns (uint256);
251
​
252
/**
253
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
254
*
255
* Returns a boolean value indicating whether the operation succeeded.
256
*
257
* IMPORTANT: Beware that changing an allowance with this method brings the risk
258
* that someone may use both the old and the new allowance by unfortunate
259
* transaction ordering. One possible solution to mitigate this race
260
* condition is to first reduce the spender's allowance to 0 and set the
261
* desired value afterwards:
262
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
263
*
264
* Emits an {Approval} event.
265
*/
266
function approve(address spender, uint256 amount) external returns (bool);
267
​
268
/**
269
* @dev Moves `amount` tokens from `sender` to `recipient` using the
270
* allowance mechanism. `amount` is then deducted from the caller's
271
* allowance.
272
*
273
* Returns a boolean value indicating whether the operation succeeded.
274
*
275
* Emits a {Transfer} event.
276
*/
277
function transferFrom(
278
address sender,
279
address recipient,
280
uint256 amount
281
) external returns (bool);
282
​
283
/**
284
* @dev Emitted when `value` tokens are moved from one account (`from`) to
285
* another (`to`).
286
*
287
* Note that `value` may be zero.
288
*/
289
event Transfer(address indexed from, address indexed to, uint256 value);
290
​
291
/**
292
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
293
* a call to {approve}. `value` is the new allowance.
294
*/
295
event Approval(address indexed owner, address indexed spender, uint256 value);
296
}
297
​
298
// File: @pancakeswap/pancake-swap-lib/contracts/utils/Address.sol
299
​
300
// License-Identifier: MIT
301
​
302
pragma solidity ^0.6.2;
303
​
304
/**
305
* @dev Collection of functions related to the address type
306
*/
307
library Address {
308
/**
309
* @dev Returns true if `account` is a contract.
310
*
311
* [IMPORTANT]
312
* ====
313
* It is unsafe to assume that an address for which this function returns
314
* false is an externally-owned account (EOA) and not a contract.
315
*
316
* Among others, `isContract` will return false for the following
317
* types of addresses:
318
*
319
* - an externally-owned account
320
* - a contract in construction
321
* - an address where a contract will be created
322
* - an address where a contract lived, but was destroyed
323
* ====
324
*/
325
function isContract(address account) internal view returns (bool) {
326
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
327
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
328
// for accounts without code, i.e. `keccak256('')`
329
bytes32 codehash;
330
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
331
// solhint-disable-next-line no-inline-assembly
332
assembly {
333
codehash := extcodehash(account)
334
}
335
return (codehash != accountHash && codehash != 0x0);
336
}
337
​
338
/**
339
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
340
* `recipient`, forwarding all available gas and reverting on errors.
341
*
342
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
343
* of certain opcodes, possibly making contracts go over the 2300 gas limit
344
* imposed by `transfer`, making them unable to receive funds via
345
* `transfer`. {sendValue} removes this limitation.
346
*
347
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
348
*
349
* IMPORTANT: because control is transferred to `recipient`, care must be
350
* taken to not create reentrancy vulnerabilities. Consider using
351
* {ReentrancyGuard} or the
352
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
353
*/
354
function sendValue(address payable recipient, uint256 amount) internal {
355
require(address(this).balance >= amount, 'Address: insufficient balance');
356
​
357
// solhint-disable-next-line avoid-low-level-calls, avoid-call-value
358
(bool success, ) = recipient.call{value: amount}('');
359
require(success, 'Address: unable to send value, recipient may have reverted');
360
}
361
​
362
/**
363
* @dev Performs a Solidity function call using a low level `call`. A
364
* plain`call` is an unsafe replacement for a function call: use this
365
* function instead.
366
*
367
* If `target` reverts with a revert reason, it is bubbled up by this
368
* function (like regular Solidity function calls).
369
*
370
* Returns the raw returned data. To convert to the expected return value,
371
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
372
*
373
* Requirements:
374
*
375
* - `target` must be a contract.
376
* - calling `target` with `data` must not revert.
377
*
378
* _Available since v3.1._
379
*/
380
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
381
return functionCall(target, data, 'Address: low-level call failed');
382
}
383
​
384
/**
385
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
386
* `errorMessage` as a fallback revert reason when `target` reverts.
387
*
388
* _Available since v3.1._
389
*/
390
function functionCall(
391
address target,
392
bytes memory data,
393
string memory errorMessage
394
) internal returns (bytes memory) {
395
return _functionCallWithValue(target, data, 0, errorMessage);
396
}
397
​
398
/**
399
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
400
* but also transferring `value` wei to `target`.
401
*
402
* Requirements:
403
*
404
* - the calling contract must have an ETH balance of at least `value`.
405
* - the called Solidity function must be `payable`.
406
*
407
* _Available since v3.1._
408
*/
409
function functionCallWithValue(
410
address target,
411
bytes memory data,
412
uint256 value
413
) internal returns (bytes memory) {
414
return functionCallWithValue(target, data, value, 'Address: low-level call with value failed');
415
}
416
​
417
/**
418
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
419
* with `errorMessage` as a fallback revert reason when `target` reverts.
420
*
421
* _Available since v3.1._
422
*/
423
function functionCallWithValue(
424
address target,
425
bytes memory data,
426
uint256 value,
427
string memory errorMessage
428
) internal returns (bytes memory) {
429
require(address(this).balance >= value, 'Address: insufficient balance for call');
430
return _functionCallWithValue(target, data, value, errorMessage);
431
}
432
​
433
function _functionCallWithValue(
434
address target,
435
bytes memory data,
436
uint256 weiValue,
437
string memory errorMessage
438
) private returns (bytes memory) {
439
require(isContract(target), 'Address: call to non-contract');
440
​
441
// solhint-disable-next-line avoid-low-level-calls
442
(bool success, bytes memory returndata) = target.call{value: weiValue}(data);
443
if (success) {
444
return returndata;
445
} else {
446
// Look for revert reason and bubble it up if present
447
if (returndata.length > 0) {
448
// The easiest way to bubble the revert reason is using memory via assembly
449
​
450
// solhint-disable-next-line no-inline-assembly
451
assembly {
452
let returndata_size := mload(returndata)
453
revert(add(32, returndata), returndata_size)
454
}
455
} else {
456
revert(errorMessage);
457
}
458
}
459
}
460
}
461
​
462
// File: @pancakeswap/pancake-swap-lib/contracts/token/BEP20/SafeBEP20.sol
463
​
464
// License-Identifier: MIT
465
​
466
pragma solidity ^0.6.0;
467
​
468
​
469
​
470
​
471
/**
472
* @title SafeBEP20
473
* @dev Wrappers around BEP20 operations that throw on failure (when the token
474
* contract returns false). Tokens that return no value (and instead revert or
475
* throw on failure) are also supported, non-reverting calls are assumed to be
476
* successful.
477
* To use this library you can add a `using SafeBEP20 for IBEP20;` statement to your contract,
478
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
479
*/
480
library SafeBEP20 {
481
using SafeMath for uint256;
482
using Address for address;
483
​
484
function safeTransfer(
485
IBEP20 token,
486
address to,
487
uint256 value
488
) internal {
489
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
490
}
491
​
492
function safeTransferFrom(
493
IBEP20 token,
494
address from,
495
address to,
496
uint256 value
497
) internal {
498
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
499
}
500
​
501
/**
502
* @dev Deprecated. This function has issues similar to the ones found in
503
* {IBEP20-approve}, and its usage is discouraged.
504
*
505
* Whenever possible, use {safeIncreaseAllowance} and
506
* {safeDecreaseAllowance} instead.
507
*/
508
function safeApprove(
509
IBEP20 token,
510
address spender,
511
uint256 value
512
) internal {
513
// safeApprove should only be called when setting an initial allowance,
514
// or when resetting it to zero. To increase and decrease it, use
515
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
516
// solhint-disable-next-line max-line-length
517
require(
518
(value == 0) || (token.allowance(address(this), spender) == 0),
519
'SafeBEP20: approve from non-zero to non-zero allowance'
520
);
521
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
522
}
523
​
524
function safeIncreaseAllowance(
525
IBEP20 token,
526
address spender,
527
uint256 value
528
) internal {
529
uint256 newAllowance = token.allowance(address(this), spender).add(value);
530
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
531
}
532
​
533
function safeDecreaseAllowance(
534
IBEP20 token,
535
address spender,
536
uint256 value
537
) internal {
538
uint256 newAllowance = token.allowance(address(this), spender).sub(
539
value,
540
'SafeBEP20: decreased allowance below zero'
541
);
542
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
543
}
544
​
545
/**
546
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
547
* on the return value: the return value is optional (but if data is returned, it must not be false).
548
* @param token The token targeted by the call.
549
* @param data The call data (encoded using abi.encode or one of its variants).
550
*/
551
function _callOptionalReturn(IBEP20 token, bytes memory data) private {
552
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
553
// we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
554
// the target address contains contract code and also asserts for success in the low-level call.
555
​
556
bytes memory returndata = address(token).functionCall(data, 'SafeBEP20: low-level call failed');
557
if (returndata.length > 0) {
558
// Return data is optional
559
// solhint-disable-next-line max-line-length
560
require(abi.decode(returndata, (bool)), 'SafeBEP20: BEP20 operation did not succeed');
561
}
562
}
563
}
564
​
565
// File: @pancakeswap/pancake-swap-lib/contracts/GSN/Context.sol
566
​
567
// License-Identifier: GPL-3.0-or-later
568
​
569
pragma solidity >=0.4.0;
570
​
571
/*
572
* @dev Provides information about the current execution context, including the
573
* sender of the transaction and its data. While these are generally available
574
* via msg.sender and msg.data, they should not be accessed in such a direct
575
* manner, since when dealing with GSN meta-transactions the account sending and
576
* paying for execution may not be the actual sender (as far as an application
577
* is concerned).
578
*
579
* This contract is only required for intermediate, library-like contracts.
580
*/
581
contract Context {
582
// Empty internal constructor, to prevent people from mistakenly deploying
583
// an instance of this contract, which should be used via inheritance.
584
constructor() internal {}
585
​
586
function _msgSender() internal view returns (address payable) {
587
return msg.sender;
588
}
589
​
590
function _msgData() internal view returns (bytes memory) {
591
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
592
return msg.data;
593
}
594
}
595
​
596
// File: @pancakeswap/pancake-swap-lib/contracts/access/Ownable.sol
597
​
598
// License-Identifier: GPL-3.0-or-later
599
​
600
pragma solidity >=0.4.0;
601
​
602
​
603
/**
604
* @dev Contract module which provides a basic access control mechanism, where
605
* there is an account (an owner) that can be granted exclusive access to
606
* specific functions.
607
*
608
* By default, the owner account will be the one that deploys the contract. This
609
* can later be changed with {transferOwnership}.
610
*
611
* This module is used through inheritance. It will make available the modifier
612
* `onlyOwner`, which can be applied to your functions to restrict their use to
613
* the owner.
614
*/
615
contract Ownable is Context {
616
address private _owner;
617
​
618
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
619
​
620
/**
621
* @dev Initializes the contract setting the deployer as the initial owner.
622
*/
623
constructor() internal {
624
address msgSender = _msgSender();
625
_owner = msgSender;
626
emit OwnershipTransferred(address(0), msgSender);
627
}
628
​
629
/**
630
* @dev Returns the address of the current owner.
631
*/
632
function owner() public view returns (address) {
633
return _owner;
634
}
635
​
636
/**
637
* @dev Throws if called by any account other than the owner.
638
*/
639
modifier onlyOwner() {
640
require(_owner == _msgSender(), 'Ownable: caller is not the owner');
641
_;
642
}
643
​
644
/**
645
* @dev Leaves the contract without owner. It will not be possible to call
646
* `onlyOwner` functions anymore. Can only be called by the current owner.
647
*
648
* NOTE: Renouncing ownership will leave the contract without an owner,
649
* thereby removing any functionality that is only available to the owner.
650
*/
651
function renounceOwnership() public onlyOwner {
652
emit OwnershipTransferred(_owner, address(0));
653
_owner = address(0);
654
}
655
​
656
/**
657
* @dev Transfers ownership of the contract to a new account (`newOwner`).
658
* Can only be called by the current owner.
659
*/
660
function transferOwnership(address newOwner) public onlyOwner {
661
_transferOwnership(newOwner);
662
}
663
​
664
/**
665
* @dev Transfers ownership of the contract to a new account (`newOwner`).
666
*/
667
function _transferOwnership(address newOwner) internal {
668
require(newOwner != address(0), 'Ownable: new owner is the zero address');
669
emit OwnershipTransferred(_owner, newOwner);
670
_owner = newOwner;
671
}
672
}
673
​
674
// File: contracts/BNBRewardNiubi.sol
675
​
676
pragma solidity 0.6.12;
677
​
678
/*
679
* Niubiswap
680
* App: https://www.niubiswap.com
681
* Twitter: https://twitter.com/officialniubi
682
* Telegram: https://t.me/niubiswap
683
*/
684
​
685
​
686
​
687
​
688
​
689
​
690
contract BNBRewardNiubi is Ownable {
691
using SafeMath for uint256;
692
using SafeBEP20 for IBEP20;
693
​
694
// Info of each user.
695
struct UserInfo {
696
uint256 amount; // How many LP tokens the user has provided.
697
uint256 rewardDebt; // Reward debt. See explanation below.
698
}
699
​
700
// Info of each pool.
701
struct PoolInfo {
702
IBEP20 lpToken; // Address of LP token contract.
703
uint256 allocPoint; // How many allocation points assigned to this pool. Rewards to distribute per block.
704
uint256 lastRewardBlock; // Last block number that Rewards distribution occurs.
705
uint256 accRewardTokenPerShare; // Accumulated Rewards per share, times 1e12. See below.
706
}
707
​
708
// The stake TOKEN!
709
IBEP20 public stakeToken;
710
​
711
// Reward tokens created per block.
712
uint256 public rewardPerBlock;
713
​
714
​
715
// Info of each pool.
716
PoolInfo[] public poolInfo;
717
// Info of each user that stakes LP tokens.
718
mapping (address => UserInfo) public userInfo;
719
// Total allocation poitns. Must be the sum of all allocation points in all pools.
720
uint256 private totalAllocPoint = 0;
721
// The block number when Reward mining starts.
722
uint256 public startBlock;
723
// The block number when mining ends.
724
uint256 public bonusEndBlock;
725
​
726
event Deposit(address indexed user, uint256 amount);
727
event DepositBNBRewards(uint256 amount);
728
event Withdraw(address indexed user, uint256 amount);
729
event EmergencyWithdraw(address indexed user, uint256 amount);
730
event EmergencyRewardWithdraw(address indexed user, uint256 amount);
731
​
732
constructor(
733
IBEP20 _stakeToken,
734
uint256 _rewardPerBlock,
735
uint256 _startBlock,
736
uint256 _bonusEndBlock
737
) public {
738
stakeToken = _stakeToken;
739
rewardPerBlock = _rewardPerBlock;
740
startBlock = _startBlock;
741
bonusEndBlock = _bonusEndBlock;
742
​
743
// staking pool
744
poolInfo.push(PoolInfo({
745
lpToken: _stakeToken,
746
allocPoint: 1000,
747
lastRewardBlock: startBlock,
748
accRewardTokenPerShare: 0
749
}));
750
​
751
totalAllocPoint = 1000;
752
​
753
}
754
​
755
// Return reward multiplier over the given _from to _to block.
756
function getMultiplier(uint256 _from, uint256 _to) public view returns (uint256) {
757
if (_to <= bonusEndBlock) {
758
return _to.sub(_from);
759
} else if (_from >= bonusEndBlock) {
760
return 0;
761
} else {
762
return bonusEndBlock.sub(_from);
763
}
764
}
765
​
766
// View function to see pending Reward on frontend.
767
function pendingReward(address _user) external view returns (uint256) {
768
PoolInfo storage pool = poolInfo[0];
769
UserInfo storage user = userInfo[_user];
770
uint256 accRewardTokenPerShare = pool.accRewardTokenPerShare;
771
uint256 lpSupply = pool.lpToken.balanceOf(address(this));
772
if (block.number > pool.lastRewardBlock && lpSupply != 0) {
773
uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
774
uint256 tokenReward = multiplier.mul(rewardPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
775
accRewardTokenPerShare = accRewardTokenPerShare.add(tokenReward.mul(1e12).div(lpSupply));
776
}
777
return user.amount.mul(accRewardTokenPerShare).div(1e12).sub(user.rewardDebt);
778
}
779
​
780
// Update reward variables of the given pool to be up-to-date.
781
function updatePool(uint256 _pid) public {
782
PoolInfo storage pool = poolInfo[_pid];
783
if (block.number <= pool.lastRewardBlock) {
784
return;
785
}
786
uint256 lpSupply = pool.lpToken.balanceOf(address(this));
787
if (lpSupply == 0) {
788
pool.lastRewardBlock = block.number;
789
return;
790
}
791
uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
792
uint256 tokenReward = multiplier.mul(rewardPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
793
pool.accRewardTokenPerShare = pool.accRewardTokenPerShare.add(tokenReward.mul(1e12).div(lpSupply));
794
pool.lastRewardBlock = block.number;
795
}
796
​
797
// Update reward variables for all pools. Be careful of gas spending!
798
function massUpdatePools() public {
799
uint256 length = poolInfo.length;
800
for (uint256 pid = 0; pid < length; ++pid) {
801
updatePool(pid);
802
}
803
}
804
​
805
​
806
/// Deposit staking token into the contract to earn rewards.
807
/// @dev Since this contract needs to be supplied with rewards we are
808
/// sending the balance of the contract if the pending rewards are higher
809
/// @param _amount The amount of staking tokens to deposit
810
function deposit(uint256 _amount) public {
811
PoolInfo storage pool = poolInfo[0];
812
UserInfo storage user = userInfo[msg.sender];
813
updatePool(0);
814
if (user.amount > 0) {
815
uint256 pending = user.amount.mul(pool.accRewardTokenPerShare).div(1e12).sub(user.rewardDebt);
816
if(pending > 0) {
817
uint256 currentRewardBalance = rewardBalance();
818
if(currentRewardBalance > 0) {
819
if(pending > currentRewardBalance) {
820
safeTransferBNB(address(msg.sender), currentRewardBalance);
821
} else {
822
safeTransferBNB(address(msg.sender), pending);
823
}
824
}
825
}
826
}
827
if(_amount > 0) {
828
pool.lpToken.safeTransferFrom(address(msg.sender), address(this), _amount);
829
user.amount = user.amount.add(_amount);
830
}
831
user.rewardDebt = user.amount.mul(pool.accRewardTokenPerShare).div(1e12);
832
​
833
emit Deposit(msg.sender, _amount);
834
}
835
​
836
/// Withdraw rewards and/or staked tokens. Pass a 0 amount to withdraw only rewards
837
/// @param _amount The amount of staking tokens to withdraw
838
function withdraw(uint256 _amount) public {
839
PoolInfo storage pool = poolInfo[0];
840
UserInfo storage user = userInfo[msg.sender];
841
require(user.amount >= _amount, "withdraw: not good");
842
updatePool(0);
843
uint256 pending = user.amount.mul(pool.accRewardTokenPerShare).div(1e12).sub(user.rewardDebt);
844
if(pending > 0) {
845
uint256 currentRewardBalance = rewardBalance();
846
if(currentRewardBalance > 0) {
847
if(pending > currentRewardBalance) {
848
safeTransferBNB(address(msg.sender), currentRewardBalance);
849
} else {
850
safeTransferBNB(address(msg.sender), pending);
851
}
852
}
853
}
854
if(_amount > 0) {
855
user.amount = user.amount.sub(_amount);
856
pool.lpToken.safeTransfer(address(msg.sender), _amount);
857
}
858
​
859
user.rewardDebt = user.amount.mul(pool.accRewardTokenPerShare).div(1e12);
860
​
861
emit Withdraw(msg.sender, _amount);
862
}
863
​
864
/// Obtain the reward balance of this contract
865
/// @return wei balance of contract
866
function rewardBalance() public view returns (uint256) {
867
return payable(address(this)).balance;
868
}
869
​
870
// Deposit Rewards into contract
871
function depositBNBRewards() external payable {
872
require(msg.value > 0, 'Message has no BNB value to deposit into contract.');
873
emit DepositBNBRewards(msg.value);
874
}
875
​
876
/// @param to address to send BNB to
877
/// @param value wei value of BNB to transfer
878
function safeTransferBNB(address to, uint256 value) internal {
879
// Transfer BNB to address
880
(bool success, ) = to.call{gas: 23000, value: value}("");
881
require(success, 'TransferHelper: BNB_TRANSFER_FAILED');
882
}
883
​
884
/* Emergency Functions */
885
​
886
// Withdraw without caring about rewards. EMERGENCY ONLY.
887
function emergencyWithdraw() external {
888
PoolInfo storage pool = poolInfo[0];
889
UserInfo storage user = userInfo[msg.sender];
890
pool.lpToken.safeTransfer(address(msg.sender), user.amount);
891
user.amount = 0;
892
user.rewardDebt = 0;
893
emit EmergencyWithdraw(msg.sender, user.amount);
894
}
895
​
896
// Withdraw reward. EMERGENCY ONLY.
897
function emergencyRewardWithdraw(uint256 _amount) external onlyOwner {
898
require(_amount <= rewardBalance(), 'not enough rewards');
899
// Withdraw the BNB rewards
900
safeTransferBNB(address(msg.sender), _amount);
901
emit EmergencyRewardWithdraw(msg.sender, _amount);
902
}
903
​
904
}
Copied!
Contract ABI
1
[{"inputs":[{"internalType":"contract IBEP20","name":"_stakeToken","type":"address"},{"internalType":"uint256","name":"_rewardPerBlock","type":"uint256"},{"internalType":"uint256","name":"_startBlock","type":"uint256"},{"internalType":"uint256","name":"_bonusEndBlock","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"DepositBNBRewards","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"EmergencyRewardWithdraw","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"EmergencyWithdraw","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Withdraw","type":"event"},{"inputs":[],"name":"bonusEndBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"deposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"depositBNBRewards","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"emergencyRewardWithdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"emergencyWithdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_from","type":"uint256"},{"internalType":"uint256","name":"_to","type":"uint256"}],"name":"getMultiplier","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"massUpdatePools","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"pendingReward","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"poolInfo","outputs":[{"internalType":"contract IBEP20","name":"lpToken","type":"address"},{"internalType":"uint256","name":"allocPoint","type":"uint256"},{"internalType":"uint256","name":"lastRewardBlock","type":"uint256"},{"internalType":"uint256","name":"accRewardTokenPerShare","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"rewardBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rewardPerBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"stakeToken","outputs":[{"internalType":"contract IBEP20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"startBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"}],"name":"updatePool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"userInfo","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"rewardDebt","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]
Copied!

Project status:

Pending verification by Solidity dev (Light audit) (avg 3-7days)

Call for help:

Anyone qualified to help the tech team achieve this feature faster will get a 4000 NIU bounty. (claim to admin in Telegram or Discord)
Last modified 6mo ago