I have two Contracts, Domain and Serial deployed separately.

NOTE: Most of the code is ommited for simplicity.

 contract Serial {
       Domain DOMAIN; //Domain contract is imported and set by another function
       function payToSerial(string memory serialNumber, unit amount) public {
           address toAddr = serialToAddress(serialNumber) //We convert serial number to a valid address
           DOMAIN.transfer(toAddr, amount) //Use transfer() from *Domain* contract

 contract Domain {
       function transfer(address recipient, uint256 amount) public {
          //does transfer under specific conditions,

When I call transfer() from Domain contract it works, sends the funds from my wallet to whichever address I had specified.

However, when I call payToSerial() function which in turn calls the transfer() function from Domain contract (as shown in code) the transaction fails because it is trying to send funds from the Serial contract's address and not my wallet.

Is there way to modify payToSerial() function to cuurumvent this?




I tried to edit payToSerial() to accomplish that (To inherent msg.sender), but it did not work.

function payToSerial(string memory serialNumber, uint amount) public {

    DOMAIN = Domain(msg.sender);
    address toAddr = serialAddresses[serialNumber];
    DOMAIN.transfer(toAddr, amount);

I am not sure if what I am trying to do is even possible, but I wanted to ask in any case.


The obstacle:

No one can create a contract that spends from someone else's wallet. This rule doesn't change when two contracts originate from the same author.

The solution:

There is a trust relationship between the two contracts. Domain cannot spend from Serial's money, but Serial can give Domain money for a specific purpose with an expectation that unused funds will be returned.

"Here's $10. Go to the store and buy me ____, if they have any, and bring me back my change." - codify the rules in Domain's function.

You'll see money going from Serial to Domain, and back (it says "conditional"). It might cause you to reconsider how you modularize your application. In this situation, the key is everyone has to spend from the funds they have in hand.

Hope it helps.

  • Hi, thanks for answering. But I think my question might have been poorly made. I edited it now to further clarify my problem and what is going on.
    – Sky
    Jun 11 '21 at 7:37

The only way to solve it was for the Domain contract to be the one containing PayToSerial function.

  • If Domain were an ERC-20 token you could use the transferFrom function. The token's owner must approve beforehand for it to work. See this question for the difference between transfer and transferFrom ethereum.stackexchange.com/questions/46457/….
    – Ismael
    Jan 10 at 20:54

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.