Bitcoin Forum
November 06, 2024, 11:50:37 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to build a decentralized exchange (this thread actually contains a method!)  (Read 1565 times)
NikolaTesla (OP)
Newbie
*
Offline Offline

Activity: 28
Merit: 0



View Profile
April 13, 2013, 07:07:40 AM
 #1

Tired of all the threads demanding a decentralized exchange but provide no insight into how to build one? Tire no more. Here is exactly how it can be done.

First, we create a token system that has a direct 1:1 equivalence with the fiat in question. For the purposes of this thread I'll use USD, but this can be built for any currency. The token system will work exactly the same way Bitcoin works, with a blockchain to keep track of who owns what tokens. The tokens, which we can call DollarCoins for now, will for all intents and purposes be another alt currency like Bitcoin. The main difference, however, is that DollarCoins will have a set value in USD. 1 DollarCoin = 1 USD, always, no matter what. In that respect, they work exactly the same way as casino chips. A $1 casino chip can always be exchanged for $1, and $1 can always be exchanged for a $1 casino chip.

A centralized organization (this part has to be centralized, there's no other way around it) will create DollarCoins and sell them for $1 each. There is no mining involved; the centralized organization, which we'll call the House, creates them on demand whenever someone wants to buy them. They'll have a contract to always honor the DollarCoins when someone wants to cash them back to a dollar, just as a casino will always honor your chips when you want to cash out.

Once you have DollarCoins, you can send them to any address you want, just as with Bitcoins, and a blockchain system will be used to keep track of transactions and balances.

Since most end users are stupid and would be too confused by this intermediary "currency", it can be completely insulated from the end user. The end user will simply see they have "$1000" in their wallet, even though what they really have are DollarCoins, but it makes no difference, since they have equivalent value. That way, the end user, for all he knows or cares, is still exchanging Dollars for Bitcoins on the exchange. If the market is volatile and they want to sell off their Bitcoins, they can, and keep their "dollars" in their wallet. Then, when they want to transfer their "dollars" back to their bank account, they go back to the House. The House makes the exchange from DollarCoins back to real dollars and credits their bank account.

The decentralized exchange will have a blockchain-type system for keeping track of bids and asks. To participate on the exchange, you'll need a special piece of software that is essentially two wallets. One wallet for your Bitcoins, and one for your "dollars" (DollarCoins). When you place a bid, the network will confirm the bid only if you have the dollars to cover it. When you place an ask, the network will confirm the ask only if you have the Bitcoins to cover it. When you place a market buy or sell, same story; the network looks in the appropriate blockchain to see if your wallet address has the dollars (or Bitcoins) to cover the order. If the buy or sell is made, a new transaction is written to the order blockchain that updates the bids and asks. The wallet that completed the buy must automatically send DollarCoins to the wallet that did the sell (this is done through the DollarCoin blockchain). Additionally, the client wallet that completed the sell must automatically send the Bitcoins to the buying address (this must be done using the normal Bitcoin blockchain, obviously.)

So to summarize, there are 3 blockchains in total:
- The DollarCoin blockchain (for keeping track of who owns what DollarCoins)
- The Order blockchain (for keeping track of bids/asks)
- The already existing Bitcoin blockchain.

Rules for the House:
- The House must always keep enough US dollars on hand to cover the amount of DollarCoins on the market. (This is also happens to be a law for casinos in the US. They must keep enough cash on hand to cover all the playing chips on the floor.) This means the house can't just trade their own DollarCoins on the exchange for Bitcoins, unless they have the extra cash on hand to cover the extra DollarCoins they are putting out on the exchange.
- The House must always be willing to trade real dollars for dollar chips or vice versa at a 1:1 exchange. They can charge a fee to cover overhead costs. Of course, this is all insulated from the user, as stated before.
- The House should be an established organization in an industrialized western country made up of reputable people from the Bitcoin community and abide by whatever banking laws might apply in this kind of situation. The House should be completely transparent about its operations and allow public access to its records and financial statements. Perhaps it should have an organizational government that is elected by Bitcoin Foundation members.
- Obviously there can only be one house, since only they have the ability to create DollarCoins. (The House has a master address on the DollarCoin blockchain whereby they can create new DollarCoins at will.)

Rules for the decentralized exchange client:
- The client software must contain a wallet address that can send and receive Bitcoins like any other wallet. It must also be capable of sending and receiving DollarCoins to any other wallet address that is DollarCoin-compatable. (These can be two different addresses.)
- The Bitcoin part of the wallet must be unlocked while a sell order is on the books (you must enter your password to unlock the wallet in order to place a sell order.) When the sell order is filled, or cancelled, the wallet is re-locked.
- The DollarCoin portion of the wallet must be unlocked to place a buy order. When the buy is filled or cancelled, the wallet is re-locked.
- When a buy is made, the wallet automatically sends the DollarCoins to the destination address.
- When a sell is made, the wallet automatically send the Bitcoins to the destination address.

Example from start to finish:
John wires $1000 to the House so he can get started trading on the exchange. He downloads the client software needed to participate on the decentralized exchange. This client software has a wallet with a Bitcoin address and a Dollar address. (John doesn't know anything about DollarCoins, because this is obfuscated from him.) He gives his Dollar address to the House, and they send the $1000 to his wallet. This is written into the DollarCoin blockchain. He places a buy order for 5 Bitcoins on the exchange, at a bid price of $110, after entering his password to unlock his DollarCoin wallet. The other nodes confirm this is legit because the DollarCoin blockchain says John has enough funds to cover the bid.The bid gets added to the Order blockchain. Jane places a market order to sell 3 Bitcoins, which get filled by John's bid order. The Order blockchain is updated such that John now has a 2 Bitcoin bid instead of a 5, since 3 were filled. Jane's wallet sends 3 Bitcoins to John's wallet using the existing Bitcoin blockchain. John's wallet sends $330 to Jane's wallet using the DollarCoin blockchain. John cancels the remainder of his order, which gets updated in the Order blockchain, and his DollarCoin wallet re-locks.
Chet
Full Member
***
Offline Offline

Activity: 121
Merit: 100


View Profile
April 13, 2013, 07:21:28 AM
 #2

Interesting idea but it reduces the system to a single point of failure.
Your 'house' just needs to be taken out and the whole thing fails.
TierNolan
Legendary
*
Offline Offline

Activity: 1232
Merit: 1104


View Profile
April 13, 2013, 08:54:55 AM
 #3

First, we create a token system that has a direct 1:1 equivalence with the fiat in question. For the purposes of this thread I'll use USD, but this can be built for any currency. The token system will work exactly the same way Bitcoin works, with a blockchain to keep track of who owns what tokens.

You can also use coloured coins for this, so the main network can handle trading of them.  This has the advantage of not requiring merged mining and has the full support of the main network's hashing power.

The other thread does actually discuss this.

Quote
A $1 casino chip can always be exchanged for $1, and $1 can always be exchanged for a $1 casino chip.

If the casino didn't make money from gambling, then it wouldn't offer the service.  You might pay $1 + fee for a $1 token.  There might be a fee both ways.

I think having it so that it is always redeemable for $1 is better, since that becomes it's value.  However, if there is a shortage of tokens, then their price will rise until they are worth $1 + fee.

Quote
A centralized organization (this part has to be centralized, there's no other way around it) will create DollarCoins and sell them for $1 each.

This would be better handled by allowing any organisation create tokens.  Some would be trusted, some not.  The exchange rate between them would reflect how trusted they were.

Quote
Since most end users are stupid and would be too confused by this intermediary "currency", it can be completely insulated from the end user. The end user will simply see they have "$1000" in their wallet, even though what they really have are DollarCoins, but it makes no difference, since they have equivalent value.

Assuming the central authority isn't hacked or something.

With multiple authorities, users could select one as their preferred option.  The client could handle trades between (sub-)currencies too.

Quote
The decentralized exchange will have a blockchain-type system for keeping track of bids and asks. To participate on the exchange, you'll need a special piece of software that is essentially two wallets. One wallet for your Bitcoins, and one for your "dollars" (DollarCoins). When you place a bid, the network will confirm the bid only if you have the dollars to cover it. When you place an ask, the network will confirm the ask only if you have the Bitcoins to cover it.

This would mean an hour latency in trades, since you don't "have" bitcoins until 6 confirms.

Everything ideally should be on 1 blockchain so that ordering is preserved.

If coloured coins are used, then you can have a trade which is signed by both participants.  If it is included in the main bitcoin chain, then it counts, otherwise, it is fully reversed.

The buyer creates an offer transaction of the form:

Input 0: (0.000001, coloured) -> signed by BTC buyer with "SIGHASH_ANYONECANPAY + SIGHASH_SINGLE"
Output 0: (10) -> "Send to buyer's address"

This transaction is invalid, so can't be submitted to the main network.  However, the SIGHASH_ANYONECANPAY rule allows someone to add extra inputs to the transaction.  The SIGHASH_SINGLE rule allows adding of more outputs.

Input 0: (0.000001, coloured) -> signed by BTC buyer with "SIGHASH_ANYONECANPAY + SIGHASH_SINGLE"
Input 1: (10 + fee, coloured) -> signed by BTC seller (i.e. $ buyer)

Output 0: (10) -> "Send to buyer's address"
Output 1: (0.000001, coloured) -> "Send to seller's address"

It is now a valid transaction.  If the transaction makes it into the chain, then the trade occurs.  It not, then it is reversed.  It is atomic, it either fully works, or fully fails.

This system allows 2 people to trade.  It lets a buyer post "Will buy 10BTC for $1000".  However, for an exchange, you want to post "Will buy 10BTC for $1000 or market price, if lower".

Quote
Rules for the decentralized exchange client:
- The client software must contain a wallet address that can send and receive Bitcoins like any other wallet. It must also be capable of sending and receiving DollarCoins to any other wallet address that is DollarCoin-compatable. (These can be two different addresses.)
- The Bitcoin part of the wallet must be unlocked while a sell order is on the books (you must enter your password to unlock the wallet in order to place a sell order.) When the sell order is filled, or cancelled, the wallet is re-locked.
- The DollarCoin portion of the wallet must be unlocked to place a buy order. When the buy is filled or cancelled, the wallet is re-locked.
- When a buy is made, the wallet automatically sends the DollarCoins to the destination address.
- When a sell is made, the wallet automatically send the Bitcoins to the destination address.

You have no control over a client's software, all that has to happen on your block chains.

1LxbG5cKXzTwZg9mjL3gaRE835uNQEteWF
NikolaTesla (OP)
Newbie
*
Offline Offline

Activity: 28
Merit: 0



View Profile
April 13, 2013, 09:48:51 AM
 #4

You have no control over a client's software, all that has to happen on your block chains.
I didn't think of that; I suppose the client could hack their software and have it not send the coins. The network could check the bitcoin blockchain though and see if the transaction occurred before updating the order blockchain, then it would simply not confirm the buy/sell if the client hacks their software to not send coins.

Will check out the thread on colored coins.
Mike Hearn
Legendary
*
Offline Offline

Activity: 1526
Merit: 1134


View Profile
April 13, 2013, 03:05:42 PM
 #5

Please read the wiki pages on contracts and distributed exchanges. This is not a new idea:

https://en.bitcoin.it/wiki/Contracts#Example_5:_Trading_across_chains

Note that being such a backing organisation at least in most countries requires you to obtain a government license, which can be extremely expensive. And it is indeed, not decentralised. It just moves the problems around, it doesn't solve them.
juice
Newbie
*
Offline Offline

Activity: 56
Merit: 0



View Profile
April 13, 2013, 03:32:18 PM
 #6

"Once you have DollarCoins, you can send them to any address you want, just as with Bitcoins, and a blockchain system will be used to keep track of transactions and balances"


so i can pay with dollarcoins in closed loop and dont need the  bitcoin anymore Huh
jmumich
Full Member
***
Offline Offline

Activity: 209
Merit: 100


View Profile
April 13, 2013, 03:32:40 PM
 #7

I like the idea of a dollar coin or fiat coin or whatever it ends up being called.  A crypto-coin equivalent of fiat is necessary in creating a decentralized exchange.  But can it be done without a centralized "house?"

What if, for example, I can trade 1 USD for 1 USDCoin (and 1 EUR for 1 EURCoin, etc.).  Each time I trade 1 USD for 1 USDCoin I get a unique USDCoin, and that USDCoin can only be exchanged for 1 fiat USD in the physical world, but can be exchanged for other currencies through a decentralized exchange.  There would be an unlimited number of these fiat coins (as there are, theoretically unlimited fiat units). They can be created by anyone who can ensure that they are backed by a physical unit of fiat currency.  Let's call fiat coins in general FTC, but that is an overarching terms for USDCoin, EURCoin, JPYCoin, etc.    

The problem is making sure that a FTC issued is backed somewhere by a physical unit of fiat currency.  This would require something in the physical world, like an ATM, but not connected to any network.  It would just be a secure storage area for the fiat currency, and a system to issue FTC - call it a FTCATM.

For example, Alice wants to trade 100 USD for BTC.  She would first go to a FTCATM, insert her $100 in USD, and in return receive 100 USDCoins.  She can then exchange those 100 USDCoins on a p2p exchange for BTC or any other currency.  If she wants to trade out of her crypto-currency of choice, she would exchange it back to USDCoin, and then go to the FTCATM and exchange her USDCoin for fiat currency.  

The FTCATM would have to be essentially as ubiquitous as ATMs today.  But once installed they are not controlled by any central authority.  There would have to be an incentive for merchants to place these in their stores, but that should be trivial with a small fee.  There would be some centralization involved in manufacturing a FTCATM, and they would have to be extremely secure.  But there's no reason they could not be open source, and made by several different entities.

Just an idea - not sure if its been floated around, but I'd love to hear criticism, weak points, or even if it is workable.  
Sukrim
Legendary
*
Offline Offline

Activity: 2618
Merit: 1007


View Profile
April 13, 2013, 03:37:41 PM
 #8

Additionally you just described a Ripple gateway with less functionality than Ripple even in its current closed state offers.

I actually looked into such licenses and most companies with these seem to sit in Ireland or the Baltics in the Eu. Amongst them Google and Amazon btw.

Still: what does your proposed central entity do better than a simple Ripple gateway amongst potentially many others?
Also if there is already an usdcoin... and your chain would be better, cheaper, more transparent or whatnot - how do you enter that market?

https://www.coinlend.org <-- automated lending at various exchanges.
https://www.bitfinex.com <-- Trade BTC for other currencies and vice versa.
TierNolan
Legendary
*
Offline Offline

Activity: 1232
Merit: 1104


View Profile
April 13, 2013, 04:31:21 PM
 #9

so i can pay with dollarcoins in closed loop and dont need the  bitcoin anymore Huh

You still need to pay tx fees in BTCs.  Ofc, if it goes to extreme, miners could accept coloured dollar coins as fees.  You pay the coin to "true" and the first miner who gets the tx could send it to themselves.

1LxbG5cKXzTwZg9mjL3gaRE835uNQEteWF
gollum
Sr. Member
****
Offline Offline

Activity: 434
Merit: 250


In Hashrate We Trust!


View Profile
April 14, 2013, 12:23:28 AM
 #10

so i can pay with dollarcoins in closed loop and dont need the  bitcoin anymore Huh

You still need to pay tx fees in BTCs.  Ofc, if it goes to extreme, miners could accept coloured dollar coins as fees.  You pay the coin to "true" and the first miner who gets the tx could send it to themselves.

Worst case scenario: we end up with fractional reserve banking with "dollar-coins" where the mint holds 1:10 of the circulation in real dollar deposits since the mint is sure everyone wont withdraw the "dollar-coin" for real dollars at the same time.
Stampbit
Full Member
***
Offline Offline

Activity: 182
Merit: 100



View Profile
April 14, 2013, 12:27:49 AM
 #11

so i can pay with dollarcoins in closed loop and dont need the  bitcoin anymore Huh

You still need to pay tx fees in BTCs.  Ofc, if it goes to extreme, miners could accept coloured dollar coins as fees.  You pay the coin to "true" and the first miner who gets the tx could send it to themselves.

Worst case scenario: we end up with fractional reserve banking with "dollar-coins" where the mint holds 1:10 of the circulation in real dollar deposits since the mint is sure everyone wont withdraw the "dollar-coin" for real dollars at the same time.

we have that, its called freicoin.
TierNolan
Legendary
*
Offline Offline

Activity: 1232
Merit: 1104


View Profile
April 14, 2013, 12:34:12 AM
 #12

Worst case scenario: we end up with fractional reserve banking with "dollar-coins" where the mint holds 1:10 of the circulation in real dollar deposits since the mint is sure everyone wont withdraw the "dollar-coin" for real dollars at the same time.

However, they do risk hyper-transfer.  Swapping from one mint to the other is easy, so the exchange rate would collapse.  No central authority could halt the trade and call a bank holiday.

1LxbG5cKXzTwZg9mjL3gaRE835uNQEteWF
wiggi
Sr. Member
****
Offline Offline

Activity: 403
Merit: 251


View Profile
April 14, 2013, 07:17:40 PM
 #13

So to summarize, there are 3 blockchains in total:
- The DollarCoin blockchain (for keeping track of who owns what DollarCoins)
- The Order blockchain (for keeping track of bids/asks)
- The already existing Bitcoin blockchain.

Perhaps a decentralized exchange between existing blockchains (Btc, Order blockchain, Ltc) should come first.
It wouldn't solve the problem (except to take some load from btc-e  Grin
but proof of concept for the software part could convince some large online retailers to
become house and sell DollarCoin-casinochips.
nwbitcoin
Sr. Member
****
Offline Offline

Activity: 294
Merit: 250


You are a geek if you are too early to the party!


View Profile WWW
April 14, 2013, 09:07:40 PM
 #14

Sorry to appear negative, but this doesn't seem solve anything!

All you are suggesting is that rather than have a MTGox, we have a house selling edollars before you get entry into the bitcoin market.

It doesn't solve the problem of the house being ddos'd and doesn't solve the problem of getting fiat into bitcoins any better.

Have I missed something?


*Image Removed*
I use Localbitcoins to sell bitcoins for GBP by bank transfer!
misterbigg
Legendary
*
Offline Offline

Activity: 1064
Merit: 1001



View Profile
April 14, 2013, 10:43:11 PM
 #15

Why not just use Ripple?
gollum
Sr. Member
****
Offline Offline

Activity: 434
Merit: 250


In Hashrate We Trust!


View Profile
April 14, 2013, 11:17:33 PM
 #16

Why not just use Ripple?

Ripple != opensource && free
Sukrim
Legendary
*
Offline Offline

Activity: 2618
Merit: 1007


View Profile
April 14, 2013, 11:46:37 PM
 #17

The system proposed here works like a limited version of Ripple, is even less open and requires more centralized authorities.

No value transfer system will ever be completely free, so that "requirement" is out of the question. Ripple will hopefully be open source sooner than later, until then it might make sense to learn a bit more about how it works instead of calling for source code that won't help anyways, as setting up a server would only mean you have do deal with hard forks a few times per week.

https://www.coinlend.org <-- automated lending at various exchanges.
https://www.bitfinex.com <-- Trade BTC for other currencies and vice versa.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!