Balancer: Tips
CIA OfficerFollowing the tips below can significantly improve the security of your integration:
- The pool contract holds the interest on swap fees and can be dynamic. This value should not be cached to save gas because the pool owner can alter it at any time!
- If pool.getOwner() returns address(0), the pool commission is static.
- It is highly inefficient if the integrating protocol only employs single swaps (Vault.swap). When integrating, the protocol should use batch swaps because they are generally cheaper than single swaps and open up more potential pairs — there is sometimes just no pool with two selected tokens.
- For pools with TWAP functionality (Weighted2Tokens and MetaStablePool) you should not use pool.getLatest()function, because it can rely on current reserves that are easily manipulated. Use pool.getTimeWeightedAverage()instead!
- Vault Queries (queryBatchSwap, queryJoin, queryExit) cannot be used to calculate limits (maximum or minimum values) in the same transaction where swap (pool entry or exit) is done, otherwise that transaction will be open to sandwich attack and query will return values based on the attacker transaction.
- Furthermore, transactions with a bigger block height can significantly affect slippage, and doing query in the same transaction with a swap will not protect against this!
- When interacting with pools via Vault, bytes32 pool_id is passed — and it’s important to remember that this is notthe pool address, converted into bytes32.
- The on-chain id must be retrieved via pool.getPoolId().
- The weights of the pool tokens are scaled by 10¹⁸. For example, a value of 0.8*10¹⁸ will be stored for a weight of 80%.
- If there are calculations with weights, you should not forget to divide by modifier 10¹⁸, if it is not reduced when dividing two weights.
- When calculating the output value at swap, the input value must include the pool commission.
- We also advise to always keep in mind what minReturn to specify when calling the function — from which token the commission is taken, at what time, and so on.