Skip to content

Commit 908b7b0

Browse files
committedMar 6, 2019
added objectified subparser - even more convenient :)
added simple sample
1 parent 73fe0cd commit 908b7b0

File tree

3 files changed

+425
-5
lines changed

3 files changed

+425
-5
lines changed
 

‎samples/simple.sol

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
pragma solidity ^0.4.22;
2+
pragma solidity 0.4.22;
3+
pragma experimental something;
4+
5+
import "a.sol";
6+
import {a,b} from "a.sol";
7+
import "./abc.sol" as x;
8+
import * as y from "./abc.sol";
9+
import {a as b, c as d, f} from "./abc.sol";
10+
11+
contract SimpleAuction {
12+
// Parameters of the auction. Times are either
13+
// absolute unix timestamps (seconds since 1970-01-01)
14+
// or time periods in seconds.
15+
address public beneficiary;
16+
uint public auctionEnd;
17+
18+
19+
// Current state of the auction.
20+
address public highestBidder;
21+
uint public highestBid;
22+
23+
// Allowed withdrawals of previous bids
24+
mapping(address => uint) pendingReturns;
25+
26+
// Set to true at the end, disallows any change
27+
bool ended;
28+
29+
struct MyStructName {
30+
address addr;
31+
uint256 count;
32+
}
33+
34+
35+
MyStructName haha;
36+
37+
enum SomeData {DEFAULT,ONE,TWO}
38+
SomeData someData;
39+
40+
// Events that will be fired on changes.
41+
event HighestBidIncreased(address bidder, uint amount);
42+
event AuctionEnded(address winner, uint amount);
43+
44+
// The following is a so-called natspec comment,
45+
// recognizable by the three slashes.
46+
// It will be shown when the user is asked to
47+
// confirm a transaction.
48+
49+
/// Create a simple auction with `_biddingTime`
50+
/// seconds bidding time on behalf of the
51+
/// beneficiary address `_beneficiary`.
52+
constructor(
53+
uint _biddingTime,
54+
address _beneficiary
55+
) public {
56+
beneficiary = _beneficiary;
57+
auctionEnd = now + _biddingTime;
58+
}
59+
60+
function(uint a, uint b) public {}
61+
62+
/// Bid on the auction with the value sent
63+
/// together with this transaction.
64+
/// The value will only be refunded if the
65+
/// auction is not won.
66+
function bid() public payable {
67+
// No arguments are necessary, all
68+
// information is already part of
69+
// the transaction. The keyword payable
70+
// is required for the function to
71+
// be able to receive Ether.
72+
73+
// Revert the call if the bidding
74+
// period is over.
75+
require(
76+
now <= auctionEnd,
77+
"Auction already ended."
78+
);
79+
80+
// If the bid is not higher, send the
81+
// money back.
82+
require(
83+
msg.value > highestBid,
84+
"There already is a higher bid."
85+
);
86+
87+
if (highestBid != 0) {
88+
// Sending back the money by simply using
89+
// highestBidder.send(highestBid) is a security risk
90+
// because it could execute an untrusted contract.
91+
// It is always safer to let the recipients
92+
// withdraw their money themselves.
93+
pendingReturns[highestBidder] += highestBid;
94+
}
95+
highestBidder = msg.sender;
96+
highestBid = msg.value;
97+
emit HighestBidIncreased(msg.sender, msg.value);
98+
}
99+
100+
/// Withdraw a bid that was overbid.
101+
function withdraw() public returns (bool) {
102+
uint amount = pendingReturns[msg.sender];
103+
if (amount > 0) {
104+
// It is important to set this to zero because the recipient
105+
// can call this function again as part of the receiving call
106+
// before `send` returns.
107+
pendingReturns[msg.sender] = 0;
108+
109+
if (!msg.sender.send(amount)) {
110+
// No need to call throw here, just reset the amount owing
111+
pendingReturns[msg.sender] = amount;
112+
return false;
113+
}
114+
}
115+
return true;
116+
}
117+
118+
modifier loli () {
119+
120+
}
121+
122+
function lol() public {
123+
return pendingReturns[msg.sender];
124+
}
125+
126+
/// End the auction and send the highest bid
127+
/// to the beneficiary.
128+
function auctionEnd() public {
129+
// It is a good guideline to structure functions that interact
130+
// with other contracts (i.e. they call functions or send Ether)
131+
// into three phases:
132+
// 1. checking conditions
133+
// 2. performing actions (potentially changing conditions)
134+
// 3. interacting with other contracts
135+
// If these phases are mixed up, the other contract could call
136+
// back into the current contract and modify the state or cause
137+
// effects (ether payout) to be performed multiple times.
138+
// If functions called internally include interaction with external
139+
// contracts, they also have to be considered interaction with
140+
// external contracts.
141+
142+
// 1. Conditions
143+
require(now >= auctionEnd, "Auction not yet ended.");
144+
require(!ended, "auctionEnd has already been called.");
145+
146+
// 2. Effects
147+
ended = true;
148+
emit AuctionEnded(highestBidder, highestBid);
149+
150+
// 3. Interaction
151+
beneficiary.transfer(highestBid);
152+
}
153+
}

‎solidity_parser/__main__.py

+53-5
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,58 @@
66
import pprint
77

88
if __name__ == "__main__":
9-
if not len(sys.argv)>1:
10-
print("\n- missing path to solidity file.\n")
11-
print("#> python -m solidity_parser <solidity file>")
9+
if not len(sys.argv)>2 or sys.argv[1] not in ("parse","outline"):
10+
print("\n- missing subcommand or path to solidity file.\n")
11+
print("#> python -m solidity_parser <subcommand> <solidity file>")
1212
sys.exit(1)
1313

14-
node = parser.parse_file(sys.argv[1])
15-
pprint.pprint(node)
14+
node = parser.parse_file(sys.argv[2])
15+
if sys.argv[1]=="parse":
16+
pprint.pprint(node)
17+
elif sys.argv[1]=="outline":
18+
level = 0
19+
sourceUnitObject = parser.objectify(node)
20+
print("=== pragmas ===")
21+
level +=1
22+
for p in sourceUnitObject.pragmas:
23+
print(("\t" * level) + "* " + str(p))
24+
level -=1
25+
print("=== imports ===")
26+
level +=1
27+
for p in sourceUnitObject.imports:
28+
print(("\t" * level) + "* " + str(p))
29+
level = 0
30+
for contract_name, contract_object in sourceUnitObject.contracts.items():
31+
print("=== contract: " + contract_name)
32+
level +=1
33+
## statevars
34+
print(("\t" * level) + "=== Enums")
35+
level += 2
36+
for name in contract_object.enums.keys():
37+
print(("\t" * level) + "* " + str(name))
38+
level -= 2
39+
## structs
40+
print(("\t" * level) + "=== Structs")
41+
level += 2
42+
for name in contract_object.structs.keys():
43+
print(("\t" * level) + "* " + str(name))
44+
level -= 2
45+
## statevars
46+
print(("\t" * level) + "=== statevars" )
47+
level +=2
48+
for name in contract_object.stateVars.keys():
49+
print(("\t" * level) + "* " + str(name) )
50+
level -=2
51+
## modifiers
52+
print(("\t" * level) + "=== modifiers")
53+
level += 2
54+
for name in contract_object.modifiers.keys():
55+
print(("\t" * level) + "* " + str(name))
56+
level -= 2
57+
## functions
58+
print(("\t" * level) + "=== functions")
59+
level += 2
60+
for name in contract_object.functions.keys():
61+
print(("\t" * level) + "* " + str(name))
62+
level -= 2
63+

0 commit comments

Comments
 (0)
Failed to load comments.