Skip to content
Open
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
160 changes: 101 additions & 59 deletions packages/deepbook-v3/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ export class DeepBookClient {
pools,
adminCap,
});
this.balanceManager = new BalanceManagerContract(this.#config);
this.deepBook = new DeepBookContract(this.#config);
this.deepBookAdmin = new DeepBookAdminContract(this.#config);
this.flashLoans = new FlashLoanContract(this.#config);
this.governance = new GovernanceContract(this.#config);
this.balanceManager = new BalanceManagerContract(env);
this.deepBook = new DeepBookContract(env, this.#address);
this.deepBookAdmin = new DeepBookAdminContract(env, this.#config.adminCap);
this.flashLoans = new FlashLoanContract(env);
this.governance = new GovernanceContract(env);
}

/**
Expand Down Expand Up @@ -104,8 +104,11 @@ export class DeepBookClient {
*/
async whitelisted(poolKey: string) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.whitelisted(poolKey));
tx.add(this.deepBook.whitelisted(pool.address, baseCoin.type, quoteCoin.type));
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand All @@ -127,10 +130,12 @@ export class DeepBookClient {
async getQuoteQuantityOut(poolKey: string, baseQuantity: number) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.getQuoteQuantityOut(poolKey, baseQuantity));
tx.add(
this.deepBook.getQuoteQuantityOut(pool.address, baseQuantity, baseCoin.type, quoteCoin.type),
);
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand All @@ -142,8 +147,8 @@ export class DeepBookClient {

return {
baseQuantity,
baseOut: Number((baseOut / baseScalar).toFixed(9)),
quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
baseOut: Number((baseOut / baseCoin.scalar).toFixed(9)),
quoteOut: Number((quoteOut / quoteCoin.scalar).toFixed(9)),
deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9)),
};
}
Expand All @@ -158,10 +163,12 @@ export class DeepBookClient {
async getBaseQuantityOut(poolKey: string, quoteQuantity: number) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.getBaseQuantityOut(poolKey, quoteQuantity));
tx.add(
this.deepBook.getBaseQuantityOut(pool.address, quoteQuantity, baseCoin.type, quoteCoin.type),
);
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand All @@ -173,8 +180,8 @@ export class DeepBookClient {

return {
quoteQuantity: quoteQuantity,
baseOut: Number((baseOut / baseScalar).toFixed(9)),
quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
baseOut: Number((baseOut / baseCoin.scalar).toFixed(9)),
quoteOut: Number((quoteOut / quoteCoin.scalar).toFixed(9)),
deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9)),
};
}
Expand All @@ -190,10 +197,18 @@ export class DeepBookClient {
async getQuantityOut(poolKey: string, baseQuantity: number, quoteQuantity: number) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.getQuantityOut(poolKey, baseQuantity, quoteQuantity));
tx.add(
this.deepBook.getQuantityOut(
pool.address,
baseQuantity,
quoteQuantity,
baseCoin.type,
quoteCoin.type,
),
);
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand All @@ -206,8 +221,8 @@ export class DeepBookClient {
return {
baseQuantity,
quoteQuantity,
baseOut: Number((baseOut / baseScalar).toFixed(9)),
quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
baseOut: Number((baseOut / baseCoin.scalar).toFixed(9)),
quoteOut: Number((quoteOut / quoteCoin.scalar).toFixed(9)),
deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9)),
};
}
Expand All @@ -220,8 +235,13 @@ export class DeepBookClient {
*/
async accountOpenOrders(poolKey: string, managerKey: string) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.accountOpenOrders(poolKey, managerKey));
tx.add(
this.deepBook.accountOpenOrders(pool.address, managerKey, baseCoin.type, quoteCoin.type),
);
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand All @@ -243,8 +263,11 @@ export class DeepBookClient {
*/
async getOrder(poolKey: string, orderId: string) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.getOrder(poolKey, orderId));
tx.add(this.deepBook.getOrder(pool.address, orderId, baseCoin.type, quoteCoin.type));
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand Down Expand Up @@ -286,7 +309,11 @@ export class DeepBookClient {
*/
async getOrderNormalized(poolKey: string, orderId: string) {
const tx = new Transaction();
tx.add(this.deepBook.getOrder(poolKey, orderId));
const pool = this.#config.getPool(poolKey);
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.getOrder(pool.address, orderId, baseCoin.type, quoteCoin.type));
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand Down Expand Up @@ -353,8 +380,11 @@ export class DeepBookClient {
*/
async getOrders(poolKey: string, orderIds: string[]) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.getOrders(poolKey, orderIds));
tx.add(this.deepBook.getOrders(pool.address, orderIds, baseCoin.type, quoteCoin.type));
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand Down Expand Up @@ -403,7 +433,16 @@ export class DeepBookClient {
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.getLevel2Range(poolKey, priceLow, priceHigh, isBid));
tx.add(
this.deepBook.getLevel2Range(
poolKey,
priceLow,
priceHigh,
isBid,
baseCoin.type,
quoteCoin.type,
),
);
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand Down Expand Up @@ -437,7 +476,7 @@ export class DeepBookClient {
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.getLevel2TicksFromMid(poolKey, ticks));
tx.add(this.deepBook.getLevel2TicksFromMid(pool.address, ticks, baseCoin.type, quoteCoin.type));
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand Down Expand Up @@ -478,10 +517,10 @@ export class DeepBookClient {
async vaultBalances(poolKey: string) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.vaultBalances(poolKey));
tx.add(this.deepBook.vaultBalances(pool.address, baseCoin.type, quoteCoin.type));
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand All @@ -492,8 +531,8 @@ export class DeepBookClient {
const deepInVault = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));

return {
base: Number((baseInVault / baseScalar).toFixed(9)),
quote: Number((quoteInVault / quoteScalar).toFixed(9)),
base: Number((baseInVault / baseCoin.scalar).toFixed(9)),
quote: Number((quoteInVault / quoteCoin.scalar).toFixed(9)),
deep: Number((deepInVault / DEEP_SCALAR).toFixed(9)),
};
}
Expand Down Expand Up @@ -529,11 +568,11 @@ export class DeepBookClient {
async midPrice(poolKey: string) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
tx.add(this.deepBook.midPrice(poolKey));

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

tx.add(this.deepBook.midPrice(pool.address, baseCoin.type, quoteCoin.type));

const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand All @@ -554,8 +593,11 @@ export class DeepBookClient {
*/
async poolTradeParams(poolKey: string) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.poolTradeParams(poolKey));
tx.add(this.deepBook.poolTradeParams(pool.address, baseCoin.type, quoteCoin.type));
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand All @@ -582,10 +624,10 @@ export class DeepBookClient {
async poolBookParams(poolKey: string) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.poolBookParams(poolKey));
tx.add(this.deepBook.poolBookParams(pool.address, baseCoin.type, quoteCoin.type));
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand All @@ -596,9 +638,9 @@ export class DeepBookClient {
const minSize = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));

return {
tickSize: Number((tickSize * baseScalar) / quoteScalar / FLOAT_SCALAR),
lotSize: Number(lotSize / baseScalar),
minSize: Number(minSize / baseScalar),
tickSize: Number((tickSize * baseCoin.scalar) / quoteCoin.scalar / FLOAT_SCALAR),
lotSize: Number(lotSize / baseCoin.scalar),
minSize: Number(minSize / baseCoin.scalar),
};
}

Expand All @@ -611,10 +653,10 @@ export class DeepBookClient {
async account(poolKey: string, managerKey: string) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.account(poolKey, managerKey));
tx.add(this.deepBook.account(pool.address, managerKey, baseCoin.type, quoteCoin.type));
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand Down Expand Up @@ -654,25 +696,25 @@ export class DeepBookClient {
return {
epoch: accountInfo.epoch,
open_orders: accountInfo.open_orders,
taker_volume: Number(accountInfo.taker_volume) / baseScalar,
maker_volume: Number(accountInfo.maker_volume) / baseScalar,
taker_volume: Number(accountInfo.taker_volume) / baseCoin.scalar,
maker_volume: Number(accountInfo.maker_volume) / baseCoin.scalar,
active_stake: Number(accountInfo.active_stake) / DEEP_SCALAR,
inactive_stake: Number(accountInfo.inactive_stake) / DEEP_SCALAR,
created_proposal: accountInfo.created_proposal,
voted_proposal: accountInfo.voted_proposal,
unclaimed_rebates: {
base: Number(accountInfo.unclaimed_rebates.base) / baseScalar,
quote: Number(accountInfo.unclaimed_rebates.quote) / quoteScalar,
base: Number(accountInfo.unclaimed_rebates.base) / baseCoin.scalar,
quote: Number(accountInfo.unclaimed_rebates.quote) / quoteCoin.scalar,
deep: Number(accountInfo.unclaimed_rebates.deep) / DEEP_SCALAR,
},
settled_balances: {
base: Number(accountInfo.settled_balances.base) / baseScalar,
quote: Number(accountInfo.settled_balances.quote) / quoteScalar,
base: Number(accountInfo.settled_balances.base) / baseCoin.scalar,
quote: Number(accountInfo.settled_balances.quote) / quoteCoin.scalar,
deep: Number(accountInfo.settled_balances.deep) / DEEP_SCALAR,
},
owed_balances: {
base: Number(accountInfo.owed_balances.base) / baseScalar,
quote: Number(accountInfo.owed_balances.quote) / quoteScalar,
base: Number(accountInfo.owed_balances.base) / baseCoin.scalar,
quote: Number(accountInfo.owed_balances.quote) / quoteCoin.scalar,
deep: Number(accountInfo.owed_balances.deep) / DEEP_SCALAR,
},
};
Expand All @@ -688,10 +730,12 @@ export class DeepBookClient {
async lockedBalance(poolKey: string, balanceManagerKey: string) {
const tx = new Transaction();
const pool = this.#config.getPool(poolKey);
const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);

tx.add(this.deepBook.lockedBalance(poolKey, balanceManagerKey));
tx.add(
this.deepBook.lockedBalance(pool.address, balanceManagerKey, baseCoin.type, quoteCoin.type),
);
const res = await this.client.devInspectTransactionBlock({
sender: normalizeSuiAddress(this.#address),
transactionBlock: tx,
Expand All @@ -702,8 +746,8 @@ export class DeepBookClient {
const deepLocked = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));

return {
base: Number((baseLocked / baseScalar).toFixed(9)),
quote: Number((quoteLocked / quoteScalar).toFixed(9)),
base: Number((baseLocked / baseCoin.scalar).toFixed(9)),
quote: Number((quoteLocked / quoteCoin.scalar).toFixed(9)),
deep: Number((deepLocked / DEEP_SCALAR).toFixed(9)),
};
}
Expand All @@ -716,8 +760,6 @@ export class DeepBookClient {
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');
Expand Down
Loading