Skip to content

Commit

Permalink
Deepbook Locked Balance SDK (#19557)
Browse files Browse the repository at this point in the history
## Description 

Locked balance function for sdk

## Test plan 

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
tonylee08 authored Sep 25, 2024
1 parent 7a4ecf8 commit 37d259a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/blue-ducks-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/deepbook-v3': patch
---

Locked balance feature
30 changes: 30 additions & 0 deletions sdk/deepbook-v3/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,4 +567,34 @@ export class DeepBookClient {
},
};
}

/**
* @description Get the locked balances for a pool and balance manager
* @param {string} poolKey Key of the pool
* @param {string} managerKey The key of the BalanceManager
* @returns {Promise<{ base: number, quote: number, deep: number }>}
* An object with base, quote, and deep locked for the balance manager in the pool
*/
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;

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

const baseLocked = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])));
const quoteLocked = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])));
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)),
deep: Number((deepLocked / DEEP_SCALAR).toFixed(9)),
};
}
}
19 changes: 19 additions & 0 deletions sdk/deepbook-v3/src/transactions/deepbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,4 +648,23 @@ export class DeepBookContract {
typeArguments: [baseCoin.type, quoteCoin.type],
});
};

/**
* @description Get the locked balance for a given pool and balance manager
* @param {string} poolKey Key of the pool
* @param {string} managerKey The key of the BalanceManager
* @returns A function that takes a Transaction object
*/
lockedBalance = (poolKey: string, managerKey: string) => (tx: Transaction) => {
const pool = this.#config.getPool(poolKey);
const baseCoin = this.#config.getCoin(pool.baseCoin);
const quoteCoin = this.#config.getCoin(pool.quoteCoin);
const managerId = this.#config.getBalanceManager(managerKey).address;

tx.moveCall({
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::locked_balance`,
arguments: [tx.object(pool.address), tx.object(managerId)],
typeArguments: [baseCoin.type, quoteCoin.type],
});
};
}
2 changes: 1 addition & 1 deletion sdk/deepbook-v3/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface DeepbookPackageIds {
}

export const testnetPackageIds = {
DEEPBOOK_PACKAGE_ID: '0xc819e689055c241fa1f35a4c87217809edc11b2e73a10fe93300f623e434ecf4',
DEEPBOOK_PACKAGE_ID: '0xd97efea24d2b2f215adb9423211dc9c659cbc25635bdcad2cb2c92e3d28b6474',
REGISTRY_ID: '0xc0bc8c0e0ebd8f891fca1ec0d553c4a06c546a3fa1e902e8fced0606b41a72db',
DEEP_TREASURY_ID: '0x69fffdae0075f8f71f4fa793549c11079266910e8905169845af1f5d00e09dcb',
} satisfies DeepbookPackageIds;
Expand Down

0 comments on commit 37d259a

Please sign in to comment.