script (⏱,💰)

script (⏱,💰)

NG#5 - Overflow

Approach#

In Cairo, the felt type has a range, and if it exceeds that range, it will overflow. In this problem, the amount needs to be passed as a number greater than 1000000000000000, multiplied by another number 6007, so that when the calculated value overflows, it equals the balance. The balance starts at 0, and each call to claim_coin can only increase the balance by 1.

The adjacent walkthrough provides a detailed explanation. Cairo's division can calculate overflow, and it can be used with extern fn felt252_div( lhs: felt252, rhs: NonZero<felt252> ) -> felt252 nopanic; to get the answer.

If the balance is 1, try 1/6007 to get the amount. If the amount is greater than 1000000000000000, the goal is achieved. If not, try 2/6007...

Write a local test to calculate the required amount.

Then use starkli invoke [contract] claim_coin to make the balance become 1.
Finally, use starkli invoke [contract] buy_incense [calculated amount] to successfully solve the problem.

Summary#

This problem is very simple, mainly understanding the characteristics of felt252. In actual development, when it comes to mathematical operations, it is generally recommended to use integer types like u256 instead of felt252.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.