diff --git a/packages/axelar-local-dev-cosmos/src/CosmosClient.ts b/packages/axelar-local-dev-cosmos/src/CosmosClient.ts index 0fddad60..b30a4ca8 100644 --- a/packages/axelar-local-dev-cosmos/src/CosmosClient.ts +++ b/packages/axelar-local-dev-cosmos/src/CosmosClient.ts @@ -6,12 +6,12 @@ import { Decimal } from "@cosmjs/math"; import { CosmosChainInfo } from "./types"; export class CosmosClient { - chainInfo: CosmosChainInfo; + chainInfo: Required; owner: DirectSecp256k1HdWallet; - client: SigningCosmWasmClient; + public client: SigningCosmWasmClient; private constructor( - chainInfo: CosmosChainInfo, + chainInfo: Required, owner: DirectSecp256k1HdWallet, client: SigningCosmWasmClient ) { @@ -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", }; @@ -29,7 +36,7 @@ export class CosmosClient { }; const owner = await DirectSecp256k1HdWallet.fromMnemonic( - chainInfo.owner.mnemonic, + config?.owner.mnemonic, walletOptions ); @@ -58,7 +65,7 @@ export class CosmosClient { ); } - async getOwnerBalance() { + async getOwnerAccount() { return this.owner.getAccounts().then((accounts) => accounts[0].address); } } diff --git a/packages/axelar-local-dev-cosmos/src/__tests__/docker.spec.ts b/packages/axelar-local-dev-cosmos/src/__tests__/docker.spec.ts index e7986395..24bb5c03 100644 --- a/packages/axelar-local-dev-cosmos/src/__tests__/docker.spec.ts +++ b/packages/axelar-local-dev-cosmos/src/__tests__/docker.spec.ts @@ -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 () => { diff --git a/packages/axelar-local-dev-cosmos/src/lib/docker.ts b/packages/axelar-local-dev-cosmos/src/lib/docker.ts index 125ad744..aa249734 100644 --- a/packages/axelar-local-dev-cosmos/src/lib/docker.ts +++ b/packages/axelar-local-dev-cosmos/src/lib/docker.ts @@ -73,8 +73,17 @@ export async function start(options?: StartOptions): Promise { 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, { @@ -84,13 +93,8 @@ export async function start(options?: StartOptions): Promise { .then((accounts) => accounts[0].address); return { - owner: { - mnemonic, - address, - }, - denom: chain.denom, - lcdUrl: `http://localhost:${chain.port}`, - rpcUrl: `http://localhost:${chain.rpcPort}`, + mnemonic, + address, }; } diff --git a/packages/axelar-local-dev-cosmos/src/types.ts b/packages/axelar-local-dev-cosmos/src/types.ts index 0dce7e7b..aed472dd 100644 --- a/packages/axelar-local-dev-cosmos/src/types.ts +++ b/packages/axelar-local-dev-cosmos/src/types.ts @@ -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 {