|
| 1 | +import com.cryptochassis.ccapi.Event; |
| 2 | +import com.cryptochassis.ccapi.EventHandler; |
| 3 | +import com.cryptochassis.ccapi.Message; |
| 4 | +import com.cryptochassis.ccapi.PairIntString; |
| 5 | +import com.cryptochassis.ccapi.Request; |
| 6 | +import com.cryptochassis.ccapi.Session; |
| 7 | +import com.cryptochassis.ccapi.SessionConfigs; |
| 8 | +import com.cryptochassis.ccapi.SessionOptions; |
| 9 | +import com.cryptochassis.ccapi.Subscription; |
| 10 | +import com.cryptochassis.ccapi.SubscriptionList; |
| 11 | +import com.cryptochassis.ccapi.VectorPairIntString; |
| 12 | + |
| 13 | +public class Main { |
| 14 | + static class MyEventHandler extends EventHandler { |
| 15 | + @Override |
| 16 | + public boolean processEvent(Event event, Session session) { |
| 17 | + if (event.getType() == Event.Type.AUTHORIZATION_STATUS) { |
| 18 | + System.out.println(String.format("Received an event of type AUTHORIZATION_STATUS:\n%s", event.toStringPretty(2, 2))); |
| 19 | + var message = event.getMessageList().get(0); |
| 20 | + if (message.getType() == Message.Type.AUTHORIZATION_SUCCESS) { |
| 21 | + var request = new Request(Request.Operation.FIX, "coinbase", "", "same correlation id for subscription and request"); |
| 22 | + var param = new VectorPairIntString(); |
| 23 | + param.add(new PairIntString(35, "D")); |
| 24 | + param.add(new PairIntString(11, "6d4eb0fb-2229-469f-873e-557dd78ac11e")); |
| 25 | + param.add(new PairIntString(55, "BTC-USD")); |
| 26 | + param.add(new PairIntString(54, "1")); |
| 27 | + param.add(new PairIntString(44, "20000")); |
| 28 | + param.add(new PairIntString(38, "0.001")); |
| 29 | + param.add(new PairIntString(40, "2")); |
| 30 | + param.add(new PairIntString(59, "1")); |
| 31 | + request.appendParamFix(param); |
| 32 | + session.sendRequest(request); |
| 33 | + } |
| 34 | + } else if (event.getType() == Event.Type.FIX) { |
| 35 | + System.out.println(String.format("Received an event of type FIX:\n%s", event.toStringPretty(2, 2))); |
| 36 | + } |
| 37 | + return true; |
| 38 | + } |
| 39 | + } |
| 40 | + public static void main(String[] args) { |
| 41 | + if (System.getenv("COINBASE_API_KEY") == null) { |
| 42 | + System.err.println("Please set environment variable COINBASE_API_KEY"); |
| 43 | + return; |
| 44 | + } |
| 45 | + if (System.getenv("COINBASE_API_SECRET") == null) { |
| 46 | + System.err.println("Please set environment variable COINBASE_API_SECRET"); |
| 47 | + return; |
| 48 | + } |
| 49 | + if (System.getenv("COINBASE_API_PASSPHRASE") == null) { |
| 50 | + System.err.println("Please set environment variable COINBASE_API_PASSPHRASE"); |
| 51 | + return; |
| 52 | + } |
| 53 | + System.loadLibrary("ccapi_binding_java"); |
| 54 | + var eventHandler = new MyEventHandler(); |
| 55 | + var option = new SessionOptions(); |
| 56 | + var config = new SessionConfigs(); |
| 57 | + var session = new Session(option, config, eventHandler); |
| 58 | + var subscription = new Subscription("coinbase", "", "FIX", "", "same correlation id for subscription and request"); |
| 59 | + session.subscribeByFix(subscription); |
| 60 | + try { |
| 61 | + Thread.sleep(10000); |
| 62 | + } catch (InterruptedException e) { |
| 63 | + } |
| 64 | + session.stop(); |
| 65 | + System.out.println("Bye"); |
| 66 | + } |
| 67 | +} |
0 commit comments