By doing a factory reset and formatting the SD cards, I would say that it is safe enough to sell. If you want to be perfectly sure, flashing the original ROM with option of wiping the cache will make the phone as good as you got it when you purchased. Of course, still you have to format the SD card in that case as well.
To know how to flash the original ROM for you LG Optimus GT540, you can visit this page, which has a step by step instructions about this process. You should be able to find similar such page for your other Samsung device just with Google search.
Update: I found this web page, which explains about to how to downgrade i5800 to 2.1. By that process you can get the original ROM (watch out for the region) and safely sell.