From dee8b251d2863865dac70820bfca458d86dba101 Mon Sep 17 00:00:00 2001 From: npty Date: Fri, 22 Sep 2023 19:32:29 +0700 Subject: [PATCH] chore: refactor CosmosClient --- .../src/CosmosClient.ts | 66 ++++++++++++------- .../src/__tests__/CosmosClient.spec.ts | 12 +++- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/packages/axelar-local-dev-cosmos/src/CosmosClient.ts b/packages/axelar-local-dev-cosmos/src/CosmosClient.ts index 53e5700c..0fddad60 100644 --- a/packages/axelar-local-dev-cosmos/src/CosmosClient.ts +++ b/packages/axelar-local-dev-cosmos/src/CosmosClient.ts @@ -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); } } diff --git a/packages/axelar-local-dev-cosmos/src/__tests__/CosmosClient.spec.ts b/packages/axelar-local-dev-cosmos/src/__tests__/CosmosClient.spec.ts index f5955af7..56cb8db6 100644 --- a/packages/axelar-local-dev-cosmos/src/__tests__/CosmosClient.spec.ts +++ b/packages/axelar-local-dev-cosmos/src/__tests__/CosmosClient.spec.ts @@ -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); + }); });