This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.8.17; | |
contract Random { | |
// 最簡單的取亂數方式,從鏈上資訊產生亂數 | |
function getRandomFromBlock(uint number) external view returns(uint) { | |
return uint(keccak256(abi.encodePacked(block.timestamp, block.difficulty, msg.sender))) % number; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.8.17; | |
// 管理員的管理邏輯放在這個合約內 | |
contract OwnerManager { | |
uint16 public ownerCount; // 管理員人數 | |
uint16 public requireCount = 1; // 至少需要簽署的人數 | |
mapping(address => bool) public owners; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.17; | |
import "./ABDKMath64x64.sol"; | |
import "./ABDKMathQuad.sol"; | |
contract ABDKMath { | |
// 第一種寫法:計算 x 的 3.5%,計算過程是 x * 35 / 1000,輸入 1000 結果是 35 | |
function percent(uint256 x) external pure returns (uint256) { | |
int128 result = ABDKMath64x64.div( // 用內建的 function 做運算 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract PercentCalculate { | |
// 計算 x 的 3.5% | |
function percent(uint256 x) external pure returns (uint256) { | |
// 第一種寫法:Solidity 不支援浮點數運算,無法使用 | |
// return x * 0.035; | |
// 第二種寫法:x 分別輸入 10000、1000、100 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract PercentCalculate { | |
//計算 x 的 3.5% | |
function percent(uint256 x) external pure returns (uint256) { | |
// 第一種寫法:Solidity 不支援浮點數運算,無法使用 | |
// return x*0.035; | |
// 第二種寫法:x 分別輸入 10000、1000、100 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.8.17; | |
contract PiggyBank { | |
address payable owner; | |
constructor() { | |
owner = payable(msg.sender); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.8.17; | |
contract EthWallet { | |
address payable owner; | |
constructor() { | |
owner = payable(msg.sender); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.8.16; | |
// 人類 | |
contract Human { | |
event Log(string); | |
function say() public virtual { | |
emit Log("Human say"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.8.16; | |
// 爸爸 | |
contract Father { | |
event FatherLog(string, string); | |
constructor(string memory name) { | |
emit FatherLog("Fathor Constructor", name); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.8.16; | |
contract EncodeDecode { | |
// Encode:丟參數值進去,回傳 encode 後的內容 | |
function encode(string memory name, uint score) external pure returns(bytes memory) { | |
return abi.encode(name, score); | |
} |