binance-exchange / binance-java-api

binance-java-api is a lightweight Java library for the Binance API, supporting synchronous and asynchronous requests, as well as event streaming using WebSockets.
MIT License
836 stars 627 forks source link

Testnet configuarion #326

Open LexProfi opened 3 years ago

LexProfi commented 3 years ago

what parameters need to be specified to run the library to work with testnet?

georgesoler commented 3 years ago

I have the same question. I believe the connection endpoint for spot is testnet.binance.vision Do you know if anyone has created a fork capable of connecting to the spot testnet?

ts-00 commented 3 years ago

A solution would be to include "testnet.binance.vision" in BinanceApiConfig, so that BinanceApiConfig#getApiBaseUrl() will return "testnet.binance.vision", if user creates a connection like this:

Boolean useTestNet = true;
BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("API-KEY", "SECRET", useTestnet);
BinanceApiRestClient client = factory.newRestClient();

with

/**
   * Instantiates a new binance api client factory.
   *
   * @param apiKey the API key
   * @param secret the Secret
   * @param useTestnet the connection to testnet.binance.vision
   */
  private BinanceApiClientFactory(String apiKey, String secret, boolean useTestnet) {
    this.apiKey = apiKey;
    this.secret = secret;
    this.useTestnet = useTestnet;
  }

/**
   * Instantiates a new binance api client factory.
   *
   * @param apiKey the API key
   * @param secret the Secret
   */
  private BinanceApiClientFactory(String apiKey, String secret) {
    this.apiKey = apiKey;
    this.secret = secret;
  }

Currently, the API can only be used on real accounts, however, especially during development it would be nice if user can bind this API to testnet.

georgesoler commented 3 years ago

I think having the ability to connect to the spot testnet endpoint is more than just nice. Ever since Binance blocked US access to binance.com accounts there is no simple way to run test code for binance.com from a US IP addr without using a (slower) VPN and additional complications.

Thank you ts-00 for your suggestion. It is overall the right general idea. However, it is a bit more complicated.

The Spot Test Network does not appear to need the SECRET param. It seems it authenticates requests only using RSA generated API public/private Keys (see https://testnet.binance.vision). But I haven't tried it yet. Eliminating the secret param has to be done cleanly. It seems we would need to bypass call to AuthenticationInterceptor in class BinanceApiServiceGenerator and pass the useTestnet flag in a way that does not affect the real spot network endpoint behavior (and call to retrofitBuilder).

This has to be implemented carefully. At this point I just want to find out if someone has already done a pull request for this feature- to add a testnet endpoint to BinanceApiClientFactory. Don't want to duplicate any work. I am new to Github. How can we find out if someone is already working on this?

ts-00 commented 3 years ago

How can we find out if someone is already working on this?

Searching like https://github.com/binance-exchange/binance-java-api/pulls?q=is%3Apr+is%3Aopen+testnet, it seems that no one is working on it. However, I don't know if the binance team is working on it.

I also need to test code by testnet with the API, because without we can only begin to use this API with real money which is dangerous while developing..

georgesoler commented 3 years ago

I have a solution ready and tested.

I created branch 'georgesoler/binance-java-api' by forking from repository 'binance-exchange/binance-java-api' which in turn is a fork from the original project 'joaopsilva/binance-java-api'. I forked from 'binance-exchange/binance-java-api' because it's the repository forked by the most people and likely most up-to-date, but being new to GitHub it is possible I screwed up and forked the wrong branch...

You can see my changes to three source files by checking out url = https://github.com/georgesoler/binance-java-api.git I have also written this test driver that handles most use cases. Its a java source disguised as .txt and you can download it here-

BinanceSpotTestnetDriverTest.java.txt

Please feel free to modify the driver to test in your environment and let me know how it goes. It would be great if someone besides me could test this before I create a pull request. -George

antonioo28 commented 3 years ago

@georgesoler I've just made some tests with your branch and so far it's working perfectly! I especially tested the websocket with testnet and I didn't find any remarkable issue. Thank you so much!!

georgesoler commented 3 years ago

You should not work on the original master branch because the code has not been merged yet. I placed a pull request and I don't know how long before the code is reviewed and merged. Please work on the georgesoler branch for testnet purposes.

On Wed, 14 Apr 2021 at 08:38, Antonio @.***> wrote:

UPDATE: I am not able anymore to get my test account info. I was working on the original master branch. I am using "getAccount()" of BinanceApiRestClient after connecting my client with my key and secret generated for testing.

I get a com.binance.api.client.exception.BinanceApiException: Invalid API-key, IP, or permissions for action. And the error is raised here: at com.binance.api.client.impl.BinanceApiServiceGenerator.executeSync(BinanceApiServiceGenerator.java:93)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/binance-exchange/binance-java-api/issues/326#issuecomment-819615239, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM5GCDCDFRVL7DYRWX3JHRTTIWZGFANCNFSM4YUQO77A .

prime-zone commented 3 years ago

@georgesoler when I try your code with:

BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("api key", "prviate key", true, true);

It gives me an error:

java.lang.ExceptionInInitializerError Caused by: java.lang.IllegalArgumentException: Illegal URL: wss://testnet.binance.vision at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:456) at com.binance.api.client.impl.BinanceApiServiceGenerator.createService(BinanceApiServiceGenerator.java:67) at com.binance.api.client.impl.BinanceApiRestClientImpl.(BinanceApiRestClientImpl.java:27) at com.binance.api.client.BinanceApiClientFactory.newRestClient(BinanceApiClientFactory.java:101) at file.(file.java:18) Exception in thread "main" Process finished with exit code 1

Could you tell me if I'm doing something wrong or how to fix this?

georgesoler commented 3 years ago

Hi Mina, I cannot reproduce the error you report. I uploaded the driver I use to test the testnet code changes (see below). The driver includes a call to BinanceApiClientFactory.newInstance(apiKey, secret, true, true) If you haven't resolved the issue, please test by modifying the driver to suit your needs and let me know if you still get the error. Thank you. BinanceSpotTestnetDriverTest.java.txt

georgesoler commented 3 years ago

The pull request for this feature has just been merged into master branch binance-exchange/binance-java-api. Please get it from there insted of forking off my branch because I may delete my branch in the future.

naserkaka commented 2 years ago

A solution would be to include "testnet.binance.vision" in BinanceApiConfig, so that BinanceApiConfig#getApiBaseUrl() will return "testnet.binance.vision", if user creates a connection like this:

Boolean useTestNet = true;
BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("API-KEY", "SECRET", useTestnet);
BinanceApiRestClient client = factory.newRestClient();

with

/**
   * Instantiates a new binance api client factory.
   *
   * @param apiKey the API key
   * @param secret the Secret
   * @param useTestnet the connection to testnet.binance.vision
   */
  private BinanceApiClientFactory(String apiKey, String secret, boolean useTestnet) {
    this.apiKey = apiKey;
    this.secret = secret;
    this.useTestnet = useTestnet;
  }

/**
   * Instantiates a new binance api client factory.
   *
   * @param apiKey the API key
   * @param secret the Secret
   */
  private BinanceApiClientFactory(String apiKey, String secret) {
    this.apiKey = apiKey;
    this.secret = secret;
  }

Currently, the API can only be used on real accounts, however, especially during development it would be nice if user can bind this API to testnet.

its not possible to edit the file, how do I proceed with this ?