Dex
Description¶
dex.tact
Solution¶
- We first need to let the sum of the
user_balances
be 29 to setlocked
tofalse
. After unlocking, send aSolve
message when the contract balance is less than 0.5 ton to solve the challenge -
There is a loss of precision when calculating the
out_amount
. So, we may get more tokens after swapping-
Note that
amount
is serialized ascoins
, which is an alias toVarUInteger_16
. We can not send a negative value
-
-
After swapping, the contract balance will be surely above 0.5 ton. Because each swap requires sending at least 0.14 ton, and
locked
can't be set tofalse
with just one or two swaps -
We have to bypass the check and withdraw some ton from the contract.
myBalance()
returns the nanoToncoin balance of the smart contract as it was at the start of the compute phase of the current transaction. Thus, we can increase the value attached to the withdraw message. Any excess left from the incoming message after all gas costs are deducted from it will be add to the outgoing value because of the send modeSendRemainingValue
- Each transaction consists of up to 5 phases: storage phase, credit phase, compute phase, action phase and bounce phase. In the credit phase, the balance of the contract with respect to a (possible) incoming message value and collected storage fee are calculated
Exploitation¶
Before and after the swap, the sum of tokena_amount
, tokenb_amount
, user_balances(1)
and user_balances(2)
remains unchanged. So after each swap, we hope that the sum of user_balances
will increase, or at least the product of tokena_amount
and tokenb_amount
will decrease, which may result in a larger out_amount
.
swap.py
Flag¶
flag{yGNp5ttpbLnU}_Dex