> For the complete documentation index, see [llms.txt](https://calnix.gitbook.io/eth-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnix.gitbook.io/eth-dev/smart-contract-security/damn-vulnerable-defi/8.-puppet.md).

# 8. Puppet

https\://www\.damnvulnerabledefi.xyz/challenges/puppet/

#### Pricing of Tokens&#x20;

The price of dvt tokens is determined by the ratio in the uniswap pool. Calculated as follows:

* ETH in pool / dvt in Pool = price of 1 dvt in wei
* \_computeOraclePrice in PuppetPool.sol does this

<figure><img src="https://1628544884-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTgomzlmn9NrxUY0OQ3cD%2Fuploads%2FwnemCxAzC2Xu53WleTMP%2Fimage.png?alt=media&amp;token=f3dab9cf-7f9d-4dd6-a699-0338b54b5ac5" alt=""><figcaption></figcaption></figure>

However, the pool only allows a user to borrow tokens, if they deposit **twice** their value in ETH. Essentially, overcollateralized lending.

Hence, the deposit required calculation is as follows:

<figure><img src="https://1628544884-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTgomzlmn9NrxUY0OQ3cD%2Fuploads%2FtubigpKzNfM2ECanD3LA%2Fimage.png?alt=media&amp;token=308fd11c-8692-4046-83e6-363953740959" alt=""><figcaption></figcaption></figure>

If user wants to borrow 10 tokens (w/ each token worth 1 ether), he needs to deposit (10\*2) ether.

### Approach

1. Attacker to sell all of his tokens into Uniswap pool for ether -> skews pools ratio, thereby discounting token price heavily
2. Calculate collateral required to borrow 1 token from pool at new token prices
3. Based on collateral required, calculate how much attacker can borrow given his ether balances
4. In this case, attacker can borrow much more than the pool has to offer, so we would borrow everything the pool has in dvt tokens.&#x20;
5. Calculate how much ether is require for this borrow, to send as msg.value

   1. Alternatively, attacker can just send all his ether balances, since the borrow function does return the excess ether.

   <figure><img src="https://1628544884-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTgomzlmn9NrxUY0OQ3cD%2Fuploads%2FKH7h4y2OYEsMF9oaAKBX%2Fimage.png?alt=media&amp;token=79707c4c-118c-4a82-9d47-5a3d96c5d412" alt=""><figcaption></figcaption></figure>
