Hi @astro1,
Great questions!
Our particuar example code is based on a contractual relationship between a bank and the government. The idea is that the government needs to collect some kind of tax (or levy) on each banking transaction that is an internal bank transfer. To keep things simple in our example, the government is collecting a fixed amount of tax on each transfer. I would suggest an amount much smaller than 1 ether would be more appropriate, for example any value from 1 wei up to 1 gwei. This amount (which is the amount of tax paid by the Bank, and not the amount transferred from one bank account holder to another) will be automatically transferred from the Bank contract balance to the Government contract balance on each successfully executed transfer transaction. For record keeping purposes, the relevant data for each transfer is also sent from Bank to Government and stored in the government’s transaction log (an array).
Instead of as a tax, another way to interpret this transfer of value between the two contracts could be as payment, or fees, for some kind of record keeping service i.e. the Bank contract pays the Government contract for storing all of its transaction data.
However we interpret this payment from one contract to the other, it demonstrates (albeit in an overly simplistic way) how we can program automatic interactions between individual contracts for the purposes of both data and value transfer. Hopefully, this example shows the potential for these sorts of programmed interactions to be applied to many different use cases.
Yes …
In Solidity, unsigned integers representing Ether values are equivalent to uints of wei e.g.
{value: 1000000000000000000}
/* A value call of 1000000000000000000 Wei = 1000000000 Gwei = 1 Ether
However, Solidity syntax also includes the suffixes ether
and gwei
, which provide us with a useful shorthand. For example, the following 3 alternatives are all equivalent to value calls of 1 ether …
{value: 1000000000000000000}
{value: 1000000000 gwei}
{value: 1 ether}
The suffix wei
is also available, but this would only seem to be worth using in situations where there could be a risk of confusion, because the following 2 alternatives are both equivalent to value calls of 0.05 gwei …
{value: 50000000 wei}
{value: 50000000}
Here is a link to the relevant section in the Solidity documentation:
https://docs.soliditylang.org/en/latest/units-and-global-variables.html#ether-units
I’ve already partly answered this, above. The Ether value we are transferring to the Government contract is a payment made by Bank to Government in respect of the transfer of funds between two individual users of the Bank contract. The transfer itself is an internal transfer within Bank, and the amount
value sent to Government is part of the data which will be added to the Government transaction log as a record of the transfer for which Bank has made a payment (of tax, or a fee for a service).
In any case, the suffixes ether
, gwei
and wei
can only be used after literal numbers. You could reference an unsigned integer stored in a variable, but without the suffix e.g.
uint tax;
function transfer(address recipient, uint amount) public {
// additional code
governmentInstance.addTransaction{value: tax}(msg.sender, recipient, amount);
// additional code
}
Just one final point… If you haven’t already, you’ll find it really helpful to add the following function to both the Bank and Government contracts. It enables you to retrieve the total contract address balance for each contract before and after specific transactions. This is useful for testing and for understanding the movements of Ether at a contract level.
function getContractBalance() public view returns(uint) {
return address(this).balance;
}
Let me know if anything is unclear, or if you have any further questions