Skip to content

Commit

Permalink
fix: add totalBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
xsteadybcgo committed Oct 23, 2024
1 parent 81056e8 commit a30f16d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/positions/positions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class PositionsController {
@ApiNotFoundResponse({
description: '{ "errno": 1, "errmsg": "not found" }',
})
public async getRsethBalance(
public async getAllBalance(
@Param("token", new ParseAddressPipe()) token: string,
@Query() rsethQueryDto: BalanceQueryDto,
): Promise<Partial<BalanceReturnDto>> {
Expand Down Expand Up @@ -110,16 +110,17 @@ export class PositionsController {

return { account, balance };
});
console.log(
data.reduce((acc, cur) => BigInt(cur.balance) + acc, BigInt(0)),
const totalBalance = data.reduce(
(acc, cur) => BigInt(cur.balance) + acc,
BigInt(0),
);
const paging = PaginationUtil.paginate(data, page, limit);
const list = paging.items;
const meta = paging.meta;
return {
errno: 0,
errmsg: "no error",
data: list,
data: { totalBalance: totalBalance.toString(), list },
meta,
};
} catch (err) {
Expand Down
20 changes: 13 additions & 7 deletions src/positions/positions.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,21 @@ export class BalanceReturnDto {
@ApiProperty({
type: Array<{ account: string; balance: string }>,
description: "user's balance",
example: [
{
balance: "10000000000",
account: "0x22723cc5ae5a1b4514ca41f2466e2ade15cf529b",
},
],
example: {
totalBalance: "10000000000",
list: [
{
balance: "10000000000",
account: "0x22723cc5ae5a1b4514ca41f2466e2ade15cf529b",
},
],
},
required: false,
})
public readonly data?: Array<{ account: string; balance: string }>;
public readonly data?: {
totalBalance: string;
list: Array<{ account: string; balance: string }>;
};

@ApiProperty({
type: PagingMetaDto,
Expand Down
19 changes: 19 additions & 0 deletions src/positions/positions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ export class PositionsService {
block: number;
}) {
const { token, block, limit, page } = params;
const { _meta } = await fetchGraphQLData<{
_meta: {
block: {
number: number;
};
};
}>(
"https://graph.zklink.io/subgraphs/name/rseth-balance",
`
query QueryBlock {
_meta {
block {
number
}
}
}
`,
);
if (_meta.block.number < block) return [];
const data = await fetchGraphQLData<{ userPositions: any[] }>(
"https://graph.zklink.io/subgraphs/name/rseth-balance", // If more tokens need to be supported, please modify the subgraph
`query MyQuery($token: Bytes = \"${token}\") {
Expand Down

0 comments on commit a30f16d

Please sign in to comment.