Skip to content

Commit fc0add8

Browse files
committed
upgrade deepbook to sdk v2 (#731)
* upgrade deepbook to sdk v2 * pr feedback
1 parent 88c767b commit fc0add8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3101
-683
lines changed
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
3-
import { getJsonRpcFullnodeUrl, SuiJsonRpcClient } from '@mysten/sui/jsonRpc';
3+
import { SuiGrpcClient } from '@mysten/sui/grpc';
44

5-
import { DeepBookClient } from '../src/index.js'; // Adjust import source accordingly
5+
import { deepbook } from '../src/index.js'; // Adjust import source accordingly
6+
7+
const GRPC_URLS = {
8+
mainnet: 'https://fullnode.mainnet.sui.io:443',
9+
testnet: 'https://fullnode.testnet.sui.io:443',
10+
} as const;
611

712
/// Example to get open orders for a balance manager for all pools
813
(async () => {
@@ -15,21 +20,20 @@ import { DeepBookClient } from '../src/index.js'; // Adjust import source accord
1520
},
1621
};
1722

18-
const dbClient = new DeepBookClient({
19-
address: '0x0',
20-
env: env,
21-
client: new SuiJsonRpcClient({
22-
url: getJsonRpcFullnodeUrl(env),
23+
const client = new SuiGrpcClient({ network: env, baseUrl: GRPC_URLS[env] }).$extend(
24+
deepbook({
25+
address: '0x0',
26+
env: env,
27+
balanceManagers: balanceManagers,
2328
}),
24-
balanceManagers: balanceManagers,
25-
});
29+
);
2630

2731
const manager = 'MANAGER_1'; // Update the manager accordingly
2832
const pools = ['SUI_USDC', 'DEEP_SUI', 'DEEP_USDC', 'WUSDT_USDC', 'WUSDC_USDC', 'BETH_USDC']; // Live pools, add more if needed
2933
console.log('Manager:', manager);
3034

3135
for (const pool of pools) {
3236
console.log(pool);
33-
console.log(await dbClient.accountOpenOrders(pool, manager));
37+
console.log(await client.deepbook.accountOpenOrders(pool, manager));
3438
}
3539
})();

packages/deepbook-v3/examples/accountOrderMap.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
3-
import { getJsonRpcFullnodeUrl, SuiJsonRpcClient } from '@mysten/sui/jsonRpc';
3+
import { SuiGrpcClient } from '@mysten/sui/grpc';
44

5-
import { DeepBookClient } from '../src/index.js'; // Adjust import source accordingly
5+
import { deepbook } from '../src/index.js'; // Adjust import source accordingly
6+
7+
const GRPC_URLS = {
8+
mainnet: 'https://fullnode.mainnet.sui.io:443',
9+
testnet: 'https://fullnode.testnet.sui.io:443',
10+
} as const;
611

712
/// Example to get [price, quantity] for a balance manager
813
/// Bids sorted in descending order and asks sorted in ascending order
@@ -16,25 +21,24 @@ import { DeepBookClient } from '../src/index.js'; // Adjust import source accord
1621
},
1722
};
1823

19-
const dbClient = new DeepBookClient({
20-
address: '0x0',
21-
env: env,
22-
client: new SuiJsonRpcClient({
23-
url: getJsonRpcFullnodeUrl(env),
24+
const client = new SuiGrpcClient({ network: env, baseUrl: GRPC_URLS[env] }).$extend(
25+
deepbook({
26+
address: '0x0',
27+
env: env,
28+
balanceManagers: balanceManagers,
2429
}),
25-
balanceManagers: balanceManagers,
26-
});
30+
);
2731

2832
const pools = ['SUI_USDC', 'DEEP_SUI', 'DEEP_USDC', 'WUSDT_USDC', 'WUSDC_USDC', 'BETH_USDC']; // Update pools as needed
2933
const manager = 'MANAGER_1'; // Update the manager accordingly
3034
console.log('Manager:', manager);
3135
for (const pool of pools) {
32-
const orders = await dbClient.accountOpenOrders(pool, manager);
36+
const orders = await client.deepbook.accountOpenOrders(pool, manager);
3337
const bidOrdersMap = new Map<number, number>();
3438
const askOrdersMap = new Map<number, number>();
3539

3640
for (const orderId of orders) {
37-
const order = await dbClient.getOrderNormalized(pool, orderId);
41+
const order = await client.deepbook.getOrderNormalized(pool, orderId);
3842
if (!order) {
3943
continue;
4044
}
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
3-
import { getJsonRpcFullnodeUrl, SuiJsonRpcClient } from '@mysten/sui/jsonRpc';
3+
import { SuiGrpcClient } from '@mysten/sui/grpc';
44

5-
import { DeepBookClient } from '../src/index.js'; // Adjust import source accordingly
5+
import { deepbook } from '../src/index.js'; // Adjust import source accordingly
6+
7+
const GRPC_URLS = {
8+
mainnet: 'https://fullnode.mainnet.sui.io:443',
9+
testnet: 'https://fullnode.testnet.sui.io:443',
10+
} as const;
611

712
/// Example to check balance for a balance manager
813
(async () => {
@@ -15,19 +20,18 @@ import { DeepBookClient } from '../src/index.js'; // Adjust import source accord
1520
},
1621
};
1722

18-
const dbClient = new DeepBookClient({
19-
address: '0x0',
20-
env: env,
21-
client: new SuiJsonRpcClient({
22-
url: getJsonRpcFullnodeUrl(env),
23+
const client = new SuiGrpcClient({ network: env, baseUrl: GRPC_URLS[env] }).$extend(
24+
deepbook({
25+
address: '0x0',
26+
env: env,
27+
balanceManagers: balanceManagers,
2328
}),
24-
balanceManagers: balanceManagers,
25-
});
29+
);
2630

2731
const assets = ['SUI', 'USDC', 'WUSDT', 'WUSDC', 'BETH', 'DEEP']; // Update assets as needed
2832
const manager = 'MANAGER_1'; // Update the manager accordingly
2933
console.log('Manager:', manager);
3034
for (const asset of assets) {
31-
console.log(await dbClient.checkManagerBalance(manager, asset));
35+
console.log(await client.deepbook.checkManagerBalance(manager, asset));
3236
}
3337
})();

packages/deepbook-v3/examples/deepbookMarketMaker.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
3-
import { getJsonRpcFullnodeUrl, SuiJsonRpcClient } from '@mysten/sui/jsonRpc';
3+
import type { ClientWithExtensions } from '@mysten/sui/client';
4+
import { SuiGrpcClient } from '@mysten/sui/grpc';
45
import { decodeSuiPrivateKey } from '@mysten/sui/cryptography';
56
import type { Keypair } from '@mysten/sui/cryptography';
67
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
78
import type { Transaction } from '@mysten/sui/transactions';
89

9-
import { DeepBookClient } from '../src/index.js'; // Adjust path according to new structure
10+
import { deepbook, type DeepBookClient } from '../src/index.js'; // Adjust path according to new structure
1011
import type { BalanceManager } from '../src/types/index.js';
1112

12-
export class DeepBookMarketMaker extends DeepBookClient {
13+
const GRPC_URLS = {
14+
mainnet: 'https://fullnode.mainnet.sui.io:443',
15+
testnet: 'https://fullnode.testnet.sui.io:443',
16+
} as const;
17+
18+
export class DeepBookMarketMaker {
1319
keypair: Keypair;
14-
suiClient: SuiJsonRpcClient;
20+
client: ClientWithExtensions<{ deepbook: DeepBookClient }>;
1521

1622
constructor(
1723
keypair: string | Keypair,
@@ -29,37 +35,28 @@ export class DeepBookMarketMaker extends DeepBookClient {
2935

3036
const address = resolvedKeypair.toSuiAddress();
3137

32-
super({
33-
address: address,
34-
env: env,
35-
client: new SuiJsonRpcClient({
36-
url: getJsonRpcFullnodeUrl(env),
37-
}),
38-
balanceManagers: balanceManagers,
39-
adminCap: adminCap,
40-
});
41-
4238
this.keypair = resolvedKeypair;
43-
this.suiClient = new SuiJsonRpcClient({
44-
url: getJsonRpcFullnodeUrl(env),
45-
});
39+
this.client = new SuiGrpcClient({ network: env, baseUrl: GRPC_URLS[env] }).$extend(
40+
deepbook({
41+
address: address,
42+
env: env,
43+
balanceManagers: balanceManagers,
44+
adminCap: adminCap,
45+
}),
46+
);
4647
}
4748

4849
static #getSignerFromPK = (privateKey: string) => {
49-
const { schema, secretKey } = decodeSuiPrivateKey(privateKey);
50-
if (schema === 'ED25519') return Ed25519Keypair.fromSecretKey(secretKey);
50+
const { scheme, secretKey } = decodeSuiPrivateKey(privateKey);
51+
if (scheme === 'ED25519') return Ed25519Keypair.fromSecretKey(secretKey);
5152

52-
throw new Error(`Unsupported schema: ${schema}`);
53+
throw new Error(`Unsupported scheme: ${scheme}`);
5354
};
5455

5556
signAndExecute = async (tx: Transaction) => {
56-
return this.suiClient.signAndExecuteTransaction({
57+
return this.keypair.signAndExecuteTransaction({
5758
transaction: tx,
58-
signer: this.keypair,
59-
options: {
60-
showEffects: true,
61-
showObjectChanges: true,
62-
},
59+
client: this.client,
6360
});
6461
};
6562

@@ -74,11 +71,13 @@ export class DeepBookMarketMaker extends DeepBookClient {
7471
// Return 1 DEEP to DEEP_SUI pool
7572
flashLoanExample = async (tx: Transaction) => {
7673
const borrowAmount = 1;
77-
const [deepCoin, flashLoan] = tx.add(this.flashLoans.borrowBaseAsset('DEEP_SUI', borrowAmount));
74+
const [deepCoin, flashLoan] = tx.add(
75+
this.client.deepbook.flashLoans.borrowBaseAsset('DEEP_SUI', borrowAmount),
76+
);
7877

7978
// Execute trade using borrowed DEEP
8079
const [baseOut, quoteOut, deepOut] = tx.add(
81-
this.deepBook.swapExactQuoteForBase({
80+
this.client.deepbook.deepBook.swapExactQuoteForBase({
8281
poolKey: 'SUI_DBUSDC',
8382
amount: 0.5,
8483
deepAmount: 1,
@@ -91,7 +90,7 @@ export class DeepBookMarketMaker extends DeepBookClient {
9190

9291
// Execute second trade to get back DEEP for repayment
9392
const [baseOut2, quoteOut2, deepOut2] = tx.add(
94-
this.deepBook.swapExactQuoteForBase({
93+
this.client.deepbook.deepBook.swapExactQuoteForBase({
9594
poolKey: 'DEEP_SUI',
9695
amount: 10,
9796
deepAmount: 0,
@@ -103,14 +102,19 @@ export class DeepBookMarketMaker extends DeepBookClient {
103102

104103
// Return borrowed DEEP
105104
const loanRemain = tx.add(
106-
this.flashLoans.returnBaseAsset('DEEP_SUI', borrowAmount, baseOut2, flashLoan),
105+
this.client.deepbook.flashLoans.returnBaseAsset(
106+
'DEEP_SUI',
107+
borrowAmount,
108+
baseOut2,
109+
flashLoan,
110+
),
107111
);
108112
tx.transferObjects([loanRemain], this.getActiveAddress());
109113
};
110114

111115
placeLimitOrderExample = (tx: Transaction) => {
112116
tx.add(
113-
this.deepBook.placeLimitOrder({
117+
this.client.deepbook.deepBook.placeLimitOrder({
114118
poolKey: 'SUI_DBUSDC',
115119
balanceManagerKey: 'MANAGER_1',
116120
clientOrderId: '123456789',

packages/deepbook-v3/examples/useDeepbookClient.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ config();
1111
(async () => {
1212
const privateKey = process.env.PRIVATE_KEY;
1313
if (!privateKey) {
14-
throw new Error('Private key not found');
14+
throw new Error('Private key not found. Set PRIVATE_KEY environment variable.');
1515
}
1616

1717
// Initialize with balance managers if created
@@ -30,12 +30,12 @@ config();
3030

3131
const tx = new Transaction();
3232

33-
// Read only call
34-
console.log(await mmClient.checkManagerBalance('MANAGER_1', 'SUI'));
35-
console.log(await mmClient.getLevel2Range('SUI_DBUSDC', 0.1, 100, true));
33+
// Read only calls - access via client.deepbook
34+
console.log(await mmClient.client.deepbook.checkManagerBalance('MANAGER_1', 'SUI'));
35+
console.log(await mmClient.client.deepbook.getLevel2Range('SUI_DBUSDC', 0.1, 100, true));
3636

3737
// // Balance manager contract call
38-
// mmClient.balanceManager.depositIntoManager('MANAGER_1', 'SUI', 10)(tx);
38+
// mmClient.client.deepbook.balanceManager.depositIntoManager('MANAGER_1', 'SUI', 10)(tx);
3939

4040
// // Example PTB call
4141
// mmClient.placeLimitOrderExample(tx);

0 commit comments

Comments
 (0)