Python 3.9.1 Brownie v1.17.0 - 以太坊的 Python 开发框架
这是我的合同
# @version ^0.3.0
#Contract Guess Number
#Contract should support multiple guess the number games.
#Creator of games should rish 10 ETH to start a new game.
#A secret number is established during game creation
#The secret number should be between 0-100.
#The game creator cannot play in the games he created.
#Each guess of the number in a game will cost 1 ETH.
#Each game should allow only 10 gusses, once the tenth guess is made
#the contract will expire and the funds will go to the game creator.
#Whoever makes the right guess will win the balance of the game.
#Once the right guess is made a game should not allow people to play.
#Once a game makesa payment 1% of the amount will go to the contract creator as a fee.
struct game:
game_owner: address
secret_number: uint256
game_balance: uint256
guess_count: uint256
is_active: bool
curr_id: uint256
game_index: HashMap[uint256, game]
contract_owner: address
@external
def __init__():
self.contract_owner = msg.sender
self.curr_id = 0
@external
@payable
def create_game(_secret_number: uint256) -> uint256:
assert msg.value == 10*10**18, "You must pay 10 ETH to create game." # 10 ETH
assert (0 <= _secret_number) and (_secret_number <= 100)
self.game_index[self.curr_id].game_owner = msg.sender
self.game_index[self.curr_id].game_balance = self.game_index[self.curr_id].game_balance + msg.value
self.game_index[self.curr_id].secret_number = _secret_number
self.game_index[self.curr_id].guess_count = 0
self.game_index[self.curr_id].is_active = True
self.curr_id = self.curr_id + 1
return self.curr_id - 1
@external
@view
def get_game_balance(_game_id: uint256) -> uint256:
return self.game_index[_game_id].game_balance
@external
@view
def get_game_guesses(_game_id: uint256) -> uint256:
return self.game_index[_game_id].guess_count
@external
@view
def is_game_active(_game_id: uint256) -> bool:
return self.game_index[_game_id].is_active
@external
@payable
def play_game(_game_id: uint256, _guess_number: uint256)->bool:
assert msg.value == 10**18, "Pay 1 ETH to play this" # 1 ETH
assert msg.sender != self.game_index[_game_id].game_owner, "Game owner can't play this game dude!"
assert self.game_index[_game_id].is_active == True
self.game_index[_game_id].game_balance = self.game_index[_game_id].game_balance + msg.value
self.game_index[_game_id].guess_count = self.game_index[_game_id].guess_count + 1
if _guess_number == self.game_index[_game_id].secret_number:
send(msg.sender, (self.game_index[_game_id].game_balance * 99)/100)
send(self.contract_owner, (self.game_index[_game_id].game_balance)/100)
self.game_index[_game_id].game_balance = 0
self.game_index[_game_id].is_active = False
else:
if self.game_index[_game_id].guess_count == 10:
send(self.game_index[_game_id].game_owner, (self.game_index[_game_id].game_balance * 99)/100)
send(self.contract_owner, (self.game_index[_game_id].game_balance)/100)
self.game_index[_game_id].game_balance = 0
self.game_index[_game_id].is_active = False
return True
这是测试文件
import pytest
from brownie import Wei, accounts, Guess_number201
@pytest.fixture
def guess_number():
guess_number = Guess_number201.deploy({'from': accounts[0]})
guess_number.create_game(9, {'from': accounts[1], 'value': "10 ether"})
return guess_number
def test_wrong_guess(guess_number):
pre_game_balance = guess_number.get_game_balance(0)
pre_player_balance = accounts[2].balance()
pre_guess_count = guess_number.get_game_guesses(0)
guess_number.play_game(0, 8, {'from': accounts[2], 'value': "1 ether"})
assert guess_number.get_game_balance(0) == pre_game_balance + Wei('1 ether'), "The game balance is not correct"
assert accounts[2].balance() == pre_player_balance - Wei('1 ether'), "The player balance is incorrect"
assert guess_number.get_game_guesses(0) == pre_guess_count + 1
assert guess_number.is_game_active(0) == True
brownie test
并得到了错误
def test_wrong_guess(guess_number):
pre_game_balance = guess_number.get_game_balance(0)
pre_player_balance = accounts[2].balance()
pre_guess_count = guess_number.get_game_guesses(0)
guess_number.play_game(0, 8, {'from': accounts[2], 'value': "1 ether"})
> assert guess_number.get_game_balance(0) == pre_game_balance + Wei('1 ether'), "The game balance is not correct"
E AssertionError: The game balance is not correct
E assert 10000000000000000000 == (10000000000000000000 + 1000000000000000000)
E + where 10000000000000000000 = <ContractCall 'get_game_balance(uint256 _game_id)'>(0)
E + where <ContractCall 'get_game_balance(uint256 _game_id)'> = <Guess_number201 Contract '0x5Ec3c43b563E6CC34cfA370c90BE10e7bC62a7d6'>.get_game_balance
E + and 1000000000000000000 = Wei('1 ether')
问题: 如何用 vyper 做正确的断言?