eth以太坊sushi合约源码开发

- 日理万妓 2022-12-06 12:40 296阅读 0赞

以太坊或波场sushiy合约源码开发

1:官网:https://sushiswap.org/

2:部分合约源码浏览

  1. pragma solidity 0.6.12;
  2. import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
  3. import "@openzeppelin/contracts/access/Ownable.sol";
  4. // SushiToken with Governance.
  5. contract SushiToken is ERC20("SushiToken", "SUSHI"), Ownable {
  6. /// @notice Creates `_amount` token to `_to`. Must only be called by the owner (MasterChef).
  7. function mint(address _to, uint256 _amount) public onlyOwner {
  8. _mint(_to, _amount);
  9. _moveDelegates(address(0), _delegates[_to], _amount);
  10. }
  11. // Copied and modified from YAM code:
  12. // https://github.com/yam-finance/yam-protocol/blob/master/contracts/token/YAMGovernanceStorage.sol
  13. // https://github.com/yam-finance/yam-protocol/blob/master/contracts/token/YAMGovernance.sol
  14. // Which is copied and modified from COMPOUND:
  15. // https://github.com/compound-finance/compound-protocol/blob/master/contracts/Governance/Comp.sol
  16. /// @notice A record of each accounts delegate
  17. mapping (address => address) internal _delegates;
  18. /// @notice A checkpoint for marking number of votes from a given block
  19. struct Checkpoint {
  20. uint32 fromBlock;
  21. uint256 votes;
  22. }
  23. /// @notice A record of votes checkpoints for each account, by index
  24. mapping (address => mapping (uint32 => Checkpoint)) public checkpoints;
  25. /// @notice The number of checkpoints for each account
  26. mapping (address => uint32) public numCheckpoints;
  27. /// @notice The EIP-712 typehash for the contract's domain
  28. bytes32 public constant DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)");
  29. /// @notice The EIP-712 typehash for the delegation struct used by the contract
  30. bytes32 public constant DELEGATION_TYPEHASH = keccak256("Delegation(address delegatee,uint256 nonce,uint256 expiry)");
  31. /// @notice A record of states for signing / validating signatures
  32. mapping (address => uint) public nonces;
  33. /// @notice An event thats emitted when an account changes its delegate
  34. event DelegateChanged(address indexed delegator, address indexed fromDelegate, address indexed toDelegate);
  35. /// @notice An event thats emitted when a delegate account's vote balance changes
  36. event DelegateVotesChanged(address indexed delegate, uint previousBalance, uint newBalance);
  37. /**
  38. * @notice Delegate votes from `msg.sender` to `delegatee`
  39. * @param delegator The address to get delegatee for
  40. */
  41. function delegates(address delegator)
  42. external
  43. view
  44. returns (address)
  45. {
  46. return _delegates[delegator];
  47. }
  48. /**
  49. * @notice Delegate votes from `msg.sender` to `delegatee`
  50. * @param delegatee The address to delegate votes to
  51. */
  52. function delegate(address delegatee) external {
  53. return _delegate(msg.sender, delegatee);
  54. }
  55. /**
  56. * @notice Delegates votes from signatory to `delegatee`
  57. * @param delegatee The address to delegate votes to
  58. * @param nonce The contract state required to match the signature
  59. * @param expiry The time at which to expire the signature
  60. * @param v The recovery byte of the signature
  61. * @param r Half of the ECDSA signature pair
  62. * @param s Half of the ECDSA signature pair
  63. */
  64. function delegateBySig(
  65. address delegatee,
  66. uint nonce,
  67. uint expiry,
  68. uint8 v,
  69. bytes32 r,
  70. bytes32 s
  71. )
  72. external
  73. {
  74. bytes32 domainSeparator = keccak256(
  75. abi.encode(
  76. DOMAIN_TYPEHASH,
  77. keccak256(bytes(name())),
  78. getChainId(),
  79. address(this)
  80. )
  81. );
  82. bytes32 structHash = keccak256(
  83. abi.encode(
  84. DELEGATION_TYPEHASH,
  85. delegatee,
  86. nonce,
  87. expiry
  88. )
  89. );
  90. bytes32 digest = keccak256(
  91. abi.encodePacked(
  92. "\x19\x01",
  93. domainSeparator,
  94. structHash
  95. )
  96. );
  97. address signatory = ecrecover(digest, v, r, s);
  98. require(signatory != address(0), "SUSHI::delegateBySig: invalid signature");
  99. require(nonce == nonces[signatory]++, "SUSHI::delegateBySig: invalid nonce");
  100. require(now <= expiry, "SUSHI::delegateBySig: signature expired");
  101. return _delegate(signatory, delegatee);
  102. }
  103. /**
  104. * @notice Gets the current votes balance for `account`
  105. * @param account The address to get votes balance
  106. * @return The number of current votes for `account`
  107. */
  108. function getCurrentVotes(address account)
  109. external
  110. view
  111. returns (uint256)
  112. {
  113. uint32 nCheckpoints = numCheckpoints[account];
  114. return nCheckpoints > 0 ? checkpoints[account][nCheckpoints - 1].votes : 0;
  115. }
  116. /**
  117. * @notice Determine the prior number of votes for an account as of a block number
  118. * @dev Block number must be a finalized block or else this function will revert to prevent misinformation.
  119. * @param account The address of the account to check
  120. * @param blockNumber The block number to get the vote balance at
  121. * @return The number of votes the account had as of the given block
  122. */
  123. function getPriorVotes(address account, uint blockNumber)
  124. external
  125. view
  126. returns (uint256)
  127. {
  128. require(blockNumber < block.number, "SUSHI::getPriorVotes: not yet determined");
  129. uint32 nCheckpoints = numCheckpoints[account];
  130. if (nCheckpoints == 0) {
  131. return 0;
  132. }
  133. // First check most recent balance
  134. if (checkpoints[account][nCheckpoints - 1].fromBlock <= blockNumber) {
  135. return checkpoints[account][nCheckpoints - 1].votes;
  136. }
  137. // Next check implicit zero balance
  138. if (checkpoints[account][0].fromBlock > blockNumber) {
  139. return 0;
  140. }
  141. uint32 lower = 0;
  142. uint32 upper = nCheckpoints - 1;
  143. while (upper > lower) {
  144. uint32 center = upper - (upper - lower) / 2; // ceil, avoiding overflow
  145. Checkpoint memory cp = checkpoints[account][center];
  146. if (cp.fromBlock == blockNumber) {
  147. return cp.votes;
  148. } else if (cp.fromBlock < blockNumber) {
  149. lower = center;
  150. } else {
  151. upper = center - 1;
  152. }
  153. }
  154. return checkpoints[account][lower].votes;
  155. }
  156. function _delegate(address delegator, address delegatee)
  157. internal
  158. {
  159. address currentDelegate = _delegates[delegator];
  160. uint256 delegatorBalance = balanceOf(delegator); // balance of underlying SUSHIs (not scaled);
  161. _delegates[delegator] = delegatee;
  162. emit DelegateChanged(delegator, currentDelegate, delegatee);
  163. _moveDelegates(currentDelegate, delegatee, delegatorBalance);
  164. }
  165. function _moveDelegates(address srcRep, address dstRep, uint256 amount) internal {
  166. if (srcRep != dstRep && amount > 0) {
  167. if (srcRep != address(0)) {
  168. // decrease old representative
  169. uint32 srcRepNum = numCheckpoints[srcRep];
  170. uint256 srcRepOld = srcRepNum > 0 ? checkpoints[srcRep][srcRepNum - 1].votes : 0;
  171. uint256 srcRepNew = srcRepOld.sub(amount);
  172. _writeCheckpoint(srcRep, srcRepNum, srcRepOld, srcRepNew);
  173. }
  174. if (dstRep != address(0)) {
  175. // increase new representative
  176. uint32 dstRepNum = numCheckpoints[dstRep];
  177. uint256 dstRepOld = dstRepNum > 0 ? checkpoints[dstRep][dstRepNum - 1].votes : 0;
  178. uint256 dstRepNew = dstRepOld.add(amount);
  179. _writeCheckpoint(dstRep, dstRepNum, dstRepOld, dstRepNew);
  180. }
  181. }
  182. }
  183. function _writeCheckpoint(
  184. address delegatee,
  185. uint32 nCheckpoints,
  186. uint256 oldVotes,
  187. uint256 newVotes
  188. )
  189. internal
  190. {
  191. uint32 blockNumber = safe32(block.number, "SUSHI::_writeCheckpoint: block number exceeds 32 bits");
  192. if (nCheckpoints > 0 && checkpoints[delegatee][nCheckpoints - 1].fromBlock == blockNumber) {
  193. checkpoints[delegatee][nCheckpoints - 1].votes = newVotes;
  194. } else {
  195. checkpoints[delegatee][nCheckpoints] = Checkpoint(blockNumber, newVotes);
  196. numCheckpoints[delegatee] = nCheckpoints + 1;
  197. }
  198. emit DelegateVotesChanged(delegatee, oldVotes, newVotes);
  199. }
  200. function safe32(uint n, string memory errorMessage) internal pure returns (uint32) {
  201. require(n < 2**32, errorMessage);
  202. return uint32(n);
  203. }
  204. function getChainId() internal pure returns (uint) {
  205. uint256 chainId;
  206. assembly { chainId := chainid() }
  207. return chainId;
  208. }
  209. }

3:完整sushi源码下载地址:https://github.com/sushiswap/sushiswap/tree/master/contracts

发表评论

表情:
评论列表 (有 0 条评论,296人围观)

还没有评论,来说两句吧...

相关阅读

    相关 智能合约 简介

    以太坊是一个分布式的计算平台。它会生成一个名为Ether的加密货币。程序员可以在以太坊区块链上写下“智能合约”,这些以太坊智能合约会根据代码自动执行。 以太坊是什么?