irufus / gdax-java

Java based wrapper for Coinbase Pro (Formerly known as GDAX API and Coinbase Exchange API)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GDAX usage examples

irufus opened this issue · comments

Provide sample gdax-java examples in Readme.md

added one but also added a bunch of tests that show how to make various calls. These should suffice.

I'm new to Spring, I've been trying to learn about it through a number of resources however I still can't work out how to get started with this project, can you please provide a simple example such as starting up, pulling down something from the exchange and printing it to the screen without it being a "test".

Cd to the directory where you checked out the project
supply your api keys in the application.yml file (from GDAX)
./gradlew bootRun
That should be it. Get back if you need more info - I'll update the read me some time soon

There's no application.yml file present in the repository so I am not sure what is the structure of that file. Can you elaborate please?

It is a bit confusing for me due to the lack of examples, but I wanted to ask you that to make a simple order all you need is 1) Update the application.yml to use your keys 2) initialize an OrderService: (new OrderService();) 3) use the createOrder() method 4) execute with Gradle

I have been trying to build a jar file to use as a library in my project, but it is not successful:
https://stackoverflow.com/questions/45787085/implementation-of-gdax-java-as-a-library

Do you think you could guide me to any examples on implementation?

That sounds about right.

I've matched almost exactly the API structure from the documentation here: https://docs.gdax.com/#api

Here are some examples which I'll be adding to the README shortly.

  • AccountService.getAccounts() - returns an array of all Accounts
  • AccountService.getAccountHistory(String accountId) - returns the history for a given account
  • AccountService.getHolds(String accountId) - returns an array of all held funds for a given account.
  • DepositService.depositViaPaymentMethod(BigDecimal amount, String currency, String paymentMethodId) - makes a deposit from a stored payment method into your GDAX account
  • DepositService.coinbaseDeposit(BigDecimal amount, String currency, String coinbaseAccountId) - makes a deposit from a coinbase account into your GDAX account
  • MarketDataService.getMarketDataOrderBook(String productId, String level) - a call to ProductService.getProducts() will return the order book for a given product. You can then use the WebsocketFeed api to keep your orderbook up to date. This is implemented in this codebase.
  • OrderService.getOpenOrders(String accountId) - returns an array of Orders for any outstanding orders
  • OrderService.cancelOrder(String orderId) - cancels a given order
  • OrderService.createOrder(NewOrderSingle aSingleOrder) - construct an order and send it to this method to place an order for a given product on the exchange.

As I forked from this repo originally I've made efforts to keep the original repo up to date but inevitably my own version of this repo is more up to date at present.. watch this space: https://github.com/robevansuk/gdax-java

I read your instructions to compile a jar file to use as a library in my own project. However, when I run the ./gradlew jar I don't get a jar file in the build folder. Am I missing anything besides updating the application.yml?

trying to keep this as simple as possible, after having entered the key, secret and passphrase into the application.yml file I've entered these lines into the main method of GdaxApiApplication:

AccountService accountService = new AccountService();
Account[] accounts = accountService.getAccounts();

however I get a java.lang.NullPointerException and I don't understand why, can you please explain,
thanks in advance.

What is the full stack trace that came with the exception?

Exception in thread "main" java.lang.NullPointerException
	at com.coinbase.exchange.api.accounts.AccountService.getAccounts(AccountService.java:24)
	at com.coinbase.exchange.api.GdaxApiApplication.main(GdaxApiApplication.java:27)

(GdaxApiApplication.java:27) is when I call Account[] accounts = accountService.getAccounts();
I have made no changes to AccountService.java

Edit:
I was able to determine that the
@Autowired GdaxExchange exchange;
is the null element here.

To keep things very simple, from the main method, using Spring, how do I get to run accountService.getAccounts()?

If the GdaxExchange is null then something is likely to be wrong with the configuration. Have you tried debugging what happens when the AccountService makes a get request and attempts to sign the request?

debug on this line - it seems likely the securityHeaders are not correct.

ResponseEntity responseEntity = restTemplate.exchange(getBaseUrl() + resourcePath,
GET,
securityHeaders(resourcePath,
"GET",
""),
responseType);

Can someone provide a simple class with a main function that, for instance, shows the BTC balance? I'm getting a NullPointerException when I try something as simple as this:

import com.coinbase.exchange.api.accounts.AccountService;
public class Test {
    public static void main(String[] args) {
        new AccountService().getAccounts();
    }
}

Looks like you're trying to develop outside the application, but this is not how the codebase is currently set up to work. The code base relies on Spring Boot. Spring boot is a framework to develop your application within.

The gdax-java application is a (self contained) application. All you have to do is fork the code base, supply your api credentials (do not commit them), then write your application code on top of/within of it.

So for what you're trying to do you need to declare your class Test as a @component so that spring knows it should create an instance available for use from the Spring Context. Then when other classes want a reference to it, they simply annotate their field/constructor with @Autowired.

import com.coinbase.exchange.api.accounts.Account;
import com.coinbase.exchange.api.accounts.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Test {

    private static final Logger log = LoggerFactory.getLogger(Test.class);

    @Autowired
    private  AccountService accountService;

    public List<Account> main(String[] args) {
        List<Account> accounts = accountService.getAccounts();
        System.out.println("TEST: " + accounts);
        return accounts;
    }
}

If you need anything more try looking at the classes named *Service as they all follow much the same pattern.

Just a side note. I am working on a side project that utilizes gdax as a library. Once I figure out how to streamline the process of rigging it I will add this information to usage examples.

Awesome -- Thanks @robevansuk and @irufus !

@irufus I see that the code is supposed to be built on top of this current project. But two quick questions. Can I use the jar built from the project and use it in my existing spring boot project?

I tried it, the autowire to Accountservice is not getting reflected at Spring start time. Also, How would you set the API key/secret if you are using this as a jar? Appreciate any thoughts.

@irufus I figured out the first part. Just had to add the package name for the component scan in my spring boot project. Still checking on How do I pass the API key and secret. Please let me know if there's a way.

Nevermind. I got it working, just had to give the properties in my application.properties file and autowire GdaxImplementationa and Signature classes in my controller.

@irufus I am using this jar in my Android application but getting nullpointexception even using the Autowired with accountService and component with Activity class. Could you please let me know what additional I need to do here for running this jar in Android project?

Can you provide a stack trace/some more details?

@robevansuk, can you please show, how to execute the https://github.com/irufus/gdax-java/issues/2#issuecomment-345699316 code using SpringBoot? I am not familiar with Spring. I tried to run it as main and as Spring application, but I still got the nullpointer on accountService.

For examples please see - https://github.com/robevansuk/gdax-java/blob/master/CONTRIBUTE.md Where I've done what I can to provide examples.

The GUI package provides working examples.

My repo is not quite ready to merge into this as I'm building a desktop app on this API. You can see a screen shot in the gitter channel. But there's still a lot of hooking up to do and a number more features to build out.

Let me know if this was any good for you as its still open to improvements and your feedback will no doubt help others.

Yes, your link (and you forked repo version) helped me lot and it is best help for those who never encountered with Spring Boot. The problem was that I didn't know how to run SpringBoot main method.
@irufus I think, you should have CONTRIBUTE in this repository or at least put a link to robevansuk's examples to README.

Great. There is a ticket for the Contribute.MD @irufus already raised I just haven't merged anything back yet - it'll get merged eventually into this repo - a few weeks/couple of months away with any luck.

./gradle bootRun will run the application with whatever config is in the application.yml. Or you can just run the main method within your IDE and the same thing will happen.

The intention is to merge my entire repo back to this one in the coming weeks/month or two as I'm working on a desktop application that should largely mirror the web-interface version. This will then be extended over time to provide some extra stats with any luck to help decision making. Hopefully others will also contribute over time too.

I think an examples directory/modules should be added as: examples/. This may help resolve the issues with lack of Spring knowledge, etc.