Skip to content

Commit

Permalink
Merge pull request #25 from compolabs/fix/1248-balance
Browse files Browse the repository at this point in the history
[1248] Add balance query
  • Loading branch information
EchoDex authored Jul 2, 2024
2 parents dda3583 + 356f6b2 commit 5ce1994
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@compolabs/spark-orderbook-ts-sdk",
"version": "1.3.5",
"version": "1.3.6",
"type": "module",
"main": "./dist/index.сjs",
"module": "./dist/index.js",
Expand Down
35 changes: 35 additions & 0 deletions src/ReadActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Options,
OrderType,
SpotOrderWithoutTimestamp,
UserMarketBalance,
} from "./interface";

export class ReadActions {
Expand All @@ -16,6 +17,40 @@ export class ReadActions {
return BN.ZERO;
};

fetchUserMarketBalance = async (
trader: Bech32Address,
options: Options,
): Promise<UserMarketBalance> => {
const orderbookFactory = MarketContractAbi__factory.connect(
options.contractAddresses.market,
options.wallet,
);

const traderAddress = new Address(trader).toB256();

const address: AddressInput = {
bits: traderAddress,
};

const user: IdentityInput = {
Address: address,
};

const result = await orderbookFactory.functions.account(user).get();

const liquid = result.value
? result.value.liquid.toString()
: BN.ZERO.toString();
const locked = result.value
? result.value.locked.toString()
: BN.ZERO.toString();

return {
liquid,
locked,
};
};

fetchOrderById = async (
orderId: string,
options: Options,
Expand Down
10 changes: 0 additions & 10 deletions src/WriteActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ export class WriteActions {
assetId: depositAsset,
};

// console.log("deposit", depositAmount.toString(), depositAsset);

// console.log(
// "open_order",
// amount.toString(),
// tokenType as unknown as AssetTypeInput,
// type as unknown as OrderTypeInput,
// price.toString(),
// );

const tx = orderbookFactory
.multiCall([
orderbookFactory.functions.deposit().callParams({ forward }),
Expand Down
5 changes: 5 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export interface SpotOrderWithoutTimestamp {
orderPrice: BN;
}

export interface UserMarketBalance {
liquid: string;
locked: string;
}

export type MarketCreateEvent = {
id: string;
assetId: string;
Expand Down

0 comments on commit 5ce1994

Please sign in to comment.