Skip to content

Commit

Permalink
Export Update (Deepbook SDK) (#19016)
Browse files Browse the repository at this point in the history
## Description 

Deebook SDK update, floating point rounding and exports

## Test plan 

How did you test the new or updated feature?

Testnet

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
leecchh authored Aug 16, 2024
1 parent 3316ca4 commit 36f1c6f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-lies-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/deepbook-v3': minor
---

Rounding for numbers, exports update
52 changes: 29 additions & 23 deletions sdk/deepbook-v3/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class DeepBookClient {

return {
coinType: coin.type,
balance: adjusted_balance,
balance: Number(adjusted_balance.toFixed(9)),
};
}

Expand Down Expand Up @@ -142,9 +142,9 @@ export class DeepBookClient {

return {
baseQuantity,
baseOut: baseOut / baseScalar,
quoteOut: quoteOut / quoteScalar,
deepRequired: deepRequired / DEEP_SCALAR,
baseOut: Number((baseOut / baseScalar).toFixed(9)),
quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9)),
};
}

Expand Down Expand Up @@ -173,9 +173,9 @@ export class DeepBookClient {

return {
quoteQuantity: quoteQuantity,
baseOut: baseOut / baseScalar,
quoteOut: quoteOut / quoteScalar,
deepRequired: deepRequired / DEEP_SCALAR,
baseOut: Number((baseOut / baseScalar).toFixed(9)),
quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9)),
};
}

Expand Down Expand Up @@ -206,9 +206,9 @@ export class DeepBookClient {
return {
baseQuantity,
quoteQuantity,
baseOut: baseOut / baseScalar,
quoteOut: quoteOut / quoteScalar,
deepRequired: deepRequired / DEEP_SCALAR,
baseOut: Number((baseOut / baseScalar).toFixed(9)),
quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9)),
};
}

Expand Down Expand Up @@ -301,10 +301,12 @@ export class DeepBookClient {
const parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(quantities));

return {
prices: parsed_prices.map(
(price) => (Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar,
prices: parsed_prices.map((price) =>
Number(((Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar).toFixed(9)),
),
quantities: parsed_quantities.map((price) =>
Number((Number(price) / baseCoin.scalar).toFixed(9)),
),
quantities: parsed_quantities.map((price) => Number(price) / baseCoin.scalar),
};
}

Expand Down Expand Up @@ -338,14 +340,18 @@ export class DeepBookClient {
const ask_parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(ask_quantities));

return {
bid_prices: bid_parsed_prices.map(
(price) => (Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar,
bid_prices: bid_parsed_prices.map((price) =>
Number(((Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar).toFixed(9)),
),
bid_quantities: bid_parsed_quantities.map((quantity) =>
Number((Number(quantity) / baseCoin.scalar).toFixed(9)),
),
ask_prices: ask_parsed_prices.map((price) =>
Number(((Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar).toFixed(9)),
),
bid_quantities: bid_parsed_quantities.map((quantity) => Number(quantity) / baseCoin.scalar),
ask_prices: ask_parsed_prices.map(
(price) => (Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar,
ask_quantities: ask_parsed_quantities.map((quantity) =>
Number((Number(quantity) / baseCoin.scalar).toFixed(9)),
),
ask_quantities: ask_parsed_quantities.map((quantity) => Number(quantity) / baseCoin.scalar),
};
}

Expand All @@ -372,9 +378,9 @@ export class DeepBookClient {
const deepInVault = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));

return {
base: baseInVault / baseScalar,
quote: quoteInVault / quoteScalar,
deep: deepInVault / DEEP_SCALAR,
base: Number((baseInVault / baseScalar).toFixed(9)),
quote: Number((quoteInVault / quoteScalar).toFixed(9)),
deep: Number((deepInVault / DEEP_SCALAR).toFixed(9)),
};
}

Expand Down Expand Up @@ -424,6 +430,6 @@ export class DeepBookClient {
const adjusted_mid_price =
(parsed_mid_price * baseCoin.scalar) / quoteCoin.scalar / FLOAT_SCALAR;

return adjusted_mid_price;
return Number(adjusted_mid_price.toFixed(9));
}
}
2 changes: 2 additions & 0 deletions sdk/deepbook-v3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export { DeepBookAdminContract } from './transactions/deepbookAdmin.js';
export { FlashLoanContract } from './transactions/flashLoans.js';
export { GovernanceContract } from './transactions/governance.js';
export { DeepBookConfig } from './utils/config.js';
export type { BalanceManager, Coin, Pool } from './types/index.js';
export type { CoinMap, PoolMap } from './utils/constants.js';
6 changes: 4 additions & 2 deletions sdk/deepbook-v3/src/transactions/balanceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ export class BalanceManagerContract {
tx.setSenderIfNotSet(this.#config.address);
const managerId = this.#config.getBalanceManager(managerKey).address;
const coin = this.#config.getCoin(coinKey);
const depositInput = Math.round(amountToDeposit * coin.scalar);
const deposit = coinWithBalance({
type: coin.type,
balance: amountToDeposit * coin.scalar,
balance: depositInput,
});

tx.moveCall({
Expand All @@ -71,9 +72,10 @@ export class BalanceManagerContract {
(tx: Transaction) => {
const managerId = this.#config.getBalanceManager(managerKey).address;
const coin = this.#config.getCoin(coinKey);
const withdrawInput = Math.round(amountToWithdraw * coin.scalar);
const coinObject = tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::withdraw`,
arguments: [tx.object(managerId), tx.pure.u64(amountToWithdraw * coin.scalar)],
arguments: [tx.object(managerId), tx.pure.u64(withdrawInput)],
typeArguments: [coin.type],
});

Expand Down
31 changes: 21 additions & 10 deletions sdk/deepbook-v3/src/transactions/deepbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class DeepBookContract {
const balanceManager = this.#config.getBalanceManager(balanceManagerKey);
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);
const inputPrice = (price * FLOAT_SCALAR * quoteCoin.scalar) / baseCoin.scalar;
const inputQuantity = quantity * baseCoin.scalar;
const inputPrice = Math.round((price * FLOAT_SCALAR * quoteCoin.scalar) / baseCoin.scalar);
const inputQuantity = Math.round(quantity * baseCoin.scalar);

const tradeProof = tx.add(this.#config.balanceManager.generateProof(balanceManagerKey));

Expand Down Expand Up @@ -93,6 +93,7 @@ export class DeepBookContract {
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);
const tradeProof = tx.add(this.#config.balanceManager.generateProof(balanceManagerKey));
const inputQuantity = Math.round(quantity * baseCoin.scalar);

tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::place_market_order`,
Expand All @@ -102,7 +103,7 @@ export class DeepBookContract {
tradeProof,
tx.pure.u64(clientOrderId),
tx.pure.u8(selfMatchingOption),
tx.pure.u64(quantity * baseCoin.scalar),
tx.pure.u64(inputQuantity),
tx.pure.bool(isBid),
tx.pure.bool(payWithDeep),
tx.object(SUI_CLOCK_OBJECT_ID),
Expand All @@ -127,6 +128,7 @@ export class DeepBookContract {
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);
const tradeProof = tx.add(this.#config.balanceManager.generateProof(balanceManagerKey));
const inputQuantity = Math.round(newQuantity * baseCoin.scalar);

tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::modify_order`,
Expand All @@ -135,7 +137,7 @@ export class DeepBookContract {
tx.object(balanceManager.address),
tradeProof,
tx.pure.u128(orderId),
tx.pure.u64(newQuantity),
tx.pure.u64(inputQuantity),
tx.object(SUI_CLOCK_OBJECT_ID),
],
typeArguments: [baseCoin.type, quoteCoin.type],
Expand Down Expand Up @@ -524,18 +526,21 @@ export class DeepBookContract {

const baseCoinInput =
params.baseCoin ??
coinWithBalance({ type: baseCoin.type, balance: baseAmount * baseCoin.scalar });
coinWithBalance({ type: baseCoin.type, balance: Math.round(baseAmount * baseCoin.scalar) });

const deepCoin =
params.deepCoin ?? coinWithBalance({ type: deepCoinType, balance: deepAmount * DEEP_SCALAR });
params.deepCoin ??
coinWithBalance({ type: deepCoinType, balance: Math.round(deepAmount * DEEP_SCALAR) });

const minQuoteInput = Math.round(minQuote * quoteCoin.scalar);

const [baseCoinResult, quoteCoinResult, deepCoinResult] = tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::swap_exact_base_for_quote`,
arguments: [
tx.object(pool.address),
baseCoinInput,
deepCoin,
tx.pure.u64(quoteCoin.scalar * minQuote),
tx.pure.u64(minQuoteInput),
tx.object(SUI_CLOCK_OBJECT_ID),
],
typeArguments: [baseCoin.type, quoteCoin.type],
Expand Down Expand Up @@ -565,18 +570,24 @@ export class DeepBookContract {

const quoteCoinInput =
params.quoteCoin ??
coinWithBalance({ type: quoteCoin.type, balance: quoteAmount * quoteCoin.scalar });
coinWithBalance({
type: quoteCoin.type,
balance: Math.round(quoteAmount * quoteCoin.scalar),
});

const deepCoin =
params.deepCoin ?? coinWithBalance({ type: deepCoinType, balance: deepAmount * DEEP_SCALAR });
params.deepCoin ??
coinWithBalance({ type: deepCoinType, balance: Math.round(deepAmount * DEEP_SCALAR) });

const minBaseInput = Math.round(minBase * baseCoin.scalar);

const [baseCoinResult, quoteCoinResult, deepCoinResult] = tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::swap_exact_quote_for_base`,
arguments: [
tx.object(pool.address),
quoteCoinInput,
deepCoin,
tx.pure.u64(baseCoin.scalar * minBase),
tx.pure.u64(minBaseInput),
tx.object(SUI_CLOCK_OBJECT_ID),
],
typeArguments: [baseCoin.type, quoteCoin.type],
Expand Down
10 changes: 6 additions & 4 deletions sdk/deepbook-v3/src/transactions/flashLoans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ export class FlashLoanContract {
const pool = this.#config.getPool(poolKey);
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);
const inputQuantity = Math.round(borrowAmount * baseCoin.scalar);
const [baseCoinResult, flashLoan] = tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::borrow_flashloan_base`,
arguments: [tx.object(pool.address), tx.pure.u64(borrowAmount * baseCoin.scalar)],
arguments: [tx.object(pool.address), tx.pure.u64(inputQuantity)],
typeArguments: [baseCoin.type, quoteCoin.type],
});
return [baseCoinResult, flashLoan] as const;
Expand Down Expand Up @@ -57,7 +58,7 @@ export class FlashLoanContract {
const borrowScalar = baseCoin.scalar;

const [baseCoinReturn] = tx.splitCoins(baseCoinInput, [
tx.pure.u64(borrowAmount * borrowScalar),
tx.pure.u64(Math.round(borrowAmount * borrowScalar)),
]);
tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::return_flashloan_base`,
Expand All @@ -78,9 +79,10 @@ export class FlashLoanContract {
const pool = this.#config.getPool(poolKey);
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);
const inputQuantity = Math.round(borrowAmount * quoteCoin.scalar);
const [quoteCoinResult, flashLoan] = tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::borrow_flashloan_quote`,
arguments: [tx.object(pool.address), tx.pure.u64(borrowAmount * quoteCoin.scalar)],
arguments: [tx.object(pool.address), tx.pure.u64(inputQuantity)],
typeArguments: [baseCoin.type, quoteCoin.type],
});
return [quoteCoinResult, flashLoan] as const;
Expand Down Expand Up @@ -108,7 +110,7 @@ export class FlashLoanContract {
const borrowScalar = quoteCoin.scalar;

const [quoteCoinReturn] = tx.splitCoins(quoteCoinInput, [
tx.pure.u64(borrowAmount * borrowScalar),
tx.pure.u64(Math.round(borrowAmount * borrowScalar)),
]);
tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::return_flashloan_quote`,
Expand Down
9 changes: 5 additions & 4 deletions sdk/deepbook-v3/src/transactions/governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ export class GovernanceContract {
const tradeProof = tx.add(this.#config.balanceManager.generateProof(balanceManagerKey));
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);
const stakeInput = Math.round(stakeAmount * DEEP_SCALAR);

tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::stake`,
arguments: [
tx.object(pool.address),
tx.object(balanceManager.address),
tradeProof,
tx.pure.u64(stakeAmount * DEEP_SCALAR),
tx.pure.u64(stakeInput),
],
typeArguments: [baseCoin.type, quoteCoin.type],
});
Expand Down Expand Up @@ -86,9 +87,9 @@ export class GovernanceContract {
tx.object(pool.address),
tx.object(balanceManager.address),
tradeProof,
tx.pure.u64(takerFee * FLOAT_SCALAR),
tx.pure.u64(makerFee * FLOAT_SCALAR),
tx.pure.u64(stakeRequired * DEEP_SCALAR),
tx.pure.u64(Math.round(takerFee * FLOAT_SCALAR)),
tx.pure.u64(Math.round(makerFee * FLOAT_SCALAR)),
tx.pure.u64(Math.round(stakeRequired * DEEP_SCALAR)),
],
typeArguments: [baseCoin.type, quoteCoin.type],
});
Expand Down

0 comments on commit 36f1c6f

Please sign in to comment.