Skip to content

Commit

Permalink
Tests pass for SigningCosmosClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Aug 19, 2020
1 parent 3be8c87 commit 50800f5
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/launchpad/src/cosmosclient.searchtx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("CosmosClient.searchTx", () => {
const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
const accounts = await wallet.getAccounts();
const [{ address: walletAddress }] = accounts;
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, wallet);
const client = SigningCosmosClient.fromOfflineSigner(wasmd.endpoint, faucet.address, wallet);

{
const memo = "Sending more than I can afford";
Expand Down
2 changes: 1 addition & 1 deletion packages/launchpad/src/lcdapi/distribution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe("DistributionExtension", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, wallet, {});
const client = SigningCosmosClient.fromOfflineSigner(wasmd.endpoint, faucet.address, wallet, {});

const chainId = await client.getChainId();
const msg: MsgDelegate = {
Expand Down
2 changes: 1 addition & 1 deletion packages/launchpad/src/lcdapi/gov.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("GovExtension", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, wallet, {});
const client = SigningCosmosClient.fromOfflineSigner(wasmd.endpoint, faucet.address, wallet, {});

const chainId = await client.getChainId();
const proposalMsg = {
Expand Down
4 changes: 2 additions & 2 deletions packages/launchpad/src/lcdapi/lcdclient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe("LcdClient", () => {
const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
const accounts = await wallet.getAccounts();
const [{ address: walletAddress }] = accounts;
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, wallet);
const client = SigningCosmosClient.fromOfflineSigner(wasmd.endpoint, faucet.address, wallet);

{
const recipient = makeRandomAddress();
Expand Down Expand Up @@ -325,7 +325,7 @@ describe("LcdClient", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, wallet);
const client = SigningCosmosClient.fromOfflineSigner(wasmd.endpoint, faucet.address, wallet);

const recipient = makeRandomAddress();
const transferAmount = [
Expand Down
2 changes: 1 addition & 1 deletion packages/launchpad/src/lcdapi/staking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("StakingExtension", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, wallet, {});
const client = SigningCosmosClient.fromOfflineSigner(wasmd.endpoint, faucet.address, wallet, {});

const chainId = await client.getChainId();
{
Expand Down
54 changes: 27 additions & 27 deletions packages/launchpad/src/signingcosmosclient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { assert } from "@cosmjs/utils";

import { Coin, coin, coins } from "./coins";
import { Coin } from "./coins";
import { assertIsBroadcastTxSuccess, PrivateCosmosClient } from "./cosmosclient";
import { MsgDelegate } from "./msgs";
import { Secp256k1Wallet } from "./secp256k1wallet";
import { SigningCosmosClient } from "./signingcosmosclient";
import { makeRandomAddress, pendingWithoutWasmd, validatorAddress } from "./testutils.spec";
import { makeRandomAddress, pendingWithoutWasmd } from "./testutils.spec";

const httpUrl = "http://localhost:1317";

Expand All @@ -24,7 +23,7 @@ describe("SigningCosmosClient", () => {
describe("makeReadOnly", () => {
it("can be constructed", async () => {
const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
const client = new SigningCosmosClient(httpUrl, faucet.address, wallet);
const client = SigningCosmosClient.fromOfflineSigner(httpUrl, faucet.address, wallet);
expect(client).toBeTruthy();
});
});
Expand All @@ -33,7 +32,7 @@ describe("SigningCosmosClient", () => {
it("always uses authAccount implementation", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
const client = new SigningCosmosClient(httpUrl, faucet.address, wallet);
const client = SigningCosmosClient.fromOfflineSigner(httpUrl, faucet.address, wallet);

const openedClient = (client as unknown) as PrivateCosmosClient;
const blockLatestSpy = spyOn(openedClient.lcdClient, "blocksLatest").and.callThrough();
Expand All @@ -51,7 +50,7 @@ describe("SigningCosmosClient", () => {
it("works", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
const client = new SigningCosmosClient(httpUrl, faucet.address, wallet);
const client = SigningCosmosClient.fromOfflineSigner(httpUrl, faucet.address, wallet);

// instantiate
const transferAmount: readonly Coin[] = [
Expand Down Expand Up @@ -79,26 +78,27 @@ describe("SigningCosmosClient", () => {
});
});

describe("signAndBroadcast", () => {
it("works", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
const client = new SigningCosmosClient(httpUrl, faucet.address, wallet);
// TODO: move this test into InProcessOnlineSigner
// describe("signAndBroadcast", () => {
// it("works", async () => {
// pendingWithoutWasmd();
// const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
// const client = SigningCosmosClient.fromOfflineSigner(httpUrl, faucet.address, wallet);

const msg: MsgDelegate = {
type: "cosmos-sdk/MsgDelegate",
value: {
delegator_address: faucet.address,
validator_address: validatorAddress,
amount: coin(1234, "ustake"),
},
};
const fee = {
amount: coins(2000, "ucosm"),
gas: "180000", // 180k
};
const result = await client.signAndBroadcast([msg], fee, "Use your power wisely");
assertIsBroadcastTxSuccess(result);
});
});
// const msg: MsgDelegate = {
// type: "cosmos-sdk/MsgDelegate",
// value: {
// delegator_address: faucet.address,
// validator_address: validatorAddress,
// amount: coin(1234, "ustake"),
// },
// };
// const fee = {
// amount: coins(2000, "ucosm"),
// gas: "180000", // 180k
// };
// const result = await client.signAndBroadcast([msg], fee, "Use your power wisely");
// assertIsBroadcastTxSuccess(result);
// });
// });
});
2 changes: 1 addition & 1 deletion packages/launchpad/src/signingcosmosclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class SigningCosmosClient extends CosmosClient {
msgs: [sendMsg],
chainId: await this.getChainId(),
memo: memo,
fees: this.fees.send,
fee: this.fees.send,
};
return this.signer.signAndSubmit(this.senderAddress, request);
}
Expand Down
29 changes: 25 additions & 4 deletions packages/launchpad/types/onlinesigner.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BroadcastTxResult } from "./cosmosclient";
import { AuthExtension, BroadcastMode, LcdClient } from "./lcdapi";
import { Msg } from "./msgs";
import { StdFee } from "./types";
import { AccountData } from "./wallet";
import { AccountData, OfflineSigner } from "./wallet";
export interface SignRequest {
readonly msgs: readonly Msg[];
readonly chainId: string;
Expand All @@ -21,9 +23,28 @@ export interface OnlineSigner {
* Signs with whichever key corresponds to provided bech32-encoded address. Rejects if not enabled.
* Will query chain for account_number and sequence number if not set by caller.
* Will auto-set fee if not set by caller
* Will submit to the blockchain and return the TxHash (hex encoded) if it passes `CheckTx` (else error)
*
* TODO: Use BroadcastTxResult as return value?
* Will submit to the blockchain and return the BroadcastTxResult (even when rejected by the chain)
* Promise will only error on network connectivity issues, not on rejected tx.
*/
readonly signAndSubmit: (address: string, request: SignRequest) => Promise<string>;
readonly signAndSubmit: (address: string, request: SignRequest) => Promise<BroadcastTxResult>;
}
export declare class InProcessOnlineSigner implements OnlineSigner {
protected readonly signer: OfflineSigner;
protected readonly lcdClient: LcdClient & AuthExtension;
/**
* This is a default implementation of an OnlineSigner that
* takes and OfflineSigner and an LcdEndpoint in order to provide all needed functionality
* for signing.
*
* @param signer An OfflineSigner that holds the keys
* @param apiUrl The URL of a Cosmos SDK light client daemon API (sometimes called REST server or REST API)
* @param broadcastMode Defines at which point of the transaction processing the broadcastTx method returns
*/
constructor(signer: OfflineSigner, apiUrl: string, broadcastMode?: BroadcastMode);
enable(): Promise<boolean>;
getAccounts(): Promise<readonly AccountData[]>;
signAndSubmit(address: string, request: SignRequest): Promise<BroadcastTxResult>;
private broadcastTx;
private getSequence;
}
16 changes: 9 additions & 7 deletions packages/launchpad/types/signingcosmosclient.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Coin } from "./coins";
import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient";
import { BroadcastMode } from "./lcdapi";
import { Msg } from "./msgs";
import { OnlineSigner } from "./onlinesigner";
import { StdFee } from "./types";
import { OfflineSigner } from "./wallet";
/**
Expand All @@ -14,6 +14,13 @@ export declare class SigningCosmosClient extends CosmosClient {
readonly senderAddress: string;
private readonly signer;
private readonly fees;
static fromOfflineSigner(
apiUrl: string,
senderAddress: string,
signer: OfflineSigner,
customFees?: Partial<FeeTable>,
broadcastMode?: BroadcastMode,
): SigningCosmosClient;
/**
* Creates a new client with signing capability to interact with a Cosmos SDK blockchain. This is the bigger brother of CosmosClient.
*
Expand All @@ -29,7 +36,7 @@ export declare class SigningCosmosClient extends CosmosClient {
constructor(
apiUrl: string,
senderAddress: string,
signer: OfflineSigner,
signer: OnlineSigner,
customFees?: Partial<FeeTable>,
broadcastMode?: BroadcastMode,
);
Expand All @@ -40,9 +47,4 @@ export declare class SigningCosmosClient extends CosmosClient {
transferAmount: readonly Coin[],
memo?: string,
): Promise<BroadcastTxResult>;
/**
* Gets account number and sequence from the API, creates a sign doc,
* creates a single signature, assembles the signed transaction and broadcasts it.
*/
signAndBroadcast(msgs: readonly Msg[], fee: StdFee, memo?: string): Promise<BroadcastTxResult>;
}

0 comments on commit 50800f5

Please sign in to comment.