GithubHelp home page GithubHelp logo

aaron-akhtar / blockonomics-java-wrapper Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 4.0 59 KB

This is a Java Wrapper of the Blockonomics RESTful API https://www.blockonomics.co/views/api.html

Java 100.00%
blockonomics bitcoin bitcoin-payment bitcoin-wallet cryptocurrency java-wrapper

blockonomics-java-wrapper's Introduction

Blockonomics-Java-Wrapper

This is a Java Wrapper of the Blockonomics RESTful API https://www.blockonomics.co/views/api.html

Example Callback URL when using the Callback Server:

http://%YOUR_PUBLIC_IPv4_HERE%:%YOUR_PORT_HERE%/testing?secret=%YOUR_SECRET_KEY_HERE%

An Example Link:

http://1.2.3.4:8080/api1?secret=myRandomSecretKey

Be sure to place said link in your blockonomics merchant store. https://www.blockonomics.co/merchants#/stores

Examples -

Basic implementation of the base functionality:

package me.aaronakhtar.blockonomics_wrapper;

import me.aaronakhtar.blockonomics_wrapper.objects.BitcoinAddress;
import me.aaronakhtar.blockonomics_wrapper.objects.BitcoinAddressHistory;
import me.aaronakhtar.blockonomics_wrapper.objects.transaction.ConfirmedTransaction;
import me.aaronakhtar.blockonomics_wrapper.objects.transaction.PendingTransaction;

import java.util.Arrays;

public class Test {

    public static void main(String[] args) throws Exception {
        final Blockonomics blockonomics = new Blockonomics("API_KEY");
        blockonomics.modifyMonitoringAddress("SOME_BTC_ADDRESS", "Bitcoin Address #2");

        BitcoinAddressHistory bitcoinAddressHistory = blockonomics.getBitcoinAddressHistory(new BitcoinAddress[]{new BitcoinAddress("1FnQjXQc8F6jyjF8L92yLpnMhSWpw8t8jo")});
        for (ConfirmedTransaction confirmedTransaction : bitcoinAddressHistory.getHistory()){
            System.out.println(Arrays.toString(confirmedTransaction.getAddr()));
            System.out.println(confirmedTransaction.getTime());
            System.out.println(confirmedTransaction.getTxid());
            System.out.println(confirmedTransaction.getValue());
        }

        for (PendingTransaction pendingTransaction : bitcoinAddressHistory.getPendingTransactions()){
            System.out.println(Arrays.toString(pendingTransaction.getAddr()));
            System.out.println(pendingTransaction.getTime());
            System.out.println(pendingTransaction.getTxid());
            System.out.println(pendingTransaction.getValue());
            System.out.println(pendingTransaction.getStatus());
        }
        
        blockonomics.deleteMonitoringAddress("SOME_BTC_ADDRESS");
    }

}

Receiving LIVE Bitcoin Payment "Alerts" using the Blockonomics Callback Functionality: BE SURE TO SET YOUR 'CALLBACK URL' IN THE BLOCKONOMICS MERCHANT AREA: https://www.blockonomics.co/merchants#/stores

There will be an easy-to-use callback URL example below

package me.aaronakhtar.blockonomics_wrapper;

import me.aaronakhtar.blockonomics_wrapper.objects.BlockonomicsCallbackSettings;
import me.aaronakhtar.blockonomics_wrapper.objects.transaction.CallbackTransaction;
import me.aaronakhtar.blockonomics_wrapper.objects.transaction.TransactionStatus;

import java.util.Date;
import java.util.Locale;
import java.util.Random;
import java.util.Arrays;

public class Test {

    public static void main(String[] args) throws Exception {
        final Blockonomics blockonomics = new Blockonomics("API_KEY");
        
        // set the PAGE and SECRET KEY you want as this blockonomics instance's specific callback.
        // if you have multiple apis, you can create multiple instances,
        // and multiple pages on the same HTTP server for different callbacks
        // and authentication.
        //
        final BlockonomicsCallbackSettings callbackSettings 
                                    // MAKE SURE TO PUT A `/` BEFORE THE PAGE IDENTIFIER.
                = blockonomics.getCallbackSettings("/testing", "0");
        
        
        // In a production environment, you'd probably
        // want to create a boolean for that keeps the
        // callback server alive while its true, this
        // will allow you to safely close the callback
        // server when you want to shut it down, to keep the server
        // alive, run it in a thread.
        
        try {
            Blockonomics.startCallbackServer(new BlockonomicsCallbackSettings[]{callbackSettings}, 8081);
            try (AutoCloseable autoCloseable = () -> Blockonomics.stopCallbackServer()) {



                // the below thread is just to emulate new transactions.


                new Thread(){
                    @Override
                    public void run() {
                        final Random random = new Random();
                        while(true){
                            try {
                                Thread.sleep(2500);
                                
                                // you can also do this through the Blockonomics Merchants Area: https://www.blockonomics.co/merchants#/logs
                                // once you receive an actual payment, blockonomics will send a request the same way
                                // we are sending a test one, however their request will contain valid data.
                                blockonomics.sendTestPaymentToCallback(
                                        callbackSettings,
                                        TransactionStatus.PARTIALLY_CONFIRMED,
                                        "1F1tAaz5x1HUXrCNLbtMDqcw6o5GNn4xqX",
                                        random.nextDouble(),
                                        true);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    }
                }.start();


                // I've created an example timer system which
                // will be used to only listen for transactions for a
                // specified number of seconds, otherwise it will run in an
                // infinite loop, as it will never return null.

                // if you want to change this, set `100`
                // to however many seconds you want it to stay open for.
                final long stopListeningTime = System.currentTimeMillis() + (100 * 1000);

                CallbackTransaction callbackTransaction = null;
                System.out.println("Listening for transactions.");
                while (System.currentTimeMillis() < stopListeningTime) {
                    
                    // The listenForNewTransaction method is blocking, 
                    // but only for 5 seconds, so if a transaction doesn't 
                    // appear in 5 seconds, it returns null.
                    final CallbackTransaction transaction = 
                            (callbackTransaction = blockonomics.listenForNewTransaction());
                    if (transaction != null) {
                        new Thread() {
                            @Override
                            public void run() {
                                try {
                                    System.out.println("\n---------------" + transaction.getNoticedDate() + "  (NOW:" + CallbackTransaction.sdf.format(new Date()) + ")---------------");
                                    System.out.println("Address: " + transaction.getAddress().getAddress());
                                    System.out.println("Transaction ID: " + transaction.getTransactionId());
                                    System.out.println("Bitcoin Amount (Satoshis): " + transaction.getAmount());
                                    System.out.println("Confirmation Status: " + transaction.getStatus().name().toUpperCase(Locale.ROOT) + " ('" + transaction.getStatus().getI() + "')");
                                    // if you want to create invoicing, so that you  can track payments etc, you would do those checks here,
                                    // however I will persist you use threading here so that you don't miss any transactions.

                                    
                                    
                                    // EXAMPLE CALLBACK URL:
                                    // http://%YOUR_PUBLIC_IPv4_HERE%:%YOUR_PORT_HERE%/testing?secret=%YOUR_SECRET_KEY_HERE%
                                    
                                    

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }.start();
                    }

                }
                System.out.println("No longer listening for transactions.");                
                
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }      
        
        


    }
}

blockonomics-java-wrapper's People

Contributors

aaron-akhtar avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

blockonomics-java-wrapper's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.