Skip to content

Instantly share code, notes, and snippets.

// 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;
}
// 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;
// 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 做運算
// 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
// 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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.17;
contract PiggyBank {
address payable owner;
constructor() {
owner = payable(msg.sender);
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.17;
contract EthWallet {
address payable owner;
constructor() {
owner = payable(msg.sender);
}
@jack21
jack21 / Week6...CallingParentFunction.sol
Created September 17, 2022 05:08
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.17+commit.8df45f5f.js&optimize=true&runs=200&gist=
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.16;
// 人類
contract Human {
event Log(string);
function say() public virtual {
emit Log("Human say");
@jack21
jack21 / Week6...CallingParentContructor.sol
Created September 17, 2022 04:07
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.17+commit.8df45f5f.js&optimize=true&runs=200&gist=
// 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);
@jack21
jack21 / Week6...EncodeDecode.sol
Created September 17, 2022 02:36
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.17+commit.8df45f5f.js&optimize=true&runs=200&gist=
// 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);
}