1

Imagine that I am building a smart contract which simulates a lottery. I would like people to enter the lottery by buying a ticket (i.e. they would send in a certain amount of ether when they call a specific function on my contract).

Can I implement the purchase functionality so that a ticket always costs the equivalent of $2 USD? This doesn't seem straightforward given that the value of ether is constantly changing.

Wanting to have something cost a set price seems like it would occur frequently so I'm wondering if there is a standard way to implement it.

Note: I am just starting out with Ethereum/Solidity so I apologize if this is a very basic question.

4

The USD/ETH price is not intrinsically available on the blockchain. Therefore you need services such as Oraclize for pulling price feeds to your contract. Specifically, you could use the Kraken ticker API and use a simple Oraclize query to integrate that data into your contract. Beware that this is significantly more complicated than just hard-coding one rate, you might want to take into consideration edge cases such as Oraclize not sending a price update in time etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.