Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get pool DEEP conversion in SDK #19859

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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],
});
};
}
Loading