Fun with Smart Contract Lottery... #79
Unanswered
toddabraham
asked this question in
Q&A
Replies: 1 comment 2 replies
-
can you please be more clear on what you are missing? Your test is failing since the oracle is returning a different conversion from the one provided in the tutorial, since Ether value fluctuates... this means, that you can't actually test this with just a simple asserting, you need to get the conversion from the oracle in your python script, and then check with an assertion: dollars = 50
contract_result = contract.getConversionRate() # or whatever method is returning you the entrance fee
c = interface.IAggregatorV3("address of the aggregator in the network you are using for eth to usd")
(
roundId,
answer,
startedAt,
updatedAt,
answeredInRound
) = c.latestRoundData()
# x = answer / (10 ** c.decimals()) --- will give you 25XX.XX$ per eth
# y = 1 / x --- will give you 0.00NNN eth per $
# z = y * dollars --- will return the number of eth for each dollar
# Web3.toWei(z, "ether") --- will return the number of wei you should be paying
supposed_result = Web3.toWei((1 / answer / (10 ** c.decimals())) * dollars, "ether")
assert contract_result == supposed_result However, It's very not recommended checking for equality when dealing with floating point numbers... check this StackOverflow page to know how to do this |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently running a lottery unit test on
def test_get_entrance_fee
and noticed, when checking the conversion of USD$50 to eth, it's currently 0.016 instead of 0.025 as per the recording. The test of course failed when I tried to replaceexpected_entrance_fee = Web3.toWei(0.025, "ether")
withexpected_entrance_fee = Web3.toWei(0.016, "ether")
. My problem is, I wasn't able to fully trace back to where in the chain of code, between the Lottery.sol file and supporting script files, should I make the change.It's of course, not really necessary for me to do this in order to continue on with the course, but I thought it a good exercise to test my understanding.
Beta Was this translation helpful? Give feedback.
All reactions