Skip to content

Commit

Permalink
chore: refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Oct 23, 2023
1 parent 8a6c43d commit d9f2b72
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 28 deletions.
19 changes: 13 additions & 6 deletions packages/axelar-local-dev-cosmos/src/CosmosClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { Decimal } from "@cosmjs/math";
import { CosmosChainInfo } from "./types";

export class CosmosClient {
chainInfo: CosmosChainInfo;
chainInfo: Required<CosmosChainInfo>;
owner: DirectSecp256k1HdWallet;
client: SigningCosmWasmClient;
public client: SigningCosmWasmClient;

private constructor(
chainInfo: CosmosChainInfo,
chainInfo: Required<CosmosChainInfo>,
owner: DirectSecp256k1HdWallet,
client: SigningCosmWasmClient
) {
Expand All @@ -20,7 +20,14 @@ export class CosmosClient {
this.client = client;
}

static async create(chainInfo: CosmosChainInfo) {
static async create(config: CosmosChainInfo) {
const chainInfo = {
...config,
denom: config.denom || "udemo",
lcdUrl: config.lcdUrl || "http://localhost:1317",
rpcUrl: config.rpcUrl || "http://localhost:26657",
};

const walletOptions = {
prefix: "wasm",
};
Expand All @@ -29,7 +36,7 @@ export class CosmosClient {
};

const owner = await DirectSecp256k1HdWallet.fromMnemonic(
chainInfo.owner.mnemonic,
config?.owner.mnemonic,
walletOptions
);

Expand Down Expand Up @@ -58,7 +65,7 @@ export class CosmosClient {
);
}

async getOwnerBalance() {
async getOwnerAccount() {
return this.owner.getAccounts().then((accounts) => accounts[0].address);
}
}
39 changes: 28 additions & 11 deletions packages/axelar-local-dev-cosmos/src/__tests__/docker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
import { setLogger } from "@axelar-network/axelar-local-dev";
import { start, stop } from "../lib/docker";
import { getOwnerAccount, start, stop } from "../lib/docker";
import { CosmosClient } from "../CosmosClient";
import fetch from "node-fetch";

setLogger(() => undefined);

describe("docker", () => {
const chain = {
name: "testchain",
port: 1317,
rpcPort: 26657,
denom: "testdenom",
};

it("should start Cosmos container successfully", async () => {
const customDenom = "testdenom";
const { owner, rpcUrl, lcdUrl, denom } = await start({
chain: {
denom: customDenom,
name: "testchain",
port: 1317,
rpcPort: 26657,
},
});
const { owner, rpcUrl, lcdUrl, denom } = await start({ chain });

expect(owner.mnemonic).toBeDefined();
expect(owner.address).toBeDefined();
expect(rpcUrl).toBeDefined();
expect(lcdUrl).toBeDefined();
expect(denom).toBe(customDenom);
expect(denom).toBe(chain.denom);
});

it.only('should start Cosmos container with default denom "udemo"', async () => {
const config = await start({
chain,
});

const cosmosClient = await CosmosClient.create({
owner: await getOwnerAccount(chain.name),
});

const owner = await cosmosClient.getOwnerAccount();
const balance = await cosmosClient.getBalance(owner);
console.log(balance);

expect(parseInt(balance)).toBeGreaterThan(1);
expect(config.denom).toBe(chain.denom);
});

it("should stop Cosmos container gracefully", async () => {
Expand Down
20 changes: 12 additions & 8 deletions packages/axelar-local-dev-cosmos/src/lib/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,17 @@ export async function start(options?: StartOptions): Promise<CosmosChainInfo> {

logger.log("Cosmos started");

return {
owner: await getOwnerAccount(chain.name),
denom: chain.denom,
lcdUrl: `http://localhost:${chain.port}`,
rpcUrl: `http://localhost:${chain.rpcPort}`,
};
}

export async function getOwnerAccount(chainName: string) {
// Get mnemonic and address from the container
const homedir = `./private/.${chain.name}`;
const homedir = `./private/.${chainName}`;
const homePath = path.join(dockerPath, homedir);
const mnemonic = fs.readFileSync(`${homePath}/mnemonic.txt`, "utf8");
const address = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
Expand All @@ -84,13 +93,8 @@ export async function start(options?: StartOptions): Promise<CosmosChainInfo> {
.then((accounts) => accounts[0].address);

return {
owner: {
mnemonic,
address,
},
denom: chain.denom,
lcdUrl: `http://localhost:${chain.port}`,
rpcUrl: `http://localhost:${chain.rpcPort}`,
mnemonic,
address,
};
}

Expand Down
6 changes: 3 additions & 3 deletions packages/axelar-local-dev-cosmos/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export interface CosmosChainInfo {
mnemonic: string;
address: string;
};
denom: string;
rpcUrl: string;
lcdUrl: string;
denom?: string;
rpcUrl?: string;
lcdUrl?: string;
}

export interface CosmosChainOptions {
Expand Down

0 comments on commit d9f2b72

Please sign in to comment.