Created
October 12, 2021 19:22
-
-
Save JoshOrndorff/6df07174f0b3407f226a1878fef65d46 to your computer and use it in GitHub Desktop.
Solidity Snippets for Frontier Workshop 2021
This file contains 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-only | |
pragma solidity >=0.8.0; | |
contract Incrementor { | |
uint private value; | |
address private last_caller; | |
function inc() public { | |
value += 1; | |
last_caller = msg.sender; | |
} | |
function add(uint delta) public { | |
value += delta; | |
last_caller = msg.sender; | |
} | |
function get_value() view public returns (uint) { | |
return value; | |
} | |
function get_last_caller() view public returns (address) { | |
return last_caller; | |
} | |
} |
This file contains 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
pragma solidity >=0.8.0; | |
contract ModularCheck { | |
// Verify simple modular exponentiation | |
constructor() { | |
require(modExp(3, 5, 7) == 5); | |
require(modExp(5, 7, 11) == 3); | |
// This one is incorrect. Uncomment it and confirm deploy fails | |
require(modExp(5, 7, 11) == 2); | |
} | |
// Wrapper function to use the precompile. | |
// Taken from https://ethereum.stackexchange.com/a/71590/9963 | |
function modExp(uint256 _b, uint256 _e, uint256 _m) public returns (uint256 result) { | |
assembly { | |
// Free memory pointer | |
let pointer := mload(0x40) | |
// Define length of base, exponent and modulus. 0x20 == 32 bytes | |
mstore(pointer, 0x20) | |
mstore(add(pointer, 0x20), 0x20) | |
mstore(add(pointer, 0x40), 0x20) | |
// Define variables base, exponent and modulus | |
mstore(add(pointer, 0x60), _b) | |
mstore(add(pointer, 0x80), _e) | |
mstore(add(pointer, 0xa0), _m) | |
// Store the result | |
let value := mload(0xc0) | |
// Call the precompiled contract 0x05 = bigModExp | |
if iszero(call(not(0), 0x05, 0, pointer, 0xc0, value, 0x20)) { | |
revert(0, 0) | |
} | |
result := mload(value) | |
} | |
} | |
} |
This file contains 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-only | |
pragma solidity >=0.8.0; | |
/// @author The Moonbeam Team | |
/// @title The interface through which solidity contracts will interact with Crowdloan Rewards | |
/// We follow this same interface including four-byte function selectors, in the precompile that | |
/// wraps the pallet | |
interface PalletTemplate { | |
/// @dev Store a value in the pallet's storage. | |
/// @param value The new value to store. 32 bit maximum | |
function do_something(uint256 value) external; | |
/// @dev Retrieve the stored value | |
/// @return A uint256 (with max 32-bit value) indicating the value stored in the pallet | |
function get_value() external view returns (uint256); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment