Skip to content

Commit

Permalink
chore(examples): make sure create_deployment.ts example work
Browse files Browse the repository at this point in the history
Also update types in other examples

refs akash-network/akashjs#184
  • Loading branch information
ygrishajev committed May 15, 2024
1 parent a4a632f commit c708994
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 181 deletions.
2 changes: 1 addition & 1 deletion .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": ["@commitlint/config-conventional"],
"rules": {
"scope-enum": [2, "always", ["certificates", "network", "wallet", "api", "stargate", "sdl"]]
"scope-enum": [2, "always", ["certificates", "network", "wallet", "api", "stargate", "sdl", "examples"]]
}
}
9 changes: 9 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,13 @@ When sending transactions, it can be useful to get an estimate of the gas requir
);

console.log(gas);
```

## Running an example

To run an example, you need to make the required changes to the code and use typescript compiler. You can use the following command to run the example. E.g. to run the `create_deployment.ts` example:

```bash
cd examples
ts-node -r tsconfig-paths/register create_deployment.ts
```
58 changes: 28 additions & 30 deletions examples/create_deployment.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import fs from "fs";
import path from "path";
import https from "https";

import { SigningStargateClient } from "@cosmjs/stargate";
import { DirectSecp256k1HdWallet, Registry } from "cosmwasm";

// these imports should point to @akashnetwork/akashjs node module in your project
import * as cert from "../build/certificates";
import { getRpc } from "../build/rpc";
import { SDL } from "../build/sdl";
import { getAkashTypeRegistry } from "../build/stargate";
import { QueryBidsRequest, QueryClientImpl as QueryMarketClient } from "../build/protobuf/akash/market/v1beta4/query";
import { QueryClientImpl as QueryProviderClient, QueryProviderRequest } from "../build/protobuf/akash/provider/v1beta3/query";
import { MsgCreateDeployment } from "../build/protobuf/akash/deployment/v1beta3/deploymentmsg";
import { MsgCreateLease } from "../build/protobuf/akash/market/v1beta4/lease";
import { BidID } from "../build/protobuf/akash/market/v1beta4/bid";
import { MsgCreateDeployment } from "@akashnetwork/akash-api/akash/deployment/v1beta3";
import { QueryClientImpl as QueryProviderClient, QueryProviderRequest } from "@akashnetwork/akash-api/akash/provider/v1beta3";
import { QueryBidsRequest, QueryClientImpl as QueryMarketClient, MsgCreateLease, BidID } from "@akashnetwork/akash-api/akash/market/v1beta4";
import * as cert from "@akashnetwork/akashjs/build/certificates";
import { getRpc } from "@akashnetwork/akashjs/build/rpc";
import { SDL } from "@akashnetwork/akashjs/build/sdl";
import { getAkashTypeRegistry } from "@akashnetwork/akashjs/build/stargate";
import { CertificatePem } from "@akashnetwork/akashjs/build/certificates/certificate-manager/CertificateManager";
import { certificateManager } from "@akashnetwork/akashjs/build/certificates/certificate-manager";

// update this with your wallet mnemonic
const rpcEndpoint = "https://rpc.akashnet.net:443";
const mnemonic = fs.readFileSync("./fixtures/mnemonic.txt", "utf8").trim();
const rawSDL = fs.readFileSync("./fixtures/example.sdl.yaml", "utf8");
const certificatePath = "./fixtures/cert.json";
const rpcEndpoint = "https://rpc.sandbox-01.aksh.pw";
// const rpcEndpoint = "https://rpc.akashnet.net:443";
const mnemonic = fs.readFileSync(path.resolve(__dirname, "./fixtures/mnemonic.txt"), "utf8").trim();
const rawSDL = fs.readFileSync(path.resolve(__dirname, "./fixtures/example.sdl.yaml"), "utf8");
const certificatePath = path.resolve(__dirname, "./fixtures/cert.json");

type Deployment = {
id: {
Expand All @@ -38,12 +37,6 @@ type Lease = {
};
};

type Certificate = {
csr: string;
privateKey: string;
publicKey: string;
};

// you can set this to a specific deployment sequence number to skip the deployment creation
const dseq = 0;

Expand All @@ -67,12 +60,12 @@ async function loadPrerequisites() {
}

// saves the certificate into the fixtures folder
function saveCertificate(certificate: { privateKey: string; publicKey: string; csr: string }) {
function saveCertificate(certificate: CertificatePem) {
const json = JSON.stringify(certificate);
fs.writeFileSync(certificatePath, json);
}

function loadCertificate(path: string): { csr: string; privateKey: string; publicKey: string } {
function loadCertificate(path: string): CertificatePem {
const json = fs.readFileSync(path, "utf8");

try {
Expand All @@ -91,7 +84,7 @@ async function loadOrCreateCertificate(wallet: DirectSecp256k1HdWallet, client:
}

// if not, create a new one
const certificate = await cert.createCertificate(accounts[0].address);
const certificate = certificateManager.generatePEM(accounts[0].address);
const result = await cert.broadcastCertificate(certificate, accounts[0].address, client);

if (result.code !== undefined && result.code === 0) {
Expand All @@ -104,7 +97,12 @@ async function loadOrCreateCertificate(wallet: DirectSecp256k1HdWallet, client:
}

async function walletFromMnemonic(mnemonic: string) {
return DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: "akash" });
try {
return await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: "akash" });
} catch (error) {
console.error('Could not create wallet from mnemonic, have you updated "examples/fixtures/mnemonic.txt?"');
throw error;
}
}

async function createDeployment(sdl: SDL, wallet: DirectSecp256k1HdWallet, client: SigningStargateClient) {
Expand Down Expand Up @@ -243,7 +241,7 @@ async function createLease(deployment: Deployment, wallet: DirectSecp256k1HdWall
throw new Error(`Could not create lease: ${tx.rawLog} `);
}

async function queryLeaseStatus(lease: Lease, providerUri: string, certificate: Certificate) {
async function queryLeaseStatus(lease: Lease, providerUri: string, certificate: CertificatePem) {
const id = lease.id;

if (id === undefined) {
Expand All @@ -253,7 +251,7 @@ async function queryLeaseStatus(lease: Lease, providerUri: string, certificate:
const leasePath = `/lease/${id.dseq}/${id.gseq}/${id.oseq}/status`;

const agent = new https.Agent({
cert: certificate.csr,
cert: certificate.cert,
key: certificate.privateKey,
rejectUnauthorized: false
});
Expand Down Expand Up @@ -290,7 +288,7 @@ async function queryLeaseStatus(lease: Lease, providerUri: string, certificate:
});
}

async function sendManifest(sdl: SDL, lease: Lease, wallet: DirectSecp256k1HdWallet, certificate: { csr: string; privateKey: string; publicKey: string }) {
async function sendManifest(sdl: SDL, lease: Lease, wallet: DirectSecp256k1HdWallet, certificate: { cert: string; privateKey: string; publicKey: string }) {
if (lease.id === undefined) {
throw new Error("Lease ID is undefined");
}
Expand All @@ -314,7 +312,7 @@ async function sendManifest(sdl: SDL, lease: Lease, wallet: DirectSecp256k1HdWal

const uri = new URL(providerInfo.hostUri);
const agent = new https.Agent({
cert: certificate.csr,
cert: certificate.cert,
key: certificate.privateKey,
rejectUnauthorized: false
});
Expand Down
2 changes: 1 addition & 1 deletion examples/details_of_single_provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryClientImpl, QueryProviderRequest, QueryProviderResponse } from "@akashnetwork/akashjs/build/protobuf/akash/provider/v1beta3/query";
import { QueryClientImpl, QueryProviderRequest, QueryProviderResponse } from "@akashnetwork/akash-api/akash/provider/v1beta3";
import { getRpc } from "@akashnetwork/akashjs/build/rpc";

async function main() {
Expand Down
6 changes: 2 additions & 4 deletions examples/estimate_gas.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing";
import { SigningStargateClient } from "@cosmjs/stargate";

// import the required message type from akashjs
import { getAkashTypeRegistry, getTypeUrl } from "@akashnetwork/akashjs/build/stargate/index";
import { MsgCloseDeployment } from "@akashnetwork/akashjs/build/protobuf/akash/deployment/v1beta3/deploymentmsg";
import { MsgCloseDeployment } from "@akashnetwork/akash-api/akash/deployment/v1beta3";
import { getAkashTypeRegistry, getTypeUrl } from "@akashnetwork/akashjs/build/stargate";

async function main() {
const mnemonic = "your wallet mnemonic";
Expand Down
64 changes: 42 additions & 22 deletions examples/fixtures/example.sdl.yaml
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
version: '2.0'
# Welcome to the Akash Network! 🚀☁
# This file is called a Stack Definition Laguage (SDL)
# SDL is a human friendly data standard for declaring deployment attributes.
# The SDL file is a "form" to request resources from the Network.
# SDL is compatible with the YAML standard and similar to Docker Compose files.

---
# Indicates version of Akash configuration file. Currently only "2.0" is accepted.
version: "2.0"

# The top-level services entry contains a map of workloads to be ran on the Akash deployment. Each key is a service name; values are a map containing the following keys:
# https://akash.network/docs/getting-started/stack-definition-language/#services
services:
tetris:
image: bsord/tetris
# The name of the service "web"
web:
# The docker container image with version. You must specify a version, the "latest" tag doesn't work.
image: akashlytics/hello-akash-world:0.2.0
# You can map ports here https://akash.network/docs/getting-started/stack-definition-language/#servicesexpose
expose:
- port: 80
- port: 3000
as: 80
to:
- global: true

# The profiles section contains named compute and placement profiles to be used in the deployment.
# https://akash.network/docs/getting-started/stack-definition-language/#profiles
profiles:
# profiles.compute is map of named compute profiles. Each profile specifies compute resources to be leased for each service instance uses uses the profile.
# https://akash.network/docs/getting-started/stack-definition-language/#profilescompute
compute:
tetris:
# The name of the service
web:
resources:
cpu:
units: 1
units: 0.5
memory:
size: 512Mi
storage:
- size: 512Mi
gpu:
units: 0
size: 512Mi

# profiles.placement is map of named datacenter profiles. Each profile specifies required datacenter attributes and pricing configuration for each compute profile that will be used within the datacenter. It also specifies optional list of signatures of which tenants expects audit of datacenter attributes.
# https://akash.network/docs/getting-started/stack-definition-language/#profilesplacement
placement:
akash:
attributes:
host: akash
signedBy:
anyOf:
- akash1365yvmc4s7awdyj3n2sav7xfx76adc6dnmlx63
- akash18qa2a2ltfyvkyj0ggj3hkvuj6twzyumuaru9s4
dcloud:
pricing:
tetris:
# The name of the service
web:
denom: uakt
amount: 10000
amount: 1000

# The deployment section defines how to deploy the services. It is a mapping of service name to deployment configuration.
# https://akash.network/docs/getting-started/stack-definition-language/#deployment
deployment:
tetris:
akash:
profile: tetris
count: 1
# The name of the service
web:
dcloud:
profile: web
count: 1
2 changes: 1 addition & 1 deletion examples/get_deployments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryDeploymentsResponse, QueryDeploymentsRequest, QueryClientImpl } from "@akashnetwork/akashjs/build/protobuf/akash/deployment/v1beta3/query";
import { QueryDeploymentsResponse, QueryDeploymentsRequest, QueryClientImpl } from "@akashnetwork/akash-api/akash/deployment/v1beta3";
import { getRpc } from "@akashnetwork/akashjs/build/rpc";

async function main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/get_lease_status.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryClientImpl, QueryLeaseRequest, QueryLeaseResponse } from "@akashnetwork/akashjs/build/protobuf/akash/market/v1beta3/query";
import { QueryClientImpl, QueryLeaseRequest, QueryLeaseResponse } from "@akashnetwork/akash-api/akash/market/v1beta3";
import { getRpc } from "@akashnetwork/akashjs/build/rpc";

async function main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/list_all_providers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryClientImpl, QueryProvidersRequest, QueryProvidersResponse } from "@akashnetwork/akashjs/build/protobuf/akash/provider/v1beta3/query";
import { QueryClientImpl, QueryProvidersRequest, QueryProvidersResponse } from "@akashnetwork/akash-api/akash/provider/v1beta3";
import { getRpc } from "@akashnetwork/akashjs/build/rpc";

async function main() {
Expand Down
6 changes: 2 additions & 4 deletions examples/take_down_deployment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing";
import { SigningStargateClient } from "@cosmjs/stargate";

// import the required message type from akashjs
import { getAkashTypeRegistry, getTypeUrl } from "@akashnetwork/akashjs/build/stargate/index";
import { MsgCloseDeployment } from "@akashnetwork/akashjs/build/protobuf/akash/deployment/v1beta3/deploymentmsg";
import { getAkashTypeRegistry, getTypeUrl } from "@akashnetwork/akashjs/build/stargate";
import { MsgCloseDeployment } from "@akashnetwork/akash-api/akash/deployment/v1beta3";

async function main() {
const mnemonic = "your wallet mnemonic";
Expand Down
8 changes: 8 additions & 0 deletions examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.build.json",
"compilerOptions": {
"paths": {
"@akashnetwork/akashjs/build/*": ["../src/*"]
}
}
}
32 changes: 28 additions & 4 deletions package-lock.json

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

Loading

0 comments on commit c708994

Please sign in to comment.