|
| 1 | +import { SDL } from "@akashnetwork/akashjs/build/sdl"; |
| 2 | +import { BlockHttpService, DeploymentHttpService, LeaseHttpService } from "@akashnetwork/http-sdk"; |
| 3 | +import { BadRequest, InternalServerError, NotFound } from "http-errors"; |
| 4 | +import { singleton } from "tsyringe"; |
| 5 | + |
| 6 | +import { InjectWallet } from "@src/billing/providers/wallet.provider"; |
| 7 | +import { UserWalletOutput } from "@src/billing/repositories"; |
| 8 | +import { ManagedSignerService, RpcMessageService, Wallet } from "@src/billing/services"; |
| 9 | +import { BillingConfigService } from "@src/billing/services/billing-config/billing-config.service"; |
| 10 | +import { CreateDeploymentRequest, CreateDeploymentResponse, GetDeploymentResponse } from "@src/deployment/http-schemas/deployment.schema"; |
| 11 | + |
| 12 | +@singleton() |
| 13 | +export class DeploymentService { |
| 14 | + constructor( |
| 15 | + private readonly blockHttpService: BlockHttpService, |
| 16 | + private readonly deploymentHttpService: DeploymentHttpService, |
| 17 | + private readonly leaseHttpService: LeaseHttpService, |
| 18 | + private readonly signerService: ManagedSignerService, |
| 19 | + @InjectWallet("MANAGED") private readonly masterWallet: Wallet, |
| 20 | + private readonly billingConfigService: BillingConfigService, |
| 21 | + private readonly rpcMessageService: RpcMessageService, |
| 22 | + ) { } |
| 23 | + |
| 24 | + public async findByOwnerAndDseq(owner: string, dseq: string): Promise<GetDeploymentResponse['data']> { |
| 25 | + const deploymentResponse = await this.deploymentHttpService.findByOwnerAndDseq(owner, dseq); |
| 26 | + if ("code" in deploymentResponse) { |
| 27 | + if (deploymentResponse.message?.toLowerCase().includes("deployment not found")) { |
| 28 | + throw new NotFound("Deployment not found"); |
| 29 | + } |
| 30 | + |
| 31 | + throw new InternalServerError(deploymentResponse.message); |
| 32 | + } |
| 33 | + |
| 34 | + const { leases } = await this.leaseHttpService.listByOwnerAndDseq(owner, dseq); |
| 35 | + |
| 36 | + return { |
| 37 | + deployment: deploymentResponse.deployment, |
| 38 | + leases: leases.map(({ lease }) => lease), |
| 39 | + escrow_account: deploymentResponse.escrow_account |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + public async create(wallet: UserWalletOutput, input: CreateDeploymentRequest['data']): Promise<CreateDeploymentResponse['data']> { |
| 44 | + let sdl: SDL; |
| 45 | + try { |
| 46 | + sdl = SDL.fromString(input.sdl, 'beta3'); |
| 47 | + } catch (error) { |
| 48 | + if (error.name === 'SdlValidationError') { |
| 49 | + throw new BadRequest(error.message); |
| 50 | + } |
| 51 | + |
| 52 | + throw new BadRequest("Invalid SDL"); |
| 53 | + } |
| 54 | + |
| 55 | + const message = this.rpcMessageService.getCreateDeploymentMsg({ |
| 56 | + owner: wallet.address, |
| 57 | + dseq: await this.blockHttpService.getCurrentHeight(), |
| 58 | + groups: sdl.groups(), |
| 59 | + denom: this.billingConfigService.get("DEPLOYMENT_GRANT_DENOM"), |
| 60 | + amount: input.deposit, |
| 61 | + manifestVersion: await sdl.manifestVersion(), |
| 62 | + depositor: await this.masterWallet.getFirstAddress() |
| 63 | + }); |
| 64 | + |
| 65 | + return await this.signerService.executeDecodedTxByUserId(wallet.userId, [message]); |
| 66 | + } |
| 67 | +} |
0 commit comments