Created
October 12, 2018 02:34
-
-
Save code-brewer/e38d283d0d7cea6b910fa953dfb38b5c 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=false&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.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.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 1 ether"); | |
return (true, weiAmount); | |
} | |
} |
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.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