Skip to content

Commit bfc4baf

Browse files
committed
feat: implement load operation key
1 parent 44c3cc3 commit bfc4baf

File tree

10 files changed

+15312
-15322
lines changed

10 files changed

+15312
-15322
lines changed

package-lock.json

Lines changed: 15249 additions & 15165 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@
7777
"npm run eslint --fix"
7878
]
7979
}
80-
}
80+
}

src/client/index.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { MeshWallet } from '@meshsdk/core';
12
import axios, { AxiosInstance } from 'axios';
23
import { ApiConfig, ApiHeaders, PostOrderRequest, PostOrderResponse } from '../types';
34
import { Accounts } from './accounts';
45
import { Orders } from './orders';
56
import { Markets } from './markets';
6-
import { DeFiWallet } from './wallet';
7+
import { decryptWithCipher } from './components/encryption';
78

89
/**
910
* Represents the API client for interacting with the DeltaDefi API.
@@ -23,7 +24,9 @@ export class ApiClient {
2324

2425
public markets: Markets;
2526

26-
public wallet?: DeFiWallet;
27+
public masterWallet?: MeshWallet;
28+
29+
public operationWallet?: MeshWallet;
2730

2831
/**
2932
* Creates an instance of ApiClient.
@@ -32,7 +35,7 @@ export class ApiClient {
3235
* @param providedWsURL - Optional WebSocket URL for the API.
3336
*/
3437
constructor(
35-
{ network, jwt, apiKey, signingKey }: ApiConfig,
38+
{ network, jwt, apiKey, masterWallet }: ApiConfig,
3639
providedBaseURL?: string,
3740
providedWsURL?: string,
3841
) {
@@ -59,8 +62,8 @@ export class ApiClient {
5962
if (apiKey) {
6063
headers['X-API-KEY'] = apiKey;
6164
}
62-
if (signingKey) {
63-
this.wallet = new DeFiWallet(signingKey, this.networkId);
65+
if (masterWallet) {
66+
this.masterWallet = masterWallet;
6467
}
6568
if (providedBaseURL) {
6669
baseURL = providedBaseURL;
@@ -79,24 +82,61 @@ export class ApiClient {
7982
this.markets = new Markets(this.axiosInstance);
8083
}
8184

85+
/**
86+
* Initializes the operation wallet with the provided encryption password.
87+
* @param password - The password used to decrypt the operation key.
88+
*/
89+
public async loadOperationKey(password: string) {
90+
const { encrypted_operation_key: encryptedOperationKey } =
91+
await this.accounts.getOperationKey();
92+
const operationKey = await decryptWithCipher({
93+
encryptedDataJSON: encryptedOperationKey,
94+
key: password,
95+
});
96+
this.operationWallet = new MeshWallet({
97+
key: {
98+
type: 'root',
99+
bech32: operationKey,
100+
},
101+
networkId: this.networkId,
102+
});
103+
}
104+
82105
/**
83106
* Posts an order.
84107
* @param data - The post order request data.
85108
* @returns A promise that resolves to the post order response.
86109
* @throws An error if the wallet is not initialized.
87110
*/
88111
public async postOrder(data: PostOrderRequest): Promise<PostOrderResponse> {
89-
if (!this.wallet) {
90-
throw new Error('Wallet is not initialized');
112+
if (!this.operationWallet) {
113+
throw new Error('Operation wallet is not initialized');
91114
}
92115
const buildRes = await this.orders.buildPlaceOrderTransaction(data);
93-
const signedTx = await this.wallet.signTx(buildRes.tx_hex);
116+
const signedTx = await this.operationWallet.signTx(buildRes.tx_hex);
94117
const submitRes: PostOrderResponse = await this.orders.submitPlaceOrderTransaction({
95118
order_id: buildRes.order_id,
96119
signed_tx: signedTx,
97120
});
98121
return submitRes;
99122
}
123+
124+
/**
125+
* Cancels an order.
126+
* @param orderId - The ID of the order to cancel.
127+
* @returns A promise that resolves to a message indicating the order was cancelled successfully.
128+
*/
129+
public async cancelOrder(orderId: string) {
130+
if (!this.operationWallet) {
131+
throw new Error('Operation wallet is not initialized');
132+
}
133+
const buildRes = await this.orders.buildCancelOrderTransaction(orderId);
134+
const signedTx = await this.operationWallet.signTx(buildRes.tx_hex);
135+
await this.orders.submitCancelOrderTransaction({
136+
signed_tx: signedTx,
137+
});
138+
return { message: 'Order cancelled successfully', orderId };
139+
}
100140
}
101141

102142
export { encryptWithCipher, decryptWithCipher } from './components/encryption';

src/client/wallet/index.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/types/api.ts

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

44
export type ApiConfig = {
55
network?: 'preprod' | 'mainnet';
6-
signingKey?: AppWalletKeyType;
6+
masterWallet?: MeshWallet;
77
} & AuthHeaders;

src/types/requests/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ export type SubmitPlaceOrderTransactionRequest = {
7171
signed_tx: string;
7272
};
7373

74-
export type BuildCancelOrderTransactionRequest = {
75-
order_id: string;
76-
};
77-
7874
export type SubmitCancelOrderTransactionRequest = {
7975
signed_tx: string;
8076
};
@@ -83,6 +79,6 @@ export type Status = 'openOrder' | 'orderHistory' | 'tradingHistory';
8379

8480
export type GetOrderRecordRequest = {
8581
status: Status; // Must be either 'openOrder' | 'orderHistory' | 'tradingHistory'
86-
limit: number; // default number is 10 while number must be between 1 and 250
87-
page: number; // default number is 1 while number must be between 1 and 1000
82+
limit?: number; // default number is 10 while number must be between 1 and 250
83+
page?: number; // default number is 1 while number must be between 1 and 1000
8884
};

tests/accounts.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('Account APIs', () => {
2222
test('Get Orders', async () => {
2323
if (skipApiTests) return;
2424
const api = new ApiClient({ apiKey, network: 'preprod' });
25-
const res = await api.accounts.getOrderRecords();
25+
const res = await api.accounts.getOrderRecords({ status: 'openOrder' });
2626
console.log(res);
2727
});
2828
});

tests/markets.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('GetDepthResponse', () => {
1313
test('should have correct structure and non-negative values', async () => {
1414
if (skipApiTests) return;
1515
const api = new ApiClient({ apiKey, network: 'preprod' });
16-
const res = await api.markets.getDepth({ symbol: 'ADAUSDX' });
16+
const res = await api.markets.getDepth({ symbol: 'ADAUSDM' });
1717

1818
console.log('response', res);
1919

@@ -41,7 +41,7 @@ describe('GetMarketPriceRequest', () => {
4141
test('Buying price should have correct data format and non-negative vaule', async () => {
4242
if (skipApiTests) return;
4343
const api = new ApiClient({ apiKey, network: 'preprod' });
44-
const res = await api.markets.getMarketPrice({ symbol: 'ADAUSDX' });
44+
const res = await api.markets.getMarketPrice({ symbol: 'ADAUSDM' });
4545

4646
console.log('response', res);
4747

@@ -54,7 +54,7 @@ describe('GetMarketPriceRequest', () => {
5454
test('Selling price should have correct data format and non-negative vaule', async () => {
5555
if (skipApiTests) return;
5656
const api = new ApiClient({ apiKey, network: 'preprod' });
57-
const res = await api.markets.getMarketPrice({ symbol: 'ADAUSDX' });
57+
const res = await api.markets.getMarketPrice({ symbol: 'ADAUSDM' });
5858

5959
console.log('response', res);
6060

tests/orders.test.ts

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,28 @@
22
/* eslint-disable import/no-unresolved */
33
/* eslint-disable import/no-extraneous-dependencies */
44
import dotenv from 'dotenv';
5-
import { AppWalletKeyType } from '@meshsdk/core';
65
import { ApiClient } from '../src';
76

87
dotenv.config();
98

109
const skipApiTests = process.env.SKIP_API_TESTS === 'true';
1110
const apiKey = process.env.API_KEY || '';
1211

13-
const signingKey: AppWalletKeyType = {
14-
type: 'mnemonic',
15-
words: (process.env.MNEMONIC || '').split(',') || [],
16-
};
17-
1812
describe('Orders APIs', () => {
1913
test('Orders should be successfully placed and cancelled programmatically', async () => {
2014
if (skipApiTests) return;
21-
const api = new ApiClient({ apiKey, signingKey, network: 'preprod' });
22-
const buildRes = await api.orders.buildPlaceOrderTransaction({
23-
symbol: 'ADAUSDX',
15+
const api = new ApiClient({ apiKey, network: 'preprod' });
16+
const postOrderRes = await api.postOrder({
17+
symbol: 'ADAUSDM',
2418
side: 'sell',
2519
type: 'limit',
2620
quantity: 1000_000_000,
2721
price: 0.62,
2822
});
29-
console.log('build order response', buildRes);
30-
const unsignedTx = buildRes.tx_hex;
31-
const signedTx = await api.wallet!.signTx(unsignedTx);
32-
console.log('signed txs', signedTx);
33-
const res = await api.orders.submitPlaceOrderTransaction({
34-
order_id: buildRes.order_id,
35-
signed_tx: signedTx,
36-
});
37-
console.log('response', res);
38-
const cancelRes = await api.orders.buildCancelOrderTransaction(buildRes.order_id);
39-
console.log('cancel order response', cancelRes);
40-
const unsignedCancelTx = cancelRes.tx_hex;
41-
const signedCancelTx = await api.wallet!.signTx(unsignedCancelTx);
42-
console.log('signed cancel txs', signedCancelTx);
43-
const cancelRes2 = await api.orders.submitCancelOrderTransaction({
44-
signed_tx: signedCancelTx,
45-
});
46-
console.log('cancel order response', cancelRes2);
47-
});
48-
test('Orders should be successfully placed and cancelled programmatically in one api', async () => {
49-
if (skipApiTests) return;
50-
const api = new ApiClient({ apiKey, signingKey, network: 'preprod' });
51-
const buildRes = await api.postOrder({
52-
symbol: 'ADAUSDX',
53-
side: 'sell',
54-
type: 'limit',
55-
quantity: 1000_000_000,
56-
price: 0.62,
57-
});
58-
const cancelRes = await api.orders.buildCancelOrderTransaction(buildRes.order.order_id);
23+
console.log('Post order response', postOrderRes);
24+
25+
const cancelRes = await api.cancelOrder(postOrderRes.order.order_id);
5926
console.log('cancel order response', cancelRes);
60-
const unsignedCancelTx = cancelRes.tx_hex;
61-
const signedCancelTx = await api.wallet!.signTx(unsignedCancelTx);
62-
console.log('signed cancel txs', signedCancelTx);
63-
const cancelRes2 = await api.orders.submitCancelOrderTransaction({
64-
signed_tx: signedCancelTx,
65-
});
66-
console.log('cancel order response', cancelRes2);
6727
});
6828
// const sleep = (ms: number) =>
6929
// new Promise((resolve) => {

tests/wallet.test.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)