Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2ddf0ed

Browse files
committedJun 4, 2024
feat: updating api client
1 parent b4df6ad commit 2ddf0ed

File tree

7 files changed

+22
-20
lines changed

7 files changed

+22
-20
lines changed
 

‎.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# ENV for running unit tests
22

3-
BASE_URL="http://localhost:8080"
43
AUTH_KEY="testing"
54
WALLET_ADDRESS="addr_test1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
65
API_KEY=""

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@deltadefi-protocol/typescript-sdk",
33
"description": "The Typescript SDK for interacting with DeltaDeFi protocol",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"license": "Apache-2.0",
66
"main": "dist/cjs/index.js",
77
"module": "dist/mjs/index.js",

‎src/client/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { DeFiWallet } from './wallet';
88
export class ApiClient {
99
private axiosInstance: AxiosInstance;
1010

11-
public networkId = 0;
11+
public networkId: 0 | 1 = 0;
1212

1313
public accounts: Accounts;
1414

@@ -18,12 +18,17 @@ export class ApiClient {
1818

1919
public wallet?: DeFiWallet;
2020

21-
constructor(baseURL: string, { networkId, jwt, apiKey, signingKey }: ApiConfig) {
21+
constructor({ network, jwt, apiKey, signingKey }: ApiConfig, ProvidedBaseURL?: string) {
22+
let baseURL = 'https://api-dev.deltadefi.io';
2223
const headers: ApiHeaders = {
2324
'Content-Type': 'application/json',
2425
};
25-
if (networkId) {
26-
this.networkId = networkId;
26+
if (network) {
27+
this.networkId = network === 'mainnet' ? 1 : 0;
28+
baseURL =
29+
network === 'mainnet'
30+
? 'https://api-dev.deltadefi.io'
31+
: 'https://api-dev.deltadefi.io'; // TODO: input production link once available
2732
}
2833
if (jwt) {
2934
headers.Authorization = jwt;
@@ -32,7 +37,10 @@ export class ApiClient {
3237
headers['X-API-KEY'] = apiKey;
3338
}
3439
if (signingKey) {
35-
this.wallet = new DeFiWallet(signingKey, networkId || 0);
40+
this.wallet = new DeFiWallet(signingKey, this.networkId);
41+
}
42+
if (ProvidedBaseURL) {
43+
baseURL = ProvidedBaseURL;
3644
}
3745
this.axiosInstance = axios.create({
3846
baseURL,

‎src/types/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { AppWalletKeyType } from '@meshsdk/core';
22
import { AuthHeaders } from './auth';
33

44
export type ApiConfig = {
5-
networkId?: 0 | 1;
5+
network?: 'preprod' | 'mainnet';
66
signingKey?: AppWalletKeyType;
77
} & AuthHeaders;

‎tests/accounts.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ import { ApiClient } from '../src';
66
dotenv.config();
77

88
const wallet_address = process.env.WALLET_ADDRESS || '';
9-
const baseURL = process.env.BASE_URL || 'http://localhost:8080';
109
const auth_key = process.env.AUTH_KEY || '';
1110
const apiKey = process.env.API_KEY || '';
1211

1312
describe('Account APIs', () => {
1413
test('Sign In', async () => {
15-
const api = new ApiClient(baseURL, {});
14+
const api = new ApiClient({ network: 'preprod' });
1615
const res = await api.accounts.signIn({ wallet_address, auth_key });
1716
expect(res.token).not.toBe('');
1817
});
1918
test('Get Orders', async () => {
20-
const api = new ApiClient(baseURL, { apiKey });
19+
const api = new ApiClient({ apiKey, network: 'preprod' });
2120
const res = await api.accounts.getOrders();
2221
console.log(res);
2322
});

‎tests/markets.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ import { MarketDepth } from '../src/types';
66

77
dotenv.config();
88

9-
// const wallet_address = process.env.WALLET_ADDRESS || '';
10-
const baseURL = process.env.BASE_URL || 'http://localhost:8080';
11-
// const auth_key = process.env.AUTH_KEY || '';
129
const apiKey = process.env.API_KEY || '';
1310

1411
describe('GetDepthResponse', () => {
1512
test('should have correct structure and non-negative values', async () => {
16-
const api = new ApiClient(baseURL, { apiKey });
13+
const api = new ApiClient({ apiKey, network: 'preprod' });
1714
const res = await api.markets.getDepth({ pair: 'ADAUSDX' });
1815

1916
console.log('response', res);
@@ -40,7 +37,7 @@ describe('GetDepthResponse', () => {
4037

4138
describe('GetMarketPriceRequest', () => {
4239
test('Buying price should have correct data format and non-negative vaule', async () => {
43-
const api = new ApiClient(baseURL, { apiKey });
40+
const api = new ApiClient({ apiKey, network: 'preprod' });
4441
const res = await api.markets.getMarketPrice({ pair: 'ADAUSDX' });
4542

4643
console.log('response', res);
@@ -52,7 +49,7 @@ describe('GetMarketPriceRequest', () => {
5249
expect(res.price).toBeGreaterThanOrEqual(0);
5350
});
5451
test('Selling price should have correct data format and non-negative vaule', async () => {
55-
const api = new ApiClient(baseURL, { apiKey });
52+
const api = new ApiClient({ apiKey, network: 'preprod' });
5653
const res = await api.markets.getMarketPrice({ pair: 'ADAUSDX' });
5754

5855
console.log('response', res);

‎tests/orders.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { ApiClient } from '../src';
77

88
dotenv.config();
99

10-
const baseURL = process.env.BASE_URL || 'http://localhost:8080';
1110
const apiKey = process.env.API_KEY || '';
1211

1312
const signingKey: AppWalletKeyType = {
@@ -17,7 +16,7 @@ const signingKey: AppWalletKeyType = {
1716

1817
describe('Orders APIs', () => {
1918
test('Orders should be successfully placed and cancelled programmatically', async () => {
20-
const api = new ApiClient(baseURL, { apiKey, signingKey });
19+
const api = new ApiClient({ apiKey, signingKey, network: 'preprod' });
2120
const buildRes = await api.orders.buildPostOrderTransaction({
2221
pair: 'ADAUSDX',
2322
side: 'sell',
@@ -43,7 +42,7 @@ describe('Orders APIs', () => {
4342
});
4443

4544
test('Orders should be successfully placed and cancelled programmatically in one api', async () => {
46-
const api = new ApiClient(baseURL, { apiKey, signingKey });
45+
const api = new ApiClient({ apiKey, signingKey, network: 'preprod' });
4746
const buildRes = await api.postOrder({
4847
pair: 'ADAUSDX',
4948
side: 'sell',

0 commit comments

Comments
 (0)
Please sign in to comment.