Hi Micky,
I’ve cut the following Government contract from your post in the Inheritance Assignment topic, and pasted it here. I know they are all one project, but I just want to avoid code and discussion involving the external Government contract appearing in a thread related to an earlier part of the course, where external contract instances, external function calls, and interfaces haven’t been introduced yet.
My feedback on this particular contract is below your code.
pragma solidity 0.7.5;
contract Goverment {
struct Transaction {
address from;
address to;
uint amount;
uint txId;
}
Transaction[] transactionLog;
function addTransaction(address _from, address _to, uint _amount) external payable {
transactionLog.push( Transaction(_from, _to, _amount, transactionLog.length) );
}
function getTransaction(uint _index) public returns(address, address, uint) {
return(transactionLog[_index].from, transactionLog[_index].to, transactionLog[_index].amount);
}
}
Just a couple of other minor points to mention here:
- You should be getting an orange warning in the compiler, indicating that you should restrict the getTransaction function to
view
. If a function doesn’t need to change the contract state, it’s always safer to restrict it toview
(so it can read the contract state, but not change it). Similarly if a function doesn’t need to read or change the contract state, it’s safer to restrict it topure
. - Also in the getTransaction function, I would add names to the 2 address values returned, so that it’s clear which is the sender’s address and which is the recipient’s address.