Skip to content

Commit

Permalink
Get pool DEEP conversion in SDK (#19859)
Browse files Browse the repository at this point in the history
## Description 

Get pool DEEP conversion in SDK

## Test plan 

How did you test the new or updated feature?

SDK on mainnet
Sample output: { asset_is_base: false, deep_per_quote:
28.613793980999997 }
This corresponds to the current DEEP price

## 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 Nov 4, 2024
1 parent 7523fe8 commit 6a571f1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-walls-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/deepbook-v3': patch
---

Deep conversion
44 changes: 44 additions & 0 deletions sdk/deepbook-v3/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,4 +597,48 @@ export class DeepBookClient {
deep: Number((deepLocked / DEEP_SCALAR).toFixed(9)),
};
}

/**
* @description Get the DEEP price conversion for a pool
* @param {string} poolKey Key of the pool
* @returns {Promise<{ asset_is_base: bool, deep_per_quote: number }>} Deep price conversion
*/
async getPoolDeepPrice(poolKey: string) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
tx.add(this.deepBook.getPoolDeepPrice(poolKey));

const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);
const deepCoin = this.#config.getCoin('DEEP');

const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
});

const OrderDeepPrice = bcs.struct('OrderDeepPrice', {
asset_is_base: bcs.bool(),
deep_per_asset: bcs.u64(),
});

const poolDeepPriceBytes = res.results![0].returnValues![0][0];
const poolDeepPrice = OrderDeepPrice.parse(new Uint8Array(poolDeepPriceBytes));

if (poolDeepPrice.asset_is_base) {
return {
asset_is_base: poolDeepPrice.asset_is_base,
deep_per_base:
((Number(poolDeepPrice.deep_per_asset) / FLOAT_SCALAR) * baseCoin.scalar) /
deepCoin.scalar,
};
} else {
return {
asset_is_base: poolDeepPrice.asset_is_base,
deep_per_quote:
((Number(poolDeepPrice.deep_per_asset) / FLOAT_SCALAR) * quoteCoin.scalar) /
deepCoin.scalar,
};
}
}
}
17 changes: 17 additions & 0 deletions sdk/deepbook-v3/src/transactions/deepbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,4 +667,21 @@ export class DeepBookContract {
typeArguments: [baseCoin.type, quoteCoin.type],
});
};

/**
* @description Get the DEEP price conversion for a pool
* @param {string} poolKey The key to identify the pool
* @returns A function that takes a Transaction object
*/
getPoolDeepPrice = (poolKey: string) => (tx: Transaction) => {
const pool = this.#config.getPool(poolKey);
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_order_deep_price`,
arguments: [tx.object(pool.address)],
typeArguments: [baseCoin.type, quoteCoin.type],
});
};
}

0 comments on commit 6a571f1

Please sign in to comment.