Skip to content

Commit

Permalink
chore: refactor CosmosClient
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Sep 22, 2023
1 parent 5dcb4e9 commit dee8b25
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
66 changes: 41 additions & 25 deletions packages/axelar-local-dev-cosmos/src/CosmosClient.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,64 @@
import fs from "fs";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { SigningStargateClient, StargateClient } from "@cosmjs/stargate";
import { CosmosChainInfo } from "./types";
import { GasPrice } from "@cosmjs/stargate";
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { Decimal } from "@cosmjs/math";
import { CosmosChainInfo } from "./types";

export class CosmosClient {
chainInfo: CosmosChainInfo;
owner: DirectSecp256k1HdWallet | undefined;
client: SigningCosmWasmClient | undefined;
owner: DirectSecp256k1HdWallet;
client: SigningCosmWasmClient;

constructor(chainInfo: CosmosChainInfo) {
private constructor(
chainInfo: CosmosChainInfo,
owner: DirectSecp256k1HdWallet,
client: SigningCosmWasmClient
) {
this.chainInfo = chainInfo;
this.owner = owner;
this.client = client;
}

async init() {
this.owner = await DirectSecp256k1HdWallet.fromMnemonic(
this.chainInfo.owner.mnemonic,
{
prefix: "wasm",
}
static async create(chainInfo: CosmosChainInfo) {
const walletOptions = {
prefix: "wasm",
};
const clientOptions = {
gasPrice: new GasPrice(Decimal.fromAtomics("1", 6), chainInfo.denom),
};

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

this.client = await SigningCosmWasmClient.connectWithSigner(
this.chainInfo.rpcUrl,
this.owner
const client = await SigningCosmWasmClient.connectWithSigner(
chainInfo.rpcUrl,
owner,
clientOptions
);

return new CosmosClient(chainInfo, owner, client);
}

getBalance(address: string) {
return this.client
?.getBalance(address, this.chainInfo.denom)
.getBalance(address, this.chainInfo.denom)
.then((res) => res.amount);
}

async getOwnerBalance() {
if (!this.owner) {
throw new Error(
"The init method must be called before calling this method"
);
}
async uploadWasm(path: string) {
const wasm = fs.readFileSync(path);

const addresses = await this.owner
.getAccounts()
.then((accounts) => accounts.map((account) => account.address));
return this.client.upload(
this.chainInfo.owner.address,
new Uint8Array(wasm),
"auto"
);
}

return this.getBalance(addresses[0]);
async getOwnerBalance() {
return this.owner.getAccounts().then((accounts) => accounts[0].address);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { CosmosClient } from "../CosmosClient";
import fs from "fs";
import path from "path";

describe("CosmosClient", () => {
let cosmosClient: CosmosClient;

beforeAll(async () => {
const config = JSON.parse(fs.readFileSync("./config.json", "utf-8"));
cosmosClient = new CosmosClient(config);
await cosmosClient.init();
cosmosClient = await CosmosClient.create(config);
});

it("should query the balance", async () => {
const balance = await cosmosClient.getOwnerBalance();
expect(parseInt(balance || "0")).toBeGreaterThan(0);
});

it.only("should be able to upload wasm contract", async () => {
const _path = path.resolve(__dirname, "../..", "wasm/multi_send.wasm");
console.log("contract path:", _path);
const response = await cosmosClient.uploadWasm(_path);

console.log(response);
});
});

0 comments on commit dee8b25

Please sign in to comment.