Submitted by Vagner
The function _sendToken is called on rebalance to perform the rebalance operation by the owner which will transfer native token or the underlying ERC20 for a specific tOFT token to other chains. This function uses the router from Stargate to transfer the tokens, but the implementation of the swap is done wrong which will make the tokens to be lost.
_sendToken calls Stargates router swap function with the all the parameters needed as can be seen here https://github.com/Tapioca-DAO/tapiocaz-audit/blob/bcf61f79464cfdc0484aa272f9f6e28d5de36a8f/contracts/Balancer.sol#L322-L332, but the problem relies that the destination address is computed by calling abi.encode(connectedOFTs[_oft][_dstChainId].dstOft) instead of the abi.encodePacked(connectedOFTs[_oft][_dstChainId].dstOft) https://github.com/Tapioca-DAO/tapiocaz-audit/blob/bcf61f79464cfdc0484aa272f9f6e28d5de36a8f/contracts/Balancer.sol#L316-L318.
Per Stargate documentation https://stargateprotocol.gitbook.io/stargate/developers/how-to-swap , the address of the swap need to casted to bytes by using abi.encodePacked and not abi.encode, casting which is done correctly in the _sendNative function https://github.com/Tapioca-DAO/tapiocaz-audit/blob/bcf61f79464cfdc0484aa272f9f6e28d5de36a8f/contracts/Balancer.sol#L291 . The big difference between abi.encodePacked and abi.encode is that abi.encode will fill the remaining 12 bytes of casting a 20 bytes address with 0 values. Here is an example of casting the address 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
This will hurt the whole logic of the swap since when the lzReceive function on the Bridge.sol contract from Startgate will be called, the address where the funds will be sent will be a wrong address. As you can see here the lzReceive on Bridge.sol for Abitrum for example uses assembly to load 20 bytes of the payload to the toAddress https://arbiscan.io/address/0x352d8275aae3e0c2404d9f68f6cee084b5beb3dd#code#F1#L88
which in our case, for the address that I provided as an example it would be
because abi.encode was used instead of abi.ecnodePacked.
Then it will try to swap the tokens to this address, by calling sgReceive on it, which will not exist in most of the case and the assets will be lost, as specified by Stargate documentation https://stargateprotocol.gitbook.io/stargate/composability-stargatecomposed.sol
Use abi.encodePacked instead of abi.encode on _sendToken, same as the protocol does in _sendNative, so the assumptions will be correct.
0xRektora (Tapioca) confirmed
