Can you use Trader to gain a Silver instead of a Farmland, but still get the buy side-effects of Farmland?
On buy effects are resolved before on gain effects. So, you would buy the Farmland and follow it's on-buy effect of trashing a card, etc. Then, you are going to gain the Farmland. At this point, you can reveal the Trader to gain the Silver instead of the Farmland.

Your algorithm is correct, and your assumptions about Smugglers and Treasury are correct.