To list HCX to your exchange you need to build two HCNet services as mentioned below and all the code snippets are given in the appendix section.Most importantly you have to add hashcash-sdk.jar with your project and you can download it from the below link. This jar is required to build HCX related code in your project.
a.getBalance:- For balance check. (Please Review Appendix (a)).
b.sendHCX:- For HCX send. (Please Review Appendix (b))
Before jumping into implementation we shall know what is a pool account and why it is needed?
Pool account is an account which holds all HCX balances of your customers. When somebody wants to transfer HCX from his address to another, the pool account is going to send on behalf of that customer. When sending HCX to somebody you should ask about MemoId which we have seen in our sendHCX service API. Similarly when your pool account receives HCX from others you have to give your Memoid. Because every customer has the same pool account we can differentiate the customers using MemoId. Now how do we create a poll account and all let's discuss below.
You need to create a HCX Pool Account for your exchange. You can do this by using the HCX accountviewer link as given below.
In that portal you need to generate HCX key by hitting the Generate button, after that you will get HCX Private Key which starts with S….. and HCX Public Key which starts with G… Then you can sign in that portal by your private key. Till now your address has not been activated. To activate this please go through the paragraph below.
Only you would hold the private key of this Pool Account. You can fund this Pool Account by either buying HCX from another exchange or requesting us to send HCX to your Pool Account. You can buy HCX by registering an account on PayBito exchange hosted at https://trade.paybito.com. If you have bought HCX at PayBito or another exchange, you will need to transfer the HCX from that exchange's account to your Pool Account address. You can create any number of accounts by using HCX accountviewer and transfer funds between them using sendHCX service (you only need one Pool Account for integrating HCX to your exchange).
Note:- Please do remember to activate the HCX address 20 HCX is required. After that you have to deposit more HCX to send/receive as per your requirement.
HCXNotifier notifies you when a payment is being received by your exchange pool HCX address. Mainly its job is to listen to all deposit transactions. After receiving funds by your pool account you need to identify for which customer it has received. So you can easily identify it by MemoId which needs to be sent during sendHCX API call. So in this case MemoId represents to customerid or any reference to identify your customer. After identifying Memo id you need to update customer balance. Regarding this you may have a customer table and hopefully payment_history table. In this demo we will record all receiving transactions into a table called customer_HCX_tables and this table contains address,amount,memo,transactionhash columns and when row is inserted into this table one trigger will fire to update customer balance and payment_history table. (Please review appendix C)
You can use the below code to build up HCNet services. To get hashcash-sdk.jar, use the link below and add this jar to your project.
(a) getBalance(Below code snippet shows how to check balance of a HCX address)
Note:- Above code does the send transaction and if the receiving HCX address is not being activated then you might get an error if your sending amount is less than 20. To activate the address send at least 20 HCX to the receiving address. After that send transaction can happen as usual. You may ignore the memo when your pool account is going to receive the payment for the first time. After that when send/receive will originate from that pool account then you have to specify a memo(as customer reference).
You can take a reference from below JSON request and response, how to create sendHCX api.
This project is having three .java classes as below and all are the code snippets only.
It is basically DB layer and inserts data into table.( let's say customer_HCX_details table which we have discussed earlier)
This class stores the address cursor into a file. Because if your app server has some problem and unfortunately in that period your pool address received some HCXs. So we need to start notification from the last cursor only.
This class is the main class which notifies when to receive a payment and identify the customer by MemoId and insert the record into hcx_received_table ( any name you can give) table and update the pagingtoken.txt file by latest cursor.
Note: In our above HCX Notifier code , we can say your exchange pool account receives HCX then identifies sender address,receiver address,transactional hash,timestamp,deposit amount,memo id. After that it will insert the respective details into a table and you can create a trigger or if you have some better option to update the customer balance or your payment history table if any.
*Powered by HashCash