Created
September 30, 2018 09:36
-
-
Save code-brewer/f899b39178f1e2f64397ac40c893381f 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.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.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; | |
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment