0

I followed a tutorial and coded this contract:

    // SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract Trust {
    struct Kid {
        uint amount;
        uint maturity;
        bool paid;
    }
    
    mapping(address => Kid) public kids;
    address public admin;
    
    constructor() {
        admin = msg.sender;
    }
    
    function addKid(address kid, uint timeToMaturity) external payable {
        require(msg.sender == admin, 'only admin can add children');
        require(kids[kid].amount == 0, 'this child already exists in the system');
        kids[kid] = Kid(msg.value, block.timestamp + timeToMaturity, false);
    }
    
    function withdraw() external {
        Kid storage kid = kids[msg.sender];
        // if the amount is 0, that means it is one of the default mapping addresses
        require(kid.amount > 0, 'only active and verified kids can withdraw');
        require(kid.maturity <= block.timestamp, 'too early');
        require(kid.paid == false, "that kid's address has already been paid");
        kid.paid = true;
        payable(msg.sender).transfer(kid.amount);
    }
}

My question is: once the admin sends some eth to the Kid struct via the addKid() function where is that eth stored? The flow of the contract is, the deploying address can add kid structs to the contract and simultaneously fund the kid structs with ETH. But, after running a few tests I've noticed that the ETH doesn't show up in the newly added wallet until I run the withdraw() function. Do Smart Contracts have a wallet themselves that can be utilized? Also, if it helps at all I'm using Remix for my development environment.

I'm new to smart contracts but would like to learn more so any tips would be appreciated. Thanks.

0

where is that eth stored?

It's added the contract's balance.

All addresses - whether they're wallet addresses or contract addresses - have an associated balance in the state tree, which is where contract data is written to. (Contracts are differentiated from Externally Owned Accounts [wallets] by the fact that they have a populated storageRoot and codeHash - i.e. the address has some associated code with it.)

For reference, a contract's balance can be accessed inside the contract using:

address(this).balance

See Ethereum block architecture for some useful diagrams.

1
  • 1
    Thank you. This answer is exactly what I was looking for.
    – Tank12
    Apr 17 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.