2

I'm totally new to programming and I'm trying to code a Smart Contract that can receive funds and transfer them to other addresses through a function. In my code I have a modifier that defines an owner that can call the withdraw/transfer function. I have defined 3 address variables, where the function transfers ETH to. Luckily it works as I want it to.

pragma solidity ^0.7.0;

contract SubscriptionPayment {

// address variable defining the owner 
address public owner = msg.sender
;

// modifier that restricts access to the owner of contract
modifier onlyOwner{
    require(msg.sender == owner);
_;
}
// contract is able to handle ETH
receive() external payable{
}

// function to withdraw restricted to owner 
function withdraw(uint _value) external onlyOwner {
    msg.sender.transfer(_value)
;
}

// define address variables 
address payable public account1Address = 0xF6D461F87BBce30C9D03Ff7a8602156f006E2367 ;
address payable public account2Address = 0xb6a76127EDf7E0B7dfcEd9aDE73Fa8780eC26592 ;
address payable public account3Address = 0x722b95CA56b1C884f574BAE4832f053197Ca3F58 ;

// function to pay all subscriptions
function paySubscriptions() external onlyOwner {
    account1Address.transfer(1000000000000000000);
    account2Address.transfer(1000000000000000000);
    account3Address.transfer(2000000000000000000);
}

My question concerns the paySubscriptions function. Is there any way to execute the transfers to these 3 addresses individually and sequentially? Of course I could just make 3 separate functions to transfer ETH to each of these addresses but that would give me 3 separate functions to call. Is it possible to code that when one function is called, another one is called from within the contract and when this function is called, another one is called from within the contract? If so, I could code one function1 that can be called external and the other 2 functions are called from within the contract after function1 has been called/executed.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
bit-lazy
  • 21
  • 1
  • 3

3 Answers3

0

For a better understanding of your task, write how you would implement it if smart contracts could be written in Java or another language

Mad Jackal
  • 1,219
  • 1
  • 7
  • 9
0

You cold do something as follows:

Define an array of addresses and the amounts that they need transferring:

mapping (address => uint) public balances;
address payable [] public subscribers;

Then loop that mapping making payments for each such as:

 function paySubscribers() public{
       for (uint i=0; i<subscribers.length; i++) {
          address payable currAddress = subscribers[i];
          currAddress.transfer(balances[currAddress]);
      }
    
    }

I would suggest you read this article for a better understanding and more practice.

Looping in solidity

From the article:

In solidity, mapping is very useful to store token value of an address. We have seen it in many contracts and they are normally defined this way:

blockbyblock
  • 115
  • 5
0

is this what you want?

function paySubscription(address receiverAddress, uint256 amount) external onlyOwner {
    receiverAddress.transfer(amount);

}

function payAllSubs(address[] memory receivers, uint256[] amounts) external onlyOwner {
       for (uint i=0; i<receivers.length; i++) {
          address currAddress = receivers[i];
          uint256 amt = amounts[i]
          this.paySubscription(currAddress, amt);
      }
    
}
tomyhomie
  • 344
  • 2
  • 6