Adapt code to BCP changes

This commit is contained in:
Simon Warta 2020-02-14 16:29:53 +01:00
parent a12ceb8916
commit da2ce9987e
5 changed files with 36 additions and 39 deletions

View File

@ -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<SendTransaction>({
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<SendTransaction>({
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<SendTransaction>({
@ -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);

View File

@ -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<ChainData> {
private static async initialize(cosmWasmClient: CosmWasmClient): Promise<ChainId> {
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<number> {
// 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<UnsignedTransaction> | 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,
);
}
}

View File

@ -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<CosmWasmConnection>;
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<number>;
getToken(searchTicker: TokenTicker): Promise<Token | undefined>;
getAllTokens(): Promise<readonly Token[]>;

View File

@ -23,13 +23,13 @@ export async function start(args: ReadonlyArray<string>): Promise<void> {
);
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<string>): Promise<void> {
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);
}

View File

@ -54,7 +54,7 @@ export class Faucet {
public async send(job: SendJob): Promise<void> {
const sendWithFee = await this.connection.withDefaultFee<SendTransaction>({
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<void> {
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(", ")}`);
}