Skip to content

Commit e46f804

Browse files
committed
First samples
0 parents  commit e46f804

30 files changed

+485
-0
lines changed

.DS_Store

8 KB
Binary file not shown.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
venv

01-Brownie-Intro/.DS_Store

6 KB
Binary file not shown.

01-Brownie-Intro/.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PRIVATE_KEY_1='d110ccbee81214d4681d52d7653041686fc77b256b3d741fc8566932c181f665'

01-Brownie-Intro/.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.sol linguist-language=Solidity
2+
*.vy linguist-language=Python

01-Brownie-Intro/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__pycache__
2+
.history
3+
.hypothesis/
4+
build/
5+
reports/

01-Brownie-Intro/brownie-config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
networks:
2+
default: local
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
pragma solidity ^0.8.1;
2+
3+
contract Guess_number {
4+
5+
address payable player;
6+
enum State {OPEN, COMPLETE}
7+
State public currState;
8+
uint private secretNumber;
9+
uint public balance;
10+
11+
constructor (uint _secretNumber) payable {
12+
require(msg.value == 10*10**18, 'contract needs to be funded with at least 10 eth');
13+
secretNumber = _secretNumber;
14+
currState = State.OPEN;
15+
balance = balance + msg.value;
16+
}
17+
18+
function getBalance() public view returns (uint) {
19+
return balance;
20+
}
21+
22+
23+
function play(uint guessedNumber, address _player) external payable{
24+
require(msg.value == 10**18, 'you most pay to play');
25+
require (currState == State.OPEN);
26+
player = payable(_player);
27+
balance = balance + msg.value;
28+
if (guessedNumber == secretNumber) {
29+
player.transfer(address(this).balance);
30+
currState = State.COMPLETE;
31+
balance = 0;
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import os
2+
from dotenv import load_dotenv
3+
from brownie import Wei, accounts, Guess_number
4+
5+
load_dotenv()
6+
def main():
7+
deploy_account = accounts.add(os.environ['PRIVATE_KEY_1'])
8+
deployment_details = {
9+
'from':deploy_account,
10+
'value':Wei('10 ether')
11+
}
12+
guess_number = Guess_number.deploy(9,deployment_details)
13+
return guess_number
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
from brownie import Wei, accounts, Guess_number
3+
4+
@pytest.fixture
5+
def guess_number():
6+
return Guess_number.deploy(7,{'from':accounts[0],'value':'10 ether'})
7+
8+
def test_play_wrong_guess(guess_number):
9+
pre_contract_balance = guess_number.getBalance()
10+
pre_player_balance = accounts[1].balance()
11+
guess_number.play(6,accounts[1].address, {'from':accounts[1], 'value':'1 ether'})
12+
13+
assert guess_number.getBalance() == pre_contract_balance + Wei('1 ether')
14+
assert accounts[1].balance() == pre_player_balance - Wei('1 ether')
15+
assert guess_number.currState() == 0
16+
17+
def test_play_right_guess(guess_number):
18+
pre_contract_balance = guess_number.getBalance()
19+
pre_player_balance = accounts[1].balance()
20+
guess_number.play(7,accounts[1].address, {'from':accounts[1], 'value':'1 ether'})
21+
22+
assert accounts[1].balance() == pre_player_balance + pre_contract_balance
23+
assert guess_number.currState() == 1
24+
assert guess_number.getBalance() == 0

02-Vyper-01/.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.sol linguist-language=Solidity
2+
*.vy linguist-language=Python

02-Vyper-01/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__pycache__
2+
.history
3+
.hypothesis/
4+
build/
5+
reports/

02-Vyper-01/brownie-config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
networks:
2+
default: local

02-Vyper-01/contracts/Guess_number.vy

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# @version ^0.2.0
2+
3+
#Contract Guess Number
4+
#Contract should take a secret number upon deployment.
5+
#The secret number should be within the range 0-100.
6+
#It should cost 10 eth to deploy the contract.
7+
#Contract should allow people to guess the secret number
8+
#Each guess that players make will cost 1 eth.
9+
#Whoever makes the right guess will have the balance of the contract transfer to his ethereum address.
10+
#Once the right guess is made, the contract should not allow people to play.
11+
12+
secret_number: uint256
13+
curr_balance: public(uint256)
14+
active: public(bool)
15+
16+
@external
17+
@payable
18+
def __init__(_secret_number: uint256):
19+
assert msg.value == 10*(10**18), "You should pay 10 ether to deploy"
20+
assert (_secret_number>=0) and (_secret_number<=100), "The secret number is outside the allowed range"
21+
self.secret_number = _secret_number
22+
self.curr_balance = self.curr_balance + msg.value
23+
self.active = True
24+
25+
@external
26+
@payable
27+
def play(_guessed_number:uint256):
28+
assert msg.value == 10**18, "You should pay 1 ether to play"
29+
assert self.active == True, "The contract is unfortunately already void"
30+
if _guessed_number == self.secret_number:
31+
send(msg.sender, self.balance)
32+
self.curr_balance = 0
33+
self.active = False
34+
35+
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# @version ^0.2.0
2+
3+
#Contract Guess Number
4+
#Contrac should support multiple guess the number games.
5+
#Creator of games should risk 10 ETH to start a new game.
6+
#A secret number is established during game creation
7+
#The secret number should be between 0-100.
8+
#The game creator cannot play in the games he created.
9+
#Each guess of the number in a game will cost 1 Eth.
10+
#Each game should allow only 10 guesses, once the tenth guess is made
11+
#the game will expire and the funds will go to the game creator.
12+
#Whoever makes the right guess will win the balance of the game.
13+
#Once the right guess is made a game should not allow people to play.
14+
#Once a game makes a payment 1% of the amount will go to the contract creator as a fee.
15+
16+
event Game_created:
17+
owner: indexed(address)
18+
game_index: uint256
19+
time: uint256
20+
21+
event Game_solved:
22+
solver: indexed(address)
23+
game_index: uint256
24+
time: uint256
25+
26+
struct game:
27+
game_owner: address
28+
secret_number: uint256
29+
game_balance: uint256
30+
guess_count: uint256
31+
is_active: bool
32+
33+
curr_id: uint256
34+
35+
game_index: HashMap[uint256, game]
36+
37+
contract_owner: address
38+
39+
@external
40+
def __init__():
41+
self.contract_owner = msg.sender
42+
self.curr_id = 0
43+
44+
@external
45+
@payable
46+
def create_game(_secret_number:uint256) -> bool:
47+
assert msg.value == 10*(10**18), "You should pay 10 ether to create game"
48+
assert (_secret_number >= 0) and (_secret_number <= 100), "The secret number should be within 0-100"
49+
self.game_index[self.curr_id].game_owner = msg.sender
50+
self.game_index[self.curr_id].game_balance = self.game_index[self.curr_id].game_balance + msg.value
51+
self.game_index[self.curr_id].secret_number = _secret_number
52+
self.game_index[self.curr_id].guess_count = 0
53+
self.game_index[self.curr_id].is_active = True
54+
self.curr_id = self.curr_id + 1
55+
log Game_created(msg.sender, self.curr_id - 1, block.timestamp)
56+
return True
57+
58+
@external
59+
@view
60+
def get_game_balance(_game_id: uint256) -> uint256:
61+
return self.game_index[_game_id].game_balance
62+
63+
@external
64+
@view
65+
def get_game_guesses(_game_id: uint256) -> uint256:
66+
return self.game_index[_game_id].guess_count
67+
68+
@external
69+
@view
70+
def is_game_active(_game_id: uint256) -> bool:
71+
return self.game_index[_game_id].is_active
72+
73+
@external
74+
@payable
75+
def play_game(_game_id: uint256, _guessed_number:uint256) -> bool:
76+
assert msg.value == 10**18
77+
assert msg.sender != self.game_index[_game_id].game_owner
78+
assert (_guessed_number >= 0) and (_guessed_number<= 100)
79+
assert self.game_index[_game_id].is_active == True
80+
self.game_index[_game_id].game_balance = self.game_index[_game_id].game_balance + msg.value
81+
self.game_index[_game_id].guess_count = self.game_index[_game_id].guess_count + 1
82+
if _guessed_number == self.game_index[_game_id].secret_number:
83+
send(msg.sender, (self.game_index[_game_id].game_balance * 99) / 100 )
84+
send(self.contract_owner, self.game_index[_game_id].game_balance / 100)
85+
self.game_index[_game_id].game_balance = 0
86+
self.game_index[_game_id].is_active = False
87+
log Game_solved(msg.sender, _game_id, block.timestamp)
88+
else:
89+
if self.game_index[_game_id].guess_count == 10:
90+
send(self.game_index[_game_id].game_owner, (self.game_index[_game_id].game_balance * 99)/100)
91+
send(self.contract_owner, self.game_index[_game_id].game_balance / 100)
92+
self.game_index[_game_id].game_balance = 0
93+
self.game_index[_game_id].is_active = False
94+
return True

02-Vyper-01/contracts/HelloWorld.vy

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma line
2+
# @version ^0.2.0
3+
4+
#Contract Hello World
5+
6+
#Storage variables
7+
greeting: String[79]
8+
friend: String[20]
9+
greet_friend: public(String[100])
10+
11+
12+
#Functions
13+
@external
14+
def __init__(_greeting: String[79], _friend: String[20]):
15+
self.greeting = _greeting
16+
self.friend = _friend
17+
self.greet_friend = concat(self.greeting," ",self.friend)
18+
19+
@external
20+
@payable
21+
def set_greeting(_greeting: String[79]):
22+
assert msg.value == 10**18
23+
self.greeting = _greeting
24+
self.greet_friend = concat(self.greeting," ",self.friend)
25+
26+
@external
27+
def set_friend(_friend: String[20]):
28+
self.friend = _friend
29+
self.greet_friend = concat(self.greeting," ",self.friend)
30+
31+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
from brownie import Wei, accounts, Guess_number201
3+
4+
@pytest.fixture
5+
def guess_number():
6+
guess_number = Guess_number201.deploy({'from':accounts[0]})
7+
guess_number.create_game(9, {'from':accounts[5],'value':'10 ether'})
8+
return guess_number
9+
10+
def test_wrong_guess(guess_number):
11+
pre_game_balance = guess_number.get_game_balance(0)
12+
pre_player_balance = accounts[2].balance()
13+
pre_guess_count = guess_number.get_game_guesses(0)
14+
guess_number.play_game(0,8,{'from':accounts[2],'value':'1 ether'})
15+
assert guess_number.get_game_balance(0) == pre_game_balance + Wei('1 ether'), 'The game balance is not correct'
16+
assert accounts[2].balance() == pre_player_balance - Wei('1 ether'), 'The player balance is incorrect'
17+
assert guess_number.get_game_guesses(0) == pre_guess_count + 1
18+
assert guess_number.is_game_active(0) == True
19+
20+
def test_right_guess(guess_number):
21+
pre_game_balance = guess_number.get_game_balance(0)
22+
pre_player_balance = accounts[2].balance()
23+
pre_contract_owner_balance = accounts[0].balance()
24+
guess_number.play_game(0,9,{'from':accounts[2],'value':'1 ether'})
25+
assert guess_number.get_game_balance(0) == 0, 'Balance of the game is not reset'
26+
assert accounts[0].balance() == pre_contract_owner_balance + (pre_game_balance + Wei('1 ether')) / 100, 'The commision is incorrect'
27+
assert accounts[2].balance() == (pre_player_balance - Wei('1 ether')) + (pre_game_balance + Wei('1 ether')) * 99 / 100, 'the player balance is not correct'
28+
assert guess_number.is_game_active(0) == False, 'Game status is not correct'

02A-Listener/.DS_Store

6 KB
Binary file not shown.

02A-Listener/.env

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
NODE_PROVIDER='http://127.0.0.1:7545'
2+
ADDRESS='0x7041f6Cd183516361bBbF29Ec2335CaED62efDa2'
3+
ABI_PATH='../my_fungible_token/build/contracts/hello_world_coin.json'

02A-Listener/listener.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
from web3 import Web3
3+
from dotenv import load_dotenv
4+
import time
5+
import json
6+
7+
load_dotenv()
8+
node_provider = os.environ['NODE_PROVIDER']
9+
web3_connection = Web3(Web3.HTTPProvider(node_provider))
10+
11+
def are_we_connected():
12+
return web3_connection.isConnected()
13+
14+
def set_event(contract_address, abi_path):
15+
with open(abi_path) as f:
16+
abiJson = json.load(f)
17+
contract = web3_connection.eth.contract(address=contract_address, abi=abiJson['abi'])
18+
event_of_interest = contract.events.Transfer()
19+
return event_of_interest
20+
21+
def handle_event(event, event_of_interest):
22+
receipt = web3_connection.eth.waitForTransactionReceipt(event['transactionHash'])
23+
result = event_of_interest.processReceipt(receipt)
24+
print(result)
25+
26+
def log_loop(event_filter, poll_interval, event_of_interest):
27+
while True:
28+
for event in event_filter.get_new_entries():
29+
handle_event(event, event_of_interest)
30+
time.sleep(poll_interval)
31+
32+
def listen(contract_address, abi_path):
33+
block_filter = web3_connection.eth.filter({'fromBlock':'latest','address':contract_address})
34+
log_loop(block_filter, 2, set_event(contract_address, abi_path))
35+

03-Vyper_Token/.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.sol linguist-language=Solidity
2+
*.vy linguist-language=Python

03-Vyper_Token/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__pycache__
2+
.history
3+
.hypothesis/
4+
build/
5+
reports/

03-Vyper_Token/brownie-config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
networks:
2+
default: local

0 commit comments

Comments
 (0)