cosmjs icon indicating copy to clipboard operation
cosmjs copied to clipboard

Decode transactions list - javascript

Open pedroR opened this issue 3 years ago • 4 comments

Hello

I am trying to get a list of transaction of a given wallet address. At this moment I can get the block code, hash and status code. However I would like to present more detailed information such as the time of transaction and so on. But I am struggling with decode some information.

Here's some code. My problem is in the _decodeTx function.

const client: SigningStargateClient = await SigningStargateClient.connectWithSigner(constants.rpcEndpoint, currentWallet);

			const  transaction = client.searchTx({sentFromOrTo: walletAddress}).then((res: readonly IndexedTx[]) => {

				const parsedRes: IndexedTx[] = [...res];

				const parsedTransaction = _convertToTransactionList(parsedRes);
				console.log('### ', parsedTransaction);
				setTransactionList(parsedTransaction);
			}).catch(......);

const _convertToTransactionList = (transactionListData: IndexedTx[]): TransactionItem[] => {
		const result: ITransactionItem[] = [];

		for (let i = 0; i<transactionListData.length; i++){
			const item = transactionListData[i];
			const itemParsed: ITransactionItem = {
				block: item.height,
				hash: item.hash,
				code: _parseTransactionStatus(item.code), // returns a string 
				test: _decodeTx(item.tx)
			}
			result.push(itemParsed);
		}
		return result;
	}

const _decodeTx = (tx: any): any => {
		const decodedObject: any = decodeTxRaw(tx);

		/*const utf8decoder = new TextDecoder();

		const messagesDecoded = utf8decoder.decode(decodedObject.body.messages[0].value);
		const signatures = utf8decoder.decode(decodedObject.signatures[0]);
               */
		--> This approach is returning very weired characters which I can't decode properly

		// const signerInfoDecoded = decodeTxRaw(decodedObject.authInfo.signerInfos[0].publicKey.value);
               --->> Returns an error: " Error: invalid wire type 6 at offset 16"


		return decodedObject;

	}

How can I decode the details of a transaction properly?
Thanks

pedroR avatar Feb 21 '22 12:02 pedroR

For message decoding you need a Registry. This has come up a number of times in questions and is not so easy.

Why do you use tx: any? IndexedTx.tx is always of type Uint8Array.

webmaster128 avatar Feb 21 '22 12:02 webmaster128

In https://github.com/cosmos/cosmjs/issues/879#issuecomment-927929727 and following comments you should find the answer. Sorry this is not documented nicely.

webmaster128 avatar Feb 21 '22 13:02 webmaster128

Hi @webmaster128 thanks for the quick reply.

I saw the issue you refered. It's very similar to my case. But I got a question related to the query parameters I should pass. Because if I use the url as it is on the issue the txs returns empty

{
  "jsonrpc": "2.0",
  "id": -1,
  "result": {
    "txs": [],
    "total_count": "0"
  }
}

Can you please point me where I can find the acceptable query parameters?

pedroR avatar Feb 21 '22 14:02 pedroR

sentFromOrTo only works if transaction indexing is enabled on the node.

It is probably easier for you to query transactions by height to get the decoding work. Then you can debug the empty result.

webmaster128 avatar Feb 21 '22 16:02 webmaster128