Skip to content

Instantly share code, notes, and snippets.

@chriseth
chriseth / Automated Source Code Repository.md
Last active April 2, 2019 20:55
Automated Source Code Repository

To increase decentralization, it would be great to have a verified repository of smart contract source codes. Due to the metadata being part of the bytecode, we should have everything that is needed to actually perform the verification. This repository could be an actual repository on github, where people can create pull requests to have their smart contract code compiled and verified. There could be a CI job that does the verification as follows:

  • fetch the bytecode from the chain via infura or another service
  • install the correct compiler version using npm
  • only flag the PR green, if the compilation output matches the bytecode

If the directory name is the address of the contract, the source code should be easy to find, even if multiple

@chriseth
chriseth / example.yul
Created December 20, 2018 08:25
Example YUL contract
// Compile the following using ``solc --strict-assembly example.yul``
object "MyContract" {
code {
// this is the constructor.
// store the creator in the first storage slot
sstore(0, caller())
// now return the runtime code using the special functions
datacopy(0, dataoffset("Runtime"), datasize("Runtime"))
return(0, datasize("Runtime"))
}

EasterHegg 18 Workshop: Smart Contracts on Ethereum

Requirements:

Coding your first smart contract!

a. Setting up Metamask

@chriseth
chriseth / docdiff.patch
Created February 20, 2018 11:43
Differences in Solidity documentation between 26ea9ce07cf85849cd881465a4322f14bff87eb8 and a6b52fdc34650c74597c1bcdc5075b6375c62650
diff --git a/docs/abi-spec.rst b/docs/abi-spec.rst
index e39c8861..8095a3b7 100644
--- a/docs/abi-spec.rst
+++ b/docs/abi-spec.rst
@@ -6,17 +6,19 @@
Application Binary Interface Specification
******************************************
-Basic design
+Basic Design
@chriseth
chriseth / MyToken.solidity
Created December 19, 2017 15:30
MyToken - IULIA example
contract MyToken {
function MyToken() {
assembly {
// Store the creator at storage slot zero
sstore(0, caller())
}
}
function () payable {
assembly {
// Protection against sending Ether

Answers to Deep Questions about Solidity

The following list of questions was taken from https://www.reddit.com/r/ethereum/comments/72reba/do_you_have_deep_questions_about_solidity_or_the/

An updated summary on the different ways one could have two contracts interact (DELEGATECALL, STATICCALL, libraries, all that stuff) with clear pros/cons for each (gas cost, whether it requires EVM assembly directives, etc)

Question by /u/drcode

I won't talk about low-level opcodes here because of the brevity of the answer. In general, there are four ways functions can be called in Solidity:

@chriseth
chriseth / snarktest.solidity
Last active December 3, 2023 07:03
zkSNARKs test code
// This file is MIT Licensed.
//
// Copyright 2017 Christian Reitwiessner
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF O
contract PatriciaTree {
// List of all keys, not necessarily sorted.
bytes[] keys;
// Mapping of hash of key to value
mapping (bytes32 => bytes) values;
struct Label {
bytes32 data;
uint length;
}
@chriseth
chriseth / template.cpp
Created May 31, 2017 17:12
Sketch of use of templating language for the julia code generator
bool ExpresionCompiler::visit(BinaryExpression const& _expr)
{
// noteResult returns the julia-equivalent of the expression.
// it also inserts a comment containing the source location of _expr
// Anything inside angle brackets is looked up in the table passed
// as third argument and a plain string replacement is performed.
// The convert function calls visit recursively (or instanciates a
// new AST walker) and returns what was supplied to "noteResult".
@chriseth
chriseth / puritychecker.asm
Last active November 28, 2023 04:08
Purity checker
{
// Some elementary helpers ---------------------------------------
function memptr() -> addr
{
addr := 0x40
}
function allocate(size) -> addr
{
addr := mload(memptr())