|
| 1 | +#Counterparty Gateway Prototype |
| 2 | + |
| 3 | +This is a PHP based automated "vending machine" or "gateway" prototype/proof of concept for the Counterparty (XCP) protocol. |
| 4 | + |
| 5 | +A Counterparty "Vending Machine" is a program which accepts one type of asset (BTC, XCP or other user created assets) and spits |
| 6 | +out another asset using a defined exchange rate. For example, sending 0.01 BTC and receiving back 1000 LTBcoin. |
| 7 | + |
| 8 | +This prototype is intended for command line use only, there is no GUI or web interface at this moment. |
| 9 | + |
| 10 | +##Features |
| 11 | + |
| 12 | +* Send and receive both bitcoin and counterparty assets based on defined exchange rates |
| 13 | +* Accept multiple tokens at different exchange rates |
| 14 | +* Ability to automatically issue new tokens when the local supply runs out (requires source address to have ownership of the token in question) |
| 15 | +* Customizable fees and other options |
| 16 | + |
| 17 | +##Requirements |
| 18 | + |
| 19 | +* PHP 5.4 |
| 20 | +* MySQL |
| 21 | +* Python 3.4 |
| 22 | +* [Bitcoind](https://github.com/bitcoin/bitcoin) |
| 23 | +* [Counterpartyd](https://github.com/CounterpartyXCP/counterpartyd) |
| 24 | +* Your favorite flavour of linux! |
| 25 | + |
| 26 | +##Installation |
| 27 | + |
| 28 | +* Make sure you have an instance of both Bitcoind and Counterpartyd fully operational |
| 29 | +* Create a new MySQL database and import "db.sql" |
| 30 | +* Edit conf/config.php and fill out all the appropriate information |
| 31 | + |
| 32 | +To setup your vending machine, you will first want to edit "crypto-gateway.php" (this process will be improved later) |
| 33 | + |
| 34 | +Specifically, you will want to edit this section: |
| 35 | + |
| 36 | +``` |
| 37 | + $gateway = new Crypto_Gateway('1CoinEXLQtivDjckWMRBbhgVJk9RdwdYbR', 'BITCOINEX', array('BTC' => 1, 'XBTC' => 1)); |
| 38 | + $gateway->allow_two_way = true; |
| 39 | + $gateway->auto_inflate = true; |
| 40 | + $gateway->inflation_mode = 'as_needed'; |
| 41 | + $gateway->inflation_modifier = 1; |
| 42 | +``` |
| 43 | + |
| 44 | +Let's break this down line by line. |
| 45 | + |
| 46 | +``` |
| 47 | + $gateway = new Crypto_Gateway('1CoinEXLQtivDjckWMRBbhgVJk9RdwdYbR', 'BITCOINEX', array('BTC' => 1, 'XBTC' => 1)); |
| 48 | +```` |
| 49 | +
|
| 50 | +The first argument in this function is your **source address**, which is the address that outgoing transactions will be coming from. If you want it to automatically issue new tokens for you, you will need to make sure that the source address also has ownership rights to your desired token. |
| 51 | +
|
| 52 | +The second argument is the name of the primary token used by the vending machine, and can be either BTC, XCP or any Counterparty token. This is the main token that will be sent out after incoming transactions are confirmed. |
| 53 | +
|
| 54 | +The third argument is an array of which tokens are accepted and what the exchange rate is for them. In this default example, both regular BTC as well as the token "XBTC" are accepted for incoming transactions, with both of them having an equal 1:1 exchange rate. If ```'BTC' => 1 ``` was changed to ```'BTC' => 1000```, then sending 1 Bitcoin would cause 1,000 BITCOINEX to be vended out. |
| 55 | +
|
| 56 | +If you would like to have different addresses for receiving and sending transactions, you may add in the following line of code: |
| 57 | +``` $gateway->watch_address = 'MY_WATCH_ADDRESS'; ``` |
| 58 | +
|
| 59 | +... |
| 60 | +
|
| 61 | +``` |
| 62 | +$gateway->allow_two_way = true; |
| 63 | +``` |
| 64 | +
|
| 65 | +If set to true, the vending machine will allow the **primary token** to be sent back and exchanged back to one of the accepted tokens. The first item in the array of accepted tokens is what will be sent back (in this example, BTC). Note that the service fee (if any defined) is only applied one way (e.g a fee is taken when vending BTC -> BITCOINEX, but no fee is taken with BITCOINEX -> BTC) |
| 66 | +
|
| 67 | +``` |
| 68 | + $gateway->auto_inflate = true; |
| 69 | + $gateway->inflation_mode = 'as_needed'; |
| 70 | + $gateway->inflation_modifier = 1; |
| 71 | +``` |
| 72 | +
|
| 73 | +This section defines the auto issuance behavior. If you do not want the vending machine to attempt to issue new tokens when its supply runs out, just set "auto_inflate" to false. Auto issuance is triggered when there is not enough tokens left in the source address to satisfy a new vending transaction. |
| 74 | +
|
| 75 | +There are three inflation modes available (which also change what "inflation_modifier" does): |
| 76 | +
|
| 77 | +* "as_needed" - this mode creates only as many tokens as needed for whatever pending outgoing transactions, multiplied by the inflation_modifier. e.g 1,000 new tokens are needed, so only 1,000 new tokens are created. |
| 78 | +* "fixed" - this creates a fixed amount of new tokens. If inflation_modifier was set to 50,000, it would simply create 50,000 new tokens |
| 79 | +* "percent" - this increases the total existing token supply by X percent (inflation_modifier). e.g if inflation_modifier is set to 0.10 (10%) and there is already 100,000 tokens out in the wild, then an additional 10,000 would be issued. |
| 80 | +
|
| 81 | +**Other Options** |
| 82 | +
|
| 83 | +Note that some of these options are not actually used yet (will fix soon) |
| 84 | +
|
| 85 | +``` |
| 86 | + $gateway->min_btc = 0.000055; //BTC dust limit, used mostly for if source token is just BTC |
| 87 | + $gateway->min_confirms = 1; //Min number of confirms before vending out token |
| 88 | + $gateway->miner_fee = 0.0001; //BTC miners fee |
| 89 | + $gateway->dust_size = 0.000055; //size to use for each dust output |
| 90 | + $gateway->service_fee = 0.5; //% fee to take off any incoming tokens |
| 91 | + |
| 92 | +``` |
| 93 | +
|
| 94 | +
|
| 95 | +##Usage |
| 96 | +
|
| 97 | +To start up the gateway/vending machine: |
| 98 | +
|
| 99 | +``` |
| 100 | +php crypto-gateway.php > log & |
| 101 | +``` |
| 102 | +``` > log &``` simply saves the output to a log file and runs the process in the background. |
| 103 | +
|
| 104 | +To view some basic stats on incoming/outgoing payments for each token used in the vending machine, run: |
| 105 | +
|
| 106 | +``` |
| 107 | +php crypto-gateway.php stats |
| 108 | +``` |
| 109 | +
|
| 110 | +If you want to send a transaction to your source/watch address (such as adding funds to pay for bitcoin fees, xcp for issuance fees, sending initial token supply etc.), but don't want to accidently trigger the gateway, you may add specific transactions to the "ignore list" by running the following command: |
| 111 | +
|
| 112 | +``` |
| 113 | +php crypto-gateway.php ignore <transaction ID> |
| 114 | +``` |
| 115 | +
|
| 116 | +**Running Multiple Gateways** |
| 117 | +
|
| 118 | +As this is mostly a proof of concept still, it hasn't had much testing or been fleshed out much. However, you should be able to create copies of "crypto-gateway.php" and change parameters etc. as needed and run them all simultaneously. They can all use the same database. |
| 119 | +
|
0 commit comments