-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
12 changes: 10 additions & 2 deletions
12
packages/axelar-local-dev-cosmos/src/__tests__/CosmosClient.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |