-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(network): API endpoint listing bids
refs #767
- Loading branch information
Showing
11 changed files
with
507 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { BidHttpService } from "@akashnetwork/http-sdk"; | ||
import assert from "http-assert"; | ||
import flatten from "lodash/flatten"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { AuthService, Protected } from "@src/auth/services/auth.service"; | ||
import { ListBidsResponse } from "@src/bid/http-schemas/bid.schema"; | ||
import { UserWalletRepository } from "@src/billing/repositories"; | ||
|
||
@singleton() | ||
export class BidController { | ||
constructor( | ||
private readonly bidHttpService: BidHttpService, | ||
private readonly authService: AuthService, | ||
private readonly userWalletRepository: UserWalletRepository, | ||
) {} | ||
|
||
@Protected([{ action: "create", subject: "DeploymentSetting" }]) | ||
async list(dseq: string, userId?: string): Promise<ListBidsResponse> { | ||
const { currentUser } = this.authService; | ||
assert(this.isValidUserId(userId), 401); | ||
|
||
const wallets = await this.userWalletRepository.findByUserId(userId ?? currentUser.userId); | ||
const allBids = await Promise.all(wallets.map(wallet => this.bidHttpService.list(wallet.address, dseq))); | ||
|
||
return { data: flatten(allBids) }; | ||
} | ||
|
||
private isValidUserId(userId?: string) { | ||
const { currentUser, ability } = this.authService; | ||
|
||
return userId === undefined | ||
|| userId === currentUser.userId | ||
|| ability.can("manage", "all"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { z } from "zod"; | ||
|
||
const DeploymentResource_V3 = z.object({ | ||
cpu: z.object({ | ||
units: z.object({ | ||
val: z.string(), | ||
}), | ||
attributes: z.array(z.object({ | ||
key: z.string(), | ||
value: z.string(), | ||
})), | ||
}), | ||
gpu: z.object({ | ||
units: z.object({ | ||
val: z.string(), | ||
}), | ||
attributes: z.array(z.object({ | ||
key: z.string(), | ||
value: z.string(), | ||
})), | ||
}), | ||
memory: z.object({ | ||
quantity: z.object({ | ||
val: z.string(), | ||
}), | ||
attributes: z.array(z.object({ | ||
key: z.string(), | ||
value: z.string(), | ||
})), | ||
}), | ||
storage: z.array(z.object({ | ||
name: z.string(), | ||
quantity: z.object({ | ||
val: z.string(), | ||
}), | ||
attributes: z.array(z.object({ | ||
key: z.string(), | ||
value: z.string(), | ||
})), | ||
})), | ||
endpoints: z.array(z.object({ | ||
kind: z.string(), | ||
sequence_number: z.number() | ||
})) | ||
}); | ||
|
||
export const BidResponseSchema = z.object({ | ||
bid: z.object({ | ||
bid_id: z.object({ | ||
owner: z.string(), | ||
dseq: z.string(), | ||
gseq: z.number(), | ||
oseq: z.number(), | ||
provider: z.string(), | ||
}), | ||
state: z.string(), | ||
price: z.object({ | ||
denom: z.string(), | ||
amount: z.string(), | ||
}), | ||
created_at: z.string(), | ||
resources_offer: z.array(z.object({ | ||
resources: DeploymentResource_V3, | ||
count: z.number(), | ||
})) | ||
}), | ||
escrow_account: z.object({ | ||
id: z.object({ | ||
scope: z.string(), | ||
xid: z.string(), | ||
}), | ||
owner: z.string(), | ||
state: z.string(), | ||
balance: z.object({ | ||
denom: z.string(), | ||
amount: z.string(), | ||
}), | ||
transferred: z.object({ | ||
denom: z.string(), | ||
amount: z.string(), | ||
}), | ||
settled_at: z.string(), | ||
depositor: z.string(), | ||
funds: z.object({ | ||
denom: z.string(), | ||
amount: z.string(), | ||
}), | ||
}) | ||
}); | ||
|
||
export const ListBidsQuerySchema = z.object({ | ||
dseq: z.string(), | ||
userId: z.optional(z.string()), | ||
}); | ||
|
||
export const ListBidsResponseSchema = z.object({ | ||
data: z.array(BidResponseSchema) | ||
}); | ||
|
||
export type ListBidsResponse = z.infer<typeof ListBidsResponseSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createRoute } from "@hono/zod-openapi"; | ||
import { container } from "tsyringe"; | ||
|
||
import { BidController } from "@src/bid/controllers/bid/bid.controller"; | ||
import { ListBidsQuerySchema, ListBidsResponseSchema } from "@src/bid/http-schemas/bid.schema"; | ||
import { OpenApiHonoHandler } from "@src/core/services/open-api-hono-handler/open-api-hono-handler"; | ||
|
||
const listRoute = createRoute({ | ||
method: "get", | ||
path: "/v1/bids", | ||
summary: "List bids", | ||
tags: ["Bids"], | ||
request: { | ||
query: ListBidsQuerySchema | ||
}, | ||
responses: { | ||
200: { | ||
description: "List of bids", | ||
content: { | ||
"application/json": { | ||
schema: ListBidsResponseSchema | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
export const bidsRouter = new OpenApiHonoHandler(); | ||
|
||
bidsRouter.openapi(listRoute, async function routeListBids(c) { | ||
const { dseq, userId } = c.req.valid("query"); | ||
const result = await container.resolve(BidController).list(dseq, userId); | ||
return c.json(result, 200); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { AuthzHttpService, BalanceHttpService, BlockHttpService } from "@akashnetwork/http-sdk"; | ||
import { AuthzHttpService, BalanceHttpService, BidHttpService, BlockHttpService } from "@akashnetwork/http-sdk"; | ||
import { container } from "tsyringe"; | ||
|
||
import { apiNodeUrl } from "@src/utils/constants"; | ||
|
||
const SERVICES = [BalanceHttpService, AuthzHttpService, BlockHttpService]; | ||
const SERVICES = [BalanceHttpService, AuthzHttpService, BlockHttpService, BidHttpService]; | ||
|
||
SERVICES.forEach(Service => container.register(Service, { useValue: new Service({ baseURL: apiNodeUrl }) })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.