Goldman Sachs should have been wary of Ecuadorians bearing gold.

On Tuesday, Ecuador announced that it had swapped 1,165 bars of gold with Goldman. The gold is worth nearly $600 million, based on current prices. Goldman GS , in return, is giving the Ecuadorians “instruments of high security and liquidity,” which is likely cash or something close to it.

And Ecuador gets to keep its gold. As part of the deal, three years from now, the two will reverse the swap. Ecuador gets its gold back. And Goldman gets the going price for 1,165 yellow bars in 2017.

There are a number of reasons a country would do a gold swap with Wall Street. A nation could be desperate for cash, but doesn’t want to seem like it. Ecuador’s deficit is expected to reach a record $5 billion this year. Countries around the world keep piles of gold in their vaults to prove that their economies, and their currencies, are solvent. The problem is piles of gold won’t buy you goods and services for your countrymen, at least not directly. You need cash for that.

You can get cash by selling your gold. But selling your gold will make your lenders, trading partners, and citizens nervous. And Ecuador really doesn’t want to do that. Instead, Ecuador is just swapping its gold. Nothing to see here.

But that’s not the reason Ecuador says it is doing the swap. No, it says it’s doing it because it has found a new way to make money.

“Gold that was not generating any returns in vaults, causing storage costs, now becomes a productive asset that will generate profits,” the Ecuadorian central bank said in a statement when it announced the deal. “These interventions in the gold market represent the beginning of a new and permanent strategy of active participation by the bank, through purchases, sales and financial operations, that will contribute to the creation of new financial investment opportunities.”

Sounds good, unless of course you know anything about the recent history of Wall Street’s swap deals. Cities around the country have been trying to get out of interest rate swaps they signed with Wall Street banks back in 2005 and 2006 that blew up during the financial crisis. Detroit has had to pay hundreds of millions of dollars to unwind the disastrous swaps it inked with big banks, helping propel that city into bankruptcy. But Wall Street’s most notorious swap deal is the one Goldman did with Greece, which helped that debt-strapped country hide its problems for nearly a decade. That deal ended up costing the Greeks about $1 billion and created a huge payday for Goldman.

That fiasco led many governments to grow skeptical of “financial operations” that Wall Street promises will create new investment “opportunities.”

Last year, Venezuela, also somewhat cash-strapped, considered doing a similar gold swap, also with Goldman. But that nation eventually concluded that it was a bad deal and backed out.

Ecuador, though, thinks otherwise. On the day the country announced the deal, Ecuador also said it expects to make $16 million to $20 million on the trade. Take that, Wall Street. Main Street, albeit in Ecuador, wins for once.

The problem is it’s not quite clear how Ecuador could actually end up making $20 million on its gold swap. A gold swap is pretty close to a collateralized loan. Goldman is essentially lending Ecuador money for three years, and taking its gold as collateral. Typically, you pay interest or a fee for a loan like that. Venezuela said its $1.8 billion gold swap was going to cost $800 million in financing over seven years. Based on that, Ecuador could be paying Goldman a little over $100 million for its swap. On top of that, Ecuador would end up owing Goldman even more money if the price of gold rises over the next three years.

But there are a few ways Ecuador could end up making $20 million on the deal. Ecuador could be betting that the price of gold will fall 20% over the next three years. Another way would be for Ecuador to take the cash it gets from the swap and invest it elsewhere in something that will make 20% over the next three years, not an easy task given that the average bond is yielding just 1.8%.

Or Ecuador could have done another swap, this time with the dollars it received from Goldman for Ecuadorian bonds. It would, of course, have to pay Goldman another fee for that. But that swap would pay the spread between U.S. three-year interest rates at about 0.85%, and Ecuadorian interest rates, which are around 7% or 8%, or a little over 20% during the three years.

So, then, why isn’t everyone doing Ecuadorian swaps? I mean, it’s guaranteed money. For one, the fee that Goldman or anyone else charges to do these deals must be pretty high. Also, if Ecuador defaults, you lose all your money, and you still have to repay the swap.

And Ecuador has recently defaulted, sort of. Five years ago, Ecuador tricked its lenders into thinking it was going to default on its debt. It told banks and investors that it didn’t think it would be able to repay about $3 billion in bonds. Those bonds, as you would expect, plunged in value to about $0.30 for every dollar Ecuador had borrowed.

Once the bonds plunged in value, Ecuador started buying them, eventually snapping up 90% of the much cheaper bonds. The move wiped out the debt and saved the country nearly $2 billion, which was also how much the banks and investors lost on those bonds.

This was a great trade for Ecuador. Unless, of course, it ever wanted to borrow again. Now that Ecuador is projecting a record deficit, it has to. Ecuador recently announced plans to sell about $700 million in government bonds.

You would expect banks and investors to be reluctant to purchase those bonds, given Ecuador’s recent history. One thing, though, that could persuade investors to buy in is if Ecuador can show that it just got a bunch of cash from Goldman.

Of course, one thing I might ask if I were a potential investor is, “Do I really want to be lending to a country that is making $600 million bets on the price of gold, writing complicated swaps, and generally embarking on a plan to pursue ‘new financial investment opportunities,’ essentially turning its central bank into a nationalized hedge fund?” I mean, that sounds kind of risky.

Unless, of course, that nationalized hedge fund knows how to out-trade Goldman Sachs. Then I’m in.