Skip to content

Commit

Permalink
Make Cosmos connection nonce population DRYer
Browse files Browse the repository at this point in the history
  • Loading branch information
willclarktech committed Oct 29, 2019
1 parent e2ce609 commit a75cf2c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
31 changes: 14 additions & 17 deletions packages/iov-cosmos/src/cosmosconnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { Stream } from "xstream";

import { CosmosBech32Prefix, pubkeyToAddress } from "./address";
import { decodeAmount, parseTxsResponse } from "./decode";
import { RestClient } from "./restclient";
import { RestClient, TxsResponse } from "./restclient";

const { fromBase64 } = Encoding;

Expand Down Expand Up @@ -166,13 +166,8 @@ export class CosmosConnection implements BlockchainConnection {
): Promise<(ConfirmedAndSignedTransaction<UnsignedTransaction>) | FailedTransaction> {
try {
const response = await this.restClient.txsById(id);
const sender = (response.tx.value as any).msg[0].value.from_address;
const chainId = await this.chainId();
const currentHeight = await this.height();
const accountForHeight = await this.restClient.authAccounts(sender, response.height);
const nonce = (parseInt(accountForHeight.result.value.sequence, 10) - 1) as Nonce;

return parseTxsResponse(chainId, currentHeight, nonce, response);
return this.parseAndPopulateTxResponse(response, chainId);
} catch (error) {
if (error.response.status === 404) {
throw new Error("Transaction does not exist");
Expand All @@ -198,16 +193,8 @@ export class CosmosConnection implements BlockchainConnection {
): Promise<readonly (ConfirmedTransaction<LightTransaction> | FailedTransaction)[]> {
const queryString = buildQueryString(query);
const chainId = await this.chainId();
const currentHeight = await this.height();
const { txs } = await this.restClient.txs(queryString);
return Promise.all(
txs.map(async tx => {
const sender = (tx.tx.value as any).msg[0].value.from_address;
const accountForHeight = await this.restClient.authAccounts(sender, tx.height);
const nonce = (parseInt(accountForHeight.result.value.sequence, 10) - 1) as Nonce;
return parseTxsResponse(chainId, currentHeight, nonce, tx);
}),
);
const { txs: responses } = await this.restClient.txs(queryString);
return Promise.all(responses.map(response => this.parseAndPopulateTxResponse(response, chainId)));
}

public listenTx(
Expand Down Expand Up @@ -240,4 +227,14 @@ export class CosmosConnection implements BlockchainConnection {
fee: await this.getFeeQuote(tx),
};
}

private async parseAndPopulateTxResponse(
response: TxsResponse,
chainId: ChainId,
): Promise<(ConfirmedAndSignedTransaction<UnsignedTransaction>) | FailedTransaction> {
const sender = (response.tx.value as any).msg[0].value.from_address;
const accountForHeight = await this.restClient.authAccounts(sender, response.height);
const nonce = (parseInt(accountForHeight.result.value.sequence, 10) - 1) as Nonce;
return parseTxsResponse(chainId, parseInt(response.height, 10), nonce, response);
}
}
1 change: 1 addition & 0 deletions packages/iov-cosmos/types/cosmosconnection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ export declare class CosmosConnection implements BlockchainConnection {
liveTx(query: TransactionQuery): Stream<ConfirmedTransaction<LightTransaction> | FailedTransaction>;
getFeeQuote(tx: UnsignedTransaction): Promise<Fee>;
withDefaultFee<T extends UnsignedTransaction>(tx: T): Promise<T>;
private parseAndPopulateTxResponse;
}

0 comments on commit a75cf2c

Please sign in to comment.