Skip to content

Commit

Permalink
feat: ensures tests pass on api with some sdk-47 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrishajev committed Dec 20, 2024
1 parent bd487a8 commit ddaba5d
Show file tree
Hide file tree
Showing 30 changed files with 274 additions and 1,134 deletions.
11 changes: 7 additions & 4 deletions apps/api/env/.env.functional.test
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
AKASH_SANDBOX_DATABASE_CS=postgres://postgres:password@localhost:5432/console-akash-sandbox
USER_DATABASE_CS=postgres://postgres:password@localhost:5432/console-users
POSTGRES_DB_URI=postgres://postgres:password@localhost:5432/console-users
CHAIN_INDEXER_POSTGRES_DB_URI=postgres://postgres:password@localhost:5432/console-akash-sandbox
MASTER_WALLET_MNEMONIC="motion isolate mother convince snack twenty tumble boost elbow bundle modify balcony"
UAKT_TOP_UP_MASTER_WALLET_MNEMONIC="since bread kind field rookie stairs elephant tent horror rice gain tongue collect goose rural garment cover client biology toe ability boat afford mind"
USDC_TOP_UP_MASTER_WALLET_MNEMONIC="leaf brush weapon puppy depart hockey walnut hospital orphan require unfair hunt ribbon toe cereal eagle hour door awesome dress mouse when phone return"
NETWORK=sandbox
RPC_NODE_ENDPOINT=https://rpc.sandbox-01.aksh.pw:443
#RPC_NODE_ENDPOINT=https://rpc.sandbox-01.aksh.pw:443
RPC_NODE_ENDPOINT=https://testnetrpc.akashnet.net:443
API_NODE_ENDPOINT=http://api.dev.akash.pub:1317
TRIAL_DEPLOYMENT_ALLOWANCE_AMOUNT=20000000
DEPLOYMENT_ALLOWANCE_REFILL_AMOUNT=20000000
DEPLOYMENT_ALLOWANCE_REFILL_THRESHOLD=2000000
Expand All @@ -20,4 +21,6 @@ STRIPE_SECRET_KEY=STRIPE_SECRET_KEY
STRIPE_PRODUCT_ID=STRIPE_PRODUCT_ID
STRIPE_WEBHOOK_SECRET=STRIPE_WEBHOOK_SECRET
ALLOWED_CHECKOUT_REFERRERS=["http://localhost:3000"]
STRIPE_CHECKOUT_REDIRECT_URL=http://localhost:3000
STRIPE_CHECKOUT_REDIRECT_URL=http://localhost:3000
STD_OUT_LOG_FORMAT=pretty
SQL_LOG_FORMAT=pretty
3 changes: 0 additions & 3 deletions apps/api/env/.env.local.sample
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
NETWORK=sandbox
DB_HOST_WITH_DEFAULT=${DB_HOST:-localhost}
AKASH_DATABASE_CS=postgres://postgres:password@${DB_HOST_WITH_DEFAULT}:5432/console-akash-sandbox
AKASH_SANDBOX_DATABASE_CS=postgres://postgres:password@${DB_HOST_WITH_DEFAULT}:5432/console-akash-sandbox
USER_DATABASE_CS=postgres://postgres:password@${DB_HOST_WITH_DEFAULT}:5432/console-users
POSTGRES_DB_URI=postgres://postgres:password@${DB_HOST_WITH_DEFAULT}:5432/console-users
ANONYMOUS_USER_TOKEN_SECRET=ANONYMOUS_USER_TOKEN_SECRET
MASTER_WALLET_MNEMONIC=MASTER_WALLET_MNEMONIC
Expand Down
1 change: 1 addition & 0 deletions apps/api/env/.env.mainnet
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
RPC_NODE_ENDPOINT=https://consolerpc.akashnet.net
API_NODE_ENDPOINT=https://api.akashnet.net:443
DEPLOYMENT_GRANT_DENOM=ibc/170C677610AC31DF0904FFE09CD3B5C657492170E7E52372E48756B71E56F2F1
2 changes: 0 additions & 2 deletions apps/api/env/.env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Secrets
AKASH_DATABASE_CS=
GITHUB_PAT=
ANONYMOUS_USER_TOKEN_SECRET=
HealthChecks_SyncAKTMarketData=
Expand All @@ -9,7 +8,6 @@ SECRET_TOKEN=
SENTRY_DSN=
STRIPE_SECRET_KEY=
STRIPE_PRODUCT_ID=
USER_DATABASE_CS=

# Configuration
CORS_WEBSITE_URLS=
Expand Down
1 change: 1 addition & 0 deletions apps/api/env/.env.sandbox
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
RPC_NODE_ENDPOINT=https://rpc.sandbox-01.aksh.pw:443
API_NODE_ENDPOINT=https://api.sandbox-01.aksh.pw
DEPLOYMENT_GRANT_DENOM=ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84
3 changes: 3 additions & 0 deletions apps/api/env/.env.testnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
API_NODE_ENDPOINT=http://api.dev.akash.pub:1317
RPC_NODE_ENDPOINT=https://testnetrpc.akashnet.net:443
DEPLOYMENT_GRANT_DENOM=ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84
4 changes: 2 additions & 2 deletions apps/api/env/.env.unit.test
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
AKASH_SANDBOX_DATABASE_CS=postgres://postgres:password@localhost:5432/console-akash-sandbox
USER_DATABASE_CS=postgres://postgres:password@localhost:5432/console-users
POSTGRES_DB_URI=postgres://postgres:password@localhost:5432/console-users
CHAIN_INDEXER_POSTGRES_DB_URI=postgres://postgres:password@localhost:5432/console-akash-sandbox
MASTER_WALLET_MNEMONIC="motion isolate mother convince snack twenty tumble boost elbow bundle modify balcony"
UAKT_TOP_UP_MASTER_WALLET_MNEMONIC="since bread kind field rookie stairs elephant tent horror rice gain tongue collect goose rural garment cover client biology toe ability boat afford mind"
USDC_TOP_UP_MASTER_WALLET_MNEMONIC="leaf brush weapon puppy depart hockey walnut hospital orphan require unfair hunt ribbon toe cereal eagle hour door awesome dress mouse when phone return"
NETWORK=sandbox
API_NODE_ENDPOINT=https://api.sandbox-01.aksh.pw:443
RPC_NODE_ENDPOINT=https://rpc.sandbox-01.aksh.pw:443
TRIAL_DEPLOYMENT_ALLOWANCE_AMOUNT=20000000
DEPLOYMENT_ALLOWANCE_REFILL_AMOUNT=20000000
Expand Down
4 changes: 2 additions & 2 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"test:watch": "jest --selectProjects unit functional --watch"
},
"dependencies": {
"@akashnetwork/akash-api": "^1.3.0",
"@akashnetwork/akashjs": "^0.10.0",
"@akashnetwork/akash-api": "file:../../../../go-projects/akash-api/ts",
"@akashnetwork/akashjs": "file:../../../akashjs",
"@akashnetwork/database": "*",
"@akashnetwork/env-loader": "*",
"@akashnetwork/http-sdk": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export const SignTxRequestInputSchema = z.object({
.array(
z.object({
typeUrl: z.enum([
"/akash.deployment.v1beta3.MsgCreateDeployment",
"/akash.cert.v1beta3.MsgCreateCertificate",
"/akash.deployment.v1beta4.MsgCreateDeployment",
"/akash.cert.v1.MsgCreateCertificate",
"/akash.market.v1beta4.MsgCreateLease",
"/akash.deployment.v1beta3.MsgUpdateDeployment",
"/akash.deployment.v1beta3.MsgCloseDeployment",
"/akash.deployment.v1beta3.MsgDepositDeployment"
"/akash.deployment.v1beta4.MsgUpdateDeployment",
"/akash.deployment.v1beta4.MsgCloseDeployment",
"/akash.deployment.v1beta4.MsgDepositDeployment"
]),
value: z.string()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,25 @@ export class ManagedSignerService {
throw this.chainErrorService.toAppError(error, decodedMessages);
}

const tx = await this.executeManagedTx(userWallet.id, decodedMessages);
try {
const tx = await this.executeManagedTx(userWallet.id, decodedMessages);

await this.balancesService.refreshUserWalletLimits(userWallet);
await this.balancesService.refreshUserWalletLimits(userWallet);

const result = pick(tx, ["code", "hash", "transactionHash", "rawLog"]);
const result = pick(tx, ["code", "hash", "transactionHash", "rawLog"]);

if (result.hash) {
return {
...result,
transactionHash: result.hash
};
}
if (result.hash) {
return {
...result,
transactionHash: result.hash
};
}

return result;
return result;
} catch (error) {
console.log("DEBUG error", error);
throw error;
}
}

private decodeMessages(messages: StringifiedEncodeObject[]): EncodeObject[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DepositDeploymentAuthorization, MsgCloseDeployment, MsgDepositDeployment } from "@akashnetwork/akash-api/v1beta3";
import { DepositAuthorization, MsgDepositDeployment } from "@akashnetwork/akash-api/v1";
import { MsgCloseDeployment } from "@akashnetwork/akash-api/v1beta4";
import { MsgExec, MsgRevoke } from "cosmjs-types/cosmos/authz/v1beta1/tx";
import { BasicAllowance } from "cosmjs-types/cosmos/feegrant/v1beta1/feegrant";
import { MsgGrantAllowance } from "cosmjs-types/cosmos/feegrant/v1beta1/tx";
Expand Down Expand Up @@ -68,9 +69,9 @@ export class RpcMessageService {
grantee,
grant: {
authorization: {
typeUrl: `/${DepositDeploymentAuthorization.$type}`,
value: DepositDeploymentAuthorization.encode(
DepositDeploymentAuthorization.fromPartial({
typeUrl: `/${DepositAuthorization.$type}`,
value: DepositAuthorization.encode(
DepositAuthorization.fromPartial({
spendLimit: {
denom,
amount: limit.toString()
Expand Down Expand Up @@ -106,7 +107,7 @@ export class RpcMessageService {
value: MsgRevoke.fromPartial({
granter: granter,
grantee: grantee,
msgTypeUrl: "/akash.deployment.v1beta3.MsgDepositDeployment"
msgTypeUrl: `/${MsgDepositDeployment.$type}`
})
};
}
Expand All @@ -125,7 +126,7 @@ export class RpcMessageService {

getDepositDeploymentMsg({ owner, dseq, amount, denom, depositor }: DepositDeploymentMsgOptions) {
return {
typeUrl: "/akash.deployment.v1beta3.MsgDepositDeployment",
typeUrl: `/${MsgDepositDeployment.$type}`,
value: {
id: {
owner,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as v1beta3 from "@akashnetwork/akash-api/v1beta3";
import { MsgCreateDeployment } from "@akashnetwork/akash-api/v1beta4";
import { EncodeObject } from "@cosmjs/proto-signing";
import { singleton } from "tsyringe";

Expand All @@ -8,8 +8,8 @@ import { AUDITOR, TRIAL_ATTRIBUTE } from "@src/deployment/config/provider.config
@singleton()
export class TrialValidationService {
async validateLeaseProviders(decoded: EncodeObject, userWallet: UserWalletOutput) {
if (userWallet.isTrialing && decoded.typeUrl === "/akash.deployment.v1beta3.MsgCreateDeployment") {
const value = decoded.value as v1beta3.MsgCreateDeployment;
if (userWallet.isTrialing && decoded.typeUrl === "/akash.deployment.v1beta4.MsgCreateDeployment") {
const value = decoded.value as MsgCreateDeployment;

value.groups.forEach(group => {
const hasTrial = group.requirements.attributes.some(attribute => {
Expand Down
7 changes: 7 additions & 0 deletions apps/api/src/chain/config/env.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from "zod";

export const envSchema = z.object({
API_NODE_ENDPOINT: z.string(),
RPC_NODE_ENDPOINT: z.string(),
CHAIN_INDEXER_POSTGRES_DB_URI: z.string()
});
11 changes: 11 additions & 0 deletions apps/api/src/chain/services/chain-config/chain-config.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { singleton } from "tsyringe";

import { envSchema } from "@src/chain/config/env.config";
import { ConfigService } from "@src/core/services/config/config.service";

@singleton()
export class ChainConfigService extends ConfigService<typeof envSchema, unknown> {
constructor() {
super({ envSchema });
}
}
2 changes: 1 addition & 1 deletion apps/api/src/core/config/env.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod";

const envSchema = z.object({
export const envSchema = z.object({
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace"]).optional().default("info"),
STD_OUT_LOG_FORMAT: z.enum(["json", "pretty"]).optional().default("json"),
SQL_LOG_FORMAT: z.enum(["raw", "pretty"]).optional().default("raw"),
Expand Down
11 changes: 11 additions & 0 deletions apps/api/src/core/services/core-config/core-config.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { singleton } from "tsyringe";

import { envSchema } from "@src/core/config/env.config";
import { ConfigService } from "@src/core/services/config/config.service";

@singleton()
export class CoreConfigService extends ConfigService<typeof envSchema, unknown> {
constructor() {
super({ envSchema });
}
}
57 changes: 10 additions & 47 deletions apps/api/src/db/dbConnection.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
import { chainDefinitions } from "@akashnetwork/database/chainDefinitions";
import { chainModels, getChainModels, userModels } from "@akashnetwork/database/dbSchemas";
import { chainModels, userModels } from "@akashnetwork/database/dbSchemas";
import { Template, TemplateFavorite, UserSetting } from "@akashnetwork/database/dbSchemas/user";
import pg from "pg";
import { Transaction as DbTransaction } from "sequelize";
import { Sequelize } from "sequelize-typescript";
import { container } from "tsyringe";

import { config } from "@src/core/config";
import { ChainConfigService } from "@src/chain/services/chain-config/chain-config.service";
import { CoreConfigService } from "@src/core/services/core-config/core-config.service";
import { PostgresLoggerService } from "@src/core/services/postgres-logger/postgres-logger.service";
import { env } from "@src/utils/env";

function isValidNetwork(network: string): network is keyof typeof csMap {
return network in csMap;
}

const csMap = {
mainnet: env.AKASH_DATABASE_CS,
testnet: env.AKASH_TESTNET_DATABASE_CS,
sandbox: env.AKASH_SANDBOX_DATABASE_CS
};

if (!isValidNetwork(env.NETWORK)) {
throw new Error(`Invalid network: ${env.NETWORK}`);
}
const indexerDbUri = container.resolve(ChainConfigService).get("CHAIN_INDEXER_POSTGRES_DB_URI");
const coreConfig = container.resolve(CoreConfigService);
const dbUri = coreConfig.get("POSTGRES_DB_URI");

if (!csMap[env.NETWORK]) {
throw new Error(`Missing connection string for network: ${env.NETWORK}`);
}

const logger = new PostgresLoggerService({ orm: "sequelize", useFormat: config.SQL_LOG_FORMAT === "pretty" });
const logger = new PostgresLoggerService({ orm: "sequelize", useFormat: coreConfig.get("SQL_LOG_FORMAT") === "pretty" });
const logging = (msg: string) => logger.write(msg);

pg.defaults.parseInt8 = true;
export const chainDb = new Sequelize(csMap[env.NETWORK], {
export const chainDb = new Sequelize(indexerDbUri, {
dialectModule: pg,
logging,
logQueryParameters: true,
Expand All @@ -43,28 +29,7 @@ export const chainDb = new Sequelize(csMap[env.NETWORK], {
models: chainModels
});

export const chainDbs: { [key: string]: Sequelize } = Object.keys(chainDefinitions)
.filter(x => chainDefinitions[x].connectionString)
.reduce(
(obj, chain) => ({
...obj,
[chain]: new Sequelize(chainDefinitions[chain].connectionString, {
dialectModule: pg,
logging,
logQueryParameters: true,
repositoryMode: true,
transactionType: DbTransaction.TYPES.IMMEDIATE,
define: {
timestamps: false,
freezeTableName: true
},
models: getChainModels(chain)
})
}),
{}
);

export const userDb = new Sequelize(env.USER_DATABASE_CS, {
export const userDb = new Sequelize(dbUri, {
dialectModule: pg,
logging,
logQueryParameters: true,
Expand All @@ -81,5 +46,3 @@ export async function syncUserSchema() {
await Template.sync();
await TemplateFavorite.sync();
}

export const closeConnections = async () => await Promise.all([chainDb.close(), userDb.close(), ...Object.values(chainDbs).map(db => db.close())]);
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ describe(TopUpCustodialDeploymentsService.name, () => {
grantee: grant.grantee,
msgs: [
{
typeUrl: "/akash.deployment.v1beta3.MsgDepositDeployment",
value: expect.any(Buffer)
typeUrl: "/akash.deployment.v1.MsgDepositDeployment",
value: expect.any(Uint8Array)
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "@test/mocks/logger-service.mock";

import { MsgDepositDeployment } from "@akashnetwork/akash-api/v1";
import { faker } from "@faker-js/faker";

import { BillingConfig } from "@src/billing/providers";
Expand Down Expand Up @@ -107,7 +108,7 @@ describe(TopUpManagedDeploymentsService.name, () => {
if (isExpectedToTopUp) {
expect(managedSignerService.executeManagedTx).toHaveBeenCalledWith(wallet.id, [
{
typeUrl: "/akash.deployment.v1beta3.MsgDepositDeployment",
typeUrl: `/${MsgDepositDeployment.$type}`,
value: {
id: {
owner: wallet.address,
Expand Down
12 changes: 4 additions & 8 deletions apps/api/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { env } from "./env";
import { container } from "tsyringe";

import { ChainConfigService } from "@src/chain/services/chain-config/chain-config.service";

export const averageBlockTime = 6.174;
export const averageDaysInMonth = 30.437;
Expand All @@ -14,12 +16,6 @@ export const openApiExampleProviderAddress = "akash18ga02jzaq8cw52anyhzkwta5wygu
export const openApiExampleTransactionHash = "A19F1950D97E576F0D7B591D71A8D0366AA8BA0A7F3DA76F44769188644BE9EB";
export const openApiExampleValidatorAddress = "akashvaloper14mt78hz73d9tdwpdvkd59ne9509kxw8yj7qy8f";

export const defaultNodeUrlMapping: { [key: string]: string } = {
mainnet: "https://api.akashnet.net:443",
sandbox: "https://api.sandbox-01.aksh.pw",
testnet: "https://api.testnet-02.aksh.pw"
};

export const apiNodeUrl = env.REST_API_NODE_URL ?? defaultNodeUrlMapping[env.NETWORK] ?? defaultNodeUrlMapping.mainnet;
export const apiNodeUrl = container.resolve(ChainConfigService).get("API_NODE_ENDPOINT");
export const betaTypeVersion = "v1beta3";
export const betaTypeVersionMarket = "v1beta4";
4 changes: 0 additions & 4 deletions apps/api/src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ export const env = z
CORS_WEBSITE_URLS: z.string().optional(),
NODE_ENV: z.string().optional(),
HEALTHCHECKS_ENABLED: z.string().optional(),
AKASH_DATABASE_CS: z.string().optional(),
AKASH_TESTNET_DATABASE_CS: z.string().optional(),
AKASH_SANDBOX_DATABASE_CS: z.string().optional(),
USER_DATABASE_CS: z.string().optional(),
NETWORK: z.string().default("mainnet"),
REST_API_NODE_URL: z.string().optional(),
SERVER_ORIGIN: z.string().optional().default("http://localhost:3080"),
Expand Down
Loading

0 comments on commit ddaba5d

Please sign in to comment.