Hi @david.maluenda,
Following on from my previous post …
You should already have an event emitted for each transfer transaction in Bank. This should be emitted at the end of the transfer() function, after the transfer has been performed within Bank and the external Government function (addTransaction) has been executed successfully (adding the transfer data to the Government transaction log).
You could potentially add an additional event emitted by the Government contract as additional confirmation of the transaction data added to the transaction log array. But in terms of the transfer amount, and its sender and recipient addresses, this data is already contained in the event emitted by Bank. If the Government’s addTransaction() function failed to add the data to the transaction log, then the transfer() function in Bank would also fail to finish executing, and so its transfer event wouldn’t be emitted anyway. However, emitting the txId
(the transaction’s index position in the array) could be useful in order to call the getTransaction() function and retrieve a specific transaction’s data afterwards. You could add an event declaration to the top of the Government contract, and the corresponding emit statement to the end of the addTransaction() function e.g.
pragma solidity 0.7.5;
contract Government {
struct Transaction {
address from;
address to;
uint amount;
uint txId;
}
event GovTxLog(uint txId); // ADD event declaration
Transaction[] transactionLog;
function addTransaction(address _from, address _to, uint _amount) external {
/* Add a local variable to save transactionLog.length before it increases,
to ensure the txId emitted in the event is the same index number */
uint txId = transactionLog.length; // ADD
// Reference variable txId instead of expression transactionLog.length
transactionLog.push(Transaction(_from, _to, _amount, txId));
emit GovTxLog(txId); // ADD emit statement
}
// etc.
}
Both events will now be emitted (one from each contract) when the transfer() function is executed. There is only one transaction, though, so both logged events will appear together in the logs
of the same transaction receipt generated in the Remix console. However, the event data emitted from the external Government contract is not displayed in the same reader-friendly format as that emitted from Bank — although, if you know what you’re looking for, you can see that both events have been logged, as follows …
[
{ //** Logged GovTxLog event data **//
"from": "0x1bB...7B0b", // Smart contract address (Government)
"data": "0x000...0001", /* Emitted event argument(s) (except if indexed)
txId => 1 (2nd tx logged in array) integer expressed as hexadecimal */
"topics": ["0x964...aa26"] /* 1st topic => Event Signature (hash of
event name and its parameter types). Up to 3 more topics => one for
each indexed argument (but there aren't any in this example) */
},
{ //** logged Transfer event data **//
"from": "0xdda...482d", // Smart contract address (Bank)
"topic": "0xddf...b3ef", // Event Signature
"event": "Transfer", // Event name
"args": { // Logged data, according to the defined event arguments
// Same data given twice (i) by index (ii) by argument name
"0": "0x5B3...ddC4", // Sender's address
"1": "0xAb8...5cb2", // Recipient's address
"2": "2000000000000000000", // Amount transferred (always in wei)
"from": "0x5B3...ddC4", // Sender's address (repeated)
"to": "0xAb8...5cb2", // Recipient's address (repeated)
"amount": "2000000000000000000" // Amount transferred (repeated)
}
}
]
Just let me know if you have any questions