Inheritance & External Contracts - Discussion

Hi

I’m having trouble understanding how the passing of values are functioning.
When we write {value: 1 ether} where is the ether coming from? Is it the creator of the contract instance account?
I can’t see the purpose of setting a fixed value to a transaction, and I can’t figure out how to set it to a variable instead.

In your example, the value is added to the contract’s balance. Is this the only way to do it? I think I am missing an important piece of information to get this.

I think I’ve misunderstood something regarding the contact balance versus the balance of the different accounts. How do you add to a contracts balance if it’s not external?

*Edited last line to “contracts balance” instead of “contacts balance”.

Ah! That part was unclear to me! It works now thank you!!

1 Like

Hi @ElCrypto

When we write {value: 1 ether} where is the ether coming from? Is it the creator of the contract instance account?
I can’t see the purpose of setting a fixed value to a transaction, and I can’t figure out how to set it to a variable instead.

Are you talking about web3 or solidity?
Give me an example.

Regards,
Dani

Solidity.

this for an example:
GovernmentInstance.addTransaction{value: 1 ehter}(msg.sender, recipient, amount);

Hi @ElCrypto

When we write {value: 1 ether} where is the ether coming from? Is it the creator of the contract instance account?

The ether is coming from the address that is calling the function.
If I call that function using my wallet, the balance will be subtracted from my wallet and added to the contract address.

I can’t see the purpose of setting a fixed value to a transaction, and I can’t figure out how to set it to a variable instead.

You don’t need to hard code an amount, but you can set a require() so that you ask for a minimum deposit amount.

function deposit () public payable {}

The function above allows anyone to deposit any amount in the contract.

function deposit () public payable {
    require(msg.value > 1 ether);
}

The function above allows anyone to deposit any amount in the contract as long as the value sent is > than 1 ether.

Cheers,
Dani

Hi Dani

Thank you for your reply. I’m trying to do as you suggested. But I get an error when trying to deposit into a contract address (there’s is enough ether on the sending account).

Multisigwallet contract:

    function addDeposit(address payable _from, uint _amount) external payable {
        depositLog.push(Deposit(_from, _amount, depositLog.length));
    }

    function getWalletAddress() external view returns (address payable) {
        address convertContractAddress = address(this);
        address payable payableWalletAddress = address(uint160(convertContractAddress));
        return payableWalletAddress;
    }

From exchange contract:

interface MultiSigWalletInterface{
    function addDeposit(address payable _from, uint _amount) external payable;
    function getWalletAddress() external returns (address);
}

contract Exchange {
    
    MultiSigWalletInterface multisigWalletInstance = MultiSigWalletInterface(0xb3502940731B7a65F9bbDB73369c7729c8197665);
    
    address payable walletAddress;
    
    constructor() {
        walletAddress = address(uint160(multisigWalletInstance.getWalletAddress()));
    }

    
    
    mapping(address => uint) balances;
    
    function depositToWallet() payable external {
        
        walletAddress.transfer(msg.value);

        multisigWalletInstance.addDeposit(msg.sender, msg.value);
    }

Could you please tell me why that is? The error occurs when calling depositToWallet():
walletAddress.transfer(msg.value); ---- at this line.

Edit, I know I am converting address(this) to payable twice. But it shouldn’t make a difference.

Edit 2, set payable to function getWalletAddress() external returns (address payable);
Did not help though.

Hi @ElCrypto

Please paste both contracts fully so that I can try myself.
Will let you know.

Dani

Hi @dan-i dan-i

pragma solidity 0.7.5;

interface MultiSigWalletInterface{
    function addDeposit(address payable _from, uint _amount) external payable;
    function getWalletAddress() external returns (address payable);
}

contract Exchange {
    
    MultiSigWalletInterface multisigWalletInstance = MultiSigWalletInterface(0xb20063D7e4d1334e3D7fF484408C443CBA8Fb265);
    
    address payable walletAddress;
    
    constructor() {
        walletAddress = address(uint160(multisigWalletInstance.getWalletAddress()));
    }

    
    
    mapping(address => uint) balances;
    
    function depositToWallet() payable external {
        
        walletAddress.transfer(msg.value);
        

        multisigWalletInstance.addDeposit(msg.sender, msg.value);
    }
}
pragma solidity 0.7.5;

contract MultiSigWallet {

    struct Deposit {
        address _from;
        uint _amount;
        uint _txId;
    }
    
    Deposit[] depositLog;

    struct withdraw {
        address from;
        uint amount;
        uint txId;
    }
    
    withdraw[] withdrawLog;
    
    function depositToWallet() external payable {
        
    }
    
    function addDeposit(address payable _from, uint _amount) external payable {
        depositLog.push(Deposit(_from, _amount, depositLog.length));
    }

    function getWalletAddress() external view returns (address payable) {
        address convertContractAddress = address(this);
        address payable payableWalletAddress = address(uint160(convertContractAddress));
        return payableWalletAddress;
    }
    
    function getWalletSize() external view returns (uint) {
        return address(this).balance;
    }
    
}

It worked for me:

Can you tell me step by step what you are doing and include screenshots of the issue?

Hi @dan-i

I just need to confirm. Is that when you are calling the function from the Exchange contract?

Please help I am stuck.

Since I have started with the inheretance part my code had an error.

my functions like deposit, withdraw, get balance now says the following in the error.

Syntax error free functions cannot have visibility, function deposit() public payable (uint)

What did I do wrong?

1 Like

Hey @FreeSpirit2222, hope you are well.

Please share your code so we can help you to review it. :face_with_monocle:

You can use the “Preformatted Text” Button to encapsulate any kind of code you want to show.


function formatText(){

let words = “I’m a preformatted Text box, Please use me wisely!”

}

prefromatted_text-animated

Carlos Z.

Hi @thecil

Can you help with this one?

@filip Can you please help with this one?

1 Like

Hey @ElCrypto

Can you tell me step by step what you are doing and include screenshots of the issue?

Carlos Z

  1. What is the base contract?

Each Contract has a parent which is known as the derived class. The Parent Contract is the Base Contract.

  1. Which functions are available for derived contracts?

All of the functions that are available in the contract itself and those it derives from.

  1. What is hierarchical inheritance?

This is where there are multiple levels of inheritance, I.e Parent Contract has Child Contract which also has BabyContract and BabyContract is derived from Parent and Child Contracts.

Hi @thecil

I am trying to transfer to a external contract (MultiSigWallet) by calling a function in another contractract (Exchange).
I get the contract address and then cast it to a payable address. When I try to transfer from the function in Exchange.sol nothing get transferred to the contract.

interface MultiSigWalletInterface{
    function addDeposit(address payable _from, uint _amount) external payable;
    function getWalletAddress() external returns (address payable);
}

contract Exchange {
    
    MultiSigWalletInterface multisigWalletInstance = MultiSigWalletInterface(0x8c6AAc465302d9F6262b2907f00c62350Cf6fD74);
    
    address payable walletAddress;
    
    constructor() {
        walletAddress = multisigWalletInstance.getWalletAddress();
    }

    
    
    mapping(address => uint) balances;
    
    function depositToWallet() payable external {
        
        walletAddress.transfer(msg.value);
        

        multisigWalletInstance.addDeposit(msg.sender, msg.value);
    }
contract MultiSigWallet {
 function depositToWallet() external payable {
        
    }
 }

Good day Carlos

Thank you so much for your feedback. Seems the error was with Remix. I deleted all the code restarted the remix and did the same code again. Seems like everything is working perfectly now.

Thanks a lot for your help.

2 Likes

I got it to work. So nvm. I forgot to cast the address that pays the wallet to payable…

A bit disappointed in the amount of time it took to get help. A fairly simple issue.

1 Like

- What is the base contract?
It’s a derived class also named a parent contract.

- Which functions are available for derived contracts?
All of the functions that are available in the parent contract.

- What is hierarchical inheritance?
Multi-level inheritance is similar to the single level, but the difference is, it has levels of parent-child relationships. The child contract—derived from a parent contract—also acts as a parent contract of another contract.

1 Like