Skip to content

Instantly share code, notes, and snippets.

@clowestab
Created January 22, 2024 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clowestab/2b98c07c2a938171ade027ba62e2246a to your computer and use it in GitHub Desktop.
Save clowestab/2b98c07c2a938171ade027ba62e2246a 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.22+commit.4fc1097e.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.12 <0.9.0;
contract AssortedTests {
struct TestStruct {
NestedStruct[] nestedItems;
}
struct NestedStruct {
address target;
bytes32[] commands;
}
/**
* @dev decode a 32 byte bytes value to an address
*/
function bytesToAddress() public pure returns (address) {
bytes memory asBytes = abi.encodePacked(hex"000000000000000000000000c9f7e9e42b17744b72c5b07b6c38128c8fd6447a");
return abi.decode(asBytes, (address));
}
/**
* @dev Converts a bytes value to bytes32
*/
function bytesToBytes32() public pure returns (bytes32) {
bytes memory value = abi.encodePacked(hex"0000000000000000000000000000000000000000000000000000000000000001");
bytes32 asBytes32 = bytes32(value);
return asBytes32;
}
/**
* @dev Converts a bytes32 value to its unsigned integer value
*/
function bytes32ToInt() public pure returns (uint256) {
bytes32 value = 0x0000000000000000000000000000000000000000000000000000000000000001;
uint256 integerRepresentation = uint256(value);
return integerRepresentation;
}
/**
* @dev Inserts a specified byte (8 bit) value in a particular position
*/
function byteToPosition() public pure returns (bytes32) {
bytes32 empty = 0x0000000000000000000000000000000000000000000000000000000000000000;
uint256 position = 31;
//Converting bytes1 => bytes32 appends 31 additional 0 bytes to the right
//We bitwise shift the each byte in the resulting bytes32 to the right using a bitwise shift
//When you do so additional zero bytes are prepended to the left.
return empty | bytes32(bytes1(0x20)) >> (8 * position);
}
/**
* @dev Demonstrate how values are passed by reference in the context of struct arrays
*/
function refTest() public pure returns(NestedStruct memory) {
//Create a nested struct with length 2 (arbitrary)
NestedStruct[] memory nestedItems = new NestedStruct[](2);
//Create a struct and pass our NestedStruct[] as an argument
TestStruct memory mainStruct = TestStruct(nestedItems);
//Create a new bytes32 array
bytes32[] memory commands = new bytes32[](32);
//Play with length - set it to empty
assembly {
mstore(commands, 0) // Set current array length to 0
}
//Set the first (0 index) nested item to a newly instantiated NestedStruct instance
mainStruct.nestedItems[0] = NestedStruct(address(0x0), commands);
//Create a new space in our commands array
assembly {
mstore(commands, 1) // Increment command array length
}
//Arbitrary operation - 8 bit (1 byte) (2 nibbles / hex characters)
uint8 op = 0x04;
//Arbitrary operation location
uint opLoc = 3;
//By setting the 0 index command here are we manipulating the same memory pointer reference?
commands[0] = commands[0] | (bytes32(bytes1(op)) >> (8 * opLoc));
//I.E. does this return the updated commands
return mainStruct.nestedItems[0];
}
/**
* @dev Convert an integer to an address
*/
function intToAddress(uint8 idx) public pure returns (address addr) {
addr = address(uint160(bytes20(bytes1(idx)) >> (152)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment