Skip to content

Instantly share code, notes, and snippets.

View gdebenito's full-sized avatar
🎯
Focusing

Gonzalo gdebenito

🎯
Focusing
View GitHub Profile
pragma solidity >=0.4.22 <0.6.0;
contract AssemblyArray {
function bar(uint[] calldata x, uint256 y) external pure
returns (uint256 offset, uint256 length, uint256 x0, uint256 x1, uint256 y0) {
assembly {
calldatacopy(0x0,4,36)
// x offset
calldatacopy(0x20,68,100) // x length
calldatacopy(0x40,100,132) // x[0]
calldatacopy(0x60,132,164) // x[1]
pragma solidity >=0.4.22 <0.6.0;
contract AssemblyAddr {
function foo() public returns (uint256){
assembly{
mstore(0x0,caller)
return(0x0,32)
}
}
}
@gdebenito
gdebenito / StorageOptimisation1.sol
Last active February 26, 2019 14:35
Here is an example how data is tightly packed with solidity compiler.
pragma solidity >=0.4.22 <0.6.0;
contract StorageOptimization {
uint16 a;
uint16 b;
uint16 c;
uint16 d;
uint64 e;
uint128 f;
@gdebenito
gdebenito / dynamicArray.sol
Created February 28, 2019 22:41
How does solidity stores dynamic arrays in a ethereum smart contract?
pragma solidity >=0.4.22 <0.6.0;
contract DynamicArray {
uint256[] store;
function getAssembly(uint256 _idx) external view returns(uint256 _value) {
bytes32 dir = keccak256(abi.encodePacked(uint256(0))); // stored at slot 0
assembly{
_value := sload(add(dir,_idx)) // access to keccak256 of slot 0 plus offset
@gdebenito
gdebenito / mappingStorage.sol
Created February 28, 2019 23:18
How does Solidity stores mapping in a ehtereum smart contract?
pragma solidity >=0.4.22 <0.6.0;
contract Mapping {
mapping(string => uint256) store; // stored at slot 0
function getAssembly(string calldata _key) external view returns(uint256 _value) {
bytes32 dir = keccak256(abi.encodePacked(_key,uint256(0))); // concatenation of key and slot
assembly{
_value := sload(dir) // load value
@gdebenito
gdebenito / insertDate.sh
Last active June 9, 2019 07:37
This script insert date in all files of a directory. This script is a test between sequentially and forking the process
#!/bin/bash
# inputDate.sh
# inserta fechas en todos los ficheros de un directorio
# variables
MAX_FILES=1000
DIRECTORY="files"
# generamos el directorio
if [ ! -d "$DIRECTORY" ]; then
{
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/*.aux": true,
"**/*.out": true,
"**/*.nav": true,
@gdebenito
gdebenito / removeDuplicatesAndFilter.js
Last active June 27, 2019 20:36
bash_history efficent cleaner
'use-strict'
const process = require('process')
const fs = require('fs')
const filename = process.argv.slice(process.argv.length - 1, process.argv.length)[0]
const data = fs.readFileSync(filename, { encoding: 'utf-8' })
const lines = data.split('\n').map((line) => line.trim())
let dataWant = []
@gdebenito
gdebenito / neo4j-docker-compose.yml
Last active March 4, 2020 16:07
Neo4j docker compose configuration with graph algorithms and apoc procedures.
version: '3.2'
services:
neo4j:
image: 'neo4j:3.5.11'
ports:
- '7474:7474'
- '7473:7473'
- '7687:7687'
volumes:
- type: 'bind'
// Print variable name with the variable value
printVariable() {
variableName=$1;
variableValue=$(eval "echo -n \$$1");
echo "$variableName=$variableValue";
}