How To Get Confirmation From Blockchain For BTC Transactions

Posted By : Pawan Kumar Tiwari | 30-Apr-2018

Blockchain technologies are the most rapidly increasing technologies in these days. So, if you're working on it then you need to have a good knowledge of its API's and usage. The main thing is when you're receiving a transaction from blockchain, you're not aware of when your transaction is being completed. 

However, there are some methods that allow you to do the same and fulfill your requirement. Only if you're fully aware of its API and usage. 

In this article, I'll help you to get the notification for the incoming transaction. 

Let's create a demo project for the same, 

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
public class WalletNotifyTestApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(WalletNotifyTestApplication.class, args);
	}
	
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(WalletNotifyTestApplication.class);
	}
	
	
	@Bean
	BitcoinJSONRPCClient getBitcoindConnection() throws MalformedURLException, BitcoinRpcException {
		String user = "username";
		String password = "password";
		String host = "127.0.0.1";
		String port = "18332";

		URL url = new URL("http://" + user + ':' + password + "@" + host + ":" + port + "/");
		BitcoinJSONRPCClient bitcoinClient = new BitcoinJSONRPCClient(url);		
		System.out.println("Connection has been made to bitcoind running in url {} and port {}.Here is client info {}"+ host+ port +bitcoinClient.getWalletInfo());
		System.out.println("Pawan");
		return bitcoinClient;
	}
}

Now, create a Rest controller :

@RestController
@RequestMapping(URLMapping.API_PATH)
public class WalletNotificationController {
	@Autowired
	private BitcoinService bitcoinService;
	
	@RequestMapping(value = URLMapping.GET_ADDRESS, method = RequestMethod.GET)
	public String address () {
		String add = bitcoinService.getAddress();
		System.out.println(add);
		return add;
	}
	
	@RequestMapping(value=URLMapping.SAVE_BITCOIN_RECEIVE_TRANSACTION,method=RequestMethod.POST)
	void saveBitcoinReceiveTransaction(@RequestBody String data){
		String transactionHash = data.replace("txid","").replace("=", "");
		//String transactionHash1 = transactionHash.replace("=","");
		System.out.println("transactionHash  ::::"+transactionHash);
		try {
			bitcoinService.saveReceiveTransaction(transactionHash);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}

Now create a service to fullfill the request. 

	
	@Service
public class BitcoinService {
	private Logger log = LoggerFactory.getLogger(BitcoinService.class);
	/*@Autowired
	private BitcoinAddressRepository bitcoinAddressRepository;
	@Autowired 
	private TransferRepository transferRepository;*/
	@Autowired
	private BitcoinJSONRPCClient bitcoinClient;
	
	public void saveReceiveTransaction(String transactionHash) throws MalformedURLException, BitcoinRpcException{
		TransactionDTO transactionDTO = getReceiveTransactionInfo(bitcoinClient, transactionHash);
		transactionDTO.getAmount();
		if(transactionDTO.getConfirmations()>0){
			List<DetailsDTO> details = transactionDTO.getDetails().stream().filter(detail -> detail.getCategory().equals("receive")).collect(Collectors.toList());
			// We know the above details will have only one entry but still we will not use findFirst in stream 
			if(!details.isEmpty()){
				//Do your stuff here.
				}
				
			}
		}
	}
	
	/********** private method  *****************/
	
	private TransactionDTO info(BitcoinJSONRPCClient bitcoinClient,String txHash){
		Object object = bitcoinClient.query("gettransaction",txHash);
		ObjectMapper mapper = new ObjectMapper();
		TransactionDTO transactionDTO = mapper.convertValue(object, TransactionDTO.class);
		return transactionDTO;
	}
	
	}

Now you're ready to receive the transaction information for the incoming transaction. I hope, this helps. 

About Author

Author Image
Pawan Kumar Tiwari

Pawan is a passionate coder and a part time blogger. He loves to travel and learn new technologies in his leisure time.

Request for Proposal

Name is required

Comment is required

Sending message..