Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
WIP: #9 create deployment endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
natebolam committed Sep 28, 2023
1 parent 5f56e40 commit 17df8f8
Show file tree
Hide file tree
Showing 17 changed files with 286 additions and 169 deletions.
91 changes: 57 additions & 34 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"dependencies": {
"@akashnetwork/akashjs": "0.4.9",
"axios": "^1.5.1",
"cors": "^2.8.5",
"express": "^4.18.2",
"express-async-handler": "^1.2.0",
Expand Down

This file was deleted.

113 changes: 113 additions & 0 deletions api/src/data/data-sources/akash/akash-deployment-data-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { type IDeploymentDataSource } from "@src/data/interfaces/data-sources/deployment-data-source";
import {
QueryClientImpl,
QueryDeploymentsRequest,
QueryDeploymentResponse
} from "@akashnetwork/akashjs/build/protobuf/akash/deployment/v1beta3/query";
import {
MsgCreateDeployment,
MsgCreateDeploymentResponse
} from "@akashnetwork/akashjs/build/protobuf/akash/deployment/v1beta3/deploymentmsg";
import { getRpc } from "@akashnetwork/akashjs/build/rpc";
import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing";
import {
getAkashTypeRegistry,
getTypeUrl
} from "@akashnetwork/akashjs/build/stargate/index";
import { SigningStargateClient } from "@cosmjs/stargate";
import axios from "axios";

export class AkashApiDeploymentDataSource implements IDeploymentDataSource {
private readonly rpcEndpoint: string;
private readonly mnemonic: string;
constructor(rpcEndpoint: string | undefined, mnemonic: string | undefined) {
this.rpcEndpoint = rpcEndpoint || "";
this.mnemonic = mnemonic || "";
}

async create(): Promise<MsgCreateDeploymentResponse> {
const response = await axios.get(`${this.rpcEndpoint}/blocks/latest`);
const data = response.data;

const dseq = parseInt(data.block.header.height);

const wallet = await DirectSecp256k1HdWallet.fromMnemonic(this.mnemonic, {
prefix: "akash"
});

// get first account
const [account] = await wallet.getAccounts();

// Use the encode method for the message to wrap the data
const message = MsgCreateDeployment.fromPartial({
id: {
dseq: dseq,
owner: account.address
}
});

const msgAny = {
typeUrl: getTypeUrl(MsgCreateDeployment),
value: message
};

const myRegistry = new Registry(getAkashTypeRegistry());

const client = await SigningStargateClient.connectWithSigner(
this.rpcEndpoint,
wallet,
{
registry: myRegistry
}
);

const fee = {
amount: [
{
denom: "uakt",
amount: "5000"
}
],
gas: "800000"
};

const signedMessage = await client.signAndBroadcast(
account.address,
[msgAny],
fee,
"create deployment"
);

console.log(signedMessage);

return Promise.reject(new Error(`Not implemented yet`));
}

async list(): Promise<QueryDeploymentResponse[]> {
const rpc = await getRpc(this.rpcEndpoint);
const request = QueryDeploymentsRequest.fromJSON({});
const client = new QueryClientImpl(rpc);
const response = await client.Deployments(request);
if (response?.deployments?.length > 0) {
return response.deployments;
}
return Promise.reject(new Error("No deployments found"));
}

async listByOwner(ownerAddress: string): Promise<QueryDeploymentResponse[]> {
const rpc = await getRpc(this.rpcEndpoint);
const request = QueryDeploymentsRequest.fromJSON({
filters: {
owner: ownerAddress
}
});
const client = new QueryClientImpl(rpc);
const response = await client.Deployments(request);
if (response?.deployments?.length > 0) {
return response.deployments;
}
return Promise.reject(
new Error(`No deployments found for owner ${ownerAddress}`)
);
}
}
12 changes: 6 additions & 6 deletions api/src/data/interfaces/data-sources/deployment-data-source.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import AkashQuery from "@akashnetwork/akashjs/build/protobuf/akash/deployment/v1beta3/query";
import { QueryDeploymentResponse } from "@akashnetwork/akashjs/build/protobuf/akash/deployment/v1beta3/query";
import { MsgCreateDeploymentResponse } from "@akashnetwork/akashjs/build/protobuf/akash/deployment/v1beta3/deploymentmsg";

export interface DeploymentDataSource {
getAll: () => Promise<AkashQuery.QueryDeploymentResponse[]>;
getByOwner: (
ownerAddress: string
) => Promise<AkashQuery.QueryDeploymentResponse[]>;
export interface IDeploymentDataSource {
create: () => Promise<MsgCreateDeploymentResponse>;
list: () => Promise<QueryDeploymentResponse[]>;
listByOwner: (ownerAddress: string) => Promise<QueryDeploymentResponse[]>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type Deployment } from "@src/domain/entities/deployment";

export interface DeploymentRepository {
getDeployments: () => Promise<Deployment[]>;
getDeploymentsByOwner: (ownerAddress: string) => Promise<Deployment[]>;
export interface IDeploymentRepository {
create: (deployment: Deployment) => Promise<Deployment>;
list: () => Promise<Deployment[]>;
listByOwner: (ownerAddress: string) => Promise<Deployment[]>;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Deployment } from "../../../entities/deployment";

export interface GetDeploymentsByOwnerUseCase {
export interface IListByOwnerUseCase {
execute: (ownerAddress: string) => Promise<Deployment[]>;
}
Loading

0 comments on commit 17df8f8

Please sign in to comment.