From da2ce9987e9028f7f4589ff69e707ae6c296f2f8 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 14 Feb 2020 16:29:53 +0100 Subject: [PATCH] Adapt code to BCP changes --- packages/bcp/src/cosmwasmconnection.spec.ts | 25 ++++++--------- packages/bcp/src/cosmwasmconnection.ts | 35 +++++++++++---------- packages/bcp/types/cosmwasmconnection.d.ts | 5 +-- packages/faucet/src/actions/start/start.ts | 6 ++-- packages/faucet/src/faucet.ts | 4 +-- 5 files changed, 36 insertions(+), 39 deletions(-) diff --git a/packages/bcp/src/cosmwasmconnection.spec.ts b/packages/bcp/src/cosmwasmconnection.spec.ts index 91fd0495..f626a3ad 100644 --- a/packages/bcp/src/cosmwasmconnection.spec.ts +++ b/packages/bcp/src/cosmwasmconnection.spec.ts @@ -17,7 +17,6 @@ import { Bech32, Encoding } from "@iov/encoding"; import { HdPaths, Secp256k1HdWallet, UserProfile } from "@iov/keycontrol"; import { assert } from "@iov/utils"; -import { CosmWasmCodec } from "./cosmwasmcodec"; import { CosmWasmConnection, TokenConfiguration } from "./cosmwasmconnection"; import * as testdata from "./testdata.spec"; @@ -121,8 +120,7 @@ describe("CosmWasmConnection", () => { it("displays the chain ID", async () => { pendingWithoutCosmos(); const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultConfig); - const chainId = connection.chainId(); - expect(chainId).toEqual(defaultChainId); + expect(connection.chainId).toEqual(defaultChainId); connection.disconnect(); }); }); @@ -282,12 +280,11 @@ describe("CosmWasmConnection", () => { describe("integration tests", () => { it("can post and get a transaction", async () => { pendingWithoutCosmos(); - const codec = new CosmWasmCodec(defaultPrefix, defaultConfig.bankTokens); const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultConfig); const profile = new UserProfile(); const wallet = profile.addWallet(Secp256k1HdWallet.fromMnemonic(faucetMnemonic)); const faucet = await profile.createIdentity(wallet.id, defaultChainId, faucetPath); - const faucetAddress = codec.identityToAddress(faucet); + const faucetAddress = connection.codec.identityToAddress(faucet); const unsigned = await connection.withDefaultFee({ kind: "bcp/send", @@ -302,8 +299,8 @@ describe("CosmWasmConnection", () => { }, }); const nonce = await connection.getNonce({ address: faucetAddress }); - const signed = await profile.signTransaction(faucet, unsigned, codec, nonce); - const postableBytes = codec.bytesToPost(signed); + const signed = await profile.signTransaction(faucet, unsigned, connection.codec, nonce); + const postableBytes = connection.codec.bytesToPost(signed); const response = await connection.postTx(postableBytes); const { transactionId } = response; const blockInfo = await response.blockInfo.waitFor(info => !isBlockInfoPending(info)); @@ -335,12 +332,11 @@ describe("CosmWasmConnection", () => { it("can post and search for a transaction", async () => { pendingWithoutCosmos(); - const codec = new CosmWasmCodec(defaultPrefix, defaultConfig.bankTokens); const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultConfig); const profile = new UserProfile(); const wallet = profile.addWallet(Secp256k1HdWallet.fromMnemonic(faucetMnemonic)); const faucet = await profile.createIdentity(wallet.id, defaultChainId, faucetPath); - const faucetAddress = codec.identityToAddress(faucet); + const faucetAddress = connection.codec.identityToAddress(faucet); const unsigned = await connection.withDefaultFee({ kind: "bcp/send", @@ -355,8 +351,8 @@ describe("CosmWasmConnection", () => { }, }); const nonce = await connection.getNonce({ address: faucetAddress }); - const signed = await profile.signTransaction(faucet, unsigned, codec, nonce); - const postableBytes = codec.bytesToPost(signed); + const signed = await profile.signTransaction(faucet, unsigned, connection.codec, nonce); + const postableBytes = connection.codec.bytesToPost(signed); const response = await connection.postTx(postableBytes); const { transactionId } = response; const blockInfo = await response.blockInfo.waitFor(info => !isBlockInfoPending(info)); @@ -426,12 +422,11 @@ describe("CosmWasmConnection", () => { it("can send ERC20 tokens", async () => { pendingWithoutCosmos(); - const codec = new CosmWasmCodec(defaultPrefix, defaultConfig.bankTokens, defaultConfig.erc20Tokens); const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultConfig); const profile = new UserProfile(); const wallet = profile.addWallet(Secp256k1HdWallet.fromMnemonic(faucetMnemonic)); const faucet = await profile.createIdentity(wallet.id, defaultChainId, faucetPath); - const faucetAddress = codec.identityToAddress(faucet); + const faucetAddress = connection.codec.identityToAddress(faucet); const recipient = makeRandomAddress(); const unsigned = await connection.withDefaultFee({ @@ -447,8 +442,8 @@ describe("CosmWasmConnection", () => { }, }); const nonce = await connection.getNonce({ address: faucetAddress }); - const signed = await profile.signTransaction(faucet, unsigned, codec, nonce); - const postableBytes = codec.bytesToPost(signed); + const signed = await profile.signTransaction(faucet, unsigned, connection.codec, nonce); + const postableBytes = connection.codec.bytesToPost(signed); const response = await connection.postTx(postableBytes); const blockInfo = await response.blockInfo.waitFor(info => !isBlockInfoPending(info)); expect(blockInfo.state).toEqual(TransactionState.Succeeded); diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index 93e102d1..d8be0c4c 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -27,6 +27,7 @@ import { TransactionId, TransactionQuery, TransactionState, + TxCodec, UnsignedTransaction, } from "@iov/bcp"; import { Encoding, Uint53 } from "@iov/encoding"; @@ -38,16 +39,13 @@ import { Stream } from "xstream"; import { decodeCosmosPubkey, pubkeyToAddress } from "./address"; import { Caip5 } from "./caip5"; +import { CosmWasmCodec } from "./cosmwasmcodec"; import { decodeAmount, parseTxsResponseSigned, parseTxsResponseUnsigned } from "./decode"; import { buildSignedTx } from "./encode"; import { accountToNonce, BankToken, Erc20Token } from "./types"; const { fromAscii } = Encoding; -interface ChainData { - readonly chainId: ChainId; -} - // poll every 0.5 seconds (block time 1s) const defaultPollInterval = 500; @@ -75,15 +73,17 @@ export class CosmWasmConnection implements BlockchainConnection { return new CosmWasmConnection(restClient, cosmWasmClient, chainData, addressPrefix, tokens); } - private static async initialize(cosmWasmClient: CosmWasmClient): Promise { + private static async initialize(cosmWasmClient: CosmWasmClient): Promise { const rawChainId = await cosmWasmClient.chainId(); - return { chainId: Caip5.encode(rawChainId) }; + return Caip5.encode(rawChainId); } + public readonly chainId: ChainId; + public readonly codec: TxCodec; + /** @deprecated everything we use from RestClient should be available in CosmWasmClient */ private readonly restClient: RestClient; private readonly cosmWasmClient: CosmWasmClient; - private readonly chainData: ChainData; private readonly addressPrefix: CosmosAddressBech32Prefix; private readonly bankTokens: readonly BankToken[]; private readonly erc20Tokens: readonly Erc20Token[]; @@ -95,14 +95,15 @@ export class CosmWasmConnection implements BlockchainConnection { private constructor( restClient: RestClient, cosmWasmClient: CosmWasmClient, - chainData: ChainData, + chainId: ChainId, addressPrefix: CosmosAddressBech32Prefix, tokens: TokenConfiguration, ) { // tslint:disable-next-line: deprecation this.restClient = restClient; this.cosmWasmClient = cosmWasmClient; - this.chainData = chainData; + this.chainId = chainId; + this.codec = new CosmWasmCodec(addressPrefix, tokens.bankTokens, tokens.erc20Tokens); this.addressPrefix = addressPrefix; this.bankTokens = tokens.bankTokens; this.feeToken = this.bankTokens.find(() => true); @@ -121,10 +122,6 @@ export class CosmWasmConnection implements BlockchainConnection { return; } - public chainId(): ChainId { - return this.chainData.chainId; - } - public async height(): Promise { // tslint:disable-next-line: deprecation const { block } = await this.restClient.blocksLatest(); @@ -357,8 +354,7 @@ export class CosmWasmConnection implements BlockchainConnection { private parseAndPopulateTxResponseUnsigned( response: TxsResponse, ): ConfirmedTransaction | FailedTransaction { - const chainId = this.chainId(); - return parseTxsResponseUnsigned(chainId, parseInt(response.height, 10), response, this.bankTokens); + return parseTxsResponseUnsigned(this.chainId, parseInt(response.height, 10), response, this.bankTokens); } private async parseAndPopulateTxResponseSigned( @@ -369,7 +365,12 @@ export class CosmWasmConnection implements BlockchainConnection { // TODO: fix const nonce = -1 as Nonce; - const chainId = this.chainId(); - return parseTxsResponseSigned(chainId, parseInt(response.height, 10), nonce, response, this.bankTokens); + return parseTxsResponseSigned( + this.chainId, + parseInt(response.height, 10), + nonce, + response, + this.bankTokens, + ); } } diff --git a/packages/bcp/types/cosmwasmconnection.d.ts b/packages/bcp/types/cosmwasmconnection.d.ts index 7191fbab..0274f4c8 100644 --- a/packages/bcp/types/cosmwasmconnection.d.ts +++ b/packages/bcp/types/cosmwasmconnection.d.ts @@ -19,6 +19,7 @@ import { TokenTicker, TransactionId, TransactionQuery, + TxCodec, UnsignedTransaction, } from "@iov/bcp"; import { Stream } from "xstream"; @@ -44,10 +45,11 @@ export declare class CosmWasmConnection implements BlockchainConnection { tokens: TokenConfiguration, ): Promise; private static initialize; + readonly chainId: ChainId; + readonly codec: TxCodec; /** @deprecated everything we use from RestClient should be available in CosmWasmClient */ private readonly restClient; private readonly cosmWasmClient; - private readonly chainData; private readonly addressPrefix; private readonly bankTokens; private readonly erc20Tokens; @@ -55,7 +57,6 @@ export declare class CosmWasmConnection implements BlockchainConnection { private readonly supportedTokens; private constructor(); disconnect(): void; - chainId(): ChainId; height(): Promise; getToken(searchTicker: TokenTicker): Promise; getAllTokens(): Promise; diff --git a/packages/faucet/src/actions/start/start.ts b/packages/faucet/src/actions/start/start.ts index 9f803cb7..782d98e2 100644 --- a/packages/faucet/src/actions/start/start.ts +++ b/packages/faucet/src/actions/start/start.ts @@ -23,13 +23,13 @@ export async function start(args: ReadonlyArray): Promise { ); console.info(`Connecting to blockchain ${blockchainBaseUrl} ...`); const connection = await connector.establishConnection(); - console.info(`Connected to network: ${connection.chainId()}`); + console.info(`Connected to network: ${connection.chainId}`); // Profile if (!constants.mnemonic) throw new Error("The FAUCET_MNEMONIC environment variable is not set"); const [profile] = await createUserProfile( constants.mnemonic, - connection.chainId(), + connection.chainId, constants.concurrency, true, ); @@ -52,6 +52,6 @@ export async function start(args: ReadonlyArray): Promise { setInterval(async () => faucet.refill(), 60_000); // ever 60 seconds console.info("Creating webserver ..."); - const server = new Webserver(faucet, { nodeUrl: blockchainBaseUrl, chainId: connection.chainId() }); + const server = new Webserver(faucet, { nodeUrl: blockchainBaseUrl, chainId: connection.chainId }); server.start(constants.port); } diff --git a/packages/faucet/src/faucet.ts b/packages/faucet/src/faucet.ts index 6a2cd3fc..0aa9cc66 100644 --- a/packages/faucet/src/faucet.ts +++ b/packages/faucet/src/faucet.ts @@ -54,7 +54,7 @@ export class Faucet { public async send(job: SendJob): Promise { const sendWithFee = await this.connection.withDefaultFee({ kind: "bcp/send", - chainId: this.connection.chainId(), + chainId: this.connection.chainId, sender: this.codec.identityToAddress(job.sender), senderPubkey: job.sender.pubkey, recipient: job.recipient, @@ -113,7 +113,7 @@ export class Faucet { public async refill(): Promise { if (this.logging) { - console.info(`Connected to network: ${this.connection.chainId()}`); + console.info(`Connected to network: ${this.connection.chainId}`); console.info(`Tokens on network: ${(await this.loadTokenTickers()).join(", ")}`); }