Skip to content

Instantly share code, notes, and snippets.

@abhi3700
Created December 28, 2021 15:13
Show Gist options
  • Save abhi3700/b116928bed071d12c9c992b6eba92a46 to your computer and use it in GitHub Desktop.
Save abhi3700/b116928bed071d12c9c992b6eba92a46 to your computer and use it in GitHub Desktop.
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.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Incrementer {
uint256 private _value;
address private _lastCaller;
function inc() external {
_value += 1;
_lastCaller = msg.sender;
}
function add(uint256 delta) external {
_value += delta;
_lastCaller = msg.sender;
}
function getValue() public view returns ( uint256 ) {
return _value;
}
function getLastCaller() public view returns ( address ) {
return _lastCaller;
}
}
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)
}
}
}
// 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