Ethereum/Solidity - Query Transactions
Asked Answered
R

1

8

Does solidity have the ability to query transactions it puts on the blockchain, based on data in the transaction? By query I mean within solidity code running inside a smart contract (not web3.js running outside a smart contract)

So yesterday my contract executed a down payment transaction, today I want to query any/all blockchain transactions for this smart contract with a transaction type of "dp" or down payment? I want this query code to run or execute inside my smart contract.

I am not after a tx hash, I want the entire tx data for a specific transaction or transactions based on parameters, like transaction type, from address or date/time, etc.

NOTE: I want to query these transactions by executing solidity code inside my smart contract. I DO NOT WANT to use web3.js - please, please, please don't provide an answer with web3.js --- the querying has to be done inside the smart contract in solidity code. I appreciate your help - but if you answer is web3.js then you're really not helping

PLEASE - if anyone has seen examples of querying blockchain transaction in solidity code, please post a link or mention it. It seems very odd that you can post transactions to the BC, but there's not a get or retrieve or query function built into solidity. That seems, well to be honest, so basic.

I found out that while solidity can do event logging, such that all tx's are logged into a special area in the BC -- SADLY -- solidity code executing within a smart contract CAN NOT read these logs https://www.bitdegree.org/learn/solidity-events/

I just don't get it......why, why, why would you have a language that creates transactions but can't read them or query them. It just doesn't make sense to me.

I greatly appreciate answers from those knowledgeable in solidity!! Thank You Vmusic

Roundfaced answered 2/8, 2018 at 1:11 Comment(0)
I
4

This is not possible. Contracts cannot query arbitrary state within the blockchain history.

Your only options are:

  1. Maintain a record of whatever state changes within the contract, and check those states to determine if a previous transaction has take place/current transaction is valid
  2. Use web3 to retrieve the details off chain and pass it as arguments to your contract
  3. Use a service like oraclize to query external data from your smart contract. This will also eventually end up making the query off chain, but you can initiate it within a smart contract and read the results back.
Illiquid answered 2/8, 2018 at 1:20 Comment(4)
Hi Raghav - I'm confused how a transaction changes the state of a smart contract. It might change the balance, but not the state (pending, active, cancelled, etc.)Roundfaced
The balance is a state, one of many. A smart contract can be written to do all sorts of things. Whatever current values and flags it holds are the state of the contract.Illiquid
Hi Raghav --- what about a constant function?? This function queries data but does not change state?Roundfaced
Constant is deprecated. You now have view (can query state, but not write) and pure functions (cannot read or write). However, view functions can also only read variables within your contract, they cannot look up past transactions.Illiquid

© 2022 - 2024 — McMap. All rights reserved.