Skip to content

Instantly share code, notes, and snippets.

@lyhistory
lyhistory / test_constructor.sol
Created February 22, 2019 10:23
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.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity ^0.5.0;
contract A {
uint public a;
//A constructor set asinternalcauses the contract to be marked asabstract
constructor(uint _a) internal {
a = _a;
}
}
@lyhistory
lyhistory / test_assertAndrequire.sol
Created January 31, 2019 08:41
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.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity ^0.5.0;
contract Sharer {
function sendHalf(address payable addr) public payable returns (uint balance){
require(msg.value % 2 == 0, "Even value reqired.");
uint balanceBeforeTransfer = address(this).balance;
addr.transfer(msg.value / 2);
// Since transfer throws an exception on failure and
// cannot call back here, there should be no way for us to
// still have half of the money.
@lyhistory
lyhistory / test_destructuringAndReturnMultipleValues.sol
Created January 31, 2019 07:55
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.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity >0.4.23 <0.6.0;
contract C {
uint[] data;
function f() public pure returns(uint, bool, uint){
return (7, true, 2);
}
function g() public {
@lyhistory
lyhistory / test_units.sol
Created January 31, 2019 02:13
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.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity >=0.4.0 <0.6.0;
contract UnitExample {
function test() public {
assert(1 wei == 1);
assert(1 szabo == 1e12);
assert(1 finney == 1e15);
assert(1 ether == 1e18);
//The suffix years has been removed in version 0.5.0 due to::
@lyhistory
lyhistory / test_conversion.sol
Created January 30, 2019 09:31
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.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity >=0.4.0 <0.6.0;
contract ConversionExample {
function test() public {
int8 y = -3;
uint x =uint(y); //0xfffff..fd
uint32 a = 0x12345678;
uint16 b = uint16(a);// b will be 0x5678 now
@lyhistory
lyhistory / test_delete.sol
Created January 30, 2019 09:03
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.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity >=0.4.0 <0.6.0;
contract DeleteExample {
uint data;
uint[] dataArray;
function f() public {
uint x = data;
delete x; //sets x to 0, does not affect data
delete data; // sets data to 0, does not affect x
uint[] storage y = dataArray;
@lyhistory
lyhistory / test_iterable_mapping.sol
Created January 30, 2019 08:18
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.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity >=0.4.0 <0.6.0;
/// @dev Models a uint -> uint mapping where it is possible to iterate over all keys.
library IterableMapping
{
struct itmap
{
mapping(uint => IndexValue) data;
KeyFlag[] keys;
uint size;
}
@lyhistory
lyhistory / test_struct.sol
Created January 30, 2019 06:46
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.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity >=0.4.11 <0.6.0;
contract CrowFunding {
// Defines a new type with two fields
struct Funder {
address addr;
uint amount;
}
struct Campaign {
address payable beneficiary;
uint fundingGoal;
@lyhistory
lyhistory / test_assignment.sol
Created January 29, 2019 08:10
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.4.17+commit.bdeb9e52.js&optimize=false&gist=
pragma solidity >=0.4.0 <0.6.0;
contract C {
uint[] x;
// the data location of x is storage
// the data location of memoryArray is memory
function f(uint[] memory memoryArray) public{
x = memoryArray; // works, copies the whole array to storage
uint[] storage y = x; // works, assigns a pointer, data location of y is storage
//y[7]; // fine, returns the 8th element
//y.length = 2; // fine, modifies x through y
@lyhistory
lyhistory / test_external_function.sol
Created December 28, 2018 03:25
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.4.17+commit.bdeb9e52.js&optimize=false&gist=
pragma solidity >=0.4.16 <0.6.0;
contract Oracle{
struct Request{
bytes data;
function(uint) external callback;
}
Request[] requests;
event NewRequest(uint);
event Reply(uint);