From bb617056a196fa0a1fd945648251979a8b0e4fd8 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 21 Nov 2022 13:02:08 +0800 Subject: [PATCH] ERC1155-UPGRADEABLE --- .gitignore | 6 +- src/DEMO/Solidity.sol | 115 ++++++++++++++++++ src/ERC20/MyERC20.sol | 12 +- .../UpgradeableToken/UpgradeableERC1155.sol | 39 ++++++ src/小记.md | 13 ++ 5 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 src/DEMO/Solidity.sol create mode 100644 src/Proxy/UpgradeableToken/UpgradeableERC1155.sol create mode 100644 src/小记.md diff --git a/.gitignore b/.gitignore index 45b3a89..46215c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ /node_modules -.DS_Store src/*/artifacts/ +src/artifacts/ + +.DS_Store +src/*/.DS_Store +src/.DS_Store diff --git a/src/DEMO/Solidity.sol b/src/DEMO/Solidity.sol new file mode 100644 index 0000000..facb468 --- /dev/null +++ b/src/DEMO/Solidity.sol @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity ^0.8.0; + +/** + * @dev 智能合约中的一些内置函数 + */ +contract Solidity { + // 计算输入的Keccak-256散列 + function callKeccak256(bytes memory text) + public + pure + returns (bytes32 result) + { + return keccak256(text); + } + + function callKeccak256(string memory text) + public + pure + returns (bytes32 result) + { + return keccak256(bytes(text)); + } + + // 计算输入的SHA-256散列 + function callSha256(bytes memory text) public pure returns (bytes32) { + return sha256(text); + } + + function callRipemd160(bytes memory text) public pure returns (bytes20) { + return ripemd160(text); + } + + /** + * @dev 数值的另一种标记方式 + */ + function totalSupply() public pure returns (uint256) { + return 9_527_000_000e6; + } + + /** + * @dev 获取当前区块链的CHAINID + */ + function chainId() public view virtual returns (uint256) { + uint256 _chainId; + + assembly { + _chainId := chainid() + } + + return _chainId; + } + + /** + * @dev 当前时间戳,单位秒。 + */ + function timestamp() public view virtual returns (uint256) { + return block.timestamp; + } + + /** + * @dev 获取当前区块链高度 + */ + function height() public view virtual returns (uint256) { + return block.number; + } + + /** + * @dev 当前区块的难度 + */ + function difficulty() public view virtual returns (uint256) { + return block.difficulty; + } + + /** + * @dev 当前区块的gaslimit + */ + function gaslimit() public view virtual returns (uint256) { + return block.gaslimit; + } + + /** + * @dev 当前区块矿工的地址 + */ + function coinbase() public view virtual returns (address) { + return block.coinbase; + } + + /** + * @dev 剩余 gas + */ + function gasLeft() public view virtual returns (uint256) { + return gasleft(); + } + + /** + * @dev 交易中发送者愿意支付的价格 + */ + function gasPrice() public view virtual returns (uint256) { + return tx.gasprice; + } + + /** + * @dev 这笔交易的发送者。 在开发中避免使用,合约调用有Delegate call的方式可能不会与你的预期不一致。 + */ + function origin() public view virtual returns (address) { + return tx.origin; + } + + // msg.data (bytes calldata) 完成 calldata + // msg.sender (address payable) 消息发送者 (当前 caller) + // msg.sig (bytes4) calldata的前四个字节 (function identifier) + // msg.value (uint) 当前消息的wei值 +} diff --git a/src/ERC20/MyERC20.sol b/src/ERC20/MyERC20.sol index 480a670..0b1b609 100644 --- a/src/ERC20/MyERC20.sol +++ b/src/ERC20/MyERC20.sol @@ -24,7 +24,14 @@ contract MyERC20 is uint8 decimals_ ) ERC20(name_, symbol_) { _decimals = decimals_; - _mint(msg.sender, totalSupply_); + _mint(msg.sender, totalSupply_ * (10 ** decimals_)); + } + + /** + * @dev 获取当前区块链高度 + */ + function height() public view virtual returns (uint256) { + return block.number; } function decimals() public view virtual override returns (uint8) { @@ -36,7 +43,6 @@ contract MyERC20 is address to, uint256 amount ) internal virtual override(ERC20, ERC20Pausable, ERC20Snapshot) { - ERC20Pausable._beforeTokenTransfer(from, to, amount); - ERC20Snapshot._beforeTokenTransfer(from, to, amount); + super._beforeTokenTransfer(from, to, amount); } } diff --git a/src/Proxy/UpgradeableToken/UpgradeableERC1155.sol b/src/Proxy/UpgradeableToken/UpgradeableERC1155.sol new file mode 100644 index 0000000..b3ad764 --- /dev/null +++ b/src/Proxy/UpgradeableToken/UpgradeableERC1155.sol @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts-upgradeable/token/ERC1155/ERC1155Upgradeable.sol"; +import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; + +contract UpgradeableERC1155 is ERC1155Upgradeable, OwnableUpgradeable { + string private _name; + + string private _symbol; + + function name() public view virtual returns (string memory) { + return _name; + } + + function symbol() public view virtual returns (string memory) { + return _symbol; + } + + function initialize( + string memory tokenName_, + string memory symbol_, + string memory uri_ + ) public initializer { + _name = tokenName_; + _symbol = symbol_; + __ERC1155_init(uri_); + __Ownable_init(); + } + + function mint( + address to, + uint256 id, + uint256 amount + ) public virtual { + super._mint(to, id, amount, bytes("")); + } +} diff --git a/src/小记.md b/src/小记.md new file mode 100644 index 0000000..dab056a --- /dev/null +++ b/src/小记.md @@ -0,0 +1,13 @@ +## chainId + +```solidity +function chainId() public virtual view returns (uint256) { + uint256 _chainId ; + + assembly { + _chainId := chainid() + } + + return _chainId; +} +``` \ No newline at end of file