Advertisement
VNDL_Kapper

KarmaV1

May 5th, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.72 KB | None | 0 0
  1. // Copyright [2021] - [2021], [Shaun Reed] and [Karma] contributors
  2. // SPDX-License-Identifier: MIT
  3.  
  4. pragma solidity >= 0.8.0;
  5.  
  6. // ----------------------------------------------------------------------------
  7. // Import ERC Token Standard #20 Interface
  8. // ETH EIP repo: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
  9. // ----------------------------------------------------------------------------
  10. pragma solidity ^0.8.0;
  11.  
  12. /**
  13. * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
  14. * behind a proxy. Since a proxied contract can't have a constructor, it's common to move constructor logic to an
  15. * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
  16. * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
  17. *
  18. * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
  19. * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
  20. *
  21. * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
  22. * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
  23. */
  24. abstract contract Initializable {
  25.  
  26. /**
  27. * @dev Indicates that the contract has been initialized.
  28. */
  29. bool private _initialized;
  30.  
  31. /**
  32. * @dev Indicates that the contract is in the process of being initialized.
  33. */
  34. bool private _initializing;
  35.  
  36. /**
  37. * @dev Modifier to protect an initializer function from being invoked twice.
  38. */
  39. modifier initializer() {
  40. require(_initializing || !_initialized, "Initializable: contract is already initialized");
  41.  
  42. bool isTopLevelCall = !_initializing;
  43. if (isTopLevelCall) {
  44. _initializing = true;
  45. _initialized = true;
  46. }
  47.  
  48. _;
  49.  
  50. if (isTopLevelCall) {
  51. _initializing = false;
  52. }
  53. }
  54. }
  55.  
  56.  
  57. /**
  58. * @dev Interface of the ERC20 standard as defined in the EIP.
  59. */
  60. interface IERC20Upgradeable {
  61. /**
  62. * @dev Returns the amount of tokens in existence.
  63. */
  64. function totalSupply() external view returns (uint256);
  65.  
  66. /**
  67. * @dev Returns the amount of tokens owned by `account`.
  68. */
  69. function balanceOf(address account) external view returns (uint256);
  70.  
  71. /**
  72. * @dev Moves `amount` tokens from the caller's account to `recipient`.
  73. *
  74. * Returns a boolean value indicating whether the operation succeeded.
  75. *
  76. * Emits a {Transfer} event.
  77. */
  78. function transfer(address recipient, uint256 amount) external returns (bool);
  79.  
  80. /**
  81. * @dev Returns the remaining number of tokens that `spender` will be
  82. * allowed to spend on behalf of `owner` through {transferFrom}. This is
  83. * zero by default.
  84. *
  85. * This value changes when {approve} or {transferFrom} are called.
  86. */
  87. function allowance(address owner, address spender) external view returns (uint256);
  88.  
  89. /**
  90. * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
  91. *
  92. * Returns a boolean value indicating whether the operation succeeded.
  93. *
  94. * IMPORTANT: Beware that changing an allowance with this method brings the risk
  95. * that someone may use both the old and the new allowance by unfortunate
  96. * transaction ordering. One possible solution to mitigate this race
  97. * condition is to first reduce the spender's allowance to 0 and set the
  98. * desired value afterwards:
  99. * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  100. *
  101. * Emits an {Approval} event.
  102. */
  103. function approve(address spender, uint256 amount) external returns (bool);
  104.  
  105. /**
  106. * @dev Moves `amount` tokens from `sender` to `recipient` using the
  107. * allowance mechanism. `amount` is then deducted from the caller's
  108. * allowance.
  109. *
  110. * Returns a boolean value indicating whether the operation succeeded.
  111. *
  112. * Emits a {Transfer} event.
  113. */
  114. function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
  115.  
  116. /**
  117. * @dev Emitted when `value` tokens are moved from one account (`from`) to
  118. * another (`to`).
  119. *
  120. * Note that `value` may be zero.
  121. */
  122. event Transfer(address indexed from, address indexed to, uint256 value);
  123.  
  124. /**
  125. * @dev Emitted when the allowance of a `spender` for an `owner` is set by
  126. * a call to {approve}. `value` is the new allowance.
  127. */
  128. event Approval(address indexed owner, address indexed spender, uint256 value);
  129. }
  130.  
  131. /**
  132. * @dev Interface for the optional metadata functions from the ERC20 standard.
  133. *
  134. * _Available since v4.1._
  135. */
  136. interface IERC20MetadataUpgradeable is IERC20Upgradeable {
  137. /**
  138. * @dev Returns the name of the token.
  139. */
  140. function name() external view returns (string memory);
  141.  
  142. /**
  143. * @dev Returns the symbol of the token.
  144. */
  145. function symbol() external view returns (string memory);
  146.  
  147. /**
  148. * @dev Returns the decimals places of the token.
  149. */
  150. function decimals() external view returns (uint8);
  151. }
  152.  
  153. pragma solidity ^0.8.0;
  154. /*
  155. * @dev Provides information about the current execution context, including the
  156. * sender of the transaction and its data. While these are generally available
  157. * via msg.sender and msg.data, they should not be accessed in such a direct
  158. * manner, since when dealing with meta-transactions the account sending and
  159. * paying for execution may not be the actual sender (as far as an application
  160. * is concerned).
  161. *
  162. * This contract is only required for intermediate, library-like contracts.
  163. */
  164. abstract contract ContextUpgradeable is Initializable {
  165. function __Context_init() internal initializer {
  166. __Context_init_unchained();
  167. }
  168.  
  169. function __Context_init_unchained() internal initializer {
  170. }
  171. function _msgSender() internal view virtual returns (address) {
  172. return msg.sender;
  173. }
  174.  
  175. function _msgData() internal view virtual returns (bytes calldata) {
  176. this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
  177. return msg.data;
  178. }
  179. uint256[50] private __gap;
  180. }
  181.  
  182. pragma solidity ^0.8.0;
  183. /**
  184. * @dev Implementation of the {IERC20} interface.
  185. *
  186. * This implementation is agnostic to the way tokens are created. This means
  187. * that a supply mechanism has to be added in a derived contract using {_mint}.
  188. * For a generic mechanism see {ERC20PresetMinterPauser}.
  189. *
  190. * TIP: For a detailed writeup see our guide
  191. * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
  192. * to implement supply mechanisms].
  193. *
  194. * We have followed general OpenZeppelin guidelines: functions revert instead
  195. * of returning `false` on failure. This behavior is nonetheless conventional
  196. * and does not conflict with the expectations of ERC20 applications.
  197. *
  198. * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
  199. * This allows applications to reconstruct the allowance for all accounts just
  200. * by listening to said events. Other implementations of the EIP may not emit
  201. * these events, as it isn't required by the specification.
  202. *
  203. * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
  204. * functions have been added to mitigate the well-known issues around setting
  205. * allowances. See {IERC20-approve}.
  206. */
  207. contract ERC20Upgradeable is Initializable, ContextUpgradeable, IERC20Upgradeable, IERC20MetadataUpgradeable {
  208. mapping (address => uint256) private _balances;
  209.  
  210. mapping (address => mapping (address => uint256)) private _allowances;
  211.  
  212. uint256 private _totalSupply;
  213.  
  214. string private _name;
  215. string private _symbol;
  216.  
  217. /**
  218. * @dev Sets the values for {name} and {symbol}.
  219. *
  220. * The defaut value of {decimals} is 18. To select a different value for
  221. * {decimals} you should overload it.
  222. *
  223. * All two of these values are immutable: they can only be set once during
  224. * construction.
  225. */
  226. function __ERC20_init(string memory name_, string memory symbol_) internal initializer {
  227. __Context_init_unchained();
  228. __ERC20_init_unchained(name_, symbol_);
  229. }
  230.  
  231. function __ERC20_init_unchained(string memory name_, string memory symbol_) internal initializer {
  232. _name = name_;
  233. _symbol = symbol_;
  234. }
  235.  
  236. /**
  237. * @dev Returns the name of the token.
  238. */
  239. function name() public view virtual override returns (string memory) {
  240. return _name;
  241. }
  242.  
  243. /**
  244. * @dev Returns the symbol of the token, usually a shorter version of the
  245. * name.
  246. */
  247. function symbol() public view virtual override returns (string memory) {
  248. return _symbol;
  249. }
  250.  
  251. /**
  252. * @dev Returns the number of decimals used to get its user representation.
  253. * For example, if `decimals` equals `2`, a balance of `505` tokens should
  254. * be displayed to a user as `5,05` (`505 / 10 ** 2`).
  255. *
  256. * Tokens usually opt for a value of 18, imitating the relationship between
  257. * Ether and Wei. This is the value {ERC20} uses, unless this function is
  258. * overridden;
  259. *
  260. * NOTE: This information is only used for _display_ purposes: it in
  261. * no way affects any of the arithmetic of the contract, including
  262. * {IERC20-balanceOf} and {IERC20-transfer}.
  263. */
  264. function decimals() public view virtual override returns (uint8) {
  265. return 18;
  266. }
  267.  
  268. /**
  269. * @dev See {IERC20-totalSupply}.
  270. */
  271. function totalSupply() public view virtual override returns (uint256) {
  272. return _totalSupply;
  273. }
  274.  
  275. /**
  276. * @dev See {IERC20-balanceOf}.
  277. */
  278. function balanceOf(address account) public view virtual override returns (uint256) {
  279. return _balances[account];
  280. }
  281.  
  282. /**
  283. * @dev See {IERC20-transfer}.
  284. *
  285. * Requirements:
  286. *
  287. * - `recipient` cannot be the zero address.
  288. * - the caller must have a balance of at least `amount`.
  289. */
  290. function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
  291. _transfer(_msgSender(), recipient, amount);
  292. return true;
  293. }
  294.  
  295. /**
  296. * @dev See {IERC20-allowance}.
  297. */
  298. function allowance(address owner, address spender) public view virtual override returns (uint256) {
  299. return _allowances[owner][spender];
  300. }
  301.  
  302. /**
  303. * @dev See {IERC20-approve}.
  304. *
  305. * Requirements:
  306. *
  307. * - `spender` cannot be the zero address.
  308. */
  309. function approve(address spender, uint256 amount) public virtual override returns (bool) {
  310. _approve(_msgSender(), spender, amount);
  311. return true;
  312. }
  313.  
  314. /**
  315. * @dev See {IERC20-transferFrom}.
  316. *
  317. * Emits an {Approval} event indicating the updated allowance. This is not
  318. * required by the EIP. See the note at the beginning of {ERC20}.
  319. *
  320. * Requirements:
  321. *
  322. * - `sender` and `recipient` cannot be the zero address.
  323. * - `sender` must have a balance of at least `amount`.
  324. * - the caller must have allowance for ``sender``'s tokens of at least
  325. * `amount`.
  326. */
  327. function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
  328. _transfer(sender, recipient, amount);
  329.  
  330. uint256 currentAllowance = _allowances[sender][_msgSender()];
  331. require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
  332. _approve(sender, _msgSender(), currentAllowance - amount);
  333.  
  334. return true;
  335. }
  336.  
  337. /**
  338. * @dev Atomically increases the allowance granted to `spender` by the caller.
  339. *
  340. * This is an alternative to {approve} that can be used as a mitigation for
  341. * problems described in {IERC20-approve}.
  342. *
  343. * Emits an {Approval} event indicating the updated allowance.
  344. *
  345. * Requirements:
  346. *
  347. * - `spender` cannot be the zero address.
  348. */
  349. function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
  350. _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
  351. return true;
  352. }
  353.  
  354. /**
  355. * @dev Atomically decreases the allowance granted to `spender` by the caller.
  356. *
  357. * This is an alternative to {approve} that can be used as a mitigation for
  358. * problems described in {IERC20-approve}.
  359. *
  360. * Emits an {Approval} event indicating the updated allowance.
  361. *
  362. * Requirements:
  363. *
  364. * - `spender` cannot be the zero address.
  365. * - `spender` must have allowance for the caller of at least
  366. * `subtractedValue`.
  367. */
  368. function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
  369. uint256 currentAllowance = _allowances[_msgSender()][spender];
  370. require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
  371. _approve(_msgSender(), spender, currentAllowance - subtractedValue);
  372.  
  373. return true;
  374. }
  375.  
  376. /**
  377. * @dev Moves tokens `amount` from `sender` to `recipient`.
  378. *
  379. * This is internal function is equivalent to {transfer}, and can be used to
  380. * e.g. implement automatic token fees, slashing mechanisms, etc.
  381. *
  382. * Emits a {Transfer} event.
  383. *
  384. * Requirements:
  385. *
  386. * - `sender` cannot be the zero address.
  387. * - `recipient` cannot be the zero address.
  388. * - `sender` must have a balance of at least `amount`.
  389. */
  390. function _transfer(address sender, address recipient, uint256 amount) internal virtual {
  391. require(sender != address(0), "ERC20: transfer from the zero address");
  392. require(recipient != address(0), "ERC20: transfer to the zero address");
  393.  
  394. _beforeTokenTransfer(sender, recipient, amount);
  395.  
  396. uint256 senderBalance = _balances[sender];
  397. require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
  398. _balances[sender] = senderBalance - amount;
  399. _balances[recipient] += amount;
  400.  
  401. emit Transfer(sender, recipient, amount);
  402. }
  403.  
  404. /** @dev Creates `amount` tokens and assigns them to `account`, increasing
  405. * the total supply.
  406. *
  407. * Emits a {Transfer} event with `from` set to the zero address.
  408. *
  409. * Requirements:
  410. *
  411. * - `to` cannot be the zero address.
  412. */
  413. function _mint(address account, uint256 amount) internal virtual {
  414. require(account != address(0), "ERC20: mint to the zero address");
  415.  
  416. _beforeTokenTransfer(address(0), account, amount);
  417.  
  418. _totalSupply += amount;
  419. _balances[account] += amount;
  420. emit Transfer(address(0), account, amount);
  421. }
  422.  
  423. /**
  424. * @dev Destroys `amount` tokens from `account`, reducing the
  425. * total supply.
  426. *
  427. * Emits a {Transfer} event with `to` set to the zero address.
  428. *
  429. * Requirements:
  430. *
  431. * - `account` cannot be the zero address.
  432. * - `account` must have at least `amount` tokens.
  433. */
  434. function _burn(address account, uint256 amount) internal virtual {
  435. require(account != address(0), "ERC20: burn from the zero address");
  436.  
  437. _beforeTokenTransfer(account, address(0), amount);
  438.  
  439. uint256 accountBalance = _balances[account];
  440. require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
  441. _balances[account] = accountBalance - amount;
  442. _totalSupply -= amount;
  443.  
  444. emit Transfer(account, address(0), amount);
  445. }
  446.  
  447. /**
  448. * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
  449. *
  450. * This internal function is equivalent to `approve`, and can be used to
  451. * e.g. set automatic allowances for certain subsystems, etc.
  452. *
  453. * Emits an {Approval} event.
  454. *
  455. * Requirements:
  456. *
  457. * - `owner` cannot be the zero address.
  458. * - `spender` cannot be the zero address.
  459. */
  460. function _approve(address owner, address spender, uint256 amount) internal virtual {
  461. require(owner != address(0), "ERC20: approve from the zero address");
  462. require(spender != address(0), "ERC20: approve to the zero address");
  463.  
  464. _allowances[owner][spender] = amount;
  465. emit Approval(owner, spender, amount);
  466. }
  467.  
  468. /**
  469. * @dev Hook that is called before any transfer of tokens. This includes
  470. * minting and burning.
  471. *
  472. * Calling conditions:
  473. *
  474. * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
  475. * will be to transferred to `to`.
  476. * - when `from` is zero, `amount` tokens will be minted for `to`.
  477. * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
  478. * - `from` and `to` are never both zero.
  479. *
  480. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  481. */
  482. function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
  483. uint256[45] private __gap;
  484. }
  485.  
  486.  
  487. // ----------------------------------------------------------------------------
  488. // Karma Contract
  489. // ----------------------------------------------------------------------------
  490.  
  491. contract Karma is Initializable, ERC20Upgradeable
  492. {
  493. function initialize(string memory name, string memory symbol, uint256 initialSupply) public virtual initializer {
  494. __ERC20_init(name, symbol);
  495. _mint(_msgSender(), initialSupply);
  496. }
  497. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement