@riocrypto/common-server 1.0.1635 → 1.0.1638

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ import { Crypto } from "@riocrypto/common";
2
+ declare class CoinbaseClient {
3
+ private apiKey;
4
+ private baseUrl;
5
+ private productIdMappings;
6
+ constructor(apiKey: string);
7
+ private getRequestHeaders;
8
+ createMarketOrder(crypto: Crypto, amount: number): Promise<any>;
9
+ }
10
+ export declare const buildCoinbaseClient: () => Promise<CoinbaseClient>;
11
+ export {};
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.buildCoinbaseClient = void 0;
16
+ const secret_manager_client_1 = require("./secret-manager-client");
17
+ const axios_1 = __importDefault(require("axios"));
18
+ const common_1 = require("@riocrypto/common");
19
+ const crypto_1 = require("crypto");
20
+ class CoinbaseClient {
21
+ constructor(apiKey) {
22
+ this.apiKey = apiKey;
23
+ if ([common_1.RioEnv.Development, common_1.RioEnv.Production, common_1.RioEnv.Staging].includes(process.env.RIO_ENV)) {
24
+ this.baseUrl = "https://api-public.sandbox.exchange.coinbase.com";
25
+ }
26
+ else {
27
+ this.baseUrl = "https://api-public.sandbox.exchange.coinbase.com";
28
+ }
29
+ this.productIdMappings = {
30
+ [common_1.Crypto.USDTEthereum]: "USDT-USD",
31
+ };
32
+ }
33
+ getRequestHeaders(requestPath, body, method) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ // create the json request object
36
+ const cb_access_timestamp = Date.now() / 1000;
37
+ const cb_access_passphrase = yield secret_manager_client_1.secretManagerClient.getSecretValue("COINBASE_API_PASSPHRASE");
38
+ if (!cb_access_passphrase) {
39
+ throw new common_1.SecretManagerError();
40
+ }
41
+ const secret = yield secret_manager_client_1.secretManagerClient.getSecretValue("COINBASE_API_SECRET");
42
+ if (!secret) {
43
+ throw new common_1.SecretManagerError();
44
+ }
45
+ // create the prehash string by concatenating required parts
46
+ const message = cb_access_timestamp + method + requestPath + body;
47
+ // decode the base64 secret
48
+ const key = Buffer.from(secret, "base64");
49
+ // create a sha256 hmac with the secret
50
+ const hmac = (0, crypto_1.createHmac)("sha256", key);
51
+ // sign the require message with the hmac and base64 encode the result
52
+ const cb_access_sign = hmac.update(message).digest("base64");
53
+ return {
54
+ "CB-ACCESS-KEY": this.apiKey,
55
+ "CB-ACCESS-SIGN": cb_access_sign,
56
+ "CB-ACCESS-TIMESTAMP": cb_access_timestamp,
57
+ "CB-ACCESS-PASSPHRASE": cb_access_passphrase,
58
+ };
59
+ });
60
+ }
61
+ createMarketOrder(crypto, amount) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ const requestPath = "/orders";
64
+ const body = JSON.stringify({
65
+ type: "market",
66
+ funds: amount,
67
+ side: "buy",
68
+ product_id: this.productIdMappings[crypto],
69
+ });
70
+ const headers = yield this.getRequestHeaders(requestPath, body, "POST");
71
+ const { data } = yield axios_1.default.post(`${this.baseUrl}${requestPath}`, body, {
72
+ headers,
73
+ });
74
+ return data;
75
+ });
76
+ }
77
+ }
78
+ const buildCoinbaseClient = () => __awaiter(void 0, void 0, void 0, function* () {
79
+ // Retrieve secrets asynchronously
80
+ const COINBASE_API_KEY = yield secret_manager_client_1.secretManagerClient.getSecretValue("COINBASE_API_KEY");
81
+ if (!COINBASE_API_KEY) {
82
+ throw new common_1.SecretManagerError();
83
+ }
84
+ // Create GuenoClient instance and return
85
+ return new CoinbaseClient(COINBASE_API_KEY);
86
+ });
87
+ exports.buildCoinbaseClient = buildCoinbaseClient;
package/build/index.d.ts CHANGED
@@ -78,6 +78,7 @@ export * from "./clients/bitso-client";
78
78
  export * from "./clients/circle-client";
79
79
  export * from "./clients/polygon-client";
80
80
  export * from "./clients/coincover-client";
81
+ export * from "./clients/coinbase-client";
81
82
  export * from "./clients/fogbender-client";
82
83
  export * from "./helpers/get-user-helper";
83
84
  export * from "./helpers/get-processor-fees";
package/build/index.js CHANGED
@@ -94,6 +94,7 @@ __exportStar(require("./clients/bitso-client"), exports);
94
94
  __exportStar(require("./clients/circle-client"), exports);
95
95
  __exportStar(require("./clients/polygon-client"), exports);
96
96
  __exportStar(require("./clients/coincover-client"), exports);
97
+ __exportStar(require("./clients/coinbase-client"), exports);
97
98
  __exportStar(require("./clients/fogbender-client"), exports);
98
99
  __exportStar(require("./helpers/get-user-helper"), exports);
99
100
  __exportStar(require("./helpers/get-processor-fees"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common-server",
3
- "version": "1.0.1635",
3
+ "version": "1.0.1638",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",