|
| 1 | +import { MsgCreateDeployment } from "@akashnetwork/akash-api/v1beta3"; |
| 2 | +import { SDL } from "@akashnetwork/akashjs/build/sdl"; |
| 3 | +import { getAkashTypeRegistry } from "@akashnetwork/akashjs/build/stargate"; |
| 4 | +import { DeploymentHttpService, LeaseHttpService } from "@akashnetwork/http-sdk"; |
| 5 | +import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing"; |
| 6 | +import { SigningStargateClient } from "@cosmjs/stargate"; |
| 7 | +import { InternalServerError, NotFound } from "http-errors"; |
| 8 | +import { singleton } from "tsyringe"; |
| 9 | + |
| 10 | +import { BillingConfigService } from "@src/billing/services/billing-config/billing-config.service"; |
| 11 | +import { CreateDeploymentResponse, GetDeploymentResponse } from "@src/deployment/http-schemas/deployment.schema"; |
| 12 | +import { MessageTransmitterService } from "@src/deployment/services/message-transmitter/message-transmitter.service"; |
| 13 | +import { env } from "@src/utils/env"; |
| 14 | + |
| 15 | +@singleton() |
| 16 | +export class DeploymentService { |
| 17 | + constructor( |
| 18 | + private readonly config: BillingConfigService, |
| 19 | + private readonly messageTransmitterService: MessageTransmitterService, |
| 20 | + private readonly deploymentHttpService: DeploymentHttpService, |
| 21 | + private readonly leaseHttpService: LeaseHttpService, |
| 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(sdl: SDL): Promise<CreateDeploymentResponse['data']> { |
| 44 | + const dseq = (await this.messageTransmitterService.getCurrentHeight()).toString(); |
| 45 | + const manifestVersion = await sdl.manifestVersion(); |
| 46 | + const wallet = await DirectSecp256k1HdWallet.fromMnemonic(env.GPU_BOT_WALLET_MNEMONIC, { prefix: "akash" }); |
| 47 | + const [account] = await wallet.getAccounts(); |
| 48 | + const myRegistry = new Registry([...getAkashTypeRegistry()]); |
| 49 | + const client = await SigningStargateClient.connectWithSigner(this.config.get("RPC_NODE_ENDPOINT"), wallet, { |
| 50 | + registry: myRegistry, |
| 51 | + broadcastTimeoutMs: 30_000 |
| 52 | + }); |
| 53 | + |
| 54 | + const message = { |
| 55 | + typeUrl: `/akash.deployment.v1beta3.MsgCreateDeployment`, |
| 56 | + value: MsgCreateDeployment.fromPartial({ |
| 57 | + id: { |
| 58 | + owner: account.address, |
| 59 | + dseq: dseq |
| 60 | + }, |
| 61 | + groups: sdl.groups(), |
| 62 | + version: manifestVersion, |
| 63 | + deposit: { |
| 64 | + denom: "uakt", |
| 65 | + amount: "500000" |
| 66 | + }, |
| 67 | + depositor: account.address |
| 68 | + }) |
| 69 | + }; |
| 70 | + |
| 71 | + return await this.messageTransmitterService.signAndBroadcast(account.address, client, [message]); |
| 72 | + } |
| 73 | +} |
0 commit comments