From 8c68e66b9736166fdd93af2f7e06f339057cea9d Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 11 Feb 2020 11:36:06 +0100 Subject: [PATCH] Use CosmWasmClient in CosmWasmConnection --- packages/bcp/src/cosmwasmconnection.ts | 47 +++++++++++++++------- packages/bcp/types/cosmwasmconnection.d.ts | 2 + 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index 03d12e2c..a659d89f 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -1,5 +1,12 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { CosmosAddressBech32Prefix, RestClient, TxsResponse, types, unmarshalTx } from "@cosmwasm/sdk"; +import { + CosmosAddressBech32Prefix, + CosmWasmClient, + RestClient, + TxsResponse, + types, + unmarshalTx, +} from "@cosmwasm/sdk"; import { Account, AccountQuery, @@ -86,16 +93,19 @@ export class CosmWasmConnection implements BlockchainConnection { tokens: TokenConfiguration, ): Promise { const restClient = new RestClient(url); - const chainData = await this.initialize(restClient); - return new CosmWasmConnection(restClient, chainData, addressPrefix, tokens); + const cosmWasmClient = CosmWasmClient.makeReadOnly(url); + const chainData = await this.initialize(cosmWasmClient); + return new CosmWasmConnection(restClient, cosmWasmClient, chainData, addressPrefix, tokens); } - private static async initialize(restClient: RestClient): Promise { - const { node_info } = await restClient.nodeInfo(); - return { chainId: Caip5.encode(node_info.network) }; + private static async initialize(cosmWasmClient: CosmWasmClient): Promise { + const rawChainId = await cosmWasmClient.chainId(); + return { chainId: Caip5.encode(rawChainId) }; } + /** @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[]; @@ -107,11 +117,14 @@ export class CosmWasmConnection implements BlockchainConnection { private constructor( restClient: RestClient, + cosmWasmClient: CosmWasmClient, chainData: ChainData, addressPrefix: CosmosAddressBech32Prefix, tokens: TokenConfiguration, ) { + // tslint:disable-next-line: deprecation this.restClient = restClient; + this.cosmWasmClient = cosmWasmClient; this.chainData = chainData; this.addressPrefix = addressPrefix; this.bankTokens = tokens.bankTokens; @@ -136,6 +149,7 @@ export class CosmWasmConnection implements BlockchainConnection { } public async height(): Promise { + // tslint:disable-next-line: deprecation const { block } = await this.restClient.blocksLatest(); return block.header.height; } @@ -150,6 +164,7 @@ export class CosmWasmConnection implements BlockchainConnection { public async identifier(signed: PostableBytes): Promise { const tx = unmarshalTx(signed); + // tslint:disable-next-line: deprecation const bytes = await this.restClient.encodeTx(tx); const hash = new Sha256(bytes).digest(); return toHex(hash).toUpperCase() as TransactionId; @@ -157,6 +172,7 @@ export class CosmWasmConnection implements BlockchainConnection { public async getAccount(query: AccountQuery): Promise { const address = isPubkeyQuery(query) ? pubkeyToAddress(query.pubkey, this.addressPrefix) : query.address; + // tslint:disable-next-line: deprecation const { result } = await this.restClient.authAccounts(address); const bankAccount = result.value; const hasBankAccount = !!bankAccount.address; @@ -168,6 +184,7 @@ export class CosmWasmConnection implements BlockchainConnection { this.erc20Tokens.map( async (erc20): Promise => { const queryMsg = { balance: { address: address } }; + // tslint:disable-next-line: deprecation const smart = await this.restClient.queryContractSmart(erc20.contractAddress, queryMsg); const response = JSON.parse(fromAscii(smart)); const normalizedBalance = new BN(response.balance).toString(); @@ -203,9 +220,8 @@ export class CosmWasmConnection implements BlockchainConnection { public async getNonce(query: AddressQuery | PubkeyQuery): Promise { const address = isPubkeyQuery(query) ? pubkeyToAddress(query.pubkey, this.addressPrefix) : query.address; - const { result } = await this.restClient.authAccounts(address); - const { account_number, sequence } = result.value; - return accountToNonce(account_number, sequence); + const { accountNumber, sequence } = await this.cosmWasmClient.getNonce(address); + return accountToNonce(accountNumber, sequence); } public async getNonces(query: AddressQuery | PubkeyQuery, count: number): Promise { @@ -219,6 +235,7 @@ export class CosmWasmConnection implements BlockchainConnection { } public async getBlockHeader(height: number): Promise { + // tslint:disable-next-line: deprecation const { block_meta } = await this.restClient.blocks(height); return { id: block_meta.block_id.hash as BlockId, @@ -236,6 +253,7 @@ export class CosmWasmConnection implements BlockchainConnection { id: TransactionId, ): Promise | FailedTransaction> { try { + // tslint:disable-next-line: deprecation const response = await this.restClient.txsById(id); const chainId = this.chainId(); return this.parseAndPopulateTxResponse(response, chainId); @@ -248,11 +266,8 @@ export class CosmWasmConnection implements BlockchainConnection { } public async postTx(tx: PostableBytes): Promise { - const { code, txhash, raw_log } = await this.restClient.postTx(tx); - if (code) { - throw new Error(raw_log); - } - const transactionId = txhash as TransactionId; + const { transactionHash, rawLog } = await this.cosmWasmClient.postTx(tx); + const transactionId = transactionHash as TransactionId; const firstEvent: BlockInfo = { state: TransactionState.Pending }; let blockInfoInterval: NodeJS.Timeout; let lastEventSent: BlockInfo; @@ -285,7 +300,7 @@ export class CosmWasmConnection implements BlockchainConnection { return { blockInfo: new ValueAndUpdates(producer), transactionId: transactionId, - log: raw_log, + log: rawLog, }; } @@ -295,6 +310,7 @@ export class CosmWasmConnection implements BlockchainConnection { const queryString = buildQueryString(query); const chainId = this.chainId(); // TODO: we need pagination support + // tslint:disable-next-line: deprecation const response = await this.restClient.txs(queryString + "&limit=50"); const { txs } = response; return Promise.all(txs.map(tx => this.parseAndPopulateTxResponse(tx, chainId))); @@ -354,6 +370,7 @@ export class CosmWasmConnection implements BlockchainConnection { throw new Error(`Got unsupported type of message: ${firstMsg.type}`); } + // tslint:disable-next-line: deprecation const accountForHeight = await this.restClient.authAccounts(senderAddress, response.height); // this is technically not the proper nonce. maybe this causes issues for sig validation? // leaving for now unless it causes issues diff --git a/packages/bcp/types/cosmwasmconnection.d.ts b/packages/bcp/types/cosmwasmconnection.d.ts index 5b98a5fa..6d15edea 100644 --- a/packages/bcp/types/cosmwasmconnection.d.ts +++ b/packages/bcp/types/cosmwasmconnection.d.ts @@ -43,7 +43,9 @@ export declare class CosmWasmConnection implements BlockchainConnection { tokens: TokenConfiguration, ): Promise; private static initialize; + /** @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;