Reputation: 839
Is it possible for smart contract to view past transactions done on it, in other words would it be possible for a smart contract to see who has sent it Ether since its inception?
Upvotes: 2
Views: 1938
Reputation: 128
If you are trying to read a transactions with your smart contract form the blockchain the answers is "No" in the solidity lenguage doesn't exist this. You can do it with Web3.js with the example of Rob or saveing the logs of transactiosn in external database this is like this for the complexity to check a lot of blocks and a lot of transactions and the asyncrhonous problems.
In other case, you can use a external api for check the transactions and have a history, for examples: https://etherscan.io/apis
Upvotes: 1
Reputation: 1059
It would be possible if the Smart Contract has a data structure to record those activities as they happen.
Hastily prepared example:
pragma solidity ^0.4.6;
contract TrackPayments {
struct PaymentStruct {
address sender;
uint amount;
}
// look up the struct with payment details using the unique key for each payment
mapping(bytes32 => PaymentStruct) public paymentStructs;
// payment keys in order received
bytes32[] public paymentKeyList;
event LogPaymentReceived(address sender, uint amount);
function payMe() public payable returns(bool success) {
if(msg.value==0) throw;
// make a unique key ...
bytes32 newKey = sha3(msg.sender, paymentKeyList.length);
paymentStructs[newKey].sender = msg.sender;
paymentStructs[newKey].amount = msg.value;
paymentKeyList.push(newKey);
LogPaymentReceived(msg.sender, msg.value);
return true;
}
function getPaymentCount() public constant returns(uint paymentCount) { return paymentKeyList.length; }
}
This could be a little more storage-efficient at the cost of sacrificing either sequential or random access. This way does both.
Hope it helps.
Upvotes: 2