Created
October 12, 2018 09:49
-
-
Save code-brewer/10273128a2c593bcdc4bf2f2392aa497 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.4.25+commit.59dbf8f1.js&optimize=true&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
/*数组类型Demo*/ | |
contract DemoTypes303 { | |
/*String数组例子*/ | |
string[] strArr; | |
function add(string str) public { | |
strArr.push(str); | |
} | |
function getStrAt(uint n) public constant returns (string s){ | |
string storage tmp = strArr[n]; | |
return tmp; | |
} | |
function updateStrAt(uint n, string str) public { | |
strArr[n] = str; | |
} | |
function deleteStrAt(uint index) public { | |
uint len = strArr.length; | |
if (index >= len) return; | |
for (uint i = index; i<len-1; i++) { | |
strArr[i] = strArr[i+1]; | |
} | |
delete strArr[len-1]; | |
strArr.length--; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.19; | |
contract BytesOrStrings { | |
string constant _string = "cryptopus.co Medium"; | |
bytes32 constant _bytes = "cryptopus.co Medium"; | |
function getAsString() public pure returns(string) { | |
return _string; | |
} | |
function getAsBytes() public pure returns(bytes32) { | |
return _bytes; | |
} | |
} | |
contract BytesOrStringsOverSize { | |
string constant _string = "cryptopus.co Medium! abcdefghjklmnopqrstuvwsyz"; | |
//bellow line raise error: TypeError: Type literal_string "cryptopus.co Medium! abcdefghjklmnopqrstuvwsyz" is not implicitly convertible to expected type bytes32. | |
// bytes32 constant _bytes = "cryptopus.co Medium! abcdefghjklmnopqrstuvwsyz"; | |
bytes32 constant _bytes = "crypt"; | |
function getAsString() public pure returns(string) { | |
return _string; | |
} | |
function getAsBytes() public pure returns(bytes32) { | |
return _bytes; | |
} | |
} | |
import "github.com/Arachnid/solidity-stringutils/strings.sol"; | |
contract StringTest{ | |
using strings for *; | |
function concat() public pure returns(string) { | |
return "abc".toSlice().concat("def".toSlice()); | |
} | |
function concat2() public returns(string) { | |
return "123".toSlice().concat("456".toSlice()); | |
} | |
} | |
contract BytesTest{ | |
bytes public bs; | |
function push(byte b) public { //GOOGLE: solidity remix pass byte value to function | |
// function push(char b) public { // WYH: No such char type ! | |
bs.push(b); | |
} | |
function getAsString() public view returns(uint, string) { | |
return (bs.length, string(bs)); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract stringOpTest { | |
byte[] bs; | |
function convertingToString()public returns(string){ | |
// bytes32 memory hw = "Hello World"; // TypeError: Storage location can only be given for array or struct types. | |
bytes32 hw = "Hello World"; // | |
// string memory converted = string(hw); // TypeError: Explicit type conversion not allowed from "bytes32" to "string storage pointer". | |
bytes memory bytesArray = new bytes(32); | |
for (uint256 i=0; i < 32; i++) { | |
bytesArray[i] = hw[i]; | |
} | |
string memory converted = string(bytesArray); | |
return converted; | |
} | |
function byteAppend(byte b) { | |
// bs.append ?? | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract CA{ | |
uint public p; | |
event e(address add,uint p); | |
function fun(uint u1,uint u2)public{ | |
p = u1 + u2; | |
emit e(msg.sender,p); | |
} | |
} | |
contract CB{ | |
uint public q; | |
bool public b; | |
function CB() public payable { | |
} | |
function call1(address add)public returns(bool) { | |
b = add.call(bytes4(keccak256("fun(uint256,uint256)")),2,3); | |
return b; | |
} | |
function call2(address add)public returns(bool){ | |
b = add.delegatecall(bytes4(keccak256("fun(uint256,uint256)")),1,2); | |
return b; | |
} | |
function sendDemo(address add) public{ | |
uint u = 1 ether; | |
add.transfer(u); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.2; | |
contract parent{ | |
string public name; | |
event ConstrutEvt(string flag, string _name ); | |
function parent(string _name) public{ | |
name=_name; | |
emit ConstrutEvt("parent::ctor", _name); | |
} | |
} | |
contract child is parent{ | |
function child(string _name) { | |
name=_name; | |
emit ConstrutEvt("child::ctor", _name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract EventFilterTest { | |
constructor() public { | |
b = 0x12345678901234567890123456789012; | |
} | |
event Event(uint indexed a, bytes32 b) ; | |
event Event2(uint indexed a, bytes32 b) ; | |
function foo(uint a) public { | |
b = bytes32(1); | |
emit Event(a, b); | |
emit Event2(a, b); | |
} | |
bytes32 b; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract SimpleStorage { | |
uint public data; | |
// event SetX(address indexed _from, uint value); | |
event Set(string label, uint value); | |
function set(uint x) public { | |
data = x; | |
// emit SetX(msg.sender, x); | |
// emit Set("#####", x); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
contract MathTest { | |
uint256 public price = 2 ether; | |
uint256 public quantity = 2**200; | |
function addTest() external pure returns(uint256, uint256, uint256) { | |
uint256 u; // range: [0, 2**256) | |
u = 2**256 - 1; | |
assert(u + 1 == 0); | |
return (u, u+1, u+2); | |
} | |
// DO NOT USE! | |
function batchTransfer(address[] receivers, uint256 value) public pure { | |
uint256 amount = receivers.length * value; | |
// 如果用户构造的receivers.length 乘以 value 刚好为 0 ,那么就有问题了! | |
} | |
function buy() public payable returns(bool, uint256) { | |
uint256 weiAmount = msg.value; //WYH: input fund will be convert to amount in wei uint! | |
require(msg.value >= 2 ether, "value must greater than 2 ether"); | |
return (true, weiAmount); | |
} | |
function tranfsLimitFund() public payable returns(bool, uint256) { | |
uint256 weiAmount = msg.value; //WYH: input fund will be convert to amount in wei uint! | |
require(msg.value <= 2 ether, "value must no more than 2 ether"); | |
return (true, weiAmount); | |
} //测试结果: require或者assert失败时, msg.value会原路退回---也就是合约方法的调用者账户中的钱不会丢失! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract C { | |
uint[] data1; | |
uint[] data2; | |
function appendOne() public { | |
append(data1); | |
} | |
function appendTwo() public { | |
append(data2); | |
} | |
function append(uint[] storage d) internal { | |
d.push(1); | |
} | |
function getData1() view public returns(uint[]){ | |
return data1; | |
} | |
function getData2() view public returns(uint[]){ | |
return data2; | |
} | |
} | |
contract C2 { | |
uint public someVariable ; | |
uint[] public data; | |
function f() public { | |
uint[] x; | |
x.push(2); | |
data = x; | |
} | |
} | |
contract ConstantTest{ | |
uint constant var1=now; | |
uint var2=now; | |
function checkConstant() returns(uint,uint){ | |
return (var1,var2); | |
} | |
function getVar1() returns(uint){ return var1;} | |
function getVar2() returns(uint){ return var2;} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
import "github.com/Arachnid/solidity-stringutils/strings.sol"; | |
contract A{ | |
using strings for *; | |
string public flags ; | |
string myName; | |
address public owner; | |
string callResult ="R:"; | |
event Ret(string flag, address sender, address owner); | |
event ConstrutEvt(string flag, address owner ); | |
function A() public{ | |
owner = msg.sender; | |
emit ConstrutEvt("A", msg.sender); | |
} | |
function setMyName(string name) public { | |
myName = name; | |
} | |
function doSomething() public view returns(string flag, address sender, address owner){ | |
// does something | |
// callResult = callResult + "A::doSomething()"; | |
// string old = flags; | |
flags = flags.toSlice().concat("A::doSomething()".toSlice()); | |
emit Ret("A::doSomething", msg.sender, owner); | |
return ("A::doSomething", msg.sender, owner); | |
} | |
} | |
contract B is A{ | |
// using strings for *; | |
address public Bsender; | |
function doSomething() public view returns(string flag, address sender, address owner){ | |
// require(msg.sender == owner); | |
Bsender = msg.sender; | |
// do something like A but restrict this function execution to owner | |
super.doSomething(); | |
flags = flags.toSlice().concat("B::doSomething()".toSlice()); | |
emit Ret("B::doSomething", msg.sender, owner); | |
return ("B::doSomething", msg.sender, owner); | |
} // Got result: 'A::doSomething()B::doSomething()' | |
function getMyName() public view returns(string name) { | |
return myName; | |
} | |
} | |
contract EvilDoer{ | |
address owner; | |
constructor() public payable { | |
owner = msg.sender; | |
} | |
function doSomethingEvil(address addressOfdeployedB) public { | |
A contractA = A(addressOfdeployedB); | |
A contractB = new B(); | |
contractB.doSomething(); | |
} | |
function doSomethingEvil2(address addressOfdeployedB) public { | |
A contractB = A(addressOfdeployedB); | |
contractB.doSomething(); | |
contractB.setMyName("___xxx___"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract A{ | |
address public owner; | |
function A() public{ | |
owner = msg.sender; | |
} | |
function doSomething() public{ | |
// does something | |
} | |
} | |
contract B is A{ | |
function doSomething() public{ | |
// require(msg.sender == owner); | |
// do something like A but restrict this function execution to owner | |
super.doSomething(); | |
} | |
} | |
contract EvilDoer{ | |
address owner; | |
constructor() public payable { | |
owner = msg.sender; | |
} | |
function doSomethingEvil(address addressOfdeployedB) public { | |
A contractB = new B(); | |
contractB.doSomething(); | |
} | |
function doSomethingEvil2(address addressOfdeployedB) public { | |
A contractB = A(addressOfdeployedB); | |
contractB.doSomething(); //Why Failed? Error message is: 'The constructor should be payable if you send value.' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract A{ | |
string myName; | |
address public owner; | |
event Ret(string flag, address sender, address owner); | |
event ConstrutEvt(string flag, address owner ); | |
function A() public{ | |
owner = msg.sender; | |
emit ConstrutEvt("A", msg.sender); | |
} | |
function setMyName(string name) public { | |
myName = name; | |
} | |
function doSomething() public view returns(string flag, address sender, address owner){ | |
// does something | |
emit Ret("A::doSomething", msg.sender, owner); | |
return ("A::doSomething", msg.sender, owner); | |
} | |
} | |
contract B is A{ | |
address public Bsender; | |
function doSomething() public view returns(string flag, address sender, address owner){ | |
Bsender = msg.sender; | |
// do something like A but restrict this function execution to owner | |
super.doSomething(); | |
emit Ret("B::doSomething", msg.sender, owner); | |
return ("B::doSomething", msg.sender, owner); | |
} | |
function getMyName() public view returns(string name) { | |
return myName; | |
} | |
} | |
contract EvilDoer{ | |
address owner; | |
constructor() public payable { | |
owner = msg.sender; | |
} | |
function doSomethingEvil(address addressOfdeployedB) public { | |
A contractB = A(addressOfdeployedB); | |
contractB.doSomething(); | |
contractB.setMyName("___xxx___"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
//请注意这个仅是Demo,请不要用到正式环境 | |
contract PayTest { | |
function() public payable {} | |
//得到当前合约的余额 | |
function getBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
//向当前合约存款 | |
function deposit() payable public returns(address addr, uint amount){ | |
//msg.sender 全局变量,调用合约的发起方 | |
//msg.value 全局变量,调用合约的发起方转发的货币量,以wei为单位。 | |
//send() 执行的结果 | |
address(this).transfer(msg.value); | |
return (msg.sender, msg.value ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract InfoFeed { | |
address owner; | |
constructor(){ | |
owner = msg.sender; | |
} | |
function info() returns (uint ret) { return 42; } | |
function getBalance() public returns (uint) { | |
return this.balance; | |
} | |
function kill() public { | |
if (owner == msg.sender) { // 检查谁在调用 | |
selfdestruct(owner); // 销毁合约 | |
} | |
} | |
} | |
contract Consumer { | |
InfoFeed public feed; // points to contract on blockchain | |
constructor() { | |
// 会触发构造函数! | |
feed = new InfoFeed(); // new instance created; constructor called | |
} | |
function getBalance() public returns (uint) { | |
return this.balance; | |
} | |
function doTransfer(uint val) { | |
address(this).transfer(10); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract EthToContract { | |
uint256 public counter = 5; //state variable we assigned earlier | |
address public owner = msg.sender; //set owner as msg.sender | |
function add() public { //increases counter by 1 | |
counter++; | |
} | |
function subtract() public { //decreases counter by 1 | |
counter--; | |
} | |
function getCounter() public constant returns (uint256) { | |
return counter; | |
} | |
function kill() public { //self-destruct function, | |
if(msg.sender == owner) { | |
selfdestruct(owner); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.17; | |
contract SimpleStorage { | |
uint myVariable; | |
event Deposit(address indexed _from, address indexed _to, uint value); | |
event Set(address indexed from, uint value); | |
function set(uint x) public { | |
myVariable = x; | |
emit Set(msg.sender, x); | |
} | |
function get() constant public returns (uint) { | |
return myVariable; | |
} | |
// 向别的地址存款: | |
function depositTo(address to) public { | |
emit Deposit(msg.sender, to, msg.value); | |
// ... | |
} | |
function() public payable { | |
} | |
//得到当前合约的余额 | |
function getBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
//向当前合约存款 | |
function deposit() payable public returns(address addr, uint amount){ | |
//msg.sender 全局变量,调用合约的发起方 | |
//msg.value 全局变量,调用合约的发起方转发的货币量,以wei为单位。 | |
//send() 执行的结果 | |
emit Deposit(msg.sender, address(this), msg.value); | |
address(this).transfer(msg.value); | |
return (msg.sender, msg.value ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment